| 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 |
use MediaWiki\Linker\LinkTarget; |
| 28 |
use MediaWiki\Permissions\UltimateAuthority; |
| 29 |
use MediaWiki\User\User; |
| 30 |
|
| 31 |
|
| 32 |
require_once __DIR__ . '/Maintenance.php'; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class BackupReader extends Maintenance { |
| 41 |
|
| 42 |
public $reportingInterval = 100; |
| 43 |
|
| 44 |
public $pageCount = 0; |
| 45 |
|
| 46 |
public $revCount = 0; |
| 47 |
|
| 48 |
public $dryRun = false; |
| 49 |
|
| 50 |
public $uploads = false; |
| 51 |
|
| 52 |
protected $uploadCount = 0; |
| 53 |
|
| 54 |
public $imageBasePath = false; |
| 55 |
|
| 56 |
public $nsFilter = false; |
| 57 |
|
| 58 |
public $stderr; |
| 59 |
|
| 60 |
protected $importCallback; |
| 61 |
|
| 62 |
protected $logItemCallback; |
| 63 |
|
| 64 |
protected $uploadCallback; |
| 65 |
|
| 66 |
protected $startTime; |
| 67 |
|
| 68 |
public function __construct() { |
| 69 |
parent::__construct(); |
| 70 |
$gz = in_array( 'compress.zlib', stream_get_wrappers() ) |
| 71 |
? 'ok' |
| 72 |
: '(disabled; requires PHP zlib module)'; |
| 73 |
$bz2 = in_array( 'compress.bzip2', stream_get_wrappers() ) |
| 74 |
? 'ok' |
| 75 |
: '(disabled; requires PHP bzip2 module)'; |
| 76 |
|
| 77 |
$this->addDescription( |
| 78 |
<<<TEXT |
| 79 |
This script reads pages from an XML file as produced from Special:Export or |
| 80 |
dumpBackup.php, and saves them into the current wiki. |
| 81 |
|
| 82 |
Compressed XML files may be read directly: |
| 83 |
.gz $gz |
| 84 |
.bz2 $bz2 |
| 85 |
.7z (if 7za executable is in PATH) |
| 86 |
|
| 87 |
Note that for very large data sets, importDump.php may be slow; there are |
| 88 |
alternate methods which can be much faster for full site restoration: |
| 89 |
<https://www.mediawiki.org/wiki/Manual:Importing_XML_dumps> |
| 90 |
TEXT |
| 91 |
); |
| 92 |
$this->stderr = fopen( "php://stderr", "wt" ); |
| 93 |
$this->addOption( 'report', |
| 94 |
'Report position and speed after every n pages processed', false, true ); |
| 95 |
$this->addOption( 'namespaces', |
| 96 |
'Import only the pages from namespaces belonging to the list of ' . |
| 97 |
'pipe-separated namespace names or namespace indexes', false, true ); |
| 98 |
$this->addOption( 'rootpage', 'Pages will be imported as subpages of the specified page', |
| 99 |
false, true ); |
| 100 |
$this->addOption( 'dry-run', 'Parse dump without actually importing pages' ); |
| 101 |
$this->addOption( 'debug', 'Output extra verbose debug information' ); |
| 102 |
$this->addOption( 'uploads', 'Process file upload data if included (experimental)' ); |
| 103 |
$this->addOption( |
| 104 |
'no-updates', |
| 105 |
'Disable link table updates. Is faster but leaves the wiki in an inconsistent state' |
| 106 |
); |
| 107 |
$this->addOption( 'image-base-path', 'Import files from a specified path', false, true ); |
| 108 |
$this->addOption( 'skip-to', 'Start from nth page by skipping first n-1 pages', false, true ); |
| 109 |
$this->addOption( 'username-prefix', |
| 110 |
'Prefix for interwiki usernames; a trailing ">" will be added. Default: "imported>"', |
| 111 |
false, true ); |
| 112 |
$this->addOption( 'no-local-users', |
| 113 |
'Treat all usernames as interwiki. ' . |
| 114 |
'The default is to assign edits to local users where they exist.', |
| 115 |
false, false |
| 116 |
); |
| 117 |
$this->addArg( 'file', 'Dump file to import [else use stdin]', false ); |
| 118 |
} |
| 119 |
|
| 120 |
public function execute() { |
| 121 |
if ( $this->getServiceContainer()->getReadOnlyMode()->isReadOnly() ) { |
| 122 |
$this->fatalError( "Wiki is in read-only mode; you'll need to disable it for import to work." ); |
| 123 |
} |
| 124 |
|
| 125 |
$this->reportingInterval = intval( $this->getOption( 'report', 100 ) ); |
| 126 |
if ( !$this->reportingInterval ) { |
| 127 |
|
| 128 |
$this->reportingInterval = 100; |
| 129 |
} |
| 130 |
|
| 131 |
$this->dryRun = $this->hasOption( 'dry-run' ); |
| 132 |
$this->uploads = $this->hasOption( 'uploads' ); |
| 133 |
|
| 134 |
if ( $this->hasOption( 'image-base-path' ) ) { |
| 135 |
$this->imageBasePath = $this->getOption( 'image-base-path' ); |
| 136 |
} |
| 137 |
if ( $this->hasOption( 'namespaces' ) ) { |
| 138 |
$this->setNsfilter( explode( '|', $this->getOption( 'namespaces' ) ) ); |
| 139 |
} |
| 140 |
|
| 141 |
if ( $this->hasArg( 0 ) ) { |
| 142 |
$this->importFromFile( $this->getArg( 0 ) ); |
| 143 |
} else { |
| 144 |
$this->importFromStdin(); |
| 145 |
} |
| 146 |
|
| 147 |
$this->output( "Done!\n" ); |
| 148 |
$this->output( "You might want to run rebuildrecentchanges.php to regenerate RecentChanges,\n" ); |
| 149 |
$this->output( "and initSiteStats.php to update page and revision counts\n" ); |
| 150 |
} |
| 151 |
|
| 152 |
private function setNsfilter( array $namespaces ) { |
| 153 |
if ( count( $namespaces ) == 0 ) { |
| 154 |
$this->nsFilter = false; |
| 155 |
|
| 156 |
return; |
| 157 |
} |
| 158 |
$this->nsFilter = array_unique( array_map( [ $this, 'getNsIndex' ], $namespaces ) ); |
| 159 |
} |
| 160 |
|
| 161 |
private function getNsIndex( $namespace ) { |
| 162 |
$contLang = $this->getServiceContainer()->getContentLanguage(); |
| 163 |
$result = $contLang->getNsIndex( $namespace ); |
| 164 |
if ( $result !== false ) { |
| 165 |
return $result; |
| 166 |
} |
| 167 |
$ns = intval( $namespace ); |
| 168 |
if ( strval( $ns ) === $namespace && $contLang->getNsText( $ns ) !== false ) { |
| 169 |
return $ns; |
| 170 |
} |
| 171 |
$this->fatalError( "Unknown namespace text / index specified: $namespace" ); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
private function skippedNamespace( $title ) { |
| 179 |
if ( $title === null ) { |
| 180 |
|
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
$ns = $title->getNamespace(); |
| 185 |
|
| 186 |
return is_array( $this->nsFilter ) && !in_array( $ns, $this->nsFilter ); |
| 187 |
} |
| 188 |
|
| 189 |
public function reportPage( $page ) { |
| 190 |
$this->pageCount++; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public function handleRevision( WikiRevision $rev ) { |
| 197 |
$title = $rev->getTitle(); |
| 198 |
if ( !$title ) { |
| 199 |
$this->progress( "Got bogus revision with null title!" ); |
| 200 |
|
| 201 |
return; |
| 202 |
} |
| 203 |
|
| 204 |
if ( $this->skippedNamespace( $title ) ) { |
| 205 |
return; |
| 206 |
} |
| 207 |
|
| 208 |
$this->revCount++; |
| 209 |
$this->report(); |
| 210 |
|
| 211 |
if ( !$this->dryRun ) { |
| 212 |
call_user_func( $this->importCallback, $rev ); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
public function handleUpload( WikiRevision $revision ) { |
| 221 |
if ( $this->uploads ) { |
| 222 |
if ( $this->skippedNamespace( $revision->getTitle() ) ) { |
| 223 |
return false; |
| 224 |
} |
| 225 |
$this->uploadCount++; |
| 226 |
|
| 227 |
$this->progress( "upload: " . $revision->getFilename() ); |
| 228 |
|
| 229 |
if ( !$this->dryRun ) { |
| 230 |
|
| 231 |
|
| 232 |
$importer = $this->getServiceContainer()->getWikiRevisionUploadImporter(); |
| 233 |
$statusValue = $importer->import( $revision ); |
| 234 |
|
| 235 |
return $statusValue->isGood(); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
return false; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public function handleLogItem( WikiRevision $rev ) { |
| 246 |
if ( $this->skippedNamespace( $rev->getTitle() ) ) { |
| 247 |
return; |
| 248 |
} |
| 249 |
$this->revCount++; |
| 250 |
$this->report(); |
| 251 |
|
| 252 |
if ( !$this->dryRun ) { |
| 253 |
call_user_func( $this->logItemCallback, $rev ); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
private function report( $final = false ) { |
| 258 |
if ( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) { |
| 259 |
$this->showReport(); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
private function showReport() { |
| 264 |
if ( !$this->mQuiet ) { |
| 265 |
$delta = microtime( true ) - $this->startTime; |
| 266 |
if ( $delta ) { |
| 267 |
$rate = sprintf( "%.2f", $this->pageCount / $delta ); |
| 268 |
$revrate = sprintf( "%.2f", $this->revCount / $delta ); |
| 269 |
} else { |
| 270 |
$rate = '-'; |
| 271 |
$revrate = '-'; |
| 272 |
} |
| 273 |
|
| 274 |
if ( $this->pageCount ) { |
| 275 |
$this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" ); |
| 276 |
} else { |
| 277 |
$this->progress( "$this->revCount ($revrate revs/sec)" ); |
| 278 |
} |
| 279 |
} |
| 280 |
$this->waitForReplication(); |
| 281 |
} |
| 282 |
|
| 283 |
private function progress( $string ) { |
| 284 |
fwrite( $this->stderr, $string . "\n" ); |
| 285 |
} |
| 286 |
|
| 287 |
private function importFromFile( $filename ) { |
| 288 |
if ( preg_match( '/\.gz$/', $filename ) ) { |
| 289 |
$filename = 'compress.zlib://' . $filename; |
| 290 |
} elseif ( preg_match( '/\.bz2$/', $filename ) ) { |
| 291 |
$filename = 'compress.bzip2://' . $filename; |
| 292 |
} elseif ( preg_match( '/\.7z$/', $filename ) ) { |
| 293 |
$filename = 'mediawiki.compress.7z://' . $filename; |
| 294 |
} |
| 295 |
|
| 296 |
$file = fopen( $filename, 'rt' ); |
| 297 |
if ( $file === false ) { |
| 298 |
$this->fatalError( error_get_last()['message'] ?? 'Could not open file' ); |
| 299 |
} |
| 300 |
|
| 301 |
return $this->importFromHandle( $file ); |
| 302 |
} |
| 303 |
|
| 304 |
private function importFromStdin() { |
| 305 |
$file = fopen( 'php://stdin', 'rt' ); |
| 306 |
if ( self::posix_isatty( $file ) ) { |
| 307 |
$this->maybeHelp( true ); |
| 308 |
} |
| 309 |
|
| 310 |
return $this->importFromHandle( $file ); |
| 311 |
} |
| 312 |
|
| 313 |
private function importFromHandle( $handle ) { |
| 314 |
$this->startTime = microtime( true ); |
| 315 |
|
| 316 |
$user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] ); |
| 317 |
|
| 318 |
$source = new ImportStreamSource( $handle ); |
| 319 |
$importer = $this->getServiceContainer() |
| 320 |
->getWikiImporterFactory() |
| 321 |
->getWikiImporter( $source, new UltimateAuthority( $user ) ); |
| 322 |
|
| 323 |
|
| 324 |
$importer->disableStatisticsUpdate(); |
| 325 |
|
| 326 |
if ( $this->hasOption( 'debug' ) ) { |
| 327 |
$importer->setDebug( true ); |
| 328 |
} |
| 329 |
if ( $this->hasOption( 'no-updates' ) ) { |
| 330 |
$importer->setNoUpdates( true ); |
| 331 |
} |
| 332 |
$importer->setUsernamePrefix( |
| 333 |
$this->getOption( 'username-prefix', 'imported' ), |
| 334 |
!$this->hasOption( 'no-local-users' ) |
| 335 |
); |
| 336 |
if ( $this->hasOption( 'rootpage' ) ) { |
| 337 |
$statusRootPage = $importer->setTargetRootPage( $this->getOption( 'rootpage' ) ); |
| 338 |
if ( !$statusRootPage->isGood() ) { |
| 339 |
|
| 340 |
$this->fatalError( $statusRootPage ); |
| 341 |
} |
| 342 |
} |
| 343 |
if ( $this->hasOption( 'skip-to' ) ) { |
| 344 |
$nthPage = (int)$this->getOption( 'skip-to' ); |
| 345 |
$importer->setPageOffset( $nthPage ); |
| 346 |
$this->pageCount = $nthPage - 1; |
| 347 |
} |
| 348 |
$importer->setPageCallback( [ $this, 'reportPage' ] ); |
| 349 |
$importer->setNoticeCallback( static function ( $msg, $params ) { |
| 350 |
echo wfMessage( $msg, $params )->text() . "\n"; |
| 351 |
} ); |
| 352 |
$this->importCallback = $importer->setRevisionCallback( |
| 353 |
[ $this, 'handleRevision' ] ); |
| 354 |
$this->uploadCallback = $importer->setUploadCallback( |
| 355 |
[ $this, 'handleUpload' ] ); |
| 356 |
$this->logItemCallback = $importer->setLogItemCallback( |
| 357 |
[ $this, 'handleLogItem' ] ); |
| 358 |
if ( $this->uploads ) { |
| 359 |
$importer->setImportUploads( true ); |
| 360 |
} |
| 361 |
if ( $this->imageBasePath ) { |
| 362 |
$importer->setImageBasePath( $this->imageBasePath ); |
| 363 |
} |
| 364 |
|
| 365 |
if ( $this->dryRun ) { |
| 366 |
$importer->setPageOutCallback( null ); |
| 367 |
} |
| 368 |
|
| 369 |
return $importer->doImport(); |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
$maintClass = BackupReader::class; |
| 375 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 376 |
|