58 parent::__construct();
59 $gz = in_array(
'compress.zlib', stream_get_wrappers() )
61 :
'(disabled; requires PHP zlib module)';
62 $bz2 = in_array(
'compress.bzip2', stream_get_wrappers() )
64 :
'(disabled; requires PHP bzip2 module)';
68This script reads pages from an XML file as produced from Special:Export or
69dumpBackup.php, and saves them into the current wiki.
71Compressed XML files may be read directly:
74 .7z (
if 7za executable is in PATH)
76Note that
for very large data sets, importDump.php may be slow; there are
77alternate methods which can be much faster
for full site restoration:
81 $this->stderr = fopen(
"php://stderr",
"wt" );
83 'Report position and speed after every n pages processed',
false,
true );
85 'Import only the pages from namespaces belonging to the list of ' .
86 'pipe-separated namespace names or namespace indexes',
false,
true );
87 $this->
addOption(
'rootpage',
'Pages will be imported as subpages of the specified page',
89 $this->
addOption(
'dry-run',
'Parse dump without actually importing pages' );
90 $this->
addOption(
'debug',
'Output extra verbose debug information' );
91 $this->
addOption(
'uploads',
'Process file upload data if included (experimental)' );
94 'Disable link table updates. Is faster but leaves the wiki in an inconsistent state'
96 $this->
addOption(
'image-base-path',
'Import files from a specified path',
false,
true );
97 $this->
addOption(
'skip-to',
'Start from nth page by skipping first n-1 pages',
false,
true );
99 'Prefix for interwiki usernames; a trailing ">" will be added. Default: "imported>"',
102 'Treat all usernames as interwiki. ' .
103 'The default is to assign edits to local users where they exist.',
106 $this->
addArg(
'file',
'Dump file to import [else use stdin]',
false );
111 $this->
fatalError(
"Wiki is in read-only mode; you'll need to disable it for import to work." );
114 $this->reportingInterval = intval( $this->
getOption(
'report', 100 ) );
115 if ( !$this->reportingInterval ) {
117 $this->reportingInterval = 100;
120 $this->dryRun = $this->
hasOption(
'dry-run' );
121 $this->uploads = $this->
hasOption(
'uploads' );
123 if ( $this->
hasOption(
'image-base-path' ) ) {
124 $this->imageBasePath = $this->
getOption(
'image-base-path' );
126 if ( $this->
hasOption(
'namespaces' ) ) {
127 $this->setNsfilter( explode(
'|', $this->
getOption(
'namespaces' ) ) );
130 if ( $this->
hasArg( 0 ) ) {
131 $this->importFromFile( $this->
getArg( 0 ) );
133 $this->importFromStdin();
136 $this->
output(
"Done!\n" );
137 $this->
output(
"You might want to run rebuildrecentchanges.php to regenerate RecentChanges,\n" );
138 $this->
output(
"and initSiteStats.php to update page and revision counts\n" );
141 private function setNsfilter( array $namespaces ) {
142 if ( count( $namespaces ) == 0 ) {
143 $this->nsFilter =
false;
147 $this->nsFilter = array_unique( array_map( $this->getNsIndex( ... ), $namespaces ) );
150 private function getNsIndex(
string $namespace ): int {
152 $result = $contLang->getNsIndex( $namespace );
153 if ( $result !==
false ) {
156 $ns = intval( $namespace );
157 if ( strval( $ns ) === $namespace && $contLang->getNsText( $ns ) !==
false ) {
160 $this->
fatalError(
"Unknown namespace text / index specified: $namespace" );
167 private function skippedNamespace( $title ) {
168 if ( $title ===
null ) {
173 $ns = $title->getNamespace();
175 return is_array( $this->nsFilter ) && !in_array( $ns, $this->nsFilter );
186 $this->progress(
"Got bogus revision with null title!" );
191 if ( $this->skippedNamespace( $title ) ) {
197 if ( !$this->dryRun ) {
198 ( $this->importCallback )( $rev );
207 if ( $this->uploads ) {
208 if ( $this->skippedNamespace( $revision->
getTitle() ) ) {
211 $this->uploadCount++;
213 $this->progress(
"upload: " . $revision->
getFilename() );
215 if ( !$this->dryRun ) {
218 $importer = $this->getServiceContainer()->getWikiRevisionUploadImporter();
219 $statusValue = $importer->import( $revision );
221 return $statusValue->isGood();
229 if ( $this->skippedNamespace( $rev->
getTitle() ) ) {
235 if ( !$this->dryRun ) {
236 ( $this->logItemCallback )( $rev );
240 private function report(
bool $final =
false ) {
241 if ( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
246 private function showReport() {
247 if ( !$this->mQuiet ) {
248 $delta = microtime(
true ) - $this->startTime;
250 $rate = sprintf(
"%.2f", $this->pageCount / $delta );
251 $revrate = sprintf(
"%.2f", $this->revCount / $delta );
256 # Logs dumps don't have page tallies
257 if ( $this->pageCount ) {
258 $this->progress(
"$this->pageCount ($rate pages/sec $revrate revs/sec)" );
260 $this->progress(
"$this->revCount ($revrate revs/sec)" );
263 $this->waitForReplication();
266 private function progress(
string $string ) {
267 fwrite( $this->stderr, $string .
"\n" );
270 private function importFromFile(
string $filename ): bool {
271 if ( preg_match(
'/\.gz$/', $filename ) ) {
272 $filename =
'compress.zlib://' . $filename;
273 } elseif ( preg_match(
'/\.bz2$/', $filename ) ) {
274 $filename =
'compress.bzip2://' . $filename;
275 } elseif ( preg_match(
'/\.7z$/', $filename ) ) {
276 $filename =
'mediawiki.compress.7z://' . $filename;
279 $file = fopen( $filename,
'rt' );
280 if ( $file ===
false ) {
281 $this->fatalError( error_get_last()[
'message'] ??
'Could not open file' );
284 return $this->importFromHandle( $file );
287 private function importFromStdin(): bool {
288 $file = fopen(
'php://stdin',
'rt' );
289 if ( self::posix_isatty( $file ) ) {
290 $this->maybeHelp(
true );
293 return $this->importFromHandle( $file );
299 private function importFromHandle( $handle ): bool {
300 $this->startTime = microtime( true );
302 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [
'steal' =>
true ] );
305 $importer = $this->getServiceContainer()
306 ->getWikiImporterFactory()
310 $importer->disableStatisticsUpdate();
312 if ( $this->hasOption(
'debug' ) ) {
313 $importer->setDebug(
true );
315 if ( $this->hasOption(
'no-updates' ) ) {
316 $importer->setNoUpdates(
true );
318 $importer->setUsernamePrefix(
319 $this->getOption(
'username-prefix',
'imported' ),
320 !$this->hasOption(
'no-local-users' )
322 if ( $this->hasOption(
'rootpage' ) ) {
323 $statusRootPage = $importer->setTargetRootPage( $this->getOption(
'rootpage' ) );
324 if ( !$statusRootPage->isGood() ) {
326 $this->fatalError( $statusRootPage );
329 if ( $this->hasOption(
'skip-to' ) ) {
330 $nthPage = (int)$this->getOption(
'skip-to' );
331 $importer->setPageOffset( $nthPage );
332 $this->pageCount = $nthPage - 1;
334 $importer->setPageCallback( $this->reportPage( ... ) );
335 $importer->setNoticeCallback(
static function ( $msg, $params ) {
336 echo
wfMessage( $msg, $params )->text() .
"\n";
338 $this->importCallback = $importer->setRevisionCallback(
339 $this->handleRevision( ... ) );
340 $this->uploadCallback = $importer->setUploadCallback(
341 $this->handleUpload( ... ) );
342 $this->logItemCallback = $importer->setLogItemCallback(
343 $this->handleLogItem( ... ) );
344 if ( $this->uploads ) {
345 $importer->setImportUploads(
true );
347 if ( $this->imageBasePath ) {
348 $importer->setImageBasePath( $this->imageBasePath );
351 if ( $this->dryRun ) {
352 $importer->setPageOutCallback(
null );
355 return $importer->doImport();