collation.php 2.4 KB

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