| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
> |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
namespace MediaWiki\Maintenance; |
| 29 |
|
| 30 |
|
| 31 |
require_once __DIR__ . '/../Maintenance.php'; |
| 32 |
require_once __DIR__ . '/../../includes/export/WikiExporter.php'; |
| 33 |
|
| 34 |
|
| 35 |
use DumpMultiWriter; |
| 36 |
use DumpOutput; |
| 37 |
use ExportProgressFilter; |
| 38 |
use MediaWiki\MainConfigNames; |
| 39 |
use MediaWiki\Settings\SettingsBuilder; |
| 40 |
use MediaWiki\WikiMap\WikiMap; |
| 41 |
use WikiExporter; |
| 42 |
use Wikimedia\Rdbms\IMaintainableDatabase; |
| 43 |
use Wikimedia\Rdbms\LoadBalancer; |
| 44 |
use XmlDumpWriter; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
abstract class BackupDumper extends Maintenance { |
| 51 |
|
| 52 |
public $reporting = true; |
| 53 |
|
| 54 |
public $pages = null; |
| 55 |
|
| 56 |
public $skipHeader = false; |
| 57 |
|
| 58 |
public $skipFooter = false; |
| 59 |
|
| 60 |
public $startId = 0; |
| 61 |
|
| 62 |
public $endId = 0; |
| 63 |
|
| 64 |
public $revStartId = 0; |
| 65 |
|
| 66 |
public $revEndId = 0; |
| 67 |
|
| 68 |
public $dumpUploads = false; |
| 69 |
|
| 70 |
public $dumpUploadFileContents = false; |
| 71 |
|
| 72 |
public $orderRevs = false; |
| 73 |
|
| 74 |
public $limitNamespaces = []; |
| 75 |
|
| 76 |
public $stderr; |
| 77 |
|
| 78 |
|
| 79 |
protected $reportingInterval = 100; |
| 80 |
|
| 81 |
protected $pageCount = 0; |
| 82 |
|
| 83 |
protected $revCount = 0; |
| 84 |
|
| 85 |
protected $schemaVersion = null; |
| 86 |
|
| 87 |
protected $sink = null; |
| 88 |
|
| 89 |
protected $lastTime = 0; |
| 90 |
|
| 91 |
protected $pageCountLast = 0; |
| 92 |
|
| 93 |
protected $revCountLast = 0; |
| 94 |
|
| 95 |
|
| 96 |
protected $outputTypes = []; |
| 97 |
|
| 98 |
protected $filterTypes = []; |
| 99 |
|
| 100 |
|
| 101 |
protected $ID = 0; |
| 102 |
|
| 103 |
|
| 104 |
protected $startTime; |
| 105 |
|
| 106 |
protected $pageCountPart; |
| 107 |
|
| 108 |
protected $revCountPart; |
| 109 |
|
| 110 |
protected $maxCount; |
| 111 |
|
| 112 |
protected $timeOfCheckpoint; |
| 113 |
|
| 114 |
protected $egress; |
| 115 |
|
| 116 |
protected $buffer; |
| 117 |
|
| 118 |
protected $openElement; |
| 119 |
|
| 120 |
protected $atStart; |
| 121 |
|
| 122 |
protected $thisRevModel; |
| 123 |
|
| 124 |
protected $thisRevFormat; |
| 125 |
|
| 126 |
protected $lastName; |
| 127 |
|
| 128 |
protected $state; |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
protected $forcedDb = null; |
| 138 |
|
| 139 |
|
| 140 |
protected $lb; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public function __construct( $args = null ) { |
| 146 |
parent::__construct(); |
| 147 |
$this->stderr = fopen( "php://stderr", "wt" ); |
| 148 |
|
| 149 |
|
| 150 |
$this->registerOutput( 'file', \DumpFileOutput::class ); |
| 151 |
$this->registerOutput( 'gzip', \DumpGZipOutput::class ); |
| 152 |
$this->registerOutput( 'bzip2', \DumpBZip2Output::class ); |
| 153 |
$this->registerOutput( 'dbzip2', \DumpDBZip2Output::class ); |
| 154 |
$this->registerOutput( 'lbzip2', \DumpLBZip2Output::class ); |
| 155 |
$this->registerOutput( '7zip', \Dump7ZipOutput::class ); |
| 156 |
|
| 157 |
$this->registerFilter( 'latest', \DumpLatestFilter::class ); |
| 158 |
$this->registerFilter( 'notalk', \DumpNotalkFilter::class ); |
| 159 |
$this->registerFilter( 'namespace', \DumpNamespaceFilter::class ); |
| 160 |
|
| 161 |
|
| 162 |
$this->addOption( 'plugin', 'Load a dump plugin class. Specify as <class>[:<file>].', |
| 163 |
false, true, false, true ); |
| 164 |
$this->addOption( 'output', 'Begin a filtered output stream; Specify as <type>:<file>. ' . |
| 165 |
'<type>s: file, gzip, bzip2, 7zip, dbzip2, lbzip2', false, true, 'o', true ); |
| 166 |
$this->addOption( 'filter', 'Add a filter on an output branch. Specify as ' . |
| 167 |
'<type>[:<options>]. <types>s: latest, notalk, namespace', false, true, false, true ); |
| 168 |
$this->addOption( 'report', 'Report position and speed after every n pages processed. ' . |
| 169 |
'Default: 100.', false, true ); |
| 170 |
$this->addOption( '7ziplevel', '7zip compression level for all 7zip outputs. Used for ' . |
| 171 |
'-mx option to 7za command.', false, true ); |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
$this->addOption( 'schema-version', 'Schema version to use for output.', false, true ); |
| 176 |
|
| 177 |
if ( $args ) { |
| 178 |
|
| 179 |
|
| 180 |
$this->loadWithArgv( $args ); |
| 181 |
$this->processOptions(); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
public function finalSetup( SettingsBuilder $settingsBuilder ) { |
| 186 |
parent::finalSetup( $settingsBuilder ); |
| 187 |
|
| 188 |
|
| 189 |
$schemaVersion = $settingsBuilder->getConfig()->get( MainConfigNames::XmlDumpSchemaVersion ); |
| 190 |
$this->addOption( 'schema-version', 'Schema version to use for output. ' . |
| 191 |
'Default: ' . $schemaVersion, false, true ); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public function registerOutput( $name, $class ) { |
| 199 |
$this->outputTypes[$name] = $class; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public function registerFilter( $name, $class ) { |
| 207 |
$this->filterTypes[$name] = $class; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public function loadPlugin( $class, $file ) { |
| 218 |
if ( $file != '' ) { |
| 219 |
require_once $file; |
| 220 |
} |
| 221 |
$register = [ $class, 'register' ]; |
| 222 |
$register( $this ); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
protected function processOptions() { |
| 229 |
$sink = null; |
| 230 |
$sinks = []; |
| 231 |
|
| 232 |
$this->schemaVersion = WikiExporter::schemaVersion(); |
| 233 |
|
| 234 |
$options = $this->orderedOptions; |
| 235 |
foreach ( $options as [ $opt, $param ] ) { |
| 236 |
switch ( $opt ) { |
| 237 |
case 'plugin': |
| 238 |
$val = explode( ':', $param, 2 ); |
| 239 |
|
| 240 |
if ( count( $val ) === 1 ) { |
| 241 |
$this->loadPlugin( $val[0], '' ); |
| 242 |
} elseif ( count( $val ) === 2 ) { |
| 243 |
$this->loadPlugin( $val[0], $val[1] ); |
| 244 |
} |
| 245 |
|
| 246 |
break; |
| 247 |
case 'output': |
| 248 |
$split = explode( ':', $param, 2 ); |
| 249 |
if ( count( $split ) !== 2 ) { |
| 250 |
$this->fatalError( 'Invalid output parameter' ); |
| 251 |
} |
| 252 |
[ $type, $file ] = $split; |
| 253 |
if ( $sink !== null ) { |
| 254 |
$sinks[] = $sink; |
| 255 |
} |
| 256 |
if ( !isset( $this->outputTypes[$type] ) ) { |
| 257 |
$this->fatalError( "Unrecognized output sink type '$type'" ); |
| 258 |
} |
| 259 |
$class = $this->outputTypes[$type]; |
| 260 |
if ( $type === "7zip" ) { |
| 261 |
$sink = new $class( $file, intval( $this->getOption( '7ziplevel' ) ) ); |
| 262 |
} else { |
| 263 |
$sink = new $class( $file ); |
| 264 |
} |
| 265 |
|
| 266 |
break; |
| 267 |
case 'filter': |
| 268 |
$sink ??= new DumpOutput(); |
| 269 |
|
| 270 |
$split = explode( ':', $param, 2 ); |
| 271 |
$key = $split[0]; |
| 272 |
|
| 273 |
if ( !isset( $this->filterTypes[$key] ) ) { |
| 274 |
$this->fatalError( "Unrecognized filter type '$key'" ); |
| 275 |
} |
| 276 |
|
| 277 |
$type = $this->filterTypes[$key]; |
| 278 |
|
| 279 |
if ( count( $split ) === 2 ) { |
| 280 |
$filter = new $type( $sink, $split[1] ); |
| 281 |
} else { |
| 282 |
$filter = new $type( $sink ); |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
unset( $sink ); |
| 287 |
$sink = $filter; |
| 288 |
|
| 289 |
break; |
| 290 |
case 'schema-version': |
| 291 |
if ( !in_array( $param, XmlDumpWriter::$supportedSchemas ) ) { |
| 292 |
$this->fatalError( |
| 293 |
"Unsupported schema version $param. Supported versions: " . |
| 294 |
implode( ', ', XmlDumpWriter::$supportedSchemas ) |
| 295 |
); |
| 296 |
} |
| 297 |
$this->schemaVersion = $param; |
| 298 |
break; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
if ( $this->hasOption( 'report' ) ) { |
| 303 |
$this->reportingInterval = intval( $this->getOption( 'report' ) ); |
| 304 |
} |
| 305 |
|
| 306 |
$sink ??= new DumpOutput(); |
| 307 |
$sinks[] = $sink; |
| 308 |
|
| 309 |
if ( count( $sinks ) > 1 ) { |
| 310 |
$this->sink = new DumpMultiWriter( $sinks ); |
| 311 |
} else { |
| 312 |
$this->sink = $sink; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
public function dump( $history, $text = WikiExporter::TEXT ) { |
| 317 |
|
| 318 |
|
| 319 |
if ( ini_get( 'display_errors' ) ) { |
| 320 |
ini_set( 'display_errors', 'stderr' ); |
| 321 |
} |
| 322 |
|
| 323 |
$this->initProgress( $history ); |
| 324 |
|
| 325 |
$db = $this->backupDb(); |
| 326 |
$services = $this->getServiceContainer(); |
| 327 |
$exporter = $services->getWikiExporterFactory()->getWikiExporter( |
| 328 |
$db, |
| 329 |
$history, |
| 330 |
$text, |
| 331 |
$this->limitNamespaces |
| 332 |
); |
| 333 |
$exporter->setSchemaVersion( $this->schemaVersion ); |
| 334 |
$exporter->dumpUploads = $this->dumpUploads; |
| 335 |
$exporter->dumpUploadFileContents = $this->dumpUploadFileContents; |
| 336 |
|
| 337 |
$wrapper = new ExportProgressFilter( $this->sink, $this ); |
| 338 |
$exporter->setOutputSink( $wrapper ); |
| 339 |
|
| 340 |
if ( !$this->skipHeader ) { |
| 341 |
$exporter->openStream(); |
| 342 |
} |
| 343 |
|
| 344 |
if ( $history & WikiExporter::LOGS ) { |
| 345 |
if ( $this->startId || $this->endId ) { |
| 346 |
$exporter->logsByRange( $this->startId, $this->endId ); |
| 347 |
} else { |
| 348 |
$exporter->allLogs(); |
| 349 |
} |
| 350 |
} elseif ( $this->pages === null ) { |
| 351 |
|
| 352 |
if ( $this->startId || $this->endId ) { |
| 353 |
$exporter->pagesByRange( $this->startId, $this->endId, $this->orderRevs ); |
| 354 |
} elseif ( $this->revStartId || $this->revEndId ) { |
| 355 |
$exporter->revsByRange( $this->revStartId, $this->revEndId ); |
| 356 |
} else { |
| 357 |
$exporter->allPages(); |
| 358 |
} |
| 359 |
} else { |
| 360 |
|
| 361 |
$exporter->pagesByName( $this->pages ); |
| 362 |
} |
| 363 |
|
| 364 |
if ( !$this->skipFooter ) { |
| 365 |
$exporter->closeStream(); |
| 366 |
} |
| 367 |
|
| 368 |
$this->report( true ); |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
public function initProgress( $history = WikiExporter::FULL ) { |
| 378 |
$table = ( $history == WikiExporter::CURRENT ) ? 'page' : 'revision'; |
| 379 |
$field = ( $history == WikiExporter::CURRENT ) ? 'page_id' : 'rev_id'; |
| 380 |
|
| 381 |
$dbr = $this->forcedDb; |
| 382 |
if ( $this->forcedDb === null ) { |
| 383 |
$dbr = $this->getDB( DB_REPLICA, [ 'dump' ] ); |
| 384 |
} |
| 385 |
$this->maxCount = $dbr->newSelectQueryBuilder() |
| 386 |
->select( "MAX($field)" ) |
| 387 |
->from( $table ) |
| 388 |
->caller( __METHOD__ )->fetchField(); |
| 389 |
$this->startTime = microtime( true ); |
| 390 |
$this->lastTime = $this->startTime; |
| 391 |
$this->ID = getmypid(); |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
protected function backupDb() { |
| 398 |
if ( $this->forcedDb !== null ) { |
| 399 |
return $this->forcedDb; |
| 400 |
} |
| 401 |
|
| 402 |
$lbFactory = $this->getServiceContainer()->getDBLoadBalancerFactory(); |
| 403 |
$this->lb = $lbFactory->newMainLB(); |
| 404 |
$db = $this->lb->getMaintenanceConnectionRef( DB_REPLICA, 'dump' ); |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
$db->setSessionOptions( [ 'connTimeout' => 3600 * 24 ] ); |
| 409 |
|
| 410 |
return $db; |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
public function setDB( IMaintainableDatabase $db ) { |
| 420 |
parent::setDB( $db ); |
| 421 |
$this->forcedDb = $db; |
| 422 |
} |
| 423 |
|
| 424 |
public function __destruct() { |
| 425 |
if ( isset( $this->lb ) ) { |
| 426 |
$this->lb->closeAll( __METHOD__ ); |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
public function reportPage() { |
| 431 |
$this->pageCount++; |
| 432 |
} |
| 433 |
|
| 434 |
public function revCount() { |
| 435 |
$this->revCount++; |
| 436 |
$this->report(); |
| 437 |
} |
| 438 |
|
| 439 |
public function report( $final = false ) { |
| 440 |
if ( $final xor ( $this->revCount % $this->reportingInterval == 0 ) ) { |
| 441 |
$this->showReport(); |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
public function showReport() { |
| 446 |
if ( $this->reporting ) { |
| 447 |
$now = wfTimestamp( TS_DB ); |
| 448 |
$nowts = microtime( true ); |
| 449 |
$deltaAll = $nowts - $this->startTime; |
| 450 |
$deltaPart = $nowts - $this->lastTime; |
| 451 |
$this->pageCountPart = $this->pageCount - $this->pageCountLast; |
| 452 |
$this->revCountPart = $this->revCount - $this->revCountLast; |
| 453 |
|
| 454 |
if ( $deltaAll ) { |
| 455 |
$portion = $this->revCount / $this->maxCount; |
| 456 |
$eta = $this->startTime + $deltaAll / $portion; |
| 457 |
$etats = wfTimestamp( TS_DB, intval( $eta ) ); |
| 458 |
$pageRate = $this->pageCount / $deltaAll; |
| 459 |
$revRate = $this->revCount / $deltaAll; |
| 460 |
} else { |
| 461 |
$pageRate = '-'; |
| 462 |
$revRate = '-'; |
| 463 |
$etats = '-'; |
| 464 |
} |
| 465 |
if ( $deltaPart ) { |
| 466 |
$pageRatePart = $this->pageCountPart / $deltaPart; |
| 467 |
$revRatePart = $this->revCountPart / $deltaPart; |
| 468 |
} else { |
| 469 |
$pageRatePart = '-'; |
| 470 |
$revRatePart = '-'; |
| 471 |
} |
| 472 |
|
| 473 |
$dbDomain = WikiMap::getCurrentWikiDbDomain()->getId(); |
| 474 |
$this->progress( sprintf( |
| 475 |
"%s: %s (ID %d) %d pages (%0.1f|%0.1f/sec all|curr), " |
| 476 |
. "%d revs (%0.1f|%0.1f/sec all|curr), ETA %s [max %d]", |
| 477 |
$now, $dbDomain, $this->ID, $this->pageCount, $pageRate, |
| 478 |
$pageRatePart, $this->revCount, $revRate, $revRatePart, $etats, |
| 479 |
$this->maxCount |
| 480 |
) ); |
| 481 |
$this->lastTime = $nowts; |
| 482 |
$this->revCountLast = $this->revCount; |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
protected function progress( $string ) { |
| 487 |
if ( $this->reporting ) { |
| 488 |
fwrite( $this->stderr, $string . "\n" ); |
| 489 |
} |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
class_alias( BackupDumper::class, 'BackupDumper' ); |