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