From c79a16167adc5a6f29aebd44628fdf37c0dede1a Mon Sep 17 00:00:00 2001 From: Max Semenik Date: Tue, 24 May 2011 17:48:22 +0000 Subject: [PATCH] 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. --- includes/GlobalFunctions.php | 3 +++ includes/installer/DatabaseUpdater.php | 1 + maintenance/Maintenance.php | 32 +++++++++++++++++++++++++- maintenance/populateLogSearch.php | 2 +- maintenance/populateLogUsertext.php | 2 +- maintenance/populateRevisionLength.php | 2 +- maintenance/updateCollation.php | 2 +- 7 files changed, 39 insertions(+), 5 deletions(-) diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index acea5fbe84..a6c4f9f7fd 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -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 ) { diff --git a/includes/installer/DatabaseUpdater.php b/includes/installer/DatabaseUpdater.php index 01ff69042d..c931e34803 100644 --- a/includes/installer/DatabaseUpdater.php +++ b/includes/installer/DatabaseUpdater.php @@ -65,6 +65,7 @@ abstract class DatabaseUpdater { } else { $this->maintenance = new FakeMaintenance; } + $maintenance->setDB( $db ); $this->initOldGlobals(); wfRunHooks( 'LoadExtensionSchemaUpdates', array( $this ) ); } diff --git a/maintenance/Maintenance.php b/maintenance/Maintenance.php index 9d4da81873..fd886569ce 100644 --- a/maintenance/Maintenance.php +++ b/maintenance/Maintenance.php @@ -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 diff --git a/maintenance/populateLogSearch.php b/maintenance/populateLogSearch.php index 894c9ee115..b6f8c2db69 100644 --- a/maintenance/populateLogSearch.php +++ b/maintenance/populateLogSearch.php @@ -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 ); } diff --git a/maintenance/populateLogUsertext.php b/maintenance/populateLogUsertext.php index af912ad2ea..e9e6926e7b 100644 --- a/maintenance/populateLogUsertext.php +++ b/maintenance/populateLogUsertext.php @@ -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" ); diff --git a/maintenance/populateRevisionLength.php b/maintenance/populateRevisionLength.php index 7ed306e84f..d020b4cba4 100644 --- a/maintenance/populateRevisionLength.php +++ b/maintenance/populateRevisionLength.php @@ -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 ); } diff --git a/maintenance/updateCollation.php b/maintenance/updateCollation.php index 8cbea5b50a..f5b7d9c533 100644 --- a/maintenance/updateCollation.php +++ b/maintenance/updateCollation.php @@ -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 ); -- 2.20.1