From b3e99eec5b30d4a5458609d5a819f2aba2388055 Mon Sep 17 00:00:00 2001 From: Chad Horohoe Date: Mon, 24 Jan 2011 16:31:36 +0000 Subject: [PATCH] Followup to r79848 (and really, make it useful...) Turn DatabaseBase::classFromType() into newFromType() factory function for constructing a new object based on a given type and (optional) params. Documented it fairly clearly. I think it looks nicer :) --- includes/db/Database.php | 30 +++++++++++++++++++------ includes/db/LoadBalancer.php | 9 +------- includes/extauth/MediaWiki.php | 16 ++++++------- includes/filerepo/ForeignDBRepo.php | 14 ++++++++---- includes/installer/WebInstallerPage.php | 5 ++--- 5 files changed, 44 insertions(+), 30 deletions(-) diff --git a/includes/db/Database.php b/includes/db/Database.php index 4d75379c58..b28f48f074 100644 --- a/includes/db/Database.php +++ b/includes/db/Database.php @@ -522,11 +522,7 @@ abstract class DatabaseBase implements DatabaseType { /** * Same as new DatabaseMysql( ... ), kept for backward compatibility - * @param $server String: database server host - * @param $user String: database user name - * @param $password String: database user password - * @param $dbName String: database name - * @param $flags + * @deprecated */ static function newFromParams( $server, $user, $password, $dbName, $flags = 0 ) { wfDeprecated( __METHOD__ ); @@ -539,16 +535,36 @@ abstract class DatabaseBase implements DatabaseType { * $class = 'Database' . ucfirst( strtolower( $type ) ); * as well as validate against the canonical list of DB types we have * + * This factory function is mostly useful for when you need to connect to a + * database other than the MediaWiki default (such as for external auth, + * an extension, et cetera). Do not use this to connect to the MediaWiki + * database. Example uses in core: + * @see LoadBalancer::reallyOpenConnection() + * @see ExternalUser_MediaWiki::initFromCond() + * @see ForeignDBRepo::getMasterDB() + * @see WebInstaller_DBConnect::execute() + * * @param $dbType String A possible DB type + * @param $p Array An array of options to pass to the constructor. + * Valid options are: host, user, password, dbname, flags, tableprefix * @return DatabaseBase subclass or null */ - public final static function classFromType( $dbType ) { + public final static function newFromType( $dbType, $p = array() ) { $canonicalDBTypes = array( 'mysql', 'postgres', 'sqlite', 'oracle', 'mssql', 'ibm_db2' ); $dbType = strtolower( $dbType ); + if( in_array( $dbType, $canonicalDBTypes ) ) { - return 'Database' . ucfirst( $dbType ); + $class = 'Database' . ucfirst( $dbType ); + 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' + ); } else { return null; } diff --git a/includes/db/LoadBalancer.php b/includes/db/LoadBalancer.php index e6dfd6a1ef..59cd513e1d 100644 --- a/includes/db/LoadBalancer.php +++ b/includes/db/LoadBalancer.php @@ -620,23 +620,16 @@ class LoadBalancer { 'See DefaultSettings.php entry for $wgDBservers.' ); } - $type = $server['type']; $host = $server['host']; - $user = $server['user']; - $password = $server['password']; - $flags = $server['flags']; $dbname = $server['dbname']; if ( $dbNameOverride !== false ) { $dbname = $dbNameOverride; } - # Get class for this database type - $class = DatabaseBase::classFromType( $type ); - # Create object wfDebug( "Connecting to $host $dbname...\n" ); - $db = new $class( $host, $user, $password, $dbname, $flags ); + $db = DatabaseBase::newFromType( $server['type'], $server ); if ( $db->isOpen() ) { wfDebug( "Connected to $host $dbname.\n" ); } else { diff --git a/includes/extauth/MediaWiki.php b/includes/extauth/MediaWiki.php index e676574cbd..9df4ea1f6b 100644 --- a/includes/extauth/MediaWiki.php +++ b/includes/extauth/MediaWiki.php @@ -72,14 +72,14 @@ class ExternalUser_MediaWiki extends ExternalUser { private function initFromCond( $cond ) { global $wgExternalAuthConf; - $class = DatabaseBase::classFromType( $wgExternalAuthConf['DBtype'] ); - $this->mDb = new $class( - $wgExternalAuthConf['DBserver'], - $wgExternalAuthConf['DBuser'], - $wgExternalAuthConf['DBpassword'], - $wgExternalAuthConf['DBname'], - 0, - $wgExternalAuthConf['DBprefix'] + $this->mDb = DatabaseBase::newFromType( $wgExternalAuthConf['DBtype'], + array( + 'server' => $wgExternalAuthConf['DBserver'], + 'user' => $wgExternalAuthConf['DBuser'], + 'password' => $wgExternalAuthConf['DBpassword'], + 'dbname' => $wgExternalAuthConf['DBname'], + 'tableprefix' => $wgExternalAuthConf['DBprefix'], + ) ); $row = $this->mDb->selectRow( diff --git a/includes/filerepo/ForeignDBRepo.php b/includes/filerepo/ForeignDBRepo.php index 5d7bb5f9d3..590350b412 100644 --- a/includes/filerepo/ForeignDBRepo.php +++ b/includes/filerepo/ForeignDBRepo.php @@ -35,10 +35,16 @@ class ForeignDBRepo extends LocalRepo { function getMasterDB() { if ( !isset( $this->dbConn ) ) { - $class = DatabaseBase::classFromType( $this->dbType ); - $this->dbConn = new $class( $this->dbServer, $this->dbUser, - $this->dbPassword, $this->dbName, $this->dbFlags, - $this->tablePrefix ); + $this->dbConn = DatabaseBase::newFromType( $this->dbType, + array( + 'server' => $this->dbServer, + 'user' => $this->dbUser, + 'password' => $this->dbPassword, + 'dbname' => $this->dbName, + 'flags' => $this->dbFlags, + 'tableprefix' => $this->tablePrefix + ) + ); } return $this->dbConn; } diff --git a/includes/installer/WebInstallerPage.php b/includes/installer/WebInstallerPage.php index 6f968fba5d..768ce0ee82 100644 --- a/includes/installer/WebInstallerPage.php +++ b/includes/installer/WebInstallerPage.php @@ -384,9 +384,8 @@ class WebInstaller_DBConnect extends WebInstallerPage { $dbSupport = ''; foreach( $this->parent->getDBTypes() as $type ) { - $db = DatabaseBase::classFromType( $type ); - $dbSupport .= wfMsgNoTrans( "config-support-$type", - call_user_func( array( $db, 'getSoftwareLink' ) ) ) . "\n"; + $link = DatabaseBase::newFromType( $type )->getSoftwareLink(); + $dbSupport .= wfMsgNoTrans( "config-support-$type", $link ) . "\n"; } $this->addHTML( $this->parent->getInfoBox( wfMsg( 'config-support-info', $dbSupport ) ) ); -- 2.20.1