3 * Import XML dump files into the current wiki.
5 * Copyright © 2005 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
24 * @ingroup Maintenance
27 use MediaWiki\MediaWikiServices
;
29 require_once __DIR__
. '/Maintenance.php';
32 * Maintenance script that imports XML dump files into the current wiki.
34 * @ingroup Maintenance
36 class BackupReader
extends Maintenance
{
37 public $reportingInterval = 100;
38 public $pageCount = 0;
40 public $dryRun = false;
41 public $uploads = false;
42 protected $uploadCount = 0;
43 public $imageBasePath = false;
44 /** @var array|false */
45 public $nsFilter = false;
47 function __construct() {
48 parent
::__construct();
49 $gz = in_array( 'compress.zlib', stream_get_wrappers() )
51 : '(disabled; requires PHP zlib module)';
52 $bz2 = in_array( 'compress.bzip2', stream_get_wrappers() )
54 : '(disabled; requires PHP bzip2 module)';
56 $this->addDescription(
58 This script reads pages from an XML file as produced from Special:Export or
59 dumpBackup.php, and saves them into the current wiki.
61 Compressed XML files may be read directly:
64 .7z (if 7za executable is in PATH)
66 Note that for very large data sets, importDump.php may be slow; there are
67 alternate methods which can be much faster for full site restoration:
68 <https://www.mediawiki.org/wiki/Manual:Importing_XML_dumps>
71 $this->stderr
= fopen( "php://stderr", "wt" );
72 $this->addOption( 'report',
73 'Report position and speed after every n pages processed', false, true );
74 $this->addOption( 'namespaces',
75 'Import only the pages from namespaces belonging to the list of ' .
76 'pipe-separated namespace names or namespace indexes', false, true );
77 $this->addOption( 'rootpage', 'Pages will be imported as subpages of the specified page',
79 $this->addOption( 'dry-run', 'Parse dump without actually importing pages' );
80 $this->addOption( 'debug', 'Output extra verbose debug information' );
81 $this->addOption( 'uploads', 'Process file upload data if included (experimental)' );
84 'Disable link table updates. Is faster but leaves the wiki in an inconsistent state'
86 $this->addOption( 'image-base-path', 'Import files from a specified path', false, true );
87 $this->addOption( 'skip-to', 'Start from nth page by skipping first n-1 pages', false, true );
88 $this->addOption( 'username-prefix', 'Prefix for interwiki usernames', false, true );
89 $this->addOption( 'no-local-users',
90 'Treat all usernames as interwiki. ' .
91 'The default is to assign edits to local users where they exist.',
94 $this->addArg( 'file', 'Dump file to import [else use stdin]', false );
97 public function execute() {
99 $this->fatalError( "Wiki is in read-only mode; you'll need to disable it for import to work." );
102 $this->reportingInterval
= intval( $this->getOption( 'report', 100 ) );
103 if ( !$this->reportingInterval
) {
104 $this->reportingInterval
= 100; // avoid division by zero
107 $this->dryRun
= $this->hasOption( 'dry-run' );
108 $this->uploads
= $this->hasOption( 'uploads' ); // experimental!
109 if ( $this->hasOption( 'image-base-path' ) ) {
110 $this->imageBasePath
= $this->getOption( 'image-base-path' );
112 if ( $this->hasOption( 'namespaces' ) ) {
113 $this->setNsfilter( explode( '|', $this->getOption( 'namespaces' ) ) );
116 if ( $this->hasArg( 0 ) ) {
117 $this->importFromFile( $this->getArg( 0 ) );
119 $this->importFromStdin();
122 $this->output( "Done!\n" );
123 $this->output( "You might want to run rebuildrecentchanges.php to regenerate RecentChanges,\n" );
124 $this->output( "and initSiteStats.php to update page and revision counts\n" );
127 function setNsfilter( array $namespaces ) {
128 if ( count( $namespaces ) == 0 ) {
129 $this->nsFilter
= false;
133 $this->nsFilter
= array_unique( array_map( [ $this, 'getNsIndex' ], $namespaces ) );
136 private function getNsIndex( $namespace ) {
137 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
138 $result = $contLang->getNsIndex( $namespace );
139 if ( $result !== false ) {
142 $ns = intval( $namespace );
143 if ( strval( $ns ) === $namespace && $contLang->getNsText( $ns ) !== false ) {
146 $this->fatalError( "Unknown namespace text / index specified: $namespace" );
150 * @param Title|Revision $obj
151 * @throws MWException
154 private function skippedNamespace( $obj ) {
156 if ( $obj instanceof Title
) {
158 } elseif ( $obj instanceof Revision
) {
159 $title = $obj->getTitle();
160 } elseif ( $obj instanceof WikiRevision
) {
161 $title = $obj->title
;
163 throw new MWException( "Cannot get namespace of object in " . __METHOD__
);
166 if ( is_null( $title ) ) {
167 // Probably a log entry
171 $ns = $title->getNamespace();
173 return is_array( $this->nsFilter
) && !in_array( $ns, $this->nsFilter
);
176 function reportPage( $page ) {
181 * @param Revision $rev
183 function handleRevision( $rev ) {
184 $title = $rev->getTitle();
186 $this->progress( "Got bogus revision with null title!" );
191 if ( $this->skippedNamespace( $title ) ) {
198 if ( !$this->dryRun
) {
199 call_user_func( $this->importCallback
, $rev );
204 * @param Revision $revision
207 function handleUpload( $revision ) {
208 if ( $this->uploads
) {
209 if ( $this->skippedNamespace( $revision ) ) {
212 $this->uploadCount++
;
214 $this->progress( "upload: " . $revision->getFilename() );
216 if ( !$this->dryRun
) {
218 // call_user_func( $this->uploadCallback, $revision );
219 $dbw = $this->getDB( DB_MASTER
);
221 return $dbw->deadlockLoop( [ $revision, 'importUpload' ] );
228 function handleLogItem( $rev ) {
229 if ( $this->skippedNamespace( $rev ) ) {
235 if ( !$this->dryRun
) {
236 call_user_func( $this->logItemCallback
, $rev );
240 function report( $final = false ) {
241 if ( $final xor ( $this->pageCount %
$this->reportingInterval
== 0 ) ) {
246 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)" );
266 function progress( $string ) {
267 fwrite( $this->stderr
, $string . "\n" );
270 function importFromFile( $filename ) {
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' );
281 return $this->importFromHandle( $file );
284 function importFromStdin() {
285 $file = fopen( 'php://stdin', 'rt' );
286 if ( self
::posix_isatty( $file ) ) {
287 $this->maybeHelp( true );
290 return $this->importFromHandle( $file );
293 function importFromHandle( $handle ) {
294 $this->startTime
= microtime( true );
296 $source = new ImportStreamSource( $handle );
297 $importer = new WikiImporter( $source, $this->getConfig() );
299 // Updating statistics require a lot of time so disable it
300 $importer->disableStatisticsUpdate();
302 if ( $this->hasOption( 'debug' ) ) {
303 $importer->setDebug( true );
305 if ( $this->hasOption( 'no-updates' ) ) {
306 $importer->setNoUpdates( true );
308 if ( $this->hasOption( 'username-prefix' ) ) {
309 $importer->setUsernamePrefix(
310 $this->getOption( 'username-prefix' ),
311 !$this->hasOption( 'no-local-users' )
314 if ( $this->hasOption( 'rootpage' ) ) {
315 $statusRootPage = $importer->setTargetRootPage( $this->getOption( 'rootpage' ) );
316 if ( !$statusRootPage->isGood() ) {
317 // Die here so that it doesn't print "Done!"
318 $this->fatalError( $statusRootPage->getMessage()->text() );
322 if ( $this->hasOption( 'skip-to' ) ) {
323 $nthPage = (int)$this->getOption( 'skip-to' );
324 $importer->setPageOffset( $nthPage );
325 $this->pageCount
= $nthPage - 1;
327 $importer->setPageCallback( [ $this, 'reportPage' ] );
328 $importer->setNoticeCallback( function ( $msg, $params ) {
329 echo wfMessage( $msg, $params )->text() . "\n";
331 $this->importCallback
= $importer->setRevisionCallback(
332 [ $this, 'handleRevision' ] );
333 $this->uploadCallback
= $importer->setUploadCallback(
334 [ $this, 'handleUpload' ] );
335 $this->logItemCallback
= $importer->setLogItemCallback(
336 [ $this, 'handleLogItem' ] );
337 if ( $this->uploads
) {
338 $importer->setImportUploads( true );
340 if ( $this->imageBasePath
) {
341 $importer->setImageBasePath( $this->imageBasePath
);
344 if ( $this->dryRun
) {
345 $importer->setPageOutCallback( null );
348 return $importer->doImport();
352 $maintClass = BackupReader
::class;
353 require_once RUN_MAINTENANCE_IF_MAIN
;