/
database_charset.php
100 lines (91 loc) · 3.57 KB
/
database_charset.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace Concrete\Controller\SinglePage\Dashboard\System\Environment;
use Concrete\Core\Database\CharacterSetCollation\Exception;
use Concrete\Core\Database\CharacterSetCollation\Manager;
use Concrete\Core\Database\Connection\Connection;
use Concrete\Core\Http\ResponseFactoryInterface;
use Concrete\Core\Page\Controller\DashboardPageController;
use Concrete\Core\Url\Resolver\Manager\ResolverManagerInterface;
class DatabaseCharset extends DashboardPageController
{
public function view()
{
$this->requireAsset('selectize');
$connection = $this->app->make(Connection::class);
$this->set('charsetsAndCollations', $this->listCharsetsAndCollations($connection));
$this->set('collation', $this->getConfiguredCollation($connection));
}
public function set_connection_collation()
{
if (!$this->token->validate(__FUNCTION__)) {
$this->error->add($this->token->getErrorMessage());
} else {
$collation = $this->request->request->get('collation');
$manager = $this->app->make(Manager::class);
$warnings = $this->app->make('error');
try {
$manager->apply('', $collation, '', '', null, $warnings);
} catch (Exception $x) {
$this->errors->add($x);
}
}
if ($this->error->has()) {
$this->view();
} else {
if ($warnings->has()) {
$this->flash('set_connection_collation_warnings', $warnings);
} else {
$this->flash('success', t('The character set and the collation of the connection and all the tables have been updated.'));
}
return $this->app->make(ResponseFactoryInterface::class)->redirect(
$this->app->make(ResolverManagerInterface::class)->resolve([$this->request->getCurrentPage()]),
302
);
}
}
/**
* @param \Concrete\Core\Database\Connection\Connection $connection
*
* @return array
*/
protected function listCharsetsAndCollations(Connection $connection)
{
$charsetsAndDefaultCollation = $connection->getSupportedCharsets();
ksort($charsetsAndDefaultCollation, SORT_NATURAL);
$collationsForCharsets = $connection->getSupportedCollations();
ksort($collationsForCharsets, SORT_NATURAL);
$result = [];
foreach ($charsetsAndDefaultCollation as $charset => $defaultCollation) {
$collations = [
$defaultCollation => $defaultCollation,
];
foreach ($collationsForCharsets as $collation => $forCharset) {
if ($forCharset === $charset && $collation !== $defaultCollation) {
$collations[$collation] = $collation;
}
}
$result[t('Character set: %s', $charset)] = $collations;
}
return $result;
}
/**
* @param \Concrete\Core\Database\Connection\Connection $connection
*
* @return string
*/
protected function getConfiguredCollation(Connection $connection)
{
$params = $connection->getParams();
if (!empty($params['collation'])) {
return $params['collation'];
}
// legacy support
if (!empty($params['charset'])) {
$charsetsAndDefaultCollation = $connection->getSupportedCharsets();
if (isset($charsetsAndDefaultCollation[$params['charset']])) {
return $charsetsAndDefaultCollation[$params['charset']];
}
}
return '';
}
}