]> git.mxchange.org Git - friendica.git/blob - src/Worker/ExpirePosts.php
9e75ea66bd811a27e33b5b40afc80955a45c88d9
[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\Database\DBStructure;
28 use Friendica\DI;
29 use Friendica\Model\Item;
30 use Friendica\Model\Post;
31
32 class ExpirePosts
33 {
34         /**
35          * Expire posts and remove unused item-uri entries
36          *
37          * @return void
38          */
39         public static function execute()
40         {
41                 $a = DI::app();
42
43                 self::deleteExpiredOriginPosts();
44
45                 self::deleteUnusedItemUri();
46
47                 self::deleteExpiredExternalPosts();
48
49                 // update nodeinfo data after everything is cleaned up
50                 Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true], 'NodeInfo');
51         }
52
53         /**
54          * Delete expired origin posts and orphaned post related table entries
55          *
56          * @return void
57          */
58         private static function deleteExpiredOriginPosts()
59         {
60                 Logger::info('Delete expired posts');
61                 // physically remove anything that has been deleted for more than two months
62                 $condition = ["`gravity` = ? AND `deleted` AND `changed` < UTC_TIMESTAMP() - INTERVAL 60 DAY", GRAVITY_PARENT];
63                 $rows = Post::select(['guid', 'uri-id', 'uid'],  $condition);
64                 while ($row = Post::fetch($rows)) {
65                         Logger::info('Delete expired item', ['uri-id' => $row['uri-id'], 'guid' => $row['guid']]);
66                         if (DBStructure::existsTable('item')) {
67                                 DBA::delete('item', ['parent-uri-id' => $row['uri-id'], 'uid' => $row['uid']]);
68                         }
69                         Post\User::delete(['parent-uri-id' => $row['uri-id'], 'uid' => $row['uid']]);
70                 }
71                 DBA::close($rows);
72
73                 Logger::info('Deleting orphaned post entries- start');
74                 $condition = ["NOT EXISTS (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uri-id` = `post`.`uri-id`)"];
75                 DBA::delete('post', $condition);
76                 Logger::info('Orphaned post entries deleted', ['rows' => DBA::affectedRows()]);
77
78                 Logger::info('Deleting orphaned post-content entries - start');
79                 $condition = ["NOT EXISTS (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uri-id` = `post-content`.`uri-id`)"];
80                 DBA::delete('post-content', $condition);
81                 Logger::info('Orphaned post-content entries deleted', ['rows' => DBA::affectedRows()]);
82
83                 Logger::info('Deleting orphaned post-thread entries - start');
84                 $condition = ["NOT EXISTS (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uri-id` = `post-thread`.`uri-id`)"];
85                 DBA::delete('post-thread', $condition);
86                 Logger::info('Orphaned post-thread entries deleted', ['rows' => DBA::affectedRows()]);
87
88                 Logger::info('Deleting orphaned post-thread-user entries - start');
89                 $condition = ["NOT EXISTS (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uri-id` = `post-thread-user`.`uri-id`)"];
90                 DBA::delete('post-thread-user', $condition);
91                 Logger::info('Orphaned post-thread-user entries deleted', ['rows' => DBA::affectedRows()]);
92
93                 Logger::info('Delete expired posts - done');
94         }
95
96         /**
97          * Delete unused item-uri entries
98          */
99         private static function deleteUnusedItemUri()
100         {
101                 $a = DI::app();
102
103                 // We have to avoid deleting newly created "item-uri" entries.
104                 // So we fetch a post that had been stored yesterday and only delete older ones.
105                 $item = Post::selectFirst(['uri-id'], ["`uid` = ? AND `received` < UTC_TIMESTAMP() - INTERVAL ? DAY", 0, 1],
106                         ['order' => ['received' => true]]);
107                 if (empty($item['uri-id'])) {
108                         Logger::warning('No item with uri-id found - we better quit here');
109                         return;
110                 }
111                 Logger::notice('Start deleting orphaned URI-ID', ['last-id' => $item['uri-id']]);
112                 $uris = DBA::select('item-uri', ['id'], ["`id` < ?
113                         AND NOT EXISTS(SELECT `uri-id` FROM `post` WHERE `uri-id` = `item-uri`.`id`)
114                         AND NOT EXISTS(SELECT `parent-uri-id` FROM `post` WHERE `parent-uri-id` = `item-uri`.`id`)
115                         AND NOT EXISTS(SELECT `thr-parent-id` FROM `post` WHERE `thr-parent-id` = `item-uri`.`id`)
116                         AND NOT EXISTS(SELECT `external-id` FROM `post` WHERE `external-id` = `item-uri`.`id`)", $item['uri-id']]);
117
118                 $affected_count = 0;
119                 while ($rows = DBA::toArray($uris, false, 100)) {
120                         $ids = array_column($rows, 'id');
121                         DBA::delete('item-uri', ['id' => $ids]);
122                         $affected_count += DBA::affectedRows();
123                         Logger::info('Deleted', ['rows' => $affected_count]);
124                 }
125                 DBA::close($uris);
126                 Logger::notice('Orphaned URI-ID entries removed', ['rows' => $affected_count]);
127         }
128
129         /**
130          * Delete old external post entries
131          */
132         private static function deleteExpiredExternalPosts()
133         {
134                 $expire_days = DI::config()->get('system', 'dbclean-expire-days');
135                 $expire_days_unclaimed = DI::config()->get('system', 'dbclean-expire-unclaimed');
136                 if (empty($expire_days_unclaimed)) {
137                         $expire_days_unclaimed = $expire_days;
138                 }
139
140                 $limit = DI::config()->get('system', 'dbclean-expire-limit');
141                 if (empty($limit)) {
142                         return;
143                 }
144
145                 if (!empty($expire_days)) {
146                         Logger::notice('Start collecting expired threads', ['expiry_days' => $expire_days]);
147                         $uris = DBA::select('item-uri', ['id'], ["`id` IN
148                                 (SELECT `uri-id` FROM `post-thread` WHERE `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
149                                         AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-thread-user`
150                                                 WHERE (`mention` OR `starred` OR `wall` OR `pinned`) AND `uri-id` = `post-thread`.`uri-id`)
151                                         AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-category`
152                                                 WHERE `uri-id` = `post-thread`.`uri-id`)
153                                         AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-media`
154                                                 WHERE `uri-id` = `post-thread`.`uri-id`)
155                                         AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` INNER JOIN `contact` ON `contact`.`id` = `contact-id` AND `notify_new_posts`
156                                                 WHERE `parent-uri-id` = `post-thread`.`uri-id`)
157                                         AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user`
158                                                 WHERE (`origin` OR `event-id` != 0 OR `post-type` = ?) AND `parent-uri-id` = `post-thread`.`uri-id`)
159                                         AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-content`
160                                                 WHERE `resource-id` != 0 AND `uri-id` = `post-thread`.`uri-id`))",
161                                 $expire_days, Item::PT_PERSONAL_NOTE]);
162
163                         Logger::notice('Start deleting expired threads');
164                         $affected_count = 0;
165                         while ($rows = DBA::toArray($uris, false, 100)) {
166                                 $ids = array_column($rows, 'id');
167                                 DBA::delete('item-uri', ['id' => $ids]);
168                                 $affected_count += DBA::affectedRows();
169                         }
170                         DBA::close($uris);
171
172                         Logger::notice('Deleted expired threads', ['rows' => $affected_count]);
173                 }
174
175                 if (!empty($expire_days_unclaimed)) {
176                         Logger::notice('Start collecting unclaimed public items', ['expiry_days' => $expire_days_unclaimed]);
177                         $uris = DBA::select('item-uri', ['id'], ["`id` IN
178                                 (SELECT `uri-id` FROM `post-user` WHERE `gravity` = ? AND `uid` = ? AND `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
179                                         AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` AS `i` WHERE `i`.`uid` != ?
180                                                 AND `i`.`parent-uri-id` = `post-user`.`uri-id`)
181                                         AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` AS `i` WHERE `i`.`uid` = ?
182                                                 AND `i`.`parent-uri-id` = `post-user`.`uri-id` AND `i`.`received` > UTC_TIMESTAMP() - INTERVAL ? DAY))",
183                                 GRAVITY_PARENT, 0, $expire_days_unclaimed, 0, 0, $expire_days_unclaimed]);
184
185                         Logger::notice('Start deleting unclaimed public items');
186                         $affected_count = 0;
187                         while ($rows = DBA::toArray($uris, false, 100)) {
188                                 $ids = array_column($rows, 'id');
189                                 DBA::delete('item-uri', ['id' => $ids]);
190                                 $affected_count += DBA::affectedRows();
191                         }
192                         DBA::close($uris);
193                         Logger::notice('Deleted unclaimed public items', ['rows' => $affected_count]);
194                 }
195         }
196 }