3 * @file include/dbclean.php
4 * @brief The script is called from time to time to clean the database entries and remove orphaned data.
7 use Friendica\Core\Config;
8 use Friendica\Core\Worker;
10 function dbclean_run(&$argv, &$argc) {
11 if (!Config::get('system', 'dbclean', false)) {
16 $stage = intval($argv[1]);
21 // Get the expire days for step 8 and 9
22 $days = Config::get('system', 'dbclean-expire-days', 0);
25 for ($i = 1; $i <= 9; $i++) {
26 // Execute the background script for a step when it isn't finished.
27 // Execute step 8 and 9 only when $days is defined.
28 if (!Config::get('system', 'finished-dbclean-'.$i, false) && (($i < 8) || ($days > 0))) {
29 Worker::add(PRIORITY_LOW, 'dbclean', $i);
33 remove_orphans($stage);
38 * @brief Remove orphaned database entries
39 * @param integer $stage What should be deleted?
43 * 1: Old global item entries from item table without user copy.
44 * 2: Items without parents.
45 * 3: Orphaned data from thread table.
46 * 4: Orphaned data from notify table.
47 * 5: Orphaned data from notify-threads table.
48 * 6: Orphaned data from sign table.
49 * 7: Orphaned data from term table.
51 * 9: Old global item entries from expired threads
53 function remove_orphans($stage = 0) {
58 // We split the deletion in many small tasks
61 // Get the expire days for step 8 and 9
62 $days = Config::get('system', 'dbclean-expire-days', 0);
65 $last_id = Config::get('system', 'dbclean-last-id-1', 0);
67 logger("Deleting old global item entries from item table without user copy. Last ID: ".$last_id);
68 $r = dba::p("SELECT `id` FROM `item` WHERE `uid` = 0 AND
69 NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0) AND
70 `received` < UTC_TIMESTAMP() - INTERVAL 90 DAY AND `id` >= ?
71 ORDER BY `id` LIMIT ".intval($limit), $last_id);
72 $count = dba::num_rows($r);
74 logger("found global item orphans: ".$count);
75 while ($orphan = dba::fetch($r)) {
76 $last_id = $orphan["id"];
77 dba::delete('item', array('id' => $orphan["id"]));
80 logger("No global item orphans found");
83 logger("Done deleting ".$count." old global item entries from item table without user copy. Last ID: ".$last_id);
85 Config::set('system', 'dbclean-last-id-1', $last_id);
86 } elseif ($stage == 2) {
87 $last_id = Config::get('system', 'dbclean-last-id-2', 0);
89 logger("Deleting items without parents. Last ID: ".$last_id);
90 $r = dba::p("SELECT `id` FROM `item`
91 WHERE NOT EXISTS (SELECT `id` FROM `item` AS `i` WHERE `item`.`parent` = `i`.`id`)
92 AND `id` >= ? ORDER BY `id` LIMIT ".intval($limit), $last_id);
93 $count = dba::num_rows($r);
95 logger("found item orphans without parents: ".$count);
96 while ($orphan = dba::fetch($r)) {
97 $last_id = $orphan["id"];
98 dba::delete('item', array('id' => $orphan["id"]));
101 logger("No item orphans without parents found");
104 logger("Done deleting ".$count." items without parents. Last ID: ".$last_id);
106 Config::set('system', 'dbclean-last-id-2', $last_id);
108 if ($count < $limit) {
109 Config::set('system', 'finished-dbclean-2', true);
111 } elseif ($stage == 3) {
112 $last_id = Config::get('system', 'dbclean-last-id-3', 0);
114 logger("Deleting orphaned data from thread table. Last ID: ".$last_id);
115 $r = dba::p("SELECT `iid` FROM `thread`
116 WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `thread`.`iid`) AND `iid` >= ?
117 ORDER BY `iid` LIMIT ".intval($limit), $last_id);
118 $count = dba::num_rows($r);
120 logger("found thread orphans: ".$count);
121 while ($orphan = dba::fetch($r)) {
122 $last_id = $orphan["iid"];
123 dba::delete('thread', array('iid' => $orphan["iid"]));
126 logger("No thread orphans found");
129 logger("Done deleting ".$count." orphaned data from thread table. Last ID: ".$last_id);
131 Config::set('system', 'dbclean-last-id-3', $last_id);
133 if ($count < $limit) {
134 Config::set('system', 'finished-dbclean-3', true);
136 } elseif ($stage == 4) {
137 $last_id = Config::get('system', 'dbclean-last-id-4', 0);
139 logger("Deleting orphaned data from notify table. Last ID: ".$last_id);
140 $r = dba::p("SELECT `iid`, `id` FROM `notify`
141 WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `notify`.`iid`) AND `id` >= ?
142 ORDER BY `id` LIMIT ".intval($limit), $last_id);
143 $count = dba::num_rows($r);
145 logger("found notify orphans: ".$count);
146 while ($orphan = dba::fetch($r)) {
147 $last_id = $orphan["id"];
148 dba::delete('notify', array('iid' => $orphan["iid"]));
151 logger("No notify orphans found");
154 logger("Done deleting ".$count." orphaned data from notify table. Last ID: ".$last_id);
156 Config::set('system', 'dbclean-last-id-4', $last_id);
158 if ($count < $limit) {
159 Config::set('system', 'finished-dbclean-4', true);
161 } elseif ($stage == 5) {
162 $last_id = Config::get('system', 'dbclean-last-id-5', 0);
164 logger("Deleting orphaned data from notify-threads table. Last ID: ".$last_id);
165 $r = dba::p("SELECT `id` FROM `notify-threads`
166 WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `notify-threads`.`master-parent-item`) AND `id` >= ?
167 ORDER BY `id` LIMIT ".intval($limit), $last_id);
168 $count = dba::num_rows($r);
170 logger("found notify-threads orphans: ".$count);
171 while ($orphan = dba::fetch($r)) {
172 $last_id = $orphan["id"];
173 dba::delete('notify-threads', array('id' => $orphan["id"]));
176 logger("No notify-threads orphans found");
179 logger("Done deleting ".$count." orphaned data from notify-threads table. Last ID: ".$last_id);
181 Config::set('system', 'dbclean-last-id-5', $last_id);
183 if ($count < $limit) {
184 Config::set('system', 'finished-dbclean-5', true);
186 } elseif ($stage == 6) {
187 $last_id = Config::get('system', 'dbclean-last-id-6', 0);
189 logger("Deleting orphaned data from sign table. Last ID: ".$last_id);
190 $r = dba::p("SELECT `iid`, `id` FROM `sign`
191 WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `sign`.`iid`) AND `id` >= ?
192 ORDER BY `id` LIMIT ".intval($limit), $last_id);
193 $count = dba::num_rows($r);
195 logger("found sign orphans: ".$count);
196 while ($orphan = dba::fetch($r)) {
197 $last_id = $orphan["id"];
198 dba::delete('sign', array('iid' => $orphan["iid"]));
201 logger("No sign orphans found");
204 logger("Done deleting ".$count." orphaned data from sign table. Last ID: ".$last_id);
206 Config::set('system', 'dbclean-last-id-6', $last_id);
208 if ($count < $limit) {
209 Config::set('system', 'finished-dbclean-6', true);
211 } elseif ($stage == 7) {
212 $last_id = Config::get('system', 'dbclean-last-id-7', 0);
214 logger("Deleting orphaned data from term table. Last ID: ".$last_id);
215 $r = dba::p("SELECT `oid`, `tid` FROM `term`
216 WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `term`.`oid`) AND `tid` >= ?
217 ORDER BY `tid` LIMIT ".intval($limit), $last_id);
218 $count = dba::num_rows($r);
220 logger("found term orphans: ".$count);
221 while ($orphan = dba::fetch($r)) {
222 $last_id = $orphan["tid"];
223 dba::delete('term', array('oid' => $orphan["oid"]));
226 logger("No term orphans found");
229 logger("Done deleting ".$count." orphaned data from term table. Last ID: ".$last_id);
231 Config::set('system', 'dbclean-last-id-7', $last_id);
233 if ($count < $limit) {
234 Config::set('system', 'finished-dbclean-7', true);
236 } elseif ($stage == 8) {
241 $last_id = Config::get('system', 'dbclean-last-id-8', 0);
243 logger("Deleting expired threads. Last ID: ".$last_id);
244 $r = dba::p("SELECT `thread`.`iid` FROM `thread`
245 INNER JOIN `contact` ON `thread`.`contact-id` = `contact`.`id` AND NOT `notify_new_posts`
246 WHERE `thread`.`received` < UTC_TIMESTAMP() - INTERVAL ? DAY
247 AND NOT `thread`.`mention` AND NOT `thread`.`starred`
248 AND NOT `thread`.`wall` AND NOT `thread`.`origin`
249 AND `thread`.`uid` != 0 AND `thread`.`iid` >= ?
250 AND NOT `thread`.`iid` IN (SELECT `parent` FROM `item`
251 WHERE (`item`.`starred` OR (`item`.`resource-id` != '')
252 OR (`item`.`file` != '') OR (`item`.`event-id` != '')
253 OR (`item`.`attach` != '') OR `item`.`wall` OR `item`.`origin`)
254 AND `item`.`parent` = `thread`.`iid`)
255 ORDER BY `thread`.`iid` LIMIT 1000", $days, $last_id);
256 $count = dba::num_rows($r);
258 logger("found expired threads: ".$count);
259 while ($thread = dba::fetch($r)) {
260 $last_id = $thread["iid"];
261 dba::delete('thread', array('iid' => $thread["iid"]));
264 logger("No expired threads found");
267 logger("Done deleting ".$count." expired threads. Last ID: ".$last_id);
269 Config::set('system', 'dbclean-last-id-8', $last_id);
270 } elseif ($stage == 9) {
275 $last_id = Config::get('system', 'dbclean-last-id-9', 0);
276 $till_id = Config::get('system', 'dbclean-last-id-8', 0);
278 logger("Deleting old global item entries from expired threads from ID ".$last_id." to ID ".$till_id);
279 $r = dba::p("SELECT `id` FROM `item` WHERE `uid` = 0 AND
280 NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0) AND
281 `received` < UTC_TIMESTAMP() - INTERVAL 90 DAY AND `id` >= ? AND `id` <= ?
282 ORDER BY `id` LIMIT ".intval($limit), $last_id, $till_id);
283 $count = dba::num_rows($r);
285 logger("found global item entries from expired threads: ".$count);
286 while ($orphan = dba::fetch($r)) {
287 $last_id = $orphan["id"];
288 dba::delete('item', array('id' => $orphan["id"]));
291 logger("No global item entries from expired threads");
294 logger("Done deleting ".$count." old global item entries from expired threads. Last ID: ".$last_id);
296 Config::set('system', 'dbclean-last-id-9', $last_id);
299 // Call it again if not all entries were purged
300 if (($stage != 0) && ($count > 0)) {
301 Worker::add(PRIORITY_MEDIUM, 'dbclean');