removeRemoteMedia.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. $query = "
  53. SELECT DISTINCT
  54. file_to_post.file_id
  55. FROM
  56. file_to_post
  57. INNER JOIN
  58. file ON file.id = file_to_post.file_id
  59. INNER JOIN
  60. notice ON notice.id = file_to_post.post_id
  61. WHERE
  62. notice.is_local = 0 ";
  63. $query .= $image_only ? " AND file.width IS NOT NULL AND file.height IS NOT NULL " : "";
  64. $query .= $include_previews ? "" : " AND file.filehash IS NOT NULL ";
  65. $query .= " AND notice.modified <= '{$max_date}' ORDER BY notice.modified ASC";
  66. $fn = new DB_DataObject();
  67. $fn->query($query);
  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. }