Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
32 / 32 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| db_text | |
100.00% |
32 / 32 |
|
100.00% |
7 / 7 |
11 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| set | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| delete | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| set_array | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
3 | |||
| get_array | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| delete_array | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * |
| 4 | * This file is part of the phpBB Forum Software package. |
| 5 | * |
| 6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
| 7 | * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | * |
| 9 | * For full copyright and license information, please see |
| 10 | * the docs/CREDITS.txt file. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | namespace phpbb\config; |
| 15 | |
| 16 | /** |
| 17 | * Manages configuration options with an arbitrary length value stored in a TEXT |
| 18 | * column. In constrast to class \phpbb\config\db, values are never cached and |
| 19 | * prefetched, but every get operation sends a query to the database. |
| 20 | */ |
| 21 | class db_text |
| 22 | { |
| 23 | /** |
| 24 | * Database connection |
| 25 | * @var \phpbb\db\driver\driver_interface |
| 26 | */ |
| 27 | protected $db; |
| 28 | |
| 29 | /** |
| 30 | * Name of the database table used. |
| 31 | * @var string |
| 32 | */ |
| 33 | protected $table; |
| 34 | |
| 35 | /** |
| 36 | * @param \phpbb\db\driver\driver_interface $db Database connection |
| 37 | * @param string $table Table name |
| 38 | */ |
| 39 | public function __construct(\phpbb\db\driver\driver_interface $db, $table) |
| 40 | { |
| 41 | $this->db = $db; |
| 42 | $this->table = $this->db->sql_escape($table); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Sets the configuration option with the name $key to $value. |
| 47 | * |
| 48 | * @param string $key The configuration option's name |
| 49 | * @param string $value New configuration value |
| 50 | * |
| 51 | * @return void |
| 52 | */ |
| 53 | public function set($key, $value) |
| 54 | { |
| 55 | $this->set_array(array($key => $value)); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Gets the configuration value for the name $key. |
| 60 | * |
| 61 | * @param string $key The configuration option's name |
| 62 | * |
| 63 | * @return string|null String result on success |
| 64 | * null if there is no such option |
| 65 | */ |
| 66 | public function get($key) |
| 67 | { |
| 68 | $map = $this->get_array(array($key)); |
| 69 | |
| 70 | return isset($map[$key]) ? $map[$key] : null; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Removes the configuration option with the name $key. |
| 75 | * |
| 76 | * @param string $key The configuration option's name |
| 77 | * |
| 78 | * @return void |
| 79 | */ |
| 80 | public function delete($key) |
| 81 | { |
| 82 | $this->delete_array(array($key)); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Mass set configuration options: Receives an associative array, |
| 87 | * treats array keys as configuration option names and associated |
| 88 | * array values as their configuration option values. |
| 89 | * |
| 90 | * @param array $map Map from configuration names to values |
| 91 | * |
| 92 | * @return void |
| 93 | */ |
| 94 | public function set_array(array $map) |
| 95 | { |
| 96 | $this->db->sql_transaction('begin'); |
| 97 | |
| 98 | foreach ($map as $key => $value) |
| 99 | { |
| 100 | $sql = 'UPDATE ' . $this->table . " |
| 101 | SET config_value = '" . $this->db->sql_escape($value) . "' |
| 102 | WHERE config_name = '" . $this->db->sql_escape($key) . "'"; |
| 103 | $this->db->sql_query($sql); |
| 104 | |
| 105 | if (!$this->db->sql_affectedrows()) |
| 106 | { |
| 107 | $sql = 'INSERT INTO ' . $this->table . ' ' . $this->db->sql_build_array('INSERT', array( |
| 108 | 'config_name' => (string) $key, |
| 109 | 'config_value' => (string) $value, |
| 110 | )); |
| 111 | $this->db->sql_query($sql); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | $this->db->sql_transaction('commit'); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Mass get configuration options: Receives a set of configuration |
| 120 | * option names and returns the result as a key => value map where |
| 121 | * array keys are configuration option names and array values are |
| 122 | * associated config option values. |
| 123 | * |
| 124 | * @param array $keys Set of configuration option names |
| 125 | * |
| 126 | * @return array Map from configuration names to values |
| 127 | */ |
| 128 | public function get_array(array $keys) |
| 129 | { |
| 130 | $sql = 'SELECT * |
| 131 | FROM ' . $this->table . ' |
| 132 | WHERE ' . $this->db->sql_in_set('config_name', $keys, false, true); |
| 133 | $result = $this->db->sql_query($sql); |
| 134 | |
| 135 | $map = array(); |
| 136 | while ($row = $this->db->sql_fetchrow($result)) |
| 137 | { |
| 138 | $map[$row['config_name']] = $row['config_value']; |
| 139 | } |
| 140 | $this->db->sql_freeresult($result); |
| 141 | |
| 142 | return $map; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Mass delete configuration options. |
| 147 | * |
| 148 | * @param array $keys Set of configuration option names |
| 149 | * |
| 150 | * @return void |
| 151 | */ |
| 152 | public function delete_array(array $keys) |
| 153 | { |
| 154 | $sql = 'DELETE |
| 155 | FROM ' . $this->table . ' |
| 156 | WHERE ' . $this->db->sql_in_set('config_name', $keys, false, true); |
| 157 | $this->db->sql_query($sql); |
| 158 | } |
| 159 | } |