deleteRemoteMedia.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. * StoreRemoteMediaPlugin
  19. *
  20. * @category Plugin
  21. * @package GNUsocial
  22. * @author Diogo Peralta Cordeiro <mail+gnusocial@diogo.site>
  23. * @author Alexei Sorokin
  24. * @copyright 2018-2021 Free Software Foundation, Inc http://www.fsf.org
  25. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  26. */
  27. define('INSTALLDIR', dirname(__DIR__, 3));
  28. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  29. $shortoptions = 'l::a::i';
  30. $longoptions = ['limit=', 'all', 'image'];
  31. $helptext = <<<END_OF_HELP
  32. remove_remote_media.php [options]
  33. Removes remote media. Thumbs will be removed as well.
  34. -l --limit [date] This is a timestamp, format is: yyyy-mm-dd (optional time hh:mm:ss may be provided)
  35. END_OF_HELP;
  36. require_once INSTALLDIR . '/scripts/commandline.inc';
  37. $quiet = have_option('q', 'quiet');
  38. if (!have_option('l', 'limit')) {
  39. echo "You must provide a limit!\n\n";
  40. show_help();
  41. exit(1);
  42. }
  43. $max_date = get_option_value('l', 'limit');
  44. if (empty($max_date)) {
  45. echo "Invalid empty limit!";
  46. exit(1);
  47. }
  48. $fn = new DB_DataObject();
  49. $fn->query(sprintf(
  50. <<<'END'
  51. SELECT file_to_post.file_id
  52. FROM file_to_post
  53. INNER JOIN file ON file_to_post.file_id = file.id
  54. INNER JOIN notice ON file_to_post.post_id = notice.id
  55. WHERE notice.is_local = 0 AND notice.modified <= '%3$s'
  56. GROUP BY file_to_post.file_id
  57. ORDER BY MAX(notice.modified)
  58. END,
  59. $fn->escape($max_date)
  60. ));
  61. while ($fn->fetch()) {
  62. $file = File::getByID($fn->file_id);
  63. $file_info_id = $file->getID();
  64. // Delete current file
  65. $file->delete();
  66. if (!$quiet) {
  67. echo "Deleted file with id: {$file_info_id}\n";
  68. }
  69. }