Skip to content

Commit

Permalink
Merge branch 'master' into bharat_dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Almdal committed Apr 7, 2010
2 parents 5679e30 + 2657d08 commit 6652b69
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
86 changes: 86 additions & 0 deletions modules/gallery/helpers/gallery_task.php
Expand Up @@ -18,6 +18,9 @@
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class gallery_task_Core {
const MPTT_LEFT = 0;
const MPTT_RIGHT = 1;

static function available_tasks() {
$dirty_count = graphics::find_dirty_images_query()->count_records();
$tasks = array();
Expand All @@ -42,6 +45,14 @@ static function available_tasks() {
->name(t("Remove old files"))
->description(t("Remove expired files from the logs and tmp directory"))
->severity(log::SUCCESS);

$tasks[] = Task_Definition::factory()
->callback("gallery_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);

return $tasks;
}

Expand Down Expand Up @@ -298,4 +309,79 @@ static function file_cleanup($task) {
$task->log($errors);
}
}

static function fix_mptt($task) {
$start = microtime(true);

$total = $task->get("total");
if (empty($total)) {
$task->set("total", $total = db::build()->count_records("items"));
$task->set("stack", "1:" . self::MPTT_LEFT);
$task->set("ptr", 1);
$task->set("completed", 0);
}

$ptr = $task->get("ptr");
$stack = explode(" ", $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) = explode(":", array_pop($stack));
switch ($state) {
case self::MPTT_LEFT:
self::fix_mptt_set_left($id, $ptr++);
$item = ORM::factory("item", $id);
array_push($stack, $id . ":" . self::MPTT_RIGHT);
foreach (self::fix_mptt_children($id) as $child) {
array_push($stack, $child->id . ":" . self::MPTT_LEFT);
}
break;

case self::MPTT_RIGHT:
self::fix_mptt_set_right($id, $ptr++);
$completed++;
break;
}
}

$task->set("stack", implode(" ", $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 fix_mptt_children($parent_id) {
return db::build()
->select("id")
->from("items")
->where("parent_id", "=", $parent_id)
->order_by("left_ptr", "ASC")
->execute();
}

static function fix_mptt_set_left($id, $value) {
db::build()
->update("items")
->set("left_ptr", $value)
->where("id", "=", $id)
->execute();
}

static function fix_mptt_set_right($id, $value) {
db::build()
->update("items")
->set("right_ptr", $value)
->where("id", "=", $id)
->execute();
}
}
6 changes: 6 additions & 0 deletions modules/gallery/models/item.php
Expand Up @@ -332,6 +332,12 @@ public function save() {
$tmp = pathinfo($this->name, PATHINFO_FILENAME);
$tmp = preg_replace("/[^A-Za-z0-9-_]+/", "-", $tmp);
$this->slug = trim($tmp, "-");

// If the filename is all invalid characters, then the slug may be empty here. Pick a
// random value.
if (empty($this->slug)) {
$this->slug = (string)rand(1000, 9999);
}
}

// Get the width, height and mime type from our data file for photos and movies.
Expand Down
6 changes: 6 additions & 0 deletions modules/gallery/tests/Item_Model_Test.php
Expand Up @@ -324,6 +324,12 @@ public function slug_is_url_safe_test() {
$album->save();
}

public function name_with_only_invalid_chars_is_still_valid_test() {
$album = test::random_album_unsaved();
$album->name = "[]";
$album->save();
}

public function cant_change_item_type_test() {
$photo = test::random_photo();
try {
Expand Down
1 change: 1 addition & 0 deletions modules/server_add/controllers/server_add.php
Expand Up @@ -156,6 +156,7 @@ static function add($task) {
$entry_id = null;
}

$file = preg_quote($file);
foreach (glob("$file/*") as $child) {
if (is_dir($child)) {
$queue[] = array($child, $entry_id);
Expand Down

0 comments on commit 6652b69

Please sign in to comment.