ReplicationInfo.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin;
  4. use PhpMyAdmin\Query\Compatibility;
  5. use function count;
  6. use function explode;
  7. use function sprintf;
  8. final class ReplicationInfo
  9. {
  10. /** @var string[] */
  11. public $primaryVariables = [
  12. 'File',
  13. 'Position',
  14. 'Binlog_Do_DB',
  15. 'Binlog_Ignore_DB',
  16. ];
  17. /** @var string[] */
  18. public $replicaVariables = [
  19. 'Slave_IO_State',
  20. 'Replica_IO_State',
  21. 'Master_Host',
  22. 'Source_Host',
  23. 'Master_User',
  24. 'Source_User',
  25. 'Master_Port',
  26. 'Source_Port',
  27. 'Connect_Retry',
  28. 'Master_Log_File',
  29. 'Source_Log_File',
  30. 'Read_Master_Log_Pos',
  31. 'Read_Source_Log_Pos',
  32. 'Relay_Log_File',
  33. 'Relay_Log_Pos',
  34. 'Relay_Master_Log_File',
  35. 'Relay_Source_Log_File',
  36. 'Slave_IO_Running',
  37. 'Replica_IO_Running',
  38. 'Slave_SQL_Running',
  39. 'Replica_SQL_Running',
  40. 'Replicate_Do_DB',
  41. 'Replicate_Ignore_DB',
  42. 'Replicate_Do_Table',
  43. 'Replicate_Ignore_Table',
  44. 'Replicate_Wild_Do_Table',
  45. 'Replicate_Wild_Ignore_Table',
  46. 'Last_Errno',
  47. 'Last_Error',
  48. 'Skip_Counter',
  49. 'Exec_Master_Log_Pos',
  50. 'Exec_Source_Log_Pos',
  51. 'Relay_Log_Space',
  52. 'Until_Condition',
  53. 'Until_Log_File',
  54. 'Until_Log_Pos',
  55. 'Master_SSL_Allowed',
  56. 'Source_SSL_Allowed',
  57. 'Master_SSL_CA_File',
  58. 'Source_SSL_CA_File',
  59. 'Master_SSL_CA_Path',
  60. 'Source_SSL_CA_Path',
  61. 'Master_SSL_Cert',
  62. 'Source_SSL_Cert',
  63. 'Master_SSL_Cipher',
  64. 'Source_SSL_Cipher',
  65. 'Master_SSL_Key',
  66. 'Source_SSL_Key',
  67. 'Seconds_Behind_Master',
  68. 'Seconds_Behind_Source',
  69. ];
  70. /** @var array */
  71. private $primaryStatus = [];
  72. /** @var array */
  73. private $replicaStatus = [];
  74. /** @var array */
  75. private $multiPrimaryStatus = [];
  76. /** @var array */
  77. private $primaryInfo = [];
  78. /** @var array */
  79. private $replicaInfo = [];
  80. /** @var DatabaseInterface */
  81. private $dbi;
  82. public function __construct(DatabaseInterface $dbi)
  83. {
  84. $this->dbi = $dbi;
  85. }
  86. public function load(?string $connection = null): void
  87. {
  88. global $urlParams;
  89. $this->setPrimaryStatus();
  90. if (! empty($connection)) {
  91. $this->setMultiPrimaryStatus();
  92. if ($this->multiPrimaryStatus) {
  93. $this->setDefaultPrimaryConnection($connection);
  94. $urlParams['primary_connection'] = $connection;
  95. }
  96. }
  97. $this->setReplicaStatus();
  98. $this->setPrimaryInfo();
  99. $this->setReplicaInfo();
  100. }
  101. private function setPrimaryStatus(): void
  102. {
  103. $this->primaryStatus = $this->dbi->fetchResult(Compatibility::getShowBinLogStatusStmt($this->dbi));
  104. }
  105. public function getPrimaryStatus(): array
  106. {
  107. return $this->primaryStatus;
  108. }
  109. private function setReplicaStatus(): void
  110. {
  111. if (
  112. $this->dbi->isMySql() && $this->dbi->getVersion() >= 80022
  113. || $this->dbi->isMariaDB() && $this->dbi->getVersion() >= 100501
  114. ) {
  115. $this->replicaStatus = $this->dbi->fetchResult('SHOW REPLICA STATUS');
  116. } else {
  117. $this->replicaStatus = $this->dbi->fetchResult('SHOW SLAVE STATUS');
  118. }
  119. }
  120. public function getReplicaStatus(): array
  121. {
  122. return $this->replicaStatus;
  123. }
  124. private function setMultiPrimaryStatus(): void
  125. {
  126. $this->multiPrimaryStatus = [];
  127. if ($this->dbi->isMariaDB() && $this->dbi->getVersion() >= 100501) {
  128. $this->multiPrimaryStatus = $this->dbi->fetchResult('SHOW ALL REPLICAS STATUS');
  129. } elseif ($this->dbi->isMariaDB()) {
  130. $this->multiPrimaryStatus = $this->dbi->fetchResult('SHOW ALL SLAVES STATUS');
  131. }
  132. }
  133. private function setDefaultPrimaryConnection(string $connection): void
  134. {
  135. $this->dbi->query(sprintf('SET @@default_master_connection = \'%s\'', $this->dbi->escapeString($connection)));
  136. }
  137. private static function fill(array $status, string $key): array
  138. {
  139. if (empty($status[0][$key])) {
  140. return [];
  141. }
  142. return explode(',', $status[0][$key]);
  143. }
  144. private function setPrimaryInfo(): void
  145. {
  146. $this->primaryInfo = ['status' => false];
  147. if (count($this->primaryStatus) > 0) {
  148. $this->primaryInfo['status'] = true;
  149. }
  150. if (! $this->primaryInfo['status']) {
  151. return;
  152. }
  153. $this->primaryInfo['Do_DB'] = self::fill($this->primaryStatus, 'Binlog_Do_DB');
  154. $this->primaryInfo['Ignore_DB'] = self::fill($this->primaryStatus, 'Binlog_Ignore_DB');
  155. }
  156. /**
  157. * @return array
  158. */
  159. public function getPrimaryInfo(): array
  160. {
  161. return $this->primaryInfo;
  162. }
  163. private function setReplicaInfo(): void
  164. {
  165. $this->replicaInfo = ['status' => false];
  166. if (count($this->replicaStatus) > 0) {
  167. $this->replicaInfo['status'] = true;
  168. }
  169. if (! $this->replicaInfo['status']) {
  170. return;
  171. }
  172. $this->replicaInfo['Do_DB'] = self::fill($this->replicaStatus, 'Replicate_Do_DB');
  173. $this->replicaInfo['Ignore_DB'] = self::fill($this->replicaStatus, 'Replicate_Ignore_DB');
  174. $this->replicaInfo['Do_Table'] = self::fill($this->replicaStatus, 'Replicate_Do_Table');
  175. $this->replicaInfo['Ignore_Table'] = self::fill($this->replicaStatus, 'Replicate_Ignore_Table');
  176. $this->replicaInfo['Wild_Do_Table'] = self::fill($this->replicaStatus, 'Replicate_Wild_Do_Table');
  177. $this->replicaInfo['Wild_Ignore_Table'] = self::fill($this->replicaStatus, 'Replicate_Wild_Ignore_Table');
  178. }
  179. /**
  180. * @return array
  181. */
  182. public function getReplicaInfo(): array
  183. {
  184. return $this->replicaInfo;
  185. }
  186. }