MediaWiki REL1_31
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 $revQuery = Revision::getQueryInfo( [ 'page' ] );
231 $res = $this->db->select(
232 $revQuery['tables'],
233 [
234 'rev_user_text' => $revQuery['fields']['rev_user_text'],
235 'rev_user' => $revQuery['fields']['rev_user'],
236 ],
237 [
238 $this->db->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0',
239 $cond,
240 ],
241 __METHOD__,
242 [ 'DISTINCT' ],
243 $revQuery['joins']
244 );
245
246 foreach ( $res as $row ) {
247 $this->author_list .= "<contributor>" .
248 "<username>" .
249 htmlentities( $row->rev_user_text ) .
250 "</username>" .
251 "<id>" .
252 $row->rev_user .
253 "</id>" .
254 "</contributor>";
255 }
256 $this->author_list .= "</contributors>";
257 }
258
265 protected function dumpFrom( $cond = '', $orderRevs = false ) {
266 # For logging dumps...
267 if ( $this->history & self::LOGS ) {
268 $where = [];
269 # Hide private logs
270 $hideLogs = LogEventsList::getExcludeClause( $this->db );
271 if ( $hideLogs ) {
272 $where[] = $hideLogs;
273 }
274 # Add on any caller specified conditions
275 if ( $cond ) {
276 $where[] = $cond;
277 }
278 # Get logging table name for logging.* clause
279 $logging = $this->db->tableName( 'logging' );
280
281 if ( $this->buffer == self::STREAM ) {
282 $prev = $this->db->bufferResults( false );
283 }
284 $result = null; // Assuring $result is not undefined, if exception occurs early
285
286 $commentQuery = CommentStore::getStore()->getJoin( 'log_comment' );
287 $actorQuery = ActorMigration::newMigration()->getJoin( 'log_user' );
288
289 try {
290 $result = $this->db->select(
291 array_merge( [ 'logging' ], $commentQuery['tables'], $actorQuery['tables'], [ 'user' ] ),
292 [ "{$logging}.*", 'user_name' ] + $commentQuery['fields'] + $actorQuery['fields'],
293 $where,
294 __METHOD__,
295 [ 'ORDER BY' => 'log_id', 'USE INDEX' => [ 'logging' => 'PRIMARY' ] ],
296 [
297 'user' => [ 'JOIN', 'user_id = ' . $actorQuery['fields']['log_user'] ]
298 ] + $commentQuery['joins'] + $actorQuery['joins']
299 );
300 $this->outputLogStream( $result );
301 if ( $this->buffer == self::STREAM ) {
302 $this->db->bufferResults( $prev );
303 }
304 } catch ( Exception $e ) {
305 // Throwing the exception does not reliably free the resultset, and
306 // would also leave the connection in unbuffered mode.
307
308 // Freeing result
309 try {
310 if ( $result ) {
311 $result->free();
312 }
313 } catch ( Exception $e2 ) {
314 // Already in panic mode -> ignoring $e2 as $e has
315 // higher priority
316 }
317
318 // Putting database back in previous buffer mode
319 try {
320 if ( $this->buffer == self::STREAM ) {
321 $this->db->bufferResults( $prev );
322 }
323 } catch ( Exception $e2 ) {
324 // Already in panic mode -> ignoring $e2 as $e has
325 // higher priority
326 }
327
328 // Inform caller about problem
329 throw $e;
330 }
331 # For page dumps...
332 } else {
333 $revOpts = [ 'page' ];
334 if ( $this->text != self::STUB ) {
335 $revOpts[] = 'text';
336 }
337 $revQuery = Revision::getQueryInfo( $revOpts );
338
339 // We want page primary rather than revision
340 $tables = array_merge( [ 'page' ], array_diff( $revQuery['tables'], [ 'page' ] ) );
341 $join = $revQuery['joins'] + [
342 'revision' => $revQuery['joins']['page']
343 ];
344 unset( $join['page'] );
345
346 $fields = array_merge( $revQuery['fields'], [ 'page_restrictions' ] );
347
348 $conds = [];
349 if ( $cond !== '' ) {
350 $conds[] = $cond;
351 }
352 $opts = [ 'ORDER BY' => 'page_id ASC' ];
353 $opts['USE INDEX'] = [];
354 if ( is_array( $this->history ) ) {
355 # Time offset/limit for all pages/history...
356 # Set time order
357 if ( $this->history['dir'] == 'asc' ) {
358 $op = '>';
359 $opts['ORDER BY'] = 'rev_timestamp ASC';
360 } else {
361 $op = '<';
362 $opts['ORDER BY'] = 'rev_timestamp DESC';
363 }
364 # Set offset
365 if ( !empty( $this->history['offset'] ) ) {
366 $conds[] = "rev_timestamp $op " .
367 $this->db->addQuotes( $this->db->timestamp( $this->history['offset'] ) );
368 }
369 # Set query limit
370 if ( !empty( $this->history['limit'] ) ) {
371 $opts['LIMIT'] = intval( $this->history['limit'] );
372 }
373 } elseif ( $this->history & self::FULL ) {
374 # Full history dumps...
375 # query optimization for history stub dumps
376 if ( $this->text == self::STUB && $orderRevs ) {
377 $tables = $revQuery['tables'];
378 $opts['ORDER BY'] = [ 'rev_page ASC', 'rev_id ASC' ];
379 $opts['USE INDEX']['revision'] = 'rev_page_id';
380 unset( $join['revision'] );
381 $join['page'] = [ 'INNER JOIN', 'rev_page=page_id' ];
382 }
383 } elseif ( $this->history & self::CURRENT ) {
384 # Latest revision dumps...
385 if ( $this->list_authors && $cond != '' ) { // List authors, if so desired
386 $this->do_list_authors( $cond );
387 }
388 $join['revision'] = [ 'INNER JOIN', 'page_id=rev_page AND page_latest=rev_id' ];
389 } elseif ( $this->history & self::STABLE ) {
390 # "Stable" revision dumps...
391 # Default JOIN, to be overridden...
392 $join['revision'] = [ 'INNER JOIN', 'page_id=rev_page AND page_latest=rev_id' ];
393 # One, and only one hook should set this, and return false
394 if ( Hooks::run( 'WikiExporter::dumpStableQuery', [ &$tables, &$opts, &$join ] ) ) {
395 throw new MWException( __METHOD__ . " given invalid history dump type." );
396 }
397 } elseif ( $this->history & self::RANGE ) {
398 # Dump of revisions within a specified range
399 $opts['ORDER BY'] = [ 'rev_page ASC', 'rev_id ASC' ];
400 } else {
401 # Unknown history specification parameter?
402 throw new MWException( __METHOD__ . " given invalid history dump type." );
403 }
404
405 if ( $this->buffer == self::STREAM ) {
406 $prev = $this->db->bufferResults( false );
407 }
408 $result = null; // Assuring $result is not undefined, if exception occurs early
409 try {
410 Hooks::run( 'ModifyExportQuery',
411 [ $this->db, &$tables, &$cond, &$opts, &$join ] );
412
413 # Do the query!
414 $result = $this->db->select(
415 $tables,
416 $fields,
417 $conds,
418 __METHOD__,
419 $opts,
420 $join
421 );
422 # Output dump results
423 $this->outputPageStream( $result );
424
425 if ( $this->buffer == self::STREAM ) {
426 $this->db->bufferResults( $prev );
427 }
428 } catch ( Exception $e ) {
429 // Throwing the exception does not reliably free the resultset, and
430 // would also leave the connection in unbuffered mode.
431
432 // Freeing result
433 try {
434 if ( $result ) {
435 $result->free();
436 }
437 } catch ( Exception $e2 ) {
438 // Already in panic mode -> ignoring $e2 as $e has
439 // higher priority
440 }
441
442 // Putting database back in previous buffer mode
443 try {
444 if ( $this->buffer == self::STREAM ) {
445 $this->db->bufferResults( $prev );
446 }
447 } catch ( Exception $e2 ) {
448 // Already in panic mode -> ignoring $e2 as $e has
449 // higher priority
450 }
451
452 // Inform caller about problem
453 throw $e;
454 }
455 }
456 }
457
470 protected function outputPageStream( $resultset ) {
471 $last = null;
472 foreach ( $resultset as $row ) {
473 if ( $last === null ||
474 $last->page_namespace != $row->page_namespace ||
475 $last->page_title != $row->page_title ) {
476 if ( $last !== null ) {
477 $output = '';
478 if ( $this->dumpUploads ) {
479 $output .= $this->writer->writeUploads( $last, $this->dumpUploadFileContents );
480 }
481 $output .= $this->writer->closePage();
482 $this->sink->writeClosePage( $output );
483 }
484 $output = $this->writer->openPage( $row );
485 $this->sink->writeOpenPage( $row, $output );
486 $last = $row;
487 }
488 $output = $this->writer->writeRevision( $row );
489 $this->sink->writeRevision( $row, $output );
490 }
491 if ( $last !== null ) {
492 $output = '';
493 if ( $this->dumpUploads ) {
494 $output .= $this->writer->writeUploads( $last, $this->dumpUploadFileContents );
495 }
496 $output .= $this->author_list;
497 $output .= $this->writer->closePage();
498 $this->sink->writeClosePage( $output );
499 }
500 }
501
505 protected function outputLogStream( $resultset ) {
506 foreach ( $resultset as $row ) {
507 $output = $this->writer->writeLogItem( $row );
508 $this->sink->writeLogItem( $row, $output );
509 }
510 }
511}
static getExcludeClause( $db, $audience='public', User $user=null)
SQL clause to skip forbidden log types for this user.
MediaWiki exception.
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:2255
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:1015
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:1777
returning false will NOT prevent logging $e
Definition hooks.txt:2176
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
$buffer
$last