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)) {
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 logger("Deleting old global item entries from item table without user copy");
44 $r = dba::p("SELECT `id` FROM `item` WHERE `uid` = 0
45 AND NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0)
46 AND `received` < UTC_TIMESTAMP() - INTERVAL 90 DAY LIMIT ".intval($limit));
47 $count = dba::num_rows($r);
49 logger("found global item orphans: ".$count);
50 while ($orphan = dba::fetch($r)) {
51 dba::delete('item', array('id' => $orphan["id"]));
54 logger("No global item orphans found");
58 logger("Done deleting ".$count." old global item entries from item table without user copy");
60 // We will eventually set this value when we found a good way to delete these items in another way.
61 // if ($count < $limit) {
62 // Config::set('system', 'finished-dbclean-1', true);
64 } elseif ($stage == 2) {
65 logger("Deleting items without parents");
66 $r = dba::p("SELECT `id` FROM `item` WHERE NOT EXISTS (SELECT `id` FROM `item` AS `i` WHERE `item`.`parent` = `i`.`id`) LIMIT ".intval($limit));
67 $count = dba::num_rows($r);
69 logger("found item orphans without parents: ".$count);
70 while ($orphan = dba::fetch($r)) {
71 dba::delete('item', array('id' => $orphan["id"]));
74 logger("No item orphans without parents found");
77 logger("Done deleting ".$count." items without parents");
79 if ($count < $limit) {
80 Config::set('system', 'finished-dbclean-2', true);
82 } elseif ($stage == 3) {
83 logger("Deleting orphaned data from thread table");
84 $r = dba::p("SELECT `iid` FROM `thread` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `thread`.`iid`) LIMIT ".intval($limit));
85 $count = dba::num_rows($r);
87 logger("found thread orphans: ".$count);
88 while ($orphan = dba::fetch($r)) {
89 dba::delete('thread', array('iid' => $orphan["iid"]));
92 logger("No thread orphans found");
95 logger("Done deleting ".$count." orphaned data from thread table");
97 if ($count < $limit) {
98 Config::set('system', 'finished-dbclean-3', true);
100 } elseif ($stage == 4) {
101 logger("Deleting orphaned data from notify table");
102 $r = dba::p("SELECT `iid` FROM `notify` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `notify`.`iid`) LIMIT ".intval($limit));
103 $count = dba::num_rows($r);
105 logger("found notify orphans: ".$count);
106 while ($orphan = dba::fetch($r)) {
107 dba::delete('notify', array('iid' => $orphan["iid"]));
110 logger("No notify orphans found");
113 logger("Done deleting ".$count." orphaned data from notify table");
115 if ($count < $limit) {
116 Config::set('system', 'finished-dbclean-4', true);
118 } elseif ($stage == 5) {
119 logger("Deleting orphaned data from notify-threads table");
120 $r = dba::p("SELECT `id` FROM `notify-threads` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `notify-threads`.`master-parent-item`) LIMIT ".intval($limit));
121 $count = dba::num_rows($r);
123 logger("found notify-threads orphans: ".$count);
124 while ($orphan = dba::fetch($r)) {
125 dba::delete('notify-threads', array('id' => $orphan["id"]));
128 logger("No notify-threads orphans found");
131 logger("Done deleting ".$count." orphaned data from notify-threads table");
133 if ($count < $limit) {
134 Config::set('system', 'finished-dbclean-5', true);
136 } elseif ($stage == 6) {
137 logger("Deleting orphaned data from sign table");
138 $r = dba::p("SELECT `iid` FROM `sign` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `sign`.`iid`) LIMIT ".intval($limit));
139 $count = dba::num_rows($r);
141 logger("found sign orphans: ".$count);
142 while ($orphan = dba::fetch($r)) {
143 dba::delete('sign', array('iid' => $orphan["iid"]));
146 logger("No sign orphans found");
149 logger("Done deleting ".$count." orphaned data from sign table");
151 if ($count < $limit) {
152 Config::set('system', 'finished-dbclean-6', true);
154 } elseif ($stage == 7) {
155 logger("Deleting orphaned data from term table");
156 $r = dba::p("SELECT `oid` FROM `term` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `term`.`oid`) LIMIT ".intval($limit));
157 $count = dba::num_rows($r);
159 logger("found term orphans: ".$count);
160 while ($orphan = dba::fetch($r)) {
161 dba::delete('term', array('oid' => $orphan["oid"]));
164 logger("No term orphans found");
167 logger("Done deleting ".$count." orphaned data from term table");
169 if ($count < $limit) {
170 Config::set('system', 'finished-dbclean-7', true);
174 // Call it again if not all entries were purged
175 if (($stage != 0) AND ($count > 0)) {
176 proc_run(PRIORITY_MEDIUM, 'include/dbclean.php');