Remove FileRepoStatus references
[mediawiki.git] / maintenance / importDump.php
blobf0e0555af1deca3e2103865e69014db65186ae77
1 <?php
2 /**
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
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() )
46 ? 'ok'
47 : '(disabled; requires PHP zlib module)';
48 $bz2 = in_array( 'compress.bzip2', stream_get_wrappers() )
49 ? 'ok'
50 : '(disabled; requires PHP bzip2 module)';
52 $this->addDescription(
53 <<<TEXT
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:
58 .gz $gz
59 .bz2 $bz2
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>
65 TEXT
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',
74 false, true );
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)' );
78 $this->addOption(
79 'no-updates',
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() {
87 if ( wfReadOnly() ) {
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() );
107 } else {
108 $this->importFromStdin();
111 $this->output( "Done!\n" );
112 $this->output( "You might want to run rebuildrecentchanges.php to regenerate RecentChanges\n" );
115 function setNsfilter( array $namespaces ) {
116 if ( count( $namespaces ) == 0 ) {
117 $this->nsFilter = false;
119 return;
121 $this->nsFilter = array_unique( array_map( [ $this, 'getNsIndex' ], $namespaces ) );
124 private function getNsIndex( $namespace ) {
125 global $wgContLang;
126 $result = $wgContLang->getNsIndex( $namespace );
127 if ( $result !== false ) {
128 return $result;
130 $ns = intval( $namespace );
131 if ( strval( $ns ) === $namespace && $wgContLang->getNsText( $ns ) !== false ) {
132 return $ns;
134 $this->error( "Unknown namespace text / index specified: $namespace", true );
138 * @param Title|Revision $obj
139 * @return bool
141 private function skippedNamespace( $obj ) {
142 $title = null;
143 if ( $obj instanceof Title ) {
144 $title = $obj;
145 } elseif ( $obj instanceof Revision ) {
146 $title = $obj->getTitle();
147 } elseif ( $obj instanceof WikiRevision ) {
148 $title = $obj->title;
149 } else {
150 throw new MWException( "Cannot get namespace of object in " . __METHOD__ );
153 if ( is_null( $title ) ) {
154 // Probably a log entry
155 return false;
158 $ns = $title->getNamespace();
160 return is_array( $this->nsFilter ) && !in_array( $ns, $this->nsFilter );
163 function reportPage( $page ) {
164 $this->pageCount++;
168 * @param Revision $rev
170 function handleRevision( $rev ) {
171 $title = $rev->getTitle();
172 if ( !$title ) {
173 $this->progress( "Got bogus revision with null title!" );
175 return;
178 if ( $this->skippedNamespace( $title ) ) {
179 return;
182 $this->revCount++;
183 $this->report();
185 if ( !$this->dryRun ) {
186 call_user_func( $this->importCallback, $rev );
191 * @param Revision $revision
192 * @return bool
194 function handleUpload( $revision ) {
195 if ( $this->uploads ) {
196 if ( $this->skippedNamespace( $revision ) ) {
197 return false;
199 $this->uploadCount++;
200 // $this->report();
201 $this->progress( "upload: " . $revision->getFilename() );
203 if ( !$this->dryRun ) {
204 // bluuuh hack
205 // call_user_func( $this->uploadCallback, $revision );
206 $dbw = $this->getDB( DB_MASTER );
208 return $dbw->deadlockLoop( [ $revision, 'importUpload' ] );
212 return false;
215 function handleLogItem( $rev ) {
216 if ( $this->skippedNamespace( $rev ) ) {
217 return;
219 $this->revCount++;
220 $this->report();
222 if ( !$this->dryRun ) {
223 call_user_func( $this->logItemCallback, $rev );
227 function report( $final = false ) {
228 if ( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
229 $this->showReport();
233 function showReport() {
234 if ( !$this->mQuiet ) {
235 $delta = microtime( true ) - $this->startTime;
236 if ( $delta ) {
237 $rate = sprintf( "%.2f", $this->pageCount / $delta );
238 $revrate = sprintf( "%.2f", $this->revCount / $delta );
239 } else {
240 $rate = '-';
241 $revrate = '-';
243 # Logs dumps don't have page tallies
244 if ( $this->pageCount ) {
245 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
246 } else {
247 $this->progress( "$this->revCount ($revrate revs/sec)" );
250 wfWaitForSlaves();
253 function progress( $string ) {
254 fwrite( $this->stderr, $string . "\n" );
257 function importFromFile( $filename ) {
258 if ( preg_match( '/\.gz$/', $filename ) ) {
259 $filename = 'compress.zlib://' . $filename;
260 } elseif ( preg_match( '/\.bz2$/', $filename ) ) {
261 $filename = 'compress.bzip2://' . $filename;
262 } elseif ( preg_match( '/\.7z$/', $filename ) ) {
263 $filename = 'mediawiki.compress.7z://' . $filename;
266 $file = fopen( $filename, 'rt' );
268 return $this->importFromHandle( $file );
271 function importFromStdin() {
272 $file = fopen( 'php://stdin', 'rt' );
273 if ( self::posix_isatty( $file ) ) {
274 $this->maybeHelp( true );
277 return $this->importFromHandle( $file );
280 function importFromHandle( $handle ) {
281 $this->startTime = microtime( true );
283 $source = new ImportStreamSource( $handle );
284 $importer = new WikiImporter( $source, $this->getConfig() );
286 if ( $this->hasOption( 'debug' ) ) {
287 $importer->setDebug( true );
289 if ( $this->hasOption( 'no-updates' ) ) {
290 $importer->setNoUpdates( true );
292 if ( $this->hasOption( 'rootpage' ) ) {
293 $statusRootPage = $importer->setTargetRootPage( $this->getOption( 'rootpage' ) );
294 if ( !$statusRootPage->isGood() ) {
295 // Die here so that it doesn't print "Done!"
296 $this->error( $statusRootPage->getMessage()->text(), 1 );
297 return false;
300 $importer->setPageCallback( [ $this, 'reportPage' ] );
301 $this->importCallback = $importer->setRevisionCallback(
302 [ $this, 'handleRevision' ] );
303 $this->uploadCallback = $importer->setUploadCallback(
304 [ $this, 'handleUpload' ] );
305 $this->logItemCallback = $importer->setLogItemCallback(
306 [ $this, 'handleLogItem' ] );
307 if ( $this->uploads ) {
308 $importer->setImportUploads( true );
310 if ( $this->imageBasePath ) {
311 $importer->setImageBasePath( $this->imageBasePath );
314 if ( $this->dryRun ) {
315 $importer->setPageOutCallback( null );
318 return $importer->doImport();
322 $maintClass = 'BackupReader';
323 require_once RUN_MAINTENANCE_IF_MAIN;