]> git.mxchange.org Git - friendica.git/blob - src/Worker/DBClean.php
Merge pull request #8344 from MrPetovan/bug/8339-remote-follow-local-profile
[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` 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                                         DBA::delete('item', ['id' => $orphan["id"]]);
111                                 }
112                                 Worker::add(PRIORITY_MEDIUM, 'DBClean', 1, $last_id);
113                         } else {
114                                 Logger::log("No global item orphans found");
115                         }
116                         DBA::close($r);
117                         Logger::log("Done deleting ".$count." old global item entries from item table without user copy. Last ID: ".$last_id);
118
119                         DI::config()->set('system', 'dbclean-last-id-1', $last_id);
120                 } elseif ($stage == 2) {
121                         $last_id = DI::config()->get('system', 'dbclean-last-id-2', 0);
122
123                         Logger::log("Deleting items without parents. Last ID: ".$last_id);
124                         $r = DBA::p("SELECT `id` FROM `item`
125                                         WHERE NOT EXISTS (SELECT `id` FROM `item` AS `i` WHERE `item`.`parent` = `i`.`id`)
126                                         AND `id` >= ? ORDER BY `id` LIMIT ?", $last_id, $limit);
127                         $count = DBA::numRows($r);
128                         if ($count > 0) {
129                                 Logger::log("found item orphans without parents: ".$count);
130                                 while ($orphan = DBA::fetch($r)) {
131                                         $last_id = $orphan["id"];
132                                         DBA::delete('item', ['id' => $orphan["id"]]);
133                                 }
134                                 Worker::add(PRIORITY_MEDIUM, 'DBClean', 2, $last_id);
135                         } else {
136                                 Logger::log("No item orphans without parents found");
137                         }
138                         DBA::close($r);
139                         Logger::log("Done deleting ".$count." items without parents. Last ID: ".$last_id);
140
141                         DI::config()->set('system', 'dbclean-last-id-2', $last_id);
142
143                         if ($count < $limit) {
144                                 DI::config()->set('system', 'finished-dbclean-2', true);
145                         }
146                 } elseif ($stage == 3) {
147                         $last_id = DI::config()->get('system', 'dbclean-last-id-3', 0);
148
149                         Logger::log("Deleting orphaned data from thread table. Last ID: ".$last_id);
150                         $r = DBA::p("SELECT `iid` FROM `thread`
151                                         WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `thread`.`iid`) AND `iid` >= ?
152                                         ORDER BY `iid` LIMIT ?", $last_id, $limit);
153                         $count = DBA::numRows($r);
154                         if ($count > 0) {
155                                 Logger::log("found thread orphans: ".$count);
156                                 while ($orphan = DBA::fetch($r)) {
157                                         $last_id = $orphan["iid"];
158                                         DBA::delete('thread', ['iid' => $orphan["iid"]]);
159                                 }
160                                 Worker::add(PRIORITY_MEDIUM, 'DBClean', 3, $last_id);
161                         } else {
162                                 Logger::log("No thread orphans found");
163                         }
164                         DBA::close($r);
165                         Logger::log("Done deleting ".$count." orphaned data from thread table. Last ID: ".$last_id);
166
167                         DI::config()->set('system', 'dbclean-last-id-3', $last_id);
168
169                         if ($count < $limit) {
170                                 DI::config()->set('system', 'finished-dbclean-3', true);
171                         }
172                 } elseif ($stage == 4) {
173                         $last_id = DI::config()->get('system', 'dbclean-last-id-4', 0);
174
175                         Logger::log("Deleting orphaned data from notify table. Last ID: ".$last_id);
176                         $r = DBA::p("SELECT `iid`, `id` FROM `notify`
177                                         WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `notify`.`iid`) AND `id` >= ?
178                                         ORDER BY `id` LIMIT ?", $last_id, $limit);
179                         $count = DBA::numRows($r);
180                         if ($count > 0) {
181                                 Logger::log("found notify orphans: ".$count);
182                                 while ($orphan = DBA::fetch($r)) {
183                                         $last_id = $orphan["id"];
184                                         DBA::delete('notify', ['iid' => $orphan["iid"]]);
185                                 }
186                                 Worker::add(PRIORITY_MEDIUM, 'DBClean', 4, $last_id);
187                         } else {
188                                 Logger::log("No notify orphans found");
189                         }
190                         DBA::close($r);
191                         Logger::log("Done deleting ".$count." orphaned data from notify table. Last ID: ".$last_id);
192
193                         DI::config()->set('system', 'dbclean-last-id-4', $last_id);
194
195                         if ($count < $limit) {
196                                 DI::config()->set('system', 'finished-dbclean-4', true);
197                         }
198                 } elseif ($stage == 5) {
199                         $last_id = DI::config()->get('system', 'dbclean-last-id-5', 0);
200
201                         Logger::log("Deleting orphaned data from notify-threads table. Last ID: ".$last_id);
202                         $r = DBA::p("SELECT `id` FROM `notify-threads`
203                                         WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `notify-threads`.`master-parent-item`) AND `id` >= ?
204                                         ORDER BY `id` LIMIT ?", $last_id, $limit);
205                         $count = DBA::numRows($r);
206                         if ($count > 0) {
207                                 Logger::log("found notify-threads orphans: ".$count);
208                                 while ($orphan = DBA::fetch($r)) {
209                                         $last_id = $orphan["id"];
210                                         DBA::delete('notify-threads', ['id' => $orphan["id"]]);
211                                 }
212                                 Worker::add(PRIORITY_MEDIUM, 'DBClean', 5, $last_id);
213                         } else {
214                                 Logger::log("No notify-threads orphans found");
215                         }
216                         DBA::close($r);
217                         Logger::log("Done deleting ".$count." orphaned data from notify-threads table. Last ID: ".$last_id);
218
219                         DI::config()->set('system', 'dbclean-last-id-5', $last_id);
220
221                         if ($count < $limit) {
222                                 DI::config()->set('system', 'finished-dbclean-5', true);
223                         }
224                 } elseif ($stage == 6) {
225                         $last_id = DI::config()->get('system', 'dbclean-last-id-6', 0);
226
227                         Logger::log("Deleting orphaned data from sign table. Last ID: ".$last_id);
228                         $r = DBA::p("SELECT `iid`, `id` FROM `sign`
229                                         WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `sign`.`iid`) AND `id` >= ?
230                                         ORDER BY `id` LIMIT ?", $last_id, $limit);
231                         $count = DBA::numRows($r);
232                         if ($count > 0) {
233                                 Logger::log("found sign orphans: ".$count);
234                                 while ($orphan = DBA::fetch($r)) {
235                                         $last_id = $orphan["id"];
236                                         DBA::delete('sign', ['iid' => $orphan["iid"]]);
237                                 }
238                                 Worker::add(PRIORITY_MEDIUM, 'DBClean', 6, $last_id);
239                         } else {
240                                 Logger::log("No sign orphans found");
241                         }
242                         DBA::close($r);
243                         Logger::log("Done deleting ".$count." orphaned data from sign table. Last ID: ".$last_id);
244
245                         DI::config()->set('system', 'dbclean-last-id-6', $last_id);
246
247                         if ($count < $limit) {
248                                 DI::config()->set('system', 'finished-dbclean-6', true);
249                         }
250                 } elseif ($stage == 7) {
251                         $last_id = DI::config()->get('system', 'dbclean-last-id-7', 0);
252
253                         Logger::log("Deleting orphaned data from term table. Last ID: ".$last_id);
254                         $r = DBA::p("SELECT `oid`, `tid` FROM `term`
255                                         WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `term`.`oid`) AND `tid` >= ?
256                                         ORDER BY `tid` LIMIT ?", $last_id, $limit);
257                         $count = DBA::numRows($r);
258                         if ($count > 0) {
259                                 Logger::log("found term orphans: ".$count);
260                                 while ($orphan = DBA::fetch($r)) {
261                                         $last_id = $orphan["tid"];
262                                         DBA::delete('term', ['oid' => $orphan["oid"]]);
263                                 }
264                                 Worker::add(PRIORITY_MEDIUM, 'DBClean', 7, $last_id);
265                         } else {
266                                 Logger::log("No term orphans found");
267                         }
268                         DBA::close($r);
269                         Logger::log("Done deleting ".$count." orphaned data from term table. Last ID: ".$last_id);
270
271                         DI::config()->set('system', 'dbclean-last-id-7', $last_id);
272
273                         if ($count < $limit) {
274                                 DI::config()->set('system', 'finished-dbclean-7', true);
275                         }
276                 } elseif ($stage == 8) {
277                         if ($days <= 0) {
278                                 return;
279                         }
280
281                         $last_id = DI::config()->get('system', 'dbclean-last-id-8', 0);
282
283                         Logger::log("Deleting expired threads. Last ID: ".$last_id);
284                         $r = DBA::p("SELECT `thread`.`iid` FROM `thread`
285                                         INNER JOIN `contact` ON `thread`.`contact-id` = `contact`.`id` AND NOT `notify_new_posts`
286                                         WHERE `thread`.`received` < UTC_TIMESTAMP() - INTERVAL ? DAY
287                                                 AND NOT `thread`.`mention` AND NOT `thread`.`starred`
288                                                 AND NOT `thread`.`wall` AND NOT `thread`.`origin`
289                                                 AND `thread`.`uid` != 0 AND `thread`.`iid` >= ?
290                                                 AND NOT `thread`.`iid` IN (SELECT `parent` FROM `item`
291                                                                 WHERE (`item`.`starred` OR (`item`.`resource-id` != '')
292                                                                         OR (`item`.`file` != '') OR (`item`.`event-id` != '')
293                                                                         OR (`item`.`attach` != '') OR `item`.`wall` OR `item`.`origin`)
294                                                                         AND `item`.`parent` = `thread`.`iid`)
295                                         ORDER BY `thread`.`iid` LIMIT ?", $days, $last_id, $limit);
296                         $count = DBA::numRows($r);
297                         if ($count > 0) {
298                                 Logger::log("found expired threads: ".$count);
299                                 while ($thread = DBA::fetch($r)) {
300                                         $last_id = $thread["iid"];
301                                         DBA::delete('thread', ['iid' => $thread["iid"]]);
302                                 }
303                                 Worker::add(PRIORITY_MEDIUM, 'DBClean', 8, $last_id);
304                         } else {
305                                 Logger::log("No expired threads found");
306                         }
307                         DBA::close($r);
308                         Logger::log("Done deleting ".$count." expired threads. Last ID: ".$last_id);
309
310                         DI::config()->set('system', 'dbclean-last-id-8', $last_id);
311                 } elseif ($stage == 9) {
312                         if ($days <= 0) {
313                                 return;
314                         }
315
316                         $last_id = DI::config()->get('system', 'dbclean-last-id-9', 0);
317                         $till_id = DI::config()->get('system', 'dbclean-last-id-8', 0);
318
319                         Logger::log("Deleting old global item entries from expired threads from ID ".$last_id." to ID ".$till_id);
320                         $r = DBA::p("SELECT `id` FROM `item` WHERE `uid` = 0 AND
321                                                 NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0) AND
322                                                 `received` < UTC_TIMESTAMP() - INTERVAL 90 DAY AND `id` >= ? AND `id` <= ?
323                                         ORDER BY `id` LIMIT ?", $last_id, $till_id, $limit);
324                         $count = DBA::numRows($r);
325                         if ($count > 0) {
326                                 Logger::log("found global item entries from expired threads: ".$count);
327                                 while ($orphan = DBA::fetch($r)) {
328                                         $last_id = $orphan["id"];
329                                         DBA::delete('item', ['id' => $orphan["id"]]);
330                                 }
331                                 Worker::add(PRIORITY_MEDIUM, 'DBClean', 9, $last_id);
332                         } else {
333                                 Logger::log("No global item entries from expired threads");
334                         }
335                         DBA::close($r);
336                         Logger::log("Done deleting ".$count." old global item entries from expired threads. Last ID: ".$last_id);
337
338                         DI::config()->set('system', 'dbclean-last-id-9', $last_id);
339                 } elseif ($stage == 10) {
340                         $last_id = DI::config()->get('system', 'dbclean-last-id-10', 0);
341                         $days = intval(DI::config()->get('system', 'dbclean_expire_conversation', 90));
342
343                         Logger::log("Deleting old conversations. Last created: ".$last_id);
344                         $r = DBA::p("SELECT `received`, `item-uri` FROM `conversation`
345                                         WHERE `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
346                                         ORDER BY `received` LIMIT ?", $days, $limit);
347                         $count = DBA::numRows($r);
348                         if ($count > 0) {
349                                 Logger::log("found old conversations: ".$count);
350                                 while ($orphan = DBA::fetch($r)) {
351                                         $last_id = $orphan["received"];
352                                         DBA::delete('conversation', ['item-uri' => $orphan["item-uri"]]);
353                                 }
354                                 Worker::add(PRIORITY_MEDIUM, 'DBClean', 10, $last_id);
355                         } else {
356                                 Logger::log("No old conversations found");
357                         }
358                         DBA::close($r);
359                         Logger::log("Done deleting ".$count." conversations. Last created: ".$last_id);
360
361                         DI::config()->set('system', 'dbclean-last-id-10', $last_id);
362                 }
363         }
364 }