PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.3.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.3.0
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / Settings / Storage / Backend / MeasurableSettingsTable.php
matomo / app / core / Settings / Storage / Backend Last commit date
BackendInterface.php 5 years ago BaseSettingsTable.php 6 years ago Cache.php 5 years ago Config.php 5 years ago MeasurableSettingsTable.php 5 years ago NullBackend.php 5 years ago PluginSettingsTable.php 5 years ago SitesTable.php 5 years ago
MeasurableSettingsTable.php
196 lines
1 <?php
2 /**
3 * Matomo - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 *
8 */
9
10 namespace Piwik\Settings\Storage\Backend;
11
12 use Piwik\Common;
13 use Piwik\Db;
14 use Exception;
15
16 /**
17 * Measurable settings backend. Stores all settings in a "site_setting" database table.
18 *
19 * If a value that needs to be stored is an array, will insert a new row for each value of this array.
20 */
21 class MeasurableSettingsTable extends BaseSettingsTable
22 {
23 /**
24 * @var int
25 */
26 private $idSite;
27
28 /**
29 * @var string
30 */
31 private $pluginName;
32
33 public function __construct($idSite, $pluginName)
34 {
35 parent::__construct();
36
37 if (empty($pluginName)) {
38 throw new Exception('No plugin name given for MeasurableSettingsTable backend');
39 }
40
41 if (empty($idSite)) {
42 throw new Exception('No idSite given for MeasurableSettingsTable backend');
43 }
44
45 $this->idSite = (int) $idSite;
46 $this->pluginName = $pluginName;
47 }
48
49 /**
50 * Saves (persists) the current setting values in the database.
51 * @param array $values Key/value pairs of setting values to be written
52 */
53 public function save($values)
54 {
55 $this->initDbIfNeeded();
56
57 $valuesKeep = array();
58 foreach ($values as $name => $value) {
59 if (!isset($value)) {
60 continue;
61 }
62 if (is_array($value) || is_object($value)) {
63 $jsonEncoded = 1;
64 $value = json_encode($value);
65 } else {
66 $jsonEncoded = 0;
67 if (is_bool($value)) {
68 // we are currently not storing booleans as json as it could result in trouble with the UI and regress
69 // preselecting the correct value
70 $value = (int) $value;
71 }
72 }
73 $valuesKeep[] = array($this->idSite, $this->pluginName, $name, $value, $jsonEncoded);
74 }
75
76 $columns = array('idsite', 'plugin_name', 'setting_name', 'setting_value', 'json_encoded');
77
78 $table = $this->getTableName();
79 $lockKey = $this->getStorageId();
80 $this->lock->execute($lockKey, function() use ($valuesKeep, $table, $columns) {
81 $this->delete();
82 // No values = nothing to save
83 if (!empty($valuesKeep)) {
84 Db\BatchInsert::tableInsertBatchSql($table, $columns, $valuesKeep, true);
85 }
86 });
87 }
88
89 /**
90 * @inheritdoc
91 */
92 public function getStorageId()
93 {
94 return 'MeasurableSettings_' . $this->idSite . '_' . $this->pluginName;
95 }
96
97 private function jsonEncodedMissingError(Exception $e)
98 {
99 return strpos($e->getMessage(), 'json_encoded') !== false;
100 }
101
102 public function load()
103 {
104 $this->initDbIfNeeded();
105
106 $table = $this->getTableName();
107
108 $sql = "SELECT `setting_name`, `setting_value`, `json_encoded` FROM " . $table . " WHERE idsite = ? and plugin_name = ?";
109 $bind = array($this->idSite, $this->pluginName);
110
111 try {
112 $settings = $this->db->fetchAll($sql, $bind);
113 } catch (\Exception $e) {
114 // we catch an exception since json_encoded might not be present before matomo is updated to 3.5.0+ but the updater
115 // may run this query
116 if ($this->jsonEncodedMissingError($e)) {
117 $sql = "SELECT `setting_name`, `setting_value` FROM " . $table . " WHERE idsite = ? and plugin_name = ?";
118 $settings = $this->db->fetchAll($sql, $bind);
119 } else {
120 throw $e;
121 }
122
123 }
124
125 $flat = array();
126 foreach ($settings as $setting) {
127 $name = $setting['setting_name'];
128
129 if (!empty($setting['json_encoded'])) {
130 $flat[$name] = json_decode($setting['setting_value'], true);
131 } elseif (array_key_exists($name, $flat)) {
132 if (!is_array($flat[$name])) {
133 $flat[$name] = array($flat[$name]);
134 }
135 $flat[$name][] = $setting['setting_value'];
136 } else {
137 $flat[$name] = $setting['setting_value'];
138 }
139 }
140
141 return $flat;
142 }
143
144 protected function getTableName()
145 {
146 return Common::prefixTable('site_setting');
147 }
148
149 public function delete()
150 {
151 $this->initDbIfNeeded();
152
153 $table = $this->getTableName();
154 $sql = "DELETE FROM $table WHERE `idsite` = ? and plugin_name = ?";
155 $bind = array($this->idSite, $this->pluginName);
156
157 $this->db->query($sql, $bind);
158 }
159
160 /**
161 * @internal
162 * @param int $idSite
163 * @throws \Exception
164 */
165 public static function removeAllSettingsForSite($idSite)
166 {
167 try {
168 $query = sprintf('DELETE FROM %s WHERE idsite = ?', Common::prefixTable('site_setting'));
169 Db::query($query, array($idSite));
170 } catch (Exception $e) {
171 if ($e->getCode() != 42) {
172 // ignore table not found error, which might occur when updating from an older version of Piwik
173 throw $e;
174 }
175 }
176 }
177
178 /**
179 * @internal
180 * @param string $pluginName
181 * @throws \Exception
182 */
183 public static function removeAllSettingsForPlugin($pluginName)
184 {
185 try {
186 $query = sprintf('DELETE FROM %s WHERE plugin_name = ?', Common::prefixTable('site_setting'));
187 Db::query($query, array($pluginName));
188 } catch (Exception $e) {
189 if ($e->getCode() != 42) {
190 // ignore table not found error, which might occur when updating from an older version of Piwik
191 throw $e;
192 }
193 }
194 }
195 }
196