Whitelist the <wbr> element.
[mediawiki.git] / maintenance / importDump.php
blob1f47cf12c21b3f43863c583614f62ef37af7d4f3
1 <?php
2 /**
3 * Import XML dump files into the current wiki.
5 * Copyright © 2005 Brion Vibber <brion@pobox.com>
6 * http://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
23 * @file
24 * @ingroup Maintenance
27 require_once __DIR__ . '/Maintenance.php';
29 /**
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;
37 public $revCount = 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() ) ? 'ok' : '(disabled; requires PHP zlib module)';
46 $bz2 = in_array( 'compress.bzip2', stream_get_wrappers() ) ? 'ok' : '(disabled; requires PHP bzip2 module)';
48 $this->mDescription = <<<TEXT
49 This script reads pages from an XML file as produced from Special:Export or
50 dumpBackup.php, and saves them into the current wiki.
52 Compressed XML files may be read directly:
53 .gz $gz
54 .bz2 $bz2
55 .7z (if 7za executable is in PATH)
57 Note that for very large data sets, importDump.php may be slow; there are
58 alternate methods which can be much faster for full site restoration:
59 <http://www.mediawiki.org/wiki/Manual:Importing_XML_dumps>
60 TEXT;
61 $this->stderr = fopen( "php://stderr", "wt" );
62 $this->addOption( 'report',
63 'Report position and speed after every n pages processed', false, true );
64 $this->addOption( 'namespaces',
65 'Import only the pages from namespaces belonging to the list of ' .
66 'pipe-separated namespace names or namespace indexes', false, true );
67 $this->addOption( 'dry-run', 'Parse dump without actually importing pages' );
68 $this->addOption( 'debug', 'Output extra verbose debug information' );
69 $this->addOption( 'uploads', 'Process file upload data if included (experimental)' );
70 $this->addOption( 'no-updates', 'Disable link table updates. Is faster but leaves the wiki in an inconsistent state' );
71 $this->addOption( 'image-base-path', 'Import files from a specified path', false, true );
72 $this->addArg( 'file', 'Dump file to import [else use stdin]', false );
75 public function execute() {
76 if ( wfReadOnly() ) {
77 $this->error( "Wiki is in read-only mode; you'll need to disable it for import to work.", true );
80 $this->reportingInterval = intval( $this->getOption( 'report', 100 ) );
81 if ( !$this->reportingInterval ) {
82 $this->reportingInterval = 100; // avoid division by zero
85 $this->dryRun = $this->hasOption( 'dry-run' );
86 $this->uploads = $this->hasOption( 'uploads' ); // experimental!
87 if ( $this->hasOption( 'image-base-path' ) ) {
88 $this->imageBasePath = $this->getOption( 'image-base-path' );
90 if ( $this->hasOption( 'namespaces' ) ) {
91 $this->setNsfilter( explode( '|', $this->getOption( 'namespaces' ) ) );
94 if ( $this->hasArg() ) {
95 $this->importFromFile( $this->getArg() );
96 } else {
97 $this->importFromStdin();
100 $this->output( "Done!\n" );
101 $this->output( "You might want to run rebuildrecentchanges.php to regenerate RecentChanges\n" );
104 function setNsfilter( array $namespaces ) {
105 if ( count( $namespaces ) == 0 ) {
106 $this->nsFilter = false;
107 return;
109 $this->nsFilter = array_unique( array_map( array( $this, 'getNsIndex' ), $namespaces ) );
112 private function getNsIndex( $namespace ) {
113 global $wgContLang;
114 if ( ( $result = $wgContLang->getNsIndex( $namespace ) ) !== false ) {
115 return $result;
117 $ns = intval( $namespace );
118 if ( strval( $ns ) === $namespace && $wgContLang->getNsText( $ns ) !== false ) {
119 return $ns;
121 $this->error( "Unknown namespace text / index specified: $namespace", true );
125 * @param $obj Title|Revision
126 * @return bool
128 private function skippedNamespace( $obj ) {
129 if ( $obj instanceof Title ) {
130 $ns = $obj->getNamespace();
131 } elseif ( $obj instanceof Revision ) {
132 $ns = $obj->getTitle()->getNamespace();
133 } elseif ( $obj instanceof WikiRevision ) {
134 $ns = $obj->title->getNamespace();
135 } else {
136 echo wfBacktrace();
137 $this->error( "Cannot get namespace of object in " . __METHOD__, true );
139 return is_array( $this->nsFilter ) && !in_array( $ns, $this->nsFilter );
142 function reportPage( $page ) {
143 $this->pageCount++;
147 * @param $rev Revision
148 * @return mixed
150 function handleRevision( $rev ) {
151 $title = $rev->getTitle();
152 if ( !$title ) {
153 $this->progress( "Got bogus revision with null title!" );
154 return;
157 if ( $this->skippedNamespace( $title ) ) {
158 return;
161 $this->revCount++;
162 $this->report();
164 if ( !$this->dryRun ) {
165 call_user_func( $this->importCallback, $rev );
170 * @param $revision Revision
171 * @return bool
173 function handleUpload( $revision ) {
174 if ( $this->uploads ) {
175 if ( $this->skippedNamespace( $revision ) ) {
176 return;
178 $this->uploadCount++;
179 // $this->report();
180 $this->progress( "upload: " . $revision->getFilename() );
182 if ( !$this->dryRun ) {
183 // bluuuh hack
184 // call_user_func( $this->uploadCallback, $revision );
185 $dbw = wfGetDB( DB_MASTER );
186 return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
191 function handleLogItem( $rev ) {
192 if ( $this->skippedNamespace( $rev ) ) {
193 return;
195 $this->revCount++;
196 $this->report();
198 if ( !$this->dryRun ) {
199 call_user_func( $this->logItemCallback, $rev );
203 function report( $final = false ) {
204 if ( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
205 $this->showReport();
209 function showReport() {
210 if ( !$this->mQuiet ) {
211 $delta = microtime( true ) - $this->startTime;
212 if ( $delta ) {
213 $rate = sprintf( "%.2f", $this->pageCount / $delta );
214 $revrate = sprintf( "%.2f", $this->revCount / $delta );
215 } else {
216 $rate = '-';
217 $revrate = '-';
219 # Logs dumps don't have page tallies
220 if ( $this->pageCount ) {
221 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
222 } else {
223 $this->progress( "$this->revCount ($revrate revs/sec)" );
226 wfWaitForSlaves();
227 // XXX: Don't let deferred jobs array get absurdly large (bug 24375)
228 DeferredUpdates::doUpdates( 'commit' );
231 function progress( $string ) {
232 fwrite( $this->stderr, $string . "\n" );
235 function importFromFile( $filename ) {
236 if ( preg_match( '/\.gz$/', $filename ) ) {
237 $filename = 'compress.zlib://' . $filename;
238 } elseif ( preg_match( '/\.bz2$/', $filename ) ) {
239 $filename = 'compress.bzip2://' . $filename;
240 } elseif ( preg_match( '/\.7z$/', $filename ) ) {
241 $filename = 'mediawiki.compress.7z://' . $filename;
244 $file = fopen( $filename, 'rt' );
245 return $this->importFromHandle( $file );
248 function importFromStdin() {
249 $file = fopen( 'php://stdin', 'rt' );
250 if ( self::posix_isatty( $file ) ) {
251 $this->maybeHelp( true );
253 return $this->importFromHandle( $file );
256 function importFromHandle( $handle ) {
257 $this->startTime = microtime( true );
259 $source = new ImportStreamSource( $handle );
260 $importer = new WikiImporter( $source );
262 if ( $this->hasOption( 'debug' ) ) {
263 $importer->setDebug( true );
265 if ( $this->hasOption( 'no-updates' ) ) {
266 $importer->setNoUpdates( true );
268 $importer->setPageCallback( array( &$this, 'reportPage' ) );
269 $this->importCallback = $importer->setRevisionCallback(
270 array( &$this, 'handleRevision' ) );
271 $this->uploadCallback = $importer->setUploadCallback(
272 array( &$this, 'handleUpload' ) );
273 $this->logItemCallback = $importer->setLogItemCallback(
274 array( &$this, 'handleLogItem' ) );
275 if ( $this->uploads ) {
276 $importer->setImportUploads( true );
278 if ( $this->imageBasePath ) {
279 $importer->setImageBasePath( $this->imageBasePath );
282 if ( $this->dryRun ) {
283 $importer->setPageOutCallback( null );
286 return $importer->doImport();
290 $maintClass = 'BackupReader';
291 require_once RUN_MAINTENANCE_IF_MAIN;