diff options
-rw-r--r-- | modules/gallery/helpers/item.php | 16 | ||||
-rw-r--r-- | modules/gallery/tests/Item_Helper_Test.php | 20 |
2 files changed, 32 insertions, 4 deletions
diff --git a/modules/gallery/helpers/item.php b/modules/gallery/helpers/item.php index 08a04ad0..bac189f4 100644 --- a/modules/gallery/helpers/item.php +++ b/modules/gallery/helpers/item.php @@ -211,6 +211,7 @@ class item_Core { /** * Find an item by its path. If there's no match, return an empty Item_Model. + * NOTE: the caller is responsible for performing security checks on the resulting item. * @param string $path * @return object Item_Model */ @@ -222,6 +223,21 @@ class item_Core { return item::root(); } + // Check to see if there's an item in the database with a matching relative_path_cache value. + // Since that field is urlencoded, we must urlencoded the components of the path. + foreach (explode("/", $path) as $part) { + $encoded_array[] = rawurlencode($part); + } + $encoded_path = join("/", $encoded_array); + $item = ORM::factory("item") + ->where("relative_path_cache", "=", $encoded_path) + ->find(); + if ($item->loaded()) { + return $item; + } + + // Since the relative_path_cache field is a cache, it can be unavailable. If we don't find + // anything, fall back to checking the path the hard way. $paths = explode("/", $path); foreach (ORM::factory("item") ->where("name", "=", end($paths)) diff --git a/modules/gallery/tests/Item_Helper_Test.php b/modules/gallery/tests/Item_Helper_Test.php index 0aa7504e..13ecec2b 100644 --- a/modules/gallery/tests/Item_Helper_Test.php +++ b/modules/gallery/tests/Item_Helper_Test.php @@ -129,14 +129,21 @@ class Item_Helper_Test extends Gallery_Unit_Test_Case { public function find_by_path_test() { $level1 = test::random_album(); $level2 = test::random_album($level1); - $level3 = test::random_photo($level2); + $level3 = test::random_photo_unsaved($level2); $level3->name = "same.jpg"; - $level3->save(); + $level3->save()->reload(); $level2b = test::random_album($level1); - $level3b = test::random_photo($level2b); + $level3b = test::random_photo_unsaved($level2b); $level3b->name = "has spaces+plusses.jpg"; - $level3b->save(); + $level3b->save()->reload(); + + // Make sure that some of the calls below use the fallback code. + db::build() + ->update("items") + ->set(array("relative_url_cache" => null, "relative_path_cache" => null)) + ->where("id", "IN", array($level3->id, $level3b->id)) + ->execute(); // Item in album $this->assert_same( @@ -168,5 +175,10 @@ class Item_Helper_Test extends Gallery_Unit_Test_Case { // Verify that we don't get false positives $this->assert_false( item::find_by_path("foo/bar/baz")->loaded()); + + // Verify that the fallback code works + $this->assert_same( + $level3b->id, + item::find_by_path("{$level1->name}/{$level2b->name}/{$level3b->name}")->id); } } |