]> git.mxchange.org Git - friendica.git/blob - src/Worker/RemoveUnusedAvatars.php
API: Added more functions, fixed function names
[friendica.git] / src / Worker / RemoveUnusedAvatars.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Worker;
23
24 use Friendica\Core\Logger;
25 use Friendica\Core\Worker;
26 use Friendica\Database\DBA;
27 use Friendica\Model\Contact;
28 use Friendica\Model\Photo;
29
30 /**
31  * Removes cached avatars from public contacts that aren't in use
32  */
33 class RemoveUnusedAvatars
34 {
35         public static function execute()
36         {
37                 $sql = "FROM `contact` INNER JOIN `photo` ON `contact`.`id` = `contact-id`
38                         WHERE `contact`.`uid` = ? AND NOT `self` AND (`photo` != ? OR `thumb` != ? OR `micro` != ?)
39                                 AND NOT `nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` != ?)
40                                 AND NOT `contact`.`id` IN (SELECT `author-id` FROM `post-user`)
41                                 AND NOT `contact`.`id` IN (SELECT `owner-id` FROM `post-user`)
42                                 AND NOT `contact`.`id` IN (SELECT `causer-id` FROM `post-user`)
43                                 AND NOT `contact`.`id` IN (SELECT `cid` FROM `post-tag`)
44                                 AND NOT `contact`.`id` IN (SELECT `contact-id` FROM `post-user`);";
45
46                 $ret = DBA::fetchFirst("SELECT COUNT(*) AS `total` " . $sql, 0, '', '', '', 0);
47                 $total = $ret['total'] ?? 0;
48                 Logger::notice('Starting removal', ['total' => $total]);
49                 $count = 0;
50                 $contacts = DBA::p("SELECT `contact`.`id` " . $sql, 0, '', '', '', 0);
51                 while ($contact = DBA::fetch($contacts)) {
52                         Contact::update(['photo' => '', 'thumb' => '', 'micro' => ''], ['id' => $contact['id']]);
53                         Photo::delete(['contact-id' => $contact['id'], 'photo-type' => [Photo::CONTACT_AVATAR, Photo::CONTACT_BANNER]]);
54                         if ((++$count % 1000) == 0) {
55                                 if (!Worker::isInMaintenanceWindow()) {
56                                         Logger::notice('We are outside of the maintenance window, quitting');
57                                         return;
58                                 }
59                                 Logger::notice('In removal', ['count' => $count, 'total' => $total]);
60                         }
61                 }
62                 DBA::close($contacts);
63                 Logger::notice('Removal done', ['count' => $count, 'total' => $total]);
64
65                 self::fixPhotoContacts();
66                 self::deleteDuplicates();
67         }
68
69         private static function fixPhotoContacts()
70         {
71                 $total = 0;
72                 $deleted = 0;
73                 $updated1 = 0;
74                 $updated2 = 0;
75                 Logger::notice('Starting contact fix');
76                 $photos = DBA::select('photo', [], ["`uid` = ? AND `contact-id` IN (SELECT `id` FROM `contact` WHERE `uid` != ?) AND `contact-id` != ? AND `scale` IN (?, ?, ?)", 0, 0, 0, 4, 5, 6]);
77                 while ($photo = DBA::fetch($photos)) {
78                         $total++;
79                         $photo_contact = Contact::getById($photo['contact-id']);
80                         $resource = Photo::ridFromURI($photo_contact['photo']);
81                         if ($photo['resource-id'] == $resource) {
82                                 $contact = DBA::selectFirst('contact', [], ['nurl' => $photo_contact['nurl'], 'uid' => 0]);
83                                 if (!empty($contact['photo']) && ($contact['photo'] == $photo_contact['photo'])) {
84                                         Logger::notice('Photo updated to public user', ['id' => $photo['id'], 'contact-id' => $contact['id']]);
85                                         DBA::update('photo', ['contact-id' => $contact['id']], ['id' => $photo['id']]);
86                                         $updated1++;
87                                 }
88                         } else {
89                                 $updated = false;
90                                 $contacts = DBA::select('contact', [], ['nurl' => $photo_contact['nurl']]);
91                                 while ($contact = DBA::fetch($contacts)) {
92                                         if ($photo['resource-id'] == Photo::ridFromURI($contact['photo'])) {
93                                                 Logger::notice('Photo updated to given user', ['id' => $photo['id'], 'contact-id' => $contact['id'], 'uid' => $contact['uid']]);
94                                                 DBA::update('photo', ['contact-id' => $contact['id'], 'uid' => $contact['uid']], ['id' => $photo['id']]);
95                                                 $updated = true;
96                                                 $updated2++;
97                                         }
98                                 }               
99                                 DBA::close($contacts);
100                                 if (!$updated) {
101                                         Logger::notice('Photo deleted', ['id' => $photo['id']]);
102                                         Photo::delete(['id' => $photo['id']]);
103                                         $deleted++;
104                                 }
105                         }
106                 }
107                 DBA::close($photos);
108                 Logger::notice('Contact fix done', ['total' => $total, 'updated1' => $updated1, 'updated2' => $updated2, 'deleted' => $deleted]);
109         }
110
111         private static function deleteDuplicates()
112         {
113                 $size = [4 => 'photo', 5 => 'thumb', 6 => 'micro'];
114
115                 $total = 0;
116                 $deleted = 0;
117                 Logger::notice('Starting duplicate removal');
118                 $photos = DBA::p("SELECT `photo`.`id`, `photo`.`uid`, `photo`.`scale`, `photo`.`album`, `photo`.`contact-id`, `photo`.`resource-id`, `contact`.`photo`, `contact`.`thumb`, `contact`.`micro` FROM `photo` INNER JOIN `contact` ON `contact`.`id` = `photo`.`contact-id` and `photo`.`contact-id` != ? AND `photo`.`scale` IN (?, ?, ?)", 0, 4, 5, 6);
119                 while ($photo = DBA::fetch($photos)) {
120                         $resource = Photo::ridFromURI($photo[$size[$photo['scale']]]);
121                         if ($resource != $photo['resource-id'] && !empty($resource)) {
122                                 $total++;
123                                 if (DBA::exists('photo', ['resource-id' => $resource, 'scale' => $photo['scale']])) {
124                                         Logger::notice('Photo deleted', ['id' => $photo['id']]);
125                                         Photo::delete(['id' => $photo['id']]);
126                                         $deleted++;
127                                 }
128                         }
129                 }
130                 DBA::close($photos);
131                 Logger::notice('Duplicate removal done', ['total' => $total, 'deleted' => $deleted]);
132         }
133 }