LogQueryBuilder
2 years ago
Actions.php
2 years ago
ArchiveSelector.php
2 years ago
ArchiveTableCreator.php
2 years ago
ArchiveTableDao.php
2 years ago
ArchiveWriter.php
2 years ago
ArchivingDbAdapter.php
2 years ago
LogAggregator.php
2 years ago
LogQueryBuilder.php
2 years ago
LogTableTemporary.php
2 years ago
Model.php
2 years ago
RawLogDao.php
2 years ago
TableMetadata.php
2 years ago
ArchiveTableCreator.php
103 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | * |
| 9 | */ |
| 10 | namespace Piwik\DataAccess; |
| 11 | |
| 12 | use Piwik\Common; |
| 13 | use Piwik\Date; |
| 14 | class ArchiveTableCreator |
| 15 | { |
| 16 | const NUMERIC_TABLE = "numeric"; |
| 17 | const BLOB_TABLE = "blob"; |
| 18 | public static $tablesAlreadyInstalled = null; |
| 19 | public static function getNumericTable(Date $date) |
| 20 | { |
| 21 | return self::getTable($date, self::NUMERIC_TABLE); |
| 22 | } |
| 23 | public static function getBlobTable(Date $date) |
| 24 | { |
| 25 | return self::getTable($date, self::BLOB_TABLE); |
| 26 | } |
| 27 | protected static function getTable(Date $date, $type) |
| 28 | { |
| 29 | $tableNamePrefix = "archive_" . $type; |
| 30 | $tableName = $tableNamePrefix . "_" . self::getTableMonthFromDate($date); |
| 31 | $tableName = Common::prefixTable($tableName); |
| 32 | self::createArchiveTablesIfAbsent($tableName, $tableNamePrefix); |
| 33 | return $tableName; |
| 34 | } |
| 35 | protected static function createArchiveTablesIfAbsent($tableName, $tableNamePrefix) |
| 36 | { |
| 37 | if (is_null(self::$tablesAlreadyInstalled)) { |
| 38 | self::refreshTableList(); |
| 39 | } |
| 40 | if (!in_array($tableName, self::$tablesAlreadyInstalled)) { |
| 41 | self::getModel()->createArchiveTable($tableName, $tableNamePrefix); |
| 42 | self::$tablesAlreadyInstalled[] = $tableName; |
| 43 | } |
| 44 | } |
| 45 | private static function getModel() |
| 46 | { |
| 47 | return new \Piwik\DataAccess\Model(); |
| 48 | } |
| 49 | public static function clear() |
| 50 | { |
| 51 | self::$tablesAlreadyInstalled = null; |
| 52 | } |
| 53 | public static function refreshTableList() |
| 54 | { |
| 55 | self::$tablesAlreadyInstalled = self::getModel()->getInstalledArchiveTables(); |
| 56 | } |
| 57 | /** |
| 58 | * Returns all table names archive_* |
| 59 | * |
| 60 | * @param string $type The type of table to return. Either `self::NUMERIC_TABLE` or `self::BLOB_TABLE`. |
| 61 | * @param bool $forceReload |
| 62 | * @return array |
| 63 | */ |
| 64 | public static function getTablesArchivesInstalled($type = null, $forceReload = false) |
| 65 | { |
| 66 | if (is_null(self::$tablesAlreadyInstalled) || $forceReload) { |
| 67 | self::refreshTableList(); |
| 68 | } |
| 69 | if (empty($type)) { |
| 70 | return self::$tablesAlreadyInstalled; |
| 71 | } else { |
| 72 | $tableMatchRegex = '/archive_' . preg_quote($type) . '_/'; |
| 73 | } |
| 74 | $archiveTables = array(); |
| 75 | foreach (self::$tablesAlreadyInstalled as $table) { |
| 76 | if (preg_match($tableMatchRegex, $table)) { |
| 77 | $archiveTables[] = $table; |
| 78 | } |
| 79 | } |
| 80 | return $archiveTables; |
| 81 | } |
| 82 | public static function getDateFromTableName($tableName) |
| 83 | { |
| 84 | $tableName = Common::unprefixTable($tableName); |
| 85 | $date = str_replace(array('archive_numeric_', 'archive_blob_'), '', $tableName); |
| 86 | return $date; |
| 87 | } |
| 88 | public static function getTableMonthFromDate(Date $date) |
| 89 | { |
| 90 | return $date->toString('Y_m'); |
| 91 | } |
| 92 | public static function getTypeFromTableName($tableName) |
| 93 | { |
| 94 | if (strpos($tableName, 'archive_numeric_') !== false) { |
| 95 | return self::NUMERIC_TABLE; |
| 96 | } |
| 97 | if (strpos($tableName, 'archive_blob_') !== false) { |
| 98 | return self::BLOB_TABLE; |
| 99 | } |
| 100 | return false; |
| 101 | } |
| 102 | } |
| 103 |