You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConvertMysqlToMB4.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017, ownCloud GmbH
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Core\Command\Db;
  26. use Doctrine\DBAL\Platforms\MySQLPlatform;
  27. use OC\DB\MySqlTools;
  28. use OC\Migration\ConsoleOutput;
  29. use OC\Repair\Collation;
  30. use OCP\IConfig;
  31. use OCP\IDBConnection;
  32. use OCP\IURLGenerator;
  33. use Psr\Log\LoggerInterface;
  34. use Symfony\Component\Console\Command\Command;
  35. use Symfony\Component\Console\Input\InputInterface;
  36. use Symfony\Component\Console\Output\OutputInterface;
  37. class ConvertMysqlToMB4 extends Command {
  38. public function __construct(
  39. private IConfig $config,
  40. private IDBConnection $connection,
  41. private IURLGenerator $urlGenerator,
  42. private LoggerInterface $logger,
  43. ) {
  44. parent::__construct();
  45. }
  46. protected function configure() {
  47. $this
  48. ->setName('db:convert-mysql-charset')
  49. ->setDescription('Convert charset of MySQL/MariaDB to use utf8mb4');
  50. }
  51. protected function execute(InputInterface $input, OutputInterface $output): int {
  52. if (!$this->connection->getDatabasePlatform() instanceof MySQLPlatform) {
  53. $output->writeln("This command is only valid for MySQL/MariaDB databases.");
  54. return 1;
  55. }
  56. $tools = new MySqlTools();
  57. if (!$tools->supports4ByteCharset($this->connection)) {
  58. $url = $this->urlGenerator->linkToDocs('admin-mysql-utf8mb4');
  59. $output->writeln("The database is not properly setup to use the charset utf8mb4.");
  60. $output->writeln("For more information please read the documentation at $url");
  61. return 1;
  62. }
  63. // enable charset
  64. $this->config->setSystemValue('mysql.utf8mb4', true);
  65. // run conversion
  66. $coll = new Collation($this->config, $this->logger, $this->connection, false);
  67. $coll->run(new ConsoleOutput($output));
  68. return 0;
  69. }
  70. }