http://www.mediawiki.org/wiki/Special:Code/MediaWiki/88729

Revision: 88729
Author:   maxsem
Date:     2011-05-24 17:48:22 +0000 (Tue, 24 May 2011)
Log Message:
-----------
Introduced Maintenance::getDB() and corresponding setDB() to control externally 
what database object should be used by maintenance script. Currently used by 
updater to avoid DatabaseSqliteTest from running stuff like Populate* on the 
live database instead of the one used for testing.

Modified Paths:
--------------
    trunk/phase3/includes/GlobalFunctions.php
    trunk/phase3/includes/installer/DatabaseUpdater.php
    trunk/phase3/maintenance/Maintenance.php
    trunk/phase3/maintenance/populateLogSearch.php
    trunk/phase3/maintenance/populateLogUsertext.php
    trunk/phase3/maintenance/populateRevisionLength.php
    trunk/phase3/maintenance/updateCollation.php

Modified: trunk/phase3/includes/GlobalFunctions.php
===================================================================
--- trunk/phase3/includes/GlobalFunctions.php   2011-05-24 17:47:26 UTC (rev 
88728)
+++ trunk/phase3/includes/GlobalFunctions.php   2011-05-24 17:48:22 UTC (rev 
88729)
@@ -3117,6 +3117,9 @@
  * 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 ) {

Modified: trunk/phase3/includes/installer/DatabaseUpdater.php
===================================================================
--- trunk/phase3/includes/installer/DatabaseUpdater.php 2011-05-24 17:47:26 UTC 
(rev 88728)
+++ trunk/phase3/includes/installer/DatabaseUpdater.php 2011-05-24 17:48:22 UTC 
(rev 88729)
@@ -65,6 +65,7 @@
                } else {
                        $this->maintenance = new FakeMaintenance;
                }
+               $maintenance->setDB( $db );
                $this->initOldGlobals();
                wfRunHooks( 'LoadExtensionSchemaUpdates', array( $this ) );
        }

Modified: trunk/phase3/maintenance/Maintenance.php
===================================================================
--- trunk/phase3/maintenance/Maintenance.php    2011-05-24 17:47:26 UTC (rev 
88728)
+++ trunk/phase3/maintenance/Maintenance.php    2011-05-24 17:48:22 UTC (rev 
88729)
@@ -108,6 +108,9 @@
        // 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 @@
 
                $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 @@
         */
        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' );
@@ -1062,6 +1068,30 @@
        }
 
        /**
+        * 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
         */

Modified: trunk/phase3/maintenance/populateLogSearch.php
===================================================================
--- trunk/phase3/maintenance/populateLogSearch.php      2011-05-24 17:47:26 UTC 
(rev 88728)
+++ trunk/phase3/maintenance/populateLogSearch.php      2011-05-24 17:48:22 UTC 
(rev 88729)
@@ -35,7 +35,7 @@
        }
 
        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 );
                }

Modified: trunk/phase3/maintenance/populateLogUsertext.php
===================================================================
--- trunk/phase3/maintenance/populateLogUsertext.php    2011-05-24 17:47:26 UTC 
(rev 88728)
+++ trunk/phase3/maintenance/populateLogUsertext.php    2011-05-24 17:48:22 UTC 
(rev 88729)
@@ -33,7 +33,7 @@
        }
 
        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" );

Modified: trunk/phase3/maintenance/populateRevisionLength.php
===================================================================
--- trunk/phase3/maintenance/populateRevisionLength.php 2011-05-24 17:47:26 UTC 
(rev 88728)
+++ trunk/phase3/maintenance/populateRevisionLength.php 2011-05-24 17:48:22 UTC 
(rev 88729)
@@ -30,7 +30,7 @@
        }
 
        public function execute() {
-               $db = wfGetDB( DB_MASTER );
+               $db = $this->getDB( DB_MASTER );
                if ( !$db->tableExists( 'revision' ) ) {
                        $this->error( "revision table does not exist", true );
                }

Modified: trunk/phase3/maintenance/updateCollation.php
===================================================================
--- trunk/phase3/maintenance/updateCollation.php        2011-05-24 17:47:26 UTC 
(rev 88728)
+++ trunk/phase3/maintenance/updateCollation.php        2011-05-24 17:48:22 UTC 
(rev 88729)
@@ -60,7 +60,7 @@
        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 );


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to