Handle missing titles and usernames when importing log items
[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->mDescription = <<<TEXT
53 This script reads pages from an XML file as produced from Special:Export or
54 dumpBackup.php, and saves them into the current wiki.
55
56 Compressed XML files may be read directly:
57 .gz $gz
58 .bz2 $bz2
59 .7z (if 7za executable is in PATH)
60
61 Note that for very large data sets, importDump.php may be slow; there are
62 alternate methods which can be much faster for full site restoration:
63 <https://www.mediawiki.org/wiki/Manual:Importing_XML_dumps>
64 TEXT;
65 $this->stderr = fopen( "php://stderr", "wt" );
66 $this->addOption( 'report',
67 'Report position and speed after every n pages processed', false, true );
68 $this->addOption( 'namespaces',
69 'Import only the pages from namespaces belonging to the list of ' .
70 'pipe-separated namespace names or namespace indexes', false, true );
71 $this->addOption( 'dry-run', 'Parse dump without actually importing pages' );
72 $this->addOption( 'debug', 'Output extra verbose debug information' );
73 $this->addOption( 'uploads', 'Process file upload data if included (experimental)' );
74 $this->addOption(
75 'no-updates',
76 'Disable link table updates. Is faster but leaves the wiki in an inconsistent state'
77 );
78 $this->addOption( 'image-base-path', 'Import files from a specified path', false, true );
79 $this->addArg( 'file', 'Dump file to import [else use stdin]', false );
80 }
81
82 public function execute() {
83 if ( wfReadOnly() ) {
84 $this->error( "Wiki is in read-only mode; you'll need to disable it for import to work.", true );
85 }
86
87 $this->reportingInterval = intval( $this->getOption( 'report', 100 ) );
88 if ( !$this->reportingInterval ) {
89 $this->reportingInterval = 100; // avoid division by zero
90 }
91
92 $this->dryRun = $this->hasOption( 'dry-run' );
93 $this->uploads = $this->hasOption( 'uploads' ); // experimental!
94 if ( $this->hasOption( 'image-base-path' ) ) {
95 $this->imageBasePath = $this->getOption( 'image-base-path' );
96 }
97 if ( $this->hasOption( 'namespaces' ) ) {
98 $this->setNsfilter( explode( '|', $this->getOption( 'namespaces' ) ) );
99 }
100
101 if ( $this->hasArg() ) {
102 $this->importFromFile( $this->getArg() );
103 } else {
104 $this->importFromStdin();
105 }
106
107 $this->output( "Done!\n" );
108 $this->output( "You might want to run rebuildrecentchanges.php to regenerate RecentChanges\n" );
109 }
110
111 function setNsfilter( array $namespaces ) {
112 if ( count( $namespaces ) == 0 ) {
113 $this->nsFilter = false;
114
115 return;
116 }
117 $this->nsFilter = array_unique( array_map( array( $this, 'getNsIndex' ), $namespaces ) );
118 }
119
120 private function getNsIndex( $namespace ) {
121 global $wgContLang;
122 $result = $wgContLang->getNsIndex( $namespace );
123 if ( $result !== false ) {
124 return $result;
125 }
126 $ns = intval( $namespace );
127 if ( strval( $ns ) === $namespace && $wgContLang->getNsText( $ns ) !== false ) {
128 return $ns;
129 }
130 $this->error( "Unknown namespace text / index specified: $namespace", true );
131 }
132
133 /**
134 * @param Title|Revision $obj
135 * @return bool
136 */
137 private function skippedNamespace( $obj ) {
138 $title = null;
139 if ( $obj instanceof Title ) {
140 $title = $obj;
141 } elseif ( $obj instanceof Revision ) {
142 $title = $obj->getTitle();
143 } elseif ( $obj instanceof WikiRevision ) {
144 $title = $obj->title;
145 } else {
146 throw new MWException( "Cannot get namespace of object in " . __METHOD__ );
147 }
148
149 if ( is_null( $title ) ) {
150 // Probably a log entry
151 return false;
152 }
153
154 $ns = $title->getNamespace();
155
156 return is_array( $this->nsFilter ) && !in_array( $ns, $this->nsFilter );
157 }
158
159 function reportPage( $page ) {
160 $this->pageCount++;
161 }
162
163 /**
164 * @param Revision $rev
165 */
166 function handleRevision( $rev ) {
167 $title = $rev->getTitle();
168 if ( !$title ) {
169 $this->progress( "Got bogus revision with null title!" );
170
171 return;
172 }
173
174 if ( $this->skippedNamespace( $title ) ) {
175 return;
176 }
177
178 $this->revCount++;
179 $this->report();
180
181 if ( !$this->dryRun ) {
182 call_user_func( $this->importCallback, $rev );
183 }
184 }
185
186 /**
187 * @param Revision $revision
188 * @return bool
189 */
190 function handleUpload( $revision ) {
191 if ( $this->uploads ) {
192 if ( $this->skippedNamespace( $revision ) ) {
193 return false;
194 }
195 $this->uploadCount++;
196 // $this->report();
197 $this->progress( "upload: " . $revision->getFilename() );
198
199 if ( !$this->dryRun ) {
200 // bluuuh hack
201 // call_user_func( $this->uploadCallback, $revision );
202 $dbw = wfGetDB( DB_MASTER );
203
204 return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
205 }
206 }
207
208 return false;
209 }
210
211 function handleLogItem( $rev ) {
212 if ( $this->skippedNamespace( $rev ) ) {
213 return;
214 }
215 $this->revCount++;
216 $this->report();
217
218 if ( !$this->dryRun ) {
219 call_user_func( $this->logItemCallback, $rev );
220 }
221 }
222
223 function report( $final = false ) {
224 if ( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
225 $this->showReport();
226 }
227 }
228
229 function showReport() {
230 if ( !$this->mQuiet ) {
231 $delta = microtime( true ) - $this->startTime;
232 if ( $delta ) {
233 $rate = sprintf( "%.2f", $this->pageCount / $delta );
234 $revrate = sprintf( "%.2f", $this->revCount / $delta );
235 } else {
236 $rate = '-';
237 $revrate = '-';
238 }
239 # Logs dumps don't have page tallies
240 if ( $this->pageCount ) {
241 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
242 } else {
243 $this->progress( "$this->revCount ($revrate revs/sec)" );
244 }
245 }
246 wfWaitForSlaves();
247 }
248
249 function progress( $string ) {
250 fwrite( $this->stderr, $string . "\n" );
251 }
252
253 function importFromFile( $filename ) {
254 if ( preg_match( '/\.gz$/', $filename ) ) {
255 $filename = 'compress.zlib://' . $filename;
256 } elseif ( preg_match( '/\.bz2$/', $filename ) ) {
257 $filename = 'compress.bzip2://' . $filename;
258 } elseif ( preg_match( '/\.7z$/', $filename ) ) {
259 $filename = 'mediawiki.compress.7z://' . $filename;
260 }
261
262 $file = fopen( $filename, 'rt' );
263
264 return $this->importFromHandle( $file );
265 }
266
267 function importFromStdin() {
268 $file = fopen( 'php://stdin', 'rt' );
269 if ( self::posix_isatty( $file ) ) {
270 $this->maybeHelp( true );
271 }
272
273 return $this->importFromHandle( $file );
274 }
275
276 function importFromHandle( $handle ) {
277 $this->startTime = microtime( true );
278
279 $source = new ImportStreamSource( $handle );
280 $importer = new WikiImporter( $source, $this->getConfig() );
281
282 if ( $this->hasOption( 'debug' ) ) {
283 $importer->setDebug( true );
284 }
285 if ( $this->hasOption( 'no-updates' ) ) {
286 $importer->setNoUpdates( true );
287 }
288 $importer->setPageCallback( array( &$this, 'reportPage' ) );
289 $this->importCallback = $importer->setRevisionCallback(
290 array( &$this, 'handleRevision' ) );
291 $this->uploadCallback = $importer->setUploadCallback(
292 array( &$this, 'handleUpload' ) );
293 $this->logItemCallback = $importer->setLogItemCallback(
294 array( &$this, 'handleLogItem' ) );
295 if ( $this->uploads ) {
296 $importer->setImportUploads( true );
297 }
298 if ( $this->imageBasePath ) {
299 $importer->setImageBasePath( $this->imageBasePath );
300 }
301
302 if ( $this->dryRun ) {
303 $importer->setPageOutCallback( null );
304 }
305
306 return $importer->doImport();
307 }
308 }
309
310 $maintClass = 'BackupReader';
311 require_once RUN_MAINTENANCE_IF_MAIN;