ConvertMysqlToMB4.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 ownCloud GmbH
  4. * SPDX-License-Identifier: AGPL-3.0-only
  5. */
  6. namespace OC\Core\Command\Db;
  7. use OC\DB\MySqlTools;
  8. use OC\Migration\ConsoleOutput;
  9. use OC\Repair\Collation;
  10. use OCP\IConfig;
  11. use OCP\IDBConnection;
  12. use OCP\IURLGenerator;
  13. use Psr\Log\LoggerInterface;
  14. use Symfony\Component\Console\Command\Command;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. class ConvertMysqlToMB4 extends Command {
  18. public function __construct(
  19. private IConfig $config,
  20. private IDBConnection $connection,
  21. private IURLGenerator $urlGenerator,
  22. private LoggerInterface $logger,
  23. ) {
  24. parent::__construct();
  25. }
  26. protected function configure() {
  27. $this
  28. ->setName('db:convert-mysql-charset')
  29. ->setDescription('Convert charset of MySQL/MariaDB to use utf8mb4');
  30. }
  31. protected function execute(InputInterface $input, OutputInterface $output): int {
  32. if ($this->connection->getDatabaseProvider() !== IDBConnection::PLATFORM_MYSQL) {
  33. $output->writeln('This command is only valid for MySQL/MariaDB databases.');
  34. return 1;
  35. }
  36. $tools = new MySqlTools();
  37. if (!$tools->supports4ByteCharset($this->connection)) {
  38. $url = $this->urlGenerator->linkToDocs('admin-mysql-utf8mb4');
  39. $output->writeln('The database is not properly setup to use the charset utf8mb4.');
  40. $output->writeln("For more information please read the documentation at $url");
  41. return 1;
  42. }
  43. // enable charset
  44. $this->config->setSystemValue('mysql.utf8mb4', true);
  45. // run conversion
  46. $coll = new Collation($this->config, $this->logger, $this->connection, false);
  47. $coll->run(new ConsoleOutput($output));
  48. return 0;
  49. }
  50. }