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` WHERE (`mention` OR `starred` OR `wall` OR `pinned`) AND `uri-id` = `post-thread`.`uri-id`)
54 AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin` AND `uri-id` = `post-thread`.`uri-id`)
55 AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-category` WHERE `uri-id` = `post-thread`.`uri-id`)
56 AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-media` WHERE `uri-id` = `post-thread`.`uri-id`)
57 AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `item` 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 `item`
60 WHERE (`origin` OR `starred` OR `resource-id` != 0 OR `event-id` != 0 OR `wall` OR `attach` != '' OR `post-type` = ?)
61 AND `parent-uri-id` = `post-thread`.`uri-id`))
62 ORDER BY `id` LIMIT ?", $expire_days, Item::PT_PERSONAL_NOTE, $limit);
64 $rows = DBA::affectedRows();
65 Logger::notice('Deleted expired threads', ['result' => $ret, 'rows' => $rows]);
67 if (!Worker::isInMaintenanceWindow()) {
68 Logger::notice('We are outside of the maintenance window, quitting');
71 } while ($rows >= $limit);
74 if (!empty($expire_days_unclaimed)) {
76 Logger::notice('Start deleting unclaimed public items', ['expiry_days' => $expire_days_unclaimed]);
77 $ret = DBA::e("DELETE FROM `item-uri` WHERE `id` IN
78 (SELECT `uri-id` FROM `item` WHERE `gravity` = ? AND `uid` = ? AND `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
79 AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `item` AS `i` WHERE `i`.`uid` != ?
80 AND `i`.`parent-uri-id` = `item`.`uri-id`)
81 AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `item` AS `i` WHERE `i`.`uid` = ?
82 AND `i`.`parent-uri-id` = `item`.`uri-id` AND `i`.`received` > UTC_TIMESTAMP() - INTERVAL ? DAY))
83 ORDER BY `id` LIMIT ?",
84 GRAVITY_PARENT, 0, $expire_days_unclaimed, 0, 0, $expire_days_unclaimed, $limit);
86 $rows = DBA::affectedRows();
87 Logger::notice('Deleted unclaimed public items', ['result' => $ret, 'rows' => $rows]);
89 if (!Worker::isInMaintenanceWindow()) {
90 Logger::notice('We are outside of the maintenance window, quitting');
93 } while ($rows >= $limit);