3 * @file src/Worker/DBClean.php
4 * 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;
15 public static function execute($stage = 0) {
17 if (!Config::get('system', 'dbclean', false)) {
22 self::forkCleanProcess();
24 self::removeOrphans($stage);
29 * Fork the different DBClean processes
31 private static function forkCleanProcess() {
32 // Get the expire days for step 8 and 9
33 $days = Config::get('system', 'dbclean-expire-days', 0);
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);
45 * 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.
61 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
63 private static function removeOrphans($stage) {
64 // We split the deletion in many small tasks
65 $limit = Config::get('system', 'dbclean-expire-limit', 1000);
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);
71 if ($days_unclaimed == 0) {
72 $days_unclaimed = $days;
76 if ($days_unclaimed <= 0) {
80 $last_id = Config::get('system', 'dbclean-last-id-1', 0);
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);
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"]]);
94 Worker::add(PRIORITY_MEDIUM, 'DBClean', 1, $last_id);
96 Logger::log("No global item orphans found");
99 Logger::log("Done deleting ".$count." old global item entries from item table without user copy. Last ID: ".$last_id);
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);
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);
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"]]);
116 Worker::add(PRIORITY_MEDIUM, 'DBClean', 2, $last_id);
118 Logger::log("No item orphans without parents found");
121 Logger::log("Done deleting ".$count." items without parents. Last ID: ".$last_id);
123 Config::set('system', 'dbclean-last-id-2', $last_id);
125 if ($count < $limit) {
126 Config::set('system', 'finished-dbclean-2', true);
128 } elseif ($stage == 3) {
129 $last_id = Config::get('system', 'dbclean-last-id-3', 0);
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);
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"]]);
142 Worker::add(PRIORITY_MEDIUM, 'DBClean', 3, $last_id);
144 Logger::log("No thread orphans found");
147 Logger::log("Done deleting ".$count." orphaned data from thread table. Last ID: ".$last_id);
149 Config::set('system', 'dbclean-last-id-3', $last_id);
151 if ($count < $limit) {
152 Config::set('system', 'finished-dbclean-3', true);
154 } elseif ($stage == 4) {
155 $last_id = Config::get('system', 'dbclean-last-id-4', 0);
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);
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"]]);
168 Worker::add(PRIORITY_MEDIUM, 'DBClean', 4, $last_id);
170 Logger::log("No notify orphans found");
173 Logger::log("Done deleting ".$count." orphaned data from notify table. Last ID: ".$last_id);
175 Config::set('system', 'dbclean-last-id-4', $last_id);
177 if ($count < $limit) {
178 Config::set('system', 'finished-dbclean-4', true);
180 } elseif ($stage == 5) {
181 $last_id = Config::get('system', 'dbclean-last-id-5', 0);
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);
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"]]);
194 Worker::add(PRIORITY_MEDIUM, 'DBClean', 5, $last_id);
196 Logger::log("No notify-threads orphans found");
199 Logger::log("Done deleting ".$count." orphaned data from notify-threads table. Last ID: ".$last_id);
201 Config::set('system', 'dbclean-last-id-5', $last_id);
203 if ($count < $limit) {
204 Config::set('system', 'finished-dbclean-5', true);
206 } elseif ($stage == 6) {
207 $last_id = Config::get('system', 'dbclean-last-id-6', 0);
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);
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"]]);
220 Worker::add(PRIORITY_MEDIUM, 'DBClean', 6, $last_id);
222 Logger::log("No sign orphans found");
225 Logger::log("Done deleting ".$count." orphaned data from sign table. Last ID: ".$last_id);
227 Config::set('system', 'dbclean-last-id-6', $last_id);
229 if ($count < $limit) {
230 Config::set('system', 'finished-dbclean-6', true);
232 } elseif ($stage == 7) {
233 $last_id = Config::get('system', 'dbclean-last-id-7', 0);
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);
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"]]);
246 Worker::add(PRIORITY_MEDIUM, 'DBClean', 7, $last_id);
248 Logger::log("No term orphans found");
251 Logger::log("Done deleting ".$count." orphaned data from term table. Last ID: ".$last_id);
253 Config::set('system', 'dbclean-last-id-7', $last_id);
255 if ($count < $limit) {
256 Config::set('system', 'finished-dbclean-7', true);
258 } elseif ($stage == 8) {
263 $last_id = Config::get('system', 'dbclean-last-id-8', 0);
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);
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"]]);
285 Worker::add(PRIORITY_MEDIUM, 'DBClean', 8, $last_id);
287 Logger::log("No expired threads found");
290 Logger::log("Done deleting ".$count." expired threads. Last ID: ".$last_id);
292 Config::set('system', 'dbclean-last-id-8', $last_id);
293 } elseif ($stage == 9) {
298 $last_id = Config::get('system', 'dbclean-last-id-9', 0);
299 $till_id = Config::get('system', 'dbclean-last-id-8', 0);
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);
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"]]);
313 Worker::add(PRIORITY_MEDIUM, 'DBClean', 9, $last_id);
315 Logger::log("No global item entries from expired threads");
318 Logger::log("Done deleting ".$count." old global item entries from expired threads. Last ID: ".$last_id);
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));
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);
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"]]);
336 Worker::add(PRIORITY_MEDIUM, 'DBClean', 10, $last_id);
338 Logger::log("No old conversations found");
341 Logger::log("Done deleting ".$count." conversations. Last created: ".$last_id);
343 Config::set('system', 'dbclean-last-id-10', $last_id);