|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Morris Jobke <[email protected]> |
|
6
|
|
|
* @author Robin Appelman <[email protected]> |
|
7
|
|
|
* @author Thomas Müller <[email protected]> |
|
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
|
|
|
|
|
25
|
|
|
namespace OC\Repair; |
|
26
|
|
|
|
|
27
|
|
|
use Doctrine\DBAL\Exception\DriverException; |
|
28
|
|
|
use Doctrine\DBAL\Platforms\MySqlPlatform; |
|
29
|
|
|
use OCP\IConfig; |
|
30
|
|
|
use OCP\IDBConnection; |
|
31
|
|
|
use OCP\ILogger; |
|
32
|
|
|
use OCP\Migration\IOutput; |
|
33
|
|
|
use OCP\Migration\IRepairStep; |
|
34
|
|
|
|
|
35
|
|
|
class Collation implements IRepairStep { |
|
36
|
|
|
/** @var IConfig */ |
|
37
|
|
|
protected $config; |
|
38
|
|
|
|
|
39
|
|
|
/** @var ILogger */ |
|
40
|
|
|
protected $logger; |
|
41
|
|
|
|
|
42
|
|
|
/** @var IDBConnection */ |
|
43
|
|
|
protected $connection; |
|
44
|
|
|
|
|
45
|
|
|
/** @var bool */ |
|
46
|
|
|
protected $ignoreFailures; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param IConfig $config |
|
50
|
|
|
* @param ILogger $logger |
|
51
|
|
|
* @param IDBConnection $connection |
|
52
|
|
|
* @param bool $ignoreFailures |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct(IConfig $config, ILogger $logger, IDBConnection $connection, $ignoreFailures) { |
|
55
|
|
|
$this->connection = $connection; |
|
56
|
|
|
$this->config = $config; |
|
57
|
|
|
$this->logger = $logger; |
|
58
|
|
|
$this->ignoreFailures = $ignoreFailures; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function getName() { |
|
62
|
|
|
return 'Repair MySQL collation'; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Fix mime types |
|
67
|
|
|
*/ |
|
68
|
|
|
public function run(IOutput $output) { |
|
69
|
|
|
if (!$this->connection->getDatabasePlatform() instanceof MySqlPlatform) { |
|
|
|
|
|
|
70
|
|
|
$output->info('Not a mysql database -> nothing to no'); |
|
71
|
|
|
return; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$characterSet = $this->config->getSystemValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8'; |
|
75
|
|
|
|
|
76
|
|
|
$tables = $this->getAllNonUTF8BinTables($this->connection); |
|
|
|
|
|
|
77
|
|
|
foreach ($tables as $table) { |
|
78
|
|
|
$output->info("Change row format for $table ..."); |
|
79
|
|
|
$query = $this->connection->prepare('ALTER TABLE `' . $table . '` ROW_FORMAT = DYNAMIC;'); |
|
80
|
|
|
try { |
|
81
|
|
|
$query->execute(); |
|
82
|
|
|
} catch (DriverException $e) { |
|
|
|
|
|
|
83
|
|
|
// Just log this |
|
84
|
|
|
$this->logger->logException($e); |
|
85
|
|
|
if (!$this->ignoreFailures) { |
|
86
|
|
|
throw $e; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$output->info("Change collation for $table ..."); |
|
91
|
|
|
$query = $this->connection->prepare('ALTER TABLE `' . $table . '` CONVERT TO CHARACTER SET ' . $characterSet . ' COLLATE ' . $characterSet . '_bin;'); |
|
92
|
|
|
try { |
|
93
|
|
|
$query->execute(); |
|
94
|
|
|
} catch (DriverException $e) { |
|
|
|
|
|
|
95
|
|
|
// Just log this |
|
96
|
|
|
$this->logger->logException($e); |
|
97
|
|
|
if (!$this->ignoreFailures) { |
|
98
|
|
|
throw $e; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param \Doctrine\DBAL\Connection $connection |
|
106
|
|
|
* @return string[] |
|
107
|
|
|
*/ |
|
108
|
|
|
protected function getAllNonUTF8BinTables($connection) { |
|
109
|
|
|
$dbName = $this->config->getSystemValue("dbname"); |
|
110
|
|
|
$characterSet = $this->config->getSystemValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8'; |
|
111
|
|
|
$rows = $connection->fetchAll( |
|
112
|
|
|
"SELECT DISTINCT(TABLE_NAME) AS `table`" . |
|
113
|
|
|
" FROM INFORMATION_SCHEMA . COLUMNS" . |
|
114
|
|
|
" WHERE TABLE_SCHEMA = ?" . |
|
115
|
|
|
" AND (COLLATION_NAME <> '" . $characterSet . "_bin' OR CHARACTER_SET_NAME <> '" . $characterSet . "')" . |
|
116
|
|
|
" AND TABLE_NAME LIKE \"*PREFIX*%\"", |
|
117
|
|
|
array($dbName) |
|
118
|
|
|
); |
|
119
|
|
|
$result = array(); |
|
120
|
|
|
foreach ($rows as $row) { |
|
121
|
|
|
$result[] = $row['table']; |
|
122
|
|
|
} |
|
123
|
|
|
return $result; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.