PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 1.0.5
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v1.0.5
5.12.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 6 years ago BaseSettingsTable.php 6 years ago Cache.php 6 years ago Config.php 6 years ago MeasurableSettingsTable.php 6 years ago NullBackend.php 6 years ago PluginSettingsTable.php 6 years ago SitesTable.php 6 years ago
MeasurableSettingsTable.php
199 lines
1 <?php
2 /**
3 * Piwik - 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\Concurrency\Lock;
14 use Piwik\Container\StaticContainer;
15 use Piwik\Db;
16 use Exception;
17 use Piwik\Version;
18
19 /**
20 * Measurable settings backend. Stores all settings in a "site_setting" database table.
21 *
22 * If a value that needs to be stored is an array, will insert a new row for each value of this array.
23 */
24 class MeasurableSettingsTable extends BaseSettingsTable
25 {
26 /**
27 * @var int
28 */
29 private $idSite;
30
31 /**
32 * @var string
33 */
34 private $pluginName;
35
36 public function __construct($idSite, $pluginName)
37 {
38 parent::__construct();
39
40 if (empty($pluginName)) {
41 throw new Exception('No plugin name given for MeasurableSettingsTable backend');
42 }
43
44 if (empty($idSite)) {
45 throw new Exception('No idSite given for MeasurableSettingsTable backend');
46 }
47
48 $this->idSite = (int) $idSite;
49 $this->pluginName = $pluginName;
50 }
51
52 /**
53 * Saves (persists) the current setting values in the database.
54 * @param array $values Key/value pairs of setting values to be written
55 */
56 public function save($values)
57 {
58 $this->initDbIfNeeded();
59
60 $valuesKeep = array();
61 foreach ($values as $name => $value) {
62 if (!isset($value)) {
63 continue;
64 }
65 if (is_array($value) || is_object($value)) {
66 $jsonEncoded = 1;
67 $value = json_encode($value);
68 } else {
69 $jsonEncoded = 0;
70 if (is_bool($value)) {
71 // we are currently not storing booleans as json as it could result in trouble with the UI and regress
72 // preselecting the correct value
73 $value = (int) $value;
74 }
75 }
76 $valuesKeep[] = array($this->idSite, $this->pluginName, $name, $value, $jsonEncoded);
77 }
78
79 $columns = array('idsite', 'plugin_name', 'setting_name', 'setting_value', 'json_encoded');
80
81 $table = $this->getTableName();
82 $lockKey = $this->getStorageId();
83 $this->lock->execute($lockKey, function() use ($valuesKeep, $table, $columns) {
84 $this->delete();
85 // No values = nothing to save
86 if (!empty($valuesKeep)) {
87 Db\BatchInsert::tableInsertBatchSql($table, $columns, $valuesKeep, true);
88 }
89 });
90 }
91
92 /**
93 * @inheritdoc
94 */
95 public function getStorageId()
96 {
97 return 'MeasurableSettings_' . $this->idSite . '_' . $this->pluginName;
98 }
99
100 private function jsonEncodedMissingError(Exception $e)
101 {
102 return strpos($e->getMessage(), 'json_encoded') !== false;
103 }
104
105 public function load()
106 {
107 $this->initDbIfNeeded();
108
109 $table = $this->getTableName();
110
111 $sql = "SELECT `setting_name`, `setting_value`, `json_encoded` FROM " . $table . " WHERE idsite = ? and plugin_name = ?";
112 $bind = array($this->idSite, $this->pluginName);
113
114 try {
115 $settings = $this->db->fetchAll($sql, $bind);
116 } catch (\Exception $e) {
117 // we catch an exception since json_encoded might not be present before matomo is updated to 3.5.0+ but the updater
118 // may run this query
119 if ($this->jsonEncodedMissingError($e)) {
120 $sql = "SELECT `setting_name`, `setting_value` FROM " . $table . " WHERE idsite = ? and plugin_name = ?";
121 $settings = $this->db->fetchAll($sql, $bind);
122 } else {
123 throw $e;
124 }
125
126 }
127
128 $flat = array();
129 foreach ($settings as $setting) {
130 $name = $setting['setting_name'];
131
132 if (!empty($setting['json_encoded'])) {
133 $flat[$name] = json_decode($setting['setting_value'], true);
134 } elseif (array_key_exists($name, $flat)) {
135 if (!is_array($flat[$name])) {
136 $flat[$name] = array($flat[$name]);
137 }
138 $flat[$name][] = $setting['setting_value'];
139 } else {
140 $flat[$name] = $setting['setting_value'];
141 }
142 }
143
144 return $flat;
145 }
146
147 protected function getTableName()
148 {
149 return Common::prefixTable('site_setting');
150 }
151
152 public function delete()
153 {
154 $this->initDbIfNeeded();
155
156 $table = $this->getTableName();
157 $sql = "DELETE FROM $table WHERE `idsite` = ? and plugin_name = ?";
158 $bind = array($this->idSite, $this->pluginName);
159
160 $this->db->query($sql, $bind);
161 }
162
163 /**
164 * @internal
165 * @param int $idSite
166 * @throws \Exception
167 */
168 public static function removeAllSettingsForSite($idSite)
169 {
170 try {
171 $query = sprintf('DELETE FROM %s WHERE idsite = ?', Common::prefixTable('site_setting'));
172 Db::query($query, array($idSite));
173 } catch (Exception $e) {
174 if ($e->getCode() != 42) {
175 // ignore table not found error, which might occur when updating from an older version of Piwik
176 throw $e;
177 }
178 }
179 }
180
181 /**
182 * @internal
183 * @param string $pluginName
184 * @throws \Exception
185 */
186 public static function removeAllSettingsForPlugin($pluginName)
187 {
188 try {
189 $query = sprintf('DELETE FROM %s WHERE plugin_name = ?', Common::prefixTable('site_setting'));
190 Db::query($query, array($pluginName));
191 } catch (Exception $e) {
192 if ($e->getCode() != 42) {
193 // ignore table not found error, which might occur when updating from an older version of Piwik
194 throw $e;
195 }
196 }
197 }
198 }
199