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( $namespace ) {
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 ) {
219 if ( $this->uploads ) {
220 if ( $this->skippedNamespace( $revision->
getTitle() ) ) {
223 $this->uploadCount++;
225 $this->progress(
"upload: " . $revision->
getFilename() );
227 if ( !$this->dryRun ) {
231 $statusValue = $importer->import( $revision );
233 return $statusValue->isGood();
241 if ( $this->skippedNamespace( $rev->
getTitle() ) ) {
247 if ( !$this->dryRun ) {
252 private function report( $final =
false ) {
253 if ( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
258 private function showReport() {
259 if ( !$this->mQuiet ) {
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)" );
278 private function progress( $string ) {
279 fwrite( $this->stderr, $string .
"\n" );
282 private function importFromFile( $filename ) {
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() {
300 $file = fopen(
'php://stdin',
'rt' );
301 if ( self::posix_isatty( $file ) ) {
305 return $this->importFromHandle( $file );
308 private function importFromHandle( $handle ) {
309 $this->startTime = microtime(
true );
311 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [
'steal' =>
true ] );
315 ->getWikiImporterFactory()
319 $importer->disableStatisticsUpdate();
322 $importer->setDebug(
true );
324 if ( $this->
hasOption(
'no-updates' ) ) {
325 $importer->setNoUpdates(
true );
327 $importer->setUsernamePrefix(
328 $this->
getOption(
'username-prefix',
'imported' ),
332 $statusRootPage = $importer->setTargetRootPage( $this->
getOption(
'rootpage' ) );
333 if ( !$statusRootPage->isGood() ) {
339 $nthPage = (int)$this->
getOption(
'skip-to' );
340 $importer->setPageOffset( $nthPage );
341 $this->pageCount = $nthPage - 1;
343 $importer->setPageCallback( [ $this,
'reportPage' ] );
344 $importer->setNoticeCallback(
static function ( $msg,
$params ) {
347 $this->importCallback = $importer->setRevisionCallback(
348 [ $this,
'handleRevision' ] );
349 $this->uploadCallback = $importer->setUploadCallback(
350 [ $this,
'handleUpload' ] );
351 $this->logItemCallback = $importer->setLogItemCallback(
352 [ $this,
'handleLogItem' ] );
353 if ( $this->uploads ) {
354 $importer->setImportUploads(
true );
356 if ( $this->imageBasePath ) {
357 $importer->setImageBasePath( $this->imageBasePath );
360 if ( $this->dryRun ) {
361 $importer->setPageOutCallback(
null );
364 return $importer->doImport();
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.