Translated using Weblate (Portuguese)
[phpmyadmin.git] / src / Replication / ReplicationInfo.php
blobab4a2f7d8311fb446a7f7e386d552e2cd0e337cd
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin\Replication;
7 use PhpMyAdmin\DatabaseInterface;
9 use function explode;
10 use function sprintf;
12 final class ReplicationInfo
14 /** @var string[] */
15 public array $primaryVariables = ['File', 'Position', 'Binlog_Do_DB', 'Binlog_Ignore_DB'];
17 /** @var string[] */
18 public array $replicaVariables = [
19 'Slave_IO_State',
20 'Master_Host',
21 'Master_User',
22 'Master_Port',
23 'Connect_Retry',
24 'Master_Log_File',
25 'Read_Master_Log_Pos',
26 'Relay_Log_File',
27 'Relay_Log_Pos',
28 'Relay_Master_Log_File',
29 'Slave_IO_Running',
30 'Slave_SQL_Running',
31 'Replicate_Do_DB',
32 'Replicate_Ignore_DB',
33 'Replicate_Do_Table',
34 'Replicate_Ignore_Table',
35 'Replicate_Wild_Do_Table',
36 'Replicate_Wild_Ignore_Table',
37 'Last_Errno',
38 'Last_Error',
39 'Skip_Counter',
40 'Exec_Master_Log_Pos',
41 'Relay_Log_Space',
42 'Until_Condition',
43 'Until_Log_File',
44 'Until_Log_Pos',
45 'Master_SSL_Allowed',
46 'Master_SSL_CA_File',
47 'Master_SSL_CA_Path',
48 'Master_SSL_Cert',
49 'Master_SSL_Cipher',
50 'Master_SSL_Key',
51 'Seconds_Behind_Master',
54 /** @var mixed[] */
55 private array $primaryStatus = [];
57 /** @var mixed[] */
58 private array $replicaStatus = [];
60 /** @var mixed[] */
61 private array $multiPrimaryStatus = [];
63 /** @var mixed[] */
64 private array $primaryInfo = [];
66 /** @var mixed[] */
67 private array $replicaInfo = [];
69 public function __construct(private DatabaseInterface $dbi)
73 public function load(string|null $connection = null): void
75 $GLOBALS['urlParams'] ??= null;
77 $this->setPrimaryStatus();
79 if ($connection !== null && $connection !== '') {
80 $this->setMultiPrimaryStatus();
82 if ($this->multiPrimaryStatus !== []) {
83 $this->setDefaultPrimaryConnection($connection);
84 $GLOBALS['urlParams']['primary_connection'] = $connection;
88 $this->setReplicaStatus();
89 $this->setPrimaryInfo();
90 $this->setReplicaInfo();
93 private function setPrimaryStatus(): void
95 $this->primaryStatus = $this->dbi->fetchResult('SHOW MASTER STATUS');
98 /** @return mixed[] */
99 public function getPrimaryStatus(): array
101 return $this->primaryStatus;
104 private function setReplicaStatus(): void
106 $this->replicaStatus = $this->dbi->fetchResult('SHOW SLAVE STATUS');
109 /** @return mixed[] */
110 public function getReplicaStatus(): array
112 return $this->replicaStatus;
115 private function setMultiPrimaryStatus(): void
117 $this->multiPrimaryStatus = $this->dbi->fetchResult('SHOW ALL SLAVES STATUS');
120 private function setDefaultPrimaryConnection(string $connection): void
122 $this->dbi->query(sprintf('SET @@default_master_connection = %s', $this->dbi->quoteString($connection)));
126 * @param mixed[] $status
128 * @return string[]
130 private function fill(array $status, string $key): array
132 if (empty($status[0][$key])) {
133 return [];
136 return explode(',', $status[0][$key]);
139 private function setPrimaryInfo(): void
141 $this->primaryInfo = ['status' => $this->primaryStatus !== []];
143 if (! $this->primaryInfo['status']) {
144 return;
147 $this->primaryInfo['Do_DB'] = $this->fill($this->primaryStatus, 'Binlog_Do_DB');
148 $this->primaryInfo['Ignore_DB'] = $this->fill($this->primaryStatus, 'Binlog_Ignore_DB');
151 /** @return mixed[] */
152 public function getPrimaryInfo(): array
154 return $this->primaryInfo;
157 private function setReplicaInfo(): void
159 $this->replicaInfo = ['status' => $this->replicaStatus !== []];
161 if (! $this->replicaInfo['status']) {
162 return;
165 $this->replicaInfo['Do_DB'] = $this->fill($this->replicaStatus, 'Replicate_Do_DB');
166 $this->replicaInfo['Ignore_DB'] = $this->fill($this->replicaStatus, 'Replicate_Ignore_DB');
167 $this->replicaInfo['Do_Table'] = $this->fill($this->replicaStatus, 'Replicate_Do_Table');
168 $this->replicaInfo['Ignore_Table'] = $this->fill($this->replicaStatus, 'Replicate_Ignore_Table');
169 $this->replicaInfo['Wild_Do_Table'] = $this->fill($this->replicaStatus, 'Replicate_Wild_Do_Table');
170 $this->replicaInfo['Wild_Ignore_Table'] = $this->fill($this->replicaStatus, 'Replicate_Wild_Ignore_Table');
173 /** @return mixed[] */
174 public function getReplicaInfo(): array
176 return $this->replicaInfo;