From 6f2fbd448d97cd5b14e19905c89918ecad79babb Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Thu, 11 Feb 2016 22:43:26 +0100 Subject: [PATCH] Fixed the delete orphan script to include deleted notices The file_to_post table sometimes had post_id with values that did not exist in the notice table. --- scripts/delete_orphan_files.php | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/scripts/delete_orphan_files.php b/scripts/delete_orphan_files.php index ad575187bd..5e6c2633a8 100755 --- a/scripts/delete_orphan_files.php +++ b/scripts/delete_orphan_files.php @@ -37,20 +37,28 @@ 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 id FROM file'. + ' 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_id)'. + ' OR NOT EXISTS (SELECT notice.id FROM notice WHERE notice.id=post_id);'; + +print " {$file->N} found.\n"; +if ($file->N == 0) { + exit(0); +} 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"; exit(0); } } - -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: "; + print "\nDeleting: "; while ($file->fetch()) { try { $file->getPath(); -- 2.39.5