PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.8.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.8.2
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 / Changes / Model.php
matomo / app / core / Changes Last commit date
Model.php 3 months ago UserChanges.php 3 months ago
Model.php
197 lines
1 <?php
2
3 /**
4 * Matomo - free/libre analytics platform
5 *
6 * @link https://matomo.org
7 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8 */
9 namespace Piwik\Changes;
10
11 use Piwik\Log\LoggerInterface;
12 use Piwik\Piwik;
13 use Piwik\Common;
14 use Piwik\Date;
15 use Piwik\Db;
16 use Piwik\Tracker\Db\DbException;
17 use Piwik\Updater\Migration;
18 use Piwik\Container\StaticContainer;
19 use Piwik\Plugin\Manager as PluginManager;
20 /**
21 * Change model class
22 *
23 * Handle all data access operations for changes
24 *
25 */
26 class Model
27 {
28 public const NO_CHANGES_EXIST = 0;
29 public const CHANGES_EXIST = 1;
30 public const NEW_CHANGES_EXIST = 2;
31 /**
32 * @var Db\AdapterInterface
33 */
34 private $db;
35 /** @var array */
36 private $changeItems = null;
37 public function __construct()
38 {
39 $this->db = Db::get();
40 }
41 /**
42 * Add any new changes for a plugin to the changes table
43 *
44 *
45 * @throws \Exception
46 */
47 public function addChanges(string $pluginName) : void
48 {
49 $pluginManager = PluginManager::getInstance();
50 if ($pluginManager && $pluginManager->isValidPluginName($pluginName) && $pluginManager->isPluginInFilesystem($pluginName) && $pluginManager->isPluginActivated($pluginName)) {
51 $plugin = $pluginManager->loadPlugin($pluginName);
52 if (!$plugin) {
53 return;
54 }
55 $changes = $plugin->getChanges();
56 foreach ($changes as $change) {
57 $this->addChange($pluginName, $change);
58 }
59 }
60 }
61 /**
62 * Remove all changes for a plugin
63 *
64 */
65 public function removeChanges(string $pluginName) : void
66 {
67 $table = Common::prefixTable('changes');
68 try {
69 $this->db->query("DELETE FROM `" . $table . "` WHERE plugin_name = ?", [$pluginName]);
70 } catch (\Exception $e) {
71 if (Db::get()->isErrNo($e, Migration\Db::ERROR_CODE_TABLE_NOT_EXISTS)) {
72 return;
73 }
74 throw $e;
75 }
76 }
77 /**
78 * Add a change item to the database table
79 *
80 * @param array $change
81 */
82 public function addChange(string $pluginName, array $change) : void
83 {
84 if (!isset($change['version']) || !isset($change['title']) || !isset($change['description'])) {
85 StaticContainer::get(LoggerInterface::class)->warning("Change item for plugin {plugin} missing version, title or description fields - ignored", ['plugin' => $pluginName]);
86 return;
87 }
88 $table = Common::prefixTable('changes');
89 $fields = ['created_time', 'plugin_name', 'version', 'title', 'description'];
90 $params = [Date::now()->getDatetime(), $pluginName, $change['version'], $change['title'], $change['description']];
91 if (isset($change['link_name']) && isset($change['link'])) {
92 $fields[] = 'link_name';
93 $fields[] = 'link';
94 $params[] = $change['link_name'];
95 $params[] = $change['link'];
96 }
97 $insertSql = 'INSERT IGNORE INTO ' . $table . ' (' . implode(',', $fields) . ')
98 VALUES (' . Common::getSqlStringFieldsArray($params) . ')';
99 try {
100 $this->db->query($insertSql, $params);
101 } catch (\Exception $e) {
102 if (Db::get()->isErrNo($e, Migration\Db::ERROR_CODE_TABLE_NOT_EXISTS)) {
103 return;
104 }
105 throw $e;
106 }
107 }
108 /**
109 * Check if any changes items exist
110 *
111 * @param int|null $newerThanId Only count new changes as having a key > than this sequential key
112 *
113 */
114 public function doChangesExist(?int $newerThanId = null) : int
115 {
116 $changeItems = $this->getChangeItems();
117 $all = 0;
118 $new = 0;
119 foreach ($changeItems as $c) {
120 $all++;
121 if ($newerThanId === null || isset($c['idchange']) && $c['idchange'] > $newerThanId) {
122 $new++;
123 }
124 }
125 if ($all === 0) {
126 return self::NO_CHANGES_EXIST;
127 } elseif ($all > 0 && $new === 0) {
128 return self::CHANGES_EXIST;
129 } else {
130 return self::NEW_CHANGES_EXIST;
131 }
132 }
133 /**
134 * Get count of new changes
135 *
136 * @param int|null $newerThanId Only count new changes as having a key > than this sequential key
137 *
138 */
139 public function getNewChangesCount(?int $newerThanId = null) : int
140 {
141 $changes = $this->getChangeItems();
142 $new = 0;
143 foreach ($changes as $c) {
144 if ($newerThanId === null || isset($c['idchange']) && $c['idchange'] > $newerThanId) {
145 $new++;
146 }
147 }
148 return $new;
149 }
150 /**
151 * Return an array of change items for the last 6 months from the changes table
152 *
153 * @return array
154 * @throws DbException
155 */
156 public function getChangeItems() : array
157 {
158 if ($this->changeItems !== null) {
159 return $this->changeItems;
160 }
161 $table = Common::prefixTable('changes');
162 $selectSql = "SELECT * FROM `" . $table . "` WHERE title IS NOT NULL AND created_time > ? ORDER BY idchange DESC";
163 try {
164 $changes = $this->db->fetchAll($selectSql, [Date::now()->subMonth(6)]);
165 } catch (\Exception $e) {
166 if (Db::get()->isErrNo($e, Migration\Db::ERROR_CODE_TABLE_NOT_EXISTS)) {
167 return [];
168 }
169 throw $e;
170 }
171 array_walk($changes, function (&$change) {
172 $change['idchange'] = (int) $change['idchange'];
173 });
174 /**
175 * Event triggered before changes are displayed
176 *
177 * Can be used to filter out unwanted changes
178 *
179 * **Example**
180 *
181 * Piwik::addAction('Changes.filterChanges', function ($changes) {
182 * foreach ($changes as $k => $c) {
183 * // Hide changes for the CoreHome plugin
184 * if (isset($c['plugin_name']) && $c['plugin_name'] == 'CoreHome') {
185 * unset($changes[$k]);
186 * }
187 * }
188 * });
189 *
190 * @param array &$changes
191 */
192 Piwik::postEvent('Changes.filterChanges', array(&$changes));
193 $this->changeItems = $changes;
194 return $this->changeItems;
195 }
196 }
197