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\Logger;
11 use Friendica\Core\Worker;
12 use Friendica\Database\DBA;
14 require_once 'include/dba.php';
17 public static function execute($stage = 0) {
19 if (!Config::get('system', 'dbclean', false)) {
24 self::forkCleanProcess();
26 self::removeOrphans($stage);
31 * @brief Fork the different DBClean processes
33 private static function forkCleanProcess() {
34 // Get the expire days for step 8 and 9
35 $days = Config::get('system', 'dbclean-expire-days', 0);
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);
47 * @brief Remove orphaned database entries
48 * @param integer $stage What should be deleted?
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.
60 * 9: Old global item entries from expired threads.
61 * 10: Old conversations.
63 private static function removeOrphans($stage) {
66 // We split the deletion in many small tasks
67 $limit = Config::get('system', 'dbclean-expire-limit', 1000);
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);
73 if ($days_unclaimed == 0) {
74 $days_unclaimed = $days;
78 if ($days_unclaimed <= 0) {
82 $last_id = Config::get('system', 'dbclean-last-id-1', 0);
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);
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"]]);
96 Worker::add(PRIORITY_MEDIUM, 'DBClean', 1, $last_id);
98 Logger::log("No global item orphans found");
101 Logger::log("Done deleting ".$count." old global item entries from item table without user copy. Last ID: ".$last_id);
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);
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);
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"]]);
118 Worker::add(PRIORITY_MEDIUM, 'DBClean', 2, $last_id);
120 Logger::log("No item orphans without parents found");
123 Logger::log("Done deleting ".$count." items without parents. Last ID: ".$last_id);
125 Config::set('system', 'dbclean-last-id-2', $last_id);
127 if ($count < $limit) {
128 Config::set('system', 'finished-dbclean-2', true);
130 } elseif ($stage == 3) {
131 $last_id = Config::get('system', 'dbclean-last-id-3', 0);
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);
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"]]);
144 Worker::add(PRIORITY_MEDIUM, 'DBClean', 3, $last_id);
146 Logger::log("No thread orphans found");
149 Logger::log("Done deleting ".$count." orphaned data from thread table. Last ID: ".$last_id);
151 Config::set('system', 'dbclean-last-id-3', $last_id);
153 if ($count < $limit) {
154 Config::set('system', 'finished-dbclean-3', true);
156 } elseif ($stage == 4) {
157 $last_id = Config::get('system', 'dbclean-last-id-4', 0);
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);
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"]]);
170 Worker::add(PRIORITY_MEDIUM, 'DBClean', 4, $last_id);
172 Logger::log("No notify orphans found");
175 Logger::log("Done deleting ".$count." orphaned data from notify table. Last ID: ".$last_id);
177 Config::set('system', 'dbclean-last-id-4', $last_id);
179 if ($count < $limit) {
180 Config::set('system', 'finished-dbclean-4', true);
182 } elseif ($stage == 5) {
183 $last_id = Config::get('system', 'dbclean-last-id-5', 0);
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);
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"]]);
196 Worker::add(PRIORITY_MEDIUM, 'DBClean', 5, $last_id);
198 Logger::log("No notify-threads orphans found");
201 Logger::log("Done deleting ".$count." orphaned data from notify-threads table. Last ID: ".$last_id);
203 Config::set('system', 'dbclean-last-id-5', $last_id);
205 if ($count < $limit) {
206 Config::set('system', 'finished-dbclean-5', true);
208 } elseif ($stage == 6) {
209 $last_id = Config::get('system', 'dbclean-last-id-6', 0);
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);
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"]]);
222 Worker::add(PRIORITY_MEDIUM, 'DBClean', 6, $last_id);
224 Logger::log("No sign orphans found");
227 Logger::log("Done deleting ".$count." orphaned data from sign table. Last ID: ".$last_id);
229 Config::set('system', 'dbclean-last-id-6', $last_id);
231 if ($count < $limit) {
232 Config::set('system', 'finished-dbclean-6', true);
234 } elseif ($stage == 7) {
235 $last_id = Config::get('system', 'dbclean-last-id-7', 0);
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);
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"]]);
248 Worker::add(PRIORITY_MEDIUM, 'DBClean', 7, $last_id);
250 Logger::log("No term orphans found");
253 Logger::log("Done deleting ".$count." orphaned data from term table. Last ID: ".$last_id);
255 Config::set('system', 'dbclean-last-id-7', $last_id);
257 if ($count < $limit) {
258 Config::set('system', 'finished-dbclean-7', true);
260 } elseif ($stage == 8) {
265 $last_id = Config::get('system', 'dbclean-last-id-8', 0);
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);
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"]]);
287 Worker::add(PRIORITY_MEDIUM, 'DBClean', 8, $last_id);
289 Logger::log("No expired threads found");
292 Logger::log("Done deleting ".$count." expired threads. Last ID: ".$last_id);
294 Config::set('system', 'dbclean-last-id-8', $last_id);
295 } elseif ($stage == 9) {
300 $last_id = Config::get('system', 'dbclean-last-id-9', 0);
301 $till_id = Config::get('system', 'dbclean-last-id-8', 0);
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);
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"]]);
315 Worker::add(PRIORITY_MEDIUM, 'DBClean', 9, $last_id);
317 Logger::log("No global item entries from expired threads");
320 Logger::log("Done deleting ".$count." old global item entries from expired threads. Last ID: ".$last_id);
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));
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);
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"]]);
338 Worker::add(PRIORITY_MEDIUM, 'DBClean', 10, $last_id);
340 Logger::log("No old conversations found");
343 Logger::log("Done deleting ".$count." conversations. Last created: ".$last_id);
345 Config::set('system', 'dbclean-last-id-10', $last_id);