From 58a0e0c06d7d05ebfbf2bf415a72184aeda85c0b Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Fri, 18 Oct 2013 13:05:08 -0700 Subject: [PATCH] Cleaned up DatabaseBase constructor to use an array Change-Id: I094185585dc844ca4d2d8b629107b2ab8f9bef39 --- includes/db/Database.php | 55 +++++++++++-------- includes/db/DatabaseOracle.php | 25 +++++++-- includes/db/DatabaseSqlite.php | 39 +++++++------ .../includes/db/DatabaseMysqlBaseTest.php | 1 + 4 files changed, 76 insertions(+), 44 deletions(-) diff --git a/includes/db/Database.php b/includes/db/Database.php index cd907e9a69..4a84578f92 100644 --- a/includes/db/Database.php +++ b/includes/db/Database.php @@ -681,29 +681,37 @@ abstract class DatabaseBase implements IDatabase, DatabaseType { * connection object, by specifying no parameters to __construct(). This * feature is deprecated and should be removed. * - * FIXME: The long list of formal parameters here is not really appropriate - * for MySQL, and not at all appropriate for any other DBMS. It should be - * replaced by named parameters as in DatabaseBase::factory(). - * * DatabaseBase subclasses should not be constructed directly in external * code. DatabaseBase::factory() should be used instead. * - * @param string $server database server host - * @param string $user database user name - * @param string $password database user password - * @param string $dbName database name - * @param $flags - * @param string $tablePrefix database table prefixes. By default use the prefix gave in LocalSettings.php - * @param bool $foreign disable some operations specific to local databases + * @param array Parameters passed from DatabaseBase::factory() */ - function __construct( $server = false, $user = false, $password = false, $dbName = false, - $flags = 0, $tablePrefix = 'get from global', $foreign = false - ) { + function __construct( $params = null ) { global $wgDBprefix, $wgCommandLineMode, $wgDebugDBTransactions; $this->mTrxAtomicLevels = new SplStack; - $this->mFlags = $flags; + if ( is_array( $params ) ) { // MW 1.22 + $server = $params['host']; + $user = $params['user']; + $password = $params['password']; + $dbName = $params['dbname']; + $flags = $params['flags']; + $tablePrefix = $params['tablePrefix']; + $foreign = $params['foreign']; + } else { // legacy calling pattern + wfDeprecated( __METHOD__ . " method called without parameter array.", "1.22" ); + $args = func_get_args(); + $server = isset( $args[0] ) ? $args[0] : false; + $user = isset( $args[1] ) ? $args[1] : false; + $password = isset( $args[2] ) ? $args[2] : false; + $dbName = isset( $args[3] ) ? $args[3] : false; + $flags = isset( $args[4] ) ? $args[4] : 0; + $tablePrefix = isset( $args[5] ) ? $args[5] : 'get from global'; + $foreign = isset( $args[6] ) ? $args[6] : false; + } + + $this->mFlags = $flags; if ( $this->mFlags & DBO_DEFAULT ) { if ( $wgCommandLineMode ) { $this->mFlags &= ~DBO_TRX; @@ -800,15 +808,16 @@ abstract class DatabaseBase implements IDatabase, DatabaseType { $class = 'Database' . ucfirst( $driver ); if ( class_exists( $class ) && is_subclass_of( $class, 'DatabaseBase' ) ) { - return new $class( - isset( $p['host'] ) ? $p['host'] : false, - isset( $p['user'] ) ? $p['user'] : false, - isset( $p['password'] ) ? $p['password'] : false, - isset( $p['dbname'] ) ? $p['dbname'] : false, - isset( $p['flags'] ) ? $p['flags'] : 0, - isset( $p['tablePrefix'] ) ? $p['tablePrefix'] : 'get from global', - isset( $p['foreign'] ) ? $p['foreign'] : false + $params = array( + 'host' => isset( $p['host'] ) ? $p['host'] : false, + 'user' => isset( $p['user'] ) ? $p['user'] : false, + 'password' => isset( $p['password'] ) ? $p['password'] : false, + 'dbname' => isset( $p['dbname'] ) ? $p['dbname'] : false, + 'flags' => isset( $p['flags'] ) ? $p['flags'] : 0, + 'tablePrefix' => isset( $p['tablePrefix'] ) ? $p['tablePrefix'] : 'get from global', + 'foreign' => isset( $p['foreign'] ) ? $p['foreign'] : false ); + return new $class( $params ); } else { return null; } diff --git a/includes/db/DatabaseOracle.php b/includes/db/DatabaseOracle.php index fbaa4da5a2..97070fb863 100644 --- a/includes/db/DatabaseOracle.php +++ b/includes/db/DatabaseOracle.php @@ -196,12 +196,27 @@ class DatabaseOracle extends DatabaseBase { var $mFieldInfoCache = array(); - function __construct( $server = false, $user = false, $password = false, $dbName = false, - $flags = 0, $tablePrefix = 'get from global' ) - { + function __construct( $p = null ) { global $wgDBprefix; - $tablePrefix = $tablePrefix == 'get from global' ? strtoupper( $wgDBprefix ) : strtoupper( $tablePrefix ); - parent::__construct( $server, $user, $password, $dbName, $flags, $tablePrefix ); + + if ( !is_array( $p ) ) { // legacy calling pattern + wfDeprecated( __METHOD__ . " method called without parameter array.", "1.22" ); + $args = func_get_args(); + $p = array( + 'host' => isset( $args[0] ) ? $args[0] : false, + 'user' => isset( $args[1] ) ? $args[1] : false, + 'password' => isset( $args[2] ) ? $args[2] : false, + 'dbname' => isset( $args[3] ) ? $args[3] : false, + 'flags' => isset( $args[4] ) ? $args[4] : 0, + 'tablePrefix' => isset( $args[5] ) ? $args[5] : 'get from global', + 'foreign' => isset( $args[6] ) ? $args[6] : false + ); + } + if ( $p['tablePrefix'] == 'get from global' ) { + $p['tablePrefix'] = $wgDBprefix; + } + $p['tablePrefix'] = strtoupper( $p['tablePrefix'] ); + parent::__construct( $p ); wfRunHooks( 'DatabaseOraclePostInit', array( $this ) ); } diff --git a/includes/db/DatabaseSqlite.php b/includes/db/DatabaseSqlite.php index 06dfd84595..79a3b1ecf3 100644 --- a/includes/db/DatabaseSqlite.php +++ b/includes/db/DatabaseSqlite.php @@ -39,23 +39,30 @@ class DatabaseSqlite extends DatabaseBase { */ protected $mConn; - /** - * Constructor. - * Parameters $server, $user and $password are not used. - * @param $server string - * @param $user string - * @param $password string - * @param $dbName string - * @param $flags int - */ - function __construct( $server = false, $user = false, $password = false, $dbName = false, $flags = 0 ) { - $this->mName = $dbName; - parent::__construct( $server, $user, $password, $dbName, $flags ); + function __construct( $p = null ) { + global $wgSharedDB; + + if ( !is_array( $p ) ) { // legacy calling pattern + wfDeprecated( __METHOD__ . " method called without parameter array.", "1.22" ); + $args = func_get_args(); + $p = array( + 'host' => isset( $args[0] ) ? $args[0] : false, + 'user' => isset( $args[1] ) ? $args[1] : false, + 'password' => isset( $args[2] ) ? $args[2] : false, + 'dbname' => isset( $args[3] ) ? $args[3] : false, + 'flags' => isset( $args[4] ) ? $args[4] : 0, + 'tablePrefix' => isset( $args[5] ) ? $args[5] : 'get from global', + 'foreign' => isset( $args[6] ) ? $args[6] : false + ); + } + $this->mName = $p['dbname']; + parent::__construct( $p ); // parent doesn't open when $user is false, but we can work with $dbName - if ( $dbName && !$this->isOpen() ) { - global $wgSharedDB; - if ( $this->open( $server, $user, $password, $dbName ) && $wgSharedDB ) { - $this->attachDatabase( $wgSharedDB ); + if ( $p['dbname'] && !$this->isOpen() ) { + if ( $this->open( $p['host'], $p['user'], $p['password'], $p['dbname'] ) ) { + if ( $wgSharedDB ) { + $this->attachDatabase( $wgSharedDB ); + } } } } diff --git a/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php b/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php index 134f856601..636e488bb6 100644 --- a/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php +++ b/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php @@ -31,6 +31,7 @@ */ class FakeDatabaseMysqlBase extends DatabaseMysqlBase { // From DatabaseBase + function __construct() {} protected function closeConnection() {} protected function doQuery( $sql ) {} -- 2.20.1