ConvertMysqlToMB4.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017, ownCloud GmbH
  4. *
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Thomas Müller <thomas.mueller@tmit.eu>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Core\Command\Db;
  24. use Doctrine\DBAL\Platforms\MySqlPlatform;
  25. use OC\DB\MySqlTools;
  26. use OC\Migration\ConsoleOutput;
  27. use OC\Repair\Collation;
  28. use OCP\IConfig;
  29. use OCP\IDBConnection;
  30. use OCP\ILogger;
  31. use OCP\IURLGenerator;
  32. use Symfony\Component\Console\Command\Command;
  33. use Symfony\Component\Console\Input\InputInterface;
  34. use Symfony\Component\Console\Output\OutputInterface;
  35. class ConvertMysqlToMB4 extends Command {
  36. /** @var IConfig */
  37. private $config;
  38. /** @var IDBConnection */
  39. private $connection;
  40. /** @var IURLGenerator */
  41. private $urlGenerator;
  42. /** @var ILogger */
  43. private $logger;
  44. /**
  45. * @param IConfig $config
  46. * @param IDBConnection $connection
  47. * @param IURLGenerator $urlGenerator
  48. * @param ILogger $logger
  49. */
  50. public function __construct(IConfig $config, IDBConnection $connection, IURLGenerator $urlGenerator, ILogger $logger) {
  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) {
  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. }