30require_once __DIR__ .
'/Maintenance.php';
59 parent::__construct();
60 $gz = in_array(
'compress.zlib', stream_get_wrappers() )
62 :
'(disabled; requires PHP zlib module)';
63 $bz2 = in_array(
'compress.bzip2', stream_get_wrappers() )
65 :
'(disabled; requires PHP bzip2 module)';
69This script reads pages from an XML file as produced from Special:Export or
70dumpBackup.php, and saves them into the current wiki.
72Compressed XML files may be read directly:
75 .7z (
if 7za executable is in PATH)
77Note that
for very large data sets, importDump.php may be slow; there are
78alternate methods which can be much faster
for full site restoration:
82 $this->stderr = fopen(
"php://stderr",
"wt" );
84 'Report position and speed after every n pages processed',
false,
true );
86 'Import only the pages from namespaces belonging to the list of ' .
87 'pipe-separated namespace names or namespace indexes',
false,
true );
88 $this->
addOption(
'rootpage',
'Pages will be imported as subpages of the specified page',
90 $this->
addOption(
'dry-run',
'Parse dump without actually importing pages' );
91 $this->
addOption(
'debug',
'Output extra verbose debug information' );
92 $this->
addOption(
'uploads',
'Process file upload data if included (experimental)' );
95 'Disable link table updates. Is faster but leaves the wiki in an inconsistent state'
97 $this->
addOption(
'image-base-path',
'Import files from a specified path',
false,
true );
98 $this->
addOption(
'skip-to',
'Start from nth page by skipping first n-1 pages',
false,
true );
99 $this->
addOption(
'username-prefix',
'Prefix for interwiki usernames',
false,
true );
101 'Treat all usernames as interwiki. ' .
102 'The default is to assign edits to local users where they exist.',
105 $this->
addArg(
'file',
'Dump file to import [else use stdin]',
false );
110 $this->
fatalError(
"Wiki is in read-only mode; you'll need to disable it for import to work." );
113 $this->reportingInterval = intval( $this->
getOption(
'report', 100 ) );
114 if ( !$this->reportingInterval ) {
116 $this->reportingInterval = 100;
119 $this->dryRun = $this->
hasOption(
'dry-run' );
120 $this->uploads = $this->
hasOption(
'uploads' );
122 if ( $this->
hasOption(
'image-base-path' ) ) {
123 $this->imageBasePath = $this->
getOption(
'image-base-path' );
125 if ( $this->
hasOption(
'namespaces' ) ) {
129 if ( $this->
hasArg( 0 ) ) {
135 $this->
output(
"Done!\n" );
136 $this->
output(
"You might want to run rebuildrecentchanges.php to regenerate RecentChanges,\n" );
137 $this->
output(
"and initSiteStats.php to update page and revision counts\n" );
141 if ( count( $namespaces ) == 0 ) {
142 $this->nsFilter =
false;
146 $this->nsFilter = array_unique( array_map( [ $this,
'getNsIndex' ], $namespaces ) );
150 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
151 $result = $contLang->getNsIndex( $namespace );
152 if ( $result !==
false ) {
155 $ns = intval( $namespace );
156 if ( strval( $ns ) === $namespace && $contLang->getNsText( $ns ) !==
false ) {
159 $this->
fatalError(
"Unknown namespace text / index specified: $namespace" );
173 $ns =
$title->getNamespace();
175 return is_array( $this->nsFilter ) && !in_array( $ns, $this->nsFilter );
188 $this->
progress(
"Got bogus revision with null title!" );
200 if ( !$this->dryRun ) {
201 call_user_func( $this->importCallback, $rev );
210 if ( $this->uploads ) {
214 $this->uploadCount++;
218 if ( !$this->dryRun ) {
223 return $dbw->deadlockLoop( [ $revision,
'importUpload' ] );
240 if ( !$this->dryRun ) {
241 call_user_func( $this->logItemCallback, $rev );
245 private function report( $final =
false ) {
246 if ( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
252 if ( !$this->mQuiet ) {
255 $rate = sprintf(
"%.2f", $this->pageCount / $delta );
256 $revrate = sprintf(
"%.2f", $this->revCount / $delta );
261 # Logs dumps don't have page tallies
262 if ( $this->pageCount ) {
263 $this->
progress(
"$this->pageCount ($rate pages/sec $revrate revs/sec)" );
265 $this->
progress(
"$this->revCount ($revrate revs/sec)" );
268 MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->waitForReplication();
272 fwrite( $this->stderr, $string .
"\n" );
276 if ( preg_match(
'/\.gz$/', $filename ) ) {
277 $filename =
'compress.zlib://' . $filename;
278 } elseif ( preg_match(
'/\.bz2$/', $filename ) ) {
279 $filename =
'compress.bzip2://' . $filename;
280 } elseif ( preg_match(
'/\.7z$/', $filename ) ) {
281 $filename =
'mediawiki.compress.7z://' . $filename;
284 $file = fopen( $filename,
'rt' );
290 $file = fopen(
'php://stdin',
'rt' );
291 if ( self::posix_isatty(
$file ) ) {
299 $this->startTime = microtime(
true );
305 $importer->disableStatisticsUpdate();
308 $importer->setDebug(
true );
310 if ( $this->
hasOption(
'no-updates' ) ) {
311 $importer->setNoUpdates(
true );
313 if ( $this->
hasOption(
'username-prefix' ) ) {
314 $importer->setUsernamePrefix(
320 $statusRootPage = $importer->setTargetRootPage( $this->
getOption(
'rootpage' ) );
321 if ( !$statusRootPage->isGood() ) {
323 $this->
fatalError( $statusRootPage->getMessage(
false,
false,
'en' )->text() );
328 $nthPage = (int)$this->
getOption(
'skip-to' );
329 $importer->setPageOffset( $nthPage );
330 $this->pageCount = $nthPage - 1;
332 $importer->setPageCallback( [ $this,
'reportPage' ] );
333 $importer->setNoticeCallback(
function ( $msg, $params ) {
334 echo
wfMessage( $msg, $params )->text() .
"\n";
336 $this->importCallback = $importer->setRevisionCallback(
337 [ $this,
'handleRevision' ] );
338 $this->uploadCallback = $importer->setUploadCallback(
339 [ $this,
'handleUpload' ] );
340 $this->logItemCallback = $importer->setLogItemCallback(
341 [ $this,
'handleLogItem' ] );
342 if ( $this->uploads ) {
343 $importer->setImportUploads(
true );
345 if ( $this->imageBasePath ) {
346 $importer->setImageBasePath( $this->imageBasePath );
349 if ( $this->dryRun ) {
350 $importer->setPageOutCallback(
null );
353 return $importer->doImport();
wfReadOnly()
Check whether the wiki is in read-only mode.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
const RUN_MAINTENANCE_IF_MAIN
Maintenance script that imports XML dump files into the current wiki.
handleRevision(WikiRevision $rev)
importFromFile( $filename)
callable null $logItemCallback
callable null $uploadCallback
execute()
Do the actual work.
handleLogItem(WikiRevision $rev)
skippedNamespace( $title)
__construct()
Default constructor.
importFromHandle( $handle)
setNsfilter(array $namespaces)
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)
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.
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.
XML file reader for the page data importer.
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.