3 * @copyright Copyright (C) 2010-2021, the Friendica project
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\Database;
27 use Friendica\Database\DBA;
28 use Friendica\Database\DBStructure;
30 use Friendica\Model\Item;
31 use Friendica\Model\Post;
36 * Expire posts and remove unused item-uri entries
40 public static function execute()
42 self::deleteExpiredOriginPosts();
44 self::deleteOrphanedEntries();
46 self::deleteUnusedItemUri();
48 self::deleteExpiredExternalPosts();
50 if (DI::config()->get('system', 'add_missing_posts')) {
51 self::addMissingEntries();
54 // Set the expiry for origin posta
55 Worker::add(PRIORITY_LOW, 'Expire');
57 // update nodeinfo data after everything is cleaned up
58 Worker::add(PRIORITY_LOW, 'NodeInfo');
62 * Delete expired origin posts and orphaned post related table entries
66 private static function deleteExpiredOriginPosts()
68 Logger::notice('Delete expired posts');
69 // physically remove anything that has been deleted for more than two months
70 $condition = ["`gravity` = ? AND `deleted` AND `changed` < UTC_TIMESTAMP() - INTERVAL 60 DAY", GRAVITY_PARENT];
71 $rows = Post::select(['guid', 'uri-id', 'uid'], $condition);
72 while ($row = Post::fetch($rows)) {
73 Logger::info('Delete expired item', ['uri-id' => $row['uri-id'], 'guid' => $row['guid']]);
74 Post\User::delete(['parent-uri-id' => $row['uri-id'], 'uid' => $row['uid']]);
78 Logger::notice('Delete expired posts - done');
82 * Delete orphaned entries in the post related tables
86 private static function deleteOrphanedEntries()
88 Logger::notice('Delete orphaned entries');
90 // "post-user" is the leading table. So we delete every entry that isn't found there
91 $tables = ['item', 'post', 'post-content', 'post-thread', 'post-thread-user'];
92 foreach ($tables as $table) {
93 if (($table == 'item') && !DBStructure::existsTable('item')) {
97 Logger::notice('Start collecting orphaned entries', ['table' => $table]);
98 $uris = DBA::select($table, ['uri-id'], ["NOT `uri-id` IN (SELECT `uri-id` FROM `post-user`)"]);
100 Logger::notice('Deleting orphaned entries - start', ['table' => $table]);
101 while ($rows = DBA::toArray($uris, false, 100)) {
102 $ids = array_column($rows, 'uri-id');
103 DBA::delete($table, ['uri-id' => $ids]);
104 $affected_count += DBA::affectedRows();
107 Logger::notice('Orphaned entries deleted', ['table' => $table, 'rows' => $affected_count]);
109 Logger::notice('Delete orphaned entries - done');
113 * Add missing entries in some post related tables
117 private static function addMissingEntries()
119 Logger::notice('Adding missing entries');
122 $userposts = DBA::select('post-user', [], ["`uri-id` not in (select `uri-id` from `post`)"]);
123 while ($fields = DBA::fetch($userposts)) {
124 $post_fields = DBStructure::getFieldsForTable('post', $fields);
125 DBA::insert('post', $post_fields, Database::INSERT_IGNORE);
128 DBA::close($userposts);
130 Logger::notice('Added post entries', ['rows' => $rows]);
132 Logger::notice('No post entries added');
136 $userposts = DBA::select('post-user', [], ["`gravity` = ? AND `uri-id` not in (select `uri-id` from `post-thread`)", GRAVITY_PARENT]);
137 while ($fields = DBA::fetch($userposts)) {
138 $post_fields = DBStructure::getFieldsForTable('post-thread', $fields);
139 $post_fields['commented'] = $post_fields['changed'] = $post_fields['created'];
140 DBA::insert('post-thread', $post_fields, Database::INSERT_IGNORE);
143 DBA::close($userposts);
145 Logger::notice('Added post-thread entries', ['rows' => $rows]);
147 Logger::notice('No post-thread entries added');
151 $userposts = DBA::select('post-user', [], ["`gravity` = ? AND `id` not in (select `post-user-id` from `post-thread-user`)", GRAVITY_PARENT]);
152 while ($fields = DBA::fetch($userposts)) {
153 $post_fields = DBStructure::getFieldsForTable('post-thread-user', $fields);
154 $post_fields['commented'] = $post_fields['changed'] = $post_fields['created'];
155 DBA::insert('post-thread-user', $post_fields, Database::INSERT_IGNORE);
158 DBA::close($userposts);
160 Logger::notice('Added post-thread-user entries', ['rows' => $rows]);
162 Logger::notice('No post-thread-user entries added');
167 * Delete unused item-uri entries
169 private static function deleteUnusedItemUri()
171 // We have to avoid deleting newly created "item-uri" entries.
172 // So we fetch a post that had been stored yesterday and only delete older ones.
173 $item = Post::selectFirst(['uri-id'], ["`uid` = ? AND `received` < UTC_TIMESTAMP() - INTERVAL ? DAY", 0, 1],
174 ['order' => ['received' => true]]);
175 if (empty($item['uri-id'])) {
176 Logger::warning('No item with uri-id found - we better quit here');
179 Logger::notice('Start collecting orphaned URI-ID', ['last-id' => $item['uri-id']]);
180 $uris = DBA::select('item-uri', ['id'], ["`id` < ?
181 AND NOT EXISTS(SELECT `uri-id` FROM `post-user` WHERE `uri-id` = `item-uri`.`id`)
182 AND NOT EXISTS(SELECT `parent-uri-id` FROM `post-user` WHERE `parent-uri-id` = `item-uri`.`id`)
183 AND NOT EXISTS(SELECT `thr-parent-id` FROM `post-user` WHERE `thr-parent-id` = `item-uri`.`id`)
184 AND NOT EXISTS(SELECT `external-id` FROM `post-user` WHERE `external-id` = `item-uri`.`id`)
185 AND NOT EXISTS(SELECT `uri-id` FROM `mail` WHERE `uri-id` = `item-uri`.`id`)
186 AND NOT EXISTS(SELECT `parent-uri-id` FROM `mail` WHERE `parent-uri-id` = `item-uri`.`id`)
187 AND NOT EXISTS(SELECT `thr-parent-id` FROM `mail` WHERE `thr-parent-id` = `item-uri`.`id`)", $item['uri-id']]);
189 Logger::notice('Start deleting orphaned URI-ID', ['last-id' => $item['uri-id']]);
191 while ($rows = DBA::toArray($uris, false, 100)) {
192 $ids = array_column($rows, 'id');
193 DBA::delete('item-uri', ['id' => $ids]);
194 $affected_count += DBA::affectedRows();
195 Logger::info('Deleted', ['rows' => $affected_count]);
198 Logger::notice('Orphaned URI-ID entries removed', ['rows' => $affected_count]);
202 * Delete old external post entries
204 private static function deleteExpiredExternalPosts()
206 $expire_days = DI::config()->get('system', 'dbclean-expire-days');
207 $expire_days_unclaimed = DI::config()->get('system', 'dbclean-expire-unclaimed');
208 if (empty($expire_days_unclaimed)) {
209 $expire_days_unclaimed = $expire_days;
212 $limit = DI::config()->get('system', 'dbclean-expire-limit');
217 if (!empty($expire_days)) {
218 Logger::notice('Start collecting expired threads', ['expiry_days' => $expire_days]);
219 $uris = DBA::select('item-uri', ['id'], ["`id` IN
220 (SELECT `uri-id` FROM `post-thread` WHERE `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
221 AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-thread-user`
222 WHERE (`mention` OR `starred` OR `wall` OR `pinned`) AND `uri-id` = `post-thread`.`uri-id`)
223 AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-category`
224 WHERE `uri-id` = `post-thread`.`uri-id`)
225 AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-media`
226 WHERE `uri-id` = `post-thread`.`uri-id`)
227 AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` INNER JOIN `contact` ON `contact`.`id` = `contact-id` AND `notify_new_posts`
228 WHERE `parent-uri-id` = `post-thread`.`uri-id`)
229 AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user`
230 WHERE (`origin` OR `event-id` != 0 OR `post-type` = ?) AND `parent-uri-id` = `post-thread`.`uri-id`)
231 AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-content`
232 WHERE `resource-id` != 0 AND `uri-id` = `post-thread`.`uri-id`))",
233 $expire_days, Item::PT_PERSONAL_NOTE]);
235 Logger::notice('Start deleting expired threads');
237 while ($rows = DBA::toArray($uris, false, 100)) {
238 $ids = array_column($rows, 'id');
239 DBA::delete('item-uri', ['id' => $ids]);
240 $affected_count += DBA::affectedRows();
244 Logger::notice('Deleted expired threads', ['rows' => $affected_count]);
247 if (!empty($expire_days_unclaimed)) {
248 Logger::notice('Start collecting unclaimed public items', ['expiry_days' => $expire_days_unclaimed]);
249 $uris = DBA::select('item-uri', ['id'], ["`id` IN
250 (SELECT `uri-id` FROM `post-user` WHERE `gravity` = ? AND `uid` = ? AND `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
251 AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` AS `i` WHERE `i`.`uid` != ?
252 AND `i`.`parent-uri-id` = `post-user`.`uri-id`)
253 AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` AS `i` WHERE `i`.`uid` = ?
254 AND `i`.`parent-uri-id` = `post-user`.`uri-id` AND `i`.`received` > UTC_TIMESTAMP() - INTERVAL ? DAY))",
255 GRAVITY_PARENT, 0, $expire_days_unclaimed, 0, 0, $expire_days_unclaimed]);
257 Logger::notice('Start deleting unclaimed public items');
259 while ($rows = DBA::toArray($uris, false, 100)) {
260 $ids = array_column($rows, 'id');
261 DBA::delete('item-uri', ['id' => $ids]);
262 $affected_count += DBA::affectedRows();
265 Logger::notice('Deleted unclaimed public items', ['rows' => $affected_count]);