3.12.0-b1.php
5 years ago
3.12.0-b7.php
5 years ago
4.0.0-b1.php
5 years ago
4.1.2-b1.php
5 years ago
4.1.2-b2.php
5 years ago
4.3.0-b3.php
5 years ago
4.3.0-b4.php
5 years ago
4.3.0-rc2.php
5 years ago
4.0.0-b1.php
239 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\Updates; |
| 11 | |
| 12 | use Piwik\DataAccess\TableMetadata; |
| 13 | use Piwik\Date; |
| 14 | use Piwik\DbHelper; |
| 15 | use Piwik\Plugin\Manager; |
| 16 | use Piwik\Plugins\CoreHome\Columns\Profilable; |
| 17 | use Piwik\Plugins\CoreHome\Columns\VisitorSecondsSinceFirst; |
| 18 | use Piwik\Plugins\CoreHome\Columns\VisitorSecondsSinceOrder; |
| 19 | use Piwik\Plugins\Installation\ServerFilesGenerator; |
| 20 | use Piwik\Plugins\PagePerformance\Columns\TimeDomCompletion; |
| 21 | use Piwik\Plugins\PagePerformance\Columns\TimeDomProcessing; |
| 22 | use Piwik\Plugins\PagePerformance\Columns\TimeNetwork; |
| 23 | use Piwik\Plugins\PagePerformance\Columns\TimeOnLoad; |
| 24 | use Piwik\Plugins\PagePerformance\Columns\TimeServer; |
| 25 | use Piwik\Plugins\PagePerformance\Columns\TimeTransfer; |
| 26 | use Piwik\Plugins\UsersManager\Model; |
| 27 | use Piwik\Common; |
| 28 | use Piwik\Config; |
| 29 | use Piwik\Plugins\UserCountry\LocationProvider; |
| 30 | use Piwik\Plugins\VisitorInterest\Columns\VisitorSecondsSinceLast; |
| 31 | use Piwik\Updater; |
| 32 | use Piwik\Updates as PiwikUpdates; |
| 33 | use Piwik\Updater\Migration\Factory as MigrationFactory; |
| 34 | |
| 35 | /** |
| 36 | * Update for version 4.0.0-b1. |
| 37 | */ |
| 38 | class Updates_4_0_0_b1 extends PiwikUpdates |
| 39 | { |
| 40 | /** |
| 41 | * @var MigrationFactory |
| 42 | */ |
| 43 | private $migration; |
| 44 | |
| 45 | public function __construct(MigrationFactory $factory) |
| 46 | { |
| 47 | $this->migration = $factory; |
| 48 | } |
| 49 | |
| 50 | public function getMigrations(Updater $updater) |
| 51 | { |
| 52 | $tableMetadata = new TableMetadata(); |
| 53 | |
| 54 | $columnsToAdd = []; |
| 55 | |
| 56 | $migrations = []; |
| 57 | |
| 58 | /** APP SPECIFIC TOKEN START */ |
| 59 | $migrations[] = $this->migration->db->createTable('user_token_auth', array( |
| 60 | 'idusertokenauth' => 'BIGINT UNSIGNED NOT NULL AUTO_INCREMENT', |
| 61 | 'login' => 'VARCHAR(100) NOT NULL', |
| 62 | 'description' => 'VARCHAR('.Model::MAX_LENGTH_TOKEN_DESCRIPTION.') NOT NULL', |
| 63 | 'password' => 'VARCHAR(191) NOT NULL', |
| 64 | 'system_token' => 'TINYINT(1) NOT NULL DEFAULT 0', |
| 65 | 'hash_algo' => 'VARCHAR(30) NOT NULL', |
| 66 | 'last_used' => 'DATETIME NULL', |
| 67 | 'date_created' => ' DATETIME NOT NULL', |
| 68 | 'date_expired' => ' DATETIME NULL', |
| 69 | ), 'idusertokenauth'); |
| 70 | $migrations[] = $this->migration->db->addUniqueKey('user_token_auth', 'password', 'uniq_password'); |
| 71 | |
| 72 | $migrations[] = $this->migration->db->dropIndex('user', 'uniq_keytoken'); |
| 73 | |
| 74 | $userModel = new Model(); |
| 75 | foreach ($userModel->getUsers(array()) as $user) { |
| 76 | if (!empty($user['token_auth'])) { |
| 77 | $migrations[] = $this->migration->db->insert('user_token_auth', array( |
| 78 | 'login' => $user['login'], |
| 79 | 'description' => 'Created by Matomo 4 migration', |
| 80 | 'password' => $userModel->hashTokenAuth($user['token_auth']), |
| 81 | 'date_created' => Date::now()->getDatetime() |
| 82 | )); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** APP SPECIFIC TOKEN END */ |
| 87 | |
| 88 | // invalidations table |
| 89 | $migrations[] = $this->migration->db->createTable('archive_invalidations', [ |
| 90 | 'idinvalidation' => 'BIGINT UNSIGNED NOT NULL AUTO_INCREMENT', |
| 91 | 'idarchive' => 'INTEGER UNSIGNED NULL', |
| 92 | 'name' => 'VARCHAR(255) NOT NULL', |
| 93 | 'idsite' => 'INTEGER NOT NULL', |
| 94 | 'date1' => 'DATE NOT NULL', |
| 95 | 'date2' => 'DATE NOT NULL', |
| 96 | 'period' => 'TINYINT UNSIGNED NOT NULL', |
| 97 | 'ts_invalidated' => 'DATETIME NOT NULL', |
| 98 | 'ts_started' => 'DATETIME NULL', |
| 99 | 'status' => 'TINYINT(1) UNSIGNED DEFAULT 0', |
| 100 | 'report' => 'VARCHAR(255) NULL', |
| 101 | ], ['idinvalidation']); |
| 102 | |
| 103 | $migrations[] = $this->migration->db->addIndex('archive_invalidations', ['idsite', 'date1', 'period'], 'index_idsite_dates_period_name'); |
| 104 | |
| 105 | $migrations[] = $this->migration->db->dropColumn('user', 'alias'); |
| 106 | $migrations[] = $this->migration->db->dropColumn('user', 'token_auth'); |
| 107 | |
| 108 | // keep piwik_ignore for existing installs |
| 109 | $migrations[] = $this->migration->config->set('Tracker', 'ignore_visits_cookie_name', 'piwik_ignore'); |
| 110 | |
| 111 | $migrations[] = $this->migration->db->changeColumn('log_link_visit_action', 'interaction_position', 'pageview_position', 'MEDIUMINT UNSIGNED DEFAULT NULL'); |
| 112 | |
| 113 | // Move the site search fields of log_visit out of custom variables into their own fields |
| 114 | $columnsToAdd['log_link_visit_action']['search_cat'] = 'VARCHAR(200) NULL'; |
| 115 | $columnsToAdd['log_link_visit_action']['search_count'] = 'INTEGER(10) UNSIGNED NULL'; |
| 116 | |
| 117 | // replace days to ... dimensions w/ seconds dimensions |
| 118 | foreach (['log_visit', 'log_conversion'] as $table) { |
| 119 | $columnsToAdd[$table]['visitor_seconds_since_first'] = VisitorSecondsSinceFirst::COLUMN_TYPE; |
| 120 | $columnsToAdd[$table]['visitor_seconds_since_order'] = VisitorSecondsSinceOrder::COLUMN_TYPE; |
| 121 | } |
| 122 | $columnsToAdd['log_visit']['visitor_seconds_since_last'] = VisitorSecondsSinceLast::COLUMN_TYPE; |
| 123 | $columnsToAdd['log_visit']['profilable'] = Profilable::COLUMN_TYPE; |
| 124 | $columnsToAdd['log_link_visit_action'][TimeDomCompletion::COLUMN_NAME] = TimeDomCompletion::COLUMN_TYPE; |
| 125 | $columnsToAdd['log_link_visit_action'][TimeDomProcessing::COLUMN_NAME] = TimeDomProcessing::COLUMN_TYPE; |
| 126 | $columnsToAdd['log_link_visit_action'][TimeNetwork::COLUMN_NAME] = TimeNetwork::COLUMN_TYPE; |
| 127 | $columnsToAdd['log_link_visit_action'][TimeOnLoad::COLUMN_NAME] = TimeOnLoad::COLUMN_TYPE; |
| 128 | $columnsToAdd['log_link_visit_action'][TimeServer::COLUMN_NAME] = TimeServer::COLUMN_TYPE; |
| 129 | $columnsToAdd['log_link_visit_action'][TimeTransfer::COLUMN_NAME] = TimeTransfer::COLUMN_TYPE; |
| 130 | |
| 131 | $columnsToMaybeAdd = ['revenue', 'revenue_discount', 'revenue_shipping', 'revenue_subtotal', 'revenue_tax']; |
| 132 | $columnsLogConversion = $tableMetadata->getColumns(Common::prefixTable('log_conversion')); |
| 133 | foreach ($columnsToMaybeAdd as $columnToMaybeAdd) { |
| 134 | if (!in_array($columnToMaybeAdd, $columnsLogConversion, true)) { |
| 135 | $columnsToAdd['log_conversion'][$columnToMaybeAdd] = 'DOUBLE NULL DEFAULT NULL'; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | foreach ($columnsToAdd as $table => $columns) { |
| 140 | $migrations[] = $this->migration->db->addColumns($table, $columns); |
| 141 | |
| 142 | foreach ($columns as $columnName => $columnType) { |
| 143 | $optionKey = 'version_' . $table . '.' . $columnName; |
| 144 | $optionValue = $columnType; |
| 145 | |
| 146 | if ($table == 'log_visit' && isset($columnsToAdd['log_conversion'][$columnName])) { |
| 147 | $optionValue .= '1'; // column is in log_conversion too |
| 148 | } |
| 149 | |
| 150 | $migrations[] = $this->migration->db->sql("INSERT IGNORE INTO `" . Common::prefixTable('option') |
| 151 | . "` (option_name, option_value) VALUES ('$optionKey', '$optionValue')"); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // init seconds_to_... columns |
| 156 | $logVisitColumns = $tableMetadata->getColumns(Common::prefixTable('log_visit')); |
| 157 | $hasDaysColumnInVisit = in_array('visitor_days_since_first', $logVisitColumns); |
| 158 | |
| 159 | if ($hasDaysColumnInVisit) { |
| 160 | $migrations[] = $this->migration->db->sql("UPDATE " . Common::prefixTable('log_visit') |
| 161 | . " SET visitor_seconds_since_first = visitor_days_since_first * 86400, |
| 162 | visitor_seconds_since_order = visitor_days_since_order * 86400, |
| 163 | visitor_seconds_since_last = visitor_days_since_last * 86400"); |
| 164 | } |
| 165 | |
| 166 | $logConvColumns = $tableMetadata->getColumns(Common::prefixTable('log_conversion')); |
| 167 | $hasDaysColumnInConv = in_array('visitor_days_since_first', $logConvColumns); |
| 168 | |
| 169 | if ($hasDaysColumnInConv) { |
| 170 | $migrations[] = $this->migration->db->sql("UPDATE " . Common::prefixTable('log_conversion') |
| 171 | . " SET visitor_seconds_since_first = visitor_days_since_first * 86400, |
| 172 | visitor_seconds_since_order = visitor_days_since_order * 86400"); |
| 173 | } |
| 174 | |
| 175 | if (Manager::getInstance()->isPluginInstalled('CustomVariables')) { |
| 176 | $visitActionTable = Common::prefixTable('log_link_visit_action'); |
| 177 | $migrations[] = $this->migration->db->sql("UPDATE $visitActionTable SET search_cat = if(custom_var_k4 = '_pk_scat', custom_var_v4, search_cat), search_count = if(custom_var_k5 = '_pk_scount', custom_var_v5, search_count) WHERE custom_var_k4 = '_pk_scat' or custom_var_k5 = '_pk_scount'"); |
| 178 | } |
| 179 | |
| 180 | // remove old days_to_... columns |
| 181 | $migrations[] = $this->migration->db->dropColumns('log_visit', [ |
| 182 | 'config_gears', |
| 183 | 'config_director', |
| 184 | 'visitor_days_since_first', |
| 185 | 'visitor_days_since_order', |
| 186 | 'visitor_days_since_last', |
| 187 | ]); |
| 188 | $migrations[] = $this->migration->db->dropColumns('log_conversion', [ |
| 189 | 'visitor_days_since_first', |
| 190 | 'visitor_days_since_order', |
| 191 | ]); |
| 192 | |
| 193 | $config = Config::getInstance(); |
| 194 | |
| 195 | if (!empty($config->mail['type']) && $config->mail['type'] === 'Crammd5') { |
| 196 | $migrations[] = $this->migration->config->set('mail', 'type', 'Cram-md5'); |
| 197 | } |
| 198 | |
| 199 | $migrations[] = $this->migration->plugin->activate('PagePerformance'); |
| 200 | if (!Manager::getInstance()->isPluginActivated('CustomDimensions')) { |
| 201 | $migrations[] = $this->migration->plugin->activate('CustomDimensions'); |
| 202 | } |
| 203 | |
| 204 | $configTableLimit = Config::getInstance()->getFromLocalConfig('General')['datatable_archiving_maximum_rows_custom_variables'] ?? null; |
| 205 | $configSubTableLimit = Config::getInstance()->getFromLocalConfig('General')['datatable_archiving_maximum_rows_subtable_custom_variables'] ?? null; |
| 206 | |
| 207 | if ($configTableLimit) { |
| 208 | $migrations[] = $this->migration->config->set('General', 'datatable_archiving_maximum_rows_custom_dimensions', $configTableLimit); |
| 209 | } |
| 210 | if ($configSubTableLimit) { |
| 211 | $migrations[] = $this->migration->config->set('General', 'datatable_archiving_maximum_rows_subtable_custom_dimensions', $configSubTableLimit); |
| 212 | } |
| 213 | |
| 214 | $migrations[] = $this->migration->db->changeColumnType('session', 'id', 'VARCHAR(191)'); |
| 215 | $migrations[] = $this->migration->db->changeColumnType('site_url', 'url', 'VARCHAR(190)'); |
| 216 | $migrations[] = $this->migration->db->changeColumnType('option', 'option_name', 'VARCHAR(191)'); |
| 217 | |
| 218 | $migrations[] = $this->migration->db->changeColumnType('log_action', 'name', 'VARCHAR(4096)'); |
| 219 | $migrations[] = $this->migration->db->changeColumnType('log_conversion', 'url', 'VARCHAR(4096)'); |
| 220 | return $migrations; |
| 221 | } |
| 222 | |
| 223 | public function doUpdate(Updater $updater) |
| 224 | { |
| 225 | $updater->executeMigrations(__FILE__, $this->getMigrations($updater)); |
| 226 | } |
| 227 | |
| 228 | protected function usesGeoIpLegacyLocationProvider() |
| 229 | { |
| 230 | $currentProvider = LocationProvider::getCurrentProviderId(); |
| 231 | |
| 232 | return in_array($currentProvider, [ |
| 233 | 'geoip_pecl', |
| 234 | 'geoip_php', |
| 235 | 'geoip_serverbased', |
| 236 | ]); |
| 237 | } |
| 238 | } |
| 239 |