diff options
author | robocoder <anthon.pang@gmail.com> | 2011-03-05 21:28:08 +0300 |
---|---|---|
committer | robocoder <anthon.pang@gmail.com> | 2011-03-05 21:28:08 +0300 |
commit | 6f7dccc9c68e18c834246ce472e0f70b3844e000 (patch) | |
tree | cbb7496f777f40e4e4fb8f5a7dff5f28f9db53a2 /core/Option.php | |
parent | 410da5dae2ca94a97faeb924414360e381bf277d (diff) |
fixes #2150
git-svn-id: http://dev.piwik.org/svn/trunk@4030 59fd770c-687e-43c8-a1e3-f5a4ff64c105
Diffstat (limited to 'core/Option.php')
-rw-r--r-- | core/Option.php | 81 |
1 files changed, 75 insertions, 6 deletions
diff --git a/core/Option.php b/core/Option.php index c8147a23ce..39e12c98d3 100644 --- a/core/Option.php +++ b/core/Option.php @@ -21,9 +21,13 @@ class Piwik_Option { private $all = array(); + private $loaded = false; static private $instance = null; + /** + * Singleton + * * @return Piwik_Option */ static public function getInstance() @@ -37,6 +41,12 @@ class Piwik_Option private function __construct() {} + /** + * Returns the option value for the requested option $name, fetching from database, if not in cache. + * + * @param string $name Key + * @return string|false Value or false, if not found + */ public function get($name) { $this->autoload(); @@ -55,6 +65,13 @@ class Piwik_Option return $value; } + /** + * Sets the option value in the database and cache + * + * @param string $name + * @param string $value + * @param int $autoload if set to 1, this option value will be automatically loaded; should be set to 1 for options that will always be used in the Piwik request. + */ public function set($name, $value, $autoload = 0) { $autoload = (int)$autoload; @@ -64,14 +81,64 @@ class Piwik_Option array($name, $value, $autoload, $value)); $this->all[$name] = $value; } - + + /** + * Delete key-value pair from database and reload cache. + * + * @param string $name Key to match exactly + * @param string $value Optional value + */ + public function delete($name, $value = null) + { + $sql = 'DELETE FROM '. Piwik_Common::prefixTable('option') . ' WHERE option_name = ?'; + $bind[] = $name; + + if(isset($value)) + { + $sql .= ' AND option_value = ?'; + $bind[] = $value; + } + + Piwik_Query($sql, $bind); + + $this->clearCache(); + } + + /** + * Delete key-value pair(s) from database and reload cache. + * The supplied pattern should use '%' as wildcards, and literal '_' should be escaped. + * + * @param string $name Pattern of key to match. + * @param string $value Optional value + */ + public function deleteLike($name, $value = null) + { + $sql = 'DELETE FROM `'. Piwik_Common::prefixTable('option') . '` WHERE option_name LIKE ?'; + $bind[] = $name; + + if(isset($value)) + { + $sql .= ' AND option_value = ?'; + $bind[] = $value; + } + + Piwik_Query($sql, $bind); + + $this->clearCache(); + } + + /** + * Initialize cache with autoload settings. + * + * @param bool $forceReload Forces a reload if true; default is false + */ private function autoload() { - static $loaded = false; - if($loaded) + if($this->loaded) { return; } + $all = Piwik_FetchAll('SELECT option_value, option_name FROM `'. Piwik_Common::prefixTable('option') . '` WHERE autoload = 1'); @@ -79,7 +146,8 @@ class Piwik_Option { $this->all[$option['option_name']] = $option['option_value']; } - $loaded = true; + + $this->loaded = true; } /** @@ -90,6 +158,7 @@ class Piwik_Option */ public function clearCache() { + $this->loaded = false; $this->all = array(); } } @@ -97,8 +166,8 @@ class Piwik_Option /** * Returns the option value for the requested option $name * - * @param string $name - * @return string|false if not found + * @param string $name Key + * @return string|false Value or false, if not found */ function Piwik_GetOption($name) { |