Collation.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  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 Robin Appelman <robin@icewind.nl>
  9. * @author Robin Müller <coder-hugo@users.noreply.github.com>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Repair;
  28. use Doctrine\DBAL\Exception\DriverException;
  29. use Doctrine\DBAL\Platforms\MySQLPlatform;
  30. use OCP\IConfig;
  31. use OCP\IDBConnection;
  32. use OCP\Migration\IOutput;
  33. use OCP\Migration\IRepairStep;
  34. use Psr\Log\LoggerInterface;
  35. class Collation implements IRepairStep {
  36. /** @var IConfig */
  37. protected $config;
  38. protected LoggerInterface $logger;
  39. /** @var IDBConnection */
  40. protected $connection;
  41. /** @var bool */
  42. protected $ignoreFailures;
  43. /**
  44. * @param bool $ignoreFailures
  45. */
  46. public function __construct(
  47. IConfig $config,
  48. LoggerInterface $logger,
  49. IDBConnection $connection,
  50. $ignoreFailures
  51. ) {
  52. $this->connection = $connection;
  53. $this->config = $config;
  54. $this->logger = $logger;
  55. $this->ignoreFailures = $ignoreFailures;
  56. }
  57. public function getName() {
  58. return 'Repair MySQL collation';
  59. }
  60. /**
  61. * Fix mime types
  62. */
  63. public function run(IOutput $output) {
  64. if (!$this->connection->getDatabasePlatform() instanceof MySQLPlatform) {
  65. $output->info('Not a mysql database -> nothing to do');
  66. return;
  67. }
  68. $characterSet = $this->config->getSystemValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8';
  69. $tables = $this->getAllNonUTF8BinTables($this->connection);
  70. foreach ($tables as $table) {
  71. $output->info("Change row format for $table ...");
  72. $query = $this->connection->prepare('ALTER TABLE `' . $table . '` ROW_FORMAT = DYNAMIC;');
  73. try {
  74. $query->execute();
  75. } catch (DriverException $e) {
  76. // Just log this
  77. $this->logger->error($e->getMessage(), ['exception' => $e]);
  78. if (!$this->ignoreFailures) {
  79. throw $e;
  80. }
  81. }
  82. $output->info("Change collation for $table ...");
  83. $query = $this->connection->prepare('ALTER TABLE `' . $table . '` CONVERT TO CHARACTER SET ' . $characterSet . ' COLLATE ' . $characterSet . '_bin;');
  84. try {
  85. $query->execute();
  86. } catch (DriverException $e) {
  87. // Just log this
  88. $this->logger->error($e->getMessage(), ['exception' => $e]);
  89. if (!$this->ignoreFailures) {
  90. throw $e;
  91. }
  92. }
  93. }
  94. if (empty($tables)) {
  95. $output->info('All tables already have the correct collation -> nothing to do');
  96. }
  97. }
  98. /**
  99. * @param IDBConnection $connection
  100. * @return string[]
  101. */
  102. protected function getAllNonUTF8BinTables(IDBConnection $connection) {
  103. $dbName = $this->config->getSystemValue("dbname");
  104. $characterSet = $this->config->getSystemValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8';
  105. // fetch tables by columns
  106. $statement = $connection->executeQuery(
  107. "SELECT DISTINCT(TABLE_NAME) AS `table`" .
  108. " FROM INFORMATION_SCHEMA . COLUMNS" .
  109. " WHERE TABLE_SCHEMA = ?" .
  110. " AND (COLLATION_NAME <> '" . $characterSet . "_bin' OR CHARACTER_SET_NAME <> '" . $characterSet . "')" .
  111. " AND TABLE_NAME LIKE '*PREFIX*%'",
  112. [$dbName]
  113. );
  114. $rows = $statement->fetchAll();
  115. $result = [];
  116. foreach ($rows as $row) {
  117. $result[$row['table']] = true;
  118. }
  119. // fetch tables by collation
  120. $statement = $connection->executeQuery(
  121. "SELECT DISTINCT(TABLE_NAME) AS `table`" .
  122. " FROM INFORMATION_SCHEMA . TABLES" .
  123. " WHERE TABLE_SCHEMA = ?" .
  124. " AND TABLE_COLLATION <> '" . $characterSet . "_bin'" .
  125. " AND TABLE_NAME LIKE '*PREFIX*%'",
  126. [$dbName]
  127. );
  128. $rows = $statement->fetchAll();
  129. foreach ($rows as $row) {
  130. $result[$row['table']] = true;
  131. }
  132. return array_keys($result);
  133. }
  134. }