ConvertMysqlToMB4.php 1.7 KB

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