summaryrefslogtreecommitdiff
path: root/modules/gallery/libraries
diff options
context:
space:
mode:
Diffstat (limited to 'modules/gallery/libraries')
-rw-r--r--modules/gallery/libraries/Gallery_I18n.php (renamed from modules/gallery/libraries/I18n.php)20
-rw-r--r--modules/gallery/libraries/Gallery_View.php6
-rw-r--r--modules/gallery/libraries/IdentityProvider.php4
-rw-r--r--modules/gallery/libraries/MY_Database.php27
-rw-r--r--modules/gallery/libraries/MY_Forge.php7
-rw-r--r--modules/gallery/libraries/MY_Kohana_Exception.php31
-rw-r--r--modules/gallery/libraries/MY_ORM.php10
-rw-r--r--modules/gallery/libraries/MY_View.php36
-rw-r--r--modules/gallery/libraries/ORM_MPTT.php146
-rw-r--r--modules/gallery/libraries/SafeString.php2
-rw-r--r--modules/gallery/libraries/Sendmail.php4
-rw-r--r--modules/gallery/libraries/drivers/Cache/Database.php121
12 files changed, 247 insertions, 167 deletions
diff --git a/modules/gallery/libraries/I18n.php b/modules/gallery/libraries/Gallery_I18n.php
index c3336052..9a5e7dc1 100644
--- a/modules/gallery/libraries/I18n.php
+++ b/modules/gallery/libraries/Gallery_I18n.php
@@ -27,7 +27,7 @@
* @return String The translated message string.
*/
function t($message, $options=array()) {
- return I18n::instance()->translate($message, $options);
+ return Gallery_I18n::instance()->translate($message, $options);
}
/**
@@ -43,11 +43,11 @@ function t($message, $options=array()) {
* @return String The translated message string.
*/
function t2($singular, $plural, $count, $options=array()) {
- return I18n::instance()->translate(array("one" => $singular, "other" => $plural),
+ return Gallery_I18n::instance()->translate(array("one" => $singular, "other" => $plural),
array_merge($options, array("count" => $count)));
}
-class I18n_Core {
+class Gallery_I18n_Core {
private static $_instance;
private $_config = array();
private $_call_log = array();
@@ -64,7 +64,7 @@ class I18n_Core {
if (empty($config['default_locale'])) {
$config['default_locale'] = module::get_var('gallery', 'default_locale');
}
- self::$_instance = new I18n_Core($config);
+ self::$_instance = new Gallery_I18n_Core($config);
}
return self::$_instance;
@@ -128,21 +128,21 @@ class I18n_Core {
if (!isset($this->_cache[$locale])) {
$this->_cache[$locale] = array();
// TODO: Load data from locale file instead of the DB.
- foreach (Database::instance()
+ foreach (db::build()
->select("key", "translation")
->from("incoming_translations")
- ->where(array("locale" => $locale))
- ->get()
+ ->where("locale", "=", $locale)
+ ->execute()
->as_array() as $row) {
$this->_cache[$locale][$row->key] = unserialize($row->translation);
}
// Override incoming with outgoing...
- foreach (Database::instance()
+ foreach (db::build()
->select("key", "translation")
->from("outgoing_translations")
- ->where(array("locale" => $locale))
- ->get()
+ ->where("locale", "=", $locale)
+ ->execute()
->as_array() as $row) {
$this->_cache[$locale][$row->key] = unserialize($row->translation);
}
diff --git a/modules/gallery/libraries/Gallery_View.php b/modules/gallery/libraries/Gallery_View.php
index 3bf56d0f..940c5321 100644
--- a/modules/gallery/libraries/Gallery_View.php
+++ b/modules/gallery/libraries/Gallery_View.php
@@ -32,7 +32,7 @@ class Gallery_View_Core extends View {
if (($path = gallery::find_file("js", $file, false))) {
$this->scripts[$path] = 1;
} else {
- Kohana::log("error", "Can't find script file: $file");
+ Kohana_Log::add("error", "Can't find script file: $file");
}
}
@@ -55,7 +55,7 @@ class Gallery_View_Core extends View {
if (($path = gallery::find_file("css", $file, false))) {
$this->css[$path] = 1;
} else {
- Kohana::log("error", "Can't find css file: $file");
+ Kohana_Log::add("error", "Can't find css file: $file");
}
}
@@ -130,7 +130,7 @@ class Gallery_View_Core extends View {
$search[] = $match[0];
$replace[] = "url('" . url::abs_file($relative) . "')";
} else {
- Kohana::log("error", "Missing URL reference '{$match[1]}' in CSS file '$css_file'");
+ Kohana_Log::add("error", "Missing URL reference '{$match[1]}' in CSS file '$css_file'");
}
}
$replace = str_replace(DIRECTORY_SEPARATOR, "/", $replace);
diff --git a/modules/gallery/libraries/IdentityProvider.php b/modules/gallery/libraries/IdentityProvider.php
index e213ae97..bcb3056a 100644
--- a/modules/gallery/libraries/IdentityProvider.php
+++ b/modules/gallery/libraries/IdentityProvider.php
@@ -54,7 +54,7 @@ class IdentityProvider_Core {
*/
static function reset() {
self::$instance = null;
- Kohana::config_clear("identity");
+ Kohana_Config::instance()->clear("identity");
}
/**
@@ -90,7 +90,7 @@ class IdentityProvider_Core {
get_class($this), "IdentityProvider_Driver");
}
- Kohana::log("debug", "Identity Library initialized");
+ Kohana_Log::add("debug", "Identity Library initialized");
}
/**
diff --git a/modules/gallery/libraries/MY_Database.php b/modules/gallery/libraries/MY_Database.php
index c56f16e8..52bc46d6 100644
--- a/modules/gallery/libraries/MY_Database.php
+++ b/modules/gallery/libraries/MY_Database.php
@@ -17,34 +17,9 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
-class Database extends Database_Core {
+abstract class Database extends Database_Core {
protected $_table_names;
- public function open_paren() {
- $this->where[] = "(";
- return $this;
- }
-
- public function close_paren() {
- // Search backwards for the last opening paren and resolve it
- $i = count($this->where) - 1;
- $this->where[$i] .= ")";
- while (--$i >= 0) {
- if ($this->where[$i] == "(") {
- // Remove the paren from the where clauses, and add it to the right of the operator of the
- // next where clause. If removing the paren makes the next where clause the first element
- // in the where list, then the operator shouldn't be there. It's there because we
- // calculate whether or not we need an operator based on the number of where clauses, and
- // the open paren seems like a where clause even though it isn't.
- array_splice($this->where, $i, 1);
- $this->where[$i] = preg_replace("/^(AND|OR) /", $i ? "\\1 (" : "(", $this->where[$i]);
- return $this;
- }
- }
-
- throw new Kohana_Database_Exception('database.missing_open_paren');
- }
-
/**
* Parse the query string and convert any strings of the form `\([a-zA-Z0-9_]*?)\]
* table prefix . $1
diff --git a/modules/gallery/libraries/MY_Forge.php b/modules/gallery/libraries/MY_Forge.php
index b40d067d..9564f941 100644
--- a/modules/gallery/libraries/MY_Forge.php
+++ b/modules/gallery/libraries/MY_Forge.php
@@ -24,14 +24,13 @@ class Forge extends Forge_Core {
*/
public function __construct($action=null, $title='', $method=null, $attr=array()) {
parent::__construct($action, $title, $method, $attr);
- $this->hidden("csrf")->value("");
+ $this->hidden("csrf")->value(access::csrf_token());
}
/**
* Use our own template
*/
public function render($template="form.html", $custom=false) {
- $this->hidden["csrf"]->value(access::csrf_token());
return parent::render($template, $custom);
}
@@ -43,8 +42,8 @@ class Forge extends Forge_Core {
if (isset($input->inputs)) {
$input->add_rules_from($model);
}
- if (isset($model->rules[$name])) {
- $input->rules($model->rules[$name]);
+ if (isset($model->form_rules[$name])) {
+ $input->rules($model->form_rules[$name]);
}
}
}
diff --git a/modules/gallery/libraries/MY_Kohana_Exception.php b/modules/gallery/libraries/MY_Kohana_Exception.php
new file mode 100644
index 00000000..dd5998a1
--- /dev/null
+++ b/modules/gallery/libraries/MY_Kohana_Exception.php
@@ -0,0 +1,31 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class Kohana_Exception extends Kohana_Exception_Core {
+ /**
+ * Dump out the full stack trace as part of the text representation of the exception.
+ */
+ public static function text($e) {
+ return sprintf(
+ "%s [ %s ]: %s\n%s [ %s ]\n%s",
+ get_class($e), $e->getCode(), strip_tags($e->getMessage()),
+ $e->getFile(), $e->getLine(),
+ $e->getTraceAsString());
+ }
+} \ No newline at end of file
diff --git a/modules/gallery/libraries/MY_ORM.php b/modules/gallery/libraries/MY_ORM.php
index 2c9ad1d7..56c776aa 100644
--- a/modules/gallery/libraries/MY_ORM.php
+++ b/modules/gallery/libraries/MY_ORM.php
@@ -21,16 +21,6 @@ class ORM extends ORM_Core {
// Track the original value of this ORM so that we can look it up in ORM::original()
protected $original = null;
- public function open_paren() {
- $this->db->open_paren();
- return $this;
- }
-
- public function close_paren() {
- $this->db->close_paren();
- return $this;
- }
-
public function save() {
model_cache::clear();
$result = parent::save();
diff --git a/modules/gallery/libraries/MY_View.php b/modules/gallery/libraries/MY_View.php
index eb55aca6..cec59ec1 100644
--- a/modules/gallery/libraries/MY_View.php
+++ b/modules/gallery/libraries/MY_View.php
@@ -18,6 +18,35 @@
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class View extends View_Core {
+ static $global_data = array();
+
+ /**
+ * Reimplement Kohana 2.3's View::set_global() functionality.
+ */
+ public function set_global($key, $value) {
+ View::$global_data[$key] = $value;
+ }
+
+ public function is_set($key) {
+ return parent::is_set($key) ? true : array_key_exists($key, View::$global_data);
+ }
+
+ /**
+ * Completely replace View_Core::__get() so that local data trumps global data, trumps members.
+ * This simulates the Kohana 2.3 behavior.
+ */
+ public function &__get($key) {
+ if (isset($this->kohana_local_data[$key])) {
+ return $this->kohana_local_data[$key];
+ } else if (isset(View::$global_data[$key])) {
+ return View::$global_data[$key];
+ } else if (isset($this->$key)) {
+ return $this->$key;
+ } else {
+ throw new Kohana_Exception('Undefined view variable: :var', array(':var' => $key));
+ }
+ }
+
/**
* Override View_Core::__construct so that we can set the csrf value into all views.
*
@@ -34,11 +63,12 @@ class View extends View_Core {
*
* @see View_Core::render
*/
- public function render($print=false, $renderer=false) {
+ public function render($print=false, $renderer=false, $modifier=false) {
try {
- return parent::render($print, $renderer);
+ $this->kohana_local_data = array_merge(View::$global_data, $this->kohana_local_data);
+ return parent::render($print, $renderer, $modifier);
} catch (Exception $e) {
- Kohana::Log("error", $e->getMessage() . "\n" . $e->getTraceAsString());
+ Kohana_Log::add("error", $e->getMessage() . "\n" . $e->getTraceAsString());
return "";
}
}
diff --git a/modules/gallery/libraries/ORM_MPTT.php b/modules/gallery/libraries/ORM_MPTT.php
index ebd7abc2..949ca48c 100644
--- a/modules/gallery/libraries/ORM_MPTT.php
+++ b/modules/gallery/libraries/ORM_MPTT.php
@@ -52,10 +52,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
@@ -94,10 +100,16 @@ class ORM_MPTT_Core extends ORM {
$this->lock();
$this->reload(); // Assume that the prior lock holder may have changed this entry
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;
@@ -135,10 +147,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();
}
@@ -149,14 +161,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=null, $where=null, $order_by=array("id" => "ASC")) {
+ if ($where) {
+ $this->merge_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);
}
@@ -167,10 +182,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->merge_where($where);
+ }
+
return $this
- ->where($where)
- ->where("parent_id", $this->id)
+ ->where("parent_id", "=", $this->id)
->count_all();
}
@@ -180,15 +198,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->merge_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);
}
@@ -198,11 +219,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->merge_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();
}
@@ -231,23 +255,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;
@@ -258,20 +291,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/libraries/SafeString.php b/modules/gallery/libraries/SafeString.php
index ba3a8ffd..cc63f3a7 100644
--- a/modules/gallery/libraries/SafeString.php
+++ b/modules/gallery/libraries/SafeString.php
@@ -146,7 +146,7 @@ class SafeString_Core {
* Escape special HTML chars ("<", ">", "&", etc.) to HTML entities.
*/
private static function _escape_for_html($dirty_html) {
- return html::specialchars($dirty_html);
+ return html::chars($dirty_html);
}
/**
diff --git a/modules/gallery/libraries/Sendmail.php b/modules/gallery/libraries/Sendmail.php
index 7bc21a67..aa2b51a9 100644
--- a/modules/gallery/libraries/Sendmail.php
+++ b/modules/gallery/libraries/Sendmail.php
@@ -52,7 +52,7 @@ class Sendmail_Core {
break;
case "header":
if (count($value) != 2) {
- Kohana::log("error", wordwrap("Invalid header parameters\n" . Kohana::debug($value)));
+ Kohana_Log::add("error", wordwrap("Invalid header parameters\n" . Kohana::debug($value)));
throw new Exception("@todo INVALID_HEADER_PARAMETERS");
}
$this->headers[$value[0]] = $value[1];
@@ -71,7 +71,7 @@ class Sendmail_Core {
public function send() {
if (empty($this->to)) {
- Kohana::log("error", wordwrap("Sending mail failed:\nNo to address specified"));
+ Kohana_Log::add("error", wordwrap("Sending mail failed:\nNo to address specified"));
throw new Exception("@todo TO_IS_REQUIRED_FOR_MAIL");
}
$to = implode(", ", $this->to);
diff --git a/modules/gallery/libraries/drivers/Cache/Database.php b/modules/gallery/libraries/drivers/Cache/Database.php
index 7e2aeabc..5c453f28 100644
--- a/modules/gallery/libraries/drivers/Cache/Database.php
+++ b/modules/gallery/libraries/drivers/Cache/Database.php
@@ -20,43 +20,33 @@
/*
* Based on the Cache_Sqlite_Driver developed by the Kohana Team
*/
-class Cache_Database_Driver implements Cache_Driver {
+class Cache_Database_Driver extends Cache_Driver {
// Kohana database instance
protected $db;
/**
- * Tests that the storage location is a directory and is writable.
- */
- public function __construct() {
- // Open up an instance of the database
- $this->db = Database::instance();
-
- if (!$this->db->table_exists("caches")) {
- throw new Exception("@todo Cache table is not defined");
- }
- }
-
- /**
* Checks if a cache id is already set.
*
* @param string cache id
* @return boolean
*/
public function exists($id) {
- $count = $this->db->count_records("caches", array("key" => $id, "expiration >=" => time()));
+ $count = db::build()
+ ->where("key", "=", $id)
+ ->where("expiration", ">=", time())
+ ->count_records("caches");
return $count > 0;
}
/**
* Sets a cache item to the given data, tags, and lifetime.
*
- * @param string cache id to set
- * @param string data in the cache
+ * @param array assoc array of key => value pairs
* @param array cache tags
* @param integer lifetime
* @return bool
*/
- public function set($id, $data, array $tags = NULL, $lifetime) {
+ public function set($items, $tags=null, $lifetime=null) {
if (!empty($tags)) {
// Escape the tags, adding brackets so the tag can be explicitly matched
$tags = "<" . implode(">,<", $tags) . ">";
@@ -69,46 +59,53 @@ class Cache_Database_Driver implements Cache_Driver {
$lifetime += time();
}
- if ($this->exists($id)) {
- $status = $this->db->update(
- "caches",
- array("tags" => $tags, "expiration" => $lifetime, "cache" => serialize($data)), array("key" => $id));
- } else {
- $status = $this->db->insert(
- "caches",
- array("key" => $id, "tags" => $tags, "expiration" => $lifetime, "cache" => serialize($data)));
+ foreach ($items as $id => $data) {
+ if ($this->exists($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")
+ ->columns("key", "tags", "expiration", "cache")
+ ->values($id, $tags, $lifetime, serialize($data))
+ ->execute();
+ }
}
- return count($status) > 0;
+ return true;
}
/**
- * Finds an array of ids for a given tag.
- *
- * @param string tag name
- * @return array of ids that match the tag
+ * Get cache items by tag
+ * @param array cache tags
+ * @return array cached data
*/
- public function find($tag) {
- $db_result = $this->db->from("caches")
- ->like("tags", "<$tag>")
- ->get()
- ->result(true);
+ public function get_tag($tags) {
+ $db = db::build()
+ ->select()
+ ->from("caches");
+ foreach ($tags as $tag) {
+ $db->where("tags", "LIKE", "<$tag>");
+ }
+ $db_result = $db->execute();
// An array will always be returned
$result = array();
+ // Disable notices for unserializing
+ $ER = error_reporting(~E_NOTICE);
if ($db_result->count() > 0) {
- // Disable notices for unserializing
- $ER = error_reporting(~E_NOTICE);
-
foreach ($db_result as $row) {
// Add each cache to the array
$result[$row->key] = unserialize($row->cache);
}
-
- // Turn notices back on
- error_reporting($ER);
}
+ error_reporting($ER);
return $result;
}
@@ -120,9 +117,13 @@ class Cache_Database_Driver implements Cache_Driver {
* @param string cache id
* @return mixed|NULL
*/
- public function get($id) {
+ public function get($keys, $single=false) {
$data = null;
- $result = $this->db->getwhere("caches", array("key" => $id));
+ $result = db::build()
+ ->select()
+ ->from("caches")
+ ->where("key", "IN", $keys)
+ ->execute();
if (count($result) > 0) {
$cache = $result->current();
@@ -153,31 +154,47 @@ class Cache_Database_Driver implements Cache_Driver {
* @return bool
*/
public function delete($id, $tag = false) {
- $this->db->from("caches");
+ $db = db::build()
+ ->delete("caches");
if ($id === true) {
- $this->db->where(1);
// Delete all caches
+ $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;
}
/**
+ * Delete cache items by tag
+ */
+ public function delete_tag($tags) {
+ return $this->delete($tags, true);
+ }
+
+ /**
* Deletes all cache files that are older than the current time.
*/
public function delete_expired() {
// Delete all expired caches
- $status = $this->db->from("caches")
- ->where(array("expiration !=" => 0, "expiration <=" => time()))
- ->delete();
+ $status = db::build()
+ ->delete("caches")
+ ->where("expiration", "<>", 0)
+ ->where("expiration", "<=", time())
+ ->execute();
return count($status) > 0;
}
-} // End Cache Database Driver \ No newline at end of file
+ /**
+ * Empty the cache
+ */
+ public function delete_all() {
+ Database::instance()->query("TRUNCATE {caches}");
+ }
+} \ No newline at end of file