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
142 * @throws MWException
145 private function skippedNamespace( $obj ) {
147 if ( $obj instanceof Title
) {
149 } elseif ( $obj instanceof Revision
) {
150 $title = $obj->getTitle();
151 } elseif ( $obj instanceof WikiRevision
) {
152 $title = $obj->title
;
154 throw new MWException( "Cannot get namespace of object in " . __METHOD__
);
157 if ( is_null( $title ) ) {
158 // Probably a log entry
162 $ns = $title->getNamespace();
164 return is_array( $this->nsFilter
) && !in_array( $ns, $this->nsFilter
);
167 function reportPage( $page ) {
172 * @param Revision $rev
174 function handleRevision( $rev ) {
175 $title = $rev->getTitle();
177 $this->progress( "Got bogus revision with null title!" );
182 if ( $this->skippedNamespace( $title ) ) {
189 if ( !$this->dryRun
) {
190 call_user_func( $this->importCallback
, $rev );
195 * @param Revision $revision
198 function handleUpload( $revision ) {
199 if ( $this->uploads
) {
200 if ( $this->skippedNamespace( $revision ) ) {
203 $this->uploadCount++
;
205 $this->progress( "upload: " . $revision->getFilename() );
207 if ( !$this->dryRun
) {
209 // call_user_func( $this->uploadCallback, $revision );
210 $dbw = $this->getDB( DB_MASTER
);
212 return $dbw->deadlockLoop( [ $revision, 'importUpload' ] );
219 function handleLogItem( $rev ) {
220 if ( $this->skippedNamespace( $rev ) ) {
226 if ( !$this->dryRun
) {
227 call_user_func( $this->logItemCallback
, $rev );
231 function report( $final = false ) {
232 if ( $final xor ( $this->pageCount %
$this->reportingInterval
== 0 ) ) {
237 function showReport() {
238 if ( !$this->mQuiet
) {
239 $delta = microtime( true ) - $this->startTime
;
241 $rate = sprintf( "%.2f", $this->pageCount
/ $delta );
242 $revrate = sprintf( "%.2f", $this->revCount
/ $delta );
247 # Logs dumps don't have page tallies
248 if ( $this->pageCount
) {
249 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
251 $this->progress( "$this->revCount ($revrate revs/sec)" );
257 function progress( $string ) {
258 fwrite( $this->stderr
, $string . "\n" );
261 function importFromFile( $filename ) {
262 if ( preg_match( '/\.gz$/', $filename ) ) {
263 $filename = 'compress.zlib://' . $filename;
264 } elseif ( preg_match( '/\.bz2$/', $filename ) ) {
265 $filename = 'compress.bzip2://' . $filename;
266 } elseif ( preg_match( '/\.7z$/', $filename ) ) {
267 $filename = 'mediawiki.compress.7z://' . $filename;
270 $file = fopen( $filename, 'rt' );
272 return $this->importFromHandle( $file );
275 function importFromStdin() {
276 $file = fopen( 'php://stdin', 'rt' );
277 if ( self
::posix_isatty( $file ) ) {
278 $this->maybeHelp( true );
281 return $this->importFromHandle( $file );
284 function importFromHandle( $handle ) {
285 $this->startTime
= microtime( true );
287 $source = new ImportStreamSource( $handle );
288 $importer = new WikiImporter( $source, $this->getConfig() );
290 // Updating statistics require a lot of time so disable it
291 $importer->disableStatisticsUpdate();
293 if ( $this->hasOption( 'debug' ) ) {
294 $importer->setDebug( true );
296 if ( $this->hasOption( 'no-updates' ) ) {
297 $importer->setNoUpdates( true );
299 if ( $this->hasOption( 'rootpage' ) ) {
300 $statusRootPage = $importer->setTargetRootPage( $this->getOption( 'rootpage' ) );
301 if ( !$statusRootPage->isGood() ) {
302 // Die here so that it doesn't print "Done!"
303 $this->error( $statusRootPage->getMessage()->text(), 1 );
307 if ( $this->hasOption( 'skip-to' ) ) {
308 $nthPage = (int)$this->getOption( 'skip-to' );
309 $importer->setPageOffset( $nthPage );
310 $this->pageCount
= $nthPage - 1;
312 $importer->setPageCallback( [ $this, 'reportPage' ] );
313 $this->importCallback
= $importer->setRevisionCallback(
314 [ $this, 'handleRevision' ] );
315 $this->uploadCallback
= $importer->setUploadCallback(
316 [ $this, 'handleUpload' ] );
317 $this->logItemCallback
= $importer->setLogItemCallback(
318 [ $this, 'handleLogItem' ] );
319 if ( $this->uploads
) {
320 $importer->setImportUploads( true );
322 if ( $this->imageBasePath
) {
323 $importer->setImageBasePath( $this->imageBasePath
);
326 if ( $this->dryRun
) {
327 $importer->setPageOutCallback( null );
330 return $importer->doImport();
334 $maintClass = 'BackupReader';
335 require_once RUN_MAINTENANCE_IF_MAIN
;