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;
9 function dbclean_run(&$argv, &$argc) {
10 if (!Config::get('system', 'dbclean', false)) {
15 $stage = intval($argv[1]);
20 // Get the expire days for step 8 and 9
21 $days = Config::get('system', 'dbclean-expire-days', 0);
24 for ($i = 1; $i <= 9; $i++) {
25 // Execute the background script for a step when it isn't finished.
26 // Execute step 8 and 9 only when $days is defined.
27 if (!Config::get('system', 'finished-dbclean-'.$i, false) AND (($i < 8) OR ($days > 0))) {
28 proc_run(PRIORITY_LOW, 'include/dbclean.php', $i);
32 remove_orphans($stage);
37 * @brief Remove orphaned database entries
39 function remove_orphans($stage = 0) {
44 // We split the deletion in many small tasks
47 // Get the expire days for step 8 and 9
48 $days = Config::get('system', 'dbclean-expire-days', 0);
51 $last_id = Config::get('system', 'dbclean-last-id-1', 0);
53 logger("Deleting old global item entries from item table without user copy. Last ID: ".$last_id);
54 $r = dba::p("SELECT `id` FROM `item` WHERE `uid` = 0 AND
55 NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0) AND
56 `received` < UTC_TIMESTAMP() - INTERVAL 90 DAY AND `id` >= ?
57 ORDER BY `id` LIMIT ".intval($limit), $last_id);
58 $count = dba::num_rows($r);
60 logger("found global item orphans: ".$count);
61 while ($orphan = dba::fetch($r)) {
62 $last_id = $orphan["id"];
63 dba::delete('item', array('id' => $orphan["id"]));
66 logger("No global item orphans found");
69 logger("Done deleting ".$count." old global item entries from item table without user copy. Last ID: ".$last_id);
71 Config::set('system', 'dbclean-last-id-1', $last_id);
72 } elseif ($stage == 2) {
73 $last_id = Config::get('system', 'dbclean-last-id-2', 0);
75 logger("Deleting items without parents. Last ID: ".$last_id);
76 $r = dba::p("SELECT `id` FROM `item`
77 WHERE NOT EXISTS (SELECT `id` FROM `item` AS `i` WHERE `item`.`parent` = `i`.`id`)
78 AND `id` >= ? ORDER BY `id` LIMIT ".intval($limit), $last_id);
79 $count = dba::num_rows($r);
81 logger("found item orphans without parents: ".$count);
82 while ($orphan = dba::fetch($r)) {
83 $last_id = $orphan["id"];
84 dba::delete('item', array('id' => $orphan["id"]));
87 logger("No item orphans without parents found");
90 logger("Done deleting ".$count." items without parents. Last ID: ".$last_id);
92 Config::set('system', 'dbclean-last-id-2', $last_id);
94 if ($count < $limit) {
95 Config::set('system', 'finished-dbclean-2', true);
97 } elseif ($stage == 3) {
98 $last_id = Config::get('system', 'dbclean-last-id-3', 0);
100 logger("Deleting orphaned data from thread table. Last ID: ".$last_id);
101 $r = dba::p("SELECT `iid` FROM `thread`
102 WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `thread`.`iid`) AND `iid` >= ?
103 ORDER BY `iid` LIMIT ".intval($limit), $last_id);
104 $count = dba::num_rows($r);
106 logger("found thread orphans: ".$count);
107 while ($orphan = dba::fetch($r)) {
108 $last_id = $orphan["iid"];
109 dba::delete('thread', array('iid' => $orphan["iid"]));
112 logger("No thread orphans found");
115 logger("Done deleting ".$count." orphaned data from thread table. Last ID: ".$last_id);
117 Config::set('system', 'dbclean-last-id-3', $last_id);
119 if ($count < $limit) {
120 Config::set('system', 'finished-dbclean-3', true);
122 } elseif ($stage == 4) {
123 $last_id = Config::get('system', 'dbclean-last-id-4', 0);
125 logger("Deleting orphaned data from notify table. Last ID: ".$last_id);
126 $r = dba::p("SELECT `iid`, `id` FROM `notify`
127 WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `notify`.`iid`) AND `id` >= ?
128 ORDER BY `id` LIMIT ".intval($limit), $last_id);
129 $count = dba::num_rows($r);
131 logger("found notify orphans: ".$count);
132 while ($orphan = dba::fetch($r)) {
133 $last_id = $orphan["id"];
134 dba::delete('notify', array('iid' => $orphan["iid"]));
137 logger("No notify orphans found");
140 logger("Done deleting ".$count." orphaned data from notify table. Last ID: ".$last_id);
142 Config::set('system', 'dbclean-last-id-4', $last_id);
144 if ($count < $limit) {
145 Config::set('system', 'finished-dbclean-4', true);
147 } elseif ($stage == 5) {
148 $last_id = Config::get('system', 'dbclean-last-id-5', 0);
150 logger("Deleting orphaned data from notify-threads table. Last ID: ".$last_id);
151 $r = dba::p("SELECT `id` FROM `notify-threads`
152 WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `notify-threads`.`master-parent-item`) AND `id` >= ?
153 ORDER BY `id` LIMIT ".intval($limit), $last_id);
154 $count = dba::num_rows($r);
156 logger("found notify-threads orphans: ".$count);
157 while ($orphan = dba::fetch($r)) {
158 $last_id = $orphan["id"];
159 dba::delete('notify-threads', array('id' => $orphan["id"]));
162 logger("No notify-threads orphans found");
165 logger("Done deleting ".$count." orphaned data from notify-threads table. Last ID: ".$last_id);
167 Config::set('system', 'dbclean-last-id-5', $last_id);
169 if ($count < $limit) {
170 Config::set('system', 'finished-dbclean-5', true);
172 } elseif ($stage == 6) {
173 $last_id = Config::get('system', 'dbclean-last-id-6', 0);
175 logger("Deleting orphaned data from sign table. Last ID: ".$last_id);
176 $r = dba::p("SELECT `iid`, `id` FROM `sign`
177 WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `sign`.`iid`) AND `id` >= ?
178 ORDER BY `id` LIMIT ".intval($limit), $last_id);
179 $count = dba::num_rows($r);
181 logger("found sign orphans: ".$count);
182 while ($orphan = dba::fetch($r)) {
183 $last_id = $orphan["id"];
184 dba::delete('sign', array('iid' => $orphan["iid"]));
187 logger("No sign orphans found");
190 logger("Done deleting ".$count." orphaned data from sign table. Last ID: ".$last_id);
192 Config::set('system', 'dbclean-last-id-6', $last_id);
194 if ($count < $limit) {
195 Config::set('system', 'finished-dbclean-6', true);
197 } elseif ($stage == 7) {
198 $last_id = Config::get('system', 'dbclean-last-id-7', 0);
200 logger("Deleting orphaned data from term table. Last ID: ".$last_id);
201 $r = dba::p("SELECT `oid`, `tid` FROM `term`
202 WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `term`.`oid`) AND `tid` >= ?
203 ORDER BY `tid` LIMIT ".intval($limit), $last_id);
204 $count = dba::num_rows($r);
206 logger("found term orphans: ".$count);
207 while ($orphan = dba::fetch($r)) {
208 $last_id = $orphan["tid"];
209 dba::delete('term', array('oid' => $orphan["oid"]));
212 logger("No term orphans found");
215 logger("Done deleting ".$count." orphaned data from term table. Last ID: ".$last_id);
217 Config::set('system', 'dbclean-last-id-7', $last_id);
219 if ($count < $limit) {
220 Config::set('system', 'finished-dbclean-7', true);
222 } elseif ($stage == 8) {
227 $last_id = Config::get('system', 'dbclean-last-id-8', 0);
229 logger("Deleting expired threads. Last ID: ".$last_id);
230 $r = dba::p("SELECT `thread`.`iid` FROM `thread`
231 INNER JOIN `contact` ON `thread`.`contact-id` = `contact`.`id` AND NOT `notify_new_posts`
232 WHERE `thread`.`received` < UTC_TIMESTAMP() - INTERVAL ? DAY
233 AND NOT `thread`.`mention` AND NOT `thread`.`starred`
234 AND NOT `thread`.`wall` AND NOT `thread`.`origin`
235 AND `thread`.`uid` != 0 AND `thread`.`iid` >= ?
236 AND NOT `thread`.`iid` IN (SELECT `parent` FROM `item`
237 WHERE (`item`.`starred` OR (`item`.`resource-id` != '')
238 OR (`item`.`file` != '') OR (`item`.`event-id` != '')
239 OR (`item`.`attach` != '') OR `item`.`wall` OR `item`.`origin`)
240 AND `item`.`parent` = `thread`.`iid`)
241 ORDER BY `thread`.`iid` LIMIT 1000", $days, $last_id);
242 $count = dba::num_rows($r);
244 logger("found expired threads: ".$count);
245 while ($thread = dba::fetch($r)) {
246 $last_id = $thread["iid"];
247 dba::delete('thread', array('iid' => $thread["iid"]));
250 logger("No expired threads found");
253 logger("Done deleting ".$count." expired threads. Last ID: ".$last_id);
255 Config::set('system', 'dbclean-last-id-8', $last_id);
256 } elseif ($stage == 9) {
261 $last_id = Config::get('system', 'dbclean-last-id-9', 0);
262 $till_id = Config::get('system', 'dbclean-last-id-8', 0);
264 logger("Deleting old global item entries from expired threads from ID ".$last_id." to ID ".$till_id);
265 $r = dba::p("SELECT `id` FROM `item` WHERE `uid` = 0 AND
266 NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0) AND
267 `received` < UTC_TIMESTAMP() - INTERVAL 90 DAY AND `id` >= ? AND `id` <= ?
268 ORDER BY `id` LIMIT ".intval($limit), $last_id, $till_id);
269 $count = dba::num_rows($r);
271 logger("found global item entries from expired threads: ".$count);
272 while ($orphan = dba::fetch($r)) {
273 $last_id = $orphan["id"];
274 dba::delete('item', array('id' => $orphan["id"]));
277 logger("No global item entries from expired threads");
280 logger("Done deleting ".$count." old global item entries from expired threads. Last ID: ".$last_id);
282 Config::set('system', 'dbclean-last-id-9', $last_id);
285 // Call it again if not all entries were purged
286 if (($stage != 0) AND ($count > 0)) {
287 proc_run(PRIORITY_MEDIUM, 'include/dbclean.php');