Collation.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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\Platforms\MySqlPlatform;
  26. use OCP\Migration\IOutput;
  27. use OCP\Migration\IRepairStep;
  28. class Collation implements IRepairStep {
  29. /**
  30. * @var \OCP\IConfig
  31. */
  32. protected $config;
  33. /**
  34. * @var \OC\DB\Connection
  35. */
  36. protected $connection;
  37. /**
  38. * @param \OCP\IConfig $config
  39. * @param \OC\DB\Connection $connection
  40. */
  41. public function __construct($config, $connection) {
  42. $this->connection = $connection;
  43. $this->config = $config;
  44. }
  45. public function getName() {
  46. return 'Repair MySQL collation';
  47. }
  48. /**
  49. * Fix mime types
  50. */
  51. public function run(IOutput $output) {
  52. if (!$this->connection->getDatabasePlatform() instanceof MySqlPlatform) {
  53. $output->info('Not a mysql database -> nothing to no');
  54. return;
  55. }
  56. $tables = $this->getAllNonUTF8BinTables($this->connection);
  57. foreach ($tables as $table) {
  58. $output->info("Change collation for $table ...");
  59. $query = $this->connection->prepare('ALTER TABLE `' . $table . '` CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin;');
  60. $query->execute();
  61. }
  62. }
  63. /**
  64. * @param \Doctrine\DBAL\Connection $connection
  65. * @return string[]
  66. */
  67. protected function getAllNonUTF8BinTables($connection) {
  68. $dbName = $this->config->getSystemValue("dbname");
  69. $rows = $connection->fetchAll(
  70. "SELECT DISTINCT(TABLE_NAME) AS `table`" .
  71. " FROM INFORMATION_SCHEMA . COLUMNS" .
  72. " WHERE TABLE_SCHEMA = ?" .
  73. " AND (COLLATION_NAME <> 'utf8_bin' OR CHARACTER_SET_NAME <> 'utf8')" .
  74. " AND TABLE_NAME LIKE \"*PREFIX*%\"",
  75. array($dbName)
  76. );
  77. $result = array();
  78. foreach ($rows as $row) {
  79. $result[] = $row['table'];
  80. }
  81. return $result;
  82. }
  83. }