r80864 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r80863‎ | r80864 | r80865 >
Date:16:31, 24 January 2011
Author:demon
Status:resolved (Comments)
Tags:
Comment:
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 :)
Modified paths:
  • /trunk/phase3/includes/db/Database.php (modified) (history)
  • /trunk/phase3/includes/db/LoadBalancer.php (modified) (history)
  • /trunk/phase3/includes/extauth/MediaWiki.php (modified) (history)
  • /trunk/phase3/includes/filerepo/ForeignDBRepo.php (modified) (history)
  • /trunk/phase3/includes/installer/WebInstallerPage.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/db/Database.php
@@ -522,11 +522,7 @@
523523
524524 /**
525525 * Same as new DatabaseMysql( ... ), kept for backward compatibility
526 - * @param $server String: database server host
527 - * @param $user String: database user name
528 - * @param $password String: database user password
529 - * @param $dbName String: database name
530 - * @param $flags
 526+ * @deprecated
531527 */
532528 static function newFromParams( $server, $user, $password, $dbName, $flags = 0 ) {
533529 wfDeprecated( __METHOD__ );
@@ -539,16 +535,36 @@
540536 * $class = 'Database' . ucfirst( strtolower( $type ) );
541537 * as well as validate against the canonical list of DB types we have
542538 *
 539+ * This factory function is mostly useful for when you need to connect to a
 540+ * database other than the MediaWiki default (such as for external auth,
 541+ * an extension, et cetera). Do not use this to connect to the MediaWiki
 542+ * database. Example uses in core:
 543+ * @see LoadBalancer::reallyOpenConnection()
 544+ * @see ExternalUser_MediaWiki::initFromCond()
 545+ * @see ForeignDBRepo::getMasterDB()
 546+ * @see WebInstaller_DBConnect::execute()
 547+ *
543548 * @param $dbType String A possible DB type
 549+ * @param $p Array An array of options to pass to the constructor.
 550+ * Valid options are: host, user, password, dbname, flags, tableprefix
544551 * @return DatabaseBase subclass or null
545552 */
546 - public final static function classFromType( $dbType ) {
 553+ public final static function newFromType( $dbType, $p = array() ) {
547554 $canonicalDBTypes = array(
548555 'mysql', 'postgres', 'sqlite', 'oracle', 'mssql', 'ibm_db2'
549556 );
550557 $dbType = strtolower( $dbType );
 558+
551559 if( in_array( $dbType, $canonicalDBTypes ) ) {
552 - return 'Database' . ucfirst( $dbType );
 560+ $class = 'Database' . ucfirst( $dbType );
 561+ return new $class(
 562+ isset( $p['host'] ) ? $p['host'] : false,
 563+ isset( $p['user'] ) ? $p['user'] : false,
 564+ isset( $p['password'] ) ? $p['password'] : false,
 565+ isset( $p['dbname'] ) ? $p['dbname'] : false,
 566+ isset( $p['flags'] ) ? $p['flags'] : 0,
 567+ isset( $p['tableprefix'] ) ? $p['tableprefix'] : 'get from global'
 568+ );
553569 } else {
554570 return null;
555571 }
Index: trunk/phase3/includes/db/LoadBalancer.php
@@ -620,23 +620,16 @@
621621 'See DefaultSettings.php entry for $wgDBservers.' );
622622 }
623623
624 - $type = $server['type'];
625624 $host = $server['host'];
626 - $user = $server['user'];
627 - $password = $server['password'];
628 - $flags = $server['flags'];
629625 $dbname = $server['dbname'];
630626
631627 if ( $dbNameOverride !== false ) {
632628 $dbname = $dbNameOverride;
633629 }
634630
635 - # Get class for this database type
636 - $class = DatabaseBase::classFromType( $type );
637 -
638631 # Create object
639632 wfDebug( "Connecting to $host $dbname...\n" );
640 - $db = new $class( $host, $user, $password, $dbname, $flags );
 633+ $db = DatabaseBase::newFromType( $server['type'], $server );
641634 if ( $db->isOpen() ) {
642635 wfDebug( "Connected to $host $dbname.\n" );
643636 } else {
Index: trunk/phase3/includes/filerepo/ForeignDBRepo.php
@@ -35,10 +35,16 @@
3636
3737 function getMasterDB() {
3838 if ( !isset( $this->dbConn ) ) {
39 - $class = DatabaseBase::classFromType( $this->dbType );
40 - $this->dbConn = new $class( $this->dbServer, $this->dbUser,
41 - $this->dbPassword, $this->dbName, $this->dbFlags,
42 - $this->tablePrefix );
 39+ $this->dbConn = DatabaseBase::newFromType( $this->dbType,
 40+ array(
 41+ 'server' => $this->dbServer,
 42+ 'user' => $this->dbUser,
 43+ 'password' => $this->dbPassword,
 44+ 'dbname' => $this->dbName,
 45+ 'flags' => $this->dbFlags,
 46+ 'tableprefix' => $this->tablePrefix
 47+ )
 48+ );
4349 }
4450 return $this->dbConn;
4551 }
Index: trunk/phase3/includes/installer/WebInstallerPage.php
@@ -384,9 +384,8 @@
385385
386386 $dbSupport = '';
387387 foreach( $this->parent->getDBTypes() as $type ) {
388 - $db = DatabaseBase::classFromType( $type );
389 - $dbSupport .= wfMsgNoTrans( "config-support-$type",
390 - call_user_func( array( $db, 'getSoftwareLink' ) ) ) . "\n";
 388+ $link = DatabaseBase::newFromType( $type )->getSoftwareLink();
 389+ $dbSupport .= wfMsgNoTrans( "config-support-$type", $link ) . "\n";
391390 }
392391 $this->addHTML( $this->parent->getInfoBox(
393392 wfMsg( 'config-support-info', $dbSupport ) ) );
Index: trunk/phase3/includes/extauth/MediaWiki.php
@@ -72,14 +72,14 @@
7373 private function initFromCond( $cond ) {
7474 global $wgExternalAuthConf;
7575
76 - $class = DatabaseBase::classFromType( $wgExternalAuthConf['DBtype'] );
77 - $this->mDb = new $class(
78 - $wgExternalAuthConf['DBserver'],
79 - $wgExternalAuthConf['DBuser'],
80 - $wgExternalAuthConf['DBpassword'],
81 - $wgExternalAuthConf['DBname'],
82 - 0,
83 - $wgExternalAuthConf['DBprefix']
 76+ $this->mDb = DatabaseBase::newFromType( $wgExternalAuthConf['DBtype'],
 77+ array(
 78+ 'server' => $wgExternalAuthConf['DBserver'],
 79+ 'user' => $wgExternalAuthConf['DBuser'],
 80+ 'password' => $wgExternalAuthConf['DBpassword'],
 81+ 'dbname' => $wgExternalAuthConf['DBname'],
 82+ 'tableprefix' => $wgExternalAuthConf['DBprefix'],
 83+ )
8484 );
8585
8686 $row = $this->mDb->selectRow(

Follow-up revisions

RevisionCommit summaryAuthorDate
r80887Followup r80864: DatabaseSqlite exploded on installer page because it tried t...demon17:37, 24 January 2011
r80895Fix for r80864: the $dbname variable was not there only to make pretty debug ...ialex18:45, 24 January 2011
r83414MFT r80864, r80895demon22:41, 6 March 2011
r87498Fixes ForeignDBRepo SQL server connection. Fixes bug #28836.valhallasw15:04, 5 May 2011
r87544Fix for r80864, host not server. Like r87498demon14:57, 6 May 2011
r90430Fixes for r80864 for 1.18 backport:...tstarling07:00, 20 June 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r79848Add DatabaseBase::classFromType() to reduce the 'Database' . $type duplicationdemon22:32, 7 January 2011

Comments

#Comment by Bryan (talk | contribs)   15:12, 5 May 2011

See bug 28836. Compare:

+			return new $class(
+				isset( $p['host'] ) ? $p['host'] : false,

and

+			$this->dbConn = DatabaseBase::newFromType( $this->dbType,
+				array(
+					'server' => $this->dbServer,
+		$this->mDb = DatabaseBase::newFromType( $wgExternalAuthConf['DBtype'],
+			array(
+				'server'      => $wgExternalAuthConf['DBserver'],
#Comment by 😂 (talk | contribs)   14:57, 6 May 2011

See r87498, r87544.

Status & tagging log