]> git.mxchange.org Git - friendica.git/blobdiff - src/Worker/RemoveUnusedContacts.php
Added comment
[friendica.git] / src / Worker / RemoveUnusedContacts.php
index c2a0719870c721657f2b1491d5d7201c05f17ca9..40a94baaf59cd01ed1b7eb517cf1dabdbff60634 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2022, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -24,7 +24,9 @@ namespace Friendica\Worker;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Database\DBA;
+use Friendica\Database\DBStructure;
 use Friendica\Model\Photo;
+use Friendica\Util\DateTimeFormat;
 
 /**
  * Removes public contacts that aren't in use
@@ -33,15 +35,14 @@ 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];
+               $condition = ["`id` != ? AND `uid` = ? AND NOT `self` AND NOT `nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` != ?)
+                       AND (NOT `network` IN (?, ?, ?, ?, ?, ?) OR (`archive` AND `success_update` < ?))
+                       AND NOT `id` IN (SELECT `author-id` FROM `post-user`) AND NOT `id` IN (SELECT `owner-id` FROM `post-user`)
+                       AND NOT `id` IN (SELECT `causer-id` FROM `post-user`) AND NOT `id` IN (SELECT `cid` FROM `post-tag`)
+                       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`)
+                       AND `created` < ?",
+                       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')];
 
                $total = DBA::count('contact', $condition);
                Logger::notice('Starting removal', ['total' => $total]);
@@ -49,6 +50,30 @@ class RemoveUnusedContacts
                $contacts = DBA::select('contact', ['id', 'uid'], $condition);
                while ($contact = DBA::fetch($contacts)) {
                        if (Photo::delete(['uid' => $contact['uid'], 'contact-id' => $contact['id']])) {
+                               if (DBStructure::existsTable('thread')) {
+                                       DBA::delete('thread', ['owner-id' => $contact['id']]);
+                                       DBA::delete('thread', ['author-id' => $contact['id']]);
+                               }
+                               if (DBStructure::existsTable('item')) {
+                                       DBA::delete('item', ['owner-id' => $contact['id']]);
+                                       DBA::delete('item', ['author-id' => $contact['id']]);
+                                       DBA::delete('item', ['causer-id' => $contact['id']]);
+                               }
+
+                               // There should be none entry for the contact in these tables when none was found in "post-user".
+                               // But we want to be sure since the foreign key prohibits deletion otherwise.
+                               DBA::delete('post', ['owner-id' => $contact['id']]);
+                               DBA::delete('post', ['author-id' => $contact['id']]);
+                               DBA::delete('post', ['causer-id' => $contact['id']]);
+                               
+                               DBA::delete('post-thread', ['owner-id' => $contact['id']]);
+                               DBA::delete('post-thread', ['author-id' => $contact['id']]);
+                               DBA::delete('post-thread', ['causer-id' => $contact['id']]);
+
+                               DBA::delete('post-thread-user', ['owner-id' => $contact['id']]);
+                               DBA::delete('post-thread-user', ['author-id' => $contact['id']]);
+                               DBA::delete('post-thread-user', ['causer-id' => $contact['id']]);
+
                                DBA::delete('contact', ['id' => $contact['id']]);
                                if ((++$count % 1000) == 0) {
                                        Logger::notice('In removal', ['count' => $count, 'total' => $total]);