]> git.mxchange.org Git - friendica.git/blob - src/Worker/ExpirePosts.php
Detection of local requests
[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 `uri-id` FROM `event` WHERE `uri-id` = `item-uri`.`id`)
187                         AND NOT EXISTS(SELECT `uri-id` FROM `contact` WHERE `uri-id` = `item-uri`.`id`)
188                         AND NOT EXISTS(SELECT `uri-id` FROM `apcontact` WHERE `uri-id` = `item-uri`.`id`)
189                         AND NOT EXISTS(SELECT `uri-id` FROM `fcontact` WHERE `uri-id` = `item-uri`.`id`)
190                         AND NOT EXISTS(SELECT `parent-uri-id` FROM `mail` WHERE `parent-uri-id` = `item-uri`.`id`)
191                         AND NOT EXISTS(SELECT `thr-parent-id` FROM `mail` WHERE `thr-parent-id` = `item-uri`.`id`)", $item['uri-id']]);
192
193                 Logger::notice('Start deleting orphaned URI-ID', ['last-id' => $item['uri-id']]);
194                 $affected_count = 0;
195                 while ($rows = DBA::toArray($uris, false, 100)) {
196                         $ids = array_column($rows, 'id');
197                         DBA::delete('item-uri', ['id' => $ids]);
198                         $affected_count += DBA::affectedRows();
199                         Logger::info('Deleted', ['rows' => $affected_count]);
200                 }
201                 DBA::close($uris);
202                 Logger::notice('Orphaned URI-ID entries removed', ['rows' => $affected_count]);
203         }
204
205         /**
206          * Delete old external post entries
207          */
208         private static function deleteExpiredExternalPosts()
209         {
210                 $expire_days = DI::config()->get('system', 'dbclean-expire-days');
211                 $expire_days_unclaimed = DI::config()->get('system', 'dbclean-expire-unclaimed');
212                 if (empty($expire_days_unclaimed)) {
213                         $expire_days_unclaimed = $expire_days;
214                 }
215
216                 $limit = DI::config()->get('system', 'dbclean-expire-limit');
217                 if (empty($limit)) {
218                         return;
219                 }
220
221                 if (!empty($expire_days)) {
222                         Logger::notice('Start collecting expired threads', ['expiry_days' => $expire_days]);
223                         $uris = DBA::select('item-uri', ['id'], ["`id` IN
224                                 (SELECT `uri-id` FROM `post-thread` WHERE `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
225                                         AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-thread-user`
226                                                 WHERE (`mention` OR `starred` OR `wall` OR `pinned`) AND `uri-id` = `post-thread`.`uri-id`)
227                                         AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-category`
228                                                 WHERE `uri-id` = `post-thread`.`uri-id`)
229                                         AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-media`
230                                                 WHERE `uri-id` = `post-thread`.`uri-id`)
231                                         AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` INNER JOIN `contact` ON `contact`.`id` = `contact-id` AND `notify_new_posts`
232                                                 WHERE `parent-uri-id` = `post-thread`.`uri-id`)
233                                         AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user`
234                                                 WHERE (`origin` OR `event-id` != 0 OR `post-type` = ?) AND `parent-uri-id` = `post-thread`.`uri-id`)
235                                         AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-content`
236                                                 WHERE `resource-id` != 0 AND `uri-id` = `post-thread`.`uri-id`))",
237                                 $expire_days, Item::PT_PERSONAL_NOTE]);
238
239                         Logger::notice('Start deleting expired threads');
240                         $affected_count = 0;
241                         while ($rows = DBA::toArray($uris, false, 100)) {
242                                 $ids = array_column($rows, 'id');
243                                 DBA::delete('item-uri', ['id' => $ids]);
244                                 $affected_count += DBA::affectedRows();
245                         }
246                         DBA::close($uris);
247
248                         Logger::notice('Deleted expired threads', ['rows' => $affected_count]);
249                 }
250
251                 if (!empty($expire_days_unclaimed)) {
252                         Logger::notice('Start collecting unclaimed public items', ['expiry_days' => $expire_days_unclaimed]);
253                         $uris = DBA::select('item-uri', ['id'], ["`id` IN
254                                 (SELECT `uri-id` FROM `post-user` WHERE `gravity` = ? AND `uid` = ? AND `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
255                                         AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` AS `i` WHERE `i`.`uid` != ?
256                                                 AND `i`.`parent-uri-id` = `post-user`.`uri-id`)
257                                         AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` AS `i` WHERE `i`.`uid` = ?
258                                                 AND `i`.`parent-uri-id` = `post-user`.`uri-id` AND `i`.`received` > UTC_TIMESTAMP() - INTERVAL ? DAY))",
259                                 GRAVITY_PARENT, 0, $expire_days_unclaimed, 0, 0, $expire_days_unclaimed]);
260
261                         Logger::notice('Start deleting unclaimed public items');
262                         $affected_count = 0;
263                         while ($rows = DBA::toArray($uris, false, 100)) {
264                                 $ids = array_column($rows, 'id');
265                                 DBA::delete('item-uri', ['id' => $ids]);
266                                 $affected_count += DBA::affectedRows();
267                         }
268                         DBA::close($uris);
269                         Logger::notice('Deleted unclaimed public items', ['rows' => $affected_count]);
270                 }
271         }
272 }