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;
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)) {
49 Logger::notice('Start deleting expired threads', ['expiry_days' => $expire_days]);
50 /// @todo replace that query later
51 $ret = DBA::e("DELETE FROM `item-uri` WHERE `id` IN
52 (SELECT `uri-id` FROM `thread`
53 INNER JOIN `contact` ON `id` = `contact-id` AND NOT `notify_new_posts`
54 WHERE `thread`.`received` < UTC_TIMESTAMP() - INTERVAL ? DAY
55 AND NOT `thread`.`mention` AND NOT `thread`.`starred`
56 AND NOT `thread`.`wall` AND NOT `thread`.`origin`
57 AND `thread`.`uid` != 0 AND NOT `iid` IN (SELECT `parent` FROM `item`
58 WHERE (`item`.`starred` OR (`item`.`resource-id` != '')
59 OR (`item`.`event-id` != '') OR (`item`.`attach` != '')
60 OR `item`.`wall` OR `item`.`origin`
61 OR `item`.`uri-id` IN (SELECT `uri-id` FROM `post-category`
62 WHERE `post-category`.`uri-id` = `item`.`uri-id`))
63 AND `item`.`parent` = `thread`.`iid`))
64 ORDER BY `id` LIMIT ?", $expire_days, $limit);
66 $rows = DBA::affectedRows();
67 Logger::notice('Deleted expired threads', ['result' => $ret, 'rows' => $rows]);
69 if (!Worker::isInMaintenanceWindow()) {
70 Logger::notice('We are outside of the maintenance window, quitting');
73 } while ($rows >= $limit);
76 if (!empty($expire_days_unclaimed)) {
78 Logger::notice('Start deleting unclaimed public items', ['expiry_days' => $expire_days_unclaimed]);
79 $ret = DBA::e("DELETE FROM `item-uri` WHERE `id` IN
80 (SELECT `uri-id` FROM `item` WHERE `gravity` = ? AND `uid` = ? AND `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
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`)
83 AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `item` AS `i` WHERE `i`.`uid` = ?
84 AND `i`.`parent-uri-id` = `item`.`uri-id` AND `i`.`received` > UTC_TIMESTAMP() - INTERVAL ? DAY))
85 ORDER BY `id` LIMIT ?",
86 GRAVITY_PARENT, 0, $expire_days_unclaimed, 0, 0, $expire_days_unclaimed, $limit);
88 $rows = DBA::affectedRows();
89 Logger::notice('Deleted unclaimed public items', ['result' => $ret, 'rows' => $rows]);
91 if (!Worker::isInMaintenanceWindow()) {
92 Logger::notice('We are outside of the maintenance window, quitting');
95 } while ($rows >= $limit);