summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
Diffstat (limited to 'modules')
-rw-r--r--modules/gallery/helpers/gallery_task.php86
-rw-r--r--modules/gallery/models/item.php6
-rw-r--r--modules/gallery/tests/Item_Model_Test.php6
-rw-r--r--modules/server_add/controllers/server_add.php1
4 files changed, 99 insertions, 0 deletions
diff --git a/modules/gallery/helpers/gallery_task.php b/modules/gallery/helpers/gallery_task.php
index fa95c612..bc128b3e 100644
--- a/modules/gallery/helpers/gallery_task.php
+++ b/modules/gallery/helpers/gallery_task.php
@@ -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();
@@ -42,6 +45,14 @@ class gallery_task_Core {
->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;
}
@@ -298,4 +309,79 @@ class gallery_task_Core {
$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();
+ }
} \ No newline at end of file
diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php
index 4a87a2ab..7fc37325 100644
--- a/modules/gallery/models/item.php
+++ b/modules/gallery/models/item.php
@@ -332,6 +332,12 @@ class Item_Model extends ORM_MPTT {
$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.
diff --git a/modules/gallery/tests/Item_Model_Test.php b/modules/gallery/tests/Item_Model_Test.php
index d0676292..8ab85a30 100644
--- a/modules/gallery/tests/Item_Model_Test.php
+++ b/modules/gallery/tests/Item_Model_Test.php
@@ -324,6 +324,12 @@ class Item_Model_Test extends Gallery_Unit_Test_Case {
$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 {
diff --git a/modules/server_add/controllers/server_add.php b/modules/server_add/controllers/server_add.php
index 3e460f20..715274ab 100644
--- a/modules/server_add/controllers/server_add.php
+++ b/modules/server_add/controllers/server_add.php
@@ -156,6 +156,7 @@ class Server_Add_Controller extends Admin_Controller {
$entry_id = null;
}
+ $file = preg_quote($file);
foreach (glob("$file/*") as $child) {
if (is_dir($child)) {
$queue[] = array($child, $entry_id);