]> git.mxchange.org Git - friendica.git/blob - src/Worker/DBClean.php
Switch all item deletion logging to info level
[friendica.git] / src / Worker / DBClean.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\DI;
28
29 /**
30  * The script is called from time to time to clean the database entries and remove orphaned data.
31  */
32 class DBClean {
33         public static function execute($stage = 0) {
34
35                 if (!DI::config()->get('system', 'dbclean', false)) {
36                         return;
37                 }
38
39                 if ($stage == 0) {
40                         self::forkCleanProcess();
41                 } else {
42                         self::removeOrphans($stage);
43                 }
44         }
45
46         /**
47          * Fork the different DBClean processes
48          */
49         private static function forkCleanProcess() {
50                 // Get the expire days for step 8 and 9
51                 $days = DI::config()->get('system', 'dbclean-expire-days', 0);
52
53                 for ($i = 1; $i <= 10; $i++) {
54                         // Execute the background script for a step when it isn't finished.
55                         // Execute step 8 and 9 only when $days is defined.
56                         if (!DI::config()->get('system', 'finished-dbclean-'.$i, false) && (($i < 8) || ($i > 9) || ($days > 0))) {
57                                 Worker::add(PRIORITY_LOW, 'DBClean', $i);
58                         }
59                 }
60         }
61
62         /**
63          * Remove orphaned database entries
64          *
65          * @param integer $stage What should be deleted?
66          *
67          * Values for $stage:
68          * ------------------
69          *  1:    Old global item entries from item table without user copy.
70          *  2:    Items without parents.
71          *  3:    Orphaned data from thread table.
72          *  4:    Orphaned data from notify table.
73          *  5:    Orphaned data from notify-threads table.
74          *  6:    Orphaned data from sign table.
75          *  7:    Orphaned data from term table.
76          *  8:    Expired threads.
77          *  9:    Old global item entries from expired threads.
78          * 10:    Old conversations.
79          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
80          */
81         private static function removeOrphans($stage) {
82                 // We split the deletion in many small tasks
83                 $limit = DI::config()->get('system', 'dbclean-expire-limit', 1000);
84
85                 // Get the expire days for step 8 and 9
86                 $days = DI::config()->get('system', 'dbclean-expire-days', 0);
87                 $days_unclaimed = DI::config()->get('system', 'dbclean-expire-unclaimed', 90);
88
89                 if ($days_unclaimed == 0) {
90                         $days_unclaimed = $days;
91                 }
92
93                 if ($stage == 1) {
94                         if ($days_unclaimed <= 0) {
95                                 return;
96                         }
97
98                         $last_id = DI::config()->get('system', 'dbclean-last-id-1', 0);
99
100                         Logger::log("Deleting old global item entries from item table without user copy. Last ID: ".$last_id);
101                         $r = DBA::p("SELECT `id`, `guid` FROM `item` WHERE `uid` = 0 AND
102                                                 NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0) AND
103                                                 `received` < UTC_TIMESTAMP() - INTERVAL ? DAY AND `id` >= ?
104                                         ORDER BY `id` LIMIT ?", $days_unclaimed, $last_id, $limit);
105                         $count = DBA::numRows($r);
106                         if ($count > 0) {
107                                 Logger::log("found global item orphans: ".$count);
108                                 while ($orphan = DBA::fetch($r)) {
109                                         $last_id = $orphan["id"];
110                                         Logger::info('Delete global orphan item', ['id' => $orphan['id'], 'guid' => $orphan['guid']]);
111                                         DBA::delete('item', ['id' => $orphan["id"]]);
112                                 }
113                                 Worker::add(PRIORITY_MEDIUM, 'DBClean', 1, $last_id);
114                         } else {
115                                 Logger::log("No global item orphans found");
116                         }
117                         DBA::close($r);
118                         Logger::log("Done deleting ".$count." old global item entries from item table without user copy. Last ID: ".$last_id);
119
120                         DI::config()->set('system', 'dbclean-last-id-1', $last_id);
121                 } elseif ($stage == 2) {
122                         $last_id = DI::config()->get('system', 'dbclean-last-id-2', 0);
123
124                         Logger::log("Deleting items without parents. Last ID: ".$last_id);
125                         $r = DBA::p("SELECT `id`, `guid` FROM `item`
126                                         WHERE NOT EXISTS (SELECT `id` FROM `item` AS `i` WHERE `item`.`parent` = `i`.`id`)
127                                         AND `id` >= ? ORDER BY `id` LIMIT ?", $last_id, $limit);
128                         $count = DBA::numRows($r);
129                         if ($count > 0) {
130                                 Logger::log("found item orphans without parents: ".$count);
131                                 while ($orphan = DBA::fetch($r)) {
132                                         $last_id = $orphan["id"];
133                                         Logger::info('Delete orphan item', ['id' => $orphan['id'], 'guid' => $orphan['guid']]);
134                                         DBA::delete('item', ['id' => $orphan["id"]]);
135                                 }
136                                 Worker::add(PRIORITY_MEDIUM, 'DBClean', 2, $last_id);
137                         } else {
138                                 Logger::log("No item orphans without parents found");
139                         }
140                         DBA::close($r);
141                         Logger::log("Done deleting ".$count." items without parents. Last ID: ".$last_id);
142
143                         DI::config()->set('system', 'dbclean-last-id-2', $last_id);
144
145                         if ($count < $limit) {
146                                 DI::config()->set('system', 'finished-dbclean-2', true);
147                         }
148                 } elseif ($stage == 3) {
149                         $last_id = DI::config()->get('system', 'dbclean-last-id-3', 0);
150
151                         Logger::log("Deleting orphaned data from thread table. Last ID: ".$last_id);
152                         $r = DBA::p("SELECT `iid` FROM `thread`
153                                         WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `thread`.`iid`) AND `iid` >= ?
154                                         ORDER BY `iid` LIMIT ?", $last_id, $limit);
155                         $count = DBA::numRows($r);
156                         if ($count > 0) {
157                                 Logger::log("found thread orphans: ".$count);
158                                 while ($orphan = DBA::fetch($r)) {
159                                         $last_id = $orphan["iid"];
160                                         DBA::delete('thread', ['iid' => $orphan["iid"]]);
161                                 }
162                                 Worker::add(PRIORITY_MEDIUM, 'DBClean', 3, $last_id);
163                         } else {
164                                 Logger::log("No thread orphans found");
165                         }
166                         DBA::close($r);
167                         Logger::log("Done deleting ".$count." orphaned data from thread table. Last ID: ".$last_id);
168
169                         DI::config()->set('system', 'dbclean-last-id-3', $last_id);
170
171                         if ($count < $limit) {
172                                 DI::config()->set('system', 'finished-dbclean-3', true);
173                         }
174                 } elseif ($stage == 4) {
175                         $last_id = DI::config()->get('system', 'dbclean-last-id-4', 0);
176
177                         Logger::log("Deleting orphaned data from notify table. Last ID: ".$last_id);
178                         $r = DBA::p("SELECT `iid`, `id` FROM `notify`
179                                         WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `notify`.`iid`) AND `id` >= ?
180                                         ORDER BY `id` LIMIT ?", $last_id, $limit);
181                         $count = DBA::numRows($r);
182                         if ($count > 0) {
183                                 Logger::log("found notify orphans: ".$count);
184                                 while ($orphan = DBA::fetch($r)) {
185                                         $last_id = $orphan["id"];
186                                         DBA::delete('notify', ['iid' => $orphan["iid"]]);
187                                 }
188                                 Worker::add(PRIORITY_MEDIUM, 'DBClean', 4, $last_id);
189                         } else {
190                                 Logger::log("No notify orphans found");
191                         }
192                         DBA::close($r);
193                         Logger::log("Done deleting ".$count." orphaned data from notify table. Last ID: ".$last_id);
194
195                         DI::config()->set('system', 'dbclean-last-id-4', $last_id);
196
197                         if ($count < $limit) {
198                                 DI::config()->set('system', 'finished-dbclean-4', true);
199                         }
200                 } elseif ($stage == 5) {
201                         $last_id = DI::config()->get('system', 'dbclean-last-id-5', 0);
202
203                         Logger::log("Deleting orphaned data from notify-threads table. Last ID: ".$last_id);
204                         $r = DBA::p("SELECT `id` FROM `notify-threads`
205                                         WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `notify-threads`.`master-parent-item`) AND `id` >= ?
206                                         ORDER BY `id` LIMIT ?", $last_id, $limit);
207                         $count = DBA::numRows($r);
208                         if ($count > 0) {
209                                 Logger::log("found notify-threads orphans: ".$count);
210                                 while ($orphan = DBA::fetch($r)) {
211                                         $last_id = $orphan["id"];
212                                         DBA::delete('notify-threads', ['id' => $orphan["id"]]);
213                                 }
214                                 Worker::add(PRIORITY_MEDIUM, 'DBClean', 5, $last_id);
215                         } else {
216                                 Logger::log("No notify-threads orphans found");
217                         }
218                         DBA::close($r);
219                         Logger::log("Done deleting ".$count." orphaned data from notify-threads table. Last ID: ".$last_id);
220
221                         DI::config()->set('system', 'dbclean-last-id-5', $last_id);
222
223                         if ($count < $limit) {
224                                 DI::config()->set('system', 'finished-dbclean-5', true);
225                         }
226                 } elseif ($stage == 6) {
227                         $last_id = DI::config()->get('system', 'dbclean-last-id-6', 0);
228
229                         Logger::log("Deleting orphaned data from sign table. Last ID: ".$last_id);
230                         $r = DBA::p("SELECT `iid`, `id` FROM `sign`
231                                         WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `sign`.`iid`) AND `id` >= ?
232                                         ORDER BY `id` LIMIT ?", $last_id, $limit);
233                         $count = DBA::numRows($r);
234                         if ($count > 0) {
235                                 Logger::log("found sign orphans: ".$count);
236                                 while ($orphan = DBA::fetch($r)) {
237                                         $last_id = $orphan["id"];
238                                         DBA::delete('sign', ['iid' => $orphan["iid"]]);
239                                 }
240                                 Worker::add(PRIORITY_MEDIUM, 'DBClean', 6, $last_id);
241                         } else {
242                                 Logger::log("No sign orphans found");
243                         }
244                         DBA::close($r);
245                         Logger::log("Done deleting ".$count." orphaned data from sign table. Last ID: ".$last_id);
246
247                         DI::config()->set('system', 'dbclean-last-id-6', $last_id);
248
249                         if ($count < $limit) {
250                                 DI::config()->set('system', 'finished-dbclean-6', true);
251                         }
252                 } elseif ($stage == 7) {
253                         $last_id = DI::config()->get('system', 'dbclean-last-id-7', 0);
254
255                         Logger::log("Deleting orphaned data from term table. Last ID: ".$last_id);
256                         $r = DBA::p("SELECT `oid`, `tid` FROM `term`
257                                         WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `term`.`oid`) AND `tid` >= ?
258                                         ORDER BY `tid` LIMIT ?", $last_id, $limit);
259                         $count = DBA::numRows($r);
260                         if ($count > 0) {
261                                 Logger::log("found term orphans: ".$count);
262                                 while ($orphan = DBA::fetch($r)) {
263                                         $last_id = $orphan["tid"];
264                                         DBA::delete('term', ['oid' => $orphan["oid"]]);
265                                 }
266                                 Worker::add(PRIORITY_MEDIUM, 'DBClean', 7, $last_id);
267                         } else {
268                                 Logger::log("No term orphans found");
269                         }
270                         DBA::close($r);
271                         Logger::log("Done deleting ".$count." orphaned data from term table. Last ID: ".$last_id);
272
273                         DI::config()->set('system', 'dbclean-last-id-7', $last_id);
274
275                         if ($count < $limit) {
276                                 DI::config()->set('system', 'finished-dbclean-7', true);
277                         }
278                 } elseif ($stage == 8) {
279                         if ($days <= 0) {
280                                 return;
281                         }
282
283                         $last_id = DI::config()->get('system', 'dbclean-last-id-8', 0);
284
285                         Logger::log("Deleting expired threads. Last ID: ".$last_id);
286                         $r = DBA::p("SELECT `thread`.`iid` FROM `thread`
287                                         INNER JOIN `contact` ON `thread`.`contact-id` = `contact`.`id` AND NOT `notify_new_posts`
288                                         WHERE `thread`.`received` < UTC_TIMESTAMP() - INTERVAL ? DAY
289                                                 AND NOT `thread`.`mention` AND NOT `thread`.`starred`
290                                                 AND NOT `thread`.`wall` AND NOT `thread`.`origin`
291                                                 AND `thread`.`uid` != 0 AND `thread`.`iid` >= ?
292                                                 AND NOT `thread`.`iid` IN (SELECT `parent` FROM `item`
293                                                                 WHERE (`item`.`starred` OR (`item`.`resource-id` != '')
294                                                                         OR (`item`.`file` != '') OR (`item`.`event-id` != '')
295                                                                         OR (`item`.`attach` != '') OR `item`.`wall` OR `item`.`origin`)
296                                                                         AND `item`.`parent` = `thread`.`iid`)
297                                         ORDER BY `thread`.`iid` LIMIT ?", $days, $last_id, $limit);
298                         $count = DBA::numRows($r);
299                         if ($count > 0) {
300                                 Logger::log("found expired threads: ".$count);
301                                 while ($thread = DBA::fetch($r)) {
302                                         $last_id = $thread["iid"];
303                                         DBA::delete('thread', ['iid' => $thread["iid"]]);
304                                 }
305                                 Worker::add(PRIORITY_MEDIUM, 'DBClean', 8, $last_id);
306                         } else {
307                                 Logger::log("No expired threads found");
308                         }
309                         DBA::close($r);
310                         Logger::log("Done deleting ".$count." expired threads. Last ID: ".$last_id);
311
312                         DI::config()->set('system', 'dbclean-last-id-8', $last_id);
313                 } elseif ($stage == 9) {
314                         if ($days <= 0) {
315                                 return;
316                         }
317
318                         $last_id = DI::config()->get('system', 'dbclean-last-id-9', 0);
319                         $till_id = DI::config()->get('system', 'dbclean-last-id-8', 0);
320
321                         Logger::log("Deleting old global item entries from expired threads from ID ".$last_id." to ID ".$till_id);
322                         $r = DBA::p("SELECT `id`, `guid` FROM `item` WHERE `uid` = 0 AND
323                                                 NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0) AND
324                                                 `received` < UTC_TIMESTAMP() - INTERVAL 90 DAY AND `id` >= ? AND `id` <= ?
325                                         ORDER BY `id` LIMIT ?", $last_id, $till_id, $limit);
326                         $count = DBA::numRows($r);
327                         if ($count > 0) {
328                                 Logger::log("found global item entries from expired threads: ".$count);
329                                 while ($orphan = DBA::fetch($r)) {
330                                         $last_id = $orphan["id"];
331                                         Logger::info('Delete expired thread item', ['id' => $orphan['id'], 'guid' => $orphan['guid']]);
332                                         DBA::delete('item', ['id' => $orphan["id"]]);
333                                 }
334                                 Worker::add(PRIORITY_MEDIUM, 'DBClean', 9, $last_id);
335                         } else {
336                                 Logger::log("No global item entries from expired threads");
337                         }
338                         DBA::close($r);
339                         Logger::log("Done deleting ".$count." old global item entries from expired threads. Last ID: ".$last_id);
340
341                         DI::config()->set('system', 'dbclean-last-id-9', $last_id);
342                 } elseif ($stage == 10) {
343                         $last_id = DI::config()->get('system', 'dbclean-last-id-10', 0);
344                         $days = intval(DI::config()->get('system', 'dbclean_expire_conversation', 90));
345
346                         Logger::log("Deleting old conversations. Last created: ".$last_id);
347                         $r = DBA::p("SELECT `received`, `item-uri` FROM `conversation`
348                                         WHERE `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
349                                         ORDER BY `received` LIMIT ?", $days, $limit);
350                         $count = DBA::numRows($r);
351                         if ($count > 0) {
352                                 Logger::log("found old conversations: ".$count);
353                                 while ($orphan = DBA::fetch($r)) {
354                                         $last_id = $orphan["received"];
355                                         DBA::delete('conversation', ['item-uri' => $orphan["item-uri"]]);
356                                 }
357                                 Worker::add(PRIORITY_MEDIUM, 'DBClean', 10, $last_id);
358                         } else {
359                                 Logger::log("No old conversations found");
360                         }
361                         DBA::close($r);
362                         Logger::log("Done deleting ".$count." conversations. Last created: ".$last_id);
363
364                         DI::config()->set('system', 'dbclean-last-id-10', $last_id);
365                 }
366         }
367 }