Merge "MimeAnalyzer: Add testcases for mp3 detection"
[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 public $imageBasePath = false;
41 public $nsFilter = false;
42
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)';
51
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.
56
57 Compressed XML files may be read directly:
58 .gz $gz
59 .bz2 $bz2
60 .7z (if 7za executable is in PATH)
61
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
66 );
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'
81 );
82 $this->addOption( 'image-base-path', 'Import files from a specified path', false, true );
83 $this->addOption( 'skip-to', 'Start from nth page by skipping first n-1 pages', false, true );
84 $this->addArg( 'file', 'Dump file to import [else use stdin]', false );
85 }
86
87 public function execute() {
88 if ( wfReadOnly() ) {
89 $this->error( "Wiki is in read-only mode; you'll need to disable it for import to work.", true );
90 }
91
92 $this->reportingInterval = intval( $this->getOption( 'report', 100 ) );
93 if ( !$this->reportingInterval ) {
94 $this->reportingInterval = 100; // avoid division by zero
95 }
96
97 $this->dryRun = $this->hasOption( 'dry-run' );
98 $this->uploads = $this->hasOption( 'uploads' ); // experimental!
99 if ( $this->hasOption( 'image-base-path' ) ) {
100 $this->imageBasePath = $this->getOption( 'image-base-path' );
101 }
102 if ( $this->hasOption( 'namespaces' ) ) {
103 $this->setNsfilter( explode( '|', $this->getOption( 'namespaces' ) ) );
104 }
105
106 if ( $this->hasArg() ) {
107 $this->importFromFile( $this->getArg() );
108 } else {
109 $this->importFromStdin();
110 }
111
112 $this->output( "Done!\n" );
113 $this->output( "You might want to run rebuildrecentchanges.php to regenerate RecentChanges,\n" );
114 $this->output( "and initSiteStats.php to update page and revision counts\n" );
115 }
116
117 function setNsfilter( array $namespaces ) {
118 if ( count( $namespaces ) == 0 ) {
119 $this->nsFilter = false;
120
121 return;
122 }
123 $this->nsFilter = array_unique( array_map( [ $this, 'getNsIndex' ], $namespaces ) );
124 }
125
126 private function getNsIndex( $namespace ) {
127 global $wgContLang;
128 $result = $wgContLang->getNsIndex( $namespace );
129 if ( $result !== false ) {
130 return $result;
131 }
132 $ns = intval( $namespace );
133 if ( strval( $ns ) === $namespace && $wgContLang->getNsText( $ns ) !== false ) {
134 return $ns;
135 }
136 $this->error( "Unknown namespace text / index specified: $namespace", true );
137 }
138
139 /**
140 * @param Title|Revision $obj
141 * @return bool
142 */
143 private function skippedNamespace( $obj ) {
144 $title = null;
145 if ( $obj instanceof Title ) {
146 $title = $obj;
147 } elseif ( $obj instanceof Revision ) {
148 $title = $obj->getTitle();
149 } elseif ( $obj instanceof WikiRevision ) {
150 $title = $obj->title;
151 } else {
152 throw new MWException( "Cannot get namespace of object in " . __METHOD__ );
153 }
154
155 if ( is_null( $title ) ) {
156 // Probably a log entry
157 return false;
158 }
159
160 $ns = $title->getNamespace();
161
162 return is_array( $this->nsFilter ) && !in_array( $ns, $this->nsFilter );
163 }
164
165 function reportPage( $page ) {
166 $this->pageCount++;
167 }
168
169 /**
170 * @param Revision $rev
171 */
172 function handleRevision( $rev ) {
173 $title = $rev->getTitle();
174 if ( !$title ) {
175 $this->progress( "Got bogus revision with null title!" );
176
177 return;
178 }
179
180 if ( $this->skippedNamespace( $title ) ) {
181 return;
182 }
183
184 $this->revCount++;
185 $this->report();
186
187 if ( !$this->dryRun ) {
188 call_user_func( $this->importCallback, $rev );
189 }
190 }
191
192 /**
193 * @param Revision $revision
194 * @return bool
195 */
196 function handleUpload( $revision ) {
197 if ( $this->uploads ) {
198 if ( $this->skippedNamespace( $revision ) ) {
199 return false;
200 }
201 $this->uploadCount++;
202 // $this->report();
203 $this->progress( "upload: " . $revision->getFilename() );
204
205 if ( !$this->dryRun ) {
206 // bluuuh hack
207 // call_user_func( $this->uploadCallback, $revision );
208 $dbw = $this->getDB( DB_MASTER );
209
210 return $dbw->deadlockLoop( [ $revision, 'importUpload' ] );
211 }
212 }
213
214 return false;
215 }
216
217 function handleLogItem( $rev ) {
218 if ( $this->skippedNamespace( $rev ) ) {
219 return;
220 }
221 $this->revCount++;
222 $this->report();
223
224 if ( !$this->dryRun ) {
225 call_user_func( $this->logItemCallback, $rev );
226 }
227 }
228
229 function report( $final = false ) {
230 if ( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
231 $this->showReport();
232 }
233 }
234
235 function showReport() {
236 if ( !$this->mQuiet ) {
237 $delta = microtime( true ) - $this->startTime;
238 if ( $delta ) {
239 $rate = sprintf( "%.2f", $this->pageCount / $delta );
240 $revrate = sprintf( "%.2f", $this->revCount / $delta );
241 } else {
242 $rate = '-';
243 $revrate = '-';
244 }
245 # Logs dumps don't have page tallies
246 if ( $this->pageCount ) {
247 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
248 } else {
249 $this->progress( "$this->revCount ($revrate revs/sec)" );
250 }
251 }
252 wfWaitForSlaves();
253 }
254
255 function progress( $string ) {
256 fwrite( $this->stderr, $string . "\n" );
257 }
258
259 function importFromFile( $filename ) {
260 if ( preg_match( '/\.gz$/', $filename ) ) {
261 $filename = 'compress.zlib://' . $filename;
262 } elseif ( preg_match( '/\.bz2$/', $filename ) ) {
263 $filename = 'compress.bzip2://' . $filename;
264 } elseif ( preg_match( '/\.7z$/', $filename ) ) {
265 $filename = 'mediawiki.compress.7z://' . $filename;
266 }
267
268 $file = fopen( $filename, 'rt' );
269
270 return $this->importFromHandle( $file );
271 }
272
273 function importFromStdin() {
274 $file = fopen( 'php://stdin', 'rt' );
275 if ( self::posix_isatty( $file ) ) {
276 $this->maybeHelp( true );
277 }
278
279 return $this->importFromHandle( $file );
280 }
281
282 function importFromHandle( $handle ) {
283 $this->startTime = microtime( true );
284
285 $source = new ImportStreamSource( $handle );
286 $importer = new WikiImporter( $source, $this->getConfig() );
287
288 // Updating statistics require a lot of time so disable it
289 $importer->disableStatisticsUpdate();
290
291 if ( $this->hasOption( 'debug' ) ) {
292 $importer->setDebug( true );
293 }
294 if ( $this->hasOption( 'no-updates' ) ) {
295 $importer->setNoUpdates( true );
296 }
297 if ( $this->hasOption( 'rootpage' ) ) {
298 $statusRootPage = $importer->setTargetRootPage( $this->getOption( 'rootpage' ) );
299 if ( !$statusRootPage->isGood() ) {
300 // Die here so that it doesn't print "Done!"
301 $this->error( $statusRootPage->getMessage()->text(), 1 );
302 return false;
303 }
304 }
305 if ( $this->hasOption( 'skip-to' ) ) {
306 $nthPage = (int)$this->getOption( 'skip-to' );
307 $importer->setPageOffset( $nthPage );
308 $this->pageCount = $nthPage - 1;
309 }
310 $importer->setPageCallback( [ $this, 'reportPage' ] );
311 $this->importCallback = $importer->setRevisionCallback(
312 [ $this, 'handleRevision' ] );
313 $this->uploadCallback = $importer->setUploadCallback(
314 [ $this, 'handleUpload' ] );
315 $this->logItemCallback = $importer->setLogItemCallback(
316 [ $this, 'handleLogItem' ] );
317 if ( $this->uploads ) {
318 $importer->setImportUploads( true );
319 }
320 if ( $this->imageBasePath ) {
321 $importer->setImageBasePath( $this->imageBasePath );
322 }
323
324 if ( $this->dryRun ) {
325 $importer->setPageOutCallback( null );
326 }
327
328 return $importer->doImport();
329 }
330 }
331
332 $maintClass = 'BackupReader';
333 require_once RUN_MAINTENANCE_IF_MAIN;