removeRemoteMedia.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * GNU social - a federating social network
  5. *
  6. * GNU Social - StoreRemoteMediaPlugin
  7. *
  8. * LICENCE: This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * @category Plugin
  22. * @package GNUsocial
  23. * @copyright 2018 Free Software Foundation http://fsf.org
  24. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  25. * @link https://www.gnu.org/software/social/
  26. */
  27. // Script author: Diogo Cordeiro <diogo@fc.up.pt>
  28. define('INSTALLDIR', realpath(__DIR__ . '/../../..'));
  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. In most cases, (if not all), an URL will be kept for the original attachment.
  34. In case the attachment is an image its thumbs will be removed as well.
  35. -l --limit [date] This is a timestamp, format is: yyyy-mm-dd (optional time hh:mm:ss may be provided)
  36. -a --all By default only remote attachments will be deleted, by using this flag you will remove oembed previews and alike
  37. -i --image Remove image only attachments (will ignore oembed previews and alike)
  38. END_OF_HELP;
  39. require_once INSTALLDIR . '/scripts/commandline.inc';
  40. $quiet = have_option('q', 'quiet');
  41. $include_previews = have_option('a', 'all');
  42. $image_only = have_option('i', 'image');
  43. if (!have_option('l', 'limit')) {
  44. echo "You must provide a limit!\n\n";
  45. show_help();
  46. exit(1);
  47. }
  48. $max_date = get_option_value('l', 'limit');
  49. if (empty($max_date)) {
  50. echo "Invalid empty limit!";
  51. exit(1);
  52. }
  53. $query = "
  54. SELECT DISTINCT
  55. file_to_post.file_id
  56. FROM
  57. file_to_post
  58. INNER JOIN
  59. file ON file.id = file_to_post.file_id
  60. INNER JOIN
  61. notice ON notice.id = file_to_post.post_id
  62. WHERE
  63. notice.is_local = 0 ";
  64. $query .= $image_only ? " AND file.width IS NOT NULL AND file.height IS NOT NULL " : "";
  65. $query .= $include_previews ? "" : " AND file.filehash IS NOT NULL ";
  66. $query .= " AND notice.modified <= '{$max_date}' ORDER BY notice.modified ASC";
  67. $fn = new DB_DataObject();
  68. $fn->query($query);
  69. while ($fn->fetch()) {
  70. $file = File::getByID($fn->file_id);
  71. $file_info_id = $file->getID();
  72. // Delete current file
  73. $file->delete();
  74. if (!$quiet) {
  75. echo "Deleted file with id: {$file_info_id}\n";
  76. }
  77. }