clean_thumbnails.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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', \dirname(__DIR__));
  21. \define('PUBLICDIR', INSTALLDIR . \DIRECTORY_SEPARATOR . 'public');
  22. $shortoptions = 'y::a::f';
  23. $longoptions = ['yes', 'all', 'force'];
  24. $helptext = <<<'END_OF_HELP'
  25. clean_thumbnails.php [options]
  26. Deletes all local thumbnails so they can be regenerated. Also deletes
  27. if the original File object does not exist, even for remote entries.
  28. -y --yes do not wait for confirmation
  29. -a --all delete remote thumbnails
  30. -f --force delete even if we can't regenerate later
  31. Will print '.' for deleted local files and 'x' where File entry was missing.
  32. If the script seems to stop, it is processing correct File_thumbnail entries.
  33. END_OF_HELP;
  34. require_once INSTALLDIR . '/scripts/commandline.inc';
  35. $only_local = !have_option('a', 'all');
  36. if (!have_option('y', 'yes')) {
  37. echo 'About to delete locally generated thumbnails to allow regeneration. Are you sure? [y/N] ';
  38. $response = fgets(\STDIN);
  39. if (mb_strtolower(trim($response)) != 'y') {
  40. echo "Aborting.\n";
  41. exit(0);
  42. }
  43. }
  44. echo 'Deleting';
  45. $thumbs = new File_thumbnail();
  46. $thumbs->find();
  47. while ($thumbs->fetch()) {
  48. try {
  49. $file = $thumbs->getFile();
  50. $is_local = $file->isLocal();
  51. if ($is_local || !$only_local) {
  52. // only delete if we can regenerate it
  53. if (!$is_local && !have_option('f', 'force')) {
  54. try {
  55. $file->getPath();
  56. } catch (Exception $e) {
  57. // We can't regenerate later if we don't have the original.
  58. continue;
  59. }
  60. }
  61. $thumbs->delete();
  62. echo '.';
  63. }
  64. } catch (NoResultException $e) {
  65. // No File object for thumbnail, let's delete the thumbnail entry
  66. $thumbs->delete();
  67. echo 'x';
  68. }
  69. }
  70. echo "\nDONE.\n";