3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Worker;
24 use Friendica\Core\Logger;
25 use Friendica\Core\Worker;
26 use Friendica\Database\DBA;
27 use Friendica\Database\DBStructure;
29 use Friendica\Model\Item;
30 use Friendica\Model\Post;
35 * Expire posts and remove unused item-uri entries
39 public static function execute()
41 self::deleteExpiredOriginPosts();
43 self::deleteUnusedItemUri();
45 self::deleteExpiredExternalPosts();
47 // Set the expiry for origin posta
48 Worker::add(PRIORITY_LOW, 'Expire');
50 // update nodeinfo data after everything is cleaned up
51 Worker::add(PRIORITY_LOW, 'NodeInfo');
55 * Delete expired origin posts and orphaned post related table entries
59 private static function deleteExpiredOriginPosts()
61 Logger::info('Delete expired posts');
62 // physically remove anything that has been deleted for more than two months
63 $condition = ["`gravity` = ? AND `deleted` AND `changed` < UTC_TIMESTAMP() - INTERVAL 60 DAY", GRAVITY_PARENT];
64 $rows = Post::select(['guid', 'uri-id', 'uid'], $condition);
65 while ($row = Post::fetch($rows)) {
66 Logger::info('Delete expired item', ['uri-id' => $row['uri-id'], 'guid' => $row['guid']]);
67 if (DBStructure::existsTable('item')) {
68 DBA::delete('item', ['parent-uri-id' => $row['uri-id'], 'uid' => $row['uid']]);
70 Post\User::delete(['parent-uri-id' => $row['uri-id'], 'uid' => $row['uid']]);
74 Logger::info('Deleting orphaned post entries - start');
75 $condition = ["NOT EXISTS (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uri-id` = `post`.`uri-id`)"];
76 DBA::delete('post', $condition);
77 Logger::info('Orphaned post entries deleted', ['rows' => DBA::affectedRows()]);
79 Logger::info('Deleting orphaned post-content entries - start');
80 $condition = ["NOT EXISTS (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uri-id` = `post-content`.`uri-id`)"];
81 DBA::delete('post-content', $condition);
82 Logger::info('Orphaned post-content entries deleted', ['rows' => DBA::affectedRows()]);
84 Logger::info('Deleting orphaned post-thread entries - start');
85 $condition = ["NOT EXISTS (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uri-id` = `post-thread`.`uri-id`)"];
86 DBA::delete('post-thread', $condition);
87 Logger::info('Orphaned post-thread entries deleted', ['rows' => DBA::affectedRows()]);
89 Logger::info('Deleting orphaned post-thread-user entries - start');
90 $condition = ["NOT EXISTS (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uri-id` = `post-thread-user`.`uri-id`)"];
91 DBA::delete('post-thread-user', $condition);
92 Logger::info('Orphaned post-thread-user entries deleted', ['rows' => DBA::affectedRows()]);
94 Logger::info('Delete expired posts - done');
98 * Delete unused item-uri entries
100 private static function deleteUnusedItemUri()
104 // We have to avoid deleting newly created "item-uri" entries.
105 // So we fetch a post that had been stored yesterday and only delete older ones.
106 $item = Post::selectFirst(['uri-id'], ["`uid` = ? AND `received` < UTC_TIMESTAMP() - INTERVAL ? DAY", 0, 1],
107 ['order' => ['received' => true]]);
108 if (empty($item['uri-id'])) {
109 Logger::warning('No item with uri-id found - we better quit here');
112 Logger::notice('Start deleting orphaned URI-ID', ['last-id' => $item['uri-id']]);
113 $uris = DBA::select('item-uri', ['id'], ["`id` < ?
114 AND NOT EXISTS(SELECT `uri-id` FROM `post` WHERE `uri-id` = `item-uri`.`id`)
115 AND NOT EXISTS(SELECT `parent-uri-id` FROM `post` WHERE `parent-uri-id` = `item-uri`.`id`)
116 AND NOT EXISTS(SELECT `thr-parent-id` FROM `post` WHERE `thr-parent-id` = `item-uri`.`id`)
117 AND NOT EXISTS(SELECT `external-id` FROM `post` WHERE `external-id` = `item-uri`.`id`)", $item['uri-id']]);
120 while ($rows = DBA::toArray($uris, false, 100)) {
121 $ids = array_column($rows, 'id');
122 DBA::delete('item-uri', ['id' => $ids]);
123 $affected_count += DBA::affectedRows();
124 Logger::info('Deleted', ['rows' => $affected_count]);
127 Logger::notice('Orphaned URI-ID entries removed', ['rows' => $affected_count]);
131 * Delete old external post entries
133 private static function deleteExpiredExternalPosts()
135 $expire_days = DI::config()->get('system', 'dbclean-expire-days');
136 $expire_days_unclaimed = DI::config()->get('system', 'dbclean-expire-unclaimed');
137 if (empty($expire_days_unclaimed)) {
138 $expire_days_unclaimed = $expire_days;
141 $limit = DI::config()->get('system', 'dbclean-expire-limit');
146 if (!empty($expire_days)) {
147 Logger::notice('Start collecting expired threads', ['expiry_days' => $expire_days]);
148 $uris = DBA::select('item-uri', ['id'], ["`id` IN
149 (SELECT `uri-id` FROM `post-thread` WHERE `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
150 AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-thread-user`
151 WHERE (`mention` OR `starred` OR `wall` OR `pinned`) AND `uri-id` = `post-thread`.`uri-id`)
152 AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-category`
153 WHERE `uri-id` = `post-thread`.`uri-id`)
154 AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-media`
155 WHERE `uri-id` = `post-thread`.`uri-id`)
156 AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` INNER JOIN `contact` ON `contact`.`id` = `contact-id` AND `notify_new_posts`
157 WHERE `parent-uri-id` = `post-thread`.`uri-id`)
158 AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user`
159 WHERE (`origin` OR `event-id` != 0 OR `post-type` = ?) AND `parent-uri-id` = `post-thread`.`uri-id`)
160 AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-content`
161 WHERE `resource-id` != 0 AND `uri-id` = `post-thread`.`uri-id`))",
162 $expire_days, Item::PT_PERSONAL_NOTE]);
164 Logger::notice('Start deleting expired threads');
166 while ($rows = DBA::toArray($uris, false, 100)) {
167 $ids = array_column($rows, 'id');
168 DBA::delete('item-uri', ['id' => $ids]);
169 $affected_count += DBA::affectedRows();
173 Logger::notice('Deleted expired threads', ['rows' => $affected_count]);
176 if (!empty($expire_days_unclaimed)) {
177 Logger::notice('Start collecting unclaimed public items', ['expiry_days' => $expire_days_unclaimed]);
178 $uris = DBA::select('item-uri', ['id'], ["`id` IN
179 (SELECT `uri-id` FROM `post-user` WHERE `gravity` = ? AND `uid` = ? AND `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
180 AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` AS `i` WHERE `i`.`uid` != ?
181 AND `i`.`parent-uri-id` = `post-user`.`uri-id`)
182 AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` AS `i` WHERE `i`.`uid` = ?
183 AND `i`.`parent-uri-id` = `post-user`.`uri-id` AND `i`.`received` > UTC_TIMESTAMP() - INTERVAL ? DAY))",
184 GRAVITY_PARENT, 0, $expire_days_unclaimed, 0, 0, $expire_days_unclaimed]);
186 Logger::notice('Start deleting unclaimed public items');
188 while ($rows = DBA::toArray($uris, false, 100)) {
189 $ids = array_column($rows, 'id');
190 DBA::delete('item-uri', ['id' => $ids]);
191 $affected_count += DBA::affectedRows();
194 Logger::notice('Deleted unclaimed public items', ['rows' => $affected_count]);