ConvertMysqlToMB4.php 2.8 KB

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