17require_once __DIR__ .
'/../Maintenance.php';
18require_once __DIR__ .
'/../../includes/Export/WikiExporter.php';
39use Wikimedia\Rdbms\IMaintainableDatabase;
40use Wikimedia\Rdbms\IReadableDatabase;
41use Wikimedia\Timestamp\ConvertibleTimestamp;
42use Wikimedia\Timestamp\TimestampFormat as TS;
142 parent::__construct();
143 $this->stderr = fopen(
"php://stderr",
"wt" );
158 $this->
addOption(
'plugin',
'Load a dump plugin class. Specify as <class>[:<file>].',
159 false,
true,
false,
true );
160 $this->
addOption(
'output',
'Begin a filtered output stream; Specify as <type>:<file>. ' .
161 '<type>s: file, gzip, bzip2, 7zip, dbzip2, lbzip2',
false,
true,
'o',
true );
162 $this->
addOption(
'filter',
'Add a filter on an output branch. Specify as ' .
163 '<type>[:<options>]. <types>s: latest, notalk, namespace',
false,
true,
false,
true );
164 $this->
addOption(
'report',
'Report position and speed after every n pages processed. ' .
165 'Default: 100.',
false,
true );
166 $this->
addOption(
'7ziplevel',
'7zip compression level for all 7zip outputs. Used for ' .
167 '-mx option to 7za command.',
false,
true );
171 $this->
addOption(
'schema-version',
'Schema version to use for output.',
false,
true );
182 parent::finalSetup( $settingsBuilder );
186 $this->
addOption(
'schema-version',
'Schema version to use for output. ' .
195 $this->outputTypes[$name] = $class;
203 $this->filterTypes[$name] = $class;
217 $register = [ $class,
'register' ];
228 $this->schemaVersion = WikiExporter::schemaVersion();
231 foreach ( $options as [ $opt, $param ] ) {
234 $val = explode(
':', $param, 2 );
236 if ( count( $val ) === 1 ) {
238 } elseif ( count( $val ) === 2 ) {
244 $split = explode(
':', $param, 2 );
245 if ( count( $split ) !== 2 ) {
246 $this->
fatalError(
'Invalid output parameter' );
248 [ $type, $file ] = $split;
249 if (
$sink !==
null ) {
252 if ( !isset( $this->outputTypes[$type] ) ) {
253 $this->
fatalError(
"Unrecognized output sink type '$type'" );
255 $class = $this->outputTypes[$type];
256 if ( $type ===
"7zip" ) {
257 $sink =
new $class( $file, intval( $this->
getOption(
'7ziplevel' ) ) );
259 $sink =
new $class( $file );
266 $split = explode(
':', $param, 2 );
269 if ( !isset( $this->filterTypes[$key] ) ) {
270 $this->
fatalError(
"Unrecognized filter type '$key'" );
273 $type = $this->filterTypes[$key];
275 if ( count( $split ) === 2 ) {
276 $filter =
new $type(
$sink, $split[1] );
278 $filter =
new $type(
$sink );
286 case 'schema-version':
287 if ( !in_array( $param, XmlDumpWriter::$supportedSchemas ) ) {
289 "Unsupported schema version $param. Supported versions: " .
290 implode(
', ', XmlDumpWriter::$supportedSchemas )
293 $this->schemaVersion = $param;
299 $this->reportingInterval = intval( $this->
getOption(
'report' ) );
305 if ( count( $sinks ) > 1 ) {
316 public function dump( $history, $text = WikiExporter::TEXT ) {
317 # Notice messages will foul up your XML output even if they're
318 # relatively harmless.
319 if ( ini_get(
'display_errors' ) ) {
320 ini_set(
'display_errors',
'stderr' );
326 $exporter = $services->getWikiExporterFactory()->getWikiExporter(
330 $this->limitNamespaces
332 $exporter->setSchemaVersion( $this->schemaVersion );
337 $exporter->setOutputSink( $wrapper );
339 if ( !$this->skipHeader ) {
340 $exporter->openStream();
342 # Log item dumps: all or by range
343 if ( $history & WikiExporter::LOGS ) {
344 if ( $this->startId || $this->endId ) {
345 $exporter->logsByRange( $this->startId, $this->endId );
347 $exporter->allLogs();
349 } elseif ( $this->pages ===
null ) {
350 # Page dumps: all or by page ID range
351 if ( $this->startId || $this->endId ) {
352 $exporter->pagesByRange( $this->startId, $this->endId, $this->orderRevs );
353 } elseif ( $this->revStartId || $this->revEndId ) {
354 $exporter->revsByRange( $this->revStartId, $this->revEndId );
356 $exporter->allPages();
359 # Dump of specific pages
360 $exporter->pagesByName( $this->pages );
363 if ( !$this->skipFooter ) {
364 $exporter->closeStream();
377 $table = ( $history == WikiExporter::CURRENT ) ?
'page' :
'revision';
378 $field = ( $history == WikiExporter::CURRENT ) ?
'page_id' :
'rev_id';
381 $this->maxCount = $dbr->newSelectQueryBuilder()
382 ->select(
"MAX($field)" )
384 ->caller( __METHOD__ )->fetchField();
385 $this->startTime = microtime(
true );
386 $this->startTimer = ConvertibleTimestamp::hrtime();
388 $this->ID = getmypid();
392 if ( $this->forcedDb ) {
393 return $this->forcedDb;
397 ->getDBLoadBalancerFactory()
403 $db->setSessionOptions( [
'connTimeout' => 3600 * 24 ] );
415 parent::setDB( $db );
416 $this->forcedDb = $db;
428 public function report(
bool $final =
false ) {
429 if ( $final xor ( $this->
revCount % $this->reportingInterval == 0 ) ) {
435 if ( $this->reporting ) {
436 $now = ConvertibleTimestamp::now( TS::DB );
437 $nowTimer = ConvertibleTimestamp::hrtime();
438 $deltaAll = ( $nowTimer - $this->startTimer ) / 1e9;
439 $deltaPart = ( $nowTimer - $this->lastTimer ) / 1e9;
440 $this->pageCountPart = $this->pageCount - $this->pageCountLast;
441 $this->revCountPart = $this->
revCount - $this->revCountLast;
444 $portion = $this->
revCount / $this->maxCount;
449 $eta = $this->startTime + $deltaAll / $portion;
451 $pageRate = $this->pageCount / $deltaAll;
452 $revRate = $this->
revCount / $deltaAll;
459 $pageRatePart = $this->pageCountPart / $deltaPart;
460 $revRatePart = $this->revCountPart / $deltaPart;
466 $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
468 "%s: %s (ID %d) %d pages (%0.1f|%0.1f/sec all|curr), "
469 .
"%d revs (%0.1f|%0.1f/sec all|curr), ETA %s [max %d]",
470 $now, $dbDomain, $this->ID, $this->pageCount, $pageRate,
471 $pageRatePart, $this->
revCount, $revRate, $revRatePart, $etats,
474 $this->lastTimer = $nowTimer;
475 $this->revCountLast = $this->revCount;
480 if ( $this->reporting ) {
481 fwrite( $this->stderr, $string .
"\n" );
487class_alias( BackupDumper::class,
'BackupDumper' );
wfTimestamp( $outputtype=TS::UNIX, $ts=0)
Get a timestamp string in one of various formats.
if(!defined('MW_SETUP_CALLBACK'))
A class containing constants representing the names of configuration variables.
const XmlDumpSchemaVersion
Name constant for the XmlDumpSchemaVersion setting, for use with Config\get().
bool $skipHeader
don't output <mediawiki> and <siteinfo>
finalSetup(SettingsBuilder $settingsBuilder)
Handle some last-minute setup here.
bool $dumpUploadFileContents
dump( $history, $text=WikiExporter::TEXT)
int float $lastTimer
Timer reading of the previous report, not a timestamp.
array< string, class-string< DumpOutput > > $outputTypes
string null $schemaVersion
null means use default
string[] null $pages
null means all pages
processOptions()
Processes arguments and sets $this->$sink accordingly.
report(bool $final=false)
int float $startTimer
Timer reading taken at the same moment as $startTime.
string null $thisRevFormat
array null $limitNamespaces
float $startTime
UNIX timestamp, used as the basis for the reported ETA.
bool $skipFooter
don't output </mediawiki>
setDB(IMaintainableDatabase $db)
Force the dump to use the provided database connection for database operations, wherever possible.
string null $thisRevModel
array< string, class-string< DumpFilter > > $filterTypes
registerFilter( $name, $class)
ExportProgressFilter $egress
registerOutput( $name, $class)
DumpMultiWriter DumpOutput null $sink
Output filters.
initProgress( $history=WikiExporter::FULL)
Initialise starting time and maximum revision count.
loadPlugin( $class, $file)
Load a plugin and register it.
getAnalyticsReplica(string|false $virtualDomain=false)
Get a replica connection meant for slow, long-running or analytics-style queries (the "vslow" query g...
array $orderedOptions
Used to read the options in the order they were passed.
loadWithArgv( $argv)
Load params and arguments from a given array of command-line arguments.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
getServiceContainer()
Returns the main service container.
Advanced database interface for IDatabase handles that include maintenance methods.