From 97b62e303467e15383c3c41e9e06d5477bc91ff2 Mon Sep 17 00:00:00 2001 From: Chad Horohoe Date: Fri, 7 Jan 2011 22:32:52 +0000 Subject: [PATCH] Add DatabaseBase::classFromType() to reduce the 'Database' . $type duplication --- includes/db/Database.php | 21 +++++++++++++++++++++ includes/db/LoadBalancer.php | 2 +- includes/extauth/MediaWiki.php | 2 +- includes/filerepo/ForeignDBRepo.php | 2 +- includes/installer/WebInstallerPage.php | 2 +- 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/includes/db/Database.php b/includes/db/Database.php index 017760a526..33f9795f2b 100644 --- a/includes/db/Database.php +++ b/includes/db/Database.php @@ -540,6 +540,27 @@ abstract class DatabaseBase implements DatabaseType { return new DatabaseMysql( $server, $user, $password, $dbName, $flags ); } + /** + * Given a DB type, construct the name of the appropriate child class of + * DatabaseBase. This is designed to replace all of the manual stuff like: + * $class = 'Database' . ucfirst( strtolower( $type ) ); + * as well as validate against the canonical list of DB types we have + * + * @param $dbType String A possible DB type + * @return DatabaseBase subclass or null + */ + public final static function classFromType( $dbType ) { + $canonicalDBTypes = array( + 'mysql', 'postgres', 'sqlite', 'oracle', 'mssql', 'ibm_db2' + ); + $dbType = strtolower( $dbType ); + if( in_array( $dbType, $canonicalDBTypes ) ) { + return 'Database' . ucfirst( $dbType ); + } else { + return null; + } + } + protected function installErrorHandler() { $this->mPHPError = false; $this->htmlErrors = ini_set( 'html_errors', '0' ); diff --git a/includes/db/LoadBalancer.php b/includes/db/LoadBalancer.php index 93f74520f3..2be8fd350c 100644 --- a/includes/db/LoadBalancer.php +++ b/includes/db/LoadBalancer.php @@ -632,7 +632,7 @@ class LoadBalancer { } # Get class for this database type - $class = 'Database' . ucfirst( $type ); + $class = DatabaseBase::classFromType( $type ); # Create object wfDebug( "Connecting to $host $dbname...\n" ); diff --git a/includes/extauth/MediaWiki.php b/includes/extauth/MediaWiki.php index a069021848..7480a6e05d 100644 --- a/includes/extauth/MediaWiki.php +++ b/includes/extauth/MediaWiki.php @@ -72,7 +72,7 @@ class ExternalUser_MediaWiki extends ExternalUser { private function initFromCond( $cond ) { global $wgExternalAuthConf; - $class = 'Database' . $wgExternalAuthConf['DBtype']; + $class = DatabaseBase::classFromType( $wgExternalAuthConf['DBtype'] ) $this->mDb = new $class( $wgExternalAuthConf['DBserver'], $wgExternalAuthConf['DBuser'], diff --git a/includes/filerepo/ForeignDBRepo.php b/includes/filerepo/ForeignDBRepo.php index 7a1053a4e1..5d7bb5f9d3 100644 --- a/includes/filerepo/ForeignDBRepo.php +++ b/includes/filerepo/ForeignDBRepo.php @@ -35,7 +35,7 @@ class ForeignDBRepo extends LocalRepo { function getMasterDB() { if ( !isset( $this->dbConn ) ) { - $class = 'Database' . ucfirst( $this->dbType ); + $class = DatabaseBase::classFromType( $this->dbType ); $this->dbConn = new $class( $this->dbServer, $this->dbUser, $this->dbPassword, $this->dbName, $this->dbFlags, $this->tablePrefix ); diff --git a/includes/installer/WebInstallerPage.php b/includes/installer/WebInstallerPage.php index 34f9043b9c..a38142c979 100644 --- a/includes/installer/WebInstallerPage.php +++ b/includes/installer/WebInstallerPage.php @@ -384,7 +384,7 @@ class WebInstaller_DBConnect extends WebInstallerPage { $dbSupport = ''; foreach( $this->parent->getDBTypes() as $type ) { - $db = 'Database' . ucfirst( $type ); + $db = DatabaseBase::classFromType( $type ); $dbSupport .= wfMsgNoTrans( "config-support-$type", call_user_func( array( $db, 'getSoftwareLink' ) ) ) . "\n"; } -- 2.20.1