Introduced Maintenance::getDB() and corresponding setDB() to control externally what...
authorMax Semenik <maxsem@users.mediawiki.org>
Tue, 24 May 2011 17:48:22 +0000 (17:48 +0000)
committerMax Semenik <maxsem@users.mediawiki.org>
Tue, 24 May 2011 17:48:22 +0000 (17:48 +0000)
includes/GlobalFunctions.php
includes/installer/DatabaseUpdater.php
maintenance/Maintenance.php
maintenance/populateLogSearch.php
maintenance/populateLogUsertext.php
maintenance/populateRevisionLength.php
maintenance/updateCollation.php

index acea5fb..a6c4f9f 100644 (file)
@@ -3117,6 +3117,9 @@ function wfSplitWikiID( $wiki ) {
  * will always return the same object, unless the underlying connection or load
  * balancer is manually destroyed.
  *
+ * Note 2: use $this->getDB() in maintenance scripts that may be invoked by
+ * updater to ensure that a proper database is being updated.
+ *
  * @return DatabaseBase
  */
 function &wfGetDB( $db, $groups = array(), $wiki = false ) {
index 01ff690..c931e34 100644 (file)
@@ -65,6 +65,7 @@ abstract class DatabaseUpdater {
                } else {
                        $this->maintenance = new FakeMaintenance;
                }
+               $maintenance->setDB( $db );
                $this->initOldGlobals();
                wfRunHooks( 'LoadExtensionSchemaUpdates', array( $this ) );
        }
index 9d4da81..fd88656 100644 (file)
@@ -108,6 +108,9 @@ abstract class Maintenance {
        // Generic options which might or not be supported by the script
        private $mDependantParameters = array();
 
+       // Used by getDD() / setDB()
+       private $mDb = null;
+
        /**
         * List of all the core maintenance scripts. This is added
         * to scripts added by extensions in $wgMaintenanceScripts
@@ -448,6 +451,9 @@ abstract class Maintenance {
 
                $child = new $maintClass();
                $child->loadParamsAndArgs( $this->mSelf, $this->mOptions, $this->mArgs );
+               if ( !is_null( $this->mDb ) ) {
+                       $child->setDB( $this->mDb );
+               }
                return $child;
        }
 
@@ -958,7 +964,7 @@ abstract class Maintenance {
         */
        public function purgeRedundantText( $delete = true ) {
                # Data should come off the master, wrapped in a transaction
-               $dbw = wfGetDB( DB_MASTER );
+               $dbw = $this->getDB( DB_MASTER );
                $dbw->begin();
 
                $tbl_arc = $dbw->tableName( 'archive' );
@@ -1061,6 +1067,30 @@ abstract class Maintenance {
                return self::$mCoreScripts;
        }
 
+       /**
+        * Returns a database to be used by current maintenance script. It can be set by setDB().
+        * If not set, wfGetDB() will be used.
+        * This function has the same parameters as wfGetDB()
+        *
+        * @return DatabaseBase
+        */
+       protected function &getDB( $db, $groups = array(), $wiki = false ) {
+               if ( is_null( $this->mDb ) ) {
+                       return wfGetDB( $db, $groups, $wiki );
+               } else {
+                       return $this->mDb;
+               }
+       }
+
+       /**
+        * Sets database object to be returned by getDB().
+        *
+        * @param $db DatabaseBase: Database object to be used
+        */
+       public function setDB( &$db ) {
+               $this->mDb = $db;
+       }
+
        /**
         * Lock the search index
         * @param &$db Database object
index 894c9ee..b6f8c2d 100644 (file)
@@ -35,7 +35,7 @@ class PopulateLogSearch extends Maintenance {
        }
 
        public function execute() {
-               $db = wfGetDB( DB_MASTER );
+               $db = $this->getDB( DB_MASTER );
                if ( !$db->tableExists( 'log_search' ) ) {
                        $this->error( "log_search does not exist", true );
                }
index af912ad..e9e6926 100644 (file)
@@ -33,7 +33,7 @@ class PopulateLogUsertext extends Maintenance {
        }
 
        public function execute() {
-               $db = wfGetDB( DB_MASTER );
+               $db = $this->getDB( DB_MASTER );
                $start = $db->selectField( 'logging', 'MIN(log_id)', false, __METHOD__ );
                if ( !$start ) {
                        $this->output( "Nothing to do.\n" );
index 7ed306e..d020b4c 100644 (file)
@@ -30,7 +30,7 @@ class PopulateRevisionLength extends Maintenance {
        }
 
        public function execute() {
-               $db = wfGetDB( DB_MASTER );
+               $db = $this->getDB( DB_MASTER );
                if ( !$db->tableExists( 'revision' ) ) {
                        $this->error( "revision table does not exist", true );
                }
index 8cbea5b..f5b7d9c 100644 (file)
@@ -60,7 +60,7 @@ TEXT;
        public function execute() {
                global $wgCategoryCollation, $wgMiserMode;
 
-               $dbw = wfGetDB( DB_MASTER );
+               $dbw = $this->getDB( DB_MASTER );
                $force = $this->getOption( 'force' );
 
                $options = array( 'LIMIT' => self::BATCH_SIZE );