29 require_once __DIR__ .
'/Maintenance.php';
65 parent::__construct();
66 $gz = in_array(
'compress.zlib', stream_get_wrappers() )
68 :
'(disabled; requires PHP zlib module)';
69 $bz2 = in_array(
'compress.bzip2', stream_get_wrappers() )
71 :
'(disabled; requires PHP bzip2 module)';
75 This script reads pages from an XML file as produced from Special:Export or
76 dumpBackup.php, and saves them into the current wiki.
78 Compressed XML files may be read directly:
81 .7z (
if 7za executable is in PATH)
83 Note that
for very large data sets, importDump.php may be slow; there are
84 alternate methods which can be much faster
for full site restoration:
88 $this->stderr = fopen(
"php://stderr",
"wt" );
90 'Report position and speed after every n pages processed',
false,
true );
92 'Import only the pages from namespaces belonging to the list of ' .
93 'pipe-separated namespace names or namespace indexes',
false,
true );
94 $this->
addOption(
'rootpage',
'Pages will be imported as subpages of the specified page',
96 $this->
addOption(
'dry-run',
'Parse dump without actually importing pages' );
97 $this->
addOption(
'debug',
'Output extra verbose debug information' );
98 $this->
addOption(
'uploads',
'Process file upload data if included (experimental)' );
101 'Disable link table updates. Is faster but leaves the wiki in an inconsistent state'
103 $this->
addOption(
'image-base-path',
'Import files from a specified path',
false,
true );
104 $this->
addOption(
'skip-to',
'Start from nth page by skipping first n-1 pages',
false,
true );
105 $this->
addOption(
'username-prefix',
'Prefix for interwiki usernames',
false,
true );
107 'Treat all usernames as interwiki. ' .
108 'The default is to assign edits to local users where they exist.',
111 $this->
addArg(
'file',
'Dump file to import [else use stdin]',
false );
116 $this->
fatalError(
"Wiki is in read-only mode; you'll need to disable it for import to work." );
119 $this->reportingInterval = intval( $this->
getOption(
'report', 100 ) );
120 if ( !$this->reportingInterval ) {
122 $this->reportingInterval = 100;
125 $this->dryRun = $this->
hasOption(
'dry-run' );
126 $this->uploads = $this->
hasOption(
'uploads' );
128 if ( $this->
hasOption(
'image-base-path' ) ) {
129 $this->imageBasePath = $this->
getOption(
'image-base-path' );
131 if ( $this->
hasOption(
'namespaces' ) ) {
132 $this->setNsfilter( explode(
'|', $this->
getOption(
'namespaces' ) ) );
135 if ( $this->
hasArg( 0 ) ) {
136 $this->importFromFile( $this->
getArg( 0 ) );
138 $this->importFromStdin();
141 $this->
output(
"Done!\n" );
142 $this->
output(
"You might want to run rebuildrecentchanges.php to regenerate RecentChanges,\n" );
143 $this->
output(
"and initSiteStats.php to update page and revision counts\n" );
146 private function setNsfilter( array $namespaces ) {
147 if ( count( $namespaces ) == 0 ) {
148 $this->nsFilter =
false;
152 $this->nsFilter = array_unique( array_map( [ $this,
'getNsIndex' ], $namespaces ) );
155 private function getNsIndex( $namespace ) {
157 $result = $contLang->getNsIndex( $namespace );
158 if ( $result !==
false ) {
161 $ns = intval( $namespace );
162 if ( strval( $ns ) === $namespace && $contLang->getNsText( $ns ) !==
false ) {
165 $this->
fatalError(
"Unknown namespace text / index specified: $namespace" );
172 private function skippedNamespace( $title ) {
173 if ( $title ===
null ) {
178 $ns = $title->getNamespace();
180 return is_array( $this->nsFilter ) && !in_array( $ns, $this->nsFilter );
193 $this->progress(
"Got bogus revision with null title!" );
198 if ( $this->skippedNamespace( $title ) ) {
205 if ( !$this->dryRun ) {
206 call_user_func( $this->importCallback, $rev );
215 if ( $this->uploads ) {
216 if ( $this->skippedNamespace( $revision->
getTitle() ) ) {
219 $this->uploadCount++;
221 $this->progress(
"upload: " . $revision->
getFilename() );
223 if ( !$this->dryRun ) {
227 $statusValue = $importer->import( $revision );
229 return $statusValue->isGood();
240 if ( $this->skippedNamespace( $rev->
getTitle() ) ) {
246 if ( !$this->dryRun ) {
247 call_user_func( $this->logItemCallback, $rev );
251 private function report( $final =
false ) {
252 if ( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
257 private function showReport() {
258 if ( !$this->mQuiet ) {
261 $rate = sprintf(
"%.2f", $this->pageCount / $delta );
262 $revrate = sprintf(
"%.2f", $this->revCount / $delta );
267 # Logs dumps don't have page tallies
268 if ( $this->pageCount ) {
269 $this->progress(
"$this->pageCount ($rate pages/sec $revrate revs/sec)" );
271 $this->progress(
"$this->revCount ($revrate revs/sec)" );
277 private function progress( $string ) {
278 fwrite( $this->stderr, $string .
"\n" );
281 private function importFromFile( $filename ) {
282 if ( preg_match(
'/\.gz$/', $filename ) ) {
283 $filename =
'compress.zlib://' . $filename;
284 } elseif ( preg_match(
'/\.bz2$/', $filename ) ) {
285 $filename =
'compress.bzip2://' . $filename;
286 } elseif ( preg_match(
'/\.7z$/', $filename ) ) {
287 $filename =
'mediawiki.compress.7z://' . $filename;
290 $file = fopen( $filename,
'rt' );
291 if (
$file ===
false ) {
292 $this->
fatalError( error_get_last()[
'message'] ??
'Could not open file' );
295 return $this->importFromHandle(
$file );
298 private function importFromStdin() {
299 $file = fopen(
'php://stdin',
'rt' );
300 if ( self::posix_isatty(
$file ) ) {
304 return $this->importFromHandle(
$file );
307 private function importFromHandle( $handle ) {
308 $this->startTime = microtime(
true );
312 ->getWikiImporterFactory()
316 $importer->disableStatisticsUpdate();
319 $importer->setDebug(
true );
321 if ( $this->
hasOption(
'no-updates' ) ) {
322 $importer->setNoUpdates(
true );
324 if ( $this->
hasOption(
'username-prefix' ) ) {
325 $importer->setUsernamePrefix(
331 $statusRootPage = $importer->setTargetRootPage( $this->
getOption(
'rootpage' ) );
332 if ( !$statusRootPage->isGood() ) {
334 $this->
fatalError( $statusRootPage->getMessage(
false,
false,
'en' )->text() );
338 $nthPage = (int)$this->
getOption(
'skip-to' );
339 $importer->setPageOffset( $nthPage );
340 $this->pageCount = $nthPage - 1;
342 $importer->setPageCallback( [ $this,
'reportPage' ] );
343 $importer->setNoticeCallback(
static function ( $msg, $params ) {
344 echo
wfMessage( $msg, $params )->text() .
"\n";
346 $this->importCallback = $importer->setRevisionCallback(
347 [ $this,
'handleRevision' ] );
348 $this->uploadCallback = $importer->setUploadCallback(
349 [ $this,
'handleUpload' ] );
350 $this->logItemCallback = $importer->setLogItemCallback(
351 [ $this,
'handleLogItem' ] );
352 if ( $this->uploads ) {
353 $importer->setImportUploads(
true );
355 if ( $this->imageBasePath ) {
356 $importer->setImageBasePath( $this->imageBasePath );
359 if ( $this->dryRun ) {
360 $importer->setPageOutCallback(
null );
363 return $importer->doImport();
368 require_once RUN_MAINTENANCE_IF_MAIN;
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Maintenance script that imports XML dump files into the current wiki.
handleRevision(WikiRevision $rev)
string false $imageBasePath
callable null $logItemCallback
callable null $uploadCallback
execute()
Do the actual work.
handleLogItem(WikiRevision $rev)
__construct()
Default constructor.
callable null $importCallback
handleUpload(WikiRevision $revision)
Imports a XML dump from a file (either from file upload, files on disk, or HTTP)
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
output( $out, $channel=null)
Throw some output to the user.
hasArg( $argId=0)
Does a given argument exist?
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
maybeHelp( $force=false)
Maybe show the help.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Represents a revision, log entry or upload during the import process.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.