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