3 declare(strict_types
=1);
5 namespace PhpMyAdmin\Replication
;
7 use PhpMyAdmin\DatabaseInterface
;
8 use PhpMyAdmin\Query\Compatibility
;
13 final class ReplicationInfo
16 public array $primaryVariables = ['File', 'Position', 'Binlog_Do_DB', 'Binlog_Ignore_DB'];
19 public array $replicaVariables = [
31 'Read_Master_Log_Pos',
32 'Read_Source_Log_Pos',
35 'Relay_Master_Log_File',
36 'Relay_Source_Log_File',
40 'Replica_SQL_Running',
42 'Replicate_Ignore_DB',
44 'Replicate_Ignore_Table',
45 'Replicate_Wild_Do_Table',
46 'Replicate_Wild_Ignore_Table',
50 'Exec_Master_Log_Pos',
51 'Exec_Source_Log_Pos',
68 'Seconds_Behind_Master',
69 'Seconds_Behind_Source',
73 private array $primaryStatus = [];
76 private array $replicaStatus = [];
79 private array $multiPrimaryStatus = [];
82 private array $primaryInfo = [];
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
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');
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
160 private function fill(array $status, string $key): array
162 if (empty($status[0][$key])) {
166 return explode(',', $status[0][$key]);
169 private function setPrimaryInfo(): void
171 $this->primaryInfo
= ['status' => $this->primaryStatus
!== []];
173 if (! $this->primaryInfo
['status']) {
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']) {
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
;