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]);
21 for ($i = 1; $i <= 7; $i++) {
22 if (!Config::get('system', 'finished-dbclean-'.$i, false)) {
23 proc_run(PRIORITY_LOW, 'include/dbclean.php', $i);
27 remove_orphans($stage);
32 * @brief Remove orphaned database entries
34 function remove_orphans($stage = 0) {
39 // We split the deletion in many small tasks
43 $last_id = Config::get('system', 'dbclean-last-id-1', 0);
45 logger("Deleting old global item entries from item table without user copy. Last ID: ".$last_id);
46 $r = dba::p("SELECT `id` FROM `item` WHERE `uid` = 0 AND
47 NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0) AND
48 `received` < UTC_TIMESTAMP() - INTERVAL 90 DAY AND `id` >= ?
49 ORDER BY `id` LIMIT ".intval($limit), $last_id);
50 $count = dba::num_rows($r);
52 logger("found global item orphans: ".$count);
53 while ($orphan = dba::fetch($r)) {
54 $last_id = $orphan["id"];
55 dba::delete('item', array('id' => $orphan["id"]));
58 logger("No global item orphans found");
61 logger("Done deleting ".$count." old global item entries from item table without user copy. Last ID: ".$last_id);
63 Config::set('system', 'dbclean-last-id-1', $last_id);
65 // We will eventually set this value when we found a good way to delete these items in another way.
66 // if ($count < $limit) {
67 // Config::set('system', 'finished-dbclean-1', true);
69 } elseif ($stage == 2) {
70 $last_id = Config::get('system', 'dbclean-last-id-2', 0);
72 logger("Deleting items without parents. Last ID: ".$last_id);
73 $r = dba::p("SELECT `id` FROM `item`
74 WHERE NOT EXISTS (SELECT `id` FROM `item` AS `i` WHERE `item`.`parent` = `i`.`id`)
75 AND `id` >= ? ORDER BY `id` LIMIT ".intval($limit), $last_id);
76 $count = dba::num_rows($r);
78 logger("found item orphans without parents: ".$count);
79 while ($orphan = dba::fetch($r)) {
80 $last_id = $orphan["id"];
81 dba::delete('item', array('id' => $orphan["id"]));
84 logger("No item orphans without parents found");
87 logger("Done deleting ".$count." items without parents. Last ID: ".$last_id);
89 Config::set('system', 'dbclean-last-id-2', $last_id);
91 if ($count < $limit) {
92 Config::set('system', 'finished-dbclean-2', true);
94 } elseif ($stage == 3) {
95 $last_id = Config::get('system', 'dbclean-last-id-3', 0);
97 logger("Deleting orphaned data from thread table. Last ID: ".$last_id);
98 $r = dba::p("SELECT `iid` FROM `thread`
99 WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `thread`.`iid`) AND `iid` >= ?
100 ORDER BY `iid` LIMIT ".intval($limit), $last_id);
101 $count = dba::num_rows($r);
103 logger("found thread orphans: ".$count);
104 while ($orphan = dba::fetch($r)) {
105 $last_id = $orphan["iid"];
106 dba::delete('thread', array('iid' => $orphan["iid"]));
109 logger("No thread orphans found");
112 logger("Done deleting ".$count." orphaned data from thread table. Last ID: ".$last_id);
114 Config::set('system', 'dbclean-last-id-3', $last_id);
116 if ($count < $limit) {
117 Config::set('system', 'finished-dbclean-3', true);
119 } elseif ($stage == 4) {
120 $last_id = Config::get('system', 'dbclean-last-id-4', 0);
122 logger("Deleting orphaned data from notify table. Last ID: ".$last_id);
123 $r = dba::p("SELECT `iid`, `id` FROM `notify`
124 WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `notify`.`iid`) AND `id` >= ?
125 ORDER BY `id` LIMIT ".intval($limit), $last_id);
126 $count = dba::num_rows($r);
128 logger("found notify orphans: ".$count);
129 while ($orphan = dba::fetch($r)) {
130 $last_id = $orphan["id"];
131 dba::delete('notify', array('iid' => $orphan["iid"]));
134 logger("No notify orphans found");
137 logger("Done deleting ".$count." orphaned data from notify table. Last ID: ".$last_id);
139 Config::set('system', 'dbclean-last-id-4', $last_id);
141 if ($count < $limit) {
142 Config::set('system', 'finished-dbclean-4', true);
144 } elseif ($stage == 5) {
145 $last_id = Config::get('system', 'dbclean-last-id-5', 0);
147 logger("Deleting orphaned data from notify-threads table. Last ID: ".$last_id);
148 $r = dba::p("SELECT `id` FROM `notify-threads`
149 WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `notify-threads`.`master-parent-item`) AND `id` >= ?
150 ORDER BY `id` LIMIT ".intval($limit), $last_id);
151 $count = dba::num_rows($r);
153 logger("found notify-threads orphans: ".$count);
154 while ($orphan = dba::fetch($r)) {
155 $last_id = $orphan["id"];
156 dba::delete('notify-threads', array('id' => $orphan["id"]));
159 logger("No notify-threads orphans found");
162 logger("Done deleting ".$count." orphaned data from notify-threads table. Last ID: ".$last_id);
164 Config::set('system', 'dbclean-last-id-5', $last_id);
166 if ($count < $limit) {
167 Config::set('system', 'finished-dbclean-5', true);
169 } elseif ($stage == 6) {
170 $last_id = Config::get('system', 'dbclean-last-id-6', 0);
172 logger("Deleting orphaned data from sign table. Last ID: ".$last_id);
173 $r = dba::p("SELECT `iid`, `id` FROM `sign`
174 WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `sign`.`iid`) AND `id` >= ?
175 ORDER BY `id` LIMIT ".intval($limit), $last_id);
176 $count = dba::num_rows($r);
178 logger("found sign orphans: ".$count);
179 while ($orphan = dba::fetch($r)) {
180 $last_id = $orphan["id"];
181 dba::delete('sign', array('iid' => $orphan["iid"]));
184 logger("No sign orphans found");
187 logger("Done deleting ".$count." orphaned data from sign table. Last ID: ".$last_id);
189 Config::set('system', 'dbclean-last-id-6', $last_id);
191 if ($count < $limit) {
192 Config::set('system', 'finished-dbclean-6', true);
194 } elseif ($stage == 7) {
195 $last_id = Config::get('system', 'dbclean-last-id-7', 0);
197 logger("Deleting orphaned data from term table. Last ID: ".$last_id);
198 $r = dba::p("SELECT `oid`, `tid` FROM `term`
199 WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `term`.`oid`) AND `tid` >= ?
200 ORDER BY `tid` LIMIT ".intval($limit), $last_id);
201 $count = dba::num_rows($r);
203 logger("found term orphans: ".$count);
204 while ($orphan = dba::fetch($r)) {
205 $last_id = $orphan["tid"];
206 dba::delete('term', array('oid' => $orphan["oid"]));
209 logger("No term orphans found");
212 logger("Done deleting ".$count." orphaned data from term table. Last ID: ".$last_id);
214 Config::set('system', 'dbclean-last-id-7', $last_id);
216 if ($count < $limit) {
217 Config::set('system', 'finished-dbclean-7', true);
221 // Call it again if not all entries were purged
222 if (($stage != 0) AND ($count > 0)) {
223 proc_run(PRIORITY_MEDIUM, 'include/dbclean.php');