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