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