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\Database\DBA;
27 use Friendica\Model\Item;
32 * Delete old post entries
34 public static function execute()
36 $expire_days = DI::config()->get('system', 'dbclean-expire-days');
37 $expire_days_unclaimed = DI::config()->get('system', 'dbclean-expire-unclaimed');
38 if (empty($expire_days_unclaimed)) {
39 $expire_days_unclaimed = $expire_days;
42 $limit = DI::config()->get('system', 'dbclean-expire-limit');
47 if (!empty($expire_days)) {
48 Logger::notice('Start collecting expired threads', ['expiry_days' => $expire_days]);
49 $uris = DBA::select('item-uri', ['id'], ["`id` IN
50 (SELECT `uri-id` FROM `post-thread` WHERE `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
51 AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-thread-user`
52 WHERE (`mention` OR `starred` OR `wall` OR `pinned`) AND `uri-id` = `post-thread`.`uri-id`)
53 AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-category`
54 WHERE `uri-id` = `post-thread`.`uri-id`)
55 AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-media`
56 WHERE `uri-id` = `post-thread`.`uri-id`)
57 AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` INNER JOIN `contact` ON `contact`.`id` = `contact-id` AND `notify_new_posts`
58 WHERE `parent-uri-id` = `post-thread`.`uri-id`)
59 AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user`
60 WHERE (`origin` OR `event-id` != 0 OR `post-type` = ?) AND `parent-uri-id` = `post-thread`.`uri-id`)
61 AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-content`
62 WHERE `resource-id` != 0 AND `uri-id` = `post-thread`.`uri-id`))",
63 $expire_days, Item::PT_PERSONAL_NOTE]);
65 Logger::notice('Start deleting expired threads');
67 while ($rows = DBA::toArray($uris, false, 100)) {
68 $ids = array_column($rows, 'id');
69 DBA::delete('item-uri', ['id' => $ids]);
70 $affected_count += DBA::affectedRows();
74 Logger::notice('Deleted expired threads', ['rows' => $affected_count]);
77 if (!empty($expire_days_unclaimed)) {
78 Logger::notice('Start collecting unclaimed public items', ['expiry_days' => $expire_days_unclaimed]);
79 $uris = DBA::select('item-uri', ['id'], ["`id` IN
80 (SELECT `uri-id` FROM `post-user` WHERE `gravity` = ? AND `uid` = ? AND `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
81 AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` AS `i` WHERE `i`.`uid` != ?
82 AND `i`.`parent-uri-id` = `post-user`.`uri-id`)
83 AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` AS `i` WHERE `i`.`uid` = ?
84 AND `i`.`parent-uri-id` = `post-user`.`uri-id` AND `i`.`received` > UTC_TIMESTAMP() - INTERVAL ? DAY))",
85 GRAVITY_PARENT, 0, $expire_days_unclaimed, 0, 0, $expire_days_unclaimed]);
87 Logger::notice('Start deleting unclaimed public items');
89 while ($rows = DBA::toArray($uris, false, 100)) {
90 $ids = array_column($rows, 'id');
91 DBA::delete('item-uri', ['id' => $ids]);
92 $affected_count += DBA::affectedRows();
95 Logger::notice('Deleted unclaimed public items', ['rows' => $affected_count]);