Merge "xmp: Lower severity of XMP parse failure log events"
[lhc/web/wiklou.git] / maintenance / importDump.php
1 <?php
2 /**
3 * Import XML dump files into the current wiki.
4 *
5 * Copyright © 2005 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
7 *
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.
12 *
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.
17 *
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
22 *
23 * @file
24 * @ingroup Maintenance
25 */
26
27 require_once __DIR__ . '/Maintenance.php';
28
29 /**
30 * Maintenance script that imports XML dump files into the current wiki.
31 *
32 * @ingroup Maintenance
33 */
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 protected $uploadCount = 0;
41 public $imageBasePath = false;
42 public $nsFilter = false;
43
44 function __construct() {
45 parent::__construct();
46 $gz = in_array( 'compress.zlib', stream_get_wrappers() )
47 ? 'ok'
48 : '(disabled; requires PHP zlib module)';
49 $bz2 = in_array( 'compress.bzip2', stream_get_wrappers() )
50 ? 'ok'
51 : '(disabled; requires PHP bzip2 module)';
52
53 $this->addDescription(
54 <<<TEXT
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.
57
58 Compressed XML files may be read directly:
59 .gz $gz
60 .bz2 $bz2
61 .7z (if 7za executable is in PATH)
62
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>
66 TEXT
67 );
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',
75 false, true );
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)' );
79 $this->addOption(
80 'no-updates',
81 'Disable link table updates. Is faster but leaves the wiki in an inconsistent state'
82 );
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->addOption( 'username-interwiki', 'Use interwiki usernames with this prefix', false, true );
86 $this->addOption( 'no-local-users',
87 'Treat all usernames as interwiki. ' .
88 'The default is to assign edits to local users where they exist.',
89 false, false
90 );
91 $this->addArg( 'file', 'Dump file to import [else use stdin]', false );
92 }
93
94 public function execute() {
95 if ( wfReadOnly() ) {
96 $this->fatalError( "Wiki is in read-only mode; you'll need to disable it for import to work." );
97 }
98
99 $this->reportingInterval = intval( $this->getOption( 'report', 100 ) );
100 if ( !$this->reportingInterval ) {
101 $this->reportingInterval = 100; // avoid division by zero
102 }
103
104 $this->dryRun = $this->hasOption( 'dry-run' );
105 $this->uploads = $this->hasOption( 'uploads' ); // experimental!
106 if ( $this->hasOption( 'image-base-path' ) ) {
107 $this->imageBasePath = $this->getOption( 'image-base-path' );
108 }
109 if ( $this->hasOption( 'namespaces' ) ) {
110 $this->setNsfilter( explode( '|', $this->getOption( 'namespaces' ) ) );
111 }
112
113 if ( $this->hasArg() ) {
114 $this->importFromFile( $this->getArg() );
115 } else {
116 $this->importFromStdin();
117 }
118
119 $this->output( "Done!\n" );
120 $this->output( "You might want to run rebuildrecentchanges.php to regenerate RecentChanges,\n" );
121 $this->output( "and initSiteStats.php to update page and revision counts\n" );
122 }
123
124 function setNsfilter( array $namespaces ) {
125 if ( count( $namespaces ) == 0 ) {
126 $this->nsFilter = false;
127
128 return;
129 }
130 $this->nsFilter = array_unique( array_map( [ $this, 'getNsIndex' ], $namespaces ) );
131 }
132
133 private function getNsIndex( $namespace ) {
134 global $wgContLang;
135 $result = $wgContLang->getNsIndex( $namespace );
136 if ( $result !== false ) {
137 return $result;
138 }
139 $ns = intval( $namespace );
140 if ( strval( $ns ) === $namespace && $wgContLang->getNsText( $ns ) !== false ) {
141 return $ns;
142 }
143 $this->fatalError( "Unknown namespace text / index specified: $namespace" );
144 }
145
146 /**
147 * @param Title|Revision $obj
148 * @return bool
149 */
150 private function skippedNamespace( $obj ) {
151 $title = null;
152 if ( $obj instanceof Title ) {
153 $title = $obj;
154 } elseif ( $obj instanceof Revision ) {
155 $title = $obj->getTitle();
156 } elseif ( $obj instanceof WikiRevision ) {
157 $title = $obj->title;
158 } else {
159 throw new MWException( "Cannot get namespace of object in " . __METHOD__ );
160 }
161
162 if ( is_null( $title ) ) {
163 // Probably a log entry
164 return false;
165 }
166
167 $ns = $title->getNamespace();
168
169 return is_array( $this->nsFilter ) && !in_array( $ns, $this->nsFilter );
170 }
171
172 function reportPage( $page ) {
173 $this->pageCount++;
174 }
175
176 /**
177 * @param Revision $rev
178 */
179 function handleRevision( $rev ) {
180 $title = $rev->getTitle();
181 if ( !$title ) {
182 $this->progress( "Got bogus revision with null title!" );
183
184 return;
185 }
186
187 if ( $this->skippedNamespace( $title ) ) {
188 return;
189 }
190
191 $this->revCount++;
192 $this->report();
193
194 if ( !$this->dryRun ) {
195 call_user_func( $this->importCallback, $rev );
196 }
197 }
198
199 /**
200 * @param Revision $revision
201 * @return bool
202 */
203 function handleUpload( $revision ) {
204 if ( $this->uploads ) {
205 if ( $this->skippedNamespace( $revision ) ) {
206 return false;
207 }
208 $this->uploadCount++;
209 // $this->report();
210 $this->progress( "upload: " . $revision->getFilename() );
211
212 if ( !$this->dryRun ) {
213 // bluuuh hack
214 // call_user_func( $this->uploadCallback, $revision );
215 $dbw = $this->getDB( DB_MASTER );
216
217 return $dbw->deadlockLoop( [ $revision, 'importUpload' ] );
218 }
219 }
220
221 return false;
222 }
223
224 function handleLogItem( $rev ) {
225 if ( $this->skippedNamespace( $rev ) ) {
226 return;
227 }
228 $this->revCount++;
229 $this->report();
230
231 if ( !$this->dryRun ) {
232 call_user_func( $this->logItemCallback, $rev );
233 }
234 }
235
236 function report( $final = false ) {
237 if ( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
238 $this->showReport();
239 }
240 }
241
242 function showReport() {
243 if ( !$this->mQuiet ) {
244 $delta = microtime( true ) - $this->startTime;
245 if ( $delta ) {
246 $rate = sprintf( "%.2f", $this->pageCount / $delta );
247 $revrate = sprintf( "%.2f", $this->revCount / $delta );
248 } else {
249 $rate = '-';
250 $revrate = '-';
251 }
252 # Logs dumps don't have page tallies
253 if ( $this->pageCount ) {
254 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
255 } else {
256 $this->progress( "$this->revCount ($revrate revs/sec)" );
257 }
258 }
259 wfWaitForSlaves();
260 }
261
262 function progress( $string ) {
263 fwrite( $this->stderr, $string . "\n" );
264 }
265
266 function importFromFile( $filename ) {
267 if ( preg_match( '/\.gz$/', $filename ) ) {
268 $filename = 'compress.zlib://' . $filename;
269 } elseif ( preg_match( '/\.bz2$/', $filename ) ) {
270 $filename = 'compress.bzip2://' . $filename;
271 } elseif ( preg_match( '/\.7z$/', $filename ) ) {
272 $filename = 'mediawiki.compress.7z://' . $filename;
273 }
274
275 $file = fopen( $filename, 'rt' );
276
277 return $this->importFromHandle( $file );
278 }
279
280 function importFromStdin() {
281 $file = fopen( 'php://stdin', 'rt' );
282 if ( self::posix_isatty( $file ) ) {
283 $this->maybeHelp( true );
284 }
285
286 return $this->importFromHandle( $file );
287 }
288
289 function importFromHandle( $handle ) {
290 $this->startTime = microtime( true );
291
292 $source = new ImportStreamSource( $handle );
293 $importer = new WikiImporter( $source, $this->getConfig() );
294
295 // Updating statistics require a lot of time so disable it
296 $importer->disableStatisticsUpdate();
297
298 if ( $this->hasOption( 'debug' ) ) {
299 $importer->setDebug( true );
300 }
301 if ( $this->hasOption( 'no-updates' ) ) {
302 $importer->setNoUpdates( true );
303 }
304 if ( $this->hasOption( 'username-prefix' ) ) {
305 $importer->setUsernamePrefix(
306 $this->getOption( 'username-prefix' ),
307 !$this->hasOption( 'no-local-users' )
308 );
309 }
310 if ( $this->hasOption( 'rootpage' ) ) {
311 $statusRootPage = $importer->setTargetRootPage( $this->getOption( 'rootpage' ) );
312 if ( !$statusRootPage->isGood() ) {
313 // Die here so that it doesn't print "Done!"
314 $this->fatalError( $statusRootPage->getMessage()->text() );
315 return false;
316 }
317 }
318 if ( $this->hasOption( 'skip-to' ) ) {
319 $nthPage = (int)$this->getOption( 'skip-to' );
320 $importer->setPageOffset( $nthPage );
321 $this->pageCount = $nthPage - 1;
322 }
323 $importer->setPageCallback( [ $this, 'reportPage' ] );
324 $this->importCallback = $importer->setRevisionCallback(
325 [ $this, 'handleRevision' ] );
326 $this->uploadCallback = $importer->setUploadCallback(
327 [ $this, 'handleUpload' ] );
328 $this->logItemCallback = $importer->setLogItemCallback(
329 [ $this, 'handleLogItem' ] );
330 if ( $this->uploads ) {
331 $importer->setImportUploads( true );
332 }
333 if ( $this->imageBasePath ) {
334 $importer->setImageBasePath( $this->imageBasePath );
335 }
336
337 if ( $this->dryRun ) {
338 $importer->setPageOutCallback( null );
339 }
340
341 return $importer->doImport();
342 }
343 }
344
345 $maintClass = 'BackupReader';
346 require_once RUN_MAINTENANCE_IF_MAIN;