]> git.mxchange.org Git - friendica.git/blob - src/Worker/ExpirePosts.php
8d50c5d9d5494366ba4de6c83259aa9c53f458c1
[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 use Friendica\Model\Item;
29
30 class ExpirePosts
31 {
32         /**
33          * Delete old post entries
34          */
35         public static function execute()
36         {
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;
41                 }
42
43                 $limit = DI::config()->get('system', 'dbclean-expire-limit');
44                 if (empty($limit)) {
45                         return;
46                 }
47
48                 if (!empty($expire_days)) {
49                         do {
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);
66
67                                 $rows = DBA::affectedRows();
68                                 Logger::notice('Deleted expired threads', ['result' => $ret, 'rows' => $rows]);
69
70                                 if (!Worker::isInMaintenanceWindow()) {
71                                         Logger::notice('We are outside of the maintenance window, quitting');
72                                         return;
73                                 }
74                         } while ($rows >= $limit);
75                 }
76
77                 if (!empty($expire_days_unclaimed)) {
78                         do {
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);
88
89                                 $rows = DBA::affectedRows();
90                                 Logger::notice('Deleted unclaimed public items', ['result' => $ret, 'rows' => $rows]);
91
92                                 if (!Worker::isInMaintenanceWindow()) {
93                                         Logger::notice('We are outside of the maintenance window, quitting');
94                                         return;
95                                 }
96                         } while ($rows >= $limit);
97                 }
98         }
99 }