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