Completed
Push — master ( 622101...91beb1 )
by Joas
08:50
created

Collation::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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) {
0 ignored issues
show
Bug introduced by
The class Doctrine\DBAL\Platforms\MySqlPlatform does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to 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 require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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);
0 ignored issues
show
Documentation introduced by
$this->connection is of type object<OCP\IDBConnection>, but the function expects a object<Doctrine\DBAL\Connection>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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) {
0 ignored issues
show
Bug introduced by
The class Doctrine\DBAL\Exception\DriverException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
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) {
0 ignored issues
show
Bug introduced by
The class Doctrine\DBAL\Exception\DriverException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
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