Merge "Add GENDER support to protection null revision edit summary"
[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->addArg( 'file', 'Dump file to import [else use stdin]', false );
84 }
85
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 );
89 }
90
91 $this->reportingInterval = intval( $this->getOption( 'report', 100 ) );
92 if ( !$this->reportingInterval ) {
93 $this->reportingInterval = 100; // avoid division by zero
94 }
95
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' );
100 }
101 if ( $this->hasOption( 'namespaces' ) ) {
102 $this->setNsfilter( explode( '|', $this->getOption( 'namespaces' ) ) );
103 }
104
105 if ( $this->hasArg() ) {
106 $this->importFromFile( $this->getArg() );
107 } else {
108 $this->importFromStdin();
109 }
110
111 $this->output( "Done!\n" );
112 $this->output( "You might want to run rebuildrecentchanges.php to regenerate RecentChanges\n" );
113 }
114
115 function setNsfilter( array $namespaces ) {
116 if ( count( $namespaces ) == 0 ) {
117 $this->nsFilter = false;
118
119 return;
120 }
121 $this->nsFilter = array_unique( array_map( [ $this, 'getNsIndex' ], $namespaces ) );
122 }
123
124 private function getNsIndex( $namespace ) {
125 global $wgContLang;
126 $result = $wgContLang->getNsIndex( $namespace );
127 if ( $result !== false ) {
128 return $result;
129 }
130 $ns = intval( $namespace );
131 if ( strval( $ns ) === $namespace && $wgContLang->getNsText( $ns ) !== false ) {
132 return $ns;
133 }
134 $this->error( "Unknown namespace text / index specified: $namespace", true );
135 }
136
137 /**
138 * @param Title|Revision $obj
139 * @return bool
140 */
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__ );
151 }
152
153 if ( is_null( $title ) ) {
154 // Probably a log entry
155 return false;
156 }
157
158 $ns = $title->getNamespace();
159
160 return is_array( $this->nsFilter ) && !in_array( $ns, $this->nsFilter );
161 }
162
163 function reportPage( $page ) {
164 $this->pageCount++;
165 }
166
167 /**
168 * @param Revision $rev
169 */
170 function handleRevision( $rev ) {
171 $title = $rev->getTitle();
172 if ( !$title ) {
173 $this->progress( "Got bogus revision with null title!" );
174
175 return;
176 }
177
178 if ( $this->skippedNamespace( $title ) ) {
179 return;
180 }
181
182 $this->revCount++;
183 $this->report();
184
185 if ( !$this->dryRun ) {
186 call_user_func( $this->importCallback, $rev );
187 }
188 }
189
190 /**
191 * @param Revision $revision
192 * @return bool
193 */
194 function handleUpload( $revision ) {
195 if ( $this->uploads ) {
196 if ( $this->skippedNamespace( $revision ) ) {
197 return false;
198 }
199 $this->uploadCount++;
200 // $this->report();
201 $this->progress( "upload: " . $revision->getFilename() );
202
203 if ( !$this->dryRun ) {
204 // bluuuh hack
205 // call_user_func( $this->uploadCallback, $revision );
206 $dbw = $this->getDB( DB_MASTER );
207
208 return $dbw->deadlockLoop( [ $revision, 'importUpload' ] );
209 }
210 }
211
212 return false;
213 }
214
215 function handleLogItem( $rev ) {
216 if ( $this->skippedNamespace( $rev ) ) {
217 return;
218 }
219 $this->revCount++;
220 $this->report();
221
222 if ( !$this->dryRun ) {
223 call_user_func( $this->logItemCallback, $rev );
224 }
225 }
226
227 function report( $final = false ) {
228 if ( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
229 $this->showReport();
230 }
231 }
232
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 = '-';
242 }
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)" );
248 }
249 }
250 wfWaitForSlaves();
251 }
252
253 function progress( $string ) {
254 fwrite( $this->stderr, $string . "\n" );
255 }
256
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;
264 }
265
266 $file = fopen( $filename, 'rt' );
267
268 return $this->importFromHandle( $file );
269 }
270
271 function importFromStdin() {
272 $file = fopen( 'php://stdin', 'rt' );
273 if ( self::posix_isatty( $file ) ) {
274 $this->maybeHelp( true );
275 }
276
277 return $this->importFromHandle( $file );
278 }
279
280 function importFromHandle( $handle ) {
281 $this->startTime = microtime( true );
282
283 $source = new ImportStreamSource( $handle );
284 $importer = new WikiImporter( $source, $this->getConfig() );
285
286 if ( $this->hasOption( 'debug' ) ) {
287 $importer->setDebug( true );
288 }
289 if ( $this->hasOption( 'no-updates' ) ) {
290 $importer->setNoUpdates( true );
291 }
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;
298 }
299 }
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 );
309 }
310 if ( $this->imageBasePath ) {
311 $importer->setImageBasePath( $this->imageBasePath );
312 }
313
314 if ( $this->dryRun ) {
315 $importer->setPageOutCallback( null );
316 }
317
318 return $importer->doImport();
319 }
320 }
321
322 $maintClass = 'BackupReader';
323 require_once RUN_MAINTENANCE_IF_MAIN;