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 public $imageBasePath = false;
41 public $nsFilter = false;
43 function __construct() {
44 parent
::__construct();
45 $gz = in_array( 'compress.zlib', stream_get_wrappers() )
47 : '(disabled; requires PHP zlib module)';
48 $bz2 = in_array( 'compress.bzip2', stream_get_wrappers() )
50 : '(disabled; requires PHP bzip2 module)';
52 $this->addDescription(
54 This script reads pages from an XML file as produced from Special:Export or
55 dumpBackup.php, and saves them into the current wiki.
57 Compressed XML files may be read directly:
60 .7z (if 7za executable is in PATH)
62 Note that for very large data sets, importDump.php may be slow; there are
63 alternate methods which can be much faster for full site restoration:
64 <https://www.mediawiki.org/wiki/Manual:Importing_XML_dumps>
67 $this->stderr
= fopen( "php://stderr", "wt" );
68 $this->addOption( 'report',
69 'Report position and speed after every n pages processed', false, true );
70 $this->addOption( 'namespaces',
71 'Import only the pages from namespaces belonging to the list of ' .
72 'pipe-separated namespace names or namespace indexes', false, true );
73 $this->addOption( 'rootpage', 'Pages will be imported as subpages of the specified page',
75 $this->addOption( 'dry-run', 'Parse dump without actually importing pages' );
76 $this->addOption( 'debug', 'Output extra verbose debug information' );
77 $this->addOption( 'uploads', 'Process file upload data if included (experimental)' );
80 'Disable link table updates. Is faster but leaves the wiki in an inconsistent state'
82 $this->addOption( 'image-base-path', 'Import files from a specified path', false, true );
83 $this->addArg( 'file', 'Dump file to import [else use stdin]', false );
86 public function execute() {
88 $this->error( "Wiki is in read-only mode; you'll need to disable it for import to work.", true );
91 $this->reportingInterval
= intval( $this->getOption( 'report', 100 ) );
92 if ( !$this->reportingInterval
) {
93 $this->reportingInterval
= 100; // avoid division by zero
96 $this->dryRun
= $this->hasOption( 'dry-run' );
97 $this->uploads
= $this->hasOption( 'uploads' ); // experimental!
98 if ( $this->hasOption( 'image-base-path' ) ) {
99 $this->imageBasePath
= $this->getOption( 'image-base-path' );
101 if ( $this->hasOption( 'namespaces' ) ) {
102 $this->setNsfilter( explode( '|', $this->getOption( 'namespaces' ) ) );
105 if ( $this->hasArg() ) {
106 $this->importFromFile( $this->getArg() );
108 $this->importFromStdin();
111 $this->output( "Done!\n" );
112 $this->output( "You might want to run rebuildrecentchanges.php to regenerate RecentChanges,\n" );
113 $this->output( "and initSiteStats.php to update page and revision counts\n" );
116 function setNsfilter( array $namespaces ) {
117 if ( count( $namespaces ) == 0 ) {
118 $this->nsFilter
= false;
122 $this->nsFilter
= array_unique( array_map( [ $this, 'getNsIndex' ], $namespaces ) );
125 private function getNsIndex( $namespace ) {
127 $result = $wgContLang->getNsIndex( $namespace );
128 if ( $result !== false ) {
131 $ns = intval( $namespace );
132 if ( strval( $ns ) === $namespace && $wgContLang->getNsText( $ns ) !== false ) {
135 $this->error( "Unknown namespace text / index specified: $namespace", true );
139 * @param Title|Revision $obj
142 private function skippedNamespace( $obj ) {
144 if ( $obj instanceof Title
) {
146 } elseif ( $obj instanceof Revision
) {
147 $title = $obj->getTitle();
148 } elseif ( $obj instanceof WikiRevision
) {
149 $title = $obj->title
;
151 throw new MWException( "Cannot get namespace of object in " . __METHOD__
);
154 if ( is_null( $title ) ) {
155 // Probably a log entry
159 $ns = $title->getNamespace();
161 return is_array( $this->nsFilter
) && !in_array( $ns, $this->nsFilter
);
164 function reportPage( $page ) {
169 * @param Revision $rev
171 function handleRevision( $rev ) {
172 $title = $rev->getTitle();
174 $this->progress( "Got bogus revision with null title!" );
179 if ( $this->skippedNamespace( $title ) ) {
186 if ( !$this->dryRun
) {
187 call_user_func( $this->importCallback
, $rev );
192 * @param Revision $revision
195 function handleUpload( $revision ) {
196 if ( $this->uploads
) {
197 if ( $this->skippedNamespace( $revision ) ) {
200 $this->uploadCount++
;
202 $this->progress( "upload: " . $revision->getFilename() );
204 if ( !$this->dryRun
) {
206 // call_user_func( $this->uploadCallback, $revision );
207 $dbw = $this->getDB( DB_MASTER
);
209 return $dbw->deadlockLoop( [ $revision, 'importUpload' ] );
216 function handleLogItem( $rev ) {
217 if ( $this->skippedNamespace( $rev ) ) {
223 if ( !$this->dryRun
) {
224 call_user_func( $this->logItemCallback
, $rev );
228 function report( $final = false ) {
229 if ( $final xor ( $this->pageCount %
$this->reportingInterval
== 0 ) ) {
234 function showReport() {
235 if ( !$this->mQuiet
) {
236 $delta = microtime( true ) - $this->startTime
;
238 $rate = sprintf( "%.2f", $this->pageCount
/ $delta );
239 $revrate = sprintf( "%.2f", $this->revCount
/ $delta );
244 # Logs dumps don't have page tallies
245 if ( $this->pageCount
) {
246 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
248 $this->progress( "$this->revCount ($revrate revs/sec)" );
254 function progress( $string ) {
255 fwrite( $this->stderr
, $string . "\n" );
258 function importFromFile( $filename ) {
259 if ( preg_match( '/\.gz$/', $filename ) ) {
260 $filename = 'compress.zlib://' . $filename;
261 } elseif ( preg_match( '/\.bz2$/', $filename ) ) {
262 $filename = 'compress.bzip2://' . $filename;
263 } elseif ( preg_match( '/\.7z$/', $filename ) ) {
264 $filename = 'mediawiki.compress.7z://' . $filename;
267 $file = fopen( $filename, 'rt' );
269 return $this->importFromHandle( $file );
272 function importFromStdin() {
273 $file = fopen( 'php://stdin', 'rt' );
274 if ( self
::posix_isatty( $file ) ) {
275 $this->maybeHelp( true );
278 return $this->importFromHandle( $file );
281 function importFromHandle( $handle ) {
282 $this->startTime
= microtime( true );
284 $source = new ImportStreamSource( $handle );
285 $importer = new WikiImporter( $source, $this->getConfig() );
287 // Updating statistics require a lot of time so disable it
288 $importer->disableStatisticsUpdate();
290 if ( $this->hasOption( 'debug' ) ) {
291 $importer->setDebug( true );
293 if ( $this->hasOption( 'no-updates' ) ) {
294 $importer->setNoUpdates( true );
296 if ( $this->hasOption( 'rootpage' ) ) {
297 $statusRootPage = $importer->setTargetRootPage( $this->getOption( 'rootpage' ) );
298 if ( !$statusRootPage->isGood() ) {
299 // Die here so that it doesn't print "Done!"
300 $this->error( $statusRootPage->getMessage()->text(), 1 );
304 $importer->setPageCallback( [ $this, 'reportPage' ] );
305 $this->importCallback
= $importer->setRevisionCallback(
306 [ $this, 'handleRevision' ] );
307 $this->uploadCallback
= $importer->setUploadCallback(
308 [ $this, 'handleUpload' ] );
309 $this->logItemCallback
= $importer->setLogItemCallback(
310 [ $this, 'handleLogItem' ] );
311 if ( $this->uploads
) {
312 $importer->setImportUploads( true );
314 if ( $this->imageBasePath
) {
315 $importer->setImageBasePath( $this->imageBasePath
);
318 if ( $this->dryRun
) {
319 $importer->setPageOutCallback( null );
322 return $importer->doImport();
326 $maintClass = 'BackupReader';
327 require_once RUN_MAINTENANCE_IF_MAIN
;