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/exif/helpers/exif_task.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'modules/exif/helpers/exif_task.php') 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. -- cgit v1.2.3 From dee3ee81e2013f0b38e0f84123dec2ce12574bd7 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Thu, 26 Nov 2009 20:25:32 -0800 Subject: Database::orwhere() is now Database_Builder::or_where() --- modules/exif/helpers/exif.php | 2 +- modules/exif/helpers/exif_task.php | 2 +- modules/gallery/controllers/albums.php | 2 +- modules/gallery/controllers/movies.php | 2 +- modules/gallery/controllers/photos.php | 2 +- modules/gallery/helpers/album.php | 2 +- modules/gallery/helpers/movie.php | 2 +- modules/gallery/helpers/photo.php | 2 +- modules/gallery/tests/Database_Test.php | 8 ++++---- modules/search/helpers/search.php | 2 +- modules/search/helpers/search_task.php | 2 +- 11 files changed, 14 insertions(+), 14 deletions(-) (limited to 'modules/exif/helpers/exif_task.php') diff --git a/modules/exif/helpers/exif.php b/modules/exif/helpers/exif.php index b4241e89..a8b12a8b 100644 --- a/modules/exif/helpers/exif.php +++ b/modules/exif/helpers/exif.php @@ -146,7 +146,7 @@ class exif_Core { ->where("type", "=", "photo") ->and_open() ->where("exif_records.item_id", "=", null) - ->orwhere("exif_records.dirty", "=", 1) + ->or_where("exif_records.dirty", "=", 1) ->close() ->get() ->count(); diff --git a/modules/exif/helpers/exif_task.php b/modules/exif/helpers/exif_task.php index 1a449fc7..66f69790 100644 --- a/modules/exif/helpers/exif_task.php +++ b/modules/exif/helpers/exif_task.php @@ -47,7 +47,7 @@ class exif_task_Core { ->where("type", "=", "photo") ->and_open() ->where("exif_records.item_id", "=", null) - ->orwhere("exif_records.dirty", "=", 1) + ->or_where("exif_records.dirty", "=", 1) ->close() ->find_all() as $item) { // The query above can take a long time, so start the timer after its done diff --git a/modules/gallery/controllers/albums.php b/modules/gallery/controllers/albums.php index 431d98a0..32db48d4 100644 --- a/modules/gallery/controllers/albums.php +++ b/modules/gallery/controllers/albums.php @@ -130,7 +130,7 @@ class Albums_Controller extends Items_Controller { ->where("id", "<>", $album->id) ->and_open() ->where("name", "=", $form->edit_item->dirname->value) - ->orwhere("slug", "=", $form->edit_item->slug->value) + ->or_where("slug", "=", $form->edit_item->slug->value) ->close() ->get() ->current()) { diff --git a/modules/gallery/controllers/movies.php b/modules/gallery/controllers/movies.php index 157c388f..e017b8c8 100644 --- a/modules/gallery/controllers/movies.php +++ b/modules/gallery/controllers/movies.php @@ -78,7 +78,7 @@ class Movies_Controller extends Items_Controller { ->where("id <>", $movie->id) ->and_open() ->where("name", "=", $form->edit_item->filename->value) - ->orwhere("slug", "=", $form->edit_item->slug->value) + ->or_where("slug", "=", $form->edit_item->slug->value) ->close() ->get() ->current()) { diff --git a/modules/gallery/controllers/photos.php b/modules/gallery/controllers/photos.php index 478447b1..5431fcfe 100644 --- a/modules/gallery/controllers/photos.php +++ b/modules/gallery/controllers/photos.php @@ -78,7 +78,7 @@ class Photos_Controller extends Items_Controller { ->where("id", "<>", $photo->id) ->and_open() ->where("name", "=", $form->edit_item->filename->value) - ->orwhere("slug", "=", $form->edit_item->slug->value) + ->or_where("slug", "=", $form->edit_item->slug->value) ->close() ->get() ->current()) { diff --git a/modules/gallery/helpers/album.php b/modules/gallery/helpers/album.php index cd8777e2..0ffb0fc6 100644 --- a/modules/gallery/helpers/album.php +++ b/modules/gallery/helpers/album.php @@ -71,7 +71,7 @@ class album_Core { ->where("parent_id", "=", $parent->id) ->and_open() ->where("name", "=", $album->name) - ->orwhere("slug", "=", $album->slug) + ->or_where("slug", "=", $album->slug) ->close() ->find()->id) { $rand = rand(); diff --git a/modules/gallery/helpers/movie.php b/modules/gallery/helpers/movie.php index 82247eb0..96dafe11 100644 --- a/modules/gallery/helpers/movie.php +++ b/modules/gallery/helpers/movie.php @@ -93,7 +93,7 @@ class movie_Core { ->where("parent_id", "=", $parent->id) ->and_open() ->where("name", "=", $movie->name) - ->orwhere("slug", "=", $movie->slug) + ->or_where("slug", "=", $movie->slug) ->close() ->find()->id) { $rand = rand(); diff --git a/modules/gallery/helpers/photo.php b/modules/gallery/helpers/photo.php index 2a563043..cc309e28 100644 --- a/modules/gallery/helpers/photo.php +++ b/modules/gallery/helpers/photo.php @@ -92,7 +92,7 @@ class photo_Core { ->where("parent_id", "=", $parent->id) ->and_open() ->where("name", "=", $photo->name) - ->orwhere("slug", "=", $photo->slug) + ->or_where("slug", "=", $photo->slug) ->close() ->find()->id) { $rand = rand(); diff --git a/modules/gallery/tests/Database_Test.php b/modules/gallery/tests/Database_Test.php index 4f5a1da2..9b428379 100644 --- a/modules/gallery/tests/Database_Test.php +++ b/modules/gallery/tests/Database_Test.php @@ -32,7 +32,7 @@ class Database_Test extends Unit_Test_Case { ->where("outer1", "=", 1) ->and_open() ->where("inner1", "=", 1) - ->orwhere("inner2", "=", 2) + ->or_where("inner2", "=", 2) ->close() ->where("outer2", "=", 2) ->compile(); @@ -46,7 +46,7 @@ class Database_Test extends Unit_Test_Case { $sql = Database::instance() ->and_open() ->where("inner1", "=", 1) - ->orwhere("inner2", "=", 2) + ->or_where("inner2", "=", 2) ->close() ->where("outer1", "=", 1) ->where("outer2", "=", 2) @@ -62,8 +62,8 @@ class Database_Test extends Unit_Test_Case { ->where("outer1", "=", 1) ->and_open() ->where("inner1", "=", 1) - ->orwhere("inner2", "=", 2) - ->orwhere("inner3", "=", 3)) + ->or_where("inner2", "=", 2) + ->or_where("inner3", "=", 3)) ->close() ->compile(); $sql = str_replace("\n", " ", $sql); diff --git a/modules/search/helpers/search.php b/modules/search/helpers/search.php index c0bb1b32..1fd5175f 100644 --- a/modules/search/helpers/search.php +++ b/modules/search/helpers/search.php @@ -78,7 +78,7 @@ class search_Core { ->join("search_records", "items.id", "search_records.item_id", "left") ->and_open() ->where("search_records.item_id", "=", null) - ->orwhere("search_records.dirty", "=", 1) + ->or_where("search_records.dirty", "=", 1) ->close() ->get() ->count(); diff --git a/modules/search/helpers/search_task.php b/modules/search/helpers/search_task.php index 061f4084..6b35cabc 100644 --- a/modules/search/helpers/search_task.php +++ b/modules/search/helpers/search_task.php @@ -46,7 +46,7 @@ class search_task_Core { 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) + ->or_where("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. -- cgit v1.2.3 From d2cb217e20d44d7928a0910ac0375740de163bb3 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Wed, 2 Dec 2009 00:34:34 -0800 Subject: Convert more database calls over to the new format - admin/maintenance page loads, the rebuild thumbs/resizes task works - Fixed up some conversion bugs in the Cache driver --- modules/exif/helpers/exif.php | 6 +-- modules/exif/helpers/exif_task.php | 9 +++-- modules/gallery/controllers/admin_maintenance.php | 22 ++++++----- modules/gallery/helpers/gallery_task.php | 4 +- modules/gallery/helpers/graphics.php | 33 ++++++++++------ .../gallery/libraries/drivers/Cache/Database.php | 46 ++++++++++++---------- modules/search/helpers/search.php | 4 +- modules/search/helpers/search_task.php | 8 ++-- 8 files changed, 77 insertions(+), 55 deletions(-) (limited to 'modules/exif/helpers/exif_task.php') diff --git a/modules/exif/helpers/exif.php b/modules/exif/helpers/exif.php index d3a8c103..3177e7eb 100644 --- a/modules/exif/helpers/exif.php +++ b/modules/exif/helpers/exif.php @@ -139,7 +139,7 @@ class exif_Core { } static function stats() { - $missing_exif = Database::instance() + $missing_exif = db::build() ->select("items.id") ->from("items") ->join("exif_records", "items.id", "exif_records.item_id", "left") @@ -148,10 +148,10 @@ class exif_Core { ->where("exif_records.item_id", "=", null) ->or_where("exif_records.dirty", "=", 1) ->close() - ->get() + ->execute() ->count(); - $total_items = ORM::factory("item")->where("type", "photo")->count_all(); + $total_items = ORM::factory("item")->where("type", "=", "photo")->count_all(); if (!$total_items) { return array(0, 0, 0); } diff --git a/modules/exif/helpers/exif_task.php b/modules/exif/helpers/exif_task.php index 66f69790..e25e2405 100644 --- a/modules/exif/helpers/exif_task.php +++ b/modules/exif/helpers/exif_task.php @@ -20,10 +20,11 @@ class exif_task_Core { static function available_tasks() { // Delete extra exif_records - Database::instance()->query( - "DELETE FROM {exif_records} " . - "WHERE {exif_records}.`item_id` NOT IN " . - "(SELECT `id` FROM {items} WHERE {items}.`type` = 'photo')"); + db::build() + ->delete("exif_records") + ->where("item_id", "NOT IN", + db::build()->select("id")->from("items")->where("type", "=", "photo")) + ->execute(); list ($remaining, $total, $percent) = exif::stats(); return array(Task_Definition::factory() diff --git a/modules/gallery/controllers/admin_maintenance.php b/modules/gallery/controllers/admin_maintenance.php index 6377f40f..213e4fe2 100644 --- a/modules/gallery/controllers/admin_maintenance.php +++ b/modules/gallery/controllers/admin_maintenance.php @@ -22,11 +22,13 @@ class Admin_Maintenance_Controller extends Admin_Controller { * Show a list of all available, running and finished tasks. */ public function index() { - $query = Database::instance()->query( - "UPDATE {tasks} SET `state` = 'stalled' " . - "WHERE done = 0 " . - "AND state <> 'stalled' " . - "AND unix_timestamp(now()) - updated > 15"); + $query = db::build() + ->update("tasks") + ->set("state", "stalled") + ->where("done", "=", 0) + ->where("state", "<>", "stalled") + ->where(new Database_Expression("UNIX_TIMESTAMP(NOW()) - `updated` > 15")) + ->execute(); $stalled_count = $query->count(); if ($stalled_count) { log::warning("tasks", @@ -138,10 +140,12 @@ class Admin_Maintenance_Controller extends Admin_Controller { public function cancel_running_tasks() { access::verify_csrf(); - Database::instance()->update( - "tasks", - array("done" => 1, "state" => "cancelled"), - array("done" => 0)); + db::build() + ->update("tasks") + ->set("done", 1) + ->set("state", "cancelled") + ->where("done", "=", 0) + ->execute(); message::success(t("All running tasks cancelled")); url::redirect("admin/maintenance"); } diff --git a/modules/gallery/helpers/gallery_task.php b/modules/gallery/helpers/gallery_task.php index 4d6de3ba..3a705027 100644 --- a/modules/gallery/helpers/gallery_task.php +++ b/modules/gallery/helpers/gallery_task.php @@ -19,7 +19,7 @@ */ class gallery_task_Core { static function available_tasks() { - $dirty_count = graphics::find_dirty_images_query()->count(); + $dirty_count = graphics::find_dirty_images_query()->count_records(); $tasks = array(); $tasks[] = Task_Definition::factory() ->callback("gallery_task::rebuild_dirty_images") @@ -47,7 +47,7 @@ class gallery_task_Core { static function rebuild_dirty_images($task) { $errors = array(); try { - $result = graphics::find_dirty_images_query(); + $result = graphics::find_dirty_images_query()->select("id")->execute(); $total_count = $task->get("total_count", $result->count()); $mode = $task->get("mode", "init"); if ($mode == "init") { diff --git a/modules/gallery/helpers/graphics.php b/modules/gallery/helpers/graphics.php index c93cc304..6fab0b54 100644 --- a/modules/gallery/helpers/graphics.php +++ b/modules/gallery/helpers/graphics.php @@ -197,11 +197,22 @@ class graphics_Core { * @return Database_Result Query result */ static function find_dirty_images_query() { - return Database::instance()->query( - "SELECT `id` FROM {items} " . - "WHERE ((`thumb_dirty` = 1 AND (`type` <> 'album' OR `album_cover_item_id` IS NOT NULL))" . - " OR (`resize_dirty` = 1 AND `type` = 'photo')) " . - " AND `id` != 1"); + return db::build() + ->from("items") + ->and_open() + ->and_open() + ->where("thumb_dirty", "=", 1) + ->and_open() + ->where("type", "<>", "album") + ->or_where("album_cover_item_id", "IS NOT", null) + ->close() + ->or_open() + ->where("resize_dirty", "=", 1) + ->where("type", "=", "photo") + ->close() + ->close() + ->where("id", "<>", 1) + ->close(); } /** @@ -209,18 +220,18 @@ class graphics_Core { */ static function mark_dirty($thumbs, $resizes) { if ($thumbs || $resizes) { - $db = Database::instance(); - $fields = array(); + $db = db::build() + ->update("items"); if ($thumbs) { - $fields["thumb_dirty"] = 1; + $db->set("thumb_dirty", 1); } if ($resizes) { - $fields["resize_dirty"] = 1; + $db->set("resize_dirty", 1); } - $db->update("items", $fields, true); + $db->execute(); } - $count = self::find_dirty_images_query()->count(); + $count = self::find_dirty_images_query()->count_records(); if ($count) { site_status::warning( t2("One of your photos is out of date. Click here to fix it", diff --git a/modules/gallery/libraries/drivers/Cache/Database.php b/modules/gallery/libraries/drivers/Cache/Database.php index a317798e..5fa2a72a 100644 --- a/modules/gallery/libraries/drivers/Cache/Database.php +++ b/modules/gallery/libraries/drivers/Cache/Database.php @@ -33,7 +33,7 @@ class Cache_Database_Driver extends Cache_Driver { public function exists($id) { $count = db::build() ->where("key", "=", $id) - ->where("expiration", ">=", "time()") + ->where("expiration", ">=", time()) ->count_records("caches"); return $count > 0; } @@ -61,16 +61,18 @@ class Cache_Database_Driver extends Cache_Driver { foreach ($items as $id => $data) { if ($this->exists($id)) { - $status = db::build()->update( - "caches", - array("tags" => $tags, "expiration" => $lifetime, "cache" => serialize($data)), - array("key", "=", $id)) + $status = db::build() + ->update("caches") + ->set("tags", $tags) + ->set("expiration", $lifetime) + ->set("cache", serialize($data)) + ->where("key", "=", $id) ->execute(); } else { - $status = db::build()->insert( - "caches", - array("key" => $id, "tags" => $tags, - "expiration" => $lifetime, "cache" => serialize($data))) + $status = db::build() + ->insert("caches") + ->columns("key", "tags", "expiration", "cache") + ->values($id, $tags, $lifetime, serialize($data)) ->execute(); } } @@ -84,11 +86,13 @@ class Cache_Database_Driver extends Cache_Driver { * @return array cached data */ public function get_tag($tags) { - $db = db::build()->from("caches"); + $db = db::build() + ->select("*") + ->from("caches"); foreach ($tags as $tag) { $db->where("tags", "LIKE", "<$tag>"); } - $db_result = $db->execute()->as_array(); + $db_result = $db->execute(); // An array will always be returned $result = array(); @@ -116,9 +120,9 @@ class Cache_Database_Driver extends Cache_Driver { public function get($keys, $single=false) { $data = null; $result = db::build() + ->select("*") ->from("caches") ->where("key", "IN", $keys) - ->select() ->execute(); if (count($result) > 0) { @@ -150,17 +154,18 @@ class Cache_Database_Driver extends Cache_Driver { * @return bool */ public function delete($id, $tag = false) { - $this->db->from("caches"); + $db = db::build() + ->delete("caches"); if ($id === true) { // Delete all caches - $this->db->where("1", "=", "1"); + $db->where("1", "=", "1"); } else if ($tag === true) { - $this->db->like("tags", "<$id>"); + $db->where("tags", "LIKE", "<$id>"); } else { - $this->db->where("key", "=", $id); + $db->where("key", "=", $id); } - $status = $this->db->delete(); + $status = $db->execute(); return count($status) > 0; } @@ -177,10 +182,11 @@ class Cache_Database_Driver extends Cache_Driver { */ public function delete_expired() { // Delete all expired caches - $status = $this->db->from("caches") + $status = db::build() + ->delete("caches") ->where("expiration", "<>", 0) ->where("expiration", "<=", time()) - ->delete(); + ->execute(); return count($status) > 0; } @@ -189,6 +195,6 @@ class Cache_Database_Driver extends Cache_Driver { * Empty the cache */ public function delete_all() { - db::build()->query("TRUNCATE {caches}")->execute(); + Database::instance()->query("TRUNCATE {caches}"); } } \ No newline at end of file diff --git a/modules/search/helpers/search.php b/modules/search/helpers/search.php index 1fd5175f..9123df09 100644 --- a/modules/search/helpers/search.php +++ b/modules/search/helpers/search.php @@ -72,7 +72,7 @@ class search_Core { } static function stats() { - $remaining = Database::instance() + $remaining = db::build() ->select("items.id") ->from("items") ->join("search_records", "items.id", "search_records.item_id", "left") @@ -80,7 +80,7 @@ class search_Core { ->where("search_records.item_id", "=", null) ->or_where("search_records.dirty", "=", 1) ->close() - ->get() + ->execute() ->count(); $total = ORM::factory("item")->count_all(); diff --git a/modules/search/helpers/search_task.php b/modules/search/helpers/search_task.php index 6b35cabc..6aa4a0d1 100644 --- a/modules/search/helpers/search_task.php +++ b/modules/search/helpers/search_task.php @@ -20,10 +20,10 @@ class search_task_Core { static function available_tasks() { // Delete extra search_records - Database::instance()->query( - "DELETE FROM {search_records} " . - "WHERE {search_records}.`item_id` NOT IN " . - "(SELECT `id` FROM {items})"); + db::build() + ->delete("search_records") + ->where("item_id", "NOT IN", db::build()->select("id")->from("items")) + ->execute(); list ($remaining, $total, $percent) = search::stats(); return array(Task_Definition::factory() -- cgit v1.2.3 From 94411b36585a5bdf6225b8af6ae4ee5c21d5779d Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Wed, 2 Dec 2009 10:02:08 -0800 Subject: Fix a bunch of places where we were using "= null" instead of "IS NULL". --- modules/exif/helpers/exif.php | 2 +- modules/exif/helpers/exif_task.php | 2 +- modules/search/helpers/search.php | 6 ++---- modules/search/helpers/search_task.php | 2 +- modules/server_add/controllers/server_add.php | 2 +- 5 files changed, 6 insertions(+), 8 deletions(-) (limited to 'modules/exif/helpers/exif_task.php') diff --git a/modules/exif/helpers/exif.php b/modules/exif/helpers/exif.php index 3177e7eb..3baed177 100644 --- a/modules/exif/helpers/exif.php +++ b/modules/exif/helpers/exif.php @@ -145,7 +145,7 @@ class exif_Core { ->join("exif_records", "items.id", "exif_records.item_id", "left") ->where("type", "=", "photo") ->and_open() - ->where("exif_records.item_id", "=", null) + ->where("exif_records.item_id", "IS", null) ->or_where("exif_records.dirty", "=", 1) ->close() ->execute() diff --git a/modules/exif/helpers/exif_task.php b/modules/exif/helpers/exif_task.php index e25e2405..27352643 100644 --- a/modules/exif/helpers/exif_task.php +++ b/modules/exif/helpers/exif_task.php @@ -47,7 +47,7 @@ class exif_task_Core { ->join("exif_records", "items.id", "exif_records.item_id", "left") ->where("type", "=", "photo") ->and_open() - ->where("exif_records.item_id", "=", null) + ->where("exif_records.item_id", "IS", null) ->or_where("exif_records.dirty", "=", 1) ->close() ->find_all() as $item) { diff --git a/modules/search/helpers/search.php b/modules/search/helpers/search.php index 9123df09..70a39272 100644 --- a/modules/search/helpers/search.php +++ b/modules/search/helpers/search.php @@ -73,15 +73,13 @@ class search_Core { static function stats() { $remaining = db::build() - ->select("items.id") ->from("items") ->join("search_records", "items.id", "search_records.item_id", "left") ->and_open() - ->where("search_records.item_id", "=", null) + ->where("search_records.item_id", "IS", null) ->or_where("search_records.dirty", "=", 1) ->close() - ->execute() - ->count(); + ->count_records(); $total = ORM::factory("item")->count_all(); $percent = round(100 * ($total - $remaining) / $total); diff --git a/modules/search/helpers/search_task.php b/modules/search/helpers/search_task.php index 6aa4a0d1..e039e493 100644 --- a/modules/search/helpers/search_task.php +++ b/modules/search/helpers/search_task.php @@ -45,7 +45,7 @@ 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) + ->where("search_records.item_id", "IS", null) ->or_where("search_records.dirty", "=", 1) ->find_all() as $item) { // The query above can take a long time, so start the timer after its done diff --git a/modules/server_add/controllers/server_add.php b/modules/server_add/controllers/server_add.php index 3c3a6c2b..78e1620c 100644 --- a/modules/server_add/controllers/server_add.php +++ b/modules/server_add/controllers/server_add.php @@ -198,7 +198,7 @@ class Server_Add_Controller extends Admin_Controller { // they're done. $entries = ORM::factory("server_add_file") ->where("task_id", "=", $task->id) - ->where("item_id", "=", null) + ->where("item_id", "IS", null) ->order_by("id", "ASC") ->limit(10) ->find_all(); -- cgit v1.2.3