]> git.mxchange.org Git - friendica.git/commitdiff
Remove unused cached avatar photo entries
authorMichael <heluecht@pirati.ca>
Sun, 20 Dec 2020 06:22:31 +0000 (06:22 +0000)
committerMichael <heluecht@pirati.ca>
Sun, 20 Dec 2020 06:22:31 +0000 (06:22 +0000)
src/Worker/Cron.php
src/Worker/RemoveUnusedAvatars.php [new file with mode: 0644]

index be102bca8cb5f5a52aea3afa069c74312f76b273..1901766d3ac1d4d341f69322388c902ae8704b44 100644 (file)
@@ -119,6 +119,8 @@ class Cron
 
                        Worker::add(PRIORITY_LOW, 'RemoveUnusedContacts');
 
+                       Worker::add(PRIORITY_LOW, 'RemoveUnusedAvatars');
+
                        // check upstream version?
                        Worker::add(PRIORITY_LOW, 'CheckVersion');
 
diff --git a/src/Worker/RemoveUnusedAvatars.php b/src/Worker/RemoveUnusedAvatars.php
new file mode 100644 (file)
index 0000000..0d9c0f6
--- /dev/null
@@ -0,0 +1,60 @@
+<?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\Core\Worker;
+use Friendica\Database\DBA;
+use Friendica\Model\Photo;
+
+/**
+ * Removes cached avatars from public contacts that aren't in use
+ */
+class RemoveUnusedAvatars
+{
+       public static function execute()
+       {
+               $condition = ["`uid` = ? AND NOT `self` AND NOT `nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` != ?)
+                       AND `id` IN (SELECT `contact-id` FROM `photo`) 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`)", 0, 0];
+
+               $total = DBA::count('contact', $condition);
+               Logger::notice('Starting removal', ['total' => $total]);
+               $count = 0;
+               $contacts = DBA::select('contact', ['id'], $condition);
+               while ($contact = DBA::fetch($contacts)) {
+                       DBA::update('contact', ['photo' => '', 'thumb' => '', 'micro' => ''], ['id' => $contact['id']]);
+                       Photo::delete(['contact-id' => $contact['id'], 'album' => Photo::CONTACT_PHOTOS]);
+                       if ((++$count % 1000) == 0) {
+                               if (!Worker::isInMaintenanceWindow()) {
+                                       Logger::notice('We are outside of the maintenance window, quitting');
+                                       return;
+                               }
+                               Logger::notice('In removal', ['count' => $count, 'total' => $total]);
+                       }
+               }
+               DBA::close($contacts);
+               Logger::notice('Removal done', ['count' => $count, 'total' => $total]);
+       }
+}