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;
28 use Friendica\Model\Item;
33 * Delete old post entries
35 public static function execute()
37 $expire_days = DI::config()->get('system', 'dbclean-expire-days');
38 $expire_days_unclaimed = DI::config()->get('system', 'dbclean-expire-unclaimed');
39 if (empty($expire_days_unclaimed)) {
40 $expire_days_unclaimed = $expire_days;
43 $limit = DI::config()->get('system', 'dbclean-expire-limit');
48 if (!empty($expire_days)) {
50 Logger::notice('Start deleting expired threads', ['expiry_days' => $expire_days]);
51 $ret = DBA::e("DELETE FROM `item-uri` WHERE `id` IN
52 (SELECT `uri-id` FROM `post-thread` WHERE `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
53 AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-thread-user`
54 WHERE (`mention` OR `starred` OR `wall` OR `pinned`) AND `uri-id` = `post-thread`.`uri-id`)
55 AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-category`
56 WHERE `uri-id` = `post-thread`.`uri-id`)
57 AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-media`
58 WHERE `uri-id` = `post-thread`.`uri-id`)
59 AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` INNER JOIN `contact` ON `contact`.`id` = `contact-id` AND `notify_new_posts`
60 WHERE `parent-uri-id` = `post-thread`.`uri-id`)
61 AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user`
62 WHERE (`origin` OR `event-id` != 0 OR `post-type` = ?) AND `parent-uri-id` = `post-thread`.`uri-id`)
63 AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-content`
64 WHERE `resource-id` != 0 AND `uri-id` = `post-thread`.`uri-id`))
65 ORDER BY `id` LIMIT ?", $expire_days, Item::PT_PERSONAL_NOTE, $limit);
67 $rows = DBA::affectedRows();
68 Logger::notice('Deleted expired threads', ['result' => $ret, 'rows' => $rows]);
70 if (!Worker::isInMaintenanceWindow()) {
71 Logger::notice('We are outside of the maintenance window, quitting');
74 } while ($rows >= $limit);
77 if (!empty($expire_days_unclaimed)) {
79 Logger::notice('Start deleting unclaimed public items', ['expiry_days' => $expire_days_unclaimed]);
80 $ret = DBA::e("DELETE FROM `item-uri` WHERE `id` IN
81 (SELECT `uri-id` FROM `post-user` WHERE `gravity` = ? AND `uid` = ? AND `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
82 AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` AS `i` WHERE `i`.`uid` != ?
83 AND `i`.`parent-uri-id` = `post-user`.`uri-id`)
84 AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` AS `i` WHERE `i`.`uid` = ?
85 AND `i`.`parent-uri-id` = `post-user`.`uri-id` AND `i`.`received` > UTC_TIMESTAMP() - INTERVAL ? DAY))
86 ORDER BY `id` LIMIT ?",
87 GRAVITY_PARENT, 0, $expire_days_unclaimed, 0, 0, $expire_days_unclaimed, $limit);
89 $rows = DBA::affectedRows();
90 Logger::notice('Deleted unclaimed public items', ['result' => $ret, 'rows' => $rows]);
92 if (!Worker::isInMaintenanceWindow()) {
93 Logger::notice('We are outside of the maintenance window, quitting');
96 } while ($rows >= $limit);