]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - scripts/delete_orphan_files.php
Make the Link header give URI for WebFinger lookup
[quix0rs-gnu-social.git] / scripts / delete_orphan_files.php
index ad575187bda9f3c5dc25c593384ac9f5e9e8ad5d..2c49c10a508d468f5e859df0a437bf789d5b63d7 100755 (executable)
@@ -33,12 +33,34 @@ attached files weren't removed as well.
 
 Will print '.' for each deleted File entry and 'x' if it also had a locally stored file.
 
+WARNING WARNING WARNING, this will also delete Qvitter files such as background etc. since
+they are not linked to notices (yet anyway).
+
 END_OF_HELP;
 
 require_once INSTALLDIR.'/scripts/commandline.inc';
 
+print "Finding File entries that are not related to a Notice (or the notice has been deleted)...";
+$file = new File();
+$sql = 'SELECT file.* FROM file'.
+        ' LEFT JOIN file_to_post ON file_to_post.file_id=file.id'.
+        ' WHERE'.
+            ' NOT EXISTS (SELECT file_to_post.file_id FROM file_to_post WHERE file.id=file_to_post.file_id)'.
+            ' OR NOT EXISTS (SELECT notice.id FROM notice WHERE notice.id=file_to_post.post_id)'.
+            ' GROUP BY file.id;';
+
+if ($file->query($sql) !== false) {
+    print " {$file->N} found.\n";
+    if ($file->N == 0) {
+        exit(0);
+    }
+} else {
+    print "FAILED";
+    exit(1);
+}
+
 if (!have_option('y', 'yes')) {
-    print "About to delete local files that are not related to a Notice. Are you sure? [y/N] ";
+    print "About to delete the entries along with locally stored files. Are you sure? [y/N] ";
     $response = fgets(STDIN);
     if (strtolower(trim($response)) != 'y') {
         print "Aborting.\n";
@@ -46,21 +68,16 @@ if (!have_option('y', 'yes')) {
     }
 }
 
-print "Finding File entries...";
-$file = new File();
-$sql = 'SELECT * FROM file WHERE NOT EXISTS (SELECT id FROM file_to_post WHERE file.id=file_id);';
-if ($file->query($sql) !== false) {
-    print " Deleting {$file->N} entries: ";
-    while ($file->fetch()) {
-        try {
-            $file->getPath();
-            $file->delete();
-            print 'x';
-        } catch (Exception $e) {
-            // either FileNotFound exception or ClientException
-            $file->delete();
-            print '.';
-        }
+print "\nDeleting: ";
+while ($file->fetch()) {
+    try {
+        $file->getPath();
+        $file->delete();
+        print 'x';
+    } catch (Exception $e) {
+        // either FileNotFound exception or ClientException
+        $file->delete();
+        print '.';
     }
 }
 print "\nDONE.\n";