+ logger("Deleting old global item entries from item table without user copy");
+ if ($db->q("SELECT `id` FROM `item` WHERE `uid` = 0
+ AND NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0)
+ AND `received` < UTC_TIMESTAMP() - INTERVAL 90 DAY LIMIT 10000", true)) {
+ $count = $db->num_rows();
+ logger("found global item orphans: ".$count);
+ while ($orphan = $db->qfetch()) {
+ q("DELETE FROM `item` WHERE `id` = %d", intval($orphan["id"]));
+ }
+ }
+ $db->qclose();
+ logger("Done deleting old global item entries from item table without user copy");
+ }
+
+ 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)) {