From 2e420522ece22942a9b3b6ee413ca0e1dfa76148 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Wed, 25 Nov 2009 13:22:24 -0800 Subject: Preliminary work to cut over to Kohana 2.4 - Kohana::log() -> Kohana_Log::add() - Kohana::config_XXX -> Kohana_Config::instance()->XXX - Implement View::set_global in MY_View - Updated Cache_Database_Driver to latest APIs - ORM::$loaded -> ORM::loaded() - Updated item::viewable() to use K2.4 parenthesization --- modules/tag/helpers/tag.php | 2 +- modules/tag/helpers/tag_event.php | 2 +- modules/tag/helpers/tag_rss.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'modules/tag/helpers') diff --git a/modules/tag/helpers/tag.php b/modules/tag/helpers/tag.php index feaf40c5..6ce18625 100644 --- a/modules/tag/helpers/tag.php +++ b/modules/tag/helpers/tag.php @@ -34,7 +34,7 @@ class tag_Core { } $tag = ORM::factory("tag")->where("name", $tag_name)->find(); - if (!$tag->loaded) { + if (!$tag->loaded()) { $tag->name = $tag_name; $tag->count = 0; $tag->save(); diff --git a/modules/tag/helpers/tag_event.php b/modules/tag/helpers/tag_event.php index a857a99d..6ee8e708 100644 --- a/modules/tag/helpers/tag_event.php +++ b/modules/tag/helpers/tag_event.php @@ -51,7 +51,7 @@ class tag_event_Core { try { tag::add($photo, $tag); } catch (Exception $e) { - Kohana::log("error", "Error adding tag: $tag\n" . + Kohana_Log::add("error", "Error adding tag: $tag\n" . $e->getMessage() . "\n" . $e->getTraceAsString()); } } diff --git a/modules/tag/helpers/tag_rss.php b/modules/tag/helpers/tag_rss.php index de5d6c72..03017dc3 100644 --- a/modules/tag/helpers/tag_rss.php +++ b/modules/tag/helpers/tag_rss.php @@ -31,7 +31,7 @@ class tag_rss_Core { static function feed($feed_id, $offset, $limit, $id) { if ($feed_id == "tag") { $tag = ORM::factory("tag", $id); - if (!$tag->loaded) { + if (!$tag->loaded()) { Kohana::show_404(); } $feed->children = $tag->items($limit, $offset, "photo"); -- cgit v1.2.3 From 4a417708f00b1ebd5865c8075bb36b7fc0f14756 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Wed, 25 Nov 2009 13:49:40 -0800 Subject: Kohana::show_404() -> throw new Kohana_404_Exception() --- modules/digibug/controllers/digibug.php | 6 +++--- modules/gallery/controllers/combined.php | 4 ++-- modules/gallery/controllers/items.php | 2 +- modules/gallery_unit_test/controllers/gallery_unit_test.php | 2 +- modules/rss/controllers/rss.php | 2 +- modules/tag/helpers/tag_rss.php | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) (limited to 'modules/tag/helpers') diff --git a/modules/digibug/controllers/digibug.php b/modules/digibug/controllers/digibug.php index cef42b2d..25f1ca3e 100644 --- a/modules/digibug/controllers/digibug.php +++ b/modules/digibug/controllers/digibug.php @@ -61,7 +61,7 @@ class Digibug_Controller extends Controller { if ($type == "full") { $remote_addr = ip2long($this->input->server("REMOTE_ADDR")); if ($remote_addr === false) { - Kohana::show_404(); + throw new Kohana_404_Exception(); } $config = Kohana::config("digibug"); @@ -76,13 +76,13 @@ class Digibug_Controller extends Controller { } } if (!$authorized) { - Kohana::show_404(); + throw new Kohana_404_Exception(); } } $proxy = ORM::factory("digibug_proxy", array("uuid" => $id)); if (!$proxy->loaded() || !$proxy->item->loaded()) { - Kohana::show_404(); + throw new Kohana_404_Exception(); } $file = $type == "full" ? $proxy->item->file_path() : $proxy->item->thumb_path(); diff --git a/modules/gallery/controllers/combined.php b/modules/gallery/controllers/combined.php index c1f42bfe..8132fc41 100644 --- a/modules/gallery/controllers/combined.php +++ b/modules/gallery/controllers/combined.php @@ -56,7 +56,7 @@ class Combined_Controller extends Controller { } if (empty($key)) { - Kohana::show_404(); + throw new Kohana_404_Exception(); } $cache = Cache::instance(); @@ -71,7 +71,7 @@ class Combined_Controller extends Controller { $content = $cache->get($key); } if (empty($content)) { - Kohana::show_404(); + throw new Kohana_404_Exception(); } // $type is either 'javascript' or 'css' diff --git a/modules/gallery/controllers/items.php b/modules/gallery/controllers/items.php index bf1f80d4..86782469 100644 --- a/modules/gallery/controllers/items.php +++ b/modules/gallery/controllers/items.php @@ -21,7 +21,7 @@ class Items_Controller extends Controller { public function __call($function, $args) { $item = ORM::factory("item", (int)$function); if (!$item->loaded()) { - return Kohana::show_404(); + throw new Kohana_404_Exception(); } // Redirect to the more specific resource type, since it will render // differently. We could also just delegate here, but it feels more appropriate diff --git a/modules/gallery_unit_test/controllers/gallery_unit_test.php b/modules/gallery_unit_test/controllers/gallery_unit_test.php index 58e0d9c5..391ad029 100644 --- a/modules/gallery_unit_test/controllers/gallery_unit_test.php +++ b/modules/gallery_unit_test/controllers/gallery_unit_test.php @@ -20,7 +20,7 @@ class Gallery_Unit_Test_Controller extends Controller { function Index() { if (!TEST_MODE) { - print Kohana::show_404(); + print throw new Kohana_404_Exception(); } // Jump through some hoops to satisfy the way that we check for the site_domain in diff --git a/modules/rss/controllers/rss.php b/modules/rss/controllers/rss.php index ed2acef8..a963a1dc 100644 --- a/modules/rss/controllers/rss.php +++ b/modules/rss/controllers/rss.php @@ -39,7 +39,7 @@ class Rss_Controller extends Controller { } } if (empty($feed)) { - Kohana::show_404(); + throw new Kohana_404_Exception(); } if ($feed->max_pages && $page > $feed->max_pages) { diff --git a/modules/tag/helpers/tag_rss.php b/modules/tag/helpers/tag_rss.php index 03017dc3..f09a4530 100644 --- a/modules/tag/helpers/tag_rss.php +++ b/modules/tag/helpers/tag_rss.php @@ -32,7 +32,7 @@ class tag_rss_Core { if ($feed_id == "tag") { $tag = ORM::factory("tag", $id); if (!$tag->loaded()) { - Kohana::show_404(); + throw new Kohana_404_Exception(); } $feed->children = $tag->items($limit, $offset, "photo"); $feed->max_pages = ceil($tag->count / $limit); -- cgit v1.2.3 From 0ef6994f23300f3c6bfb83193b4101202dbc16f8 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Wed, 25 Nov 2009 19:25:34 -0800 Subject: ORM::orderby -> ORM::order_by --- modules/image_block/helpers/image_block_block.php | 12 ++++++------ modules/tag/helpers/tag.php | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'modules/tag/helpers') diff --git a/modules/image_block/helpers/image_block_block.php b/modules/image_block/helpers/image_block_block.php index 79bd92ba..f591e8d1 100644 --- a/modules/image_block/helpers/image_block_block.php +++ b/modules/image_block/helpers/image_block_block.php @@ -35,18 +35,18 @@ class image_block_block_Core { $items = ORM::factory("item") ->viewable() - ->where("type !=", "album") - ->where("rand_key < ", $random) - ->orderby(array("rand_key" => "DESC")) + ->where("type", "!=", "album") + ->where("rand_key", "<", $random) + ->order_by(array("rand_key" => "DESC")) ->find_all(1); if ($items->count() == 0) { // Try once more. If this fails, just ditch the block altogether $items = ORM::factory("item") ->viewable() - ->where("type !=", "album") - ->where("rand_key >= ", $random) - ->orderby(array("rand_key" => "DESC")) + ->where("type", "!=", "album") + ->where("rand_key", ">=", $random) + ->order_by(array("rand_key" => "DESC")) ->find_all(1); } diff --git a/modules/tag/helpers/tag.php b/modules/tag/helpers/tag.php index 8558ac30..8694bcec 100644 --- a/modules/tag/helpers/tag.php +++ b/modules/tag/helpers/tag.php @@ -57,7 +57,7 @@ class tag_Core { */ static function popular_tags($count) { return ORM::factory("tag") - ->orderby("count", "DESC") + ->order_by("count", "DESC") ->limit($count) ->find_all(); } -- cgit v1.2.3 From 1fd0e14359a7c7164573e4aa897c07680339e713 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Thu, 26 Nov 2009 12:09:04 -0800 Subject: Convert all DB where() calls to take 3 arguments. Convert all open_paren() calls to and_open() or or_open() as appropriate. --- modules/comment/controllers/admin_comments.php | 4 +- modules/comment/helpers/comment_event.php | 6 +-- modules/comment/helpers/comment_rss.php | 4 +- modules/comment/helpers/comment_theme.php | 4 +- modules/comment/tests/Comment_Model_Test.php | 4 +- modules/exif/helpers/exif.php | 14 +++---- modules/exif/helpers/exif_task.php | 10 ++--- modules/exif/helpers/exif_theme.php | 2 +- modules/g2_import/helpers/g2_import.php | 2 +- modules/g2_import/helpers/g2_import_task.php | 2 +- modules/gallery/controllers/admin_maintenance.php | 6 +-- modules/gallery/controllers/albums.php | 12 +++--- modules/gallery/controllers/file_proxy.php | 4 +- modules/gallery/controllers/l10n_client.php | 6 +-- modules/gallery/controllers/move.php | 4 +- modules/gallery/controllers/movies.php | 10 ++--- modules/gallery/controllers/photos.php | 12 +++--- modules/gallery/helpers/MY_url.php | 6 +-- modules/gallery/helpers/access.php | 46 ++++++++++---------- modules/gallery/helpers/album.php | 10 ++--- modules/gallery/helpers/gallery_block.php | 8 ++-- modules/gallery/helpers/gallery_event.php | 12 +++--- modules/gallery/helpers/gallery_rss.php | 4 +- modules/gallery/helpers/graphics.php | 10 ++--- modules/gallery/helpers/l10n_client.php | 6 ++- modules/gallery/helpers/l10n_scanner.php | 2 +- modules/gallery/helpers/module.php | 11 ++--- modules/gallery/helpers/movie.php | 10 ++--- modules/gallery/helpers/photo.php | 10 ++--- modules/gallery/helpers/site_status.php | 4 +- modules/gallery/libraries/Gallery_I18n.php | 4 +- .../gallery/libraries/drivers/Cache/Database.php | 9 ++-- modules/gallery/models/item.php | 20 ++++----- modules/gallery/tests/Access_Helper_Test.php | 16 +++---- modules/gallery/tests/Cache_Test.php | 2 +- modules/gallery/tests/Database_Test.php | 49 +++++++++++----------- modules/gallery/tests/Gallery_Installer_Test.php | 2 +- modules/gallery/tests/I18n_Test.php | 4 +- modules/gallery/tests/Item_Helper_Test.php | 4 +- .../notification/helpers/notification_event.php | 4 +- modules/search/helpers/search.php | 10 ++--- modules/search/helpers/search_event.php | 2 +- modules/search/helpers/search_task.php | 4 +- modules/server_add/controllers/server_add.php | 8 ++-- modules/tag/controllers/admin_tags.php | 2 +- modules/tag/helpers/tag.php | 4 +- modules/tag/models/tag.php | 8 ++-- modules/tag/tests/Tag_Test.php | 6 +-- modules/user/helpers/group.php | 2 +- modules/user/helpers/user.php | 2 +- modules/user/tests/User_Groups_Test.php | 4 +- 51 files changed, 208 insertions(+), 203 deletions(-) (limited to 'modules/tag/helpers') diff --git a/modules/comment/controllers/admin_comments.php b/modules/comment/controllers/admin_comments.php index 039956d7..271c7d51 100644 --- a/modules/comment/controllers/admin_comments.php +++ b/modules/comment/controllers/admin_comments.php @@ -49,7 +49,7 @@ class Admin_Comments_Controller extends Admin_Controller { $view->content->state = $state; $view->content->comments = ORM::factory("comment") ->order_by("created", "DESC") - ->where("state", $state) + ->where("state", "=", $state) ->limit(self::$items_per_page, ($page - 1) * self::$items_per_page) ->find_all(); $view->content->pager = new Pagination(); @@ -120,7 +120,7 @@ class Admin_Comments_Controller extends Admin_Controller { access::verify_csrf(); ORM::factory("comment") - ->where("state", "spam") + ->where("state", "=", "spam") ->delete_all(); url::redirect("admin/comments/queue/spam"); } diff --git a/modules/comment/helpers/comment_event.php b/modules/comment/helpers/comment_event.php index a72102b9..cf5d0a60 100644 --- a/modules/comment/helpers/comment_event.php +++ b/modules/comment/helpers/comment_event.php @@ -29,7 +29,7 @@ class comment_event_Core { "guest_email" => null, "guest_name" => "guest", "guest_url" => null)) - ->where(array("author_id" => $user->id)) + ->where("author_id", "=", $user->id) ->update(); } @@ -40,7 +40,7 @@ class comment_event_Core { "guest_email" => null, "guest_name" => "guest", "guest_url" => null)) - ->where("1 = 1") + ->where("1", "=", "1") // @todo: why do we do this? ->update(); } @@ -65,7 +65,7 @@ class comment_event_Core { foreach (Database::instance() ->select("text") ->from("comments") - ->where("item_id", $item->id) + ->where("item_id", "=", $item->id) ->get() ->as_array() as $row) { $data[] = $row->text; diff --git a/modules/comment/helpers/comment_rss.php b/modules/comment/helpers/comment_rss.php index d0bb7859..77044884 100644 --- a/modules/comment/helpers/comment_rss.php +++ b/modules/comment/helpers/comment_rss.php @@ -35,11 +35,11 @@ class comment_rss_Core { $comments = ORM::factory("comment") ->viewable() - ->where("state", "published") + ->where("state", "=", "published") ->order_by("created", "DESC"); if ($feed_id == "item") { - $comments->where("item_id", $id); + $comments->where("item_id", "=", $id); } $feed->view = "comment.mrss"; diff --git a/modules/comment/helpers/comment_theme.php b/modules/comment/helpers/comment_theme.php index 2f12192b..ebcc1c42 100644 --- a/modules/comment/helpers/comment_theme.php +++ b/modules/comment/helpers/comment_theme.php @@ -37,8 +37,8 @@ class comment_theme_Core { $view = new View("comments.html"); $view->comments = ORM::factory("comment") - ->where("item_id", $theme->item()->id) - ->where("state", "published") + ->where("item_id", "=", $theme->item()->id) + ->where("state", "=", "published") ->order_by("created", "ASC") ->find_all(); diff --git a/modules/comment/tests/Comment_Model_Test.php b/modules/comment/tests/Comment_Model_Test.php index de19648d..aa91d6f2 100644 --- a/modules/comment/tests/Comment_Model_Test.php +++ b/modules/comment/tests/Comment_Model_Test.php @@ -29,12 +29,12 @@ class Comment_Model_Test extends Unit_Test_Case { access::allow(identity::everybody(), "view", $album); $this->assert_equal( 1, - ORM::factory("comment")->viewable()->where("comments.id", $comment->id)->count_all()); + ORM::factory("comment")->viewable()->where("comments.id", "=", $comment->id)->count_all()); // We can't see the comment when permissions are denied on the album access::deny(identity::everybody(), "view", $album); $this->assert_equal( 0, - ORM::factory("comment")->viewable()->where("comments.id", $comment->id)->count_all()); + ORM::factory("comment")->viewable()->where("comments.id", "=", $comment->id)->count_all()); } } diff --git a/modules/exif/helpers/exif.php b/modules/exif/helpers/exif.php index b6a55679..b4241e89 100644 --- a/modules/exif/helpers/exif.php +++ b/modules/exif/helpers/exif.php @@ -73,7 +73,7 @@ class exif_Core { } $item->save(); - $record = ORM::factory("exif_record")->where("item_id", $item->id)->find(); + $record = ORM::factory("exif_record")->where("item_id", "=", $item->id)->find(); if (!$record->loaded()) { $record->item_id = $item->id; } @@ -86,7 +86,7 @@ class exif_Core { static function get($item) { $exif = array(); $record = ORM::factory("exif_record") - ->where("item_id", $item->id) + ->where("item_id", "=", $item->id) ->find(); if (!$record->loaded()) { return array(); @@ -143,11 +143,11 @@ class exif_Core { ->select("items.id") ->from("items") ->join("exif_records", "items.id", "exif_records.item_id", "left") - ->where("type", "photo") - ->open_paren() - ->where("exif_records.item_id", null) - ->orwhere("exif_records.dirty", 1) - ->close_paren() + ->where("type", "=", "photo") + ->and_open() + ->where("exif_records.item_id", "=", null) + ->orwhere("exif_records.dirty", "=", 1) + ->close() ->get() ->count(); diff --git a/modules/exif/helpers/exif_task.php b/modules/exif/helpers/exif_task.php index 7c4c97c4..1a449fc7 100644 --- a/modules/exif/helpers/exif_task.php +++ b/modules/exif/helpers/exif_task.php @@ -44,11 +44,11 @@ class exif_task_Core { $start = microtime(true); foreach (ORM::factory("item") ->join("exif_records", "items.id", "exif_records.item_id", "left") - ->where("type", "photo") - ->open_paren() - ->where("exif_records.item_id", null) - ->orwhere("exif_records.dirty", 1) - ->close_paren() + ->where("type", "=", "photo") + ->and_open() + ->where("exif_records.item_id", "=", null) + ->orwhere("exif_records.dirty", "=", 1) + ->close() ->find_all() as $item) { // The query above can take a long time, so start the timer after its done // to give ourselves a little time to actually process rows. diff --git a/modules/exif/helpers/exif_theme.php b/modules/exif/helpers/exif_theme.php index bb6926d3..db51f305 100644 --- a/modules/exif/helpers/exif_theme.php +++ b/modules/exif/helpers/exif_theme.php @@ -24,7 +24,7 @@ class exif_theme_Core { $record = Database::instance() ->select("key_count") ->from("exif_records") - ->where("item_id", $item->id) + ->where("item_id", "=", $item->id) ->get() ->current(); if ($record && $record->key_count) { diff --git a/modules/g2_import/helpers/g2_import.php b/modules/g2_import/helpers/g2_import.php index 1e835a59..3cf7eb80 100644 --- a/modules/g2_import/helpers/g2_import.php +++ b/modules/g2_import/helpers/g2_import.php @@ -867,7 +867,7 @@ class g2_import_Core { */ static function map($g2_id) { if (!array_key_exists($g2_id, self::$map)) { - $g2_map = ORM::factory("g2_map")->where("g2_id", $g2_id)->find(); + $g2_map = ORM::factory("g2_map")->where("g2_id", "=", $g2_id)->find(); self::$map[$g2_id] = $g2_map->loaded() ? $g2_map->g3_id : null; } diff --git a/modules/g2_import/helpers/g2_import_task.php b/modules/g2_import/helpers/g2_import_task.php index fef0d186..e80b88b9 100644 --- a/modules/g2_import/helpers/g2_import_task.php +++ b/modules/g2_import/helpers/g2_import_task.php @@ -65,7 +65,7 @@ class g2_import_task_Core { $task->set("done", $done); $root_g2_id = g2(GalleryCoreApi::getDefaultAlbumId()); - $root = ORM::factory("g2_map")->where("g2_id", $root_g2_id)->find(); + $root = ORM::factory("g2_map")->where("g2_id", "=", $root_g2_id)->find(); if (!$root->loaded()) { $root->g2_id = $root_g2_id; $root->g3_id = 1; diff --git a/modules/gallery/controllers/admin_maintenance.php b/modules/gallery/controllers/admin_maintenance.php index 3b896553..6377f40f 100644 --- a/modules/gallery/controllers/admin_maintenance.php +++ b/modules/gallery/controllers/admin_maintenance.php @@ -41,9 +41,9 @@ class Admin_Maintenance_Controller extends Admin_Controller { $view->content = new View("admin_maintenance.html"); $view->content->task_definitions = task::get_definitions(); $view->content->running_tasks = ORM::factory("task") - ->where("done", 0)->order_by("updated", "DESC")->find_all(); + ->where("done", "=", 0)->order_by("updated", "DESC")->find_all(); $view->content->finished_tasks = ORM::factory("task") - ->where("done", 1)->order_by("updated", "DESC")->find_all(); + ->where("done", "=", 1)->order_by("updated", "DESC")->find_all(); print $view; } @@ -164,7 +164,7 @@ class Admin_Maintenance_Controller extends Admin_Controller { // Do it the long way so we can call delete and remove the cache. $finished = ORM::factory("task") - ->where(array("done" => 1)) + ->where("done", "=", 1) ->find_all(); foreach ($finished as $task) { task::remove($task->id); diff --git a/modules/gallery/controllers/albums.php b/modules/gallery/controllers/albums.php index 19140891..431d98a0 100644 --- a/modules/gallery/controllers/albums.php +++ b/modules/gallery/controllers/albums.php @@ -126,12 +126,12 @@ class Albums_Controller extends Items_Controller { if ($row = Database::instance() ->select(array("name", "slug")) ->from("items") - ->where("parent_id", $album->parent_id) - ->where("id <>", $album->id) - ->open_paren() - ->where("name", $form->edit_item->dirname->value) - ->orwhere("slug", $form->edit_item->slug->value) - ->close_paren() + ->where("parent_id", "=", $album->parent_id) + ->where("id", "<>", $album->id) + ->and_open() + ->where("name", "=", $form->edit_item->dirname->value) + ->orwhere("slug", "=", $form->edit_item->slug->value) + ->close() ->get() ->current()) { if ($row->name == $form->edit_item->dirname->value) { diff --git a/modules/gallery/controllers/file_proxy.php b/modules/gallery/controllers/file_proxy.php index 11d858c0..53afe0e4 100644 --- a/modules/gallery/controllers/file_proxy.php +++ b/modules/gallery/controllers/file_proxy.php @@ -57,7 +57,7 @@ class File_Proxy_Controller extends Controller { $path = preg_replace("|/.album.jpg$|", "", $path); // We now have the relative path to the item. Search for it in the path cache - $item = ORM::factory("item")->where("relative_path_cache", $path)->find(); + $item = ORM::factory("item")->where("relative_path_cache", "=", $path)->find(); if (!$item->loaded()) { // We didn't turn it up. It's possible that the relative_path_cache is out of date here. // There was fallback code, but bharat deleted it in 8f1bca74. If it turns out to be @@ -69,7 +69,7 @@ class File_Proxy_Controller extends Controller { if (preg_match('/.jpg$/', $path)) { foreach (array("flv", "mp4") as $ext) { $movie_path = preg_replace('/.jpg$/', ".$ext", $path); - $item = ORM::factory("item")->where("relative_path_cache", $movie_path)->find(); + $item = ORM::factory("item")->where("relative_path_cache", "=", $movie_path)->find(); if ($item->loaded()) { break; } diff --git a/modules/gallery/controllers/l10n_client.php b/modules/gallery/controllers/l10n_client.php index 2eda741c..30a18631 100644 --- a/modules/gallery/controllers/l10n_client.php +++ b/modules/gallery/controllers/l10n_client.php @@ -116,7 +116,7 @@ class L10n_Client_Controller extends Controller { foreach (Database::instance() ->select("key", "message") ->from("incoming_translations") - ->where(array("locale" => 'root')) + ->where("locale", "=", "root")) ->get() ->as_array() as $row) { $calls[$row->key] = array(unserialize($row->message), array()); @@ -131,7 +131,7 @@ class L10n_Client_Controller extends Controller { foreach (Database::instance() ->select("key", "translation") ->from("incoming_translations") - ->where(array("locale" => $locale)) + ->where("locale", "=", $locale) ->get() ->as_array() as $row) { $translations[$row->key] = unserialize($row->translation); @@ -140,7 +140,7 @@ class L10n_Client_Controller extends Controller { foreach (Database::instance() ->select("key", "translation") ->from("outgoing_translations") - ->where(array("locale" => $locale)) + ->where("locale", "=", $locale) ->get() ->as_array() as $row) { $translations[$row->key] = unserialize($row->translation); diff --git a/modules/gallery/controllers/move.php b/modules/gallery/controllers/move.php index 87b73436..863b13bb 100644 --- a/modules/gallery/controllers/move.php +++ b/modules/gallery/controllers/move.php @@ -64,8 +64,8 @@ class Move_Controller extends Controller { $view->parent = $target; $view->children = ORM::factory("item") ->viewable() - ->where("type", "album") - ->where("parent_id", $target->id) + ->where("type", "=", "album") + ->where("parent_id", "=", $target->id) ->find_all(); return $view; } diff --git a/modules/gallery/controllers/movies.php b/modules/gallery/controllers/movies.php index 3d5eac32..7ceeefdf 100644 --- a/modules/gallery/controllers/movies.php +++ b/modules/gallery/controllers/movies.php @@ -74,12 +74,12 @@ class Movies_Controller extends Items_Controller { if ($row = Database::instance() ->select(array("name", "slug")) ->from("items") - ->where("parent_id", $movie->parent_id) + ->where("parent_id", "=", $movie->parent_id) ->where("id <>", $movie->id) - ->open_paren() - ->where("name", $form->edit_item->filename->value) - ->orwhere("slug", $form->edit_item->slug->value) - ->close_paren() + ->and_open() + ->where("name", "=", $form->edit_item->filename->value) + ->orwhere("slug", "=", $form->edit_item->slug->value) + ->close() ->get() ->current()) { if ($row->name == $form->edit_item->filename->value) { diff --git a/modules/gallery/controllers/photos.php b/modules/gallery/controllers/photos.php index f052eccd..0d7daac4 100644 --- a/modules/gallery/controllers/photos.php +++ b/modules/gallery/controllers/photos.php @@ -74,12 +74,12 @@ class Photos_Controller extends Items_Controller { if ($row = Database::instance() ->select(array("name", "slug")) ->from("items") - ->where("parent_id", $photo->parent_id) - ->where("id <>", $photo->id) - ->open_paren() - ->where("name", $form->edit_item->filename->value) - ->orwhere("slug", $form->edit_item->slug->value) - ->close_paren() + ->where("parent_id", "=", $photo->parent_id) + ->where("id", "<>", $photo->id) + ->and_open() + ->where("name", "=", $form->edit_item->filename->value) + ->orwhere("slug", "=", $form->edit_item->slug->value) + ->close() ->get() ->current()) { if ($row->name == $form->edit_item->filename->value) { diff --git a/modules/gallery/helpers/MY_url.php b/modules/gallery/helpers/MY_url.php index e5eefad7..a2b2e461 100644 --- a/modules/gallery/helpers/MY_url.php +++ b/modules/gallery/helpers/MY_url.php @@ -50,12 +50,12 @@ class url extends url_Core { // In most cases, we'll have an exact match in the relative_url_cache item field. // but failing that, walk down the tree until we find it. The fallback code will fix caches // as it goes, so it'll never be run frequently. - $item = ORM::factory("item")->where("relative_url_cache", $current_uri)->find(); + $item = ORM::factory("item")->where("relative_url_cache", "=", $current_uri)->find(); if (!$item->loaded()) { $count = count(Router::$segments); foreach (ORM::factory("item") - ->where("slug", html_entity_decode(Router::$segments[$count - 1], ENT_QUOTES)) - ->where("level", $count + 1) + ->where("slug", "=", html_entity_decode(Router::$segments[$count - 1], ENT_QUOTES)) + ->where("level", "=", $count + 1) ->find_all() as $match) { if ($match->relative_url() == $current_uri) { $item = $match; diff --git a/modules/gallery/helpers/access.php b/modules/gallery/helpers/access.php index d0200a73..445f9b86 100644 --- a/modules/gallery/helpers/access.php +++ b/modules/gallery/helpers/access.php @@ -166,11 +166,11 @@ class access_Core { // For view permissions, if any parent is self::DENY, then those parents lock this one. // Return $lock = ORM::factory("item") - ->where("`left_ptr` <= $item->left_ptr") - ->where("`right_ptr` >= $item->right_ptr") - ->where("items.id <> $item->id") + ->where("left_ptr", "<=", $item->left_ptr) + ->where("right_ptr", ">=", $item->right_ptr) + ->where("items.id", "<>", $item->id) ->join("access_intents", "items.id", "access_intents.item_id") - ->where("access_intents.view_$group->id", self::DENY) + ->where("access_intents.view_$group->id", "=", self::DENY) ->order_by("level", "DESC") ->limit(1) ->find(); @@ -304,7 +304,7 @@ class access_Core { foreach (self::_get_all_groups() as $group) { self::_drop_columns($name, $group); } - $permission = ORM::factory("permission")->where("name", $name)->find(); + $permission = ORM::factory("permission")->where("name", "=", $name)->find(); if ($permission->loaded()) { $permission->delete(); } @@ -354,7 +354,7 @@ class access_Core { $access_cache->item_id = $item->id; if ($item->id != 1) { $parent_access_cache = - ORM::factory("access_cache")->where("item_id", $item->parent()->id)->find(); + ORM::factory("access_cache")->where("item_id", "=", $item->parent()->id)->find(); foreach (self::_get_all_groups() as $group) { foreach (ORM::factory("permission")->find_all() as $perm) { $field = "{$perm->name}_{$group->id}"; @@ -377,8 +377,8 @@ class access_Core { * @return void */ static function delete_item($item) { - ORM::factory("access_intent")->where("item_id", $item->id)->find()->delete(); - ORM::factory("access_cache")->where("item_id", $item->id)->find()->delete(); + ORM::factory("access_intent")->where("item_id", "=", $item->id)->find()->delete(); + ORM::factory("access_cache")->where("item_id", "=", $item->id)->find()->delete(); } /** @@ -475,7 +475,7 @@ class access_Core { * @return void */ private static function _update_access_view_cache($group, $item) { - $access = ORM::factory("access_intent")->where("item_id", $item->id)->find(); + $access = ORM::factory("access_intent")->where("item_id", "=", $item->id)->find(); $db = Database::instance(); $field = "view_{$group->id}"; @@ -490,10 +490,10 @@ class access_Core { // item, then its safe to propagate from here. if ($access->$field !== self::DENY) { $tmp_item = ORM::factory("item") - ->where("left_ptr <", $item->left_ptr) - ->where("right_ptr >", $item->right_ptr) + ->where("left_ptr", "<", $item->left_ptr) + ->where("right_ptr", ">", $item->right_ptr) ->join("access_intents", "access_intents.item_id", "items.id") - ->where("access_intents.$field", self::DENY) + ->where("access_intents.$field", "=", self::DENY) ->order_by("left_ptr", "DESC") ->limit(1) ->find(); @@ -512,10 +512,10 @@ class access_Core { $query = ORM::factory("access_intent") ->select(array("access_intents.$field", "items.left_ptr", "items.right_ptr", "items.id")) ->join("items", "items.id", "access_intents.item_id") - ->where("left_ptr >=", $item->left_ptr) - ->where("right_ptr <=", $item->right_ptr) - ->where("type", "album") - ->where("access_intents.$field IS NOT", self::INHERIT) + ->where("left_ptr", ">=", $item->left_ptr) + ->where("right_ptr", "<=", $item->right_ptr) + ->where("type", "=", "album") + ->where("access_intents.$field", "IS NOT", self::INHERIT) ->order_by("level", "DESC") ->find_all(); foreach ($query as $row) { @@ -549,7 +549,7 @@ class access_Core { * @return void */ private static function _update_access_non_view_cache($group, $perm_name, $item) { - $access = ORM::factory("access_intent")->where("item_id", $item->id)->find(); + $access = ORM::factory("access_intent")->where("item_id", "=", $item->id)->find(); $db = Database::instance(); $field = "{$perm_name}_{$group->id}"; @@ -562,9 +562,9 @@ class access_Core { if ($access->$field === self::INHERIT) { $tmp_item = ORM::factory("item") ->join("access_intents", "items.id", "access_intents.item_id") - ->where("left_ptr <", $item->left_ptr) - ->where("right_ptr >", $item->right_ptr) - ->where("$field IS NOT", self::UNKNOWN) + ->where("left_ptr", "<", $item->left_ptr) + ->where("right_ptr", ">", $item->right_ptr) + ->where($field, "IS NOT", self::UNKNOWN) ->order_by("left_ptr", "DESC") ->limit(1) ->find(); @@ -578,9 +578,9 @@ class access_Core { $query = ORM::factory("access_intent") ->select(array("access_intents.$field", "items.left_ptr", "items.right_ptr")) ->join("items", "items.id", "access_intents.item_id") - ->where("left_ptr >=", $item->left_ptr) - ->where("right_ptr <=", $item->right_ptr) - ->where("$field IS NOT", self::INHERIT) + ->where("left_ptr", ">=", $item->left_ptr) + ->where("right_ptr", "<=", $item->right_ptr) + ->where($field, "IS NOT", self::INHERIT) ->order_by("level", "ASC") ->find_all(); foreach ($query as $row) { diff --git a/modules/gallery/helpers/album.php b/modules/gallery/helpers/album.php index 84a60f83..cd8777e2 100644 --- a/modules/gallery/helpers/album.php +++ b/modules/gallery/helpers/album.php @@ -68,11 +68,11 @@ class album_Core { // Randomize the name or slug if there's a conflict // @todo Improve this. Random numbers are not user friendly while (ORM::factory("item") - ->where("parent_id", $parent->id) - ->open_paren() - ->where("name", $album->name) - ->orwhere("slug", $album->slug) - ->close_paren() + ->where("parent_id", "=", $parent->id) + ->and_open() + ->where("name", "=", $album->name) + ->orwhere("slug", "=", $album->slug) + ->close() ->find()->id) { $rand = rand(); $album->name = "{$name}-$rand"; diff --git a/modules/gallery/helpers/gallery_block.php b/modules/gallery/helpers/gallery_block.php index d09f1c80..40660874 100644 --- a/modules/gallery/helpers/gallery_block.php +++ b/modules/gallery/helpers/gallery_block.php @@ -45,8 +45,8 @@ class gallery_block_Core { $block->css_id = "g-photo-stream"; $block->title = t("Photo stream"); $block->content = new View("admin_block_photo_stream.html"); - $block->content->photos = - ORM::factory("item")->where("type", "photo")->order_by("created", "DESC")->find_all(10); + $block->content->photos = ORM::factory("item") + ->where("type", "=", "photo")->order_by("created", "DESC")->find_all(10); break; case "log_entries": @@ -62,8 +62,8 @@ class gallery_block_Core { $block->title = t("Gallery stats"); $block->content = new View("admin_block_stats.html"); $block->content->album_count = - ORM::factory("item")->where("type", "album")->where("id <>", 1)->count_all(); - $block->content->photo_count = ORM::factory("item")->where("type", "photo")->count_all(); + ORM::factory("item")->where("type", "=", "album")->where("id", "<>", 1)->count_all(); + $block->content->photo_count = ORM::factory("item")->where("type", "=", "photo")->count_all(); break; case "platform_info": diff --git a/modules/gallery/helpers/gallery_event.php b/modules/gallery/helpers/gallery_event.php index 02bfdf28..fa4db317 100644 --- a/modules/gallery/helpers/gallery_event.php +++ b/modules/gallery/helpers/gallery_event.php @@ -33,15 +33,15 @@ class gallery_event_Core { $db = Database::instance(); $db->from("tasks") ->set(array("owner_id" => $admin->id)) - ->where(array("owner_id" => $user->id)) + ->where("owner_id", "=", $user->id) ->update(); $db->from("items") ->set(array("owner_id" => $admin->id)) - ->where(array("owner_id" => $user->id)) + ->where("owner_id", "=", $user->id) ->update(); $db->from("logs") ->set(array("user_id" => $admin->id)) - ->where(array("user_id" => $user->id)) + ->where("user_id", "=", $user->id) ->update(); } @@ -50,15 +50,15 @@ class gallery_event_Core { $db = Database::instance(); $db->from("tasks") ->set(array("owner_id" => $admin->id)) - ->where("1 = 1") + ->where("1", "=", "1") // @todo why do we need this? ->update(); $db->from("items") ->set(array("owner_id" => $admin->id)) - ->where("1 = 1") + ->where("1", "=", "1") // @todo why do we need this? ->update(); $db->from("logs") ->set(array("user_id" => $admin->id)) - ->where("1 = 1") + ->where("1", "=", "1") // @todo why do we need this? ->update(); } diff --git a/modules/gallery/helpers/gallery_rss.php b/modules/gallery/helpers/gallery_rss.php index e195be8d..93ace10b 100644 --- a/modules/gallery/helpers/gallery_rss.php +++ b/modules/gallery/helpers/gallery_rss.php @@ -29,13 +29,13 @@ class gallery_rss_Core { case "latest": $feed->children = ORM::factory("item") ->viewable() - ->where("type !=", "album") + ->where("type", "<>", "album") ->order_by("created", "DESC") ->find_all($limit, $offset); $all_children = ORM::factory("item") ->viewable() - ->where("type !=", "album") + ->where("type", "<>", "album") ->order_by("created", "DESC"); $feed->max_pages = ceil($all_children->find_all()->count() / $limit); diff --git a/modules/gallery/helpers/graphics.php b/modules/gallery/helpers/graphics.php index aef09003..e45a5125 100644 --- a/modules/gallery/helpers/graphics.php +++ b/modules/gallery/helpers/graphics.php @@ -61,9 +61,9 @@ class graphics_Core { */ static function remove_rule($module_name, $target, $operation) { ORM::factory("graphics_rule") - ->where("module_name", $module_name) - ->where("target", $target) - ->where("operation", $operation) + ->where("module_name", "=", $module_name) + ->where("target", "=", $target) + ->where("operation", "=", $operation) ->delete_all(); self::mark_dirty($target == "thumb", $target == "resize"); @@ -181,8 +181,8 @@ class graphics_Core { if (empty(self::$_rules_cache[$target])) { $rules = array(); foreach (ORM::factory("graphics_rule") - ->where("target", $target) - ->where("active", true) + ->where("target", "=", $target) + ->where("active", "=", true) ->order_by("priority", "asc") ->find_all() as $rule) { $rules[] = (object)$rule->as_array(); diff --git a/modules/gallery/helpers/l10n_client.php b/modules/gallery/helpers/l10n_client.php index aaf6ff46..14ab5a85 100644 --- a/modules/gallery/helpers/l10n_client.php +++ b/modules/gallery/helpers/l10n_client.php @@ -134,12 +134,14 @@ class l10n_client_Core { // incoming_translations.message to be NULL? $locale = $message_data->locale; $entry = ORM::factory("incoming_translation") - ->where(array("key" => $key, "locale" => $locale)) + ->where("key", "=", $key) + ->where("locale", "=", $locale) ->find(); if (!$entry->loaded()) { // @todo Load a message key -> message (text) dict into memory outside of this loop $root_entry = ORM::factory("incoming_translation") - ->where(array("key" => $key, "locale" => "root")) + ->where("key", "=", $key) + ->where("locale", "=", "root") ->find(); $entry->key = $key; $entry->message = $root_entry->message; diff --git a/modules/gallery/helpers/l10n_scanner.php b/modules/gallery/helpers/l10n_scanner.php index 6c09a686..a7ce2c59 100644 --- a/modules/gallery/helpers/l10n_scanner.php +++ b/modules/gallery/helpers/l10n_scanner.php @@ -31,7 +31,7 @@ class l10n_scanner_Core { foreach (Database::instance() ->select("key") ->from("incoming_translations") - ->where("locale", "root") + ->where("locale", "=", "root") ->get() as $row) { $cache[$row->key] = true; } diff --git a/modules/gallery/helpers/module.php b/modules/gallery/helpers/module.php index b7e13b9a..d89b8401 100644 --- a/modules/gallery/helpers/module.php +++ b/modules/gallery/helpers/module.php @@ -354,7 +354,8 @@ class module_Core { $row = db::build() ->select("value") ->from("vars") - ->where(array("module_name" => "gallery", "name" => "_cache")) + ->where("module_name", "=", "gallery") + ->where("name", "=", "_cache") ->execute() ->current(); if ($row) { @@ -395,8 +396,8 @@ class module_Core { */ static function set_var($module_name, $name, $value) { $var = ORM::factory("var") - ->where("module_name", $module_name) - ->where("name", $name) + ->where("module_name", "=", $module_name) + ->where("name", "=", $name) ->find(); if (!$var->loaded()) { $var->module_name = $module_name; @@ -432,8 +433,8 @@ class module_Core { */ static function clear_var($module_name, $name) { $var = ORM::factory("var") - ->where("module_name", $module_name) - ->where("name", $name) + ->where("module_name", "=", $module_name) + ->where("name", "=", $name) ->find(); if ($var->loaded()) { $var->delete(); diff --git a/modules/gallery/helpers/movie.php b/modules/gallery/helpers/movie.php index ff86403a..82247eb0 100644 --- a/modules/gallery/helpers/movie.php +++ b/modules/gallery/helpers/movie.php @@ -90,11 +90,11 @@ class movie_Core { // Randomize the name if there's a conflict // @todo Improve this. Random numbers are not user friendly while (ORM::factory("item") - ->where("parent_id", $parent->id) - ->open_paren() - ->where("name", $movie->name) - ->orwhere("slug", $movie->slug) - ->close_paren() + ->where("parent_id", "=", $parent->id) + ->and_open() + ->where("name", "=", $movie->name) + ->orwhere("slug", "=", $movie->slug) + ->close() ->find()->id) { $rand = rand(); $movie->name = "{$name}.$rand.{$pi['extension']}"; diff --git a/modules/gallery/helpers/photo.php b/modules/gallery/helpers/photo.php index 21cb13a0..2a563043 100644 --- a/modules/gallery/helpers/photo.php +++ b/modules/gallery/helpers/photo.php @@ -89,11 +89,11 @@ class photo_Core { // Randomize the name or slug if there's a conflict // @todo Improve this. Random numbers are not user friendly while (ORM::factory("item") - ->where("parent_id", $parent->id) - ->open_paren() - ->where("name", $photo->name) - ->orwhere("slug", $photo->slug) - ->close_paren() + ->where("parent_id", "=", $parent->id) + ->and_open() + ->where("name", "=", $photo->name) + ->orwhere("slug", "=", $photo->slug) + ->close() ->find()->id) { $rand = rand(); $photo->name = "{$name}.$rand.{$pi['extension']}"; diff --git a/modules/gallery/helpers/site_status.php b/modules/gallery/helpers/site_status.php index d58b935d..04316fff 100644 --- a/modules/gallery/helpers/site_status.php +++ b/modules/gallery/helpers/site_status.php @@ -67,7 +67,7 @@ class site_status_Core { */ private static function _add($msg, $severity, $permanent_key) { $message = ORM::factory("message") - ->where("key", $permanent_key) + ->where("key", "=", $permanent_key) ->find(); if (!$message->loaded()) { $message->key = $permanent_key; @@ -82,7 +82,7 @@ class site_status_Core { * @param string $permanent_key */ static function clear($permanent_key) { - $message = ORM::factory("message")->where("key", $permanent_key)->find(); + $message = ORM::factory("message")->where("key", "=", $permanent_key)->find(); if ($message->loaded()) { $message->delete(); } diff --git a/modules/gallery/libraries/Gallery_I18n.php b/modules/gallery/libraries/Gallery_I18n.php index 42fae266..9a5e7dc1 100644 --- a/modules/gallery/libraries/Gallery_I18n.php +++ b/modules/gallery/libraries/Gallery_I18n.php @@ -131,7 +131,7 @@ class Gallery_I18n_Core { foreach (db::build() ->select("key", "translation") ->from("incoming_translations") - ->where(array("locale" => $locale)) + ->where("locale", "=", $locale) ->execute() ->as_array() as $row) { $this->_cache[$locale][$row->key] = unserialize($row->translation); @@ -141,7 +141,7 @@ class Gallery_I18n_Core { foreach (db::build() ->select("key", "translation") ->from("outgoing_translations") - ->where(array("locale" => $locale)) + ->where("locale", "=", $locale) ->execute() ->as_array() as $row) { $this->_cache[$locale][$row->key] = unserialize($row->translation); diff --git a/modules/gallery/libraries/drivers/Cache/Database.php b/modules/gallery/libraries/drivers/Cache/Database.php index eda445b6..a317798e 100644 --- a/modules/gallery/libraries/drivers/Cache/Database.php +++ b/modules/gallery/libraries/drivers/Cache/Database.php @@ -86,7 +86,7 @@ class Cache_Database_Driver extends Cache_Driver { public function get_tag($tags) { $db = db::build()->from("caches"); foreach ($tags as $tag) { - $db->where("tags", "like", "<$tag>"); + $db->where("tags", "LIKE", "<$tag>"); } $db_result = $db->execute()->as_array(); @@ -152,12 +152,12 @@ class Cache_Database_Driver extends Cache_Driver { public function delete($id, $tag = false) { $this->db->from("caches"); if ($id === true) { - $this->db->where(1); // Delete all caches + $this->db->where("1", "=", "1"); } else if ($tag === true) { $this->db->like("tags", "<$id>"); } else { - $this->db->where("key", $id); + $this->db->where("key", "=", $id); } $status = $this->db->delete(); @@ -178,7 +178,8 @@ class Cache_Database_Driver extends Cache_Driver { public function delete_expired() { // Delete all expired caches $status = $this->db->from("caches") - ->where(array("expiration !=" => 0, "expiration <=" => time())) + ->where("expiration", "<>", 0) + ->where("expiration", "<=", time()) ->delete(); return count($status) > 0; diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php index c8386b1c..c8d25cc5 100644 --- a/modules/gallery/models/item.php +++ b/modules/gallery/models/item.php @@ -288,9 +288,9 @@ class Item_Model extends ORM_MPTT { foreach (Database::instance() ->select(array("name", "slug")) ->from("items") - ->where("left_ptr <=", $this->left_ptr) - ->where("right_ptr >=", $this->right_ptr) - ->where("id <>", 1) + ->where("left_ptr", "<=", $this->left_ptr) + ->where("right_ptr", ">=", $this->right_ptr) + ->where("id", "<>", 1) ->order_by("left_ptr", "ASC") ->get() as $row) { // Don't encode the names segment @@ -433,8 +433,8 @@ class Item_Model extends ORM_MPTT { // If the comparison column has NULLs in it, we can't use comparators on it and will have to // deal with it the hard way. $count = $db->from("items") - ->where("parent_id", $this->id) - ->where($this->sort_column, NULL) + ->where("parent_id", "=", $this->id) + ->where($this->sort_column, "=", NULL) ->where($where) ->count_records(); @@ -443,8 +443,8 @@ class Item_Model extends ORM_MPTT { $sort_column = $this->sort_column; $position = $db->from("items") - ->where("parent_id", $this->id) - ->where("$sort_column $comp ", $child->$sort_column) + ->where("parent_id", "=", $this->id) + ->where($sort_column, $comp, $child->$sort_column) ->where($where) ->count_records(); @@ -457,8 +457,8 @@ class Item_Model extends ORM_MPTT { // Fix this by doing a 2nd query where we iterate over the equivalent columns and add them to // our base value. foreach ($db->from("items") - ->where("parent_id", $this->id) - ->where($sort_column, $child->$sort_column) + ->where("parent_id", "=", $this->id) + ->where($sort_column, "=", $child->$sort_column) ->where($where) ->order_by(array("id" => "ASC")) ->get() as $row) { @@ -484,7 +484,7 @@ class Item_Model extends ORM_MPTT { $position = 0; foreach ($db->select("id") ->from("items") - ->where("parent_id", $this->id) + ->where("parent_id", "=", $this->id) ->where($where) ->order_by($order_by) ->get() as $row) { diff --git a/modules/gallery/tests/Access_Helper_Test.php b/modules/gallery/tests/Access_Helper_Test.php index d90d7ed6..771c6a85 100644 --- a/modules/gallery/tests/Access_Helper_Test.php +++ b/modules/gallery/tests/Access_Helper_Test.php @@ -106,15 +106,15 @@ class Access_Helper_Test extends Unit_Test_Case { $item = album::create($root, rand(), "test album"); // New rows exist - $this->assert_true(ORM::factory("access_cache")->where("item_id", $item->id)->find()->loaded()); - $this->assert_true(ORM::factory("access_intent")->where("item_id", $item->id)->find()->loaded()); + $this->assert_true(ORM::factory("access_cache")->where("item_id", "=", $item->id)->find()->loaded()); + $this->assert_true(ORM::factory("access_intent")->where("item_id", "=", $item->id)->find()->loaded()); // Delete the item $item->delete(); // Rows are gone - $this->assert_false(ORM::factory("access_cache")->where("item_id", $item->id)->find()->loaded()); - $this->assert_false(ORM::factory("access_intent")->where("item_id", $item->id)->find()->loaded()); + $this->assert_false(ORM::factory("access_cache")->where("item_id", "=", $item->id)->find()->loaded()); + $this->assert_false(ORM::factory("access_intent")->where("item_id", "=", $item->id)->find()->loaded()); } public function new_photos_inherit_parent_permissions_test() { @@ -131,7 +131,7 @@ class Access_Helper_Test extends Unit_Test_Case { public function can_allow_deny_and_reset_intent_test() { $root = ORM::factory("item", 1); $album = album::create($root, rand(), "test album"); - $intent = ORM::factory("access_intent")->where("item_id", $album)->find(); + $intent = ORM::factory("access_intent")->where("item_id", "=", $album)->find(); // Allow access::allow(identity::everybody(), "view", $album); @@ -141,19 +141,19 @@ class Access_Helper_Test extends Unit_Test_Case { access::deny(identity::everybody(), "view", $album); $this->assert_same( access::DENY, - ORM::factory("access_intent")->where("item_id", $album)->find()->view_1); + ORM::factory("access_intent")->where("item_id", "=", $album)->find()->view_1); // Allow again. If the initial value was allow, then the first Allow clause above may not // have actually changed any values. access::allow(identity::everybody(), "view", $album); $this->assert_same( access::ALLOW, - ORM::factory("access_intent")->where("item_id", $album)->find()->view_1); + ORM::factory("access_intent")->where("item_id", "=", $album)->find()->view_1); access::reset(identity::everybody(), "view", $album); $this->assert_same( null, - ORM::factory("access_intent")->where("item_id", $album)->find()->view_1); + ORM::factory("access_intent")->where("item_id", "=", $album)->find()->view_1); } public function cant_reset_root_item_test() { diff --git a/modules/gallery/tests/Cache_Test.php b/modules/gallery/tests/Cache_Test.php index 6b525265..776c6625 100644 --- a/modules/gallery/tests/Cache_Test.php +++ b/modules/gallery/tests/Cache_Test.php @@ -20,7 +20,7 @@ class Cache_Test extends Unit_Test_Case { private $_driver; public function setup() { - Database::instance()->from("caches")->where(1)->delete(); + Database::instance()->from("caches")->where("1", "=", "1")->delete(); $this->_driver = new Cache_Database_Driver(); } diff --git a/modules/gallery/tests/Database_Test.php b/modules/gallery/tests/Database_Test.php index 98bd4046..4f5a1da2 100644 --- a/modules/gallery/tests/Database_Test.php +++ b/modules/gallery/tests/Database_Test.php @@ -20,8 +20,8 @@ class Database_Test extends Unit_Test_Case { function simple_where_test() { $sql = Database::instance() - ->where("a", 1) - ->where("b", 2) + ->where("a", "=", 1) + ->where("b", "=", 2) ->compile(); $sql = str_replace("\n", " ", $sql); $this->assert_same("SELECT * WHERE `a` = 1 AND `b` = 2", $sql); @@ -29,12 +29,12 @@ class Database_Test extends Unit_Test_Case { function compound_where_test() { $sql = Database::instance() - ->where("outer1", 1) - ->open_paren() - ->where("inner1", 1) - ->orwhere("inner2", 2) - ->close_paren() - ->where("outer2", 2) + ->where("outer1", "=", 1) + ->and_open() + ->where("inner1", "=", 1) + ->orwhere("inner2", "=", 2) + ->close() + ->where("outer2", "=", 2) ->compile(); $sql = str_replace("\n", " ", $sql); $this->assert_same( @@ -44,12 +44,12 @@ class Database_Test extends Unit_Test_Case { function group_first_test() { $sql = Database::instance() - ->open_paren() - ->where("inner1", 1) - ->orwhere("inner2", 2) - ->close_paren() - ->where("outer1", 1) - ->where("outer2", 2) + ->and_open() + ->where("inner1", "=", 1) + ->orwhere("inner2", "=", 2) + ->close() + ->where("outer1", "=", 1) + ->where("outer2", "=", 2) ->compile(); $sql = str_replace("\n", " ", $sql); $this->assert_same( @@ -59,11 +59,12 @@ class Database_Test extends Unit_Test_Case { function where_array_test() { $sql = Database::instance() - ->where("outer1", 1) - ->open_paren() - ->where("inner1", 1) - ->orwhere(array("inner2" => 2, "inner3" => 3)) - ->close_paren() + ->where("outer1", "=", 1) + ->and_open() + ->where("inner1", "=", 1) + ->orwhere("inner2", "=", 2) + ->orwhere("inner3", "=", 3)) + ->close() ->compile(); $sql = str_replace("\n", " ", $sql); $this->assert_same( @@ -73,10 +74,10 @@ class Database_Test extends Unit_Test_Case { function notlike_test() { $sql = Database::instance() - ->where("outer1", 1) - ->open_paren() - ->ornotlike("inner1", 1) - ->close_paren() + ->where("outer1", "=", 1) + ->or_open() + ->where("inner1", "NOT LIKE", 1) + ->close() ->compile(); $sql = str_replace("\n", " ", $sql); $this->assert_same( @@ -118,7 +119,7 @@ class Database_Test extends Unit_Test_Case { function prefix_no_replacement_test() { $update = Database_For_Test::instance()->from("test_tables") - ->where("1 = 1") + ->where("1", "=", "1") ->set(array("name" => "Test Name")) ->update(); diff --git a/modules/gallery/tests/Gallery_Installer_Test.php b/modules/gallery/tests/Gallery_Installer_Test.php index 36ced2bb..f36f638f 100644 --- a/modules/gallery/tests/Gallery_Installer_Test.php +++ b/modules/gallery/tests/Gallery_Installer_Test.php @@ -29,7 +29,7 @@ class Gallery_Installer_Test extends Unit_Test_Case { } public function install_registers_gallery_module_test() { - $gallery = ORM::factory("module")->where("name", "gallery")->find(); + $gallery = ORM::factory("module")->where("name", "=", "gallery")->find(); $this->assert_equal("gallery", $gallery->name); } diff --git a/modules/gallery/tests/I18n_Test.php b/modules/gallery/tests/I18n_Test.php index d0555cbf..895e3051 100644 --- a/modules/gallery/tests/I18n_Test.php +++ b/modules/gallery/tests/I18n_Test.php @@ -29,7 +29,7 @@ class Gallery_I18n_Test extends Unit_Test_Case { $this->i18n = Gallery_I18n::instance($config); ORM::factory("incoming_translation") - ->where("locale", "te_ST") + ->where("locale", "=", "te_ST") ->delete_all(); $messages_te_ST = array( @@ -62,7 +62,7 @@ class Gallery_I18n_Test extends Unit_Test_Case { $locale = $this->i18n->locale(); $this->assert_equal("de_DE", $locale); } - + public function translate_simple_test() { $result = $this->i18n->translate('Hello world'); $this->assert_equal('Hallo Welt', $result); diff --git a/modules/gallery/tests/Item_Helper_Test.php b/modules/gallery/tests/Item_Helper_Test.php index a364423a..f0c653c0 100644 --- a/modules/gallery/tests/Item_Helper_Test.php +++ b/modules/gallery/tests/Item_Helper_Test.php @@ -29,13 +29,13 @@ class Item_Helper_Test extends Unit_Test_Case { access::allow(identity::everybody(), "view", $album); $this->assert_equal( 1, - ORM::factory("item")->viewable()->where("id", $item->id)->count_all()); + ORM::factory("item")->viewable()->where("id", "=", $item->id)->count_all()); // We can't see the item when permissions are denied access::deny(identity::everybody(), "view", $album); $this->assert_equal( 0, - ORM::factory("item")->viewable()->where("id", $item->id)->count_all()); + ORM::factory("item")->viewable()->where("id", "=", $item->id)->count_all()); } public function validate_url_safe_test() { diff --git a/modules/notification/helpers/notification_event.php b/modules/notification/helpers/notification_event.php index e6d09d74..951e6e52 100644 --- a/modules/notification/helpers/notification_event.php +++ b/modules/notification/helpers/notification_event.php @@ -54,7 +54,7 @@ class notification_event_Core { static function user_deleted($user) { ORM::factory("subscriptions") - ->where(array("user_id", $user->id)) + ->where("user_id", "=", $user->id) ->delete_all(); } @@ -88,7 +88,7 @@ class notification_event_Core { static function user_before_delete($user) { try { ORM::factory("subscription") - ->where("user_id", $user->id) + ->where("user_id", "=", $user->id) ->delete_all(); } catch (Exception $e) { Kohana_Log::add("error", "@todo notification_event::user_before_delete() failed"); diff --git a/modules/search/helpers/search.php b/modules/search/helpers/search.php index 9d732c11..c0bb1b32 100644 --- a/modules/search/helpers/search.php +++ b/modules/search/helpers/search.php @@ -60,7 +60,7 @@ class search_Core { static function update($item) { $data = new ArrayObject(); - $record = ORM::factory("search_record")->where("item_id", $item->id)->find(); + $record = ORM::factory("search_record")->where("item_id", "=", $item->id)->find(); if (!$record->loaded()) { $record->item_id = $item->id; } @@ -76,10 +76,10 @@ class search_Core { ->select("items.id") ->from("items") ->join("search_records", "items.id", "search_records.item_id", "left") - ->open_paren() - ->where("search_records.item_id", null) - ->orwhere("search_records.dirty", 1) - ->close_paren() + ->and_open() + ->where("search_records.item_id", "=", null) + ->orwhere("search_records.dirty", "=", 1) + ->close() ->get() ->count(); diff --git a/modules/search/helpers/search_event.php b/modules/search/helpers/search_event.php index 836bbe15..1add6e5f 100644 --- a/modules/search/helpers/search_event.php +++ b/modules/search/helpers/search_event.php @@ -28,7 +28,7 @@ class search_event_Core { static function item_deleted($item) { ORM::factory("search_record") - ->where("item_id", $item->id) + ->where("item_id", "=", $item->id) ->delete_all(); } diff --git a/modules/search/helpers/search_task.php b/modules/search/helpers/search_task.php index 9508f420..061f4084 100644 --- a/modules/search/helpers/search_task.php +++ b/modules/search/helpers/search_task.php @@ -45,8 +45,8 @@ class search_task_Core { $start = microtime(true); foreach (ORM::factory("item") ->join("search_records", "items.id", "search_records.item_id", "left") - ->where("search_records.item_id", null) - ->orwhere("search_records.dirty", 1) + ->where("search_records.item_id", "=", null) + ->orwhere("search_records.dirty", "=", 1) ->find_all() as $item) { // The query above can take a long time, so start the timer after its done // to give ourselves a little time to actually process rows. diff --git a/modules/server_add/controllers/server_add.php b/modules/server_add/controllers/server_add.php index 053a1891..3c3a6c2b 100644 --- a/modules/server_add/controllers/server_add.php +++ b/modules/server_add/controllers/server_add.php @@ -177,7 +177,7 @@ class Server_Add_Controller extends Admin_Controller { $task->percent_complete = min($task->percent_complete + 0.1, 10); $task->status = t2("Found one file", "Found %count files", Database::instance() - ->where("task_id", $task->id) + ->where("task_id", "=", $task->id) ->count_records("server_add_files")); if (!$queue) { @@ -197,8 +197,8 @@ class Server_Add_Controller extends Admin_Controller { // will create albums first. Ignore entries which already have an Item_Model attached, // they're done. $entries = ORM::factory("server_add_file") - ->where("task_id", $task->id) - ->where("item_id", null) + ->where("task_id", "=", $task->id) + ->where("item_id", "=", null) ->order_by("id", "ASC") ->limit(10) ->find_all(); @@ -265,7 +265,7 @@ class Server_Add_Controller extends Admin_Controller { $task->done = true; $task->state = "success"; $task->percent_complete = 100; - ORM::factory("server_add_file")->where("task_id", $task->id)->delete_all(); + ORM::factory("server_add_file")->where("task_id", "=", $task->id)->delete_all(); message::info(t2("Successfully added one photo / album", "Successfully added %count photos / albums", $task->get("completed_files"))); diff --git a/modules/tag/controllers/admin_tags.php b/modules/tag/controllers/admin_tags.php index aff44803..6cd2f337 100644 --- a/modules/tag/controllers/admin_tags.php +++ b/modules/tag/controllers/admin_tags.php @@ -106,7 +106,7 @@ class Admin_Tags_Controller extends Admin_Controller { } public function check_for_duplicate(Validation $post_data, $field) { - $tag_exists = ORM::factory("tag")->where("name", $post_data[$field])->count_all(); + $tag_exists = ORM::factory("tag")->where("name", "=", $post_data[$field])->count_all(); if ($tag_exists) { $post_data->add_error($field, "in_use"); } diff --git a/modules/tag/helpers/tag.php b/modules/tag/helpers/tag.php index 8694bcec..c4c4ba15 100644 --- a/modules/tag/helpers/tag.php +++ b/modules/tag/helpers/tag.php @@ -33,7 +33,7 @@ class tag_Core { throw new exception("@todo MISSING_TAG_NAME"); } - $tag = ORM::factory("tag")->where("name", $tag_name)->find(); + $tag = ORM::factory("tag")->where("name", "=", $tag_name)->find(); if (!$tag->loaded()) { $tag->name = $tag_name; $tag->count = 0; @@ -93,7 +93,7 @@ class tag_Core { ->select("name") ->from("tags") ->join("items_tags", "tags.id", "items_tags.tag_id", "left") - ->where("items_tags.item_id", $item->id) + ->where("items_tags.item_id", "=", $item->id) ->get() as $row) { $tags[] = $row->name; } diff --git a/modules/tag/models/tag.php b/modules/tag/models/tag.php index be020f5f..f9a453be 100644 --- a/modules/tag/models/tag.php +++ b/modules/tag/models/tag.php @@ -31,9 +31,9 @@ class Tag_Model extends ORM { $model = ORM::factory("item") ->viewable() ->join("items_tags", "items.id", "items_tags.item_id") - ->where("items_tags.tag_id", $this->id); + ->where("items_tags.tag_id", "=", $this->id); if ($type) { - $model->where("items.type", $type); + $model->where("items.type", "=", $type); } return $model->find_all($limit, $offset); } @@ -47,10 +47,10 @@ class Tag_Model extends ORM { $model = ORM::factory("item") ->viewable() ->join("items_tags", "items.id", "items_tags.item_id") - ->where("items_tags.tag_id", $this->id); + ->where("items_tags.tag_id", "=", $this->id); if ($type) { - $model->where("items.type", $type); + $model->where("items.type", "=", $type); } return $model->count_all(); } diff --git a/modules/tag/tests/Tag_Test.php b/modules/tag/tests/Tag_Test.php index c9a96286..c96e7f2b 100644 --- a/modules/tag/tests/Tag_Test.php +++ b/modules/tag/tests/Tag_Test.php @@ -25,18 +25,18 @@ class Tag_Test extends Unit_Test_Case { $tag1 = "tag1"; tag::add($album, $tag1); - $tag = ORM::factory("tag")->where("name", $tag1)->find(); + $tag = ORM::factory("tag")->where("name", "=", $tag1)->find(); $this->assert_true(1, $tag->count); // Make sure adding the tag again doesn't increase the count tag::add($album, $tag1); - $tag = ORM::factory("tag")->where("name", $tag1)->find(); + $tag = ORM::factory("tag")->where("name", "=", $tag1)->find(); $this->assert_true(1, $tag->count); $rand = rand(); $album = album::create($root, $rand, $rand, $rand); tag::add($album, $tag1); - $tag = ORM::factory("tag")->where("name", $tag1)->find(); + $tag = ORM::factory("tag")->where("name", "=", $tag1)->find(); $this->assert_true(2, $tag->count); } } \ No newline at end of file diff --git a/modules/user/helpers/group.php b/modules/user/helpers/group.php index 1beaa1c2..2ada0ac1 100644 --- a/modules/user/helpers/group.php +++ b/modules/user/helpers/group.php @@ -31,7 +31,7 @@ class group_Core { * @return Group_Definition the group object */ static function create($name) { - $group = ORM::factory("group")->where("name", $name)->find(); + $group = ORM::factory("group")->where("name", "=", $name)->find(); if ($group->loaded()) { throw new Exception("@todo GROUP_ALREADY_EXISTS $name"); } diff --git a/modules/user/helpers/user.php b/modules/user/helpers/user.php index 4ed9daee..5027580c 100644 --- a/modules/user/helpers/user.php +++ b/modules/user/helpers/user.php @@ -44,7 +44,7 @@ class user_Core { * @return User_Model */ static function create($name, $full_name, $password) { - $user = ORM::factory("user")->where("name", $name)->find(); + $user = ORM::factory("user")->where("name", "=", $name)->find(); if ($user->loaded()) { throw new Exception("@todo USER_ALREADY_EXISTS $name"); } diff --git a/modules/user/tests/User_Groups_Test.php b/modules/user/tests/User_Groups_Test.php index 6aedfde5..163b7d79 100644 --- a/modules/user/tests/User_Groups_Test.php +++ b/modules/user/tests/User_Groups_Test.php @@ -21,14 +21,14 @@ class User_Groups_Test extends Unit_Test_Case { public function teardown() { try { - $group = ORM::factory("group")->where("name", "user_groups_test")->find(); + $group = ORM::factory("group")->where("name", "=", "user_groups_test")->find(); if ($group->loaded()) { $group->delete(); } } catch (Exception $e) { } try { - $user = ORM::factory("user")->where("name", "user_groups_test")->find(); + $user = ORM::factory("user")->where("name", "=", "user_groups_test")->find(); if ($user->loaded()) { $user->delete(); } -- cgit v1.2.3 From 96b00d6cfe437e376d5547a10aa8d1cf7def8c13 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Thu, 26 Nov 2009 21:14:54 -0800 Subject: Convert some more Database::instance() calls to db::build() form. --- modules/comment/helpers/comment_event.php | 5 +- modules/gallery/helpers/item.php | 4 +- modules/gallery/libraries/ORM_MPTT.php | 90 +++++++++++++++++---------- modules/gallery/models/item.php | 6 +- modules/notification/helpers/notification.php | 4 +- modules/tag/helpers/tag.php | 4 +- 6 files changed, 69 insertions(+), 44 deletions(-) (limited to 'modules/tag/helpers') diff --git a/modules/comment/helpers/comment_event.php b/modules/comment/helpers/comment_event.php index cf5d0a60..c90f7663 100644 --- a/modules/comment/helpers/comment_event.php +++ b/modules/comment/helpers/comment_event.php @@ -62,12 +62,11 @@ class comment_event_Core { } static function item_index_data($item, $data) { - foreach (Database::instance() + foreach (db::build() ->select("text") ->from("comments") ->where("item_id", "=", $item->id) - ->get() - ->as_array() as $row) { + ->execute() as $row) { $data[] = $row->text; } } diff --git a/modules/gallery/helpers/item.php b/modules/gallery/helpers/item.php index 7496d368..c3126435 100644 --- a/modules/gallery/helpers/item.php +++ b/modules/gallery/helpers/item.php @@ -140,10 +140,10 @@ class item_Core { // Guard against an empty result when we create the first item. It's unfortunate that we // have to check this every time. // @todo: figure out a better way to bootstrap the weight. - $result = Database::instance() + $result = db::build() ->select("weight")->from("items") ->order_by("weight", "desc")->limit(1) - ->get()->current(); + ->execute()->current(); return ($result ? $result->weight : 0) + 1; } diff --git a/modules/gallery/libraries/ORM_MPTT.php b/modules/gallery/libraries/ORM_MPTT.php index a67f05be..0ec0a848 100644 --- a/modules/gallery/libraries/ORM_MPTT.php +++ b/modules/gallery/libraries/ORM_MPTT.php @@ -51,10 +51,16 @@ class ORM_MPTT_Core extends ORM { try { // Make a hole in the parent for this new item - $this->db->query( - "UPDATE {{$this->table_name}} SET `left_ptr` = `left_ptr` + 2 WHERE `left_ptr` >= {$parent->right_ptr}"); - $this->db->query( - "UPDATE {{$this->table_name}} SET `right_ptr` = `right_ptr` + 2 WHERE `right_ptr` >= {$parent->right_ptr}"); + $this->db_builder + ->update($this->table_name) + ->set("left_ptr", new Database_Expression("`left_ptr` + 2")) + ->where("left_ptr", ">=", $parent->right_ptr) + ->execute(); + $this->db_builder + ->update($this->table_name) + ->set("right_ptr", new Database_Expression("`right_ptr` + 2")) + ->where("right_ptr", ">=", $parent->right_ptr) + ->execute(); $parent->right_ptr += 2; // Insert this item into the hole @@ -92,10 +98,16 @@ class ORM_MPTT_Core extends ORM { $this->lock(); try { - $this->db->query( - "UPDATE {{$this->table_name}} SET `left_ptr` = `left_ptr` - 2 WHERE `left_ptr` > {$this->right_ptr}"); - $this->db->query( - "UPDATE {{$this->table_name}} SET `right_ptr` = `right_ptr` - 2 WHERE `right_ptr` > {$this->right_ptr}"); + $this->db_builder + ->update($this->table_name) + ->set("left_ptr", new Database_Expression("`left_ptr` - 2")) + ->where("left_ptr", ">", $this->right_ptr) + ->execute(); + $this->db_builder + ->update($this->table_name) + ->set("right_ptr", new Database_Expression("`right_ptr` - 2")) + ->where("right_ptr", ">", $this->right_ptr) + ->execute(); } catch (Exception $e) { $this->unlock(); throw $e; @@ -239,23 +251,32 @@ class ORM_MPTT_Core extends ORM { try { if ($level_delta) { // Update the levels for the to-be-moved items - $this->db->query( - "UPDATE {{$this->table_name}} SET `level` = `level` + $level_delta" . - " WHERE `left_ptr` >= $original_left_ptr AND `right_ptr` <= $original_right_ptr"); + $this->db_builder + ->update($this->table_name) + ->set("level", new Database_Expression("`level` + $level_delta")) + ->where("left_ptr", ">=", $original_left_ptr) + ->where("right_ptr", "<=", $original_right_ptr) + ->execute(); } // Make a hole in the target for the move - $target->db->query( - "UPDATE {{$this->table_name}} SET `left_ptr` = `left_ptr` + $size_of_hole" . - " WHERE `left_ptr` >= $target_right_ptr"); - $target->db->query( - "UPDATE {{$this->table_name}} SET `right_ptr` = `right_ptr` + $size_of_hole" . - " WHERE `right_ptr` >= $target_right_ptr"); + $target->db_builder + ->update($this->table_name) + ->set("left_ptr", new Database_Expression("`left_ptr` + $size_of_hole")) + ->where("left_ptr", ">=", $target_right_ptr) + ->execute(); + $target->db_builder + ->update($this->table_name) + ->set("right_ptr", new Database_Expression("`right_ptr` + $size_of_hole")) + ->where("right_ptr", ">=", $target_right_ptr) + ->execute(); // Change the parent. - $this->db->query( - "UPDATE {{$this->table_name}} SET `parent_id` = {$target->id}" . - " WHERE `id` = {$this->id}"); + $this->db_builder + ->update($this->table_name) + ->set("parent_id", $target->id) + ->where("id", "=", $this->id) + ->execute(); // If the source is to the right of the target then we just adjusted its left_ptr and right_ptr above. $left_ptr = $original_left_ptr; @@ -266,20 +287,25 @@ class ORM_MPTT_Core extends ORM { } $new_offset = $target->right_ptr - $left_ptr; - $this->db->query( - "UPDATE {{$this->table_name}}" . - " SET `left_ptr` = `left_ptr` + $new_offset," . - " `right_ptr` = `right_ptr` + $new_offset" . - " WHERE `left_ptr` >= $left_ptr" . - " AND `right_ptr` <= $right_ptr"); + $this->db_builder + ->update($this->table_name) + ->set("left_ptr", new Database_Expression("`left_ptr` + $new_offset")) + ->set("right_ptr", new Database_Expression("`right_ptr` + $new_offset")) + ->where("left_ptr", ">=", $left_ptr) + ->where("right_ptr", "<=", $right_ptr) + ->execute(); // Close the hole in the source's parent after the move - $this->db->query( - "UPDATE {{$this->table_name}} SET `left_ptr` = `left_ptr` - $size_of_hole" . - " WHERE `left_ptr` > $right_ptr"); - $this->db->query( - "UPDATE {{$this->table_name}} SET `right_ptr` = `right_ptr` - $size_of_hole" . - " WHERE `right_ptr` > $right_ptr"); + $this->db_builder + ->update($this->table_name) + ->set("left_ptr", new Database_Expression("`left_ptr` - $size_of_hole")) + ->where("left_ptr", ">", $right_ptr) + ->execute(); + $this->db_builder + ->update($this->table_name) + ->set("right_ptr", new Database_Expression("`right_ptr` - $size_of_hole")) + ->where("right_ptr", ">", $right_ptr) + ->execute(); } catch (Exception $e) { $this->unlock(); throw $e; diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php index 16c57dbc..8a42cc1e 100644 --- a/modules/gallery/models/item.php +++ b/modules/gallery/models/item.php @@ -285,14 +285,14 @@ class Item_Model extends ORM_MPTT { private function _build_relative_caches() { $names = array(); $slugs = array(); - foreach (Database::instance() + foreach (db::build() ->select(array("name", "slug")) ->from("items") ->where("left_ptr", "<=", $this->left_ptr) ->where("right_ptr", ">=", $this->right_ptr) ->where("id", "<>", 1) ->order_by("left_ptr", "ASC") - ->get() as $row) { + ->execute() as $row) { // Don't encode the names segment $names[] = rawurlencode($row->name); $slugs[] = rawurlencode($row->slug); @@ -489,7 +489,7 @@ class Item_Model extends ORM_MPTT { ->where("parent_id", "=", $this->id) ->merge_where($where) ->order_by($order_by) - ->get() as $row) { + ->execute() as $row) { $position++; if ($row->id == $child->id) { break; diff --git a/modules/notification/helpers/notification.php b/modules/notification/helpers/notification.php index 7e935614..31a56c1f 100644 --- a/modules/notification/helpers/notification.php +++ b/modules/notification/helpers/notification.php @@ -170,10 +170,10 @@ class notification { } static function send_pending_notifications() { - foreach (Database::instance() + foreach (db::build() ->select("DISTINCT email") ->from("pending_notifications") - ->get() as $row) { + ->execute() as $row) { $email = $row->email; $result = ORM::factory("pending_notification") ->where("email", "=", $email) diff --git a/modules/tag/helpers/tag.php b/modules/tag/helpers/tag.php index c4c4ba15..3e8a0d20 100644 --- a/modules/tag/helpers/tag.php +++ b/modules/tag/helpers/tag.php @@ -89,12 +89,12 @@ class tag_Core { */ static function item_tags($item) { $tags = array(); - foreach (Database::instance() + foreach (db::build() ->select("name") ->from("tags") ->join("items_tags", "tags.id", "items_tags.tag_id", "left") ->where("items_tags.item_id", "=", $item->id) - ->get() as $row) { + ->execute() as $row) { $tags[] = $row->name; } return $tags; -- cgit v1.2.3 From 2f5c612036984a6f09995e8c692f399fb8c7fb15 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sun, 6 Dec 2009 21:33:17 -0800 Subject: Update database queries. --- modules/tag/helpers/tag.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'modules/tag/helpers') diff --git a/modules/tag/helpers/tag.php b/modules/tag/helpers/tag.php index 3e8a0d20..8075afe4 100644 --- a/modules/tag/helpers/tag.php +++ b/modules/tag/helpers/tag.php @@ -125,10 +125,16 @@ class tag_Core { * Delete all tags associated with an item */ static function clear_all($item) { - $db = Database::instance(); - $db->query("UPDATE {tags} SET `count` = `count` - 1 WHERE `count` > 0 " . - "AND `id` IN (SELECT `tag_id` from {items_tags} WHERE `item_id` = $item->id)"); - $db->delete("items_tags", array("item_id" => "$item->id")); + db::build() + ->update("tags") + ->set("count", new Database_Expression("`count` - 1")) + ->where("count", ">", 0) + ->where("id", "IN", db::build()->select("tag_id")->from("items_tags")->where("item_id", "=", $item->id)) + ->execute(); + db::build() + ->delete("items_tags") + ->where("item_id", "=", $item->id) + ->execute(); } /** @@ -138,6 +144,6 @@ class tag_Core { // @todo There's a potential race condition here which we can solve by adding a lock around // this and all the cases where we create/update tags. I'm loathe to do that since it's an // extremely rare case. - Database::instance() ->delete("tags", array("count" => 0)); + db::build()->delete("tags")->where("count", "=", 0)->execute(); } } \ No newline at end of file -- cgit v1.2.3 From cf6de01f1e51a8dca97347600a844a07b4f80f8b Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Wed, 23 Dec 2009 14:20:23 -0800 Subject: Provide a REST interface to retrieve tags or tagged items. GET /tag?limit=nn,offset=nn Retrieve all tags sorted by count descending GET /tag/uri/uri/uri Retrieve all the tags for the resource specified by path GET /tag/t1/t2/t3 Retrieve all the items that have the specified tags Can specifiy limit and offset to limit response size --- modules/tag/helpers/tag_rest.php | 80 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 modules/tag/helpers/tag_rest.php (limited to 'modules/tag/helpers') diff --git a/modules/tag/helpers/tag_rest.php b/modules/tag/helpers/tag_rest.php new file mode 100644 index 00000000..eec90971 --- /dev/null +++ b/modules/tag/helpers/tag_rest.php @@ -0,0 +1,80 @@ +arguments)) { + $tags = ORM::factory("tag") + ->select("name", "count") + ->orderby("count", "DESC"); + if (!empty($request->limit)) { + $tags->limit($request->limit); + } + if (!empty($request->offset)) { + $tags->offset($request->offset); + } + $response = array("tags" => array()); + foreach ($tags->find_all() as $row) { + $response["tags"][] = array("name" => $row->name, "count" => $row->count); + } + } else { + $item = ORM::factory("item") + ->where("relative_url_cache", implode("/", $request->arguments)) + ->viewable() + ->find(); + if ($item->loaded) { + $response = array("tags" => tag::item_tags($item)); + } else { + $response = array("resources" => tag_rest::_get_items($request)); + } + } + + return rest::success($response); + } + + private static function _get_items($request) { + $tags = $request->arguments; + $items = ORM::factory("item") + ->join("items_tags", "items.id", "items_tags.item_id", "left") + ->join("tags", "tags.id", "items_tags.tag_id", "left") + ->where("tags.name", array_shift($tags)); + if (!empty($request->limit)) { + $items->limit($request->limit); + } + if (!empty($request->offset)) { + $tags->offset($request->offset); + } + foreach ($tags as $tag) { + $items->orWhere("tags.name", $tag); + } + $resources = array(); + foreach ($items->find_all() as $item) { + $resources[] = array("type" => $item->type, + "has_children" => $item->children_count() > 0, + "path" => $item->relative_url(), + "thumb_url" => $item->thumb_url(true), + "thumb_dimensions" => array("width" => $item->thumb_width, + "height" => $item->thumb_height), + "has_thumb" => $item->has_thumb(), + "title" => $item->title); + } + + return $resources; + } +} -- cgit v1.2.3 From 586f2d78b85cbe964a625b3c83896c1c8a991702 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Thu, 24 Dec 2009 19:25:14 -0800 Subject: Add the functionality to add, delete and rename tags via the Gallery3 remote interface. --- modules/tag/helpers/tag_rest.php | 104 +++++++++++-- modules/tag/tests/Tag_Rest_Helper_Test.php | 227 +++++++++++++++++++++++++++++ 2 files changed, 317 insertions(+), 14 deletions(-) create mode 100644 modules/tag/tests/Tag_Rest_Helper_Test.php (limited to 'modules/tag/helpers') diff --git a/modules/tag/helpers/tag_rest.php b/modules/tag/helpers/tag_rest.php index eec90971..d62c0231 100644 --- a/modules/tag/helpers/tag_rest.php +++ b/modules/tag/helpers/tag_rest.php @@ -34,12 +34,18 @@ class tag_rest_Core { $response["tags"][] = array("name" => $row->name, "count" => $row->count); } } else { - $item = ORM::factory("item") - ->where("relative_url_cache", implode("/", $request->arguments)) - ->viewable() - ->find(); - if ($item->loaded) { - $response = array("tags" => tag::item_tags($item)); + $path = implode("/", $request->arguments); + if (strpos($path, ",") === false) { + $item = ORM::factory("item") + ->where("relative_url_cache", $path) + ->viewable() + ->find(); + // If we didn't find it and there was only one argument, retry as a tag not a path + if ($item->loaded || count($request->arguments) != 1) { + $response = array("tags" => $item->loaded ? tag::item_tags($item) : array()); + } else { + $response = array("resources" => tag_rest::_get_items($request)); + } } else { $response = array("resources" => tag_rest::_get_items($request)); } @@ -48,20 +54,90 @@ class tag_rest_Core { return rest::success($response); } + static function post($request) { + if (empty($request->arguments) || count($request->arguments) != 1 || empty($request->path)) { + return rest::invalid_request(); + } + $path = $request->path; + $tags = explode(",", $request->arguments[0]); + + $item = ORM::factory("item") + ->where("relative_url_cache", $path) + ->viewable() + ->find(); + if (!$item->loaded) { + return rest::not_found("Resource: {$path} missing."); + } + + if (!access::can("edit", $item)) { + return rest::not_found("Resource: {$path} permission denied."); + } + + foreach ($tags as $tag) { + tag::add($item, $tag); + } + return rest::success(); + } + + static function put($request) { + if (empty($request->arguments[0]) || empty($request->new_name)) { + return rest::invalid_request(); + } + + $name = $request->arguments[0]; + + $tag = ORM::factory("tag") + ->where("name", $name) + ->find(); + if (!$tag->loaded) { + return rest::not_found("Tag: {$name} not found."); + } + + $tag->name = $request->new_name; + $tag->save(); + + return rest::success(); + } + + static function delete($request) { + if (empty($request->arguments[0])) { + return rest::invalid_request(); + } + $tags = explode(",", $request->arguments[0]); + if (!empty($request->path)) { + $tag_list = ORM::factory("tag") + ->join("items_tags", "tags.id", "items_tags.tag_id") + ->join("items", "items.id", "items_tags.item_id") + ->in("tags.name", $tags) + ->where("relative_url_cache", $request->path) + ->viewable() + ->find_all(); + } else { + $tag_list = ORM::factory("tag") + ->in("name", $tags) + ->find_all(); + } + + foreach ($tag_list as $row) { + $row->delete(); + }; + + tag::compact(); + return rest::success(); + } + private static function _get_items($request) { - $tags = $request->arguments; + $tags = explode(",", $request->arguments[0]); $items = ORM::factory("item") - ->join("items_tags", "items.id", "items_tags.item_id", "left") - ->join("tags", "tags.id", "items_tags.tag_id", "left") - ->where("tags.name", array_shift($tags)); + ->select("distinct *") + ->join("items_tags", "items.id", "items_tags.item_id") + ->join("tags", "tags.id", "items_tags.tag_id") + ->in("tags.name", $tags); if (!empty($request->limit)) { $items->limit($request->limit); } if (!empty($request->offset)) { - $tags->offset($request->offset); - } - foreach ($tags as $tag) { - $items->orWhere("tags.name", $tag); + $items->offset($request->offset); } $resources = array(); foreach ($items->find_all() as $item) { diff --git a/modules/tag/tests/Tag_Rest_Helper_Test.php b/modules/tag/tests/Tag_Rest_Helper_Test.php new file mode 100644 index 00000000..1c550366 --- /dev/null +++ b/modules/tag/tests/Tag_Rest_Helper_Test.php @@ -0,0 +1,227 @@ +_save = array($_GET, $_POST, $_SERVER, $_FILES); + $this->_saved_active_user = identity::active_user(); + + $this->_user = identity::create_user("access_test", "Access Test", "password"); + $key = ORM::factory("user_access_token"); + $this->_access_key = $key->access_key = md5($this->_user->name . rand()); + $key->user_id = $this->_user->id; + $key->save(); + + $root = ORM::factory("item", 1); + $this->_album = album::create($root, "album", "Test Album", rand()); + tag::add($this->_album, "albums"); + tag::add($this->_album, "A1"); + tag::add($this->_album, "T1"); + $this->_child = album::create($this->_album, "child", "Test Child Album", rand()); + tag::add($this->_child, "albums"); + tag::add($this->_child, "C1"); + tag::add($this->_child, "T1"); + + $filename = MODPATH . "gallery/tests/test.jpg"; + $rand = rand(); + $this->_photo = photo::create($this->_child, $filename, "$rand.jpg", $rand); + tag::add($this->_photo, "photos"); + tag::add($this->_photo, "P1"); + tag::add($this->_photo, "T1"); + + $filename = MODPATH . "gallery/tests/test.jpg"; + $rand = rand(); + $this->_sibling = photo::create($this->_album, $filename, "$rand.jpg", $rand); + tag::add($this->_sibling, "photos"); + tag::add($this->_sibling, "P3"); + } + + public function teardown() { + list($_GET, $_POST, $_SERVER, $_FILES) = $this->_save; + identity::set_active_user($this->_saved_active_user); + + try { + if (!empty($this->_user)) { + $this->_user->delete(); + } + if (!empty($this->_album)) { + $this->_album->delete(); + } + Database::instance()->query("TRUNCATE {tags}"); + Database::instance()->query("TRUNCATE {items_tags}"); + + } catch (Exception $e) { } + } + + public function tag_rest_get_all_test() { + $request = (object)array("arguments" => array(), "limit" => 2, "offset" => 1); + + $this->assert_equal( + json_encode(array("status" => "OK", + "tags" => array(array("name" => "albums", "count" => 2), + array("name" => "photos", "count" => 2)))), + tag_rest::get($request)); + } + + public function tag_rest_get_tags_for_item_test() { + $request = (object)array("arguments" => explode("/", $this->_photo->relative_url())); + + $this->assert_equal( + json_encode(array("status" => "OK", + "tags" => array("photos", "P1", "T1"))), + tag_rest::get($request)); + } + + public function tag_rest_get_items_test() { + $request = (object)array("arguments" => array("albums")); + + $resources = array(); + foreach (array($this->_album, $this->_child) as $resource) { + $resources[] = array("type" => $resource->type, + "has_children" => $resource->children_count() > 0, + "path" => $resource->relative_url(), + "thumb_url" => $resource->thumb_url(), + "thumb_dimensions" => array( + "width" => $resource->thumb_width, + "height" => $resource->thumb_height), + "has_thumb" => $resource->has_thumb(), + "title" => $resource->title); + + } + $this->assert_equal(json_encode(array("status" => "OK", "resources" => $resources)), + tag_rest::get($request)); + } + + public function tag_rest_add_tags_for_item_no_path_test() { + $request = (object)array("arguments" => array("new,one")); + + $this->assert_equal( + json_encode(array("status" => "ERROR", "message" => "Invalid request")), + tag_rest::post($request)); + } + + public function tag_rest_add_tags_for_item_not_found_test() { + $request = (object)array("path" => $this->_photo->relative_url() . "b", + "arguments" => array("new,one")); + $this->assert_equal( + json_encode(array("status" => "ERROR", "message" => "Resource not found")), + tag_rest::post($request)); + } + + public function tag_rest_add_tags_for_item_no_access_test() { + identity::set_active_user($this->_user); + $request = (object)array("path" => $this->_photo->relative_url(), + "arguments" => array("new,one")); + + $this->assert_equal( + json_encode(array("status" => "ERROR", "message" => "Resource not found")), + tag_rest::post($request)); + } + + public function tag_rest_add_tags_for_item_test() { + access::allow(identity::registered_users(), "edit", $this->_child); + identity::set_active_user($this->_user); + $request = (object)array("path" => $this->_photo->relative_url(), + "arguments" => array("new,one")); + + $this->assert_equal( + json_encode(array("status" => "OK")), + tag_rest::post($request)); + $request = (object)array("arguments" => explode("/", $this->_photo->relative_url())); + $this->assert_equal( + json_encode(array("status" => "OK", + "tags" => array("photos", "P1", "T1", "new", "one"))), + tag_rest::get($request)); + } + + public function tag_rest_update_tag_no_arguments_test() { + $request = (object)array("arguments" => array()); + + $this->assert_equal( + json_encode(array("status" => "ERROR", "message" => "Invalid request")), + tag_rest::put($request)); + } + + public function tag_rest_update_tag_one_arguments_test() { + $request = (object)array("arguments" => array("photos")); + + $this->assert_equal( + json_encode(array("status" => "ERROR", "message" => "Invalid request")), + tag_rest::put($request)); + + $request = (object)array("arguments" => array(), "new_name" => "valid"); + + $this->assert_equal( + json_encode(array("status" => "ERROR", "message" => "Invalid request")), + tag_rest::put($request)); + } + + public function tag_rest_update_tags_not_found_test() { + $request = (object)array("arguments" => array("not"), "new_name" => "found"); + + $this->assert_equal( + json_encode(array("status" => "ERROR", "message" => "Resource not found")), + tag_rest::put($request)); + } + + public function tag_rest_update_tags_test() { + $request = (object)array("arguments" => array("albums"), "new_name" => "new name"); + + $this->assert_equal(json_encode(array("status" => "OK")), tag_rest::put($request)); + + $request = (object)array("arguments" => array("new name")); + $resources = array(); + foreach (array($this->_album, $this->_child) as $resource) { + $resources[] = array("type" => $resource->type, + "has_children" => $resource->children_count() > 0, + "path" => $resource->relative_url(), + "thumb_url" => $resource->thumb_url(), + "thumb_dimensions" => array( + "width" => $resource->thumb_width, + "height" => $resource->thumb_height), + "has_thumb" => $resource->has_thumb(), + "title" => $resource->title); + + } + $this->assert_equal( + json_encode(array("status" => "OK", "resources" => $resources)), + tag_rest::get($request)); + } + + public function tag_rest_delete_tag_test() { + $request = (object)array("arguments" => array("T1,P1")); + + $this->assert_equal(json_encode(array("status" => "OK")), tag_rest::delete($request)); + + $request = (object)array("arguments" => array("T1,P1")); + $this->assert_equal(json_encode(array("status" => "OK", "resources" => array())), + tag_rest::get($request)); + } + + public function tag_rest_delete_tag_from_item_test() { + $request = (object)array("arguments" => array("T1,P1"), + $this->_photo->relative_url()); + + $this->assert_equal(json_encode(array("status" => "OK")), tag_rest::delete($request)); + + $request = (object)array("arguments" => explode("/", $this->_photo->relative_url())); + $this->assert_equal(json_encode(array("status" => "OK", "tags" => array("photos"))), + tag_rest::get($request)); + } +} -- cgit v1.2.3 From bccb6fc02146fb07cd1b472a90092e78e2259e91 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Sun, 27 Dec 2009 08:32:12 -0800 Subject: Clean up validation the check for duplicate names or slugs, finish converting the rest API to Kohana 2.4 --- modules/gallery/helpers/gallery_rest.php | 93 ++++++++++++++-------- modules/gallery/helpers/item.php | 8 +- modules/gallery/tests/Gallery_Rest_Helper_Test.php | 8 +- modules/image_block/helpers/image_block_rest.php | 12 +-- modules/rest/controllers/rest.php | 27 ++++--- modules/rest/helpers/rest.php | 2 +- modules/rest/tests/Rest_Controller_Test.php | 2 +- modules/tag/helpers/tag_rest.php | 62 ++++++++------- modules/tag/models/tag.php | 4 +- modules/tag/tests/Tag_Rest_Helper_Test.php | 4 +- 10 files changed, 129 insertions(+), 93 deletions(-) (limited to 'modules/tag/helpers') diff --git a/modules/gallery/helpers/gallery_rest.php b/modules/gallery/helpers/gallery_rest.php index 227a6f02..1d790d8c 100644 --- a/modules/gallery/helpers/gallery_rest.php +++ b/modules/gallery/helpers/gallery_rest.php @@ -22,11 +22,11 @@ class gallery_rest_Core { $path = implode("/", $request->arguments); $item = ORM::factory("item") - ->where("relative_url_cache", $path) + ->where("relative_url_cache", "=", $path) ->viewable() ->find(); - if (!$item->loaded) { + if (!$item->loaded()) { return rest::not_found("Resource: {$path} missing."); } @@ -62,11 +62,11 @@ class gallery_rest_Core { $path = implode("/", $request->arguments); $item = ORM::factory("item") - ->where("relative_url_cache", $path) + ->where("relative_url_cache", "=", $path) ->viewable() ->find(); - if (!$item->loaded) { + if (!$item->loaded()) { return rest::not_found("Resource: {$path} missing."); } @@ -75,12 +75,13 @@ class gallery_rest_Core { } // Validate the request data - $new_values = gallery_rest::_validate($request, $item); + $new_values = gallery_rest::_validate($request, $item->parent_id, $item->id); $errors = $new_values->errors(); if (empty($errors)) { item::update($item, $new_values->as_array()); - log::success("content", "Updated $item->type", "type}s/$item->id\">view"); + log::success("content", "Updated $item->type", + "type}s/$item->id\">view"); return rest::success(); } else { @@ -94,15 +95,15 @@ class gallery_rest_Core { } $path = implode("/", $request->arguments); - $components = explode("/", $path); + $components = $request->arguments; $name = urldecode(array_pop($components)); $parent = ORM::factory("item") - ->where("relative_url_cache", implode("/", $components)) + ->where("relative_url_cache", "=", implode("/", $components)) ->viewable() ->find(); - if (!$parent->loaded) { + if (!$parent->loaded()) { return rest::not_found("Resource: {$path} missing."); } @@ -111,7 +112,7 @@ class gallery_rest_Core { } // Validate the request data - $new_values = gallery_rest::_validate($request); + $new_values = gallery_rest::_validate($request, $parent->id); $errors = $new_values->errors(); if (!empty($errors)) { return rest::validation_error($errors); @@ -121,10 +122,10 @@ class gallery_rest_Core { $new_item = album::create( $parent, $name, - empty($request->title) ? $name : $request->title, - empty($request->description) ? null : $request->description, + empty($new_values["title"]) ? $name : $new_values["title"], + empty($new_values["description"]) ? null : $new_values["description"], identity::active_user()->id, - empty($request->slug) ? $name : $request->slug); + empty($new_values["slug"]) ? $name : $new_values["slug"]); $log_message = t("Added an album"); } else { $temp_filename = upload::save("image"); @@ -153,11 +154,11 @@ class gallery_rest_Core { $path = implode("/", $request->arguments); $item = ORM::factory("item") - ->where("relative_url_cache", $path) + ->where("relative_url_cache", "=", $path) ->viewable() ->find(); - if (!$item->loaded) { + if (!$item->loaded()) { return rest::success(); } @@ -193,7 +194,7 @@ class gallery_rest_Core { "path" => $child->relative_url(), "thumb_url" => $child->thumb_url(true), "thumb_dimensions" => array("width" => $child->thumb_width, - "height" => $child->thumb_height), + "height" => $child->thumb_height), "has_thumb" => $child->has_thumb(), "title" => $child->title); } @@ -201,38 +202,40 @@ class gallery_rest_Core { return $children; } - private static function _validate($request, $item=null) { + private static function _validate($request, $parent_id, $item_id=0) { $new_values = array(); - $fields = array("title", "description", "name", "slug", "image"); - if (empty($item)) { - $item = ORM::factory("item"); - $item->id = 0; - } - if ($item->id == 1) { + $fields = array("name" => "length[0,255]", + "title" => "required|length[0,255]", + "description" => "length[0,65535]", + "slug" => "required|length[0,255]"); + if ($item_id == 1) { unset($request["name"]); unset($request["slug"]); } - foreach ($fields as $field) { + foreach (array_keys($fields) as $field) { if (isset($request->$field)) { $new_values[$field] = $request->$field; } else if (isset($item->$field)) { $new_values[$field] = $item->$field; } } - - $new_values = new Validation($new_values); - foreach ($item->rules as $field => $rules) { - foreach (explode("|", $rules) as $rule) { - $new_values->add_rules($field, $rule); - } + if (!empty($request->image)) { + $new_values["image"] = $request->image; } + + $new_values = Validation::factory($new_values) + ->add_rules("name", "length[0,255]") + ->add_rules("title", "length[0,255]") + ->add_rules("description", "length[0,65535]") + ->add_rules("slug", "length[0,255]"); if (isset($new_values["image"])) { $new_values->add_rules( "image", "upload::valid", "upload::required", "upload::type[gif,jpg,jpeg,png,flv,mp4]"); } - if ($new_values->validate() && $item->id != 1) { - $errors = item::check_for_conflicts($item, $new_values["name"], $new_values["slug"]); + if ($new_values->validate() && $item_id != 1) { + $errors = gallery_rest::_check_for_conflicts($parent_id, $item_id, + $new_values["name"], $new_values["slug"]); if (!empty($errors)) { !empty($errors["name_conflict"]) OR $new_values->add_error("name", "Duplicate Name"); !empty($errors["slug_conflict"]) OR @@ -242,4 +245,30 @@ class gallery_rest_Core { return $new_values; } + + private static function _check_for_conflicts($parent_id, $item_id, $new_name, $new_slug) { + $errors = array(); + + if ($row = db::build() + ->select(array("name", "slug")) + ->from("items") + ->where("parent_id", "=", $parent_id) + ->where("id", "<>", $item_id) + ->and_open() + ->where("name", "=", $new_name) + ->or_where("slug", "=", $new_slug) + ->close() + ->execute() + ->current()) { + if ($row->name == $new_name) { + $errors["name_conflict"] = 1; + } + if ($row->slug == $new_slug) { + $errors["slug_conflict"] = 1; + } + } + + return $errors; + } + } diff --git a/modules/gallery/helpers/item.php b/modules/gallery/helpers/item.php index c620ba95..fc390e70 100644 --- a/modules/gallery/helpers/item.php +++ b/modules/gallery/helpers/item.php @@ -122,11 +122,11 @@ class item_Core { if ($row = db::build() ->select(array("name", "slug")) ->from("items") - ->where("parent_id", $item->parent_id) - ->where("id <>", $item->id) + ->where("parent_id", "=", $item->parent_id) + ->where("id", "<>", $item->id) ->and_open() - ->where("name", $new_name) - ->orwhere("slug", $new_slug) + ->where("name", "=", $new_name) + ->or_where("slug", "=", $new_slug) ->close() ->execute() ->current()) { diff --git a/modules/gallery/tests/Gallery_Rest_Helper_Test.php b/modules/gallery/tests/Gallery_Rest_Helper_Test.php index 14c73248..f36f6aaf 100644 --- a/modules/gallery/tests/Gallery_Rest_Helper_Test.php +++ b/modules/gallery/tests/Gallery_Rest_Helper_Test.php @@ -208,7 +208,7 @@ class Gallery_Rest_Helper_Test extends Unit_Test_Case { "parent_path" => $this->_album->relative_url()))), gallery_rest::delete($request)); $this->_child->reload(); - $this->assert_false($this->_child->loaded); + $this->assert_false($this->_child->loaded()); } public function gallery_rest_delete_photo_test() { @@ -222,7 +222,7 @@ class Gallery_Rest_Helper_Test extends Unit_Test_Case { "parent_path" => $this->_album->relative_url()))), gallery_rest::delete($request)); $this->_sibling->reload(); - $this->assert_false($this->_sibling->loaded); + $this->assert_false($this->_sibling->loaded()); } public function gallery_rest_post_album_test() { @@ -235,9 +235,9 @@ class Gallery_Rest_Helper_Test extends Unit_Test_Case { $this->assert_equal(json_encode(array("status" => "OK", "path" => $new_path)), gallery_rest::post($request)); $album = ORM::factory("item") - ->where("relative_url_cache", $new_path) + ->where("relative_url_cache", "=", $new_path) ->find(); - $this->assert_true($album->loaded); + $this->assert_true($album->loaded()); $this->assert_equal("new child", $album->slug); } } diff --git a/modules/image_block/helpers/image_block_rest.php b/modules/image_block/helpers/image_block_rest.php index 45f849b1..7afd974c 100644 --- a/modules/image_block/helpers/image_block_rest.php +++ b/modules/image_block/helpers/image_block_rest.php @@ -26,18 +26,18 @@ class image_block_rest_Core { $items = ORM::factory("item") ->viewable() - ->where("type !=", "album") - ->where("rand_key < ", $random) - ->orderby(array("rand_key" => "DESC")) + ->where("type", "!=", "album") + ->where("rand_key", "<", $random) + ->order_by(array("rand_key" => "DESC")) ->find_all(1); if ($items->count() == 0) { // Try once more. If this fails, just ditch the block altogether $items = ORM::factory("item") ->viewable() - ->where("type !=", "album") - ->where("rand_key >= ", $random) - ->orderby(array("rand_key" => "DESC")) + ->where("type", "!=", "album") + ->where("rand_key", ">= ", $random) + ->order_by(array("rand_key" => "DESC")) ->find_all(1); } break; diff --git a/modules/rest/controllers/rest.php b/modules/rest/controllers/rest.php index 1289d62b..6715bc15 100644 --- a/modules/rest/controllers/rest.php +++ b/modules/rest/controllers/rest.php @@ -18,7 +18,7 @@ */ class Rest_Controller extends Controller { public function access_key() { - $request = (object)$this->input->get(); + $request = (object)Input::instance()->get(); if (empty($request->user) || empty($request->password)) { print rest::forbidden("No user or password supplied"); return; @@ -36,13 +36,13 @@ class Rest_Controller extends Controller { } $key = ORM::factory("user_access_token") - ->where("user_id", $user->id) + ->where("user_id", "=", $user->id) ->find(); - if (!$key->loaded) { + if (!$key->loaded()) { $key->user_id = $user->id; $key->access_key = md5($user->name . rand()); $key->save(); - Kohana::log("alert", Kohana::debug($key->as_array())); + Kohana_Log::add("alert", Kohana::debug($key->as_array())); } print rest::success(array("token" => $key->access_key)); } @@ -67,22 +67,23 @@ class Rest_Controller extends Controller { } private function _normalize_request($args=array()) { - $method = strtolower($this->input->server("REQUEST_METHOD")); + $input = Input::instance(); + $method = strtolower($input->server("REQUEST_METHOD")); $request = new stdClass(); - foreach (array_keys($this->input->get()) as $key) { - $request->$key = $this->input->get($key); + foreach (array_keys($input->get()) as $key) { + $request->$key = $input->get($key); } if ($method != "get") { - foreach (array_keys($this->input->post()) as $key) { - $request->$key = $this->input->post($key); + foreach (array_keys($input->post()) as $key) { + $request->$key = $input->post($key); } foreach (array_keys($_FILES) as $key) { $request->$key = $_FILES[$key]; } } - $request->method = strtolower($this->input->server("HTTP_X_GALLERY_REQUEST_METHOD", $method)); - $request->access_token = $this->input->server("HTTP_X_GALLERY_REQUEST_KEY"); + $request->method = strtolower($input->server("HTTP_X_GALLERY_REQUEST_METHOD", $method)); + $request->access_token = $input->server("HTTP_X_GALLERY_REQUEST_KEY"); $request->arguments = $args; // Let the rest handler figure out what the arguments mean return $request; @@ -93,10 +94,10 @@ class Rest_Controller extends Controller { $user = identity::guest(); } else { $key = ORM::factory("user_access_token") - ->where("access_key", $access_token) + ->where("access_key", "=", $access_token) ->find(); - if ($key->loaded) { + if ($key->loaded()) { $user = identity::lookup_user($key->user_id); if (empty($user)) { print rest::forbidden("User not found: {$key->user_id}"); diff --git a/modules/rest/helpers/rest.php b/modules/rest/helpers/rest.php index ad6ca7c7..276ff0c2 100644 --- a/modules/rest/helpers/rest.php +++ b/modules/rest/helpers/rest.php @@ -88,7 +88,7 @@ class rest_Core { private static function _format_failure_response($message, $log_message) { if (!empty($log_message)) { - Kohana::log("info", $log_message); + Kohana_Log::add("info", $log_message); } // We don't need to save the session for this request Session::abort_save(); diff --git a/modules/rest/tests/Rest_Controller_Test.php b/modules/rest/tests/Rest_Controller_Test.php index b7fbd5a3..6bebc47d 100644 --- a/modules/rest/tests/Rest_Controller_Test.php +++ b/modules/rest/tests/Rest_Controller_Test.php @@ -173,7 +173,7 @@ class rest_rest { static function get($request) { self::$request = $request; $item = ORM::factory("item") - ->where("relative_url_cache", implode("/", $request->arguments)) + ->where("relative_url_cache", "=", implode("/", $request->arguments)) ->find(); $response["path"] = $item->relative_url(); $response["title"] = $item->title; diff --git a/modules/tag/helpers/tag_rest.php b/modules/tag/helpers/tag_rest.php index d62c0231..cca9a88b 100644 --- a/modules/tag/helpers/tag_rest.php +++ b/modules/tag/helpers/tag_rest.php @@ -18,40 +18,44 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class tag_rest_Core { + // If no arguments just return all the tags. If 2 or more then it is a path then + // return the tags for that item. But if its only 1, then is it a path or a tag? + // Assume a tag first, if nothing is found then try finding the item. static function get($request) { - if (empty($request->arguments)) { + $resources = array(); + switch (count($request->arguments)) { + case 0: $tags = ORM::factory("tag") ->select("name", "count") - ->orderby("count", "DESC"); + ->order_by("count", "DESC"); if (!empty($request->limit)) { $tags->limit($request->limit); } if (!empty($request->offset)) { $tags->offset($request->offset); } - $response = array("tags" => array()); + $resources = array("tags" => array()); foreach ($tags->find_all() as $row) { - $response["tags"][] = array("name" => $row->name, "count" => $row->count); + $resources["tags"][] = array("name" => $row->name, "count" => $row->count); } - } else { - $path = implode("/", $request->arguments); - if (strpos($path, ",") === false) { - $item = ORM::factory("item") - ->where("relative_url_cache", $path) - ->viewable() - ->find(); - // If we didn't find it and there was only one argument, retry as a tag not a path - if ($item->loaded || count($request->arguments) != 1) { - $response = array("tags" => $item->loaded ? tag::item_tags($item) : array()); - } else { - $response = array("resources" => tag_rest::_get_items($request)); - } - } else { - $response = array("resources" => tag_rest::_get_items($request)); + break; + case 1: + $resources = tag_rest::_get_items($request); + if (!empty($resources)) { + $resources = array("resources" =>$resources); + break; + } + default: + $item = ORM::factory("item") + ->where("relative_url_cache", "=", implode("/", $request->arguments)) + ->viewable() + ->find(); + if ($item->loaded()) { + $resources = array("tags" => tag::item_tags($item)); } } - return rest::success($response); + return rest::success($resources); } static function post($request) { @@ -62,10 +66,10 @@ class tag_rest_Core { $tags = explode(",", $request->arguments[0]); $item = ORM::factory("item") - ->where("relative_url_cache", $path) + ->where("relative_url_cache", "=", $path) ->viewable() ->find(); - if (!$item->loaded) { + if (!$item->loaded()) { return rest::not_found("Resource: {$path} missing."); } @@ -87,9 +91,9 @@ class tag_rest_Core { $name = $request->arguments[0]; $tag = ORM::factory("tag") - ->where("name", $name) + ->where("name", "=", $name) ->find(); - if (!$tag->loaded) { + if (!$tag->loaded()) { return rest::not_found("Tag: {$name} not found."); } @@ -108,13 +112,13 @@ class tag_rest_Core { $tag_list = ORM::factory("tag") ->join("items_tags", "tags.id", "items_tags.tag_id") ->join("items", "items.id", "items_tags.item_id") - ->in("tags.name", $tags) - ->where("relative_url_cache", $request->path) + ->where("tags.name", "IN", $tags) + ->where("relative_url_cache", "=", $request->path) ->viewable() ->find_all(); } else { $tag_list = ORM::factory("tag") - ->in("name", $tags) + ->where("name", "IN", $tags) ->find_all(); } @@ -129,10 +133,10 @@ class tag_rest_Core { private static function _get_items($request) { $tags = explode(",", $request->arguments[0]); $items = ORM::factory("item") - ->select("distinct *") + ->select_distinct("*") ->join("items_tags", "items.id", "items_tags.item_id") ->join("tags", "tags.id", "items_tags.tag_id") - ->in("tags.name", $tags); + ->where("tags.name", "IN", $tags); if (!empty($request->limit)) { $items->limit($request->limit); } diff --git a/modules/tag/models/tag.php b/modules/tag/models/tag.php index b2ce9eda..d0d2117c 100644 --- a/modules/tag/models/tag.php +++ b/modules/tag/models/tag.php @@ -108,7 +108,9 @@ class Tag_Model extends ORM { $result = parent::delete(); if ($related_item_ids) { - foreach (ORM::factory("item")->in("id", array_keys($related_item_ids))->find_all() as $item) { + foreach (ORM::factory("item") + ->where("id", "IN", array_keys($related_item_ids)) + ->find_all() as $item) { module::event("item_related_update", $item); } } diff --git a/modules/tag/tests/Tag_Rest_Helper_Test.php b/modules/tag/tests/Tag_Rest_Helper_Test.php index 1c550366..6b1c9a33 100644 --- a/modules/tag/tests/Tag_Rest_Helper_Test.php +++ b/modules/tag/tests/Tag_Rest_Helper_Test.php @@ -210,11 +210,11 @@ class Tag_Rest_Helper_Test extends Unit_Test_Case { $this->assert_equal(json_encode(array("status" => "OK")), tag_rest::delete($request)); $request = (object)array("arguments" => array("T1,P1")); - $this->assert_equal(json_encode(array("status" => "OK", "resources" => array())), + $this->assert_equal(json_encode(array("status" => "OK")), tag_rest::get($request)); } - public function tag_rest_delete_tag_from_item_test() { + public function tag_rest_delete_tagc_from_item_test() { $request = (object)array("arguments" => array("T1,P1"), $this->_photo->relative_url()); -- cgit v1.2.3 From 5d6e2642119d64590bcafcb18eed19353e12666b Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Wed, 30 Dec 2009 12:07:24 -0800 Subject: whitespace. --- modules/tag/helpers/tag_rest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'modules/tag/helpers') diff --git a/modules/tag/helpers/tag_rest.php b/modules/tag/helpers/tag_rest.php index cca9a88b..ed6cfc1c 100644 --- a/modules/tag/helpers/tag_rest.php +++ b/modules/tag/helpers/tag_rest.php @@ -42,7 +42,7 @@ class tag_rest_Core { case 1: $resources = tag_rest::_get_items($request); if (!empty($resources)) { - $resources = array("resources" =>$resources); + $resources = array("resources" => $resources); break; } default: -- cgit v1.2.3 From 11792a12bb2002a434217efabe232022dd253b67 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Wed, 30 Dec 2009 17:08:01 -0800 Subject: 1) Remove the rest::not_found method and replace it with "throw new Kohana_404_Exception 2) Don't use the input path to lookup the item via relative_path_cache. Instead use url::get_item_from_uri method. --- modules/gallery/helpers/gallery_rest.php | 72 ++++++++-------------- modules/gallery/tests/Gallery_Rest_Helper_Test.php | 16 +++-- modules/rest/helpers/rest.php | 9 +-- modules/tag/helpers/tag_rest.php | 6 +- modules/tag/tests/Tag_Rest_Helper_Test.php | 27 +++++--- 5 files changed, 59 insertions(+), 71 deletions(-) (limited to 'modules/tag/helpers') diff --git a/modules/gallery/helpers/gallery_rest.php b/modules/gallery/helpers/gallery_rest.php index 1d790d8c..94c7dc6f 100644 --- a/modules/gallery/helpers/gallery_rest.php +++ b/modules/gallery/helpers/gallery_rest.php @@ -21,14 +21,7 @@ class gallery_rest_Core { static function get($request) { $path = implode("/", $request->arguments); - $item = ORM::factory("item") - ->where("relative_url_cache", "=", $path) - ->viewable() - ->find(); - - if (!$item->loaded()) { - return rest::not_found("Resource: {$path} missing."); - } + $item = gallery_rest::_get_item($path); $parent = $item->parent(); $response_data = array("type" => $item->type, @@ -60,25 +53,19 @@ class gallery_rest_Core { return rest::invalid_request(); } $path = implode("/", $request->arguments); - - $item = ORM::factory("item") - ->where("relative_url_cache", "=", $path) - ->viewable() - ->find(); - - if (!$item->loaded()) { - return rest::not_found("Resource: {$path} missing."); - } - - if (!access::can("edit", $item)) { - return rest::not_found("Resource: {$path} permission denied."); - } + $item = gallery_rest::_get_item($path, "edit"); // Validate the request data $new_values = gallery_rest::_validate($request, $item->parent_id, $item->id); $errors = $new_values->errors(); if (empty($errors)) { - item::update($item, $new_values->as_array()); + $item->title = $new_values->title; + $item->description = $new_values->description; + if ($item->id != 1) { + $item->rename($new_values->name); + } + $item->slug = $new_values->slug; + $item->save(); log::success("content", "Updated $item->type", "type}s/$item->id\">view"); @@ -93,23 +80,11 @@ class gallery_rest_Core { if (empty($request->arguments)) { return rest::invalid_request(); } - $path = implode("/", $request->arguments); $components = $request->arguments; $name = urldecode(array_pop($components)); - $parent = ORM::factory("item") - ->where("relative_url_cache", "=", implode("/", $components)) - ->viewable() - ->find(); - - if (!$parent->loaded()) { - return rest::not_found("Resource: {$path} missing."); - } - - if (!access::can("edit", $parent)) { - return rest::not_found("Resource: {$path} permission denied."); - } + $parent = gallery_rest::_get_item(implode("/", $components), "edit"); // Validate the request data $new_values = gallery_rest::_validate($request, $parent->id); @@ -153,18 +128,7 @@ class gallery_rest_Core { } $path = implode("/", $request->arguments); - $item = ORM::factory("item") - ->where("relative_url_cache", "=", $path) - ->viewable() - ->find(); - - if (!$item->loaded()) { - return rest::success(); - } - - if (!access::can("edit", $item)) { - return rest::not_found("Resource: {$path} permission denied."); - } + $item = gallery_rest::_get_item($path, "edit"); if ($item->id == 1) { return rest::invalid_request("Attempt to delete the root album"); @@ -183,6 +147,20 @@ class gallery_rest_Core { return rest::success(array("resource" => array("parent_path" => $parent->relative_url()))); } + private static function _get_item($path, $permission="view") { + $item = url::get_item_from_uri($path); + + if (!$item->loaded()) { + throw new Kohana_404_Exception(); + } + + if (!access::can($permission, $item)) { + throw new Kohana_404_Exception(); + } + + return $item; + } + private static function _get_children($item, $request) { $children = array(); $limit = empty($request->limit) ? null : $request->limit; diff --git a/modules/gallery/tests/Gallery_Rest_Helper_Test.php b/modules/gallery/tests/Gallery_Rest_Helper_Test.php index f36f6aaf..fba83d47 100644 --- a/modules/gallery/tests/Gallery_Rest_Helper_Test.php +++ b/modules/gallery/tests/Gallery_Rest_Helper_Test.php @@ -136,8 +136,12 @@ class Gallery_Rest_Helper_Test extends Unit_Test_Case { "title" => "Updated Title", "name" => "new name"); - $this->assert_equal(json_encode(array("status" => "ERROR", "message" => "Resource not found")), - gallery_rest::put($request)); + try { + gallery_rest::put($request); + } catch (Kohana_404_Exception $k404) { + } catch (Exception $e) { + $this->assert_false(true, $e->__toString()); + } } public function gallery_rest_put_album_no_edit_permission_test() { @@ -147,8 +151,12 @@ class Gallery_Rest_Helper_Test extends Unit_Test_Case { "title" => "Updated Title", "name" => "new name"); - $this->assert_equal(json_encode(array("status" => "ERROR", "message" => "Resource not found")), - gallery_rest::put($request)); + try { + gallery_rest::put($request); + } catch (Kohana_404_Exception $k404) { + } catch (Exception $e) { + $this->assert_false(true, $e->__toString()); + } } public function gallery_rest_put_album_rename_conflict_test() { diff --git a/modules/rest/helpers/rest.php b/modules/rest/helpers/rest.php index 276ff0c2..4b3166c0 100644 --- a/modules/rest/helpers/rest.php +++ b/modules/rest/helpers/rest.php @@ -46,14 +46,7 @@ class rest_Core { } /** - * Resource Not Found - */ - static function not_found($log_message=null) { - return self::_format_failure_response(t("Resource not found"), $log_message); - } - - /** - * Resource Not Found + * Request failed */ static function fail($log_message=null) { return self::_format_failure_response($log_message, $log_message); diff --git a/modules/tag/helpers/tag_rest.php b/modules/tag/helpers/tag_rest.php index ed6cfc1c..cfcf93b2 100644 --- a/modules/tag/helpers/tag_rest.php +++ b/modules/tag/helpers/tag_rest.php @@ -70,11 +70,11 @@ class tag_rest_Core { ->viewable() ->find(); if (!$item->loaded()) { - return rest::not_found("Resource: {$path} missing."); + throw new Kohana_404_Exception(); } if (!access::can("edit", $item)) { - return rest::not_found("Resource: {$path} permission denied."); + throw new Kohana_404_Exception(); } foreach ($tags as $tag) { @@ -94,7 +94,7 @@ class tag_rest_Core { ->where("name", "=", $name) ->find(); if (!$tag->loaded()) { - return rest::not_found("Tag: {$name} not found."); + throw new Kohana_404_Exception(); } $tag->name = $request->new_name; diff --git a/modules/tag/tests/Tag_Rest_Helper_Test.php b/modules/tag/tests/Tag_Rest_Helper_Test.php index 6b1c9a33..4408bf4b 100644 --- a/modules/tag/tests/Tag_Rest_Helper_Test.php +++ b/modules/tag/tests/Tag_Rest_Helper_Test.php @@ -119,9 +119,12 @@ class Tag_Rest_Helper_Test extends Unit_Test_Case { public function tag_rest_add_tags_for_item_not_found_test() { $request = (object)array("path" => $this->_photo->relative_url() . "b", "arguments" => array("new,one")); - $this->assert_equal( - json_encode(array("status" => "ERROR", "message" => "Resource not found")), - tag_rest::post($request)); + try { + tag_rest::post($request); + } catch (Kohana_404_Exception $k404) { + } catch (Exception $e) { + $this->assert_false(true, $e->__toString()); + } } public function tag_rest_add_tags_for_item_no_access_test() { @@ -129,9 +132,12 @@ class Tag_Rest_Helper_Test extends Unit_Test_Case { $request = (object)array("path" => $this->_photo->relative_url(), "arguments" => array("new,one")); - $this->assert_equal( - json_encode(array("status" => "ERROR", "message" => "Resource not found")), - tag_rest::post($request)); + try { + tag_rest::post($request); + } catch (Kohana_404_Exception $k404) { + } catch (Exception $e) { + $this->assert_false(true, $e->__toString()); + } } public function tag_rest_add_tags_for_item_test() { @@ -175,9 +181,12 @@ class Tag_Rest_Helper_Test extends Unit_Test_Case { public function tag_rest_update_tags_not_found_test() { $request = (object)array("arguments" => array("not"), "new_name" => "found"); - $this->assert_equal( - json_encode(array("status" => "ERROR", "message" => "Resource not found")), - tag_rest::put($request)); + try { + tag_rest::put($request); + } catch (Kohana_404_Exception $k404) { + } catch (Exception $e) { + $this->assert_false(true, $e->__toString()); + } } public function tag_rest_update_tags_test() { -- cgit v1.2.3 From 1a12a5e3c89c41ebd087591c16611fbab4293f5b Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Thu, 31 Dec 2009 11:51:51 -0800 Subject: Create a Rest_Exception class and use it to convey status to the client instead of calling rest::forbidden and other rest helper error messages. --- modules/gallery/helpers/gallery_rest.php | 7 ++-- modules/rest/controllers/rest.php | 23 +++++------ modules/rest/helpers/rest.php | 39 ++++--------------- modules/rest/libraries/Rest_Exception.php | 41 ++++++++++++++++++++ modules/rest/tests/Rest_Controller_Test.php | 60 ++++++++++++++++++++--------- modules/tag/helpers/tag_rest.php | 6 +-- 6 files changed, 109 insertions(+), 67 deletions(-) create mode 100644 modules/rest/libraries/Rest_Exception.php (limited to 'modules/tag/helpers') diff --git a/modules/gallery/helpers/gallery_rest.php b/modules/gallery/helpers/gallery_rest.php index 21e2b939..563a2c7c 100644 --- a/modules/gallery/helpers/gallery_rest.php +++ b/modules/gallery/helpers/gallery_rest.php @@ -50,7 +50,7 @@ class gallery_rest_Core { static function put($request) { if (empty($request->arguments)) { - return rest::invalid_request(); + Rest_Exception::trigger(400, "Bad request"); } $path = implode("/", $request->arguments); $item = gallery_rest::_get_item($path, "edit"); @@ -78,7 +78,7 @@ class gallery_rest_Core { static function post($request) { if (empty($request->arguments)) { - return rest::invalid_request(); + Rest_Exception::trigger(400, "Bad request"); } $components = $request->arguments; @@ -125,6 +125,7 @@ class gallery_rest_Core { static function delete($request) { if (empty($request->arguments)) { + Rest_Exception::trigger(400, "Bad request", $log_message); return rest::invalid_request(); } $path = implode("/", $request->arguments); @@ -132,7 +133,7 @@ class gallery_rest_Core { $item = gallery_rest::_get_item($path, "edit"); if ($item->id == 1) { - return rest::invalid_request("Attempt to delete the root album"); + Rest_Exception::trigger(400, "Bad request", "Attempt to delete the root album"); } $parent = $item->parent(); diff --git a/modules/rest/controllers/rest.php b/modules/rest/controllers/rest.php index 6715bc15..b71e60f5 100644 --- a/modules/rest/controllers/rest.php +++ b/modules/rest/controllers/rest.php @@ -20,18 +20,17 @@ class Rest_Controller extends Controller { public function access_key() { $request = (object)Input::instance()->get(); if (empty($request->user) || empty($request->password)) { - print rest::forbidden("No user or password supplied"); - return; + Rest_Exception::trigger(403, "Forbidden", "No user or password supplied"); } $user = identity::lookup_user_by_name($request->user); if (empty($user)) { - print rest::forbidden("User '{$request->user}' not found"); + Rest_Exception::trigger(403, "Forbidden", "User '{$request->user}' not found"); return; } if (!identity::is_correct_password($user, $request->password)) { - print rest::forbidden("Invalid password for '{$request->user}'."); + Rest_Exception::trigger(403, "Forbidden", "Invalid password for '{$request->user}'."); return; } @@ -55,14 +54,16 @@ class Rest_Controller extends Controller { $handler_method = $request->method; if (!method_exists($handler_class, $handler_method)) { - print rest::not_implemented("$handler_class::$handler_method is not implemented"); - return; + Rest_Exception::trigger(501, "Not implemented", "$handler_class::$handler_method"); } print call_user_func(array($handler_class, $handler_method), $request); } + } catch (Rest_Exception $e) { + $e->sendHeaders(); } catch (Exception $e) { - print rest::internal_error($e->__toString()); + Kohana_Log::add("error", $e->__toString()); + header("HTTP/1.1 500 Internal Error"); } } @@ -100,12 +101,12 @@ class Rest_Controller extends Controller { if ($key->loaded()) { $user = identity::lookup_user($key->user_id); if (empty($user)) { - print rest::forbidden("User not found: {$key->user_id}"); - return false;; + Rest_Exception::trigger(403, "Forbidden", $log_message, + "User not found: {$key->user_id}"); } } else { - print rest::forbidden("Invalid user access token supplied: {$key->user_id}"); - return false; + Rest_Exception::trigger(403, "Forbidden", $log_message, + "Invalid user access token supplied: {$key->user_id}"); } } identity::set_active_user($user); diff --git a/modules/rest/helpers/rest.php b/modules/rest/helpers/rest.php index 4b3166c0..7684567c 100644 --- a/modules/rest/helpers/rest.php +++ b/modules/rest/helpers/rest.php @@ -17,39 +17,23 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class rest_Core { - /** - * Authorization Failure - */ - static function forbidden($log_message=null) { - return self::_format_failure_response(t("Authorization failed"), $log_message); - } - - /** - * Invalid Failure - */ - static function invalid_request($log_message=null) { - return self::_format_failure_response(t("Invalid request"), $log_message); - } - /** * Not Implemented */ static function not_implemented($log_message=null) { - return self::_format_failure_response(t("Service not implemented"), $log_message); - } - - /** - * Internal Error - */ - static function internal_error($log_message=null) { - return self::_format_failure_response(t("Internal error"), $log_message); + Rest_Exception::trigger(501, "Not implemented", $log_message); } /** * Request failed */ static function fail($log_message=null) { - return self::_format_failure_response($log_message, $log_message); + if (!empty($log_message)) { + Kohana_Log::add("info", $log_message); + } + // We don't need to save the session for this request + Session::abort_save(); + return json_encode(array("status" => "ERROR", "message" => (string)$message)); } /** @@ -78,13 +62,4 @@ class rest_Core { Session::abort_save(); return json_encode($response); } - - private static function _format_failure_response($message, $log_message) { - if (!empty($log_message)) { - Kohana_Log::add("info", $log_message); - } - // We don't need to save the session for this request - Session::abort_save(); - return json_encode(array("status" => "ERROR", "message" => (string)$message)); - } } diff --git a/modules/rest/libraries/Rest_Exception.php b/modules/rest/libraries/Rest_Exception.php new file mode 100644 index 00000000..acdcb568 --- /dev/null +++ b/modules/rest/libraries/Rest_Exception.php @@ -0,0 +1,41 @@ +getMessage()}'); + } +} // End Rest Exception \ No newline at end of file diff --git a/modules/rest/tests/Rest_Controller_Test.php b/modules/rest/tests/Rest_Controller_Test.php index 6bebc47d..21b83fe6 100644 --- a/modules/rest/tests/Rest_Controller_Test.php +++ b/modules/rest/tests/Rest_Controller_Test.php @@ -75,26 +75,38 @@ class Rest_Controller_Test extends Unit_Test_Case { public function rest_access_key_no_parameters_test() { $_SERVER["REQUEST_METHOD"] = "GET"; - $this->assert_equal( - json_encode(array("status" => "ERROR", "message" => (string)t("Authorization failed"))), - $this->_call_controller()); + try { + $this->_call_controller(); + } catch (Rest_Exception $e) { + $this->assert_equal("403 Forbidden", $e->getMessage()); + } catch (Exception $e) { + $this->assert_false(true, $e->__toString()); + } } public function rest_access_key_user_not_found_test() { $_SERVER["REQUEST_METHOD"] = "POST"; $_POST["request"] = json_encode(array("user" => "access_test2", "password" => "password")); - $this->assert_equal( - json_encode(array("status" => "ERROR", "message" => (string)t("Authorization failed"))), - $this->_call_controller()); + try { + $this->_call_controller(); + } catch (Rest_Exception $e) { + $this->assert_equal("403 Forbidden", $e->getMessage()); + } catch (Exception $e) { + $this->assert_false(true, $e->__toString()); + } } public function rest_access_key_invalid_password_test() { $_SERVER["REQUEST_METHOD"] = "POST"; - $this->assert_equal( - json_encode(array("status" => "ERROR", "message" => (string)t("Authorization failed"))), - $this->_call_controller()); + try { + $this->_call_controller(); + } catch (Rest_Exception $e) { + $this->assert_equal("403 Forbidden", $e->getMessage()); + } catch (Exception $e) { + $this->assert_false(true, $e->__toString()); + } } public function rest_get_resource_no_request_key_test() { @@ -114,9 +126,13 @@ class Rest_Controller_Test extends Unit_Test_Case { $_SERVER["HTTP_X_GALLERY_REQUEST_KEY"] = md5($this->_access_key); // screw up the access key; $_SERVER["REQUEST_METHOD"] = "GET"; - $this->assert_equal( - json_encode(array("status" => "ERROR", "message" => (string)t("Authorization failed"))), - $this->_call_controller()); + try { + $this->_call_controller(); + } catch (Rest_Exception $e) { + $this->assert_equal("403 Forbidden", $e->getMessage()); + } catch (Exception $e) { + $this->assert_false(true, $e->__toString()); + } } public function rest_get_resource_no_user_for_key_test() { @@ -126,9 +142,13 @@ class Rest_Controller_Test extends Unit_Test_Case { $this->_user->delete(); unset($this->_user); - $this->assert_equal( - json_encode(array("status" => "ERROR", "message" => (string)t("Authorization failed"))), - $this->_call_controller("rest", explode("/", $this->_photo->relative_url()))); + try { + $this->_call_controller("rest", explode("/", $this->_photo->relative_url())); + } catch (Rest_Exception $e) { + $this->assert_equal("403 Forbidden", $e->getMessage()); + } catch (Exception $e) { + $this->assert_false(true, $e->__toString()); + } } public function rest_get_resource_no_handler_test() { @@ -136,9 +156,13 @@ class Rest_Controller_Test extends Unit_Test_Case { $_SERVER["HTTP_X_GALLERY_REQUEST_KEY"] = $this->_access_key; $_SERVER["HTTP_X_GALLERY_REQUEST_METHOD"] = "PUT"; - $this->assert_equal( - json_encode(array("status" => "ERROR", "message" => (string)t("Service not implemented"))), - $this->_call_controller("rest", explode("/", $this->_photo->relative_url()))); + try { + $this->_call_controller("rest", explode("/", $this->_photo->relative_url())); + } catch (Rest_Exception $e) { + $this->assert_equal("501 Not Implemented", $e->getMessage()); + } catch (Exception $e) { + $this->assert_false(true, $e->__toString()); + } } public function rest_get_resource_test() { diff --git a/modules/tag/helpers/tag_rest.php b/modules/tag/helpers/tag_rest.php index cfcf93b2..29b74510 100644 --- a/modules/tag/helpers/tag_rest.php +++ b/modules/tag/helpers/tag_rest.php @@ -60,7 +60,7 @@ class tag_rest_Core { static function post($request) { if (empty($request->arguments) || count($request->arguments) != 1 || empty($request->path)) { - return rest::invalid_request(); + Rest_Exception::trigger(400, "Bad request"); } $path = $request->path; $tags = explode(",", $request->arguments[0]); @@ -85,7 +85,7 @@ class tag_rest_Core { static function put($request) { if (empty($request->arguments[0]) || empty($request->new_name)) { - return rest::invalid_request(); + Rest_Exception::trigger(400, "Bad request"); } $name = $request->arguments[0]; @@ -105,7 +105,7 @@ class tag_rest_Core { static function delete($request) { if (empty($request->arguments[0])) { - return rest::invalid_request(); + Rest_Exception::trigger(400, "Bad request"); } $tags = explode(",", $request->arguments[0]); if (!empty($request->path)) { -- cgit v1.2.3 From 5b9801092b3c347161f9e3b8069e05945a5010d2 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Sat, 2 Jan 2010 16:55:06 -0800 Subject: Remove the Rest_Exception::trigger method. --- modules/gallery/helpers/gallery_rest.php | 9 +++--- modules/rest/controllers/rest.php | 51 +++++++++++++++---------------- modules/rest/helpers/rest.php | 6 ++-- modules/rest/libraries/Rest_Exception.php | 35 +++++++++------------ modules/tag/helpers/tag_rest.php | 6 ++-- 5 files changed, 49 insertions(+), 58 deletions(-) (limited to 'modules/tag/helpers') diff --git a/modules/gallery/helpers/gallery_rest.php b/modules/gallery/helpers/gallery_rest.php index 563a2c7c..a87ebb4e 100644 --- a/modules/gallery/helpers/gallery_rest.php +++ b/modules/gallery/helpers/gallery_rest.php @@ -50,7 +50,7 @@ class gallery_rest_Core { static function put($request) { if (empty($request->arguments)) { - Rest_Exception::trigger(400, "Bad request"); + throw new Rest_Exception(400, "Bad request"); } $path = implode("/", $request->arguments); $item = gallery_rest::_get_item($path, "edit"); @@ -78,7 +78,7 @@ class gallery_rest_Core { static function post($request) { if (empty($request->arguments)) { - Rest_Exception::trigger(400, "Bad request"); + throw new Rest_Exception(400, "Bad request"); } $components = $request->arguments; @@ -125,15 +125,14 @@ class gallery_rest_Core { static function delete($request) { if (empty($request->arguments)) { - Rest_Exception::trigger(400, "Bad request", $log_message); - return rest::invalid_request(); + throw new Rest_Exception(400, "Bad request"); } $path = implode("/", $request->arguments); $item = gallery_rest::_get_item($path, "edit"); if ($item->id == 1) { - Rest_Exception::trigger(400, "Bad request", "Attempt to delete the root album"); + throw new Rest_Exception(400, "Bad request"); } $parent = $item->parent(); diff --git a/modules/rest/controllers/rest.php b/modules/rest/controllers/rest.php index 39ca4797..26e5b31a 100644 --- a/modules/rest/controllers/rest.php +++ b/modules/rest/controllers/rest.php @@ -19,32 +19,34 @@ */ class Rest_Controller extends Controller { public function access_key() { - $request = (object)Input::instance()->get(); - if (empty($request->user) || empty($request->password)) { - Rest_Exception::trigger(403, "Forbidden", "No user or password supplied"); - } + try { + $request = (object)Input::instance()->get(); + if (empty($request->user) || empty($request->password)) { + throw new Rest_Exception(403, "Forbidden"); + } - $user = identity::lookup_user_by_name($request->user); - if (empty($user)) { - Rest_Exception::trigger(403, "Forbidden", "User '{$request->user}' not found"); - return; - } + $user = identity::lookup_user_by_name($request->user); + if (empty($user)) { + throw new Rest_Exception(403, "Forbidden"); + } - if (!identity::is_correct_password($user, $request->password)) { - Rest_Exception::trigger(403, "Forbidden", "Invalid password for '{$request->user}'."); - return; - } + if (!identity::is_correct_password($user, $request->password)) { + throw new Rest_Exception(403, "Forbidden"); + } - $key = ORM::factory("user_access_token") - ->where("user_id", "=", $user->id) - ->find(); - if (!$key->loaded()) { - $key->user_id = $user->id; - $key->access_key = md5($user->name . rand()); - $key->save(); + $key = ORM::factory("user_access_token") + ->where("user_id", "=", $user->id) + ->find(); + if (!$key->loaded()) { + $key->user_id = $user->id; + $key->access_key = md5($user->name . rand()); + $key->save(); + } + print rest::success(array("token" => $key->access_key)); + } catch (Rest_Exception $e) { + $e->sendHeaders(); } - print rest::success(array("token" => $key->access_key)); - } + } public function __call($function, $args) { $request = rest::normalize_request($args); @@ -54,16 +56,13 @@ class Rest_Controller extends Controller { $handler_method = $request->method; if (!method_exists($handler_class, $handler_method)) { - Rest_Exception::trigger(501, "Not implemented", "$handler_class::$handler_method"); + throw new Rest_Exception(403, "Forbidden"); } print call_user_func(array($handler_class, $handler_method), $request); } } catch (Rest_Exception $e) { $e->sendHeaders(); - } catch (Exception $e) { - Kohana_Log::add("error", $e->__toString()); - header("HTTP/1.1 500 Internal Error"); } } } \ No newline at end of file diff --git a/modules/rest/helpers/rest.php b/modules/rest/helpers/rest.php index 00790e6b..be0644f2 100644 --- a/modules/rest/helpers/rest.php +++ b/modules/rest/helpers/rest.php @@ -92,12 +92,10 @@ class rest_Core { if ($key->loaded()) { $user = identity::lookup_user($key->user_id); if (empty($user)) { - Rest_Exception::trigger(403, "Forbidden", $log_message, - "User not found: {$key->user_id}"); + throw new Rest_Exception(403, "Forbidden"); } } else { - Rest_Exception::trigger(403, "Forbidden", $log_message, - "Invalid user access token supplied: {$key->user_id}"); + throw new Rest_Exception(403, "Forbidden"); } } identity::set_active_user($user); diff --git a/modules/rest/libraries/Rest_Exception.php b/modules/rest/libraries/Rest_Exception.php index acdcb568..905b94a0 100644 --- a/modules/rest/libraries/Rest_Exception.php +++ b/modules/rest/libraries/Rest_Exception.php @@ -1,15 +1,22 @@ -arguments) || count($request->arguments) != 1 || empty($request->path)) { - Rest_Exception::trigger(400, "Bad request"); + throw new Rest_Exception(400, "Bad request"); } $path = $request->path; $tags = explode(",", $request->arguments[0]); @@ -85,7 +85,7 @@ class tag_rest_Core { static function put($request) { if (empty($request->arguments[0]) || empty($request->new_name)) { - Rest_Exception::trigger(400, "Bad request"); + throw new Rest_Exception(400, "Bad request"); } $name = $request->arguments[0]; @@ -105,7 +105,7 @@ class tag_rest_Core { static function delete($request) { if (empty($request->arguments[0])) { - Rest_Exception::trigger(400, "Bad request"); + throw new Rest_Exception(400, "Bad request"); } $tags = explode(",", $request->arguments[0]); if (!empty($request->path)) { -- cgit v1.2.3