c2a0719870c721657f2b1491d5d7201c05f17ca9
[friendica.git/.git] / src / Worker / RemoveUnusedContacts.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\Protocol;
26 use Friendica\Database\DBA;
27 use Friendica\Model\Photo;
28
29 /**
30  * Removes public contacts that aren't in use
31  */
32 class RemoveUnusedContacts
33 {
34         public static function execute()
35         {
36                 $condition = ["`uid` = ? AND NOT `self` AND NOT `nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` != ?)
37                         AND (NOT `network` IN (?, ?, ?, ?, ?, ?) OR (`archive` AND `success_update` < UTC_TIMESTAMP() - INTERVAL ? DAY))
38                         AND NOT `id` IN (SELECT `author-id` FROM `item`) AND NOT `id` IN (SELECT `owner-id` FROM `item`)
39                         AND NOT `id` IN (SELECT `causer-id` FROM `item`) AND NOT `id` IN (SELECT `cid` FROM `post-tag`)
40                         AND NOT `id` IN (SELECT `contact-id` FROM `item`) AND NOT `id` IN (SELECT `author-id` FROM `thread`)
41                         AND NOT `id` IN (SELECT `owner-id` FROM `thread`) AND NOT `id` IN (SELECT `contact-id` FROM `thread`)
42                         AND NOT `id` IN (SELECT `contact-id` FROM `post-user`) AND NOT `id` IN (SELECT `cid` FROM `user-contact`) 
43                         AND NOT `id` IN (SELECT `cid` FROM `event`) AND NOT `id` IN (SELECT `contact-id` FROM `group_member`)",
44                         0, 0, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::FEED, Protocol::MAIL, Protocol::ACTIVITYPUB, 365];
45
46                 $total = DBA::count('contact', $condition);
47                 Logger::notice('Starting removal', ['total' => $total]);
48                 $count = 0;
49                 $contacts = DBA::select('contact', ['id', 'uid'], $condition);
50                 while ($contact = DBA::fetch($contacts)) {
51                         if (Photo::delete(['uid' => $contact['uid'], 'contact-id' => $contact['id']])) {
52                                 DBA::delete('contact', ['id' => $contact['id']]);
53                                 if ((++$count % 1000) == 0) {
54                                         Logger::notice('In removal', ['count' => $count, 'total' => $total]);
55                                 }
56                         }
57                 }
58                 DBA::close($contacts);
59                 Logger::notice('Removal done', ['count' => $count, 'total' => $total]);
60         }
61 }