66 parent::__construct();
67 $gz = in_array(
'compress.zlib', stream_get_wrappers() )
69 :
'(disabled; requires PHP zlib module)';
70 $bz2 = in_array(
'compress.bzip2', stream_get_wrappers() )
72 :
'(disabled; requires PHP bzip2 module)';
76This script reads pages from an XML file as produced from Special:Export or
77dumpBackup.php, and saves them into the current wiki.
79Compressed XML files may be read directly:
82 .7z (
if 7za executable is in PATH)
84Note that
for very large data sets, importDump.php may be slow; there are
85alternate methods which can be much faster
for full site restoration:
89 $this->stderr = fopen(
"php://stderr",
"wt" );
91 'Report position and speed after every n pages processed',
false,
true );
93 'Import only the pages from namespaces belonging to the list of ' .
94 'pipe-separated namespace names or namespace indexes',
false,
true );
95 $this->
addOption(
'rootpage',
'Pages will be imported as subpages of the specified page',
97 $this->
addOption(
'dry-run',
'Parse dump without actually importing pages' );
98 $this->
addOption(
'debug',
'Output extra verbose debug information' );
99 $this->
addOption(
'uploads',
'Process file upload data if included (experimental)' );
102 'Disable link table updates. Is faster but leaves the wiki in an inconsistent state'
104 $this->
addOption(
'image-base-path',
'Import files from a specified path',
false,
true );
105 $this->
addOption(
'skip-to',
'Start from nth page by skipping first n-1 pages',
false,
true );
107 'Prefix for interwiki usernames; a trailing ">" will be added. Default: "imported>"',
110 'Treat all usernames as interwiki. ' .
111 'The default is to assign edits to local users where they exist.',
114 $this->
addArg(
'file',
'Dump file to import [else use stdin]',
false );
118 if ( MediaWikiServices::getInstance()->getReadOnlyMode()->isReadOnly() ) {
119 $this->
fatalError(
"Wiki is in read-only mode; you'll need to disable it for import to work." );
122 $this->reportingInterval = intval( $this->
getOption(
'report', 100 ) );
123 if ( !$this->reportingInterval ) {
125 $this->reportingInterval = 100;
128 $this->dryRun = $this->
hasOption(
'dry-run' );
129 $this->uploads = $this->
hasOption(
'uploads' );
131 if ( $this->
hasOption(
'image-base-path' ) ) {
132 $this->imageBasePath = $this->
getOption(
'image-base-path' );
134 if ( $this->
hasOption(
'namespaces' ) ) {
135 $this->setNsfilter( explode(
'|', $this->
getOption(
'namespaces' ) ) );
138 if ( $this->
hasArg( 0 ) ) {
139 $this->importFromFile( $this->
getArg( 0 ) );
141 $this->importFromStdin();
144 $this->
output(
"Done!\n" );
145 $this->
output(
"You might want to run rebuildrecentchanges.php to regenerate RecentChanges,\n" );
146 $this->
output(
"and initSiteStats.php to update page and revision counts\n" );
149 private function setNsfilter( array $namespaces ) {
150 if ( count( $namespaces ) == 0 ) {
151 $this->nsFilter =
false;
155 $this->nsFilter = array_unique( array_map( [ $this,
'getNsIndex' ], $namespaces ) );
158 private function getNsIndex( $namespace ) {
159 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
160 $result = $contLang->getNsIndex( $namespace );
161 if ( $result !==
false ) {
164 $ns = intval( $namespace );
165 if ( strval( $ns ) === $namespace && $contLang->getNsText( $ns ) !==
false ) {
168 $this->
fatalError(
"Unknown namespace text / index specified: $namespace" );
176 private function skippedNamespace(
$title ) {
182 $ns =
$title->getNamespace();
184 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 call_user_func( $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 ) {
232 return $dbw->deadlockLoop( [ $revision,
'importUpload' ] );
243 if ( $this->skippedNamespace( $rev->
getTitle() ) ) {
249 if ( !$this->dryRun ) {
250 call_user_func( $this->logItemCallback, $rev );
254 private function report( $final =
false ) {
255 if ( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
260 private function showReport() {
261 if ( !$this->mQuiet ) {
264 $rate = sprintf(
"%.2f", $this->pageCount / $delta );
265 $revrate = sprintf(
"%.2f", $this->revCount / $delta );
270 # Logs dumps don't have page tallies
271 if ( $this->pageCount ) {
272 $this->progress(
"$this->pageCount ($rate pages/sec $revrate revs/sec)" );
274 $this->progress(
"$this->revCount ($revrate revs/sec)" );
277 MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->waitForReplication();
280 private function progress( $string ) {
281 fwrite( $this->stderr, $string .
"\n" );
284 private function importFromFile( $filename ) {
285 if ( preg_match(
'/\.gz$/', $filename ) ) {
286 $filename =
'compress.zlib://' . $filename;
287 } elseif ( preg_match(
'/\.bz2$/', $filename ) ) {
288 $filename =
'compress.bzip2://' . $filename;
289 } elseif ( preg_match(
'/\.7z$/', $filename ) ) {
290 $filename =
'mediawiki.compress.7z://' . $filename;
293 $file = fopen( $filename,
'rt' );
294 if (
$file ===
false ) {
295 $this->
fatalError( error_get_last()[
'message'] ??
'Could not open file' );
298 return $this->importFromHandle(
$file );
301 private function importFromStdin() {
302 $file = fopen(
'php://stdin',
'rt' );
303 if ( self::posix_isatty(
$file ) ) {
307 return $this->importFromHandle(
$file );
310 private function importFromHandle( $handle ) {
311 $this->startTime = microtime(
true );
314 $importer = MediaWikiServices::getInstance()
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() ) {
335 $this->
fatalError( $statusRootPage->getMessage(
false,
false,
'en' )->text() );
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 ) {
345 echo
wfMessage( $msg, $params )->text() .
"\n";
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();
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.
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.