#2069 - Change "Fix your Gallery" task go faster and be more comprehensive.

- optimize MPTT pointer rebuilding for leaf nodes (i.e. non-albums).
- reverse order_by to try and preserve existing tree ordering.
- reset item level while we're here.
- use "$stack[] = 123" instead of array_push($stack, 123) since it's faster.

--HG--
extra : source : 297e4c0eccc5a7940224ff8e908b366e83017354
This commit is contained in:
shadlaws
2013-05-14 13:35:09 +02:00
parent 4d8fbd1b9d
commit 2ebe38b148
+34 -16
View File
@@ -398,42 +398,60 @@ class gallery_task_Core {
switch ($state) {
case self::FIX_STATE_START_MPTT:
$task->set("ptr", $ptr = 1);
$task->set("stack", item::root()->id . ":L");
$task->set("stack", item::root()->id . ":album:1:L");
$state = self::FIX_STATE_RUN_MPTT;
break;
case self::FIX_STATE_RUN_MPTT:
$ptr = $task->get("ptr");
$stack = explode(" ", $task->get("stack"));
list ($id, $ptr_mode) = explode(":", array_pop($stack));
list ($id, $type, $level, $ptr_mode) = explode(":", array_pop($stack));
if ($ptr_mode == "L") {
$stack[] = "$id:R";
db::build()
->update("items")
->set("left_ptr", $ptr++)
->where("id", "=", $id)
->execute();
if ($type == "album") {
// Albums could be parent nodes.
$stack[] = "$id:$type:$level:R";
db::build()
->update("items")
->set("left_ptr", $ptr++)
->where("id", "=", $id)
->execute();
foreach (db::build()
->select(array("id"))
->from("items")
->where("parent_id", "=", $id)
->order_by("left_ptr", "ASC")
->execute() as $child) {
array_push($stack, "{$child->id}:L");
$level++;
foreach (db::build()
->select(array("id", "type"))
->from("items")
->where("parent_id", "=", $id)
->order_by("left_ptr", "DESC") // DESC since array_pop effectively reverses them
->execute() as $child) {
$stack[] = "{$child->id}:{$child->type}:$level:L";
}
$completed++;
} else {
// Non-albums must be leaf nodes.
db::build()
->update("items")
->set("left_ptr", $ptr++)
->set("right_ptr", $ptr++)
->set("level", $level)
->set("relative_path_cache", null)
->set("relative_url_cache", null)
->where("id", "=", $id)
->execute();
$completed += 2; // we updated two pointers
}
} else if ($ptr_mode == "R") {
db::build()
->update("items")
->set("right_ptr", $ptr++)
->set("level", $level)
->set("relative_path_cache", null)
->set("relative_url_cache", null)
->where("id", "=", $id)
->execute();
$completed++;
}
$task->set("ptr", $ptr);
$task->set("stack", implode(" ", $stack));
$completed++;
if (empty($stack)) {
$state = self::FIX_STATE_START_DUPE_SLUGS;