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