Add DatabaseBase::classFromType() to reduce the 'Database' . $type duplication
authorChad Horohoe <demon@users.mediawiki.org>
Fri, 7 Jan 2011 22:32:52 +0000 (22:32 +0000)
committerChad Horohoe <demon@users.mediawiki.org>
Fri, 7 Jan 2011 22:32:52 +0000 (22:32 +0000)
includes/db/Database.php
includes/db/LoadBalancer.php
includes/extauth/MediaWiki.php
includes/filerepo/ForeignDBRepo.php
includes/installer/WebInstallerPage.php

index 017760a..33f9795 100644 (file)
@@ -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' );
index 93f7452..2be8fd3 100644 (file)
@@ -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" );
index a069021..7480a6e 100644 (file)
@@ -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'],
index 7a1053a..5d7bb5f 100644 (file)
@@ -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 );
index 34f9043..a38142c 100644 (file)
@@ -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";
                }