clean_file_table.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * StatusNet - a distributed open-source microblogging tool
  5. * Copyright (C) 2008, 2009, StatusNet, Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
  21. $shortoptions = 'y';
  22. $longoptions = array('yes');
  23. $helptext = <<<END_OF_HELP
  24. clean_file_table.php [options]
  25. Deletes all local files where the filename cannot be found in the filesystem.
  26. -y --yes do not wait for confirmation
  27. Will print '.' for each file, except for deleted ones which are marked as 'x'.
  28. END_OF_HELP;
  29. require_once INSTALLDIR.'/scripts/commandline.inc';
  30. if (!have_option('y', 'yes')) {
  31. print "About to delete local file entries where the file cannot be found. Are you sure? [y/N] ";
  32. $response = fgets(STDIN);
  33. if (strtolower(trim($response)) != 'y') {
  34. print "Aborting.\n";
  35. exit(0);
  36. }
  37. }
  38. print "Deleting";
  39. $file = new File();
  40. $file->whereAdd('filename IS NOT NULL'); // local files
  41. $file->whereAdd('filehash IS NULL', 'AND'); // without filehash value
  42. if ($file->find()) {
  43. while ($file->fetch()) {
  44. try {
  45. $file->getPath();
  46. print '.';
  47. } catch (FileNotFoundException $e) {
  48. $file->delete();
  49. print 'x';
  50. }
  51. }
  52. }
  53. print "\nDONE.\n";