]> git.mxchange.org Git - friendica.git/blob - src/Worker/ExpirePosts.php
Inverted condition
[friendica.git] / src / Worker / ExpirePosts.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
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\Database;
27 use Friendica\Database\DBA;
28 use Friendica\Database\DBStructure;
29 use Friendica\DI;
30 use Friendica\Model\Item;
31 use Friendica\Model\Post;
32
33 class ExpirePosts
34 {
35         /**
36          * Expire posts and remove unused item-uri entries
37          *
38          * @return void
39          */
40         public static function execute()
41         {
42                 self::deleteExpiredOriginPosts();
43
44                 self::deleteOrphanedEntries();
45
46                 self::deleteUnusedItemUri();
47
48                 self::deleteExpiredExternalPosts();
49
50                 if (DI::config()->get('system', 'add_missing_posts')) {
51                         self::addMissingEntries();
52                 }
53
54                 // Set the expiry for origin posta
55                 Worker::add(PRIORITY_LOW, 'Expire');
56
57                 // update nodeinfo data after everything is cleaned up
58                 Worker::add(PRIORITY_LOW, 'NodeInfo');
59         }
60
61         /**
62          * Delete expired origin posts and orphaned post related table entries
63          *
64          * @return void
65          */
66         private static function deleteExpiredOriginPosts()
67         {
68                 Logger::notice('Delete expired posts');
69                 // physically remove anything that has been deleted for more than two months
70                 $condition = ["`gravity` = ? AND `deleted` AND `changed` < UTC_TIMESTAMP() - INTERVAL 60 DAY", GRAVITY_PARENT];
71                 $rows = Post::select(['guid', 'uri-id', 'uid'],  $condition);
72                 while ($row = Post::fetch($rows)) {
73                         Logger::info('Delete expired item', ['uri-id' => $row['uri-id'], 'guid' => $row['guid']]);
74                         Post\User::delete(['parent-uri-id' => $row['uri-id'], 'uid' => $row['uid']]);
75                 }
76                 DBA::close($rows);
77
78                 Logger::notice('Delete expired posts - done');
79         }
80
81         /**
82          * Delete orphaned entries in the post related tables
83          *
84          * @return void
85          */
86         private static function deleteOrphanedEntries()
87         {
88                 Logger::notice('Delete orphaned entries');
89
90                 // "post-user" is the leading table. So we delete every entry that isn't found there
91                 $tables = ['item', 'post', 'post-content', 'post-thread', 'post-thread-user'];
92                 foreach ($tables as $table) {
93                         if (($table == 'item') && !DBStructure::existsTable('item')) {
94                                 continue;
95                         }
96
97                         Logger::notice('Start collecting orphaned entries', ['table' => $table]);
98                         $uris = DBA::select($table, ['uri-id'], ["NOT `uri-id` IN (SELECT `uri-id` FROM `post-user`)"]);
99                         $affected_count = 0;
100                         Logger::notice('Deleting orphaned entries - start', ['table' => $table]);
101                         while ($rows = DBA::toArray($uris, false, 100)) {
102                                 $ids = array_column($rows, 'uri-id');
103                                 DBA::delete($table, ['uri-id' => $ids]);
104                                 $affected_count += DBA::affectedRows();
105                         }
106                         DBA::close($uris);
107                         Logger::notice('Orphaned entries deleted', ['table' => $table, 'rows' => $affected_count]);
108                 }
109                 Logger::notice('Delete orphaned entries - done');
110         }
111
112         /**
113          * Add missing entries in some post related tables
114          *
115          * @return void
116          */
117         private static function addMissingEntries()
118         {
119                 Logger::notice('Adding missing entries');
120
121                 $rows = 0;
122                 $userposts = DBA::select('post-user', [], ["`uri-id` not in (select `uri-id` from `post`)"]);
123                 while ($fields = DBA::fetch($userposts)) {
124                         $post_fields = DBStructure::getFieldsForTable('post', $fields);
125                         DBA::insert('post', $post_fields, Database::INSERT_IGNORE);
126                         $rows++;
127                 }
128                 DBA::close($userposts);
129                 if ($rows > 0) {
130                         Logger::notice('Added post entries', ['rows' => $rows]);
131                 } else {
132                         Logger::notice('No post entries added');
133                 }
134
135                 $rows = 0;
136                 $userposts = DBA::select('post-user', [], ["`gravity` = ? AND `uri-id` not in (select `uri-id` from `post-thread`)", GRAVITY_PARENT]);
137                 while ($fields = DBA::fetch($userposts)) {
138                         $post_fields = DBStructure::getFieldsForTable('post-thread', $fields);
139                         $post_fields['commented'] = $post_fields['changed'] = $post_fields['created'];
140                         DBA::insert('post-thread', $post_fields, Database::INSERT_IGNORE);
141                         $rows++;
142                 }
143                 DBA::close($userposts);
144                 if ($rows > 0) {
145                         Logger::notice('Added post-thread entries', ['rows' => $rows]);
146                 } else {
147                         Logger::notice('No post-thread entries added');
148                 }
149
150                 $rows = 0;
151                 $userposts = DBA::select('post-user', [], ["`gravity` = ? AND `id` not in (select `post-user-id` from `post-thread-user`)", GRAVITY_PARENT]);
152                 while ($fields = DBA::fetch($userposts)) {
153                         $post_fields = DBStructure::getFieldsForTable('post-thread-user', $fields);
154                         $post_fields['commented'] = $post_fields['changed'] = $post_fields['created'];
155                         DBA::insert('post-thread-user', $post_fields, Database::INSERT_IGNORE);
156                         $rows++;
157                 }
158                 DBA::close($userposts);
159                 if ($rows > 0) {
160                         Logger::notice('Added post-thread-user entries', ['rows' => $rows]);
161                 } else {
162                         Logger::notice('No post-thread-user entries added');
163                 }
164         }
165
166         /**
167          * Delete unused item-uri entries
168          */
169         private static function deleteUnusedItemUri()
170         {
171                 // We have to avoid deleting newly created "item-uri" entries.
172                 // So we fetch a post that had been stored yesterday and only delete older ones.
173                 $item = Post::selectFirst(['uri-id'], ["`uid` = ? AND `received` < UTC_TIMESTAMP() - INTERVAL ? DAY", 0, 1],
174                         ['order' => ['received' => true]]);
175                 if (empty($item['uri-id'])) {
176                         Logger::warning('No item with uri-id found - we better quit here');
177                         return;
178                 }
179                 Logger::notice('Start collecting orphaned URI-ID', ['last-id' => $item['uri-id']]);
180                 $uris = DBA::select('item-uri', ['id'], ["`id` < ?
181                         AND NOT EXISTS(SELECT `uri-id` FROM `post-user` WHERE `uri-id` = `item-uri`.`id`)
182                         AND NOT EXISTS(SELECT `parent-uri-id` FROM `post-user` WHERE `parent-uri-id` = `item-uri`.`id`)
183                         AND NOT EXISTS(SELECT `thr-parent-id` FROM `post-user` WHERE `thr-parent-id` = `item-uri`.`id`)
184                         AND NOT EXISTS(SELECT `external-id` FROM `post-user` WHERE `external-id` = `item-uri`.`id`)
185                         AND NOT EXISTS(SELECT `uri-id` FROM `mail` WHERE `uri-id` = `item-uri`.`id`)
186                         AND NOT EXISTS(SELECT `parent-uri-id` FROM `mail` WHERE `parent-uri-id` = `item-uri`.`id`)
187                         AND NOT EXISTS(SELECT `thr-parent-id` FROM `mail` WHERE `thr-parent-id` = `item-uri`.`id`)", $item['uri-id']]);
188
189                 Logger::notice('Start deleting orphaned URI-ID', ['last-id' => $item['uri-id']]);
190                 $affected_count = 0;
191                 while ($rows = DBA::toArray($uris, false, 100)) {
192                         $ids = array_column($rows, 'id');
193                         DBA::delete('item-uri', ['id' => $ids]);
194                         $affected_count += DBA::affectedRows();
195                         Logger::info('Deleted', ['rows' => $affected_count]);
196                 }
197                 DBA::close($uris);
198                 Logger::notice('Orphaned URI-ID entries removed', ['rows' => $affected_count]);
199         }
200
201         /**
202          * Delete old external post entries
203          */
204         private static function deleteExpiredExternalPosts()
205         {
206                 $expire_days = DI::config()->get('system', 'dbclean-expire-days');
207                 $expire_days_unclaimed = DI::config()->get('system', 'dbclean-expire-unclaimed');
208                 if (empty($expire_days_unclaimed)) {
209                         $expire_days_unclaimed = $expire_days;
210                 }
211
212                 $limit = DI::config()->get('system', 'dbclean-expire-limit');
213                 if (empty($limit)) {
214                         return;
215                 }
216
217                 if (!empty($expire_days)) {
218                         Logger::notice('Start collecting expired threads', ['expiry_days' => $expire_days]);
219                         $uris = DBA::select('item-uri', ['id'], ["`id` IN
220                                 (SELECT `uri-id` FROM `post-thread` WHERE `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
221                                         AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-thread-user`
222                                                 WHERE (`mention` OR `starred` OR `wall` OR `pinned`) AND `uri-id` = `post-thread`.`uri-id`)
223                                         AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-category`
224                                                 WHERE `uri-id` = `post-thread`.`uri-id`)
225                                         AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-media`
226                                                 WHERE `uri-id` = `post-thread`.`uri-id`)
227                                         AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` INNER JOIN `contact` ON `contact`.`id` = `contact-id` AND `notify_new_posts`
228                                                 WHERE `parent-uri-id` = `post-thread`.`uri-id`)
229                                         AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user`
230                                                 WHERE (`origin` OR `event-id` != 0 OR `post-type` = ?) AND `parent-uri-id` = `post-thread`.`uri-id`)
231                                         AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-content`
232                                                 WHERE `resource-id` != 0 AND `uri-id` = `post-thread`.`uri-id`))",
233                                 $expire_days, Item::PT_PERSONAL_NOTE]);
234
235                         Logger::notice('Start deleting expired threads');
236                         $affected_count = 0;
237                         while ($rows = DBA::toArray($uris, false, 100)) {
238                                 $ids = array_column($rows, 'id');
239                                 DBA::delete('item-uri', ['id' => $ids]);
240                                 $affected_count += DBA::affectedRows();
241                         }
242                         DBA::close($uris);
243
244                         Logger::notice('Deleted expired threads', ['rows' => $affected_count]);
245                 }
246
247                 if (!empty($expire_days_unclaimed)) {
248                         Logger::notice('Start collecting unclaimed public items', ['expiry_days' => $expire_days_unclaimed]);
249                         $uris = DBA::select('item-uri', ['id'], ["`id` IN
250                                 (SELECT `uri-id` FROM `post-user` WHERE `gravity` = ? AND `uid` = ? AND `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
251                                         AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` AS `i` WHERE `i`.`uid` != ?
252                                                 AND `i`.`parent-uri-id` = `post-user`.`uri-id`)
253                                         AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` AS `i` WHERE `i`.`uid` = ?
254                                                 AND `i`.`parent-uri-id` = `post-user`.`uri-id` AND `i`.`received` > UTC_TIMESTAMP() - INTERVAL ? DAY))",
255                                 GRAVITY_PARENT, 0, $expire_days_unclaimed, 0, 0, $expire_days_unclaimed]);
256
257                         Logger::notice('Start deleting unclaimed public items');
258                         $affected_count = 0;
259                         while ($rows = DBA::toArray($uris, false, 100)) {
260                                 $ids = array_column($rows, 'id');
261                                 DBA::delete('item-uri', ['id' => $ids]);
262                                 $affected_count += DBA::affectedRows();
263                         }
264                         DBA::close($uris);
265                         Logger::notice('Deleted unclaimed public items', ['rows' => $affected_count]);
266                 }
267         }
268 }