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 proc_run(PRIORITY_LOW, 'include/dbclean.php', 1);
22 proc_run(PRIORITY_LOW, 'include/dbclean.php', 2);
23 proc_run(PRIORITY_LOW, 'include/dbclean.php', 3);
24 proc_run(PRIORITY_LOW, 'include/dbclean.php', 4);
25 proc_run(PRIORITY_LOW, 'include/dbclean.php', 5);
26 proc_run(PRIORITY_LOW, 'include/dbclean.php', 6);
27 proc_run(PRIORITY_LOW, 'include/dbclean.php', 7);
29 remove_orphans($stage);
34 * @brief Remove orphaned database entries
36 function remove_orphans($stage = 0) {
41 // We split the deletion in many small tasks
44 if (($stage == 1) OR ($stage == 0)) {
45 logger("Deleting old global item entries from item table without user copy");
46 if ($db->q("SELECT `id` FROM `item` WHERE `uid` = 0
47 AND NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0)
48 AND `received` < UTC_TIMESTAMP() - INTERVAL 90 DAY LIMIT ".intval($limit), true)) {
49 $count = $db->num_rows();
50 logger("found global item orphans: ".$count);
51 while ($orphan = $db->qfetch()) {
52 q("DELETE FROM `item` WHERE `id` = %d", intval($orphan["id"]));
56 logger("Done deleting old global item entries from item table without user copy");
59 if (($stage == 2) OR ($stage == 0)) {
60 logger("Deleting items without parents");
61 if ($db->q("SELECT `id` FROM `item` WHERE NOT EXISTS (SELECT `id` FROM `item` AS `i` WHERE `item`.`parent` = `i`.`id`) LIMIT ".intval($limit), true)) {
62 $count = $db->num_rows();
63 logger("found item orphans without parents: ".$count);
64 while ($orphan = $db->qfetch()) {
65 q("DELETE FROM `item` WHERE `id` = %d", intval($orphan["id"]));
69 logger("Done deleting items without parents");
72 if (($stage == 3) OR ($stage == 0)) {
73 logger("Deleting orphaned data from thread table");
74 if ($db->q("SELECT `iid` FROM `thread` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `thread`.`iid`) LIMIT ".intval($limit), true)) {
75 $count = $db->num_rows();
76 logger("found thread orphans: ".$count);
77 while ($orphan = $db->qfetch()) {
78 q("DELETE FROM `thread` WHERE `iid` = %d", intval($orphan["iid"]));
82 logger("Done deleting orphaned data from thread table");
85 if (($stage == 4) OR ($stage == 0)) {
86 logger("Deleting orphaned data from notify table");
87 if ($db->q("SELECT `iid` FROM `notify` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `notify`.`iid`) LIMIT ".intval($limit), true)) {
88 $count = $db->num_rows();
89 logger("found notify orphans: ".$count);
90 while ($orphan = $db->qfetch()) {
91 q("DELETE FROM `notify` WHERE `iid` = %d", intval($orphan["iid"]));
95 logger("Done deleting orphaned data from notify table");
98 if (($stage == 5) OR ($stage == 0)) {
99 logger("Deleting orphaned data from notify-threads table");
100 if ($db->q("SELECT `id` FROM `notify-threads` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `notify-threads`.`master-parent-item`) LIMIT ".intval($limit), true)) {
101 $count = $db->num_rows();
102 logger("found notify-threads orphans: ".$count);
103 while ($orphan = $db->qfetch()) {
104 q("DELETE FROM `notify-threads` WHERE `id` = %d", intval($orphan["id"]));
108 logger("Done deleting orphaned data from notify-threads table");
112 if (($stage == 6) OR ($stage == 0)) {
113 logger("Deleting orphaned data from sign table");
114 if ($db->q("SELECT `iid` FROM `sign` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `sign`.`iid`) LIMIT ".intval($limit), true)) {
115 $count = $db->num_rows();
116 logger("found sign orphans: ".$count);
117 while ($orphan = $db->qfetch()) {
118 q("DELETE FROM `sign` WHERE `iid` = %d", intval($orphan["iid"]));
122 logger("Done deleting orphaned data from sign table");
126 if (($stage == 7) OR ($stage == 0)) {
127 logger("Deleting orphaned data from term table");
128 if ($db->q("SELECT `oid` FROM `term` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `term`.`oid`) LIMIT ".intval($limit), true)) {
129 $count = $db->num_rows();
130 logger("found term orphans: ".$count);
131 while ($orphan = $db->qfetch()) {
132 q("DELETE FROM `term` WHERE `oid` = %d", intval($orphan["oid"]));
136 logger("Done deleting orphaned data from term table");
139 // Call it again if not all entries were purged
140 if (($stage != 0) AND ($count > 0)) {
141 proc_run(PRIORITY_MEDIUM, 'include/dbclean.php');