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/gallery/libraries/ORM_MPTT.php | 56 +++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 22 deletions(-) (limited to 'modules/gallery/libraries/ORM_MPTT.php') diff --git a/modules/gallery/libraries/ORM_MPTT.php b/modules/gallery/libraries/ORM_MPTT.php index 83d2445c..01b2d7b7 100644 --- a/modules/gallery/libraries/ORM_MPTT.php +++ b/modules/gallery/libraries/ORM_MPTT.php @@ -133,10 +133,10 @@ class ORM_MPTT_Core extends ORM { */ function parents() { return $this - ->where("`left_ptr` <= {$this->left_ptr}") - ->where("`right_ptr` >= {$this->right_ptr}") - ->where("id <> {$this->id}") - ->orderby("left_ptr", "ASC") + ->where("left_ptr", "<=", $this->left_ptr) + ->where("right_ptr", ">=", $this->right_ptr) + ->where("id", "<>", $this->id) + ->order_by("left_ptr", "ASC") ->find_all(); } @@ -147,14 +147,17 @@ class ORM_MPTT_Core extends ORM { * @param integer SQL limit * @param integer SQL offset * @param array additional where clauses - * @param array orderby + * @param array order_by * @return array ORM */ - function children($limit=null, $offset=0, $where=array(), $orderby=array("id" => "ASC")) { + function children($limit=null, $offset=0, $where=null, $order_by=array("id" => "ASC")) { + if ($where) { + $this->where($where); + } + return $this - ->where("parent_id", $this->id) - ->where($where) - ->orderby($orderby) + ->where("parent_id", "=", $this->id) + ->order_by($order_by) ->find_all($limit, $offset); } @@ -165,10 +168,13 @@ class ORM_MPTT_Core extends ORM { * @param array additional where clauses * @return array ORM */ - function children_count($where=array()) { + function children_count($where=null) { + if ($where) { + $this->where($where); + } + return $this - ->where($where) - ->where("parent_id", $this->id) + ->where("parent_id", "=", $this->id) ->count_all(); } @@ -178,15 +184,18 @@ class ORM_MPTT_Core extends ORM { * @param integer SQL limit * @param integer SQL offset * @param array additional where clauses - * @param array orderby + * @param array order_by * @return object ORM_Iterator */ - function descendants($limit=null, $offset=0, $where=array(), $orderby=array("id" => "ASC")) { + function descendants($limit=null, $offset=0, $where=null, $order_by=array("id" => "ASC")) { + if ($where) { + $this->where($where); + } + return $this - ->where("left_ptr >", $this->left_ptr) - ->where("right_ptr <=", $this->right_ptr) - ->where($where) - ->orderby($orderby) + ->where("left_ptr", ">", $this->left_ptr) + ->where("right_ptr", "<=", $this->right_ptr) + ->order_by($order_by) ->find_all($limit, $offset); } @@ -196,11 +205,14 @@ class ORM_MPTT_Core extends ORM { * @param array additional where clauses * @return integer child count */ - function descendants_count($where=array()) { + function descendants_count($where=null) { + if ($where) { + $this->where($where); + } + return $this - ->where("left_ptr >", $this->left_ptr) - ->where("right_ptr <=", $this->right_ptr) - ->where($where) + ->where("left_ptr", ">", $this->left_ptr) + ->where("right_ptr", "<=", $this->right_ptr) ->count_all(); } -- cgit v1.2.3 From 54be15191b983e72b4643f3559303b35b7c48795 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Thu, 26 Nov 2009 18:47:40 -0800 Subject: Overload Database_Builder to add merge_where() which takes predefined where clauses and adds them to the existing query. Update all existing queries that take an additional where clause to use it. --- modules/gallery/controllers/movies.php | 2 +- modules/gallery/controllers/photos.php | 2 +- modules/gallery/libraries/ORM_MPTT.php | 8 +++--- modules/gallery/models/item.php | 16 ++++++----- .../libraries/MY_Database_Builder.php | 31 ++++++++++++++++++++++ 5 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 modules/kohana23_compat/libraries/MY_Database_Builder.php (limited to 'modules/gallery/libraries/ORM_MPTT.php') diff --git a/modules/gallery/controllers/movies.php b/modules/gallery/controllers/movies.php index 7ceeefdf..157c388f 100644 --- a/modules/gallery/controllers/movies.php +++ b/modules/gallery/controllers/movies.php @@ -21,7 +21,7 @@ class Movies_Controller extends Items_Controller { public function _show($movie) { access::required("view", $movie); - $where = array("type != " => "album"); + $where = array(array("type", "!=", "album")); $position = $movie->parent()->get_position($movie, $where); if ($position > 1) { list ($previous_item, $ignore, $next_item) = diff --git a/modules/gallery/controllers/photos.php b/modules/gallery/controllers/photos.php index 0d7daac4..478447b1 100644 --- a/modules/gallery/controllers/photos.php +++ b/modules/gallery/controllers/photos.php @@ -21,7 +21,7 @@ class Photos_Controller extends Items_Controller { public function _show($photo) { access::required("view", $photo); - $where = array("type != " => "album"); + $where = array(array("type", "!=", "album")); $position = $photo->parent()->get_position($photo, $where); if ($position > 1) { list ($previous_item, $ignore, $next_item) = diff --git a/modules/gallery/libraries/ORM_MPTT.php b/modules/gallery/libraries/ORM_MPTT.php index 01b2d7b7..90b9d38a 100644 --- a/modules/gallery/libraries/ORM_MPTT.php +++ b/modules/gallery/libraries/ORM_MPTT.php @@ -152,7 +152,7 @@ class ORM_MPTT_Core extends ORM { */ function children($limit=null, $offset=0, $where=null, $order_by=array("id" => "ASC")) { if ($where) { - $this->where($where); + $this->merge_where($where); } return $this @@ -170,7 +170,7 @@ class ORM_MPTT_Core extends ORM { */ function children_count($where=null) { if ($where) { - $this->where($where); + $this->merge_where($where); } return $this @@ -189,7 +189,7 @@ class ORM_MPTT_Core extends ORM { */ function descendants($limit=null, $offset=0, $where=null, $order_by=array("id" => "ASC")) { if ($where) { - $this->where($where); + $this->merge_where($where); } return $this @@ -207,7 +207,7 @@ class ORM_MPTT_Core extends ORM { */ function descendants_count($where=null) { if ($where) { - $this->where($where); + $this->merge_where($where); } return $this diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php index c8d25cc5..acc4e96f 100644 --- a/modules/gallery/models/item.php +++ b/modules/gallery/models/item.php @@ -428,14 +428,14 @@ class Item_Model extends ORM_MPTT { } else { $comp = "<"; } - $db = Database::instance(); + $db = db::build(); // 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($where) + ->merge_where($where) ->count_records(); if (empty($count)) { @@ -445,7 +445,7 @@ class Item_Model extends ORM_MPTT { $position = $db->from("items") ->where("parent_id", "=", $this->id) ->where($sort_column, $comp, $child->$sort_column) - ->where($where) + ->merge_where($where) ->count_records(); // We stopped short of our target value in the sort (notice that we're using a < comparator @@ -456,12 +456,14 @@ 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") + foreach ($db + ->select("id") + ->from("items") ->where("parent_id", "=", $this->id) ->where($sort_column, "=", $child->$sort_column) - ->where($where) + ->merge_where($where) ->order_by(array("id" => "ASC")) - ->get() as $row) { + ->execute() as $row) { $position++; if ($row->id == $child->id) { break; @@ -485,7 +487,7 @@ class Item_Model extends ORM_MPTT { foreach ($db->select("id") ->from("items") ->where("parent_id", "=", $this->id) - ->where($where) + ->merge_where($where) ->order_by($order_by) ->get() as $row) { $position++; diff --git a/modules/kohana23_compat/libraries/MY_Database_Builder.php b/modules/kohana23_compat/libraries/MY_Database_Builder.php new file mode 100644 index 00000000..974f9c6d --- /dev/null +++ b/modules/kohana23_compat/libraries/MY_Database_Builder.php @@ -0,0 +1,31 @@ +where($tuple[0], $tuple[1], $tuple[2]); + } + return $this; + } +} \ No newline at end of file -- cgit v1.2.3 From a3d904bcbab7cb153c2b617fc4389175ed00a4fc Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Thu, 26 Nov 2009 19:36:57 -0800 Subject: ORM::find_all() now uses null as the default value for offset. --- modules/gallery/libraries/ORM_MPTT.php | 2 +- modules/gallery/models/item.php | 2 +- modules/organize/views/organize_tree.html.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'modules/gallery/libraries/ORM_MPTT.php') diff --git a/modules/gallery/libraries/ORM_MPTT.php b/modules/gallery/libraries/ORM_MPTT.php index 90b9d38a..a67f05be 100644 --- a/modules/gallery/libraries/ORM_MPTT.php +++ b/modules/gallery/libraries/ORM_MPTT.php @@ -150,7 +150,7 @@ class ORM_MPTT_Core extends ORM { * @param array order_by * @return array ORM */ - function children($limit=null, $offset=0, $where=null, $order_by=array("id" => "ASC")) { + function children($limit=null, $offset=null, $where=null, $order_by=array("id" => "ASC")) { if ($where) { $this->merge_where($where); } diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php index acc4e96f..16c57dbc 100644 --- a/modules/gallery/models/item.php +++ b/modules/gallery/models/item.php @@ -597,7 +597,7 @@ class Item_Model extends ORM_MPTT { * @param array order_by * @return array ORM */ - function children($limit=null, $offset=0, $where=array(), $order_by=null) { + function children($limit=null, $offset=null, $where=array(), $order_by=null) { if (empty($order_by)) { $order_by = array($this->sort_column => $this->sort_order); // Use id as a tie breaker diff --git a/modules/organize/views/organize_tree.html.php b/modules/organize/views/organize_tree.html.php index 1ccb942c..c5257956 100644 --- a/modules/organize/views/organize_tree.html.php +++ b/modules/organize/views/organize_tree.html.php @@ -8,7 +8,7 @@ title) ?>
    - children(null, 0, array(array("type", "=", "album"))) as $child): ?> + children(null, null, array(array("type", "=", "album"))) as $child): ?> contains($selected)): ?> $selected, "album" => $child)); ?> -- 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/gallery/libraries/ORM_MPTT.php') 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 a6da027aad5072a85719fc9e201ee946963e74f0 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Tue, 22 Dec 2009 16:22:24 -0800 Subject: The default value for $offset should always be null (according to the new K24 ORM). Fix up a bad where tuple in the test. --- modules/gallery/libraries/ORM_MPTT.php | 2 +- modules/gallery/models/item.php | 2 +- modules/gallery/tests/ORM_MPTT_Test.php | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) (limited to 'modules/gallery/libraries/ORM_MPTT.php') diff --git a/modules/gallery/libraries/ORM_MPTT.php b/modules/gallery/libraries/ORM_MPTT.php index 949ca48c..0ea519c9 100644 --- a/modules/gallery/libraries/ORM_MPTT.php +++ b/modules/gallery/libraries/ORM_MPTT.php @@ -201,7 +201,7 @@ class ORM_MPTT_Core extends ORM { * @param array order_by * @return object ORM_Iterator */ - function descendants($limit=null, $offset=0, $where=null, $order_by=array("id" => "ASC")) { + function descendants($limit=null, $offset=null, $where=null, $order_by=array("id" => "ASC")) { if ($where) { $this->merge_where($where); } diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php index e2208b73..e3d27b6d 100644 --- a/modules/gallery/models/item.php +++ b/modules/gallery/models/item.php @@ -634,7 +634,7 @@ class Item_Model extends ORM_MPTT { * @param array additional where clauses * @return object ORM_Iterator */ - function descendants($limit=null, $offset=0, $where=array(), $order_by=null) { + function descendants($limit=null, $offset=null, $where=array(), $order_by=null) { if (empty($order_by)) { $order_by = array($this->sort_column => $this->sort_order); // Use id as a tie breaker diff --git a/modules/gallery/tests/ORM_MPTT_Test.php b/modules/gallery/tests/ORM_MPTT_Test.php index 36a81d2c..69b6bea9 100644 --- a/modules/gallery/tests/ORM_MPTT_Test.php +++ b/modules/gallery/tests/ORM_MPTT_Test.php @@ -190,8 +190,8 @@ class ORM_MPTT_Test extends Unit_Test_Case { $parent->reload(); $this->assert_equal(3, $parent->descendants()->count()); - $this->assert_equal(2, $parent->descendants(null, 0, array("type" => "photo"))->count()); - $this->assert_equal(1, $parent->descendants(null, 0, array("type" => "album"))->count()); + $this->assert_equal(2, $parent->descendants(null, null, array(array("type", "=", "photo")))->count()); + $this->assert_equal(1, $parent->descendants(null, null, array(array("type", "=", "album")))->count()); } public function descendant_limit_test() { -- cgit v1.2.3