ConvertMysqlToMB4.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. private IConfig $config;
  39. private IDBConnection $connection;
  40. private IURLGenerator $urlGenerator;
  41. private LoggerInterface $logger;
  42. public function __construct(
  43. IConfig $config,
  44. IDBConnection $connection,
  45. IURLGenerator $urlGenerator,
  46. LoggerInterface $logger
  47. ) {
  48. $this->config = $config;
  49. $this->connection = $connection;
  50. $this->urlGenerator = $urlGenerator;
  51. $this->logger = $logger;
  52. parent::__construct();
  53. }
  54. protected function configure() {
  55. $this
  56. ->setName('db:convert-mysql-charset')
  57. ->setDescription('Convert charset of MySQL/MariaDB to use utf8mb4');
  58. }
  59. protected function execute(InputInterface $input, OutputInterface $output): int {
  60. if (!$this->connection->getDatabasePlatform() instanceof MySQLPlatform) {
  61. $output->writeln("This command is only valid for MySQL/MariaDB databases.");
  62. return 1;
  63. }
  64. $tools = new MySqlTools();
  65. if (!$tools->supports4ByteCharset($this->connection)) {
  66. $url = $this->urlGenerator->linkToDocs('admin-mysql-utf8mb4');
  67. $output->writeln("The database is not properly setup to use the charset utf8mb4.");
  68. $output->writeln("For more information please read the documentation at $url");
  69. return 1;
  70. }
  71. // enable charset
  72. $this->config->setSystemValue('mysql.utf8mb4', true);
  73. // run conversion
  74. $coll = new Collation($this->config, $this->logger, $this->connection, false);
  75. $coll->run(new ConsoleOutput($output));
  76. return 0;
  77. }
  78. }