]> git.mxchange.org Git - friendica.git/blob - src/Worker/RemoveUnusedContacts.php
Continued:
[friendica.git] / src / Worker / RemoveUnusedContacts.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2024, 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\Contact\Avatar;
25 use Friendica\Core\Logger;
26 use Friendica\Core\Protocol;
27 use Friendica\Database\DBA;
28 use Friendica\Database\DBStructure;
29 use Friendica\Model\Contact;
30 use Friendica\Model\Photo;
31 use Friendica\Util\DateTimeFormat;
32
33 /**
34  * Removes public contacts that aren't in use
35  */
36 class RemoveUnusedContacts
37 {
38         public static function execute()
39         {
40                 $condition = ["`id` != ? AND `uid` = ? AND NOT `self` AND NOT `nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` != ?)
41                         AND (NOT `network` IN (?, ?, ?, ?, ?, ?) OR (`archive` AND `success_update` < ?))
42                         AND NOT `id` IN (SELECT `author-id` FROM `post-user` WHERE `author-id` = `contact`.`id`)
43                         AND NOT `id` IN (SELECT `owner-id` FROM `post-user` WHERE `owner-id` = `contact`.`id`)
44                         AND NOT `id` IN (SELECT `causer-id` FROM `post-user` WHERE `causer-id` IS NOT NULL AND `causer-id` = `contact`.`id`)
45                         AND NOT `id` IN (SELECT `cid` FROM `post-tag` WHERE `cid` = `contact`.`id`)
46                         AND NOT `id` IN (SELECT `contact-id` FROM `post-user` WHERE `contact-id` = `contact`.`id`)
47                         AND NOT `id` IN (SELECT `cid` FROM `user-contact` WHERE `cid` = `contact`.`id`)
48                         AND NOT `id` IN (SELECT `cid` FROM `event` WHERE `cid` = `contact`.`id`)
49                         AND NOT `id` IN (SELECT `contact-id` FROM `group_member` WHERE `contact-id` = `contact`.`id`)
50                         AND `created` < ?",
51                         0, 0, 0, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::FEED, Protocol::MAIL, Protocol::ACTIVITYPUB, DateTimeFormat::utc('now - 365 days'), DateTimeFormat::utc('now - 30 days')];
52
53                 $total = DBA::count('contact', $condition);
54                 Logger::notice('Starting removal', ['total' => $total]);
55                 $count = 0;
56                 $contacts = DBA::select('contact', ['id', 'uid', 'photo', 'thumb', 'micro'], $condition);
57                 while ($contact = DBA::fetch($contacts)) {
58                         Photo::delete(['uid' => $contact['uid'], 'contact-id' => $contact['id']]);
59                         Avatar::deleteCache($contact);
60
61                         if (DBStructure::existsTable('thread')) {
62                                 DBA::delete('thread', ['owner-id' => $contact['id']]);
63                                 DBA::delete('thread', ['author-id' => $contact['id']]);
64                         }
65                         if (DBStructure::existsTable('item')) {
66                                 DBA::delete('item', ['owner-id' => $contact['id']]);
67                                 DBA::delete('item', ['author-id' => $contact['id']]);
68                                 DBA::delete('item', ['causer-id' => $contact['id']]);
69                         }
70
71                         // There should be none entry for the contact in these tables when none was found in "post-user".
72                         // But we want to be sure since the foreign key prohibits deletion otherwise.
73                         DBA::delete('post', ['owner-id' => $contact['id']]);
74                         DBA::delete('post', ['author-id' => $contact['id']]);
75                         DBA::delete('post', ['causer-id' => $contact['id']]);
76
77                         DBA::delete('post-thread', ['owner-id' => $contact['id']]);
78                         DBA::delete('post-thread', ['author-id' => $contact['id']]);
79                         DBA::delete('post-thread', ['causer-id' => $contact['id']]);
80
81                         DBA::delete('post-thread-user', ['owner-id' => $contact['id']]);
82                         DBA::delete('post-thread-user', ['author-id' => $contact['id']]);
83                         DBA::delete('post-thread-user', ['causer-id' => $contact['id']]);
84
85                         Contact::deleteById($contact['id']);
86                         if ((++$count % 1000) == 0) {
87                                 Logger::info('In removal', ['count' => $count, 'total' => $total]);
88                         }
89                 }
90                 DBA::close($contacts);
91                 Logger::notice('Removal done', ['count' => $count, 'total' => $total]);
92         }
93 }