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