]> git.mxchange.org Git - friendica.git/blob - src/Worker/ExpirePosts.php
14fb560b13421ed068e1fec160394614c3b611fa
[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\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Model\Item;
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                         Logger::notice('Start collecting expired threads', ['expiry_days' => $expire_days]);
49                         $uris = DBA::select('item-uri', ['id'], ["`id` IN
50                                 (SELECT `uri-id` FROM `post-thread` WHERE `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
51                                         AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-thread-user`
52                                                 WHERE (`mention` OR `starred` OR `wall` OR `pinned`) AND `uri-id` = `post-thread`.`uri-id`)
53                                         AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-category`
54                                                 WHERE `uri-id` = `post-thread`.`uri-id`)
55                                         AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-media`
56                                                 WHERE `uri-id` = `post-thread`.`uri-id`)
57                                         AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` 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 `post-user`
60                                                 WHERE (`origin` OR `event-id` != 0 OR `post-type` = ?) AND `parent-uri-id` = `post-thread`.`uri-id`)
61                                         AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-content`
62                                                 WHERE `resource-id` != 0 AND `uri-id` = `post-thread`.`uri-id`))",
63                                 $expire_days, Item::PT_PERSONAL_NOTE]);
64
65                         Logger::notice('Start deleting expired threads');
66                         $affected_count = 0;
67                         while ($rows = DBA::toArray($uris, false, 100)) {
68                                 $ids = array_column($rows, 'id');
69                                 DBA::delete('item-uri', ['id' => $ids]);
70                                 $affected_count += DBA::affectedRows();
71                         }
72                         DBA::close($uris);
73
74                         Logger::notice('Deleted expired threads', ['rows' => $affected_count]);
75                 }
76
77                 if (!empty($expire_days_unclaimed)) {
78                         Logger::notice('Start collecting unclaimed public items', ['expiry_days' => $expire_days_unclaimed]);
79                         $uris = DBA::select('item-uri', ['id'], ["`id` IN
80                                 (SELECT `uri-id` FROM `post-user` WHERE `gravity` = ? AND `uid` = ? AND `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
81                                         AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` AS `i` WHERE `i`.`uid` != ?
82                                                 AND `i`.`parent-uri-id` = `post-user`.`uri-id`)
83                                         AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` AS `i` WHERE `i`.`uid` = ?
84                                                 AND `i`.`parent-uri-id` = `post-user`.`uri-id` AND `i`.`received` > UTC_TIMESTAMP() - INTERVAL ? DAY))",
85                                 GRAVITY_PARENT, 0, $expire_days_unclaimed, 0, 0, $expire_days_unclaimed]);
86
87                         Logger::notice('Start deleting unclaimed public items');
88                         $affected_count = 0;
89                         while ($rows = DBA::toArray($uris, false, 100)) {
90                                 $ids = array_column($rows, 'id');
91                                 DBA::delete('item-uri', ['id' => $ids]);
92                                 $affected_count += DBA::affectedRows();
93                         }
94                         DBA::close($uris);
95                         Logger::notice('Deleted unclaimed public items', ['rows' => $affected_count]);
96                 }
97         }
98 }