diff options
author | Nils Adermann <naderman@naderman.de> | 2011-01-10 02:27:18 +0100 |
---|---|---|
committer | Nils Adermann <naderman@naderman.de> | 2011-01-10 04:05:23 +0100 |
commit | fb2642bbc6360dacfd4a3cc9f7e9447b02cb46a1 (patch) | |
tree | e96dc907ae493b520beccc8823008903c81e86af /phpBB/includes/config/db.php | |
parent | 5ea59ba3222965c824284a7f0d0648bbe12a7bec (diff) | |
download | forums-fb2642bbc6360dacfd4a3cc9f7e9447b02cb46a1.tar forums-fb2642bbc6360dacfd4a3cc9f7e9447b02cb46a1.tar.gz forums-fb2642bbc6360dacfd4a3cc9f7e9447b02cb46a1.tar.bz2 forums-fb2642bbc6360dacfd4a3cc9f7e9447b02cb46a1.tar.xz forums-fb2642bbc6360dacfd4a3cc9f7e9447b02cb46a1.zip |
[task/config-class] Implemented a config class to replace the global array.
There is a phpbb_config class which simply holds an array and does not persist
any data. It implements ArrayAccess, Countable and IteratorAggregate to allow
regular use of configuration as if it was still an array. The phpbb_config_db
class depends on an instance of the dbal and a cache driver. It obtains the
configuration data from cache and database as necessary and persists data to
the database.
The functions set_config and set_config_count remain for backward compatability
but they only call methods on the new config class now instead of directly
manipulating the database and cache.
PHPBB3-9988
Diffstat (limited to 'phpBB/includes/config/db.php')
-rw-r--r-- | phpBB/includes/config/db.php | 175 |
1 files changed, 175 insertions, 0 deletions
diff --git a/phpBB/includes/config/db.php b/phpBB/includes/config/db.php new file mode 100644 index 0000000000..e6b7bffb73 --- /dev/null +++ b/phpBB/includes/config/db.php @@ -0,0 +1,175 @@ +<?php +/** +* +* @package phpBB3 +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ + exit; +} + +/** +* Configuration container class +* @package phpBB3 +*/ +class phpbb_config_db extends phpbb_config +{ + /** + * Cache instance + * @var phpbb_cache_driver_interface + */ + protected $cache; + + /** + * Database connection + * @var dbal + */ + protected $db; + + /** + * Creates a configuration container with a default set of values + * + * @param phpbb_cache_driver_interface $cache Cache instance + * @param dbal $db Database connection + * @param string $table Configuration table name + */ + public function __construct(phpbb_cache_driver_interface $cache, dbal $db, $table) + { + $this->db = $db; + $this->cache = $cache; + $this->table = $table; + + if (($config = $cache->get('config')) !== false) + { + $sql = 'SELECT config_name, config_value + FROM ' . $this->table . ' + WHERE is_dynamic = 1'; + $result = $this->db->sql_query($sql); + $config += $this->db->sql_fetchrowset($result); + $this->db->sql_freeresult($result); + } + else + { + $config = $cached_config = array(); + + $sql = 'SELECT config_name, config_value, is_dynamic + FROM ' . $this->table; + $result = $this->db->sql_query($sql); + + while ($row = $this->db->sql_fetchrow($result)) + { + if (!$row['is_dynamic']) + { + $cached_config[$row['config_name']] = $row['config_value']; + } + + $config[$row['config_name']] = $row['config_value']; + } + $this->db->sql_freeresult($result); + + $cache->put('config', $cached_config); + } + + parent::__construct($config); + } + + /** + * Sets a configuration option's value + * + * @param string $key The configuration option's name + * @param string $value New configuration value + * @param bool $cache Whether this variable should be cached or if it + * changes too frequently to be efficiently cached. + */ + public function set($key, $value, $cache = true) + { + $this->set_atomic($key, false, $value, $cache); + } + + /** + * Sets a configuration option's value only if the old_value matches the + * current configuration value or the configuration value does not exist yet. + * + * @param string $key The configuration option's name + * @param mixed $old_value Current configuration value or false to ignore + * the old value + * @param string $new_value New configuration value + * @param bool $cache Whether this variable should be cached or if it + * changes too frequently to be efficiently cached + * @return bool True if the value was changed, false otherwise + */ + public function set_atomic($key, $old_value, $new_value, $cache = true) + { + $sql = 'UPDATE ' . $this->table . " + SET config_value = '" . $this->db->sql_escape($new_value) . "' + WHERE config_name = '" . $this->db->sql_escape($key) . "'"; + + if ($old_value !== false) + { + $sql .= " AND config_value = '" . $this->db->sql_escape($old_value) . "'"; + } + + $result = $this->db->sql_query($sql); + + if (!$this->db->sql_affectedrows($result) && isset($this->config[$key])) + { + return false; + } + + if (!isset($this->config[$key])) + { + $sql = 'INSERT INTO ' . $this->table . ' ' . $this->db->sql_build_array('INSERT', array( + 'config_name' => $key, + 'config_value' => $new_value, + 'is_dynamic' => ($cache) ? 0 : 1)); + $this->db->sql_query($sql); + } + + if ($cache) + { + $this->cache->destroy('config'); + } + + $this->config[$key] = $new_value; + return true; + } + + /** + * Increments an integer config value directly in the database. + * + * Using this method instead of setting the new value directly avoids race + * conditions and unlike set_atomic it cannot fail. + * + * @param string $key The configuration option's name + * @param int $increment Amount to increment by + * @param bool $cache Whether this variable should be cached or if it + * changes too frequently to be efficiently cached. + */ + function increment($key, $increment, $cache = true) + { + if (!isset($this->config[$key])) + { + $this->set($key, '0', $cache); + } + + $sql_update = $this->db->cast_expr_to_string($this->db->cast_expr_to_bigint('config_value') . ' + ' . (int) $increment); + + $this->db->sql_query('UPDATE ' . $this->table . ' + SET config_value = ' . $sql_update . " + WHERE config_name = '" . $this->db->sql_escape($key) . "'"); + + if ($cache) + { + $this->cache->destroy('config'); + } + + $this->config[$key] += $increment; + } +} |