PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.12.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.12.1
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 / DataAccess / ArchiveTableCreator.php
matomo / app / core / DataAccess Last commit date
LogQueryBuilder 2 weeks ago Actions.php 8 months ago ArchiveSelector.php 2 weeks ago ArchiveTableCreator.php 2 weeks ago ArchiveTableDao.php 2 weeks ago ArchiveWriter.php 2 weeks ago ArchivingDbAdapter.php 2 weeks ago LogAggregator.php 2 weeks ago LogQueryBuilder.php 2 weeks ago LogTableTemporary.php 2 years ago Model.php 2 weeks ago RawLogDao.php 2 weeks ago TableMetadata.php 1 year ago
ArchiveTableCreator.php
181 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\DataAccess;
10
11 use Piwik\Common;
12 use Piwik\Date;
13 class ArchiveTableCreator
14 {
15 public const NUMERIC_TABLE = "numeric";
16 public const BLOB_TABLE = "blob";
17 /**
18 * @var list<string>|null
19 */
20 public static $tablesAlreadyInstalled = null;
21 /**
22 * Returns the prefixed numeric archive table name for the given date.
23 *
24 * In Matomo 5, omitting `$createIfMissing` keeps the legacy behavior and creates missing archive tables,
25 * but this is deprecated and will change in Matomo 6. Pass `true` to explicitly create missing tables or
26 * `false` to only return an existing table.
27 *
28 * @phpstan-return ($createIfMissing is false ? string|null : string)
29 */
30 public static function getNumericTable(Date $date, ?bool $createIfMissing = null) : ?string
31 {
32 return self::getTable($date, self::NUMERIC_TABLE, $createIfMissing);
33 }
34 /**
35 * Returns the prefixed blob archive table name for the given date.
36 *
37 * In Matomo 5, omitting `$createIfMissing` keeps the legacy behavior and creates missing archive tables,
38 * but this is deprecated and will change in Matomo 6. Pass `true` to explicitly create missing tables or
39 * `false` to only return an existing table.
40 *
41 * @phpstan-return ($createIfMissing is false ? string|null : string)
42 */
43 public static function getBlobTable(Date $date, ?bool $createIfMissing = null) : ?string
44 {
45 return self::getTable($date, self::BLOB_TABLE, $createIfMissing);
46 }
47 /**
48 * @phpstan-return ($createIfMissing is false ? string|null : string)
49 */
50 protected static function getTable(Date $date, string $type, ?bool $createIfMissing = null) : ?string
51 {
52 if ($createIfMissing === null) {
53 self::triggerLegacyDefaultDeprecation($type);
54 $createIfMissing = \true;
55 }
56 if ($createIfMissing) {
57 return self::ensureArchiveTableExists($date, $type);
58 }
59 return self::getExistingTable($date, $type);
60 }
61 protected static function getExistingTable(Date $date, string $type) : ?string
62 {
63 $tableNamePrefix = "archive_" . $type;
64 $tableName = $tableNamePrefix . "_" . self::getTableMonthFromDate($date);
65 $tableName = Common::prefixTable($tableName);
66 if (is_null(self::$tablesAlreadyInstalled)) {
67 self::refreshTableList();
68 }
69 if (!in_array($tableName, self::$tablesAlreadyInstalled)) {
70 self::refreshTableList();
71 if (!in_array($tableName, self::$tablesAlreadyInstalled)) {
72 return null;
73 }
74 }
75 return $tableName;
76 }
77 protected static function ensureArchiveTableExists(Date $date, string $type) : string
78 {
79 $tableNamePrefix = "archive_" . $type;
80 $tableName = $tableNamePrefix . "_" . self::getTableMonthFromDate($date);
81 $tableName = Common::prefixTable($tableName);
82 self::createArchiveTablesIfAbsent($tableName, $tableNamePrefix);
83 return $tableName;
84 }
85 /**
86 * @deprecated Will be removed in Matomo 6 once omitted get*Table() calls stop using the legacy default behavior.
87 */
88 private static function triggerLegacyDefaultDeprecation(string $type) : void
89 {
90 $method = $type === self::NUMERIC_TABLE ? 'getNumericTable' : 'getBlobTable';
91 trigger_error(sprintf("Omitting \$createIfMissing in ArchiveTableCreator::%s() is deprecated. Pass true to create missing archive tables or false to only return existing ones. In Matomo 6 the default behavior will change to lookup-only.", $method), \E_USER_DEPRECATED);
92 }
93 protected static function createArchiveTablesIfAbsent($tableName, $tableNamePrefix)
94 {
95 if (is_null(self::$tablesAlreadyInstalled)) {
96 self::refreshTableList();
97 }
98 if (!in_array($tableName, self::$tablesAlreadyInstalled)) {
99 self::getModel()->createArchiveTable($tableName, $tableNamePrefix);
100 self::$tablesAlreadyInstalled[] = $tableName;
101 }
102 }
103 private static function getModel()
104 {
105 return new \Piwik\DataAccess\Model();
106 }
107 public static function clear()
108 {
109 self::$tablesAlreadyInstalled = null;
110 }
111 public static function refreshTableList()
112 {
113 self::$tablesAlreadyInstalled = self::getModel()->getInstalledArchiveTables();
114 }
115 /**
116 * Returns all table names archive_*
117 *
118 * @param string|null $type The type of table to return. Either `self::NUMERIC_TABLE` or `self::BLOB_TABLE`,
119 * or null to return both.
120 * @param bool $forceReload
121 * @return array
122 */
123 public static function getTablesArchivesInstalled($type = null, $forceReload = \false)
124 {
125 if (is_null(self::$tablesAlreadyInstalled) || $forceReload) {
126 self::refreshTableList();
127 }
128 if (empty($type)) {
129 return self::$tablesAlreadyInstalled;
130 } else {
131 $tableMatchRegex = '/archive_' . preg_quote($type) . '_/';
132 }
133 $archiveTables = array();
134 foreach (self::$tablesAlreadyInstalled as $table) {
135 if (preg_match($tableMatchRegex, $table)) {
136 $archiveTables[] = $table;
137 }
138 }
139 return $archiveTables;
140 }
141 /**
142 * Returns the latest table name archive_*.
143 * If no type is specified, blob table is returned when both blob and numeric are found for the same year_month.
144 *
145 * @param string|null $type The type of archive table to return. Either `self::NUMERIC_TABLE` or `self::BLOB_TABLE`.
146 */
147 public static function getLatestArchiveTableInstalled(?string $type = null, bool $forceReload = \false) : ?string
148 {
149 $archiveTables = static::getTablesArchivesInstalled($type, $forceReload);
150 // skip if there is no archive table (yet)
151 if (0 === count($archiveTables)) {
152 return null;
153 }
154 // sort tables so we have them in descending order of their date
155 usort($archiveTables, function ($a, $b) {
156 return static::getDateFromTableName($b) <=> static::getDateFromTableName($a);
157 });
158 return $archiveTables[0];
159 }
160 public static function getDateFromTableName($tableName)
161 {
162 $tableName = Common::unprefixTable($tableName);
163 $date = str_replace(array('archive_numeric_', 'archive_blob_'), '', $tableName);
164 return $date;
165 }
166 public static function getTableMonthFromDate(Date $date)
167 {
168 return $date->toString('Y_m');
169 }
170 public static function getTypeFromTableName($tableName)
171 {
172 if (strpos($tableName, 'archive_numeric_') !== \false) {
173 return self::NUMERIC_TABLE;
174 }
175 if (strpos($tableName, 'archive_blob_') !== \false) {
176 return self::BLOB_TABLE;
177 }
178 return \false;
179 }
180 }
181