+ if (($stage == 2) OR ($stage == 0)) {
+ logger("Deleting items without parents");
+ if ($db->q("SELECT `id` FROM `item` WHERE NOT EXISTS (SELECT `id` FROM `item` AS `i` WHERE `item`.`parent` = `i`.`id`) LIMIT 10000", true)) {
+ $count = $db->num_rows();
+ logger("found item orphans without parents: ".$count);
+ while ($orphan = $db->qfetch()) {
+ q("DELETE FROM `item` WHERE `id` = %d", intval($orphan["id"]));
+ }
+ }
+ $db->qclose();
+ logger("Done deleting items without parents");
+ }
+
+ if (($stage == 3) OR ($stage == 0)) {
+ logger("Deleting orphaned data from thread table");
+ if ($db->q("SELECT `iid` FROM `thread` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `thread`.`iid`)", true)) {
+ $count = $db->num_rows();
+ logger("found thread orphans: ".$count);
+ while ($orphan = $db->qfetch()) {
+ q("DELETE FROM `thread` WHERE `iid` = %d", intval($orphan["iid"]));
+ }
+ }
+ $db->qclose();
+ logger("Done deleting orphaned data from thread table");
+ }
+
+ if (($stage == 4) OR ($stage == 0)) {
+ logger("Deleting orphaned data from notify table");
+ if ($db->q("SELECT `iid` FROM `notify` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `notify`.`iid`)", true)) {
+ $count = $db->num_rows();
+ logger("found notify orphans: ".$count);
+ while ($orphan = $db->qfetch()) {
+ q("DELETE FROM `notify` WHERE `iid` = %d", intval($orphan["iid"]));
+ }
+ }
+ $db->qclose();
+ logger("Done deleting orphaned data from notify table");
+ }