3 * @file include/dbclean.php
4 * @brief The script is called from time to time to clean the database entries and remove orphaned data.
6 require_once("boot.php");
8 function dbclean_run(&$argv, &$argc) {
16 @include(".htconfig.php");
17 require_once("include/dba.php");
18 $db = new dba($db_host, $db_user, $db_pass, $db_data);
19 unset($db_host, $db_user, $db_pass, $db_data);
22 load_config('config');
23 load_config('system');
25 if (!get_config("system", "dbclean")) {
30 $stage = intval($argv[1]);
35 if (get_config("system", "worker") AND ($stage == 0)) {
36 proc_run(PRIORITY_LOW, 'include/dbclean.php', 1);
37 proc_run(PRIORITY_LOW, 'include/dbclean.php', 2);
38 proc_run(PRIORITY_LOW, 'include/dbclean.php', 3);
39 proc_run(PRIORITY_LOW, 'include/dbclean.php', 4);
40 proc_run(PRIORITY_LOW, 'include/dbclean.php', 5);
41 proc_run(PRIORITY_LOW, 'include/dbclean.php', 6);
42 proc_run(PRIORITY_LOW, 'include/dbclean.php', 7);
44 remove_orphans($stage);
49 * @brief Remove orphaned database entries
51 function remove_orphans($stage = 0) {
56 if (($stage == 1) OR ($stage == 0)) {
57 logger("Deleting old global item entries from item table without user copy");
58 if ($db->q("SELECT `id` FROM `item` WHERE `uid` = 0
59 AND NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0)
60 AND `received` < UTC_TIMESTAMP() - INTERVAL 90 DAY LIMIT 10000", true)) {
61 $count = $db->num_rows();
62 logger("found global item orphans: ".$count);
63 while ($orphan = $db->qfetch()) {
64 q("DELETE FROM `item` WHERE `id` = %d", intval($orphan["id"]));
68 logger("Done deleting old global item entries from item table without user copy");
71 if (($stage == 2) OR ($stage == 0)) {
72 logger("Deleting items without parents");
73 if ($db->q("SELECT `id` FROM `item` WHERE NOT EXISTS (SELECT `id` FROM `item` AS `i` WHERE `item`.`parent` = `i`.`id`) LIMIT 10000", true)) {
74 $count = $db->num_rows();
75 logger("found item orphans without parents: ".$count);
76 while ($orphan = $db->qfetch()) {
77 q("DELETE FROM `item` WHERE `id` = %d", intval($orphan["id"]));
81 logger("Done deleting items without parents");
84 if (($stage == 3) OR ($stage == 0)) {
85 logger("Deleting orphaned data from thread table");
86 if ($db->q("SELECT `iid` FROM `thread` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `thread`.`iid`)", true)) {
87 $count = $db->num_rows();
88 logger("found thread orphans: ".$count);
89 while ($orphan = $db->qfetch()) {
90 q("DELETE FROM `thread` WHERE `iid` = %d", intval($orphan["iid"]));
94 logger("Done deleting orphaned data from thread table");
97 if (($stage == 4) OR ($stage == 0)) {
98 logger("Deleting orphaned data from notify table");
99 if ($db->q("SELECT `iid` FROM `notify` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `notify`.`iid`)", true)) {
100 $count = $db->num_rows();
101 logger("found notify orphans: ".$count);
102 while ($orphan = $db->qfetch()) {
103 q("DELETE FROM `notify` WHERE `iid` = %d", intval($orphan["iid"]));
107 logger("Done deleting orphaned data from notify table");
110 if (($stage == 5) OR ($stage == 0)) {
111 logger("Deleting orphaned data from notify-threads table");
112 if ($db->q("SELECT `id` FROM `notify-threads` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `notify-threads`.`master-parent-item`)", true)) {
113 $count = $db->num_rows();
114 logger("found notify-threads orphans: ".$count);
115 while ($orphan = $db->qfetch()) {
116 q("DELETE FROM `notify-threads` WHERE `id` = %d", intval($orphan["id"]));
120 logger("Done deleting orphaned data from notify-threads table");
124 if (($stage == 6) OR ($stage == 0)) {
125 logger("Deleting orphaned data from sign table");
126 if ($db->q("SELECT `iid` FROM `sign` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `sign`.`iid`)", true)) {
127 $count = $db->num_rows();
128 logger("found sign orphans: ".$count);
129 while ($orphan = $db->qfetch()) {
130 q("DELETE FROM `sign` WHERE `iid` = %d", intval($orphan["iid"]));
134 logger("Done deleting orphaned data from sign table");
138 if (($stage == 7) OR ($stage == 0)) {
139 logger("Deleting orphaned data from term table");
140 if ($db->q("SELECT `oid` FROM `term` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `term`.`oid`)", true)) {
141 $count = $db->num_rows();
142 logger("found term orphans: ".$count);
143 while ($orphan = $db->qfetch()) {
144 q("DELETE FROM `term` WHERE `oid` = %d", intval($orphan["oid"]));
148 logger("Done deleting orphaned data from term table");
151 // Call it again if not all entries were purged
152 if (($stage != 0) AND ($count > 0) AND get_config("system", "worker")) {
153 proc_run(PRIORITY_LOW, 'include/dbclean.php');
158 if (array_search(__file__,get_included_files())===0){
159 dbclean_run($_SERVER["argv"],$_SERVER["argc"]);