From 2b8adf50733da5d55c8846b5407946fa4bf78fbf Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 5 Dec 2020 21:07:48 +0000 Subject: [PATCH 1/2] Remove unused contacts --- src/Worker/Cron.php | 2 + src/Worker/RemoveUnusedContacts.php | 59 +++++++++++++++++++++++++++++ src/Worker/UpdateContacts.php | 5 ++- src/Worker/UpdateGServers.php | 9 +++-- 4 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 src/Worker/RemoveUnusedContacts.php diff --git a/src/Worker/Cron.php b/src/Worker/Cron.php index 18d9de5920..be102bca8c 100644 --- a/src/Worker/Cron.php +++ b/src/Worker/Cron.php @@ -117,6 +117,8 @@ class Cron Worker::add(PRIORITY_LOW, 'CleanItemUri'); + Worker::add(PRIORITY_LOW, 'RemoveUnusedContacts'); + // check upstream version? Worker::add(PRIORITY_LOW, 'CheckVersion'); diff --git a/src/Worker/RemoveUnusedContacts.php b/src/Worker/RemoveUnusedContacts.php new file mode 100644 index 0000000000..383a4485f5 --- /dev/null +++ b/src/Worker/RemoveUnusedContacts.php @@ -0,0 +1,59 @@ +. + * + */ + +namespace Friendica\Worker; + +use Friendica\Core\Logger; +use Friendica\Core\Protocol; +use Friendica\Database\DBA; +use Friendica\Model\Photo; + +/** + * Removes public contacts that aren't in use + */ +class RemoveUnusedContacts { + public static function execute() { + $condition = ["`uid` = ? AND NOT `self` AND NOT `nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` != ?) + AND (NOT `network` IN (?, ?, ?, ?, ?, ?) OR (`archive` AND `success_update` < UTC_TIMESTAMP() - INTERVAL ? DAY)) + AND NOT `id` IN (SELECT `author-id` FROM `item`) AND NOT `id` IN (SELECT `owner-id` FROM `item`) + AND NOT `id` IN (SELECT `causer-id` FROM `item`) AND NOT `id` IN (SELECT `cid` FROM `post-tag`) + AND NOT `id` IN (SELECT `contact-id` FROM `item`) AND NOT `id` IN (SELECT `author-id` FROM `thread`) + AND NOT `id` IN (SELECT `owner-id` FROM `thread`) AND NOT `id` IN (SELECT `contact-id` FROM `thread`) + AND NOT `id` IN (SELECT `contact-id` FROM `post-user`) AND NOT `id` IN (SELECT `cid` FROM `user-contact`) + AND NOT `id` IN (SELECT `cid` FROM `event`) AND NOT `id` IN (SELECT `contact-id` FROM `group_member`)", + 0, 0, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::FEED, Protocol::MAIL, Protocol::ACTIVITYPUB, 365]; + + $total = DBA::count('contact', $condition); + Logger::notice('Starting removal', ['total' => $total]); + $count = 0; + $contacts = DBA::select('contact', ['id', 'uid'], $condition); + while ($contact = DBA::fetch($contacts)) { + if (Photo::delete(['uid' => $contact['uid'], 'contact-id' => $contact['id']])) { + DBA::delete('contact', ['id' => $contact['id']]); + if ((++$count % 1000) == 0) { + Logger::notice('In removal', ['count' => $count, 'total' => $total]); + } + } + } + DBA::close($contacts); + Logger::notice('Removal done', ['count' => $count, 'total' => $total]); + } +} diff --git a/src/Worker/UpdateContacts.php b/src/Worker/UpdateContacts.php index bf234d989c..c1f38a3b43 100644 --- a/src/Worker/UpdateContacts.php +++ b/src/Worker/UpdateContacts.php @@ -74,8 +74,9 @@ class UpdateContacts $count = 0; foreach ($ids as $id) { - Worker::add(PRIORITY_LOW, "UpdateContact", $id); - ++$count; + if (Worker::add(PRIORITY_LOW, "UpdateContact", $id)) { + ++$count; + } } Logger::info('Initiated update for federated contacts', ['count' => $count]); diff --git a/src/Worker/UpdateGServers.php b/src/Worker/UpdateGServers.php index 486dbc93c2..4963273c08 100644 --- a/src/Worker/UpdateGServers.php +++ b/src/Worker/UpdateGServers.php @@ -62,12 +62,15 @@ class UpdateGServers // There are duplicated "url" but not "nurl". So we check both addresses instead of just overwriting them, // since that would mean loosing data. if (!empty($gserver['url'])) { - Worker::add(PRIORITY_LOW, 'UpdateGServer', $gserver['url']); + if (Worker::add(PRIORITY_LOW, 'UpdateGServer', $gserver['url'])) { + $count++; + } } if (!empty($gserver['nurl']) && ($gserver['nurl'] != Strings::normaliseLink($gserver['url']))) { - Worker::add(PRIORITY_LOW, 'UpdateGServer', $gserver['nurl']); + if (Worker::add(PRIORITY_LOW, 'UpdateGServer', $gserver['nurl'])) { + $count++; + } } - $count++; } DBA::close($gservers); Logger::info('Updated servers', ['count' => $count]); From 878a418c6b5585a69a713de662dfbdca2a4a6d90 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 5 Dec 2020 21:58:15 +0000 Subject: [PATCH 2/2] Code standards --- src/Worker/RemoveUnusedContacts.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Worker/RemoveUnusedContacts.php b/src/Worker/RemoveUnusedContacts.php index 383a4485f5..c2a0719870 100644 --- a/src/Worker/RemoveUnusedContacts.php +++ b/src/Worker/RemoveUnusedContacts.php @@ -29,8 +29,10 @@ use Friendica\Model\Photo; /** * Removes public contacts that aren't in use */ -class RemoveUnusedContacts { - public static function execute() { +class RemoveUnusedContacts +{ + public static function execute() + { $condition = ["`uid` = ? AND NOT `self` AND NOT `nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` != ?) AND (NOT `network` IN (?, ?, ?, ?, ?, ?) OR (`archive` AND `success_update` < UTC_TIMESTAMP() - INTERVAL ? DAY)) AND NOT `id` IN (SELECT `author-id` FROM `item`) AND NOT `id` IN (SELECT `owner-id` FROM `item`)