Database dependency injection for BackupDumpers
authorChristian Aistleitner <christian@quelltextlich.at>
Tue, 3 Apr 2012 09:23:20 +0000 (11:23 +0200)
committerChristian Aistleitner <christian@quelltextlich.at>
Tue, 3 Apr 2012 10:56:59 +0000 (12:56 +0200)
Change-Id: I48f8cd176cc51fe133a8490e3e001a8be4978691

maintenance/backup.inc
maintenance/backupTextPass.inc

index 470a513..6eccb26 100644 (file)
@@ -60,6 +60,15 @@ class BackupDumper {
 
        var $outputTypes = array(), $filterTypes = array();
 
+       /**
+        * The dependency-injected database to use.
+        *
+        * @var DatabaseBase|null
+        *
+        * @see self::setDb
+        */
+       protected $forcedDb = null;
+
        /**
         * @var LoadBalancer
         */
@@ -245,7 +254,10 @@ class BackupDumper {
                $table = ( $history == WikiExporter::CURRENT ) ? 'page' : 'revision';
                $field = ( $history == WikiExporter::CURRENT ) ? 'page_id' : 'rev_id';
 
-               $dbr = wfGetDB( DB_SLAVE );
+               $dbr = $this->forcedDb;
+               if ( $this->forcedDb === null ) {
+                       $dbr = wfGetDB( DB_SLAVE );
+               }
                $this->maxCount = $dbr->selectField( $table, "MAX($field)", '', __METHOD__ );
                $this->startTime = wfTime();
                $this->lastTime = $this->startTime;
@@ -259,6 +271,10 @@ class BackupDumper {
         * @return DatabaseBase
         */
        function backupDb() {
+               if ( $this->forcedDb !== null ) {
+                       return $this->forcedDb;
+               }
+
                $this->lb = wfGetLBFactory()->newMainLB();
                $db = $this->lb->getConnection( DB_SLAVE, 'backup' );
 
@@ -269,6 +285,18 @@ class BackupDumper {
                return $db;
        }
 
+       /**
+        * Force the dump to use the provided database connection for database
+        * operations, wherever possible.
+        *
+        * @param $db DatabaseBase|null: (Optional) the database connection to
+        *            use. If null, resort to use the globally provided ways to
+        *            get database connections.
+        */
+       function setDb( DatabaseBase $db = null ) {
+               $this->forcedDb = $db;
+       }
+
        function __destruct() {
                if ( isset( $this->lb ) ) {
                        $this->lb->closeAll();
index ecf2afb..2b533ec 100644 (file)
@@ -84,6 +84,11 @@ class TextPassDumper extends BackupDumper {
                        unset( $this->lb );
                }
 
+               if ( $this->forcedDb !== null ) {
+                       $this->db = $this->forcedDb;
+                       return;
+               }
+
                if ( isset( $this->db ) && $this->db->isOpen() ) {
                        throw new MWException( 'DB is set and has not been closed by the Load Balancer' );
                }