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