From 08afb43653d14b020bd00dbaa483034d4e0a08f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Tue, 7 Jan 2020 14:31:46 -0300 Subject: [PATCH] Remove replication.inc.php file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Extracts the replication.inc.php code to the ReplicationInfo class and removes the file. Signed-off-by: MaurĂ­cio Meneghini Fauth --- .../Database/StructureController.php | 3 +- .../Server/DatabasesController.php | 3 +- .../Server/ReplicationController.php | 3 +- libraries/classes/Replication.php | 27 -- libraries/classes/ReplicationInfo.php | 230 ++++++++++++++++++ libraries/classes/Server/Status/Data.php | 3 +- libraries/replication.inc.php | 197 --------------- .../Server/Status/AdvisorControllerTest.php | 3 +- test/classes/ReplicationGuiTest.php | 8 +- 9 files changed, 243 insertions(+), 234 deletions(-) create mode 100644 libraries/classes/ReplicationInfo.php delete mode 100644 libraries/replication.inc.php diff --git a/libraries/classes/Controllers/Database/StructureController.php b/libraries/classes/Controllers/Database/StructureController.php index 8ee9b7be47..2efe342066 100644 --- a/libraries/classes/Controllers/Database/StructureController.php +++ b/libraries/classes/Controllers/Database/StructureController.php @@ -18,6 +18,7 @@ use PhpMyAdmin\Message; use PhpMyAdmin\RecentFavoriteTable; use PhpMyAdmin\Relation; use PhpMyAdmin\Replication; +use PhpMyAdmin\ReplicationInfo; use PhpMyAdmin\Response; use PhpMyAdmin\Sanitize; use PhpMyAdmin\Template; @@ -149,7 +150,7 @@ class StructureController extends AbstractController Core::sendHeaderLocation($uri); } - include_once ROOT_PATH . 'libraries/replication.inc.php'; + ReplicationInfo::load(); PageSettings::showGroup('DbStructure'); diff --git a/libraries/classes/Controllers/Server/DatabasesController.php b/libraries/classes/Controllers/Server/DatabasesController.php index cf20571ff2..9546093e3f 100644 --- a/libraries/classes/Controllers/Server/DatabasesController.php +++ b/libraries/classes/Controllers/Server/DatabasesController.php @@ -16,6 +16,7 @@ use PhpMyAdmin\Controllers\AbstractController; use PhpMyAdmin\DatabaseInterface; use PhpMyAdmin\Html\Generator; use PhpMyAdmin\Message; +use PhpMyAdmin\ReplicationInfo; use PhpMyAdmin\Response; use PhpMyAdmin\Template; use PhpMyAdmin\Url; @@ -87,8 +88,8 @@ class DatabasesController extends AbstractController $scripts = $header->getScripts(); $scripts->addFile('server/databases.js'); - include_once ROOT_PATH . 'libraries/replication.inc.php'; include_once ROOT_PATH . 'libraries/server_common.inc.php'; + ReplicationInfo::load(); $this->setSortDetails($params['sort_by'], $params['sort_order']); $this->hasStatistics = ! empty($params['statistics']); diff --git a/libraries/classes/Controllers/Server/ReplicationController.php b/libraries/classes/Controllers/Server/ReplicationController.php index eecd10fcfd..24899c53d8 100644 --- a/libraries/classes/Controllers/Server/ReplicationController.php +++ b/libraries/classes/Controllers/Server/ReplicationController.php @@ -11,6 +11,7 @@ namespace PhpMyAdmin\Controllers\Server; use PhpMyAdmin\Controllers\AbstractController; use PhpMyAdmin\DatabaseInterface; use PhpMyAdmin\ReplicationGui; +use PhpMyAdmin\ReplicationInfo; use PhpMyAdmin\Response; use PhpMyAdmin\Template; @@ -46,7 +47,7 @@ class ReplicationController extends AbstractController global $replication_info, $server_slave_replication, $url_params; require_once ROOT_PATH . 'libraries/server_common.inc.php'; - require_once ROOT_PATH . 'libraries/replication.inc.php'; + ReplicationInfo::load(); $header = $this->response->getHeader(); $scripts = $header->getScripts(); diff --git a/libraries/classes/Replication.php b/libraries/classes/Replication.php index 611b62fe13..2d0a62cb13 100644 --- a/libraries/classes/Replication.php +++ b/libraries/classes/Replication.php @@ -15,33 +15,6 @@ namespace PhpMyAdmin; */ class Replication { - /** - * Fill global replication_info variable. - * - * @param string $type Type: master, slave - * @param string $replicationInfoKey Key in replication_info variable - * @param array $mysqlInfo MySQL data about replication - * @param string $mysqlKey MySQL key - * - * @return array - */ - public function fillInfo( - $type, - $replicationInfoKey, - array $mysqlInfo, - $mysqlKey - ) { - $GLOBALS['replication_info'][$type][$replicationInfoKey] - = empty($mysqlInfo[$mysqlKey]) - ? [] - : explode( - ',', - $mysqlInfo[$mysqlKey] - ); - - return $GLOBALS['replication_info'][$type][$replicationInfoKey]; - } - /** * Extracts database or table name from string * diff --git a/libraries/classes/ReplicationInfo.php b/libraries/classes/ReplicationInfo.php new file mode 100644 index 0000000000..6c31b1a050 --- /dev/null +++ b/libraries/classes/ReplicationInfo.php @@ -0,0 +1,230 @@ +fetchResult('SHOW MASTER STATUS'); + + /** + * set selected master server + */ + if (! empty($_POST['master_connection'])) { + /** + * check for multi-master replication functionality + */ + $server_slave_multi_replication = $dbi->fetchResult( + 'SHOW ALL SLAVES STATUS' + ); + if ($server_slave_multi_replication) { + $dbi->query( + "SET @@default_master_connection = '" + . $dbi->escapeString( + $_POST['master_connection'] + ) . "'" + ); + $url_params['master_connection'] = $_POST['master_connection']; + } + } + + /** + * get slave replication from server + */ + $server_slave_replication = $dbi->fetchResult('SHOW SLAVE STATUS'); + + /** + * replication types + */ + $replication_types = [ + 'master', + 'slave', + ]; + + /** + * define variables for master status + */ + $master_variables = [ + 'File', + 'Position', + 'Binlog_Do_DB', + 'Binlog_Ignore_DB', + ]; + + /** + * Define variables for slave status + */ + $slave_variables = [ + 'Slave_IO_State', + 'Master_Host', + 'Master_User', + 'Master_Port', + 'Connect_Retry', + 'Master_Log_File', + 'Read_Master_Log_Pos', + 'Relay_Log_File', + 'Relay_Log_Pos', + 'Relay_Master_Log_File', + 'Slave_IO_Running', + 'Slave_SQL_Running', + 'Replicate_Do_DB', + 'Replicate_Ignore_DB', + 'Replicate_Do_Table', + 'Replicate_Ignore_Table', + 'Replicate_Wild_Do_Table', + 'Replicate_Wild_Ignore_Table', + 'Last_Errno', + 'Last_Error', + 'Skip_Counter', + 'Exec_Master_Log_Pos', + 'Relay_Log_Space', + 'Until_Condition', + 'Until_Log_File', + 'Until_Log_Pos', + 'Master_SSL_Allowed', + 'Master_SSL_CA_File', + 'Master_SSL_CA_Path', + 'Master_SSL_Cert', + 'Master_SSL_Cipher', + 'Master_SSL_Key', + 'Seconds_Behind_Master', + ]; + /** + * define important variables, which need to be watched for + * correct running of replication in slave mode + * + * @usedby PhpMyAdmin\ReplicationGui->getHtmlForReplicationStatusTable() + */ + // TODO change to regexp or something, to allow for negative match. + // To e.g. highlight 'Last_Error' + // + $slave_variables_alerts = [ + 'Slave_IO_Running' => 'No', + 'Slave_SQL_Running' => 'No', + ]; + $slave_variables_oks = [ + 'Slave_IO_Running' => 'Yes', + 'Slave_SQL_Running' => 'Yes', + ]; + + // check which replication is available and + // set $server_{master/slave}_status and assign values + + // replication info is more easily passed to functions + $replication_info = []; + + foreach ($replication_types as $type) { + if (count(${'server_' . $type . '_replication'}) > 0) { + $replication_info[$type]['status'] = true; + } else { + $replication_info[$type]['status'] = false; + } + if ($replication_info[$type]['status']) { + if ($type == 'master') { + self::fill( + $type, + 'Do_DB', + $server_master_replication[0], + 'Binlog_Do_DB' + ); + + self::fill( + $type, + 'Ignore_DB', + $server_master_replication[0], + 'Binlog_Ignore_DB' + ); + } elseif ($type == 'slave') { + self::fill( + $type, + 'Do_DB', + $server_slave_replication[0], + 'Replicate_Do_DB' + ); + + self::fill( + $type, + 'Ignore_DB', + $server_slave_replication[0], + 'Replicate_Ignore_DB' + ); + + self::fill( + $type, + 'Do_Table', + $server_slave_replication[0], + 'Replicate_Do_Table' + ); + + self::fill( + $type, + 'Ignore_Table', + $server_slave_replication[0], + 'Replicate_Ignore_Table' + ); + + self::fill( + $type, + 'Wild_Do_Table', + $server_slave_replication[0], + 'Replicate_Wild_Do_Table' + ); + + self::fill( + $type, + 'Wild_Ignore_Table', + $server_slave_replication[0], + 'Replicate_Wild_Ignore_Table' + ); + } + } + } + } + + /** + * Fill global replication_info variable. + * + * @param string $type Type: master, slave + * @param string $replicationInfoKey Key in replication_info variable + * @param array $mysqlInfo MySQL data about replication + * @param string $mysqlKey MySQL key + * + * @return array + */ + private static function fill( + $type, + $replicationInfoKey, + array $mysqlInfo, + $mysqlKey + ) { + global $replication_info; + + $replication_info[$type][$replicationInfoKey] = empty($mysqlInfo[$mysqlKey]) + ? [] + : explode( + ',', + $mysqlInfo[$mysqlKey] + ); + + return $replication_info[$type][$replicationInfoKey]; + } +} diff --git a/libraries/classes/Server/Status/Data.php b/libraries/classes/Server/Status/Data.php index ad5c64aa99..63c2dbbd8b 100644 --- a/libraries/classes/Server/Status/Data.php +++ b/libraries/classes/Server/Status/Data.php @@ -9,6 +9,7 @@ declare(strict_types=1); namespace PhpMyAdmin\Server\Status; +use PhpMyAdmin\ReplicationInfo; use PhpMyAdmin\Url; /** @@ -316,7 +317,7 @@ class Data global $replication_info; if (! isset($replication_info)) { - require_once ROOT_PATH . 'libraries/replication.inc.php'; + ReplicationInfo::load(); } $this->selfUrl = basename($GLOBALS['PMA_PHP_SELF']); diff --git a/libraries/replication.inc.php b/libraries/replication.inc.php deleted file mode 100644 index 724559de2a..0000000000 --- a/libraries/replication.inc.php +++ /dev/null @@ -1,197 +0,0 @@ -fetchResult('SHOW MASTER STATUS'); - -/** - * set selected master server - */ -if (! empty($_POST['master_connection'])) { - /** - * check for multi-master replication functionality - */ - $server_slave_multi_replication = $dbi->fetchResult( - 'SHOW ALL SLAVES STATUS' - ); - if ($server_slave_multi_replication) { - $dbi->query( - "SET @@default_master_connection = '" - . $dbi->escapeString( - $_POST['master_connection'] - ) . "'" - ); - $url_params['master_connection'] = $_POST['master_connection']; - } -} - -/** - * get slave replication from server - */ -$server_slave_replication = $dbi->fetchResult('SHOW SLAVE STATUS'); - -/** - * replication types - */ -$replication_types = [ - 'master', - 'slave', -]; - - -/** - * define variables for master status - */ -$master_variables = [ - 'File', - 'Position', - 'Binlog_Do_DB', - 'Binlog_Ignore_DB', -]; - -/** - * Define variables for slave status - */ -$slave_variables = [ - 'Slave_IO_State', - 'Master_Host', - 'Master_User', - 'Master_Port', - 'Connect_Retry', - 'Master_Log_File', - 'Read_Master_Log_Pos', - 'Relay_Log_File', - 'Relay_Log_Pos', - 'Relay_Master_Log_File', - 'Slave_IO_Running', - 'Slave_SQL_Running', - 'Replicate_Do_DB', - 'Replicate_Ignore_DB', - 'Replicate_Do_Table', - 'Replicate_Ignore_Table', - 'Replicate_Wild_Do_Table', - 'Replicate_Wild_Ignore_Table', - 'Last_Errno', - 'Last_Error', - 'Skip_Counter', - 'Exec_Master_Log_Pos', - 'Relay_Log_Space', - 'Until_Condition', - 'Until_Log_File', - 'Until_Log_Pos', - 'Master_SSL_Allowed', - 'Master_SSL_CA_File', - 'Master_SSL_CA_Path', - 'Master_SSL_Cert', - 'Master_SSL_Cipher', - 'Master_SSL_Key', - 'Seconds_Behind_Master', -]; -/** - * define important variables, which need to be watched for - * correct running of replication in slave mode - * - * @usedby PhpMyAdmin\ReplicationGui->getHtmlForReplicationStatusTable() - */ -// TODO change to regexp or something, to allow for negative match. -// To e.g. highlight 'Last_Error' -// -$slave_variables_alerts = [ - 'Slave_IO_Running' => 'No', - 'Slave_SQL_Running' => 'No', -]; -$slave_variables_oks = [ - 'Slave_IO_Running' => 'Yes', - 'Slave_SQL_Running' => 'Yes', -]; - -// check which replication is available and -// set $server_{master/slave}_status and assign values - -// replication info is more easily passed to functions -$replication_info = []; - -foreach ($replication_types as $type) { - if (count(${'server_' . $type . '_replication'}) > 0) { - $replication_info[$type]['status'] = true; - } else { - $replication_info[$type]['status'] = false; - } - if ($replication_info[$type]['status']) { - if ($type == 'master') { - $replication->fillInfo( - $type, - 'Do_DB', - $server_master_replication[0], - 'Binlog_Do_DB' - ); - - $replication->fillInfo( - $type, - 'Ignore_DB', - $server_master_replication[0], - 'Binlog_Ignore_DB' - ); - } elseif ($type == 'slave') { - $replication->fillInfo( - $type, - 'Do_DB', - $server_slave_replication[0], - 'Replicate_Do_DB' - ); - - $replication->fillInfo( - $type, - 'Ignore_DB', - $server_slave_replication[0], - 'Replicate_Ignore_DB' - ); - - $replication->fillInfo( - $type, - 'Do_Table', - $server_slave_replication[0], - 'Replicate_Do_Table' - ); - - $replication->fillInfo( - $type, - 'Ignore_Table', - $server_slave_replication[0], - 'Replicate_Ignore_Table' - ); - - $replication->fillInfo( - $type, - 'Wild_Do_Table', - $server_slave_replication[0], - 'Replicate_Wild_Do_Table' - ); - - $replication->fillInfo( - $type, - 'Wild_Ignore_Table', - $server_slave_replication[0], - 'Replicate_Wild_Ignore_Table' - ); - } - } -} diff --git a/test/classes/Controllers/Server/Status/AdvisorControllerTest.php b/test/classes/Controllers/Server/Status/AdvisorControllerTest.php index e5ca1e44db..b6e03f664d 100644 --- a/test/classes/Controllers/Server/Status/AdvisorControllerTest.php +++ b/test/classes/Controllers/Server/Status/AdvisorControllerTest.php @@ -12,6 +12,7 @@ use PhpMyAdmin\Advisor; use PhpMyAdmin\Config; use PhpMyAdmin\Controllers\Server\Status\AdvisorController; use PhpMyAdmin\DatabaseInterface; +use PhpMyAdmin\ReplicationInfo; use PhpMyAdmin\Response; use PhpMyAdmin\Server\Status\Data; use PhpMyAdmin\Template; @@ -40,7 +41,7 @@ class AdvisorControllerTest extends TestCase $GLOBALS['cfg']['Server']['DisableIS'] = false; $GLOBALS['cfg']['Server']['host'] = 'localhost'; - require_once ROOT_PATH . 'libraries/replication.inc.php'; + ReplicationInfo::load(); //this data is needed when PhpMyAdmin\Server\Status\Data constructs $serverStatus = [ diff --git a/test/classes/ReplicationGuiTest.php b/test/classes/ReplicationGuiTest.php index 6ae8afb3bd..4517796efd 100644 --- a/test/classes/ReplicationGuiTest.php +++ b/test/classes/ReplicationGuiTest.php @@ -11,14 +11,10 @@ namespace PhpMyAdmin\Tests; use PhpMyAdmin\DatabaseInterface; use PhpMyAdmin\Replication; use PhpMyAdmin\ReplicationGui; +use PhpMyAdmin\ReplicationInfo; use PhpMyAdmin\Template; use PHPUnit\Framework\TestCase; -/* -* Include to test. -*/ -require_once ROOT_PATH . 'libraries/replication.inc.php'; - /** * PhpMyAdmin\Tests\ReplicationGuiTest class * @@ -60,6 +56,8 @@ class ReplicationGuiTest extends TestCase $GLOBALS['table'] = 'table'; $GLOBALS['url_params'] = []; + ReplicationInfo::load(); + $this->replicationGui = new ReplicationGui(new Replication(), new Template()); //$_SESSION