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.

Collation.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Repair;
  25. use Doctrine\DBAL\Exception\DriverException;
  26. use Doctrine\DBAL\Platforms\MySqlPlatform;
  27. use OCP\IConfig;
  28. use OCP\IDBConnection;
  29. use OCP\ILogger;
  30. use OCP\Migration\IOutput;
  31. use OCP\Migration\IRepairStep;
  32. class Collation implements IRepairStep {
  33. /** @var IConfig */
  34. protected $config;
  35. /** @var ILogger */
  36. protected $logger;
  37. /** @var IDBConnection */
  38. protected $connection;
  39. /** @var bool */
  40. protected $ignoreFailures;
  41. /**
  42. * @param IConfig $config
  43. * @param ILogger $logger
  44. * @param IDBConnection $connection
  45. * @param bool $ignoreFailures
  46. */
  47. public function __construct(IConfig $config, ILogger $logger, IDBConnection $connection, $ignoreFailures) {
  48. $this->connection = $connection;
  49. $this->config = $config;
  50. $this->logger = $logger;
  51. $this->ignoreFailures = $ignoreFailures;
  52. }
  53. public function getName() {
  54. return 'Repair MySQL collation';
  55. }
  56. /**
  57. * Fix mime types
  58. */
  59. public function run(IOutput $output) {
  60. if (!$this->connection->getDatabasePlatform() instanceof MySqlPlatform) {
  61. $output->info('Not a mysql database -> nothing to no');
  62. return;
  63. }
  64. $characterSet = $this->config->getSystemValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8';
  65. $tables = $this->getAllNonUTF8BinTables($this->connection);
  66. foreach ($tables as $table) {
  67. $output->info("Change collation for $table ...");
  68. $query = $this->connection->prepare('ALTER TABLE `' . $table . '` CONVERT TO CHARACTER SET ' . $characterSet . ' COLLATE ' . $characterSet . '_bin;');
  69. try {
  70. $query->execute();
  71. } catch (DriverException $e) {
  72. // Just log this
  73. $this->logger->logException($e);
  74. if (!$this->ignoreFailures) {
  75. throw $e;
  76. }
  77. }
  78. }
  79. }
  80. /**
  81. * @param \Doctrine\DBAL\Connection $connection
  82. * @return string[]
  83. */
  84. protected function getAllNonUTF8BinTables($connection) {
  85. $dbName = $this->config->getSystemValue("dbname");
  86. $characterSet = $this->config->getSystemValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8';
  87. $rows = $connection->fetchAll(
  88. "SELECT DISTINCT(TABLE_NAME) AS `table`" .
  89. " FROM INFORMATION_SCHEMA . COLUMNS" .
  90. " WHERE TABLE_SCHEMA = ?" .
  91. " AND (COLLATION_NAME <> '" . $characterSet . "_bin' OR CHARACTER_SET_NAME <> '" . $characterSet . "')" .
  92. " AND TABLE_NAME LIKE \"*PREFIX*%\"",
  93. array($dbName)
  94. );
  95. $result = array();
  96. foreach ($rows as $row) {
  97. $result[] = $row['table'];
  98. }
  99. return $result;
  100. }
  101. }