PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.8.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.8.2
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 6 months ago ArchiveSelector.php 3 months ago ArchiveTableCreator.php 3 months ago ArchiveTableDao.php 3 months ago ArchiveWriter.php 3 months ago ArchivingDbAdapter.php 1 year ago LogAggregator.php 3 months ago LogQueryBuilder.php 6 months ago LogTableTemporary.php 2 years ago Model.php 3 months ago RawLogDao.php 6 months ago TableMetadata.php 1 year ago
ArchiveTableCreator.php
121 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 public static $tablesAlreadyInstalled = null;
18 public static function getNumericTable(Date $date)
19 {
20 return self::getTable($date, self::NUMERIC_TABLE);
21 }
22 public static function getBlobTable(Date $date)
23 {
24 return self::getTable($date, self::BLOB_TABLE);
25 }
26 protected static function getTable(Date $date, $type)
27 {
28 $tableNamePrefix = "archive_" . $type;
29 $tableName = $tableNamePrefix . "_" . self::getTableMonthFromDate($date);
30 $tableName = Common::prefixTable($tableName);
31 self::createArchiveTablesIfAbsent($tableName, $tableNamePrefix);
32 return $tableName;
33 }
34 protected static function createArchiveTablesIfAbsent($tableName, $tableNamePrefix)
35 {
36 if (is_null(self::$tablesAlreadyInstalled)) {
37 self::refreshTableList();
38 }
39 if (!in_array($tableName, self::$tablesAlreadyInstalled)) {
40 self::getModel()->createArchiveTable($tableName, $tableNamePrefix);
41 self::$tablesAlreadyInstalled[] = $tableName;
42 }
43 }
44 private static function getModel()
45 {
46 return new \Piwik\DataAccess\Model();
47 }
48 public static function clear()
49 {
50 self::$tablesAlreadyInstalled = null;
51 }
52 public static function refreshTableList()
53 {
54 self::$tablesAlreadyInstalled = self::getModel()->getInstalledArchiveTables();
55 }
56 /**
57 * Returns all table names archive_*
58 *
59 * @param string $type The type of table to return. Either `self::NUMERIC_TABLE` or `self::BLOB_TABLE`.
60 * @param bool $forceReload
61 * @return array
62 */
63 public static function getTablesArchivesInstalled($type = null, $forceReload = \false)
64 {
65 if (is_null(self::$tablesAlreadyInstalled) || $forceReload) {
66 self::refreshTableList();
67 }
68 if (empty($type)) {
69 return self::$tablesAlreadyInstalled;
70 } else {
71 $tableMatchRegex = '/archive_' . preg_quote($type) . '_/';
72 }
73 $archiveTables = array();
74 foreach (self::$tablesAlreadyInstalled as $table) {
75 if (preg_match($tableMatchRegex, $table)) {
76 $archiveTables[] = $table;
77 }
78 }
79 return $archiveTables;
80 }
81 /**
82 * Returns the latest table name archive_*.
83 * If no type is specified, blob table is returned when both blob and numeric are found for the same year_month.
84 *
85 * @param string|null $type The type of archive table to return. Either `self::NUMERIC_TABLE` or `self::BLOB_TABLE`.
86 */
87 public static function getLatestArchiveTableInstalled(?string $type = null, bool $forceReload = \false) : ?string
88 {
89 $archiveTables = static::getTablesArchivesInstalled($type, $forceReload);
90 // skip if there is no archive table (yet)
91 if (0 === count($archiveTables)) {
92 return null;
93 }
94 // sort tables so we have them in descending order of their date
95 usort($archiveTables, function ($a, $b) {
96 return static::getDateFromTableName($b) <=> static::getDateFromTableName($a);
97 });
98 return $archiveTables[0];
99 }
100 public static function getDateFromTableName($tableName)
101 {
102 $tableName = Common::unprefixTable($tableName);
103 $date = str_replace(array('archive_numeric_', 'archive_blob_'), '', $tableName);
104 return $date;
105 }
106 public static function getTableMonthFromDate(Date $date)
107 {
108 return $date->toString('Y_m');
109 }
110 public static function getTypeFromTableName($tableName)
111 {
112 if (strpos($tableName, 'archive_numeric_') !== \false) {
113 return self::NUMERIC_TABLE;
114 }
115 if (strpos($tableName, 'archive_blob_') !== \false) {
116 return self::BLOB_TABLE;
117 }
118 return \false;
119 }
120 }
121