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 :)
This commit is contained in:
parent
2e7a059abb
commit
b3e99eec5b
5 changed files with 44 additions and 30 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 ) ) );
|
||||
|
|
|
|||
Loading…
Reference in a new issue