]> git.mxchange.org Git - friendica.git/blob - src/Worker/RemoveUnusedAvatars.php
Issue 10352: Fixing photo entries for contact avatars
[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                 $condition = ["`uid` = ? AND NOT `self` AND NOT `nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` != ?)
38                         AND `id` IN (SELECT `contact-id` FROM `photo`) AND NOT `id` IN (SELECT `author-id` FROM `post-user`)
39                         AND NOT `id` IN (SELECT `owner-id` FROM `post-user`) AND NOT `id` IN (SELECT `causer-id` FROM `post-user`)
40                         AND NOT `id` IN (SELECT `cid` FROM `post-tag`) AND NOT `id` IN (SELECT `contact-id` FROM `post-user`)", 0, 0];
41
42                 $total = DBA::count('contact', $condition);
43                 Logger::notice('Starting removal', ['total' => $total]);
44                 $count = 0;
45                 $contacts = DBA::select('contact', ['id'], $condition);
46                 while ($contact = DBA::fetch($contacts)) {
47                         DBA::update('contact', ['photo' => '', 'thumb' => '', 'micro' => ''], ['id' => $contact['id']]);
48                         Photo::delete(['contact-id' => $contact['id'], 'album' => Photo::CONTACT_PHOTOS]);
49                         if ((++$count % 1000) == 0) {
50                                 if (!Worker::isInMaintenanceWindow()) {
51                                         Logger::notice('We are outside of the maintenance window, quitting');
52                                         return;
53                                 }
54                                 Logger::notice('In removal', ['count' => $count, 'total' => $total]);
55                         }
56                 }
57                 DBA::close($contacts);
58                 Logger::notice('Removal done', ['count' => $count, 'total' => $total]);
59
60                 self::fixPhotoContacts();
61         }
62
63         private static function fixPhotoContacts()
64         {
65                 $total = 0;
66                 $deleted = 0;
67                 $updated1 = 0;
68                 $updated2 = 0;
69                 Logger::notice('Starting contact fix');
70                 $photos = DBA::select('photo', [], ["`uid` = ? AND `contact-id` IN (SELECT `id` FROM `contact` WHERE `uid` != ?)", 0, 0]);
71                 while ($photo = DBA::fetch($photos)) {
72                         $total++;
73                         $photo_contact = Contact::getById($photo['contact-id']);
74                         $resource = Photo::ridFromURI($photo_contact['photo']);
75                         if ($photo['resource-id'] == $resource) {
76                                 $contact = DBA::selectFirst('contact', [], ['nurl' => $photo_contact['nurl'], 'uid' => 0]);
77                                 if (!empty($contact['photo']) && ($contact['photo'] == $photo_contact['photo'])) {
78                                         Logger::notice('Photo updated to public user', ['id' => $photo['id'], 'contact-id' => $contact['id']]);
79                                         DBA::update('photo', ['contact-id' => $contact['id']], ['id' => $photo['id']]);
80                                         $updated1++;
81                                 }
82                         } else {
83                                 $updated = false;
84                                 $contacts = DBA::select('contact', [], ['nurl' => $photo_contact['nurl']]);
85                                 while ($contact = DBA::fetch($contacts)) {
86                                         if ($photo['resource-id'] == Photo::ridFromURI($contact['photo'])) {
87                                                 Logger::notice('Photo updated to given user', ['id' => $photo['id'], 'contact-id' => $contact['id'], 'uid' => $contact['uid']]);
88                                                 DBA::update('photo', ['contact-id' => $contact['id'], 'uid' => $contact['uid']], ['id' => $photo['id']]);
89                                                 $updated = true;
90                                                 $updated2++;
91                                         }
92                                 }               
93                                 DBA::close($contacts);
94                                 if (!$updated) {
95                                         Logger::notice('Photo deleted', ['id' => $photo['id']]);
96                                         Photo::delete(['id' => $photo['id']]);
97                                         $deleted++;
98                                 }
99                         }
100                 }
101                 DBA::close($photos);
102                 Logger::notice('Contact fix done', ['total' => $total, 'updated1' => $updated1, 'updated2' => $updated2, 'deleted' => $deleted]);
103         }
104 }