Translated using Weblate (Vietnamese)
[phpmyadmin.git] / src / Replication / ReplicationInfo.php
blob73c0d10a21648657cb52144f1c83309340a12534
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin\Replication;
7 use PhpMyAdmin\DatabaseInterface;
8 use PhpMyAdmin\Query\Compatibility;
10 use function explode;
11 use function sprintf;
13 final class ReplicationInfo
15 /** @var string[] */
16 public array $primaryVariables = ['File', 'Position', 'Binlog_Do_DB', 'Binlog_Ignore_DB'];
18 /** @var string[] */
19 public array $replicaVariables = [
20 'Slave_IO_State',
21 'Replica_IO_State',
22 'Master_Host',
23 'Source_Host',
24 'Master_User',
25 'Source_User',
26 'Master_Port',
27 'Source_Port',
28 'Connect_Retry',
29 'Master_Log_File',
30 'Source_Log_File',
31 'Read_Master_Log_Pos',
32 'Read_Source_Log_Pos',
33 'Relay_Log_File',
34 'Relay_Log_Pos',
35 'Relay_Master_Log_File',
36 'Relay_Source_Log_File',
37 'Slave_IO_Running',
38 'Replica_IO_Running',
39 'Slave_SQL_Running',
40 'Replica_SQL_Running',
41 'Replicate_Do_DB',
42 'Replicate_Ignore_DB',
43 'Replicate_Do_Table',
44 'Replicate_Ignore_Table',
45 'Replicate_Wild_Do_Table',
46 'Replicate_Wild_Ignore_Table',
47 'Last_Errno',
48 'Last_Error',
49 'Skip_Counter',
50 'Exec_Master_Log_Pos',
51 'Exec_Source_Log_Pos',
52 'Relay_Log_Space',
53 'Until_Condition',
54 'Until_Log_File',
55 'Until_Log_Pos',
56 'Master_SSL_Allowed',
57 'Source_SSL_Allowed',
58 'Master_SSL_CA_File',
59 'Source_SSL_CA_File',
60 'Master_SSL_CA_Path',
61 'Source_SSL_CA_Path',
62 'Master_SSL_Cert',
63 'Source_SSL_Cert',
64 'Master_SSL_Cipher',
65 'Source_SSL_Cipher',
66 'Master_SSL_Key',
67 'Source_SSL_Key',
68 'Seconds_Behind_Master',
69 'Seconds_Behind_Source',
72 /** @var mixed[] */
73 private array $primaryStatus = [];
75 /** @var mixed[] */
76 private array $replicaStatus = [];
78 /** @var mixed[] */
79 private array $multiPrimaryStatus = [];
81 /** @var mixed[] */
82 private array $primaryInfo = [];
84 /** @var mixed[] */
85 private array $replicaInfo = [];
87 public function __construct(private DatabaseInterface $dbi)
91 public function load(string|null $connection = null): void
93 $GLOBALS['urlParams'] ??= null;
95 $this->setPrimaryStatus();
97 if ($connection !== null && $connection !== '') {
98 $this->setMultiPrimaryStatus();
100 if ($this->multiPrimaryStatus !== []) {
101 $this->setDefaultPrimaryConnection($connection);
102 $GLOBALS['urlParams']['primary_connection'] = $connection;
106 $this->setReplicaStatus();
107 $this->setPrimaryInfo();
108 $this->setReplicaInfo();
111 private function setPrimaryStatus(): void
113 $this->primaryStatus = $this->dbi->fetchResult(Compatibility::getShowBinLogStatusStmt($this->dbi));
116 /** @return mixed[] */
117 public function getPrimaryStatus(): array
119 return $this->primaryStatus;
122 private function setReplicaStatus(): void
124 if (
125 $this->dbi->isMySql() && $this->dbi->getVersion() >= 80022
126 || $this->dbi->isMariaDB() && $this->dbi->getVersion() >= 100501
128 $this->replicaStatus = $this->dbi->fetchResult('SHOW REPLICA STATUS');
129 } else {
130 $this->replicaStatus = $this->dbi->fetchResult('SHOW SLAVE STATUS');
134 /** @return mixed[] */
135 public function getReplicaStatus(): array
137 return $this->replicaStatus;
140 private function setMultiPrimaryStatus(): void
142 $this->multiPrimaryStatus = [];
143 if ($this->dbi->isMariaDB() && $this->dbi->getVersion() >= 100501) {
144 $this->multiPrimaryStatus = $this->dbi->fetchResult('SHOW ALL REPLICAS STATUS');
145 } elseif ($this->dbi->isMariaDB()) {
146 $this->multiPrimaryStatus = $this->dbi->fetchResult('SHOW ALL SLAVES STATUS');
150 private function setDefaultPrimaryConnection(string $connection): void
152 $this->dbi->query(sprintf('SET @@default_master_connection = %s', $this->dbi->quoteString($connection)));
156 * @param mixed[] $status
158 * @return string[]
160 private function fill(array $status, string $key): array
162 if (empty($status[0][$key])) {
163 return [];
166 return explode(',', $status[0][$key]);
169 private function setPrimaryInfo(): void
171 $this->primaryInfo = ['status' => $this->primaryStatus !== []];
173 if (! $this->primaryInfo['status']) {
174 return;
177 $this->primaryInfo['Do_DB'] = $this->fill($this->primaryStatus, 'Binlog_Do_DB');
178 $this->primaryInfo['Ignore_DB'] = $this->fill($this->primaryStatus, 'Binlog_Ignore_DB');
181 /** @return mixed[] */
182 public function getPrimaryInfo(): array
184 return $this->primaryInfo;
187 private function setReplicaInfo(): void
189 $this->replicaInfo = ['status' => $this->replicaStatus !== []];
191 if (! $this->replicaInfo['status']) {
192 return;
195 $this->replicaInfo['Do_DB'] = $this->fill($this->replicaStatus, 'Replicate_Do_DB');
196 $this->replicaInfo['Ignore_DB'] = $this->fill($this->replicaStatus, 'Replicate_Ignore_DB');
197 $this->replicaInfo['Do_Table'] = $this->fill($this->replicaStatus, 'Replicate_Do_Table');
198 $this->replicaInfo['Ignore_Table'] = $this->fill($this->replicaStatus, 'Replicate_Ignore_Table');
199 $this->replicaInfo['Wild_Do_Table'] = $this->fill($this->replicaStatus, 'Replicate_Wild_Do_Table');
200 $this->replicaInfo['Wild_Ignore_Table'] = $this->fill($this->replicaStatus, 'Replicate_Wild_Ignore_Table');
203 /** @return mixed[] */
204 public function getReplicaInfo(): array
206 return $this->replicaInfo;