PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/phpBB/includes/config/db.php

http://github.com/phpbb/phpbb3
PHP | 207 lines | 107 code | 28 blank | 72 comment | 13 complexity | 08a7ecc6e99258157d08aacbaa0f1d7d MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package phpBB3
  5. * @copyright (c) 2010 phpBB Group
  6. * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
  7. *
  8. */
  9. /**
  10. * @ignore
  11. */
  12. if (!defined('IN_PHPBB'))
  13. {
  14. exit;
  15. }
  16. /**
  17. * Configuration container class
  18. * @package phpBB3
  19. */
  20. class phpbb_config_db extends phpbb_config
  21. {
  22. /**
  23. * Cache instance
  24. * @var phpbb_cache_driver_interface
  25. */
  26. protected $cache;
  27. /**
  28. * Database connection
  29. * @var dbal
  30. */
  31. protected $db;
  32. /**
  33. * Name of the database table used for configuration.
  34. * @var string
  35. */
  36. protected $table;
  37. /**
  38. * Creates a configuration container with a default set of values
  39. *
  40. * @param dbal $db Database connection
  41. * @param phpbb_cache_driver_interface $cache Cache instance
  42. * @param string $table Configuration table name
  43. */
  44. public function __construct(dbal $db, phpbb_cache_driver_interface $cache, $table)
  45. {
  46. $this->db = $db;
  47. $this->cache = $cache;
  48. $this->table = $table;
  49. if (($config = $cache->get('config')) !== false)
  50. {
  51. $sql = 'SELECT config_name, config_value
  52. FROM ' . $this->table . '
  53. WHERE is_dynamic = 1';
  54. $result = $this->db->sql_query($sql);
  55. while ($row = $this->db->sql_fetchrow($result))
  56. {
  57. $config[$row['config_name']] = $row['config_value'];
  58. }
  59. $this->db->sql_freeresult($result);
  60. }
  61. else
  62. {
  63. $config = $cached_config = array();
  64. $sql = 'SELECT config_name, config_value, is_dynamic
  65. FROM ' . $this->table;
  66. $result = $this->db->sql_query($sql);
  67. while ($row = $this->db->sql_fetchrow($result))
  68. {
  69. if (!$row['is_dynamic'])
  70. {
  71. $cached_config[$row['config_name']] = $row['config_value'];
  72. }
  73. $config[$row['config_name']] = $row['config_value'];
  74. }
  75. $this->db->sql_freeresult($result);
  76. $cache->put('config', $cached_config);
  77. }
  78. parent::__construct($config);
  79. }
  80. /**
  81. * Removes a configuration option
  82. *
  83. * @param String $key The configuration option's name
  84. * @param bool $use_cache Whether this variable should be cached or if it
  85. * changes too frequently to be efficiently cached
  86. * @return void
  87. */
  88. public function delete($key, $use_cache = true)
  89. {
  90. $sql = 'DELETE FROM ' . $this->table . "
  91. WHERE config_name = '" . $this->db->sql_escape($key) . "'";
  92. $this->db->sql_query($sql);
  93. unset($this->config[$key]);
  94. if ($use_cache)
  95. {
  96. $this->cache->destroy('config');
  97. }
  98. }
  99. /**
  100. * Sets a configuration option's value
  101. *
  102. * @param string $key The configuration option's name
  103. * @param string $value New configuration value
  104. * @param bool $use_cache Whether this variable should be cached or if it
  105. * changes too frequently to be efficiently cached.
  106. */
  107. public function set($key, $value, $use_cache = true)
  108. {
  109. $this->set_atomic($key, false, $value, $use_cache);
  110. }
  111. /**
  112. * Sets a configuration option's value only if the old_value matches the
  113. * current configuration value or the configuration value does not exist yet.
  114. *
  115. * @param string $key The configuration option's name
  116. * @param mixed $old_value Current configuration value or false to ignore
  117. * the old value
  118. * @param string $new_value New configuration value
  119. * @param bool $use_cache Whether this variable should be cached or if it
  120. * changes too frequently to be efficiently cached
  121. * @return bool True if the value was changed, false otherwise
  122. */
  123. public function set_atomic($key, $old_value, $new_value, $use_cache = true)
  124. {
  125. $sql = 'UPDATE ' . $this->table . "
  126. SET config_value = '" . $this->db->sql_escape($new_value) . "'
  127. WHERE config_name = '" . $this->db->sql_escape($key) . "'";
  128. if ($old_value !== false)
  129. {
  130. $sql .= " AND config_value = '" . $this->db->sql_escape($old_value) . "'";
  131. }
  132. $result = $this->db->sql_query($sql);
  133. if (!$this->db->sql_affectedrows($result) && isset($this->config[$key]))
  134. {
  135. return false;
  136. }
  137. if (!isset($this->config[$key]))
  138. {
  139. $sql = 'INSERT INTO ' . $this->table . ' ' . $this->db->sql_build_array('INSERT', array(
  140. 'config_name' => $key,
  141. 'config_value' => $new_value,
  142. 'is_dynamic' => ($use_cache) ? 0 : 1));
  143. $this->db->sql_query($sql);
  144. }
  145. if ($use_cache)
  146. {
  147. $this->cache->destroy('config');
  148. }
  149. $this->config[$key] = $new_value;
  150. return true;
  151. }
  152. /**
  153. * Increments an integer config value directly in the database.
  154. *
  155. * Using this method instead of setting the new value directly avoids race
  156. * conditions and unlike set_atomic it cannot fail.
  157. *
  158. * @param string $key The configuration option's name
  159. * @param int $increment Amount to increment by
  160. * @param bool $use_cache Whether this variable should be cached or if it
  161. * changes too frequently to be efficiently cached.
  162. */
  163. function increment($key, $increment, $use_cache = true)
  164. {
  165. if (!isset($this->config[$key]))
  166. {
  167. $this->set($key, '0', $use_cache);
  168. }
  169. $sql_update = $this->db->cast_expr_to_string($this->db->cast_expr_to_bigint('config_value') . ' + ' . (int) $increment);
  170. $this->db->sql_query('UPDATE ' . $this->table . '
  171. SET config_value = ' . $sql_update . "
  172. WHERE config_name = '" . $this->db->sql_escape($key) . "'");
  173. if ($use_cache)
  174. {
  175. $this->cache->destroy('config');
  176. }
  177. $this->config[$key] += $increment;
  178. }
  179. }