3.12.0-b1.php
2 years ago
3.12.0-b7.php
2 years ago
4.0.0-b1.php
7 months ago
4.0.0-b3.php
2 years ago
4.0.0-rc3.php
2 years ago
4.0.0-rc4.php
2 months ago
4.0.1-b1.php
2 years ago
4.0.4-b1.php
2 years ago
4.1.2-b1.php
2 years ago
4.1.2-b2.php
2 years ago
4.10.0-b1.php
2 months ago
4.11.0-b1.php
2 months ago
4.11.0-rc2.php
2 months ago
4.12.0-b1.php
2 months ago
4.12.0-b2.php
2 months ago
4.12.0-b3.php
2 months ago
4.12.0-b4.php
2 months ago
4.3.0-b3.php
2 years ago
4.3.0-b4.php
7 months ago
4.3.0-rc2.php
2 years ago
4.4.0-b1.php
2 years ago
4.5.0-b1.php
2 years ago
4.6.0-b1.php
2 years ago
4.6.0-b4.php
3 months ago
4.6.2-rc2.php
3 months ago
4.7.0-b2.php
2 months ago
4.7.1-b1.php
2 months ago
5.0.0-b1.php
1 year ago
5.0.0-rc2.php
2 years ago
5.0.0-rc5.php
2 months ago
5.11.0-b1.php
2 weeks ago
5.2.0-b2.php
1 year ago
5.2.0-b6.php
1 year ago
5.3.0-b1.php
1 year ago
5.3.0-rc1.php
1 year ago
5.4.0-b1.php
7 months ago
5.4.0-b3.php
7 months ago
5.4.0-b4.php
2 months ago
5.5.0-b2.php
2 months ago
5.6.0-b1.php
2 months ago
5.7.0-b1.php
4 months ago
5.7.0-b2.php
3 months ago
5.7.0-b3.php
4 months ago
5.8.0-b1.php
3 months ago
5.8.0-b2.php
3 months ago
5.9.0-b1.php
2 months ago
5.9.0-b2.php
2 months ago
5.0.0-b1.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\Updates; |
| 10 | |
| 11 | use Piwik\DataAccess\ArchiveTableCreator; |
| 12 | use Piwik\Db; |
| 13 | use Piwik\Common; |
| 14 | use Piwik\DbHelper; |
| 15 | use Piwik\SettingsPiwik; |
| 16 | use Piwik\Updater; |
| 17 | use Piwik\Updater\Migration\Db as DbAlias; |
| 18 | use Piwik\Updater\Migration\Factory; |
| 19 | use Piwik\Updates as PiwikUpdates; |
| 20 | use Piwik\Updater\Migration\Custom as CustomMigration; |
| 21 | use Piwik\Plugins\Goals\Commands\CalculateConversionPages; |
| 22 | /** |
| 23 | * Update for version 5.0.0-b1 |
| 24 | */ |
| 25 | class Updates_5_0_0_b1 extends PiwikUpdates |
| 26 | { |
| 27 | /** |
| 28 | * @var Factory |
| 29 | */ |
| 30 | private $migration; |
| 31 | private $tableName; |
| 32 | private $indexName; |
| 33 | private $newIndexName; |
| 34 | public function __construct(Factory $factory) |
| 35 | { |
| 36 | $this->migration = $factory; |
| 37 | $this->tableName = Common::prefixTable('log_visit'); |
| 38 | $this->indexName = 'index_idsite_idvisitor'; |
| 39 | $this->newIndexName = 'index_idsite_idvisitor_time'; |
| 40 | } |
| 41 | public function doUpdate(Updater $updater) |
| 42 | { |
| 43 | $updater->executeMigrations(__FILE__, $this->getMigrations($updater)); |
| 44 | } |
| 45 | public function getMigrations(Updater $updater) |
| 46 | { |
| 47 | $migrations = $this->getUpdateArchiveIndexMigrations(); |
| 48 | $migrations[] = $this->migration->db->addColumns('user_token_auth', ['post_only' => "TINYINT(2) UNSIGNED NOT NULL DEFAULT '0'"]); |
| 49 | $migrations[] = $this->migration->db->addColumns('log_conversion', ['pageviews_before' => "SMALLINT UNSIGNED DEFAULT NULL"]); |
| 50 | $instanceId = SettingsPiwik::getPiwikInstanceId(); |
| 51 | if (strpos($instanceId, '.matomo.cloud') === \false && strpos($instanceId, '.innocraft.cloud') === \false) { |
| 52 | $commandString = './console core:calculate-conversion-pages --dates=yesterday,today'; |
| 53 | $populatePagesBefore = new CustomMigration([CalculateConversionPages::class, 'calculateYesterdayAndToday'], $commandString); |
| 54 | $migrations[] = $populatePagesBefore; |
| 55 | } |
| 56 | return $this->appendLogVisitTableMigrations($migrations); |
| 57 | } |
| 58 | private function getUpdateArchiveIndexMigrations() |
| 59 | { |
| 60 | $migrations = []; |
| 61 | $tables = ArchiveTableCreator::getTablesArchivesInstalled('numeric'); |
| 62 | foreach ($tables as $table) { |
| 63 | $migrations[] = $this->migration->db->sql(sprintf('DELETE FROM `%s` WHERE ts_archived is null', $table)); |
| 64 | $hasPrefix = strpos($table, 'archive') !== 0; |
| 65 | if ($hasPrefix) { |
| 66 | $table = Common::unprefixTable($table); |
| 67 | } |
| 68 | $migrations[] = $this->migration->db->dropIndex($table, 'index_idsite_dates_period'); |
| 69 | $migrations[] = $this->migration->db->addIndex($table, ['idsite', 'date1', 'date2', 'period', 'name(6)'], 'index_idsite_dates_period'); |
| 70 | } |
| 71 | return $migrations; |
| 72 | } |
| 73 | private function appendLogVisitTableMigrations($migrations) |
| 74 | { |
| 75 | if ($this->hasNewIndex()) { |
| 76 | // correct index already exists, so don't perform anything |
| 77 | return $migrations; |
| 78 | } |
| 79 | if ($this->hasCorrectlySetOldIndex()) { |
| 80 | // already existing index has the correct fields. Try renaming, but ignore syntax error thrown if rename command does not exist |
| 81 | $migrations[] = $this->migration->db->sql("ALTER TABLE `{$this->tableName}` RENAME INDEX `{$this->indexName}` TO `{$this->newIndexName}`", [DbAlias::ERROR_CODE_SYNTAX_ERROR]); |
| 82 | } |
| 83 | // create the new index if it does not yet exist and drop the old one |
| 84 | if ($this->isTableInnoDb()) { |
| 85 | // Only InnoDB does support descending indexes as of MySQL 8 |
| 86 | $migrations[] = $this->migration->db->sql("ALTER TABLE `{$this->tableName}` ADD INDEX `{$this->newIndexName}` (`idsite`, `idvisitor`, `visit_last_action_time` DESC)", [DbAlias::ERROR_CODE_DUPLICATE_KEY, DbAlias::ERROR_CODE_KEY_COLUMN_NOT_EXISTS]); |
| 87 | } else { |
| 88 | $migrations[] = $this->migration->db->sql("ALTER TABLE `{$this->tableName}` ADD INDEX `{$this->newIndexName}` (`idsite`, `idvisitor`, `visit_last_action_time`)", [DbAlias::ERROR_CODE_DUPLICATE_KEY, DbAlias::ERROR_CODE_KEY_COLUMN_NOT_EXISTS]); |
| 89 | } |
| 90 | $migrations[] = $this->migration->db->dropIndex('log_visit', $this->indexName); |
| 91 | return $migrations; |
| 92 | } |
| 93 | private function hasCorrectlySetOldIndex() : bool |
| 94 | { |
| 95 | $sql = "SHOW INDEX FROM `{$this->tableName}` WHERE Key_name = '{$this->indexName}'"; |
| 96 | $result = Db::fetchAll($sql); |
| 97 | if (empty($result)) { |
| 98 | // No index present |
| 99 | return \false; |
| 100 | } |
| 101 | // Check that the $result contains all the required column names. This is required as there was a previous index |
| 102 | // with the same name that only consisted of two columns. We want to check this index is built with all three. |
| 103 | // $diff will be empty if all three columns are found, meaning that the index already exists. |
| 104 | $diff = array_diff(['idsite', 'idvisitor', 'visit_last_action_time'], array_column($result, 'Column_name')); |
| 105 | if (!$diff) { |
| 106 | return \true; |
| 107 | } |
| 108 | return \false; |
| 109 | } |
| 110 | private function hasNewIndex() : bool |
| 111 | { |
| 112 | return DbHelper::tableHasIndex($this->tableName, $this->newIndexName); |
| 113 | } |
| 114 | private function isTableInnoDb() : bool |
| 115 | { |
| 116 | $sql = "SHOW TABLE STATUS WHERE NAME='{$this->tableName}'"; |
| 117 | $result = Db::fetchRow($sql); |
| 118 | return strtolower($result['Engine'] ?? '') === 'innodb'; |
| 119 | } |
| 120 | } |
| 121 |