3 * Base class for exporting
5 * Copyright © 2003, 2005, 2006 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
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.
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.
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
30 use MediaWiki\MediaWikiServices
as MediaWikiServicesAlias
;
31 use MediaWiki\Storage\RevisionRecord
;
32 use Wikimedia\Rdbms\IResultWrapper
;
35 * @ingroup SpecialPage Dump
38 /** @var bool Return distinct author list (when not returning full history) */
39 public $list_authors = false;
42 public $dumpUploads = false;
45 public $dumpUploadFileContents = false;
48 public $author_list = "";
52 const STABLE
= 4; // extension defined
56 const TEXT
= XmlDumpWriter
::WRITE_CONTENT
;
57 const STUB
= XmlDumpWriter
::WRITE_STUB
;
59 const BATCH_SIZE
= 50000;
64 /** @var DumpOutput */
67 /** @var XmlDumpWriter */
76 /** @var array|null */
77 protected $limitNamespaces;
80 * Returns the default export schema version, as defined by $wgXmlDumpSchemaVersion.
83 public static function schemaVersion() {
84 global $wgXmlDumpSchemaVersion;
85 return $wgXmlDumpSchemaVersion;
90 * @param int|array $history One of WikiExporter::FULL, WikiExporter::CURRENT,
91 * WikiExporter::RANGE or WikiExporter::STABLE, or an associative array:
92 * - offset: non-inclusive offset at which to start the query
93 * - limit: maximum number of rows to return
94 * - dir: "asc" or "desc" timestamp order
95 * @param int $text One of WikiExporter::TEXT or WikiExporter::STUB
96 * @param null|array $limitNamespaces Comma-separated list of namespace numbers
101 $history = self
::CURRENT
,
103 $limitNamespaces = null
106 $this->history
= $history;
107 $this->writer
= new XmlDumpWriter( $text, self
::schemaVersion() );
108 $this->sink
= new DumpOutput();
110 $this->limitNamespaces
= $limitNamespaces;
114 * @param string $schemaVersion which schema version the generated XML should comply to.
115 * One of the values from self::$supportedSchemas, using the XML_DUMP_SCHEMA_VERSION_XX
118 public function setSchemaVersion( $schemaVersion ) {
119 $this->writer
= new XmlDumpWriter( $this->text
, $schemaVersion );
123 * Set the DumpOutput or DumpFilter object which will receive
124 * various row objects and XML output for filtering. Filters
125 * can be chained or used as callbacks.
127 * @param DumpOutput|DumpFilter &$sink
129 public function setOutputSink( &$sink ) {
130 $this->sink
=& $sink;
133 public function openStream() {
134 $output = $this->writer
->openStream();
135 $this->sink
->writeOpenStream( $output );
138 public function closeStream() {
139 $output = $this->writer
->closeStream();
140 $this->sink
->writeCloseStream( $output );
144 * Dumps a series of page and revision records for all pages
145 * in the database, either including complete history or only
146 * the most recent version.
148 public function allPages() {
149 $this->dumpFrom( '' );
153 * Dumps a series of page and revision records for those pages
154 * in the database falling within the page_id range given.
155 * @param int $start Inclusive lower limit (this id is included)
156 * @param int $end Exclusive upper limit (this id is not included)
157 * If 0, no upper limit.
158 * @param bool $orderRevs order revisions within pages in ascending order
160 public function pagesByRange( $start, $end, $orderRevs ) {
162 $condition = 'rev_page >= ' . intval( $start );
164 $condition .= ' AND rev_page < ' . intval( $end );
167 $condition = 'page_id >= ' . intval( $start );
169 $condition .= ' AND page_id < ' . intval( $end );
172 $this->dumpFrom( $condition, $orderRevs );
176 * Dumps a series of page and revision records for those pages
177 * in the database with revisions falling within the rev_id range given.
178 * @param int $start Inclusive lower limit (this id is included)
179 * @param int $end Exclusive upper limit (this id is not included)
180 * If 0, no upper limit.
182 public function revsByRange( $start, $end ) {
183 $condition = 'rev_id >= ' . intval( $start );
185 $condition .= ' AND rev_id < ' . intval( $end );
187 $this->dumpFrom( $condition );
191 * @param Title $title
193 public function pageByTitle( $title ) {
195 'page_namespace=' . $title->getNamespace() .
196 ' AND page_title=' . $this->db
->addQuotes( $title->getDBkey() ) );
200 * @param string $name
201 * @throws MWException
203 public function pageByName( $name ) {
204 $title = Title
::newFromText( $name );
205 if ( is_null( $title ) ) {
206 throw new MWException( "Can't export invalid title" );
208 $this->pageByTitle( $title );
213 * @param array $names
215 public function pagesByName( $names ) {
216 foreach ( $names as $name ) {
217 $this->pageByName( $name );
221 public function allLogs() {
222 $this->dumpFrom( '' );
229 public function logsByRange( $start, $end ) {
230 $condition = 'log_id >= ' . intval( $start );
232 $condition .= ' AND log_id < ' . intval( $end );
234 $this->dumpFrom( $condition );
238 * Generates the distinct list of authors of an article
239 * Not called by default (depends on $this->list_authors)
240 * Can be set by Special:Export when not exporting whole history
242 * @param string $cond
244 protected function do_list_authors( $cond ) {
245 $this->author_list
= "<contributors>";
248 $revQuery = Revision
::getQueryInfo( [ 'page' ] );
249 $res = $this->db
->select(
252 'rev_user_text' => $revQuery['fields']['rev_user_text'],
253 'rev_user' => $revQuery['fields']['rev_user'],
256 $this->db
->bitAnd( 'rev_deleted', RevisionRecord
::DELETED_USER
) . ' = 0',
264 foreach ( $res as $row ) {
265 $this->author_list
.= "<contributor>" .
267 htmlspecialchars( $row->rev_user_text
) .
270 ( (int)$row->rev_user
) .
274 $this->author_list
.= "</contributors>";
278 * @param string $cond
279 * @param bool $orderRevs
280 * @throws MWException
283 protected function dumpFrom( $cond = '', $orderRevs = false ) {
284 if ( $this->history
& self
::LOGS
) {
285 $this->dumpLogs( $cond );
287 $this->dumpPages( $cond, $orderRevs );
292 * @param string $cond
295 protected function dumpLogs( $cond ) {
298 $hideLogs = LogEventsList
::getExcludeClause( $this->db
);
300 $where[] = $hideLogs;
302 # Add on any caller specified conditions
306 # Get logging table name for logging.* clause
307 $logging = $this->db
->tableName( 'logging' );
309 $result = null; // Assuring $result is not undefined, if exception occurs early
311 $commentQuery = CommentStore
::getStore()->getJoin( 'log_comment' );
312 $actorQuery = ActorMigration
::newMigration()->getJoin( 'log_user' );
316 $result = $this->db
->select(
317 array_merge( [ 'logging' ], $commentQuery['tables'], $actorQuery['tables'], [ 'user' ] ),
318 [ "{$logging}.*", 'user_name' ] +
$commentQuery['fields'] +
$actorQuery['fields'],
319 array_merge( $where, [ 'log_id > ' . intval( $lastLogId ) ] ),
322 'ORDER BY' => 'log_id',
323 'USE INDEX' => [ 'logging' => 'PRIMARY' ],
324 'LIMIT' => self
::BATCH_SIZE
,
327 'user' => [ 'JOIN', 'user_id = ' . $actorQuery['fields']['log_user'] ]
328 ] +
$commentQuery['joins'] +
$actorQuery['joins']
331 if ( !$result->numRows() ) {
335 $lastLogId = $this->outputLogStream( $result );
340 * @param string $cond
341 * @param bool $orderRevs
342 * @throws MWException
345 protected function dumpPages( $cond, $orderRevs ) {
346 global $wgMultiContentRevisionSchemaMigrationStage;
347 if ( !( $wgMultiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD
) ) {
348 // TODO: Make XmlDumpWriter use a RevisionStore! (see T198706 and T174031)
349 throw new MWException(
350 'Cannot use WikiExporter with SCHEMA_COMPAT_WRITE_OLD mode disabled!'
351 . ' Support for dumping from the new schema is not implemented yet!'
355 $revQuery = MediaWikiServicesAlias
::getInstance()->getRevisionStore()->getQueryInfo(
358 $slotQuery = MediaWikiServicesAlias
::getInstance()->getRevisionStore()->getSlotsQueryInfo(
362 // We want page primary rather than revision.
363 // We also want to join in the slots and content tables.
364 // NOTE: This means we may get multiple rows per revision, and more rows
365 // than the batch size! Should be ok, since the max number of slots is
366 // fixed and low (dozens at worst).
367 $tables = array_merge( [ 'page' ], array_diff( $revQuery['tables'], [ 'page' ] ) );
368 $tables = array_merge( $tables, array_diff( $slotQuery['tables'], $tables ) );
369 $join = $revQuery['joins'] +
[
370 'revision' => $revQuery['joins']['page'],
371 'slots' => [ 'JOIN', [ 'slot_revision_id = rev_id' ] ],
372 'content' => [ 'JOIN', [ 'content_id = slot_content_id' ] ],
374 unset( $join['page'] );
376 $fields = array_merge( $revQuery['fields'], $slotQuery['fields'] );
377 $fields[] = 'page_restrictions';
379 if ( $this->text
!= self
::STUB
) {
380 $fields['_load_content'] = '1';
384 if ( $cond !== '' ) {
387 $opts = [ 'ORDER BY' => [ 'rev_page ASC', 'rev_id ASC' ] ];
388 $opts['USE INDEX'] = [];
391 if ( is_array( $this->history
) ) {
392 # Time offset/limit for all pages/history...
394 if ( $this->history
['dir'] == 'asc' ) {
395 $opts['ORDER BY'] = 'rev_timestamp ASC';
398 $opts['ORDER BY'] = 'rev_timestamp DESC';
401 if ( !empty( $this->history
['offset'] ) ) {
402 $conds[] = "rev_timestamp $op " .
403 $this->db
->addQuotes( $this->db
->timestamp( $this->history
['offset'] ) );
406 if ( !empty( $this->history
['limit'] ) ) {
407 $maxRowCount = intval( $this->history
['limit'] );
409 } elseif ( $this->history
& self
::FULL
) {
410 # Full history dumps...
411 # query optimization for history stub dumps
412 if ( $this->text
== self
::STUB
) {
413 $opts[] = 'STRAIGHT_JOIN';
414 $opts['USE INDEX']['revision'] = 'rev_page_id';
415 unset( $join['revision'] );
416 $join['page'] = [ 'JOIN', 'rev_page=page_id' ];
418 } elseif ( $this->history
& self
::CURRENT
) {
419 # Latest revision dumps...
420 if ( $this->list_authors
&& $cond != '' ) { // List authors, if so desired
421 $this->do_list_authors( $cond );
423 $join['revision'] = [ 'JOIN', 'page_id=rev_page AND page_latest=rev_id' ];
424 $opts[ 'ORDER BY' ] = [ 'page_id ASC' ];
425 } elseif ( $this->history
& self
::STABLE
) {
426 # "Stable" revision dumps...
427 # Default JOIN, to be overridden...
428 $join['revision'] = [ 'JOIN', 'page_id=rev_page AND page_latest=rev_id' ];
429 # One, and only one hook should set this, and return false
430 if ( Hooks
::run( 'WikiExporter::dumpStableQuery', [ &$tables, &$opts, &$join ] ) ) {
431 throw new MWException( __METHOD__
. " given invalid history dump type." );
433 } elseif ( $this->history
& self
::RANGE
) {
434 # Dump of revisions within a specified range. Condition already set in revsByRange().
436 # Unknown history specification parameter?
437 throw new MWException( __METHOD__
. " given invalid history dump type." );
440 $result = null; // Assuring $result is not undefined, if exception occurs early
447 $opts['LIMIT'] = self
::BATCH_SIZE
;
449 Hooks
::run( 'ModifyExportQuery',
450 [ $this->db
, &$tables, &$cond, &$opts, &$join ] );
453 // If necessary, impose the overall maximum and stop looping after this iteration.
454 if ( !empty( $maxRowCount ) && $rowCount + self
::BATCH_SIZE
> $maxRowCount ) {
455 $opts['LIMIT'] = $maxRowCount - $rowCount;
459 $queryConds = $conds;
460 $queryConds[] = 'rev_page>' . intval( $revPage ) . ' OR (rev_page=' .
461 intval( $revPage ) . ' AND rev_id' . $op . intval( $revId ) . ')';
463 # Do the query and process any results, remembering max ids for the next iteration.
464 $result = $this->db
->select(
472 if ( $result->numRows() > 0 ) {
473 $lastRow = $this->outputPageStreamBatch( $result, $lastRow );
474 $rowCount +
= $result->numRows();
475 $revPage = $lastRow->rev_page
;
476 $revId = $lastRow->rev_id
;
481 // If we are finished, close off final page element (if any).
482 if ( $done && $lastRow ) {
483 $this->finishPageStreamOutput( $lastRow );
489 * Runs through a query result set dumping page, revision, and slot records.
490 * The result set should join the page, revision, slots, and content tables,
491 * and be sorted/grouped by page and revision to avoid duplicate page records in the output.
493 * @param IResultWrapper $results
494 * @param object $lastRow the last row output from the previous call (or null if none)
495 * @return object the last row processed
497 protected function outputPageStreamBatch( $results, $lastRow ) {
500 $slotRows = $this->getSlotRowBatch( $results, $rowCarry );
506 // All revision info is present in all slot rows.
507 // Use the first slot row as the revision row.
508 $revRow = $slotRows[0];
510 if ( $this->limitNamespaces
&&
511 !in_array( $revRow->page_namespace
, $this->limitNamespaces
) ) {
516 if ( $lastRow === null ||
517 $lastRow->page_namespace
!== $revRow->page_namespace ||
518 $lastRow->page_title
!== $revRow->page_title
) {
519 if ( $lastRow !== null ) {
521 if ( $this->dumpUploads
) {
522 $output .= $this->writer
->writeUploads( $lastRow, $this->dumpUploadFileContents
);
524 $output .= $this->writer
->closePage();
525 $this->sink
->writeClosePage( $output );
527 $output = $this->writer
->openPage( $revRow );
528 $this->sink
->writeOpenPage( $revRow, $output );
530 $output = $this->writer
->writeRevision( $revRow, $slotRows );
531 $this->sink
->writeRevision( $revRow, $output );
536 throw new LogicException( 'Error while processing a stream of slot rows' );
543 * Returns all slot rows for a revision.
544 * Takes and returns a carry row from the last batch;
546 * @param IResultWrapper|array $results
547 * @param null|object &$carry A row carried over from the last call to getSlotRowBatch()
551 protected function getSlotRowBatch( $results, &$carry = null ) {
556 $slotRows[] = $carry;
561 while ( $row = $results->fetchObject() ) {
562 if ( $prev && $prev->rev_id
!== $row->rev_id
) {
574 * Final page stream output, after all batches are complete
576 * @param object $lastRow the last row output from the last batch (or null if none)
578 protected function finishPageStreamOutput( $lastRow ) {
580 if ( $this->dumpUploads
) {
581 $output .= $this->writer
->writeUploads( $lastRow, $this->dumpUploadFileContents
);
583 $output .= $this->author_list
;
584 $output .= $this->writer
->closePage();
585 $this->sink
->writeClosePage( $output );
589 * @param IResultWrapper $resultset
590 * @return int the log_id value of the last item output, or null if none
592 protected function outputLogStream( $resultset ) {
593 foreach ( $resultset as $row ) {
594 $output = $this->writer
->writeLogItem( $row );
595 $this->sink
->writeLogItem( $row, $output );
597 return isset( $row ) ?
$row->log_id
: null;