delete_orphan_files.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env php
  2. <?php
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. /**
  18. * @copyright 2008, 2009 StatusNet, Inc.
  19. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  20. */
  21. define('INSTALLDIR', dirname(__DIR__));
  22. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  23. $shortoptions = 'y';
  24. $longoptions = array('yes');
  25. $helptext = <<<END_OF_HELP
  26. delete_orphan_files.php [options]
  27. Deletes all files and their File entries where there is no link to a
  28. Notice entry. Good for cleaning up after user deletion or so where the
  29. attached files weren't removed as well.
  30. -y --yes do not wait for confirmation
  31. Will print '.' for each deleted File entry and 'x' if it also had a locally stored file.
  32. WARNING WARNING WARNING, this will also delete Qvitter files such as background etc. since
  33. they are not linked to notices (yet anyway).
  34. END_OF_HELP;
  35. require_once INSTALLDIR.'/scripts/commandline.inc';
  36. print "Finding File entries that are not related to a Notice (or the notice has been deleted)...";
  37. $file = new File();
  38. $sql = <<<'END'
  39. SELECT file.*
  40. FROM file_to_post
  41. INNER JOIN notice ON file_to_post.post_id = notice.id
  42. RIGHT JOIN file ON file_to_post.file_id = file.id
  43. WHERE file_to_post.file_id IS NULL;
  44. END;
  45. if ($file->query($sql) !== false) {
  46. print " {$file->N} found.\n";
  47. if ($file->N == 0) {
  48. exit(0);
  49. }
  50. } else {
  51. print "FAILED";
  52. exit(1);
  53. }
  54. if (!have_option('y', 'yes')) {
  55. print "About to delete the entries along with locally stored files. Are you sure? [y/N] ";
  56. $response = fgets(STDIN);
  57. if (strtolower(trim($response)) != 'y') {
  58. print "Aborting.\n";
  59. exit(0);
  60. }
  61. }
  62. print "\nDeleting: ";
  63. while ($file->fetch()) {
  64. try {
  65. $file->getPath();
  66. $file->delete();
  67. print 'x';
  68. } catch (Exception $e) {
  69. // either FileNotFound exception or ClientException
  70. $file->delete();
  71. print '.';
  72. }
  73. }
  74. print "\nDONE.\n";