Completed
Push — master ( 599977...a7d112 )
by Thomas
10:10
created

ConvertMysqlToMB4::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Thomas Müller <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2017, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OC\Core\Command\Db;
23
24
use Doctrine\DBAL\Platforms\MySqlPlatform;
25
use OC\DB\MySqlTools;
26
use OC\Migration\ConsoleOutput;
27
use OC\Repair\Collation;
28
use OCP\IConfig;
29
use OCP\IDBConnection;
30
use OCP\IURLGenerator;
31
use Symfony\Component\Console\Command\Command;
32
use Symfony\Component\Console\Input\InputInterface;
33
use Symfony\Component\Console\Output\OutputInterface;
34
35
class ConvertMysqlToMB4 extends Command {
36
	/** @var IConfig */
37
	private $config;
38
39
	/** @var IDBConnection */
40
	private $connection;
41
42
	/** @var IURLGenerator */
43
	private $urlGenerator;
44
45
	/**
46
	 * @param IConfig $config
47
	 * @param IDBConnection $connection
48
	 */
49
	public function __construct(IConfig $config, IDBConnection $connection, IURLGenerator $urlGenerator) {
50
		$this->config = $config;
51
		$this->connection = $connection;
52
		$this->urlGenerator = $urlGenerator;
53
		parent::__construct();
54
	}
55
56
	protected function configure() {
57
		$this
58
			->setName('db:convert-mysql-charset')
59
			->setDescription('Convert charset of MySQL/MariaDB to use utf8mb4');
60
	}
61
62
	protected function execute(InputInterface $input, OutputInterface $output) {
63
		if (!$this->connection->getDatabasePlatform() instanceof MySqlPlatform) {
64
			$output->writeln("This command is only valid for MySQL/MariaDB databases.");
65
			return 1;
66
		}
67
68
		$tools = new MySqlTools();
69
		if (!$tools->supports4ByteCharset($this->connection)) {
70
			$url = $this->urlGenerator->linkToDocs('admin-db-conversion');
71
			$output->writeln("The database is not properly setup to use the charset utf8mb4.");
72
			$output->writeln("For more information please read the documentation at $url");
73
			return 1;
74
		}
75
76
		// enable charset
77
		$this->config->setSystemValue('mysql.utf8mb4', true);
78
79
		// run conversion
80
		$coll = new Collation($this->config, $this->connection);
81
		$coll->run(new ConsoleOutput($output));
82
83
		return 0;
84
	}
85
}
86