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