r79848 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r79847‎ | r79848 | r79849 >
Date:22:32, 7 January 2011
Author:demon
Status:resolved (Comments)
Tags:
Comment:
Add DatabaseBase::classFromType() to reduce the 'Database' . $type duplication
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
@@ -540,6 +540,27 @@
541541 return new DatabaseMysql( $server, $user, $password, $dbName, $flags );
542542 }
543543
 544+ /**
 545+ * Given a DB type, construct the name of the appropriate child class of
 546+ * DatabaseBase. This is designed to replace all of the manual stuff like:
 547+ * $class = 'Database' . ucfirst( strtolower( $type ) );
 548+ * as well as validate against the canonical list of DB types we have
 549+ *
 550+ * @param $dbType String A possible DB type
 551+ * @return DatabaseBase subclass or null
 552+ */
 553+ public final static function classFromType( $dbType ) {
 554+ $canonicalDBTypes = array(
 555+ 'mysql', 'postgres', 'sqlite', 'oracle', 'mssql', 'ibm_db2'
 556+ );
 557+ $dbType = strtolower( $dbType );
 558+ if( in_array( $dbType, $canonicalDBTypes ) ) {
 559+ return 'Database' . ucfirst( $dbType );
 560+ } else {
 561+ return null;
 562+ }
 563+ }
 564+
544565 protected function installErrorHandler() {
545566 $this->mPHPError = false;
546567 $this->htmlErrors = ini_set( 'html_errors', '0' );
Index: trunk/phase3/includes/db/LoadBalancer.php
@@ -632,7 +632,7 @@
633633 }
634634
635635 # Get class for this database type
636 - $class = 'Database' . ucfirst( $type );
 636+ $class = DatabaseBase::classFromType( $type );
637637
638638 # Create object
639639 wfDebug( "Connecting to $host $dbname...\n" );
Index: trunk/phase3/includes/filerepo/ForeignDBRepo.php
@@ -35,7 +35,7 @@
3636
3737 function getMasterDB() {
3838 if ( !isset( $this->dbConn ) ) {
39 - $class = 'Database' . ucfirst( $this->dbType );
 39+ $class = DatabaseBase::classFromType( $this->dbType );
4040 $this->dbConn = new $class( $this->dbServer, $this->dbUser,
4141 $this->dbPassword, $this->dbName, $this->dbFlags,
4242 $this->tablePrefix );
Index: trunk/phase3/includes/installer/WebInstallerPage.php
@@ -384,7 +384,7 @@
385385
386386 $dbSupport = '';
387387 foreach( $this->parent->getDBTypes() as $type ) {
388 - $db = 'Database' . ucfirst( $type );
 388+ $db = DatabaseBase::classFromType( $type );
389389 $dbSupport .= wfMsgNoTrans( "config-support-$type",
390390 call_user_func( array( $db, 'getSoftwareLink' ) ) ) . "\n";
391391 }
Index: trunk/phase3/includes/extauth/MediaWiki.php
@@ -72,7 +72,7 @@
7373 private function initFromCond( $cond ) {
7474 global $wgExternalAuthConf;
7575
76 - $class = 'Database' . $wgExternalAuthConf['DBtype'];
 76+ $class = DatabaseBase::classFromType( $wgExternalAuthConf['DBtype'] )
7777 $this->mDb = new $class(
7878 $wgExternalAuthConf['DBserver'],
7979 $wgExternalAuthConf['DBuser'],

Follow-up revisions

RevisionCommit summaryAuthorDate
r79853Follow up r79848. Fix the syntax error.platonides00:46, 8 January 2011
r80864Followup to r79848 (and really, make it useful...)...demon16:31, 24 January 2011
r81182MFT a bunch of installer fixes. r80238, r80128, r80124, r80083, r80080, r800...demon01:59, 29 January 2011

Comments

#Comment by 😂 (talk | contribs)   14:26, 18 January 2011

On further thought, maybe this should return the class itself (like Skin::newFromKey()) and accept an array of wgDBserver, wgDBuser, etc etc.

#Comment by Dantman (talk | contribs)   14:33, 18 January 2011

Probably... classFromType is a bit of black sheep naming, when what it's really doing is what we already do in our ::newFrom* pattern of naming like Title::newFromText, Title::newFromID, User::newFromName, Skin::newFromKey. Database::newFromType or Database::newFromConfig with what you describe or something like that would really be the better name.

#Comment by 😂 (talk | contribs)   14:35, 18 January 2011

That's exactly along the lines I was thinking. Will play with this later today :)

Status & tagging log