removeRemoteMedia.php 2.9 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. * StoreRemoteMediaPlugin
  19. *
  20. * @category Plugin
  21. * @package GNUsocial
  22. * @author Diogo Cordeiro <diogo@fc.up.pt>
  23. * @copyright 2018 Free Software Foundation, Inc http://www.fsf.org
  24. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  25. */
  26. define('INSTALLDIR', dirname(__DIR__, 3));
  27. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  28. $shortoptions = 'l::a::i';
  29. $longoptions = ['limit=', 'all', 'image'];
  30. $helptext = <<<END_OF_HELP
  31. remove_remote_media.php [options]
  32. Removes remote media. In most cases, (if not all), an URL will be kept for the original attachment.
  33. In case the attachment is an image its 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. -a --all By default only remote attachments will be deleted, by using this flag you will remove oembed previews and alike
  36. -i --image Remove image only attachments (will ignore oembed previews and alike)
  37. END_OF_HELP;
  38. require_once INSTALLDIR . '/scripts/commandline.inc';
  39. $quiet = have_option('q', 'quiet');
  40. $include_previews = have_option('a', 'all');
  41. $image_only = have_option('i', 'image');
  42. if (!have_option('l', 'limit')) {
  43. echo "You must provide a limit!\n\n";
  44. show_help();
  45. exit(1);
  46. }
  47. $max_date = get_option_value('l', 'limit');
  48. if (empty($max_date)) {
  49. echo "Invalid empty limit!";
  50. exit(1);
  51. }
  52. $fn = new DB_DataObject();
  53. $fn->query(sprintf(
  54. <<<'END'
  55. SELECT file_to_post.file_id
  56. FROM file_to_post
  57. INNER JOIN file ON file_to_post.file_id = file.id
  58. INNER JOIN notice ON file_to_post.post_id = notice.id
  59. WHERE notice.is_local = 0
  60. %1$s%2$sAND notice.modified <= '%3$s'
  61. GROUP BY file_to_post.file_id
  62. ORDER BY MAX(notice.modified)
  63. END,
  64. $image_only ? 'AND file.width IS NOT NULL AND file.height IS NOT NULL ' : '',
  65. $include_previews ? '' : 'AND file.filehash IS NOT NULL ',
  66. $fn->escape($max_date)
  67. ));
  68. while ($fn->fetch()) {
  69. $file = File::getByID($fn->file_id);
  70. $file_info_id = $file->getID();
  71. // Delete current file
  72. $file->delete();
  73. if (!$quiet) {
  74. echo "Deleted file with id: {$file_info_id}\n";
  75. }
  76. }