diff --git a/modules/rescue/helpers/rescue_installer.php b/modules/rescue/helpers/rescue_installer.php new file mode 100644 index 00000000..ecd2325e --- /dev/null +++ b/modules/rescue/helpers/rescue_installer.php @@ -0,0 +1,24 @@ +callback("rescue_task::fix_mptt") + ->name(t("Fix Album/Photo hierarchy")) + ->description(t("Fix problems where your album/photo breadcrumbs are out of " . + "sync with your actual hierarchy.")) + ->severity(log::SUCCESS)); + } + + static function fix_mptt($task) { + $start = microtime(true); + + $total = $task->get("total"); + if (empty($total)) { + $task->set("total", $total = Database::instance()->count_records("items")); + $task->set("stack", array(array(1, self::LEFT))); + $task->set("ptr", 1); + $task->set("completed", 0); + } + + $ptr = $task->get("ptr"); + $stack = $task->get("stack"); + $completed = $task->get("completed"); + + // Implement a depth-first tree walk using a stack. Not the most efficient, but it's simple. + while ($stack && microtime(true) - $start < 1.5) { + list($id, $state) = array_pop($stack); + switch ($state) { + case self::LEFT: + self::set_left($id, $ptr++); + $item = ORM::factory("item", $id); + array_push($stack, array($id, self::RIGHT)); + foreach (self::children($id) as $child) { + array_push($stack, array($child->id, self::LEFT)); + } + break; + + case self::RIGHT: + self::set_right($id, $ptr++); + $completed++; + break; + } + } + + $task->set("stack", $stack); + $task->set("ptr", $ptr); + $task->set("completed", $completed); + + if ($total == $completed) { + $task->done = true; + $task->state = "success"; + $task->percent_complete = 100; + } else { + $task->percent_complete = round(100 * $completed / $total); + } + $task->status = t2("One row updated", "%count / %total rows updated", $completed, + array("total" => $total)); + } + + static function children($parent_id) { + return Database::instance() + ->select("id") + ->from("items") + ->where("parent_id", $parent_id) + ->orderby("left", "ASC") + ->get(); + } + + static function set_left($id, $value) { + Database::instance()->update("items", array("left" => $value), array("id" => $id)); + } + + static function set_right($id, $value) { + Database::instance()->update("items", array("right" => $value), array("id" => $id)); + } +} diff --git a/modules/rescue/module.info b/modules/rescue/module.info new file mode 100644 index 00000000..305afe33 --- /dev/null +++ b/modules/rescue/module.info @@ -0,0 +1,3 @@ +name = "Rescue" +description = "Fix problems in your Gallery 3 install" +version = 1