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");
56 // We will eventually set this value when we found a good way to delete these items in another way.
57 // Config::set('system', 'finished-dbclean-1', true);
60 logger("Done deleting ".$count." old global item entries from item table without user copy");
61 } elseif ($stage == 2) {
62 logger("Deleting items without parents");
63 $r = dba::p("SELECT `id` FROM `item` WHERE NOT EXISTS (SELECT `id` FROM `item` AS `i` WHERE `item`.`parent` = `i`.`id`) LIMIT ".intval($limit));
64 $count = dba::num_rows($r);
66 logger("found item orphans without parents: ".$count);
67 while ($orphan = dba::fetch($r)) {
68 dba::delete('item', array('id' => $orphan["id"]));
71 logger("No item orphans without parents found");
72 Config::set('system', 'finished-dbclean-2', true);
75 logger("Done deleting ".$count." items without parents");
76 } elseif ($stage == 3) {
77 logger("Deleting orphaned data from thread table");
78 $r = dba::p("SELECT `iid` FROM `thread` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `thread`.`iid`) LIMIT ".intval($limit));
79 $count = dba::num_rows($r);
81 logger("found thread orphans: ".$count);
82 while ($orphan = dba::fetch($r)) {
83 dba::delete('thread', array('iid' => $orphan["iid"]));
86 logger("No thread orphans found");
87 Config::set('system', 'finished-dbclean-3', true);
91 logger("Done deleting ".$count." orphaned data from thread table");
92 } elseif ($stage == 4) {
93 logger("Deleting orphaned data from notify table");
94 $r = dba::p("SELECT `iid` FROM `notify` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `notify`.`iid`) LIMIT ".intval($limit));
95 $count = dba::num_rows($r);
97 logger("found notify orphans: ".$count);
98 while ($orphan = dba::fetch($r)) {
99 dba::delete('notify', array('iid' => $orphan["iid"]));
102 logger("No notify orphans found");
103 Config::set('system', 'finished-dbclean-4', true);
106 logger("Done deleting ".$count." orphaned data from notify table");
107 } elseif ($stage == 5) {
108 logger("Deleting orphaned data from notify-threads table");
109 $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));
110 $count = dba::num_rows($r);
112 logger("found notify-threads orphans: ".$count);
113 while ($orphan = dba::fetch($r)) {
114 dba::delete('notify-threads', array('id' => $orphan["id"]));
117 logger("No notify-threads orphans found");
118 Config::set('system', 'finished-dbclean-5', true);
121 logger("Done deleting ".$count." orphaned data from notify-threads table");
122 } elseif ($stage == 6) {
123 logger("Deleting orphaned data from sign table");
124 $r = dba::p("SELECT `iid` FROM `sign` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `sign`.`iid`) LIMIT ".intval($limit));
125 $count = dba::num_rows($r);
127 logger("found sign orphans: ".$count);
128 while ($orphan = dba::fetch($r)) {
129 dba::delete('sign', array('iid' => $orphan["iid"]));
132 logger("No sign orphans found");
133 Config::set('system', 'finished-dbclean-6', true);
136 logger("Done deleting ".$count." orphaned data from sign table");
137 } elseif ($stage == 7) {
138 logger("Deleting orphaned data from term table");
139 $r = dba::p("SELECT `oid` FROM `term` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `term`.`oid`) LIMIT ".intval($limit));
140 $count = dba::num_rows($r);
142 logger("found term orphans: ".$count);
143 while ($orphan = dba::fetch($r)) {
144 dba::delete('term', array('oid' => $orphan["oid"]));
147 logger("No term orphans found");
148 Config::set('system', 'finished-dbclean-7', true);
151 logger("Done deleting ".$count." orphaned data from term table");
154 // Call it again if not all entries were purged
155 if (($stage != 0) AND ($count > 0)) {
156 proc_run(PRIORITY_MEDIUM, 'include/dbclean.php');