1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
<?php
/**
*
* @package phpBB3
* @copyright (c) 2010 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
namespace phpbb\config;
/**
* Configuration container class
* @package phpBB3
*/
class db extends \phpbb\config\config
{
/**
* Cache instance
* @var \phpbb\cache\driver\driver_interface
*/
protected $cache;
/**
* Database connection
* @var \phpbb\db\driver\driver
*/
protected $db;
/**
* Name of the database table used for configuration.
* @var string
*/
protected $table;
/**
* Creates a configuration container with a default set of values
*
* @param \phpbb\db\driver\driver $db Database connection
* @param \phpbb\cache\driver\driver_interface $cache Cache instance
* @param string $table Configuration table name
*/
public function __construct(\phpbb\db\driver\driver $db, \phpbb\cache\driver\driver_interface $cache, $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);
while ($row = $this->db->sql_fetchrow($result))
{
$config[$row['config_name']] = $row['config_value'];
}
$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);
}
/**
* Removes a configuration option
*
* @param String $key The configuration option's name
* @param bool $use_cache Whether this variable should be cached or if it
* changes too frequently to be efficiently cached
* @return null
*/
public function delete($key, $use_cache = true)
{
$sql = 'DELETE FROM ' . $this->table . "
WHERE config_name = '" . $this->db->sql_escape($key) . "'";
$this->db->sql_query($sql);
unset($this->config[$key]);
if ($use_cache)
{
$this->cache->destroy('config');
}
}
/**
* Sets a configuration option's value
*
* @param string $key The configuration option's name
* @param string $value New configuration value
* @param bool $use_cache Whether this variable should be cached or if it
* changes too frequently to be efficiently cached.
*/
public function set($key, $value, $use_cache = true)
{
$this->set_atomic($key, false, $value, $use_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 $use_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, $use_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' => ($use_cache) ? 0 : 1));
$this->db->sql_query($sql);
}
if ($use_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 $use_cache Whether this variable should be cached or if it
* changes too frequently to be efficiently cached.
*/
function increment($key, $increment, $use_cache = true)
{
if (!isset($this->config[$key]))
{
$this->set($key, '0', $use_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 ($use_cache)
{
$this->cache->destroy('config');
}
$this->config[$key] += $increment;
}
}
|