70 parent::__construct();
71 $gz = in_array(
'compress.zlib', stream_get_wrappers() )
73 :
'(disabled; requires PHP zlib module)';
74 $bz2 = in_array(
'compress.bzip2', stream_get_wrappers() )
76 :
'(disabled; requires PHP bzip2 module)';
80This script reads pages from an XML file as produced from Special:Export or
81dumpBackup.php, and saves them into the current wiki.
83Compressed XML files may be read directly:
86 .7z (
if 7za executable is in PATH)
88Note that
for very large data sets, importDump.php may be slow; there are
89alternate methods which can be much faster
for full site restoration:
93 $this->stderr = fopen(
"php://stderr",
"wt" );
95 'Report position and speed after every n pages processed',
false,
true );
97 'Import only the pages from namespaces belonging to the list of ' .
98 'pipe-separated namespace names or namespace indexes',
false,
true );
99 $this->
addOption(
'rootpage',
'Pages will be imported as subpages of the specified page',
101 $this->
addOption(
'dry-run',
'Parse dump without actually importing pages' );
102 $this->
addOption(
'debug',
'Output extra verbose debug information' );
103 $this->
addOption(
'uploads',
'Process file upload data if included (experimental)' );
106 'Disable link table updates. Is faster but leaves the wiki in an inconsistent state'
108 $this->
addOption(
'image-base-path',
'Import files from a specified path',
false,
true );
109 $this->
addOption(
'skip-to',
'Start from nth page by skipping first n-1 pages',
false,
true );
111 'Prefix for interwiki usernames; a trailing ">" will be added. Default: "imported>"',
114 'Treat all usernames as interwiki. ' .
115 'The default is to assign edits to local users where they exist.',
118 $this->
addArg(
'file',
'Dump file to import [else use stdin]',
false );
123 $this->
fatalError(
"Wiki is in read-only mode; you'll need to disable it for import to work." );
126 $this->reportingInterval = intval( $this->
getOption(
'report', 100 ) );
127 if ( !$this->reportingInterval ) {
129 $this->reportingInterval = 100;
132 $this->dryRun = $this->
hasOption(
'dry-run' );
133 $this->uploads = $this->
hasOption(
'uploads' );
135 if ( $this->
hasOption(
'image-base-path' ) ) {
136 $this->imageBasePath = $this->
getOption(
'image-base-path' );
138 if ( $this->
hasOption(
'namespaces' ) ) {
139 $this->setNsfilter( explode(
'|', $this->
getOption(
'namespaces' ) ) );
142 if ( $this->
hasArg( 0 ) ) {
143 $this->importFromFile( $this->
getArg( 0 ) );
145 $this->importFromStdin();
148 $this->
output(
"Done!\n" );
149 $this->
output(
"You might want to run rebuildrecentchanges.php to regenerate RecentChanges,\n" );
150 $this->
output(
"and initSiteStats.php to update page and revision counts\n" );
153 private function setNsfilter( array $namespaces ) {
154 if ( count( $namespaces ) == 0 ) {
155 $this->nsFilter =
false;
159 $this->nsFilter = array_unique( array_map( [ $this,
'getNsIndex' ], $namespaces ) );
162 private function getNsIndex(
string $namespace ): int {
164 $result = $contLang->getNsIndex( $namespace );
165 if ( $result !==
false ) {
168 $ns = intval( $namespace );
169 if ( strval( $ns ) === $namespace && $contLang->getNsText( $ns ) !==
false ) {
172 $this->
fatalError(
"Unknown namespace text / index specified: $namespace" );
179 private function skippedNamespace( $title ) {
180 if ( $title ===
null ) {
185 $ns = $title->getNamespace();
187 return is_array( $this->nsFilter ) && !in_array( $ns, $this->nsFilter );
197 $this->progress(
"Got bogus revision with null title!" );
202 if ( $this->skippedNamespace( $title ) ) {
209 if ( !$this->dryRun ) {
210 ( $this->importCallback )( $rev );
219 if ( $this->uploads ) {
220 if ( $this->skippedNamespace( $revision->
getTitle() ) ) {
223 $this->uploadCount++;
225 $this->progress(
"upload: " . $revision->
getFilename() );
227 if ( !$this->dryRun ) {
230 $importer = $this->getServiceContainer()->getWikiRevisionUploadImporter();
231 $statusValue = $importer->import( $revision );
233 return $statusValue->isGood();
241 if ( $this->skippedNamespace( $rev->
getTitle() ) ) {
247 if ( !$this->dryRun ) {
248 ( $this->logItemCallback )( $rev );
252 private function report(
bool $final =
false ) {
253 if ( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
258 private function showReport() {
259 if ( !$this->mQuiet ) {
260 $delta = microtime(
true ) - $this->startTime;
262 $rate = sprintf(
"%.2f", $this->pageCount / $delta );
263 $revrate = sprintf(
"%.2f", $this->revCount / $delta );
268 # Logs dumps don't have page tallies
269 if ( $this->pageCount ) {
270 $this->progress(
"$this->pageCount ($rate pages/sec $revrate revs/sec)" );
272 $this->progress(
"$this->revCount ($revrate revs/sec)" );
275 $this->waitForReplication();
278 private function progress(
string $string ) {
279 fwrite( $this->stderr, $string .
"\n" );
282 private function importFromFile(
string $filename ): bool {
283 if ( preg_match(
'/\.gz$/', $filename ) ) {
284 $filename =
'compress.zlib://' . $filename;
285 } elseif ( preg_match(
'/\.bz2$/', $filename ) ) {
286 $filename =
'compress.bzip2://' . $filename;
287 } elseif ( preg_match(
'/\.7z$/', $filename ) ) {
288 $filename =
'mediawiki.compress.7z://' . $filename;
291 $file = fopen( $filename,
'rt' );
292 if ( $file ===
false ) {
293 $this->fatalError( error_get_last()[
'message'] ??
'Could not open file' );
296 return $this->importFromHandle( $file );
299 private function importFromStdin(): bool {
300 $file = fopen(
'php://stdin',
'rt' );
301 if ( self::posix_isatty( $file ) ) {
302 $this->maybeHelp(
true );
305 return $this->importFromHandle( $file );
311 private function importFromHandle( $handle ): bool {
312 $this->startTime = microtime( true );
314 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [
'steal' =>
true ] );
317 $importer = $this->getServiceContainer()
318 ->getWikiImporterFactory()
322 $importer->disableStatisticsUpdate();
324 if ( $this->hasOption(
'debug' ) ) {
325 $importer->setDebug(
true );
327 if ( $this->hasOption(
'no-updates' ) ) {
328 $importer->setNoUpdates(
true );
330 $importer->setUsernamePrefix(
331 $this->getOption(
'username-prefix',
'imported' ),
332 !$this->hasOption(
'no-local-users' )
334 if ( $this->hasOption(
'rootpage' ) ) {
335 $statusRootPage = $importer->setTargetRootPage( $this->getOption(
'rootpage' ) );
336 if ( !$statusRootPage->isGood() ) {
338 $this->fatalError( $statusRootPage );
341 if ( $this->hasOption(
'skip-to' ) ) {
342 $nthPage = (int)$this->getOption(
'skip-to' );
343 $importer->setPageOffset( $nthPage );
344 $this->pageCount = $nthPage - 1;
346 $importer->setPageCallback( [ $this,
'reportPage' ] );
347 $importer->setNoticeCallback(
static function ( $msg, $params ) {
348 echo
wfMessage( $msg, $params )->text() .
"\n";
350 $this->importCallback = $importer->setRevisionCallback(
351 [ $this,
'handleRevision' ] );
352 $this->uploadCallback = $importer->setUploadCallback(
353 [ $this,
'handleUpload' ] );
354 $this->logItemCallback = $importer->setLogItemCallback(
355 [ $this,
'handleLogItem' ] );
356 if ( $this->uploads ) {
357 $importer->setImportUploads(
true );
359 if ( $this->imageBasePath ) {
360 $importer->setImageBasePath( $this->imageBasePath );
363 if ( $this->dryRun ) {
364 $importer->setPageOutCallback(
null );
367 return $importer->doImport();