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 |