PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.2.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.2.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 1 year ago Actions.php 2 years ago ArchiveSelector.php 1 year ago ArchiveTableCreator.php 1 year ago ArchiveTableDao.php 1 year ago ArchiveWriter.php 1 year ago ArchivingDbAdapter.php 1 year ago LogAggregator.php 1 year ago LogQueryBuilder.php 1 year ago LogTableTemporary.php 2 years ago Model.php 1 year ago RawLogDao.php 1 year ago TableMetadata.php 1 year ago
ArchiveTableCreator.php
123 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 * @param bool $forceReload
87 * @return string|null
88 */
89 public static function getLatestArchiveTableInstalled(?string $type = null, bool $forceReload = \false) : ?string
90 {
91 $archiveTables = static::getTablesArchivesInstalled($type, $forceReload);
92 // skip if there is no archive table (yet)
93 if (0 === count($archiveTables)) {
94 return null;
95 }
96 // sort tables so we have them in descending order of their date
97 usort($archiveTables, function ($a, $b) {
98 return static::getDateFromTableName($b) <=> static::getDateFromTableName($a);
99 });
100 return $archiveTables[0];
101 }
102 public static function getDateFromTableName($tableName)
103 {
104 $tableName = Common::unprefixTable($tableName);
105 $date = str_replace(array('archive_numeric_', 'archive_blob_'), '', $tableName);
106 return $date;
107 }
108 public static function getTableMonthFromDate(Date $date)
109 {
110 return $date->toString('Y_m');
111 }
112 public static function getTypeFromTableName($tableName)
113 {
114 if (strpos($tableName, 'archive_numeric_') !== \false) {
115 return self::NUMERIC_TABLE;
116 }
117 if (strpos($tableName, 'archive_blob_') !== \false) {
118 return self::BLOB_TABLE;
119 }
120 return \false;
121 }
122 }
123