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.
7 namespace Friendica\Worker;
9 use Friendica\Core\Config;
10 use Friendica\Core\Worker;
11 use Friendica\Database\DBA;
13 require_once 'include/dba.php';
16 public static function execute($stage = 0) {
18 if (!Config::get('system', 'dbclean', false)) {
23 self::forkCleanProcess();
25 self::removeOrphans($stage);
30 * @brief Fork the different DBClean processes
32 private static function forkCleanProcess() {
33 // Get the expire days for step 8 and 9
34 $days = Config::get('system', 'dbclean-expire-days', 0);
36 for ($i = 1; $i <= 10; $i++) {
37 // Execute the background script for a step when it isn't finished.
38 // Execute step 8 and 9 only when $days is defined.
39 if (!Config::get('system', 'finished-dbclean-'.$i, false) && (($i < 8) || ($i > 9) || ($days > 0))) {
40 Worker::add(PRIORITY_LOW, 'DBClean', $i);
46 * @brief Remove orphaned database entries
47 * @param integer $stage What should be deleted?
51 * 1: Old global item entries from item table without user copy.
52 * 2: Items without parents.
53 * 3: Orphaned data from thread table.
54 * 4: Orphaned data from notify table.
55 * 5: Orphaned data from notify-threads table.
56 * 6: Orphaned data from sign table.
57 * 7: Orphaned data from term table.
59 * 9: Old global item entries from expired threads.
60 * 10: Old conversations.
62 private static function removeOrphans($stage) {
65 // We split the deletion in many small tasks
68 // Get the expire days for step 8 and 9
69 $days = Config::get('system', 'dbclean-expire-days', 0);
70 $days_unclaimed = Config::get('system', 'dbclean-expire-unclaimed', 90);
72 if ($days_unclaimed == 0) {
73 $days_unclaimed = $days;
77 if ($days_unclaimed <= 0) {
81 $last_id = Config::get('system', 'dbclean-last-id-1', 0);
83 logger("Deleting old global item entries from item table without user copy. Last ID: ".$last_id);
84 $r = DBA::p("SELECT `id` FROM `item` WHERE `uid` = 0 AND
85 NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0) AND
86 `received` < UTC_TIMESTAMP() - INTERVAL ? DAY AND `id` >= ?
87 ORDER BY `id` LIMIT ?", $days_unclaimed, $last_id, $limit);
88 $count = DBA::numRows($r);
90 logger("found global item orphans: ".$count);
91 while ($orphan = DBA::fetch($r)) {
92 $last_id = $orphan["id"];
93 DBA::delete('item', ['id' => $orphan["id"]]);
95 Worker::add(PRIORITY_MEDIUM, 'DBClean', 1, $last_id);
97 logger("No global item orphans found");
100 logger("Done deleting ".$count." old global item entries from item table without user copy. Last ID: ".$last_id);
102 Config::set('system', 'dbclean-last-id-1', $last_id);
103 } elseif ($stage == 2) {
104 $last_id = Config::get('system', 'dbclean-last-id-2', 0);
106 logger("Deleting items without parents. Last ID: ".$last_id);
107 $r = DBA::p("SELECT `id` FROM `item`
108 WHERE NOT EXISTS (SELECT `id` FROM `item` AS `i` WHERE `item`.`parent` = `i`.`id`)
109 AND `id` >= ? ORDER BY `id` LIMIT ?", $last_id, $limit);
110 $count = DBA::numRows($r);
112 logger("found item orphans without parents: ".$count);
113 while ($orphan = DBA::fetch($r)) {
114 $last_id = $orphan["id"];
115 DBA::delete('item', ['id' => $orphan["id"]]);
117 Worker::add(PRIORITY_MEDIUM, 'DBClean', 2, $last_id);
119 logger("No item orphans without parents found");
122 logger("Done deleting ".$count." items without parents. Last ID: ".$last_id);
124 Config::set('system', 'dbclean-last-id-2', $last_id);
126 if ($count < $limit) {
127 Config::set('system', 'finished-dbclean-2', true);
129 } elseif ($stage == 3) {
130 $last_id = Config::get('system', 'dbclean-last-id-3', 0);
132 logger("Deleting orphaned data from thread table. Last ID: ".$last_id);
133 $r = DBA::p("SELECT `iid` FROM `thread`
134 WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `thread`.`iid`) AND `iid` >= ?
135 ORDER BY `iid` LIMIT ?", $last_id, $limit);
136 $count = DBA::numRows($r);
138 logger("found thread orphans: ".$count);
139 while ($orphan = DBA::fetch($r)) {
140 $last_id = $orphan["iid"];
141 DBA::delete('thread', ['iid' => $orphan["iid"]]);
143 Worker::add(PRIORITY_MEDIUM, 'DBClean', 3, $last_id);
145 logger("No thread orphans found");
148 logger("Done deleting ".$count." orphaned data from thread table. Last ID: ".$last_id);
150 Config::set('system', 'dbclean-last-id-3', $last_id);
152 if ($count < $limit) {
153 Config::set('system', 'finished-dbclean-3', true);
155 } elseif ($stage == 4) {
156 $last_id = Config::get('system', 'dbclean-last-id-4', 0);
158 logger("Deleting orphaned data from notify table. Last ID: ".$last_id);
159 $r = DBA::p("SELECT `iid`, `id` FROM `notify`
160 WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `notify`.`iid`) AND `id` >= ?
161 ORDER BY `id` LIMIT ?", $last_id, $limit);
162 $count = DBA::numRows($r);
164 logger("found notify orphans: ".$count);
165 while ($orphan = DBA::fetch($r)) {
166 $last_id = $orphan["id"];
167 DBA::delete('notify', ['iid' => $orphan["iid"]]);
169 Worker::add(PRIORITY_MEDIUM, 'DBClean', 4, $last_id);
171 logger("No notify orphans found");
174 logger("Done deleting ".$count." orphaned data from notify table. Last ID: ".$last_id);
176 Config::set('system', 'dbclean-last-id-4', $last_id);
178 if ($count < $limit) {
179 Config::set('system', 'finished-dbclean-4', true);
181 } elseif ($stage == 5) {
182 $last_id = Config::get('system', 'dbclean-last-id-5', 0);
184 logger("Deleting orphaned data from notify-threads table. Last ID: ".$last_id);
185 $r = DBA::p("SELECT `id` FROM `notify-threads`
186 WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `notify-threads`.`master-parent-item`) AND `id` >= ?
187 ORDER BY `id` LIMIT ?", $last_id, $limit);
188 $count = DBA::numRows($r);
190 logger("found notify-threads orphans: ".$count);
191 while ($orphan = DBA::fetch($r)) {
192 $last_id = $orphan["id"];
193 DBA::delete('notify-threads', ['id' => $orphan["id"]]);
195 Worker::add(PRIORITY_MEDIUM, 'DBClean', 5, $last_id);
197 logger("No notify-threads orphans found");
200 logger("Done deleting ".$count." orphaned data from notify-threads table. Last ID: ".$last_id);
202 Config::set('system', 'dbclean-last-id-5', $last_id);
204 if ($count < $limit) {
205 Config::set('system', 'finished-dbclean-5', true);
207 } elseif ($stage == 6) {
208 $last_id = Config::get('system', 'dbclean-last-id-6', 0);
210 logger("Deleting orphaned data from sign table. Last ID: ".$last_id);
211 $r = DBA::p("SELECT `iid`, `id` FROM `sign`
212 WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `sign`.`iid`) AND `id` >= ?
213 ORDER BY `id` LIMIT ?", $last_id, $limit);
214 $count = DBA::numRows($r);
216 logger("found sign orphans: ".$count);
217 while ($orphan = DBA::fetch($r)) {
218 $last_id = $orphan["id"];
219 DBA::delete('sign', ['iid' => $orphan["iid"]]);
221 Worker::add(PRIORITY_MEDIUM, 'DBClean', 6, $last_id);
223 logger("No sign orphans found");
226 logger("Done deleting ".$count." orphaned data from sign table. Last ID: ".$last_id);
228 Config::set('system', 'dbclean-last-id-6', $last_id);
230 if ($count < $limit) {
231 Config::set('system', 'finished-dbclean-6', true);
233 } elseif ($stage == 7) {
234 $last_id = Config::get('system', 'dbclean-last-id-7', 0);
236 logger("Deleting orphaned data from term table. Last ID: ".$last_id);
237 $r = DBA::p("SELECT `oid`, `tid` FROM `term`
238 WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `term`.`oid`) AND `tid` >= ?
239 ORDER BY `tid` LIMIT ?", $last_id, $limit);
240 $count = DBA::numRows($r);
242 logger("found term orphans: ".$count);
243 while ($orphan = DBA::fetch($r)) {
244 $last_id = $orphan["tid"];
245 DBA::delete('term', ['oid' => $orphan["oid"]]);
247 Worker::add(PRIORITY_MEDIUM, 'DBClean', 7, $last_id);
249 logger("No term orphans found");
252 logger("Done deleting ".$count." orphaned data from term table. Last ID: ".$last_id);
254 Config::set('system', 'dbclean-last-id-7', $last_id);
256 if ($count < $limit) {
257 Config::set('system', 'finished-dbclean-7', true);
259 } elseif ($stage == 8) {
264 $last_id = Config::get('system', 'dbclean-last-id-8', 0);
266 logger("Deleting expired threads. Last ID: ".$last_id);
267 $r = DBA::p("SELECT `thread`.`iid` FROM `thread`
268 INNER JOIN `contact` ON `thread`.`contact-id` = `contact`.`id` AND NOT `notify_new_posts`
269 WHERE `thread`.`received` < UTC_TIMESTAMP() - INTERVAL ? DAY
270 AND NOT `thread`.`mention` AND NOT `thread`.`starred`
271 AND NOT `thread`.`wall` AND NOT `thread`.`origin`
272 AND `thread`.`uid` != 0 AND `thread`.`iid` >= ?
273 AND NOT `thread`.`iid` IN (SELECT `parent` FROM `item`
274 WHERE (`item`.`starred` OR (`item`.`resource-id` != '')
275 OR (`item`.`file` != '') OR (`item`.`event-id` != '')
276 OR (`item`.`attach` != '') OR `item`.`wall` OR `item`.`origin`)
277 AND `item`.`parent` = `thread`.`iid`)
278 ORDER BY `thread`.`iid` LIMIT 1000", $days, $last_id);
279 $count = DBA::numRows($r);
281 logger("found expired threads: ".$count);
282 while ($thread = DBA::fetch($r)) {
283 $last_id = $thread["iid"];
284 DBA::delete('thread', ['iid' => $thread["iid"]]);
286 Worker::add(PRIORITY_MEDIUM, 'DBClean', 8, $last_id);
288 logger("No expired threads found");
291 logger("Done deleting ".$count." expired threads. Last ID: ".$last_id);
293 Config::set('system', 'dbclean-last-id-8', $last_id);
294 } elseif ($stage == 9) {
299 $last_id = Config::get('system', 'dbclean-last-id-9', 0);
300 $till_id = Config::get('system', 'dbclean-last-id-8', 0);
302 logger("Deleting old global item entries from expired threads from ID ".$last_id." to ID ".$till_id);
303 $r = DBA::p("SELECT `id` FROM `item` WHERE `uid` = 0 AND
304 NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0) AND
305 `received` < UTC_TIMESTAMP() - INTERVAL 90 DAY AND `id` >= ? AND `id` <= ?
306 ORDER BY `id` LIMIT ?", $last_id, $till_id, $limit);
307 $count = DBA::numRows($r);
309 logger("found global item entries from expired threads: ".$count);
310 while ($orphan = DBA::fetch($r)) {
311 $last_id = $orphan["id"];
312 DBA::delete('item', ['id' => $orphan["id"]]);
314 Worker::add(PRIORITY_MEDIUM, 'DBClean', 9, $last_id);
316 logger("No global item entries from expired threads");
319 logger("Done deleting ".$count." old global item entries from expired threads. Last ID: ".$last_id);
321 Config::set('system', 'dbclean-last-id-9', $last_id);
322 } elseif ($stage == 10) {
323 $last_id = Config::get('system', 'dbclean-last-id-10', 0);
324 $days = intval(Config::get('system', 'dbclean_expire_conversation', 90));
326 logger("Deleting old conversations. Last created: ".$last_id);
327 $r = DBA::p("SELECT `received`, `item-uri` FROM `conversation`
328 WHERE `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
329 ORDER BY `received` LIMIT ?", $days, $limit);
330 $count = DBA::numRows($r);
332 logger("found old conversations: ".$count);
333 while ($orphan = DBA::fetch($r)) {
334 $last_id = $orphan["received"];
335 DBA::delete('conversation', ['item-uri' => $orphan["item-uri"]]);
337 Worker::add(PRIORITY_MEDIUM, 'DBClean', 10, $last_id);
339 logger("No old conversations found");
342 logger("Done deleting ".$count." conversations. Last created: ".$last_id);
344 Config::set('system', 'dbclean-last-id-10', $last_id);