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 Wikimedia\Rdbms\ResultWrapper
;
31 use Wikimedia\Rdbms\IDatabase
;
34 * @ingroup SpecialPage Dump
37 /** @var bool Return distinct author list (when not returning full history) */
38 public $list_authors = false;
41 public $dumpUploads = false;
44 public $dumpUploadFileContents = false;
47 public $author_list = "";
51 const STABLE
= 4; // extension defined
58 const BATCH_SIZE
= 50000;
63 /** @var DumpOutput */
66 /** @var XmlDumpWriter */
70 * Returns the default export schema version, as defined by $wgXmlDumpSchemaVersion.
73 public static function schemaVersion() {
74 global $wgXmlDumpSchemaVersion;
75 return $wgXmlDumpSchemaVersion;
79 * @param IDatabase $db
80 * @param int|array $history One of WikiExporter::FULL, WikiExporter::CURRENT,
81 * WikiExporter::RANGE or WikiExporter::STABLE, or an associative array:
82 * - offset: non-inclusive offset at which to start the query
83 * - limit: maximum number of rows to return
84 * - dir: "asc" or "desc" timestamp order
85 * @param int $text One of WikiExporter::TEXT or WikiExporter::STUB
86 * @param null|array $limitNamespaces Comma-separated list of namespace numbers
89 function __construct( $db, $history = self
::CURRENT
, $text = self
::TEXT
,
90 $limitNamespaces = null
93 $this->history
= $history;
94 $this->writer
= new XmlDumpWriter( $text, self
::schemaVersion() );
95 $this->sink
= new DumpOutput();
97 $this->limitNamespaces
= $limitNamespaces;
101 * @param string $schemaVersion which schema version the generated XML should comply to.
102 * One of the values from self::$supportedSchemas, using the XML_DUMP_SCHEMA_VERSION_XX
105 public function setSchemaVersion( $schemaVersion ) {
106 $this->writer
= new XmlDumpWriter( $this->text
, $schemaVersion );
110 * Set the DumpOutput or DumpFilter object which will receive
111 * various row objects and XML output for filtering. Filters
112 * can be chained or used as callbacks.
114 * @param DumpOutput &$sink
116 public function setOutputSink( &$sink ) {
117 $this->sink
=& $sink;
120 public function openStream() {
121 $output = $this->writer
->openStream();
122 $this->sink
->writeOpenStream( $output );
125 public function closeStream() {
126 $output = $this->writer
->closeStream();
127 $this->sink
->writeCloseStream( $output );
131 * Dumps a series of page and revision records for all pages
132 * in the database, either including complete history or only
133 * the most recent version.
135 public function allPages() {
136 $this->dumpFrom( '' );
140 * Dumps a series of page and revision records for those pages
141 * in the database falling within the page_id range given.
142 * @param int $start Inclusive lower limit (this id is included)
143 * @param int $end Exclusive upper limit (this id is not included)
144 * If 0, no upper limit.
145 * @param bool $orderRevs order revisions within pages in ascending order
147 public function pagesByRange( $start, $end, $orderRevs ) {
149 $condition = 'rev_page >= ' . intval( $start );
151 $condition .= ' AND rev_page < ' . intval( $end );
154 $condition = 'page_id >= ' . intval( $start );
156 $condition .= ' AND page_id < ' . intval( $end );
159 $this->dumpFrom( $condition, $orderRevs );
163 * Dumps a series of page and revision records for those pages
164 * in the database with revisions falling within the rev_id range given.
165 * @param int $start Inclusive lower limit (this id is included)
166 * @param int $end Exclusive upper limit (this id is not included)
167 * If 0, no upper limit.
169 public function revsByRange( $start, $end ) {
170 $condition = 'rev_id >= ' . intval( $start );
172 $condition .= ' AND rev_id < ' . intval( $end );
174 $this->dumpFrom( $condition );
178 * @param Title $title
180 public function pageByTitle( $title ) {
182 'page_namespace=' . $title->getNamespace() .
183 ' AND page_title=' . $this->db
->addQuotes( $title->getDBkey() ) );
187 * @param string $name
188 * @throws MWException
190 public function pageByName( $name ) {
191 $title = Title
::newFromText( $name );
192 if ( is_null( $title ) ) {
193 throw new MWException( "Can't export invalid title" );
195 $this->pageByTitle( $title );
200 * @param array $names
202 public function pagesByName( $names ) {
203 foreach ( $names as $name ) {
204 $this->pageByName( $name );
208 public function allLogs() {
209 $this->dumpFrom( '' );
216 public function logsByRange( $start, $end ) {
217 $condition = 'log_id >= ' . intval( $start );
219 $condition .= ' AND log_id < ' . intval( $end );
221 $this->dumpFrom( $condition );
225 * Generates the distinct list of authors of an article
226 * Not called by default (depends on $this->list_authors)
227 * Can be set by Special:Export when not exporting whole history
231 protected function do_list_authors( $cond ) {
232 $this->author_list
= "<contributors>";
235 $revQuery = Revision
::getQueryInfo( [ 'page' ] );
236 $res = $this->db
->select(
239 'rev_user_text' => $revQuery['fields']['rev_user_text'],
240 'rev_user' => $revQuery['fields']['rev_user'],
243 $this->db
->bitAnd( 'rev_deleted', Revision
::DELETED_USER
) . ' = 0',
251 foreach ( $res as $row ) {
252 $this->author_list
.= "<contributor>" .
254 htmlspecialchars( $row->rev_user_text
) .
257 ( (int)$row->rev_user
) .
261 $this->author_list
.= "</contributors>";
265 * @param string $cond
266 * @param bool $orderRevs
267 * @throws MWException
270 protected function dumpFrom( $cond = '', $orderRevs = false ) {
271 if ( $this->history
& self
::LOGS
) {
272 $this->dumpLogs( $cond );
274 $this->dumpPages( $cond, $orderRevs );
279 * @param string $cond
282 protected function dumpLogs( $cond ) {
285 $hideLogs = LogEventsList
::getExcludeClause( $this->db
);
287 $where[] = $hideLogs;
289 # Add on any caller specified conditions
293 # Get logging table name for logging.* clause
294 $logging = $this->db
->tableName( 'logging' );
296 $result = null; // Assuring $result is not undefined, if exception occurs early
298 $commentQuery = CommentStore
::getStore()->getJoin( 'log_comment' );
299 $actorQuery = ActorMigration
::newMigration()->getJoin( 'log_user' );
303 $result = $this->db
->select(
304 array_merge( [ 'logging' ], $commentQuery['tables'], $actorQuery['tables'], [ 'user' ] ),
305 [ "{$logging}.*", 'user_name' ] +
$commentQuery['fields'] +
$actorQuery['fields'],
306 array_merge( $where, [ 'log_id > ' . intval( $lastLogId ) ] ),
309 'ORDER BY' => 'log_id',
310 'USE INDEX' => [ 'logging' => 'PRIMARY' ],
311 'LIMIT' => self
::BATCH_SIZE
,
314 'user' => [ 'JOIN', 'user_id = ' . $actorQuery['fields']['log_user'] ]
315 ] +
$commentQuery['joins'] +
$actorQuery['joins']
318 if ( !$result->numRows() ) {
322 $lastLogId = $this->outputLogStream( $result );
327 * @param string $cond
328 * @param bool $orderRevs
329 * @throws MWException
332 protected function dumpPages( $cond, $orderRevs ) {
333 global $wgMultiContentRevisionSchemaMigrationStage;
334 if ( !( $wgMultiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD
) ) {
335 // TODO: Make XmlDumpWriter use a RevisionStore! (see T198706 and T174031)
336 throw new MWException(
337 'Cannot use WikiExporter with SCHEMA_COMPAT_WRITE_OLD mode disabled!'
338 . ' Support for dumping from the new schema is not implemented yet!'
342 $revOpts = [ 'page' ];
344 $revQuery = Revision
::getQueryInfo( $revOpts );
346 // We want page primary rather than revision
347 $tables = array_merge( [ 'page' ], array_diff( $revQuery['tables'], [ 'page' ] ) );
348 $join = $revQuery['joins'] +
[
349 'revision' => $revQuery['joins']['page']
351 unset( $join['page'] );
353 $fields = $revQuery['fields'];
354 $fields[] = 'page_restrictions';
356 if ( $this->text
!= self
::STUB
) {
357 $fields['_load_content'] = '1';
361 if ( $cond !== '' ) {
364 $opts = [ 'ORDER BY' => [ 'rev_page ASC', 'rev_id ASC' ] ];
365 $opts['USE INDEX'] = [];
368 if ( is_array( $this->history
) ) {
369 # Time offset/limit for all pages/history...
371 if ( $this->history
['dir'] == 'asc' ) {
372 $opts['ORDER BY'] = 'rev_timestamp ASC';
375 $opts['ORDER BY'] = 'rev_timestamp DESC';
378 if ( !empty( $this->history
['offset'] ) ) {
379 $conds[] = "rev_timestamp $op " .
380 $this->db
->addQuotes( $this->db
->timestamp( $this->history
['offset'] ) );
383 if ( !empty( $this->history
['limit'] ) ) {
384 $maxRowCount = intval( $this->history
['limit'] );
386 } elseif ( $this->history
& self
::FULL
) {
387 # Full history dumps...
388 # query optimization for history stub dumps
389 if ( $this->text
== self
::STUB
) {
390 $tables = $revQuery['tables'];
391 $opts[] = 'STRAIGHT_JOIN';
392 $opts['USE INDEX']['revision'] = 'rev_page_id';
393 unset( $join['revision'] );
394 $join['page'] = [ 'JOIN', 'rev_page=page_id' ];
396 } elseif ( $this->history
& self
::CURRENT
) {
397 # Latest revision dumps...
398 if ( $this->list_authors
&& $cond != '' ) { // List authors, if so desired
399 $this->do_list_authors( $cond );
401 $join['revision'] = [ 'JOIN', 'page_id=rev_page AND page_latest=rev_id' ];
402 $opts[ 'ORDER BY' ] = [ 'page_id ASC' ];
403 } elseif ( $this->history
& self
::STABLE
) {
404 # "Stable" revision dumps...
405 # Default JOIN, to be overridden...
406 $join['revision'] = [ 'JOIN', 'page_id=rev_page AND page_latest=rev_id' ];
407 # One, and only one hook should set this, and return false
408 if ( Hooks
::run( 'WikiExporter::dumpStableQuery', [ &$tables, &$opts, &$join ] ) ) {
409 throw new MWException( __METHOD__
. " given invalid history dump type." );
411 } elseif ( $this->history
& self
::RANGE
) {
412 # Dump of revisions within a specified range. Condition already set in revsByRange().
414 # Unknown history specification parameter?
415 throw new MWException( __METHOD__
. " given invalid history dump type." );
418 $result = null; // Assuring $result is not undefined, if exception occurs early
425 $opts['LIMIT'] = self
::BATCH_SIZE
;
427 Hooks
::run( 'ModifyExportQuery',
428 [ $this->db
, &$tables, &$cond, &$opts, &$join ] );
431 // If necessary, impose the overall maximum and stop looping after this iteration.
432 if ( !empty( $maxRowCount ) && $rowCount + self
::BATCH_SIZE
> $maxRowCount ) {
433 $opts['LIMIT'] = $maxRowCount - $rowCount;
437 $queryConds = $conds;
438 $queryConds[] = 'rev_page>' . intval( $revPage ) . ' OR (rev_page=' .
439 intval( $revPage ) . ' AND rev_id' . $op . intval( $revId ) . ')';
441 # Do the query and process any results, remembering max ids for the next iteration.
442 $result = $this->db
->select(
450 if ( $result->numRows() > 0 ) {
451 $lastRow = $this->outputPageStreamBatch( $result, $lastRow );
452 $rowCount +
= $result->numRows();
453 $revPage = $lastRow->rev_page
;
454 $revId = $lastRow->rev_id
;
459 // If we are finished, close off final page element (if any).
460 if ( $done && $lastRow ) {
461 $this->finishPageStreamOutput( $lastRow );
467 * Runs through a query result set dumping page and revision records.
468 * The result set should be sorted/grouped by page to avoid duplicate
469 * page records in the output.
471 * @param ResultWrapper $results
472 * @param object $lastRow the last row output from the previous call (or null if none)
473 * @return object the last row processed
475 protected function outputPageStreamBatch( $results, $lastRow ) {
476 foreach ( $results as $row ) {
477 if ( $this->limitNamespaces
&&
478 !in_array( $row->page_namespace
, $this->limitNamespaces
) ) {
482 if ( $lastRow === null ||
483 $lastRow->page_namespace
!== $row->page_namespace ||
484 $lastRow->page_title
!== $row->page_title
) {
485 if ( $lastRow !== null ) {
487 if ( $this->dumpUploads
) {
488 $output .= $this->writer
->writeUploads( $lastRow, $this->dumpUploadFileContents
);
490 $output .= $this->writer
->closePage();
491 $this->sink
->writeClosePage( $output );
493 $output = $this->writer
->openPage( $row );
494 $this->sink
->writeOpenPage( $row, $output );
496 $output = $this->writer
->writeRevision( $row );
497 $this->sink
->writeRevision( $row, $output );
505 * Final page stream output, after all batches are complete
507 * @param object $lastRow the last row output from the last batch (or null if none)
509 protected function finishPageStreamOutput( $lastRow ) {
511 if ( $this->dumpUploads
) {
512 $output .= $this->writer
->writeUploads( $lastRow, $this->dumpUploadFileContents
);
514 $output .= $this->author_list
;
515 $output .= $this->writer
->closePage();
516 $this->sink
->writeClosePage( $output );
520 * @param ResultWrapper $resultset
521 * @return int the log_id value of the last item output, or null if none
523 protected function outputLogStream( $resultset ) {
524 foreach ( $resultset as $row ) {
525 $output = $this->writer
->writeLogItem( $row );
526 $this->sink
->writeLogItem( $row, $output );
528 return isset( $row ) ?
$row->log_id
: null;