| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\DataAccess; |
|
|
| use Piwik\Common; |
| use Piwik\Date; |
|
|
| class ArchiveTableCreator |
| { |
| public const NUMERIC_TABLE = "numeric"; |
| public const BLOB_TABLE = "blob"; |
|
|
| |
| |
| |
| public static $tablesAlreadyInstalled = null; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static function getNumericTable(Date $date, ?bool $createIfMissing = null): ?string |
| { |
| return self::getTable($date, self::NUMERIC_TABLE, $createIfMissing); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static function getBlobTable(Date $date, ?bool $createIfMissing = null): ?string |
| { |
| return self::getTable($date, self::BLOB_TABLE, $createIfMissing); |
| } |
|
|
| |
| |
| |
| protected static function getTable(Date $date, string $type, ?bool $createIfMissing = null): ?string |
| { |
| if ($createIfMissing === null) { |
| self::triggerLegacyDefaultDeprecation($type); |
| $createIfMissing = true; |
| } |
|
|
| if ($createIfMissing) { |
| return self::ensureArchiveTableExists($date, $type); |
| } |
|
|
| return self::getExistingTable($date, $type); |
| } |
|
|
| protected static function getExistingTable(Date $date, string $type): ?string |
| { |
| $tableNamePrefix = "archive_" . $type; |
| $tableName = $tableNamePrefix . "_" . self::getTableMonthFromDate($date); |
| $tableName = Common::prefixTable($tableName); |
|
|
| if (is_null(self::$tablesAlreadyInstalled)) { |
| self::refreshTableList(); |
| } |
|
|
| if (!in_array($tableName, self::$tablesAlreadyInstalled)) { |
| self::refreshTableList(); |
|
|
| if (!in_array($tableName, self::$tablesAlreadyInstalled)) { |
| return null; |
| } |
| } |
|
|
| return $tableName; |
| } |
|
|
| protected static function ensureArchiveTableExists(Date $date, string $type): string |
| { |
| $tableNamePrefix = "archive_" . $type; |
| $tableName = $tableNamePrefix . "_" . self::getTableMonthFromDate($date); |
| $tableName = Common::prefixTable($tableName); |
|
|
| self::createArchiveTablesIfAbsent($tableName, $tableNamePrefix); |
|
|
| return $tableName; |
| } |
|
|
| |
| |
| |
| private static function triggerLegacyDefaultDeprecation(string $type): void |
| { |
| $method = $type === self::NUMERIC_TABLE ? 'getNumericTable' : 'getBlobTable'; |
| trigger_error( |
| sprintf( |
| "Omitting \$createIfMissing in ArchiveTableCreator::%s() is deprecated. Pass true to create missing archive tables or false to only return existing ones. In Matomo 6 the default behavior will change to lookup-only.", |
| $method |
| ), |
| E_USER_DEPRECATED |
| ); |
| } |
|
|
| protected static function createArchiveTablesIfAbsent($tableName, $tableNamePrefix) |
| { |
| if (is_null(self::$tablesAlreadyInstalled)) { |
| self::refreshTableList(); |
| } |
|
|
| if (!in_array($tableName, self::$tablesAlreadyInstalled)) { |
| self::getModel()->createArchiveTable($tableName, $tableNamePrefix); |
| self::$tablesAlreadyInstalled[] = $tableName; |
| } |
| } |
|
|
| private static function getModel() |
| { |
| return new Model(); |
| } |
|
|
| public static function clear() |
| { |
| self::$tablesAlreadyInstalled = null; |
| } |
|
|
| public static function refreshTableList() |
| { |
| self::$tablesAlreadyInstalled = self::getModel()->getInstalledArchiveTables(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| public static function getTablesArchivesInstalled($type = null, $forceReload = false) |
| { |
| if ( |
| is_null(self::$tablesAlreadyInstalled) |
| || $forceReload |
| ) { |
| self::refreshTableList(); |
| } |
|
|
| if (empty($type)) { |
| return self::$tablesAlreadyInstalled; |
| } else { |
| $tableMatchRegex = '/archive_' . preg_quote($type) . '_/'; |
| } |
|
|
| $archiveTables = array(); |
| foreach (self::$tablesAlreadyInstalled as $table) { |
| if (preg_match($tableMatchRegex, $table)) { |
| $archiveTables[] = $table; |
| } |
| } |
| return $archiveTables; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public static function getLatestArchiveTableInstalled(?string $type = null, bool $forceReload = false): ?string |
| { |
| $archiveTables = static::getTablesArchivesInstalled($type, $forceReload); |
|
|
| |
| if (0 === count($archiveTables)) { |
| return null; |
| } |
|
|
| |
| usort($archiveTables, function ($a, $b) { |
| return static::getDateFromTableName($b) <=> static::getDateFromTableName($a); |
| }); |
|
|
| return $archiveTables[0]; |
| } |
|
|
| public static function getDateFromTableName($tableName) |
| { |
| $tableName = Common::unprefixTable($tableName); |
| $date = str_replace(array('archive_numeric_', 'archive_blob_'), '', $tableName); |
|
|
| return $date; |
| } |
|
|
| public static function getTableMonthFromDate(Date $date) |
| { |
| return $date->toString('Y_m'); |
| } |
|
|
| public static function getTypeFromTableName($tableName) |
| { |
| if (strpos($tableName, 'archive_numeric_') !== false) { |
| return self::NUMERIC_TABLE; |
| } |
|
|
| if (strpos($tableName, 'archive_blob_') !== false) { |
| return self::BLOB_TABLE; |
| } |
|
|
| return false; |
| } |
| } |
|
|