3 declare(strict_types
=1);
5 namespace PhpMyAdmin\Replication
;
7 use PhpMyAdmin\DatabaseInterface
;
12 final class ReplicationInfo
15 public array $primaryVariables = ['File', 'Position', 'Binlog_Do_DB', 'Binlog_Ignore_DB'];
18 public array $replicaVariables = [
25 'Read_Master_Log_Pos',
28 'Relay_Master_Log_File',
32 'Replicate_Ignore_DB',
34 'Replicate_Ignore_Table',
35 'Replicate_Wild_Do_Table',
36 'Replicate_Wild_Ignore_Table',
40 'Exec_Master_Log_Pos',
51 'Seconds_Behind_Master',
55 private array $primaryStatus = [];
58 private array $replicaStatus = [];
61 private array $multiPrimaryStatus = [];
64 private array $primaryInfo = [];
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
130 private function fill(array $status, string $key): array
132 if (empty($status[0][$key])) {
136 return explode(',', $status[0][$key]);
139 private function setPrimaryInfo(): void
141 $this->primaryInfo
= ['status' => $this->primaryStatus
!== []];
143 if (! $this->primaryInfo
['status']) {
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']) {
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
;