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 require_once __DIR__
. '/Maintenance.php';
30 * Maintenance script that imports XML dump files into the current wiki.
32 * @ingroup Maintenance
34 class BackupReader
extends Maintenance
{
35 public $reportingInterval = 100;
36 public $pageCount = 0;
38 public $dryRun = false;
39 public $uploads = false;
40 protected $uploadCount = 0;
41 public $imageBasePath = false;
42 public $nsFilter = false;
44 function __construct() {
45 parent
::__construct();
46 $gz = in_array( 'compress.zlib', stream_get_wrappers() )
48 : '(disabled; requires PHP zlib module)';
49 $bz2 = in_array( 'compress.bzip2', stream_get_wrappers() )
51 : '(disabled; requires PHP bzip2 module)';
53 $this->addDescription(
55 This script reads pages from an XML file as produced from Special:Export or
56 dumpBackup.php, and saves them into the current wiki.
58 Compressed XML files may be read directly:
61 .7z (if 7za executable is in PATH)
63 Note that for very large data sets, importDump.php may be slow; there are
64 alternate methods which can be much faster for full site restoration:
65 <https://www.mediawiki.org/wiki/Manual:Importing_XML_dumps>
68 $this->stderr
= fopen( "php://stderr", "wt" );
69 $this->addOption( 'report',
70 'Report position and speed after every n pages processed', false, true );
71 $this->addOption( 'namespaces',
72 'Import only the pages from namespaces belonging to the list of ' .
73 'pipe-separated namespace names or namespace indexes', false, true );
74 $this->addOption( 'rootpage', 'Pages will be imported as subpages of the specified page',
76 $this->addOption( 'dry-run', 'Parse dump without actually importing pages' );
77 $this->addOption( 'debug', 'Output extra verbose debug information' );
78 $this->addOption( 'uploads', 'Process file upload data if included (experimental)' );
81 'Disable link table updates. Is faster but leaves the wiki in an inconsistent state'
83 $this->addOption( 'image-base-path', 'Import files from a specified path', false, true );
84 $this->addOption( 'skip-to', 'Start from nth page by skipping first n-1 pages', false, true );
85 $this->addArg( 'file', 'Dump file to import [else use stdin]', false );
88 public function execute() {
90 $this->error( "Wiki is in read-only mode; you'll need to disable it for import to work.", true );
93 $this->reportingInterval
= intval( $this->getOption( 'report', 100 ) );
94 if ( !$this->reportingInterval
) {
95 $this->reportingInterval
= 100; // avoid division by zero
98 $this->dryRun
= $this->hasOption( 'dry-run' );
99 $this->uploads
= $this->hasOption( 'uploads' ); // experimental!
100 if ( $this->hasOption( 'image-base-path' ) ) {
101 $this->imageBasePath
= $this->getOption( 'image-base-path' );
103 if ( $this->hasOption( 'namespaces' ) ) {
104 $this->setNsfilter( explode( '|', $this->getOption( 'namespaces' ) ) );
107 if ( $this->hasArg() ) {
108 $this->importFromFile( $this->getArg() );
110 $this->importFromStdin();
113 $this->output( "Done!\n" );
114 $this->output( "You might want to run rebuildrecentchanges.php to regenerate RecentChanges,\n" );
115 $this->output( "and initSiteStats.php to update page and revision counts\n" );
118 function setNsfilter( array $namespaces ) {
119 if ( count( $namespaces ) == 0 ) {
120 $this->nsFilter
= false;
124 $this->nsFilter
= array_unique( array_map( [ $this, 'getNsIndex' ], $namespaces ) );
127 private function getNsIndex( $namespace ) {
129 $result = $wgContLang->getNsIndex( $namespace );
130 if ( $result !== false ) {
133 $ns = intval( $namespace );
134 if ( strval( $ns ) === $namespace && $wgContLang->getNsText( $ns ) !== false ) {
137 $this->error( "Unknown namespace text / index specified: $namespace", true );
141 * @param Title|Revision $obj
144 private function skippedNamespace( $obj ) {
146 if ( $obj instanceof Title
) {
148 } elseif ( $obj instanceof Revision
) {
149 $title = $obj->getTitle();
150 } elseif ( $obj instanceof WikiRevision
) {
151 $title = $obj->title
;
153 throw new MWException( "Cannot get namespace of object in " . __METHOD__
);
156 if ( is_null( $title ) ) {
157 // Probably a log entry
161 $ns = $title->getNamespace();
163 return is_array( $this->nsFilter
) && !in_array( $ns, $this->nsFilter
);
166 function reportPage( $page ) {
171 * @param Revision $rev
173 function handleRevision( $rev ) {
174 $title = $rev->getTitle();
176 $this->progress( "Got bogus revision with null title!" );
181 if ( $this->skippedNamespace( $title ) ) {
188 if ( !$this->dryRun
) {
189 call_user_func( $this->importCallback
, $rev );
194 * @param Revision $revision
197 function handleUpload( $revision ) {
198 if ( $this->uploads
) {
199 if ( $this->skippedNamespace( $revision ) ) {
202 $this->uploadCount++
;
204 $this->progress( "upload: " . $revision->getFilename() );
206 if ( !$this->dryRun
) {
208 // call_user_func( $this->uploadCallback, $revision );
209 $dbw = $this->getDB( DB_MASTER
);
211 return $dbw->deadlockLoop( [ $revision, 'importUpload' ] );
218 function handleLogItem( $rev ) {
219 if ( $this->skippedNamespace( $rev ) ) {
225 if ( !$this->dryRun
) {
226 call_user_func( $this->logItemCallback
, $rev );
230 function report( $final = false ) {
231 if ( $final xor ( $this->pageCount %
$this->reportingInterval
== 0 ) ) {
236 function showReport() {
237 if ( !$this->mQuiet
) {
238 $delta = microtime( true ) - $this->startTime
;
240 $rate = sprintf( "%.2f", $this->pageCount
/ $delta );
241 $revrate = sprintf( "%.2f", $this->revCount
/ $delta );
246 # Logs dumps don't have page tallies
247 if ( $this->pageCount
) {
248 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
250 $this->progress( "$this->revCount ($revrate revs/sec)" );
256 function progress( $string ) {
257 fwrite( $this->stderr
, $string . "\n" );
260 function importFromFile( $filename ) {
261 if ( preg_match( '/\.gz$/', $filename ) ) {
262 $filename = 'compress.zlib://' . $filename;
263 } elseif ( preg_match( '/\.bz2$/', $filename ) ) {
264 $filename = 'compress.bzip2://' . $filename;
265 } elseif ( preg_match( '/\.7z$/', $filename ) ) {
266 $filename = 'mediawiki.compress.7z://' . $filename;
269 $file = fopen( $filename, 'rt' );
271 return $this->importFromHandle( $file );
274 function importFromStdin() {
275 $file = fopen( 'php://stdin', 'rt' );
276 if ( self
::posix_isatty( $file ) ) {
277 $this->maybeHelp( true );
280 return $this->importFromHandle( $file );
283 function importFromHandle( $handle ) {
284 $this->startTime
= microtime( true );
286 $source = new ImportStreamSource( $handle );
287 $importer = new WikiImporter( $source, $this->getConfig() );
289 // Updating statistics require a lot of time so disable it
290 $importer->disableStatisticsUpdate();
292 if ( $this->hasOption( 'debug' ) ) {
293 $importer->setDebug( true );
295 if ( $this->hasOption( 'no-updates' ) ) {
296 $importer->setNoUpdates( true );
298 if ( $this->hasOption( 'rootpage' ) ) {
299 $statusRootPage = $importer->setTargetRootPage( $this->getOption( 'rootpage' ) );
300 if ( !$statusRootPage->isGood() ) {
301 // Die here so that it doesn't print "Done!"
302 $this->error( $statusRootPage->getMessage()->text(), 1 );
306 if ( $this->hasOption( 'skip-to' ) ) {
307 $nthPage = (int)$this->getOption( 'skip-to' );
308 $importer->setPageOffset( $nthPage );
309 $this->pageCount
= $nthPage - 1;
311 $importer->setPageCallback( [ $this, 'reportPage' ] );
312 $this->importCallback
= $importer->setRevisionCallback(
313 [ $this, 'handleRevision' ] );
314 $this->uploadCallback
= $importer->setUploadCallback(
315 [ $this, 'handleUpload' ] );
316 $this->logItemCallback
= $importer->setLogItemCallback(
317 [ $this, 'handleLogItem' ] );
318 if ( $this->uploads
) {
319 $importer->setImportUploads( true );
321 if ( $this->imageBasePath
) {
322 $importer->setImageBasePath( $this->imageBasePath
);
325 if ( $this->dryRun
) {
326 $importer->setPageOutCallback( null );
329 return $importer->doImport();
333 $maintClass = 'BackupReader';
334 require_once RUN_MAINTENANCE_IF_MAIN
;