]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/StoreRemoteMedia/scripts/removeRemoteMedia.php
[StoreRemoteMedia] script removeRemoteMedia.php was trying to remove already removed...
[quix0rs-gnu-social.git] / plugins / StoreRemoteMedia / scripts / removeRemoteMedia.php
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
28 // Script author: Diogo Cordeiro <diogo@fc.up.pt>
29
30 define('INSTALLDIR', realpath(__DIR__ . '/../../..'));
31
32 $shortoptions = 'l::a::i';
33 $longoptions = ['limit=', 'all', 'image'];
34
35 $helptext = <<<END_OF_HELP
36 remove_remote_media.php [options]
37 Removes remote media. In most cases, (if not all), an URL will be kept for the original attachment.
38 In case the attachment is an image its thumbs will be removed as well.
39
40     -l --limit [date]  This is a timestamp, format is: yyyy-mm-dd (optional time hh:mm:ss may be provided)
41     -a --all           By default only remote attachments will be deleted, by using this flag you will remove oembed previews and alike
42     -i --image         Remove image only attachments (will ignore oembed previews and alike)
43
44 END_OF_HELP;
45
46 require_once INSTALLDIR . '/scripts/commandline.inc';
47
48 $quiet = have_option('q', 'quiet');
49 $include_previews = have_option('a', 'all');
50 $image_only = have_option('i', 'image');
51
52 if (!have_option('l', 'limit')) {
53     echo "You must provide a limit!";
54     show_help();
55     exit(1);
56 }
57 $max_date = get_option_value('l', 'limit');
58 if (empty($max_date)) {
59     echo "Invalid empty limit!";
60     exit(1);
61 }
62
63 $query = "
64     SELECT DISTINCT
65         file_to_post.file_id
66     FROM
67         file_to_post
68             INNER JOIN
69         file ON file.id = file_to_post.file_id
70             INNER JOIN
71         notice ON notice.id = file_to_post.post_id
72     WHERE
73         notice.is_local = 0 ";
74
75 $query .= $image_only ? " AND file.width IS NOT NULL AND file.height IS NOT NULL " : "";
76
77 $query .= $include_previews ? "" : " AND file.filehash IS NOT NULL ";
78 $query .= " AND notice.modified <= '{$max_date}' ORDER BY notice.modified ASC";
79
80 $fn = new DB_DataObject();
81 $fn->query($query);
82 while ($fn->fetch()) {
83     $file = File::getByID($fn->file_id);
84     $file_info_id = $file->getID();
85     // Delete current file
86     $file->delete();
87     if (!$quiet) {
88         echo "Deleted file with id: {$file_info_id}\n";
89     }
90 }