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 / DataAccess / ArchiveTableCreator.php
matomo / app / core / DataAccess Last commit date
LogQueryBuilder 3 months ago Actions.php 7 months ago ArchiveSelector.php 1 month ago ArchiveTableCreator.php 1 month ago ArchiveTableDao.php 1 month ago ArchiveWriter.php 1 month ago ArchivingDbAdapter.php 1 year ago LogAggregator.php 1 month ago LogQueryBuilder.php 7 months ago LogTableTemporary.php 2 years ago Model.php 1 month ago RawLogDao.php 7 months ago TableMetadata.php 1 year ago
ArchiveTableCreator.php
180 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 $type The type of table to return. Either `self::NUMERIC_TABLE` or `self::BLOB_TABLE`.
119 * @param bool $forceReload
120 * @return array
121 */
122 public static function getTablesArchivesInstalled($type = null, $forceReload = \false)
123 {
124 if (is_null(self::$tablesAlreadyInstalled) || $forceReload) {
125 self::refreshTableList();
126 }
127 if (empty($type)) {
128 return self::$tablesAlreadyInstalled;
129 } else {
130 $tableMatchRegex = '/archive_' . preg_quote($type) . '_/';
131 }
132 $archiveTables = array();
133 foreach (self::$tablesAlreadyInstalled as $table) {
134 if (preg_match($tableMatchRegex, $table)) {
135 $archiveTables[] = $table;
136 }
137 }
138 return $archiveTables;
139 }
140 /**
141 * Returns the latest table name archive_*.
142 * If no type is specified, blob table is returned when both blob and numeric are found for the same year_month.
143 *
144 * @param string|null $type The type of archive table to return. Either `self::NUMERIC_TABLE` or `self::BLOB_TABLE`.
145 */
146 public static function getLatestArchiveTableInstalled(?string $type = null, bool $forceReload = \false) : ?string
147 {
148 $archiveTables = static::getTablesArchivesInstalled($type, $forceReload);
149 // skip if there is no archive table (yet)
150 if (0 === count($archiveTables)) {
151 return null;
152 }
153 // sort tables so we have them in descending order of their date
154 usort($archiveTables, function ($a, $b) {
155 return static::getDateFromTableName($b) <=> static::getDateFromTableName($a);
156 });
157 return $archiveTables[0];
158 }
159 public static function getDateFromTableName($tableName)
160 {
161 $tableName = Common::unprefixTable($tableName);
162 $date = str_replace(array('archive_numeric_', 'archive_blob_'), '', $tableName);
163 return $date;
164 }
165 public static function getTableMonthFromDate(Date $date)
166 {
167 return $date->toString('Y_m');
168 }
169 public static function getTypeFromTableName($tableName)
170 {
171 if (strpos($tableName, 'archive_numeric_') !== \false) {
172 return self::NUMERIC_TABLE;
173 }
174 if (strpos($tableName, 'archive_blob_') !== \false) {
175 return self::BLOB_TABLE;
176 }
177 return \false;
178 }
179 }
180