From d758e062a9fc923334cdc56de2b50d444c073d06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Fri, 13 Apr 2018 18:21:55 -0300 Subject: [PATCH] Refactor PhpMyAdmin\Replication methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces static methods with instance methods. Signed-off-by: MaurĂ­cio Meneghini Fauth --- .../Database/DatabaseStructureController.php | 10 +++++-- libraries/classes/Replication.php | 18 ++++++------ libraries/classes/ReplicationGui.php | 29 ++++++++++++++----- libraries/replication.inc.php | 18 +++++++----- 4 files changed, 48 insertions(+), 27 deletions(-) diff --git a/libraries/classes/Controllers/Database/DatabaseStructureController.php b/libraries/classes/Controllers/Database/DatabaseStructureController.php index ba3edd39e6..b394714093 100644 --- a/libraries/classes/Controllers/Database/DatabaseStructureController.php +++ b/libraries/classes/Controllers/Database/DatabaseStructureController.php @@ -60,6 +60,11 @@ class DatabaseStructureController extends DatabaseController */ private $relation; + /** + * @var Replication + */ + private $replication; + /** * Constructor */ @@ -67,6 +72,7 @@ class DatabaseStructureController extends DatabaseController { parent::__construct($response, $dbi, $db); $this->relation = new Relation(); + $this->replication = new Replication(); } /** @@ -946,10 +952,10 @@ class DatabaseStructureController extends DatabaseController protected function hasTable(array $db, $truename) { foreach ($db as $db_table) { - if ($this->db == Replication::extractDbOrTable($db_table) + if ($this->db == $this->replication->extractDbOrTable($db_table) && preg_match( "@^" . - preg_quote(mb_substr(Replication::extractDbOrTable($db_table, 'table'), 0, -1)) . "@", + preg_quote(mb_substr($this->replication->extractDbOrTable($db_table, 'table'), 0, -1)) . "@", $truename ) ) { diff --git a/libraries/classes/Replication.php b/libraries/classes/Replication.php index d53fada5eb..7f9b6b538d 100644 --- a/libraries/classes/Replication.php +++ b/libraries/classes/Replication.php @@ -27,7 +27,7 @@ class Replication * * @return array */ - public static function fillInfo( + public function fillInfo( $type, $replicationInfoKey, array $mysqlInfo, $mysqlKey ) { $GLOBALS['replication_info'][$type][$replicationInfoKey] @@ -49,7 +49,7 @@ class Replication * * @return string the extracted part */ - public static function extractDbOrTable($string, $what = 'db') + public function extractDbOrTable($string, $what = 'db') { $list = explode(".", $string); if ('db' == $what) { @@ -71,7 +71,7 @@ class Replication * * @return mixed output of DatabaseInterface::tryQuery */ - public static function slaveControl($action, $control = null, $link = null) + public function slaveControl($action, $control = null, $link = null) { $action = mb_strtoupper($action); $control = mb_strtoupper($control); @@ -101,11 +101,11 @@ class Replication * * @return string output of CHANGE MASTER mysql command */ - public static function slaveChangeMaster($user, $password, $host, $port, + public function slaveChangeMaster($user, $password, $host, $port, array $pos, $stop = true, $start = true, $link = null ) { if ($stop) { - self::slaveControl("STOP", null, $link); + $this->slaveControl("STOP", null, $link); } $out = $GLOBALS['dbi']->tryQuery( @@ -119,7 +119,7 @@ class Replication ); if ($start) { - self::slaveControl("START", null, $link); + $this->slaveControl("START", null, $link); } return $out; @@ -136,7 +136,7 @@ class Replication * * @return mixed $link mysql link on success */ - public static function connectToMaster( + public function connectToMaster( $user, $password, $host = null, $port = null, $socket = null ) { $server = array(); @@ -157,9 +157,9 @@ class Replication * @param mixed $link mysql link * * @return array an array containing File and Position in MySQL replication - * on master server, useful for self::slaveChangeMaster + * on master server, useful for slaveChangeMaster() */ - public static function slaveBinLogMaster($link = null) + public function slaveBinLogMaster($link = null) { $data = $GLOBALS['dbi']->fetchResult('SHOW MASTER STATUS', null, null, $link); $output = array(); diff --git a/libraries/classes/ReplicationGui.php b/libraries/classes/ReplicationGui.php index 57550da7a5..3a90a8eadc 100644 --- a/libraries/classes/ReplicationGui.php +++ b/libraries/classes/ReplicationGui.php @@ -21,6 +21,19 @@ use PhpMyAdmin\Util; */ class ReplicationGui { + /** + * @var Replication + */ + private $replication; + + /** + * ReplicationGui constructor. + */ + public function __construct() + { + $this->replication = new Replication(); + } + /** * returns HTML for error message * @@ -986,7 +999,7 @@ class ReplicationGui $_SESSION['replication']['sr_action_info'] = __('Unknown error'); // Attempt to connect to the new master server - $link_to_master = Replication::connectToMaster( + $link_to_master = $this->replication->connectToMaster( $sr['username'], $sr['pma_pw'], $sr['hostname'], $sr['port'] ); @@ -998,7 +1011,7 @@ class ReplicationGui ); } else { // Read the current master position - $position = Replication::slaveBinLogMaster($link_to_master); + $position = $this->replication->slaveBinLogMaster($link_to_master); if (empty($position)) { $_SESSION['replication']['sr_action_status'] = 'error'; @@ -1010,7 +1023,7 @@ class ReplicationGui } else { $_SESSION['replication']['m_correct'] = true; - if (! Replication::slaveChangeMaster( + if (! $this->replication->slaveChangeMaster( $sr['username'], $sr['pma_pw'], $sr['hostname'], @@ -1047,15 +1060,15 @@ class ReplicationGui $_REQUEST['sr_slave_control_parm'] = null; } if ($_REQUEST['sr_slave_action'] == 'reset') { - $qStop = Replication::slaveControl("STOP"); + $qStop = $this->replication->slaveControl("STOP"); $qReset = $GLOBALS['dbi']->tryQuery("RESET SLAVE;"); - $qStart = Replication::slaveControl("START"); + $qStart = $this->replication->slaveControl("START"); $result = ($qStop !== false && $qStop !== -1 && $qReset !== false && $qReset !== -1 && $qStart !== false && $qStart !== -1); } else { - $qControl = Replication::slaveControl( + $qControl = $this->replication->slaveControl( $_REQUEST['sr_slave_action'], $_REQUEST['sr_slave_control_parm'] ); @@ -1078,11 +1091,11 @@ class ReplicationGui $count = $_REQUEST['sr_skip_errors_count'] * 1; } - $qStop = Replication::slaveControl("STOP"); + $qStop = $this->replication->slaveControl("STOP"); $qSkip = $GLOBALS['dbi']->tryQuery( "SET GLOBAL SQL_SLAVE_SKIP_COUNTER = " . $count . ";" ); - $qStart = Replication::slaveControl("START"); + $qStart = $this->replication->slaveControl("START"); $result = ($qStop !== false && $qStop !== -1 && $qSkip !== false && $qSkip !== -1 && diff --git a/libraries/replication.inc.php b/libraries/replication.inc.php index bc3c393c3b..6b29eea2f9 100644 --- a/libraries/replication.inc.php +++ b/libraries/replication.inc.php @@ -11,6 +11,8 @@ if (! defined('PHPMYADMIN')) { use PhpMyAdmin\Replication; +$replication = new Replication(); + /** * get master replication from server */ @@ -128,42 +130,42 @@ foreach ($replication_types as $type) { } if ($GLOBALS['replication_info'][$type]['status']) { if ($type == "master") { - Replication::fillInfo( + $replication->fillInfo( $type, 'Do_DB', $server_master_replication[0], 'Binlog_Do_DB' ); - Replication::fillInfo( + $replication->fillInfo( $type, 'Ignore_DB', $server_master_replication[0], 'Binlog_Ignore_DB' ); } elseif ($type == "slave") { - Replication::fillInfo( + $replication->fillInfo( $type, 'Do_DB', $server_slave_replication[0], 'Replicate_Do_DB' ); - Replication::fillInfo( + $replication->fillInfo( $type, 'Ignore_DB', $server_slave_replication[0], 'Replicate_Ignore_DB' ); - Replication::fillInfo( + $replication->fillInfo( $type, 'Do_Table', $server_slave_replication[0], 'Replicate_Do_Table' ); - Replication::fillInfo( + $replication->fillInfo( $type, 'Ignore_Table', $server_slave_replication[0], 'Replicate_Ignore_Table' ); - Replication::fillInfo( + $replication->fillInfo( $type, 'Wild_Do_Table', $server_slave_replication[0], 'Replicate_Wild_Do_Table' ); - Replication::fillInfo( + $replication->fillInfo( $type, 'Wild_Ignore_Table', $server_slave_replication[0], 'Replicate_Wild_Ignore_Table' );