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
46 * @param integer $stage What should be deleted?
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.
58 * 9: Old global item entries from expired threads.
59 * 10: Old conversations.
60 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
62 private static function removeOrphans($stage) {
63 // We split the deletion in many small tasks
64 $limit = Config::get('system', 'dbclean-expire-limit', 1000);
66 // Get the expire days for step 8 and 9
67 $days = Config::get('system', 'dbclean-expire-days', 0);
68 $days_unclaimed = Config::get('system', 'dbclean-expire-unclaimed', 90);
70 if ($days_unclaimed == 0) {
71 $days_unclaimed = $days;
75 if ($days_unclaimed <= 0) {
79 $last_id = Config::get('system', 'dbclean-last-id-1', 0);
81 Logger::log("Deleting old global item entries from item table without user copy. Last ID: ".$last_id);
82 $r = DBA::p("SELECT `id` FROM `item` WHERE `uid` = 0 AND
83 NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0) AND
84 `received` < UTC_TIMESTAMP() - INTERVAL ? DAY AND `id` >= ?
85 ORDER BY `id` LIMIT ?", $days_unclaimed, $last_id, $limit);
86 $count = DBA::numRows($r);
88 Logger::log("found global item orphans: ".$count);
89 while ($orphan = DBA::fetch($r)) {
90 $last_id = $orphan["id"];
91 DBA::delete('item', ['id' => $orphan["id"]]);
93 Worker::add(PRIORITY_MEDIUM, 'DBClean', 1, $last_id);
95 Logger::log("No global item orphans found");
98 Logger::log("Done deleting ".$count." old global item entries from item table without user copy. Last ID: ".$last_id);
100 Config::set('system', 'dbclean-last-id-1', $last_id);
101 } elseif ($stage == 2) {
102 $last_id = Config::get('system', 'dbclean-last-id-2', 0);
104 Logger::log("Deleting items without parents. Last ID: ".$last_id);
105 $r = DBA::p("SELECT `id` FROM `item`
106 WHERE NOT EXISTS (SELECT `id` FROM `item` AS `i` WHERE `item`.`parent` = `i`.`id`)
107 AND `id` >= ? ORDER BY `id` LIMIT ?", $last_id, $limit);
108 $count = DBA::numRows($r);
110 Logger::log("found item orphans without parents: ".$count);
111 while ($orphan = DBA::fetch($r)) {
112 $last_id = $orphan["id"];
113 DBA::delete('item', ['id' => $orphan["id"]]);
115 Worker::add(PRIORITY_MEDIUM, 'DBClean', 2, $last_id);
117 Logger::log("No item orphans without parents found");
120 Logger::log("Done deleting ".$count." items without parents. Last ID: ".$last_id);
122 Config::set('system', 'dbclean-last-id-2', $last_id);
124 if ($count < $limit) {
125 Config::set('system', 'finished-dbclean-2', true);
127 } elseif ($stage == 3) {
128 $last_id = Config::get('system', 'dbclean-last-id-3', 0);
130 Logger::log("Deleting orphaned data from thread table. Last ID: ".$last_id);
131 $r = DBA::p("SELECT `iid` FROM `thread`
132 WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `thread`.`iid`) AND `iid` >= ?
133 ORDER BY `iid` LIMIT ?", $last_id, $limit);
134 $count = DBA::numRows($r);
136 Logger::log("found thread orphans: ".$count);
137 while ($orphan = DBA::fetch($r)) {
138 $last_id = $orphan["iid"];
139 DBA::delete('thread', ['iid' => $orphan["iid"]]);
141 Worker::add(PRIORITY_MEDIUM, 'DBClean', 3, $last_id);
143 Logger::log("No thread orphans found");
146 Logger::log("Done deleting ".$count." orphaned data from thread table. Last ID: ".$last_id);
148 Config::set('system', 'dbclean-last-id-3', $last_id);
150 if ($count < $limit) {
151 Config::set('system', 'finished-dbclean-3', true);
153 } elseif ($stage == 4) {
154 $last_id = Config::get('system', 'dbclean-last-id-4', 0);
156 Logger::log("Deleting orphaned data from notify table. Last ID: ".$last_id);
157 $r = DBA::p("SELECT `iid`, `id` FROM `notify`
158 WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `notify`.`iid`) AND `id` >= ?
159 ORDER BY `id` LIMIT ?", $last_id, $limit);
160 $count = DBA::numRows($r);
162 Logger::log("found notify orphans: ".$count);
163 while ($orphan = DBA::fetch($r)) {
164 $last_id = $orphan["id"];
165 DBA::delete('notify', ['iid' => $orphan["iid"]]);
167 Worker::add(PRIORITY_MEDIUM, 'DBClean', 4, $last_id);
169 Logger::log("No notify orphans found");
172 Logger::log("Done deleting ".$count." orphaned data from notify table. Last ID: ".$last_id);
174 Config::set('system', 'dbclean-last-id-4', $last_id);
176 if ($count < $limit) {
177 Config::set('system', 'finished-dbclean-4', true);
179 } elseif ($stage == 5) {
180 $last_id = Config::get('system', 'dbclean-last-id-5', 0);
182 Logger::log("Deleting orphaned data from notify-threads table. Last ID: ".$last_id);
183 $r = DBA::p("SELECT `id` FROM `notify-threads`
184 WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `notify-threads`.`master-parent-item`) AND `id` >= ?
185 ORDER BY `id` LIMIT ?", $last_id, $limit);
186 $count = DBA::numRows($r);
188 Logger::log("found notify-threads orphans: ".$count);
189 while ($orphan = DBA::fetch($r)) {
190 $last_id = $orphan["id"];
191 DBA::delete('notify-threads', ['id' => $orphan["id"]]);
193 Worker::add(PRIORITY_MEDIUM, 'DBClean', 5, $last_id);
195 Logger::log("No notify-threads orphans found");
198 Logger::log("Done deleting ".$count." orphaned data from notify-threads table. Last ID: ".$last_id);
200 Config::set('system', 'dbclean-last-id-5', $last_id);
202 if ($count < $limit) {
203 Config::set('system', 'finished-dbclean-5', true);
205 } elseif ($stage == 6) {
206 $last_id = Config::get('system', 'dbclean-last-id-6', 0);
208 Logger::log("Deleting orphaned data from sign table. Last ID: ".$last_id);
209 $r = DBA::p("SELECT `iid`, `id` FROM `sign`
210 WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `sign`.`iid`) AND `id` >= ?
211 ORDER BY `id` LIMIT ?", $last_id, $limit);
212 $count = DBA::numRows($r);
214 Logger::log("found sign orphans: ".$count);
215 while ($orphan = DBA::fetch($r)) {
216 $last_id = $orphan["id"];
217 DBA::delete('sign', ['iid' => $orphan["iid"]]);
219 Worker::add(PRIORITY_MEDIUM, 'DBClean', 6, $last_id);
221 Logger::log("No sign orphans found");
224 Logger::log("Done deleting ".$count." orphaned data from sign table. Last ID: ".$last_id);
226 Config::set('system', 'dbclean-last-id-6', $last_id);
228 if ($count < $limit) {
229 Config::set('system', 'finished-dbclean-6', true);
231 } elseif ($stage == 7) {
232 $last_id = Config::get('system', 'dbclean-last-id-7', 0);
234 Logger::log("Deleting orphaned data from term table. Last ID: ".$last_id);
235 $r = DBA::p("SELECT `oid`, `tid` FROM `term`
236 WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `term`.`oid`) AND `tid` >= ?
237 ORDER BY `tid` LIMIT ?", $last_id, $limit);
238 $count = DBA::numRows($r);
240 Logger::log("found term orphans: ".$count);
241 while ($orphan = DBA::fetch($r)) {
242 $last_id = $orphan["tid"];
243 DBA::delete('term', ['oid' => $orphan["oid"]]);
245 Worker::add(PRIORITY_MEDIUM, 'DBClean', 7, $last_id);
247 Logger::log("No term orphans found");
250 Logger::log("Done deleting ".$count." orphaned data from term table. Last ID: ".$last_id);
252 Config::set('system', 'dbclean-last-id-7', $last_id);
254 if ($count < $limit) {
255 Config::set('system', 'finished-dbclean-7', true);
257 } elseif ($stage == 8) {
262 $last_id = Config::get('system', 'dbclean-last-id-8', 0);
264 Logger::log("Deleting expired threads. Last ID: ".$last_id);
265 $r = DBA::p("SELECT `thread`.`iid` FROM `thread`
266 INNER JOIN `contact` ON `thread`.`contact-id` = `contact`.`id` AND NOT `notify_new_posts`
267 WHERE `thread`.`received` < UTC_TIMESTAMP() - INTERVAL ? DAY
268 AND NOT `thread`.`mention` AND NOT `thread`.`starred`
269 AND NOT `thread`.`wall` AND NOT `thread`.`origin`
270 AND `thread`.`uid` != 0 AND `thread`.`iid` >= ?
271 AND NOT `thread`.`iid` IN (SELECT `parent` FROM `item`
272 WHERE (`item`.`starred` OR (`item`.`resource-id` != '')
273 OR (`item`.`file` != '') OR (`item`.`event-id` != '')
274 OR (`item`.`attach` != '') OR `item`.`wall` OR `item`.`origin`)
275 AND `item`.`parent` = `thread`.`iid`)
276 ORDER BY `thread`.`iid` LIMIT ?", $days, $last_id, $limit);
277 $count = DBA::numRows($r);
279 Logger::log("found expired threads: ".$count);
280 while ($thread = DBA::fetch($r)) {
281 $last_id = $thread["iid"];
282 DBA::delete('thread', ['iid' => $thread["iid"]]);
284 Worker::add(PRIORITY_MEDIUM, 'DBClean', 8, $last_id);
286 Logger::log("No expired threads found");
289 Logger::log("Done deleting ".$count." expired threads. Last ID: ".$last_id);
291 Config::set('system', 'dbclean-last-id-8', $last_id);
292 } elseif ($stage == 9) {
297 $last_id = Config::get('system', 'dbclean-last-id-9', 0);
298 $till_id = Config::get('system', 'dbclean-last-id-8', 0);
300 Logger::log("Deleting old global item entries from expired threads from ID ".$last_id." to ID ".$till_id);
301 $r = DBA::p("SELECT `id` FROM `item` WHERE `uid` = 0 AND
302 NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0) AND
303 `received` < UTC_TIMESTAMP() - INTERVAL 90 DAY AND `id` >= ? AND `id` <= ?
304 ORDER BY `id` LIMIT ?", $last_id, $till_id, $limit);
305 $count = DBA::numRows($r);
307 Logger::log("found global item entries from expired threads: ".$count);
308 while ($orphan = DBA::fetch($r)) {
309 $last_id = $orphan["id"];
310 DBA::delete('item', ['id' => $orphan["id"]]);
312 Worker::add(PRIORITY_MEDIUM, 'DBClean', 9, $last_id);
314 Logger::log("No global item entries from expired threads");
317 Logger::log("Done deleting ".$count." old global item entries from expired threads. Last ID: ".$last_id);
319 Config::set('system', 'dbclean-last-id-9', $last_id);
320 } elseif ($stage == 10) {
321 $last_id = Config::get('system', 'dbclean-last-id-10', 0);
322 $days = intval(Config::get('system', 'dbclean_expire_conversation', 90));
324 Logger::log("Deleting old conversations. Last created: ".$last_id);
325 $r = DBA::p("SELECT `received`, `item-uri` FROM `conversation`
326 WHERE `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
327 ORDER BY `received` LIMIT ?", $days, $limit);
328 $count = DBA::numRows($r);
330 Logger::log("found old conversations: ".$count);
331 while ($orphan = DBA::fetch($r)) {
332 $last_id = $orphan["received"];
333 DBA::delete('conversation', ['item-uri' => $orphan["item-uri"]]);
335 Worker::add(PRIORITY_MEDIUM, 'DBClean', 10, $last_id);
337 Logger::log("No old conversations found");
340 Logger::log("Done deleting ".$count." conversations. Last created: ".$last_id);
342 Config::set('system', 'dbclean-last-id-10', $last_id);