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) {
15 @include(".htconfig.php");
16 require_once("include/dba.php");
17 $db = new dba($db_host, $db_user, $db_pass, $db_data);
18 unset($db_host, $db_user, $db_pass, $db_data);
21 load_config('config');
22 load_config('system');
25 $stage = intval($argv[1]);
30 if (get_config("system", "worker") AND ($stage == 0)) {
31 proc_run(PRIORITY_LOW, 'include/dbclean.php', 1);
32 proc_run(PRIORITY_LOW, 'include/dbclean.php', 2);
33 proc_run(PRIORITY_LOW, 'include/dbclean.php', 3);
34 proc_run(PRIORITY_LOW, 'include/dbclean.php', 4);
35 proc_run(PRIORITY_LOW, 'include/dbclean.php', 5);
36 proc_run(PRIORITY_LOW, 'include/dbclean.php', 6);
37 proc_run(PRIORITY_LOW, 'include/dbclean.php', 7);
39 remove_orphans($stage);
44 * @brief Remove orphaned database entries
46 function remove_orphans($stage = 0) {
51 if (($stage == 1) OR ($stage == 0)) {
52 logger("Deleting old global item entries from item table without user copy");
53 if ($db->q("SELECT `id` FROM `item` WHERE `uid` = 0
54 AND NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0)
55 AND `received` < UTC_TIMESTAMP() - INTERVAL 90 DAY LIMIT 10000", true)) {
56 $count = $db->num_rows();
57 logger("found global item orphans: ".$count);
58 while ($orphan = $db->qfetch()) {
59 q("DELETE FROM `item` WHERE `id` = %d", intval($orphan["id"]));
63 logger("Done deleting old global item entries from item table without user copy");
66 if (($stage == 2) OR ($stage == 0)) {
67 logger("Deleting items without parents");
68 if ($db->q("SELECT `id` FROM `item` WHERE NOT EXISTS (SELECT `id` FROM `item` AS `i` WHERE `item`.`parent` = `i`.`id`) LIMIT 10000", true)) {
69 $count = $db->num_rows();
70 logger("found item orphans without parents: ".$count);
71 while ($orphan = $db->qfetch()) {
72 q("DELETE FROM `item` WHERE `id` = %d", intval($orphan["id"]));
76 logger("Done deleting items without parents");
79 if (($stage == 3) OR ($stage == 0)) {
80 logger("Deleting orphaned data from thread table");
81 if ($db->q("SELECT `iid` FROM `thread` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `thread`.`iid`)", true)) {
82 $count = $db->num_rows();
83 logger("found thread orphans: ".$count);
84 while ($orphan = $db->qfetch()) {
85 q("DELETE FROM `thread` WHERE `iid` = %d", intval($orphan["iid"]));
89 logger("Done deleting orphaned data from thread table");
92 if (($stage == 4) OR ($stage == 0)) {
93 logger("Deleting orphaned data from notify table");
94 if ($db->q("SELECT `iid` FROM `notify` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `notify`.`iid`)", true)) {
95 $count = $db->num_rows();
96 logger("found notify orphans: ".$count);
97 while ($orphan = $db->qfetch()) {
98 q("DELETE FROM `notify` WHERE `iid` = %d", intval($orphan["iid"]));
102 logger("Done deleting orphaned data from notify table");
105 if (($stage == 5) OR ($stage == 0)) {
106 logger("Deleting orphaned data from notify-threads table");
107 if ($db->q("SELECT `id` FROM `notify-threads` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `notify-threads`.`master-parent-item`)", true)) {
108 $count = $db->num_rows();
109 logger("found notify-threads orphans: ".$count);
110 while ($orphan = $db->qfetch()) {
111 q("DELETE FROM `notify-threads` WHERE `id` = %d", intval($orphan["id"]));
115 logger("Done deleting orphaned data from notify-threads table");
119 if (($stage == 6) OR ($stage == 0)) {
120 logger("Deleting orphaned data from sign table");
121 if ($db->q("SELECT `iid` FROM `sign` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `sign`.`iid`)", true)) {
122 $count = $db->num_rows();
123 logger("found sign orphans: ".$count);
124 while ($orphan = $db->qfetch()) {
125 q("DELETE FROM `sign` WHERE `iid` = %d", intval($orphan["iid"]));
129 logger("Done deleting orphaned data from sign table");
133 if (($stage == 7) OR ($stage == 0)) {
134 logger("Deleting orphaned data from term table");
135 if ($db->q("SELECT `oid` FROM `term` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `term`.`oid`)", true)) {
136 $count = $db->num_rows();
137 logger("found term orphans: ".$count);
138 while ($orphan = $db->qfetch()) {
139 q("DELETE FROM `term` WHERE `oid` = %d", intval($orphan["oid"]));
143 logger("Done deleting orphaned data from term table");
146 // Call it again if not all entries were purged
147 if (($stage != 0) AND ($count > 0) AND get_config("system", "worker")) {
148 proc_run(PRIORITY_LOW, 'include/dbclean.php');
153 if (array_search(__file__,get_included_files())===0){
154 dbclean_run($_SERVER["argv"],$_SERVER["argc"]);