Merge "MessageFormatterFactory"
[lhc/web/wiklou.git] / maintenance / includes / BackupDumper.php
index 45786d8..358dc21 100644 (file)
  */
 
 require_once __DIR__ . '/../Maintenance.php';
+require_once __DIR__ . '/../../includes/export/WikiExporter.php';
 
 use MediaWiki\MediaWikiServices;
 use Wikimedia\Rdbms\LoadBalancer;
-use Wikimedia\Rdbms\IDatabase;
+use Wikimedia\Rdbms\IMaintainableDatabase;
 
 /**
  * @ingroup Dump
@@ -47,10 +48,12 @@ abstract class BackupDumper extends Maintenance {
        public $dumpUploads = false;
        public $dumpUploadFileContents = false;
        public $orderRevs = false;
+       public $limitNamespaces = [];
 
        protected $reportingInterval = 100;
        protected $pageCount = 0;
        protected $revCount = 0;
+       protected $schemaVersion = null; // use default
        protected $server = null; // use default
        protected $sink = null; // Output filters
        protected $lastTime = 0;
@@ -65,7 +68,7 @@ abstract class BackupDumper extends Maintenance {
        /**
         * The dependency-injected database to use.
         *
-        * @var IDatabase|null
+        * @var IMaintainableDatabase|null
         *
         * @see self::setDB
         */
@@ -86,6 +89,7 @@ abstract class BackupDumper extends Maintenance {
                $this->registerOutput( 'gzip', DumpGZipOutput::class );
                $this->registerOutput( 'bzip2', DumpBZip2Output::class );
                $this->registerOutput( 'dbzip2', DumpDBZip2Output::class );
+               $this->registerOutput( 'lbzip2', DumpLBZip2Output::class );
                $this->registerOutput( '7zip', Dump7ZipOutput::class );
 
                $this->registerFilter( 'latest', DumpLatestFilter::class );
@@ -96,11 +100,13 @@ abstract class BackupDumper extends Maintenance {
                $this->addOption( 'plugin', 'Load a dump plugin class. Specify as <class>[:<file>].',
                        false, true, false, true );
                $this->addOption( 'output', 'Begin a filtered output stream; Specify as <type>:<file>. ' .
-                       '<type>s: file, gzip, bzip2, 7zip, dbzip2', false, true, false, true );
+                       '<type>s: file, gzip, bzip2, 7zip, dbzip2, lbzip2', false, true, false, true );
                $this->addOption( 'filter', 'Add a filter on an output branch. Specify as ' .
                        '<type>[:<options>]. <types>s: latest, notalk, namespace', false, true, false, true );
                $this->addOption( 'report', 'Report position and speed after every n pages processed. ' .
                        'Default: 100.', false, true );
+               $this->addOption( 'schema-version', 'Schema version to use for output. ' .
+                       'Default: ' . WikiExporter::schemaVersion(), false, true );
                $this->addOption( 'server', 'Force reading from MySQL server', false, true );
                $this->addOption( '7ziplevel', '7zip compression level for all 7zip outputs. Used for ' .
                        '-mx option to 7za command.', false, true );
@@ -155,10 +161,11 @@ abstract class BackupDumper extends Maintenance {
                $sink = null;
                $sinks = [];
 
+               $this->schemaVersion = WikiExporter::schemaVersion();
+
                $options = $this->orderedOptions;
                foreach ( $options as $arg ) {
-                       $opt = $arg[0];
-                       $param = $arg[1];
+                       list( $opt, $param ) = $arg;
 
                        switch ( $opt ) {
                                case 'plugin':
@@ -215,6 +222,15 @@ abstract class BackupDumper extends Maintenance {
                                        unset( $sink );
                                        $sink = $filter;
 
+                                       break;
+                               case 'schema-version':
+                                       if ( !in_array( $param, XmlDumpWriter::$supportedSchemas ) ) {
+                                               $this->fatalError(
+                                                       "Unsupported schema version $param. Supported versions: " .
+                                                       implode( ', ', XmlDumpWriter::$supportedSchemas )
+                                               );
+                                       }
+                                       $this->schemaVersion = $param;
                                        break;
                        }
                }
@@ -249,7 +265,8 @@ abstract class BackupDumper extends Maintenance {
                $this->initProgress( $history );
 
                $db = $this->backupDb();
-               $exporter = new WikiExporter( $db, $history, $text );
+               $exporter = new WikiExporter( $db, $history, $text, $this->limitNamespaces );
+               $exporter->setSchemaVersion( $this->schemaVersion );
                $exporter->dumpUploads = $this->dumpUploads;
                $exporter->dumpUploadFileContents = $this->dumpUploadFileContents;
 
@@ -299,7 +316,7 @@ abstract class BackupDumper extends Maintenance {
 
                $dbr = $this->forcedDb;
                if ( $this->forcedDb === null ) {
-                       $dbr = wfGetDB( DB_REPLICA );
+                       $dbr = $this->getDB( DB_REPLICA );
                }
                $this->maxCount = $dbr->selectField( $table, "MAX($field)", '', __METHOD__ );
                $this->startTime = microtime( true );
@@ -311,7 +328,7 @@ abstract class BackupDumper extends Maintenance {
         * @todo Fixme: the --server parameter is currently not respected, as it
         * doesn't seem terribly easy to ask the load balancer for a particular
         * connection by name.
-        * @return IDatabase
+        * @return IMaintainableDatabase
         */
        function backupDb() {
                if ( $this->forcedDb !== null ) {
@@ -320,7 +337,7 @@ abstract class BackupDumper extends Maintenance {
 
                $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
                $this->lb = $lbFactory->newMainLB();
-               $db = $this->lb->getConnection( DB_REPLICA, 'dump' );
+               $db = $this->lb->getMaintenanceConnectionRef( DB_REPLICA, 'dump' );
 
                // Discourage the server from disconnecting us if it takes a long time
                // to read out the big ol' batch query.
@@ -333,10 +350,9 @@ abstract class BackupDumper extends Maintenance {
         * Force the dump to use the provided database connection for database
         * operations, wherever possible.
         *
-        * @param IDatabase|null $db (Optional) the database connection to use. If null, resort to
-        *   use the globally provided ways to get database connections.
+        * @param IMaintainableDatabase $db The database connection to use
         */
-       function setDB( IDatabase $db = null ) {
+       function setDB( IMaintainableDatabase $db ) {
                parent::setDB( $db );
                $this->forcedDb = $db;
        }
@@ -395,10 +411,12 @@ abstract class BackupDumper extends Maintenance {
                                $pageRatePart = '-';
                                $revRatePart = '-';
                        }
+
+                       $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
                        $this->progress( sprintf(
                                "%s: %s (ID %d) %d pages (%0.1f|%0.1f/sec all|curr), "
                                        . "%d revs (%0.1f|%0.1f/sec all|curr), ETA %s [max %d]",
-                               $now, wfWikiID(), $this->ID, $this->pageCount, $pageRate,
+                               $now, $dbDomain, $this->ID, $this->pageCount, $pageRate,
                                $pageRatePart, $this->revCount, $revRate, $revRatePart, $etats,
                                $this->maxCount
                        ) );