3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Worker;
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;
31 * Removes cached avatars from public contacts that aren't in use
33 class RemoveUnusedAvatars
35 public static function execute()
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`);";
46 $ret = DBA::fetchFirst("SELECT COUNT(*) AS `total` " . $sql, 0, '', '', '', 0);
47 $total = $ret['total'] ?? 0;
48 Logger::notice('Starting removal', ['total' => $total]);
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');
59 Logger::notice('In removal', ['count' => $count, 'total' => $total]);
62 DBA::close($contacts);
63 Logger::notice('Removal done', ['count' => $count, 'total' => $total]);
65 self::fixPhotoContacts();
66 self::deleteDuplicates();
69 private static function fixPhotoContacts()
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)) {
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']]);
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']]);
99 DBA::close($contacts);
101 Logger::notice('Photo deleted', ['id' => $photo['id']]);
102 Photo::delete(['id' => $photo['id']]);
108 Logger::notice('Contact fix done', ['total' => $total, 'updated1' => $updated1, 'updated2' => $updated2, 'deleted' => $deleted]);
111 private static function deleteDuplicates()
113 $size = [4 => 'photo', 5 => 'thumb', 6 => 'micro'];
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)) {
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']]);
131 Logger::notice('Duplicate removal done', ['total' => $total, 'deleted' => $deleted]);