MediaWiki REL1_30
WikiExporter.php
Go to the documentation of this file.
1<?php
32
38 public $list_authors = false;
39
41 public $dumpUploads = false;
42
45
47 public $author_list = "";
48
49 const FULL = 1;
50 const CURRENT = 2;
51 const STABLE = 4; // extension defined
52 const LOGS = 8;
53 const RANGE = 16;
54
55 const BUFFER = 0;
56 const STREAM = 1;
57
58 const TEXT = 0;
59 const STUB = 1;
60
62 public $buffer;
63
65 public $text;
66
68 public $sink;
69
74 public static function schemaVersion() {
75 return "0.10";
76 }
77
94 function __construct( $db, $history = self::CURRENT,
95 $buffer = self::BUFFER, $text = self::TEXT ) {
96 $this->db = $db;
97 $this->history = $history;
98 $this->buffer = $buffer;
99 $this->writer = new XmlDumpWriter();
100 $this->sink = new DumpOutput();
101 $this->text = $text;
102 }
103
111 public function setOutputSink( &$sink ) {
112 $this->sink =& $sink;
113 }
114
115 public function openStream() {
116 $output = $this->writer->openStream();
117 $this->sink->writeOpenStream( $output );
118 }
119
120 public function closeStream() {
121 $output = $this->writer->closeStream();
122 $this->sink->writeCloseStream( $output );
123 }
124
130 public function allPages() {
131 $this->dumpFrom( '' );
132 }
133
142 public function pagesByRange( $start, $end, $orderRevs ) {
143 if ( $orderRevs ) {
144 $condition = 'rev_page >= ' . intval( $start );
145 if ( $end ) {
146 $condition .= ' AND rev_page < ' . intval( $end );
147 }
148 } else {
149 $condition = 'page_id >= ' . intval( $start );
150 if ( $end ) {
151 $condition .= ' AND page_id < ' . intval( $end );
152 }
153 }
154 $this->dumpFrom( $condition, $orderRevs );
155 }
156
164 public function revsByRange( $start, $end ) {
165 $condition = 'rev_id >= ' . intval( $start );
166 if ( $end ) {
167 $condition .= ' AND rev_id < ' . intval( $end );
168 }
169 $this->dumpFrom( $condition );
170 }
171
175 public function pageByTitle( $title ) {
176 $this->dumpFrom(
177 'page_namespace=' . $title->getNamespace() .
178 ' AND page_title=' . $this->db->addQuotes( $title->getDBkey() ) );
179 }
180
185 public function pageByName( $name ) {
186 $title = Title::newFromText( $name );
187 if ( is_null( $title ) ) {
188 throw new MWException( "Can't export invalid title" );
189 } else {
190 $this->pageByTitle( $title );
191 }
192 }
193
197 public function pagesByName( $names ) {
198 foreach ( $names as $name ) {
199 $this->pageByName( $name );
200 }
201 }
202
203 public function allLogs() {
204 $this->dumpFrom( '' );
205 }
206
211 public function logsByRange( $start, $end ) {
212 $condition = 'log_id >= ' . intval( $start );
213 if ( $end ) {
214 $condition .= ' AND log_id < ' . intval( $end );
215 }
216 $this->dumpFrom( $condition );
217 }
218
226 protected function do_list_authors( $cond ) {
227 $this->author_list = "<contributors>";
228 // rev_deleted
229
230 $res = $this->db->select(
231 [ 'page', 'revision' ],
232 [ 'DISTINCT rev_user_text', 'rev_user' ],
233 [
234 $this->db->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0',
235 $cond,
236 'page_id = rev_id',
237 ],
238 __METHOD__
239 );
240
241 foreach ( $res as $row ) {
242 $this->author_list .= "<contributor>" .
243 "<username>" .
244 htmlentities( $row->rev_user_text ) .
245 "</username>" .
246 "<id>" .
247 $row->rev_user .
248 "</id>" .
249 "</contributor>";
250 }
251 $this->author_list .= "</contributors>";
252 }
253
260 protected function dumpFrom( $cond = '', $orderRevs = false ) {
261 # For logging dumps...
262 if ( $this->history & self::LOGS ) {
263 $where = [];
264 # Hide private logs
265 $hideLogs = LogEventsList::getExcludeClause( $this->db );
266 if ( $hideLogs ) {
267 $where[] = $hideLogs;
268 }
269 # Add on any caller specified conditions
270 if ( $cond ) {
271 $where[] = $cond;
272 }
273 # Get logging table name for logging.* clause
274 $logging = $this->db->tableName( 'logging' );
275
276 if ( $this->buffer == self::STREAM ) {
277 $prev = $this->db->bufferResults( false );
278 }
279 $result = null; // Assuring $result is not undefined, if exception occurs early
280
281 $commentQuery = CommentStore::newKey( 'log_comment' )->getJoin();
282
283 try {
284 $result = $this->db->select( [ 'logging', 'user' ] + $commentQuery['tables'],
285 [ "{$logging}.*", 'user_name' ] + $commentQuery['fields'], // grab the user name
286 $where,
287 __METHOD__,
288 [ 'ORDER BY' => 'log_id', 'USE INDEX' => [ 'logging' => 'PRIMARY' ] ],
289 [ 'user' => [ 'JOIN', 'user_id = log_user' ] ] + $commentQuery['joins']
290 );
291 $this->outputLogStream( $result );
292 if ( $this->buffer == self::STREAM ) {
293 $this->db->bufferResults( $prev );
294 }
295 } catch ( Exception $e ) {
296 // Throwing the exception does not reliably free the resultset, and
297 // would also leave the connection in unbuffered mode.
298
299 // Freeing result
300 try {
301 if ( $result ) {
302 $result->free();
303 }
304 } catch ( Exception $e2 ) {
305 // Already in panic mode -> ignoring $e2 as $e has
306 // higher priority
307 }
308
309 // Putting database back in previous buffer mode
310 try {
311 if ( $this->buffer == self::STREAM ) {
312 $this->db->bufferResults( $prev );
313 }
314 } catch ( Exception $e2 ) {
315 // Already in panic mode -> ignoring $e2 as $e has
316 // higher priority
317 }
318
319 // Inform caller about problem
320 throw $e;
321 }
322 # For page dumps...
323 } else {
324 $tables = [ 'page', 'revision' ];
325 $opts = [ 'ORDER BY' => 'page_id ASC' ];
326 $opts['USE INDEX'] = [];
327 $join = [];
328 if ( is_array( $this->history ) ) {
329 # Time offset/limit for all pages/history...
330 $revJoin = 'page_id=rev_page';
331 # Set time order
332 if ( $this->history['dir'] == 'asc' ) {
333 $op = '>';
334 $opts['ORDER BY'] = 'rev_timestamp ASC';
335 } else {
336 $op = '<';
337 $opts['ORDER BY'] = 'rev_timestamp DESC';
338 }
339 # Set offset
340 if ( !empty( $this->history['offset'] ) ) {
341 $revJoin .= " AND rev_timestamp $op " .
342 $this->db->addQuotes( $this->db->timestamp( $this->history['offset'] ) );
343 }
344 $join['revision'] = [ 'INNER JOIN', $revJoin ];
345 # Set query limit
346 if ( !empty( $this->history['limit'] ) ) {
347 $opts['LIMIT'] = intval( $this->history['limit'] );
348 }
349 } elseif ( $this->history & self::FULL ) {
350 # Full history dumps...
351 # query optimization for history stub dumps
352 if ( $this->text == self::STUB && $orderRevs ) {
353 $tables = [ 'revision', 'page' ];
354 $opts[] = 'STRAIGHT_JOIN';
355 $opts['ORDER BY'] = [ 'rev_page ASC', 'rev_id ASC' ];
356 $opts['USE INDEX']['revision'] = 'rev_page_id';
357 $join['page'] = [ 'INNER JOIN', 'rev_page=page_id' ];
358 } else {
359 $join['revision'] = [ 'INNER JOIN', 'page_id=rev_page' ];
360 }
361 } elseif ( $this->history & self::CURRENT ) {
362 # Latest revision dumps...
363 if ( $this->list_authors && $cond != '' ) { // List authors, if so desired
364 $this->do_list_authors( $cond );
365 }
366 $join['revision'] = [ 'INNER JOIN', 'page_id=rev_page AND page_latest=rev_id' ];
367 } elseif ( $this->history & self::STABLE ) {
368 # "Stable" revision dumps...
369 # Default JOIN, to be overridden...
370 $join['revision'] = [ 'INNER JOIN', 'page_id=rev_page AND page_latest=rev_id' ];
371 # One, and only one hook should set this, and return false
372 if ( Hooks::run( 'WikiExporter::dumpStableQuery', [ &$tables, &$opts, &$join ] ) ) {
373 throw new MWException( __METHOD__ . " given invalid history dump type." );
374 }
375 } elseif ( $this->history & self::RANGE ) {
376 # Dump of revisions within a specified range
377 $join['revision'] = [ 'INNER JOIN', 'page_id=rev_page' ];
378 $opts['ORDER BY'] = [ 'rev_page ASC', 'rev_id ASC' ];
379 } else {
380 # Unknown history specification parameter?
381 throw new MWException( __METHOD__ . " given invalid history dump type." );
382 }
383 # Query optimization hacks
384 if ( $cond == '' ) {
385 $opts[] = 'STRAIGHT_JOIN';
386 $opts['USE INDEX']['page'] = 'PRIMARY';
387 }
388 # Build text join options
389 if ( $this->text != self::STUB ) { // 1-pass
390 $tables[] = 'text';
391 $join['text'] = [ 'INNER JOIN', 'rev_text_id=old_id' ];
392 }
393
394 if ( $this->buffer == self::STREAM ) {
395 $prev = $this->db->bufferResults( false );
396 }
397 $result = null; // Assuring $result is not undefined, if exception occurs early
398 try {
399 Hooks::run( 'ModifyExportQuery',
400 [ $this->db, &$tables, &$cond, &$opts, &$join ] );
401
402 $commentQuery = CommentStore::newKey( 'rev_comment' )->getJoin();
403
404 # Do the query!
405 $result = $this->db->select(
406 $tables + $commentQuery['tables'],
407 [ '*' ] + $commentQuery['fields'],
408 $cond,
409 __METHOD__,
410 $opts,
411 $join + $commentQuery['joins']
412 );
413 # Output dump results
414 $this->outputPageStream( $result );
415
416 if ( $this->buffer == self::STREAM ) {
417 $this->db->bufferResults( $prev );
418 }
419 } catch ( Exception $e ) {
420 // Throwing the exception does not reliably free the resultset, and
421 // would also leave the connection in unbuffered mode.
422
423 // Freeing result
424 try {
425 if ( $result ) {
426 $result->free();
427 }
428 } catch ( Exception $e2 ) {
429 // Already in panic mode -> ignoring $e2 as $e has
430 // higher priority
431 }
432
433 // Putting database back in previous buffer mode
434 try {
435 if ( $this->buffer == self::STREAM ) {
436 $this->db->bufferResults( $prev );
437 }
438 } catch ( Exception $e2 ) {
439 // Already in panic mode -> ignoring $e2 as $e has
440 // higher priority
441 }
442
443 // Inform caller about problem
444 throw $e;
445 }
446 }
447 }
448
461 protected function outputPageStream( $resultset ) {
462 $last = null;
463 foreach ( $resultset as $row ) {
464 if ( $last === null ||
465 $last->page_namespace != $row->page_namespace ||
466 $last->page_title != $row->page_title ) {
467 if ( $last !== null ) {
468 $output = '';
469 if ( $this->dumpUploads ) {
470 $output .= $this->writer->writeUploads( $last, $this->dumpUploadFileContents );
471 }
472 $output .= $this->writer->closePage();
473 $this->sink->writeClosePage( $output );
474 }
475 $output = $this->writer->openPage( $row );
476 $this->sink->writeOpenPage( $row, $output );
477 $last = $row;
478 }
479 $output = $this->writer->writeRevision( $row );
480 $this->sink->writeRevision( $row, $output );
481 }
482 if ( $last !== null ) {
483 $output = '';
484 if ( $this->dumpUploads ) {
485 $output .= $this->writer->writeUploads( $last, $this->dumpUploadFileContents );
486 }
488 $output .= $this->writer->closePage();
489 $this->sink->writeClosePage( $output );
490 }
491 }
492
496 protected function outputLogStream( $resultset ) {
497 foreach ( $resultset as $row ) {
498 $output = $this->writer->writeLogItem( $row );
499 $this->sink->writeLogItem( $row, $output );
500 }
501 }
502}
static newKey( $key)
Static constructor for easier chaining.
static getExcludeClause( $db, $audience='public', User $user=null)
SQL clause to skip forbidden log types for this user.
MediaWiki exception.
const DELETED_USER
Definition Revision.php:92
revsByRange( $start, $end)
Dumps a series of page and revision records for those pages in the database with revisions falling wi...
dumpFrom( $cond='', $orderRevs=false)
pageByName( $name)
pagesByName( $names)
setOutputSink(&$sink)
Set the DumpOutput or DumpFilter object which will receive various row objects and XML output for fil...
pageByTitle( $title)
pagesByRange( $start, $end, $orderRevs)
Dumps a series of page and revision records for those pages in the database falling within the page_i...
bool $dumpUploadFileContents
outputPageStream( $resultset)
Runs through a query result set dumping page and revision records.
DumpOutput $sink
allPages()
Dumps a series of page and revision records for all pages in the database, either including complete ...
logsByRange( $start, $end)
do_list_authors( $cond)
Generates the distinct list of authors of an article Not called by default (depends on $this->list_au...
outputLogStream( $resultset)
static schemaVersion()
Returns the export schema version.
__construct( $db, $history=self::CURRENT, $buffer=self::BUFFER, $text=self::TEXT)
If using WikiExporter::STREAM to stream a large amount of data, provide a database connection which i...
string $author_list
bool $list_authors
Return distinct author list (when not returning full history)
Result wrapper for grabbing data queried from an IDatabase object.
$res
Definition database.txt:21
An extension writer
Definition hooks.txt:51
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title after the basic globals have been set but before ordinary actions take place $output
Definition hooks.txt:2225
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist & $tables
Definition hooks.txt:1013
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc next in line in page history
Definition hooks.txt:1760
returning false will NOT prevent logging $e
Definition hooks.txt:2146
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:40
$last