]> git.mxchange.org Git - friendica.git/commitdiff
Merge pull request #9623 from annando/removed-unused
authorHypolite Petovan <hypolite@mrpetovan.com>
Sun, 6 Dec 2020 00:33:39 +0000 (19:33 -0500)
committerGitHub <noreply@github.com>
Sun, 6 Dec 2020 00:33:39 +0000 (19:33 -0500)
Remove unused contacts

src/Worker/Cron.php
src/Worker/RemoveUnusedContacts.php [new file with mode: 0644]
src/Worker/UpdateContacts.php
src/Worker/UpdateGServers.php

index 18d9de5920a56425b4fb0d15f2e18b475eb19309..be102bca8cb5f5a52aea3afa069c74312f76b273 100644 (file)
@@ -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 (file)
index 0000000..c2a0719
--- /dev/null
@@ -0,0 +1,61 @@
+<?php
+/**
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+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]);
+       }
+}
index bf234d989c0983923434cebe869040708433609a..c1f38a3b43b976b68d04ed4f016176b6cac29ba3 100644 (file)
@@ -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]);
index 486dbc93c225ecfe96d9c96571c7549ba39a85c6..4963273c08474448063887eb27f2addfec8522e6 100644 (file)
@@ -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]);