]> git.mxchange.org Git - friendica.git/blob - src/Worker/ExpirePosts.php
Improve expiry query
[friendica.git] / src / Worker / ExpirePosts.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Worker;
23
24 use Friendica\Core\Logger;
25 use Friendica\Core\Worker;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
28
29 class ExpirePosts
30 {
31         /**
32          * Delete old post entries
33          */
34         public static function execute()
35         {
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;
40                 }
41
42                 $limit = DI::config()->get('system', 'dbclean-expire-limit');
43                 if (empty($limit)) {
44                         return;
45                 }
46
47                 if (!empty($expire_days)) {
48                         do {
49                                 Logger::notice('Start deleting expired threads', ['expiry_days' => $expire_days]);
50                                 $ret = DBA::e("DELETE FROM `item-uri` WHERE `id` IN
51                                         (SELECT `uri-id` FROM `thread`
52                                         INNER JOIN `contact` ON `id` = `contact-id` AND NOT `notify_new_posts`
53                                         WHERE `thread`.`received` < UTC_TIMESTAMP() - INTERVAL ? DAY
54                                                 AND NOT `thread`.`mention` AND NOT `thread`.`starred`
55                                                 AND NOT `thread`.`wall` AND NOT `thread`.`origin`
56                                                 AND `thread`.`uid` != 0 AND NOT `iid` IN (SELECT `parent` FROM `item`
57                                                         WHERE (`item`.`starred` OR (`item`.`resource-id` != '')
58                                                                 OR (`item`.`event-id` != '') OR (`item`.`attach` != '')
59                                                                 OR `item`.`wall` OR `item`.`origin`
60                                                                 OR `item`.`uri-id` IN (SELECT `uri-id` FROM `post-category`
61                                                                         WHERE `post-category`.`uri-id` = `item`.`uri-id`))
62                                                                 AND `item`.`parent` = `thread`.`iid`))
63                                         ORDER BY `id` LIMIT ?", $expire_days, $limit);
64
65                                 $rows = DBA::affectedRows();
66                                 Logger::notice('Deleted expired threads', ['result' => $ret, 'rows' => $rows]);
67
68                                 if (!Worker::isInMaintenanceWindow()) {
69                                         Logger::notice('We are outside of the maintenance window, quitting');
70                                         return;
71                                 }
72                         } while ($rows >= $limit);
73                 }
74
75                 if (!empty($expire_days_unclaimed)) {
76                         do {
77                                 Logger::notice('Start deleting unclaimed public items', ['expiry_days' => $expire_days_unclaimed]);
78                                 $ret = DBA::e("DELETE FROM `item-uri` WHERE `id` IN
79                                         (SELECT `uri-id` FROM `item` WHERE `gravity` = ? AND `uid` = ? AND `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
80                                                 AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `item` AS `i` WHERE `i`.`uid` != ?
81                                                         AND `i`.`parent-uri-id` = `item`.`uri-id`)
82                                                 AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `item` AS `i` WHERE `i`.`uid` = ?
83                                                         AND `i`.`parent-uri-id` = `item`.`uri-id` AND `i`.`received` > UTC_TIMESTAMP() - INTERVAL ? DAY))
84                                         ORDER BY `id` LIMIT ?",
85                                         GRAVITY_PARENT, 0, $expire_days_unclaimed, 0, 0, $expire_days_unclaimed, $limit);
86
87                                 $rows = DBA::affectedRows();
88                                 Logger::notice('Deleted unclaimed public items', ['result' => $ret, 'rows' => $rows]);
89
90                                 if (!Worker::isInMaintenanceWindow()) {
91                                         Logger::notice('We are outside of the maintenance window, quitting');
92                                         return;
93                                 }
94                         } while ($rows >= $limit);
95                 }
96         }
97 }