|
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\ILogger; |
|
31
|
|
|
use OCP\IURLGenerator; |
|
32
|
|
|
use Symfony\Component\Console\Command\Command; |
|
33
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
34
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
35
|
|
|
|
|
36
|
|
|
class ConvertMysqlToMB4 extends Command { |
|
37
|
|
|
/** @var IConfig */ |
|
38
|
|
|
private $config; |
|
39
|
|
|
|
|
40
|
|
|
/** @var IDBConnection */ |
|
41
|
|
|
private $connection; |
|
42
|
|
|
|
|
43
|
|
|
/** @var IURLGenerator */ |
|
44
|
|
|
private $urlGenerator; |
|
45
|
|
|
|
|
46
|
|
|
/** @var ILogger */ |
|
47
|
|
|
private $logger; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param IConfig $config |
|
51
|
|
|
* @param IDBConnection $connection |
|
52
|
|
|
* @param IURLGenerator $urlGenerator |
|
53
|
|
|
* @param ILogger $logger |
|
54
|
|
|
*/ |
|
55
|
|
View Code Duplication |
public function __construct(IConfig $config, IDBConnection $connection, IURLGenerator $urlGenerator, ILogger $logger) { |
|
|
|
|
|
|
56
|
|
|
$this->config = $config; |
|
57
|
|
|
$this->connection = $connection; |
|
58
|
|
|
$this->urlGenerator = $urlGenerator; |
|
59
|
|
|
$this->logger = $logger; |
|
60
|
|
|
parent::__construct(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
protected function configure() { |
|
64
|
|
|
$this |
|
65
|
|
|
->setName('db:convert-mysql-charset') |
|
66
|
|
|
->setDescription('Convert charset of MySQL/MariaDB to use utf8mb4'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) { |
|
70
|
|
|
if (!$this->connection->getDatabasePlatform() instanceof MySqlPlatform) { |
|
|
|
|
|
|
71
|
|
|
$output->writeln("This command is only valid for MySQL/MariaDB databases."); |
|
72
|
|
|
return 1; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$tools = new MySqlTools(); |
|
76
|
|
|
if (!$tools->supports4ByteCharset($this->connection)) { |
|
77
|
|
|
$url = $this->urlGenerator->linkToDocs('admin-mysql-utf8mb4'); |
|
78
|
|
|
$output->writeln("The database is not properly setup to use the charset utf8mb4."); |
|
79
|
|
|
$output->writeln("For more information please read the documentation at $url"); |
|
80
|
|
|
return 1; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// enable charset |
|
84
|
|
|
$this->config->setSystemValue('mysql.utf8mb4', true); |
|
85
|
|
|
|
|
86
|
|
|
// run conversion |
|
87
|
|
|
$coll = new Collation($this->config, $this->logger, $this->connection, false); |
|
88
|
|
|
$coll->run(new ConsoleOutput($output)); |
|
89
|
|
|
|
|
90
|
|
|
return 0; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.