From b6ace0e06c2ef3f8f98ac55feb934ebe0d304107 Mon Sep 17 00:00:00 2001 From: Stefan Giehl Date: Sun, 17 May 2020 22:39:32 +0200 Subject: Use utf8mb4 character set if possible (#15618) --- plugins/CoreUpdater/Commands/ConvertToUtf8mb4.php | 141 ++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 plugins/CoreUpdater/Commands/ConvertToUtf8mb4.php (limited to 'plugins/CoreUpdater/Commands/ConvertToUtf8mb4.php') diff --git a/plugins/CoreUpdater/Commands/ConvertToUtf8mb4.php b/plugins/CoreUpdater/Commands/ConvertToUtf8mb4.php new file mode 100644 index 0000000000..141b71470c --- /dev/null +++ b/plugins/CoreUpdater/Commands/ConvertToUtf8mb4.php @@ -0,0 +1,141 @@ +setName('core:convert-to-utf8mb4'); + + $this->setDescription('Converts the database to utf8mb4'); + + $this->addOption('show', null, InputOption::VALUE_NONE, Piwik::translate('Show all commands / queries only.')); + $this->addOption('yes', null, InputOption::VALUE_NONE, Piwik::translate('CoreUpdater_ConsoleParameterDescription')); + $this->addOption('keep-tracking', null, InputOption::VALUE_NONE, 'Do not disable tracking while conversion is running'); + } + + public function isEnabled() + { + $dbSettings = new Db\Settings(); + $charset = $dbSettings->getUsedCharset(); + + return $charset !== 'utf8mb4'; + } + + /** + * Execute command like: ./console core:convert-to-utf8mb4 --yes + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $yes = $input->getOption('yes'); + $keepTracking = $input->getOption('keep-tracking'); + $show = $input->getOption('show'); + + $queries = DbHelper::getUtf8mb4ConversionQueries(); + + if ($show) { + $this->showCommands($queries, $keepTracking, $output); + return; + } + + $output->writeln("This command will convert all Matomo database tables to utf8mb4.\n"); + + if (DbHelper::getDefaultCharset() !== 'utf8mb4') { + $this->writeSuccessMessage($output, array('Your database does not support utf8mb4')); + return; + } + + if (!$keepTracking) { + $output->writeln("Tracking will be disabled during this process.\n"); + } + + $output->writeln('If you want to see what this command is going to do use the --show option.'); + + if (!$yes) { + $yes = $this->askForUpdateConfirmation($input, $output); + } + + if ($yes) { + + if (!$keepTracking) { + $output->writeln("\n" . Piwik::translate('Disabling Matomo Tracking')); + $config = Config::getInstance(); + $config->Tracker['record_statistics'] = '0'; + $config->forceSave(); + } + + $output->writeln("\n" . Piwik::translate('CoreUpdater_ConsoleStartingDbUpgrade')); + + foreach ($queries as $query) { + $output->write("\n" . 'Executing ' . $query . '... '); + Db::get()->exec($query); + $output->write(' done.'); + } + + $output->writeln("\n" . 'Updating used database charset in config.ini.php.'); + $config = Config::getInstance(); + $config->database['charset'] = 'utf8mb4'; + + if (!$keepTracking) { + $output->writeln("\n" . Piwik::translate('Enabling Matomo Tracking')); + $config->Tracker['record_statistics'] = '1'; + } + + $config->forceSave(); + + $this->writeSuccessMessage($output, array('Conversion to utf8mb4 successful.')); + + } else { + $this->writeSuccessMessage($output, array('Database conversion skipped.')); + } + } + + protected function showCommands($queries, $keepTracking, OutputInterface $output) + { + $output->writeln("To manually convert all Matomo database tables to utf8mb4 follow these steps."); + if (!$keepTracking) { + $output->writeln(''); + $output->writeln('** Disable Matomo Tracking with this command: **'); + $output->writeln('./console config:set --section=Tracker --key=record_statistics --value=0'); + } + $output->writeln(''); + $output->writeln('** Execute the following database queries: **'); + $output->writeln(implode("\n", $queries)); + $output->writeln(''); + $output->writeln('** Change configured database charset to utf8mb4 with this command: **'); + $output->writeln('./console config:set --section=database --key=charset --value=utf8mb4'); + if (!$keepTracking) { + $output->writeln(''); + $output->writeln('** Enable Matomo Tracking again with this command: **'); + $output->writeln('./console config:set --section=Tracker --key=record_statistics --value=1'); + } + } + + private function askForUpdateConfirmation(InputInterface $input, OutputInterface $output) + { + $helper = $this->getHelper('question'); + $question = new ConfirmationQuestion('Execute updates? (y/N) ', false); + + return $helper->ask($input, $output, $question); + } +} -- cgit v1.2.3