Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[lhc/web/wiklou.git] / includes / export / WikiExporter.php
1 <?php
2 /**
3 * Base class for exporting
4 *
5 * Copyright © 2003, 2005, 2006 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 */
25
26 /**
27 * @defgroup Dump Dump
28 */
29
30 use MediaWiki\MediaWikiServices as MediaWikiServicesAlias;
31 use Wikimedia\Rdbms\IResultWrapper;
32 use Wikimedia\Rdbms\IDatabase;
33
34 /**
35 * @ingroup SpecialPage Dump
36 */
37 class WikiExporter {
38 /** @var bool Return distinct author list (when not returning full history) */
39 public $list_authors = false;
40
41 /** @var bool */
42 public $dumpUploads = false;
43
44 /** @var bool */
45 public $dumpUploadFileContents = false;
46
47 /** @var string */
48 public $author_list = "";
49
50 const FULL = 1;
51 const CURRENT = 2;
52 const STABLE = 4; // extension defined
53 const LOGS = 8;
54 const RANGE = 16;
55
56 const TEXT = XmlDumpWriter::WRITE_CONTENT;
57 const STUB = XmlDumpWriter::WRITE_STUB;
58
59 const BATCH_SIZE = 50000;
60
61 /** @var int */
62 public $text;
63
64 /** @var DumpOutput */
65 public $sink;
66
67 /** @var XmlDumpWriter */
68 private $writer;
69
70 /** @var IDatabase */
71 protected $db;
72
73 /** @var array|int */
74 protected $history;
75
76 /** @var array|null */
77 protected $limitNamespaces;
78
79 /**
80 * Returns the default export schema version, as defined by $wgXmlDumpSchemaVersion.
81 * @return string
82 */
83 public static function schemaVersion() {
84 global $wgXmlDumpSchemaVersion;
85 return $wgXmlDumpSchemaVersion;
86 }
87
88 /**
89 * @param IDatabase $db
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
97 * to limit results
98 */
99 function __construct(
100 $db,
101 $history = self::CURRENT,
102 $text = self::TEXT,
103 $limitNamespaces = null
104 ) {
105 $this->db = $db;
106 $this->history = $history;
107 $this->writer = new XmlDumpWriter( $text, self::schemaVersion() );
108 $this->sink = new DumpOutput();
109 $this->text = $text;
110 $this->limitNamespaces = $limitNamespaces;
111 }
112
113 /**
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
116 * constants.
117 */
118 public function setSchemaVersion( $schemaVersion ) {
119 $this->writer = new XmlDumpWriter( $this->text, $schemaVersion );
120 }
121
122 /**
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.
126 *
127 * @param DumpOutput &$sink
128 */
129 public function setOutputSink( &$sink ) {
130 $this->sink =& $sink;
131 }
132
133 public function openStream() {
134 $output = $this->writer->openStream();
135 $this->sink->writeOpenStream( $output );
136 }
137
138 public function closeStream() {
139 $output = $this->writer->closeStream();
140 $this->sink->writeCloseStream( $output );
141 }
142
143 /**
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.
147 */
148 public function allPages() {
149 $this->dumpFrom( '' );
150 }
151
152 /**
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
159 */
160 public function pagesByRange( $start, $end, $orderRevs ) {
161 if ( $orderRevs ) {
162 $condition = 'rev_page >= ' . intval( $start );
163 if ( $end ) {
164 $condition .= ' AND rev_page < ' . intval( $end );
165 }
166 } else {
167 $condition = 'page_id >= ' . intval( $start );
168 if ( $end ) {
169 $condition .= ' AND page_id < ' . intval( $end );
170 }
171 }
172 $this->dumpFrom( $condition, $orderRevs );
173 }
174
175 /**
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.
181 */
182 public function revsByRange( $start, $end ) {
183 $condition = 'rev_id >= ' . intval( $start );
184 if ( $end ) {
185 $condition .= ' AND rev_id < ' . intval( $end );
186 }
187 $this->dumpFrom( $condition );
188 }
189
190 /**
191 * @param Title $title
192 */
193 public function pageByTitle( $title ) {
194 $this->dumpFrom(
195 'page_namespace=' . $title->getNamespace() .
196 ' AND page_title=' . $this->db->addQuotes( $title->getDBkey() ) );
197 }
198
199 /**
200 * @param string $name
201 * @throws MWException
202 */
203 public function pageByName( $name ) {
204 $title = Title::newFromText( $name );
205 if ( is_null( $title ) ) {
206 throw new MWException( "Can't export invalid title" );
207 } else {
208 $this->pageByTitle( $title );
209 }
210 }
211
212 /**
213 * @param array $names
214 */
215 public function pagesByName( $names ) {
216 foreach ( $names as $name ) {
217 $this->pageByName( $name );
218 }
219 }
220
221 public function allLogs() {
222 $this->dumpFrom( '' );
223 }
224
225 /**
226 * @param int $start
227 * @param int $end
228 */
229 public function logsByRange( $start, $end ) {
230 $condition = 'log_id >= ' . intval( $start );
231 if ( $end ) {
232 $condition .= ' AND log_id < ' . intval( $end );
233 }
234 $this->dumpFrom( $condition );
235 }
236
237 /**
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
241 *
242 * @param array $cond
243 */
244 protected function do_list_authors( $cond ) {
245 $this->author_list = "<contributors>";
246 // rev_deleted
247
248 $revQuery = Revision::getQueryInfo( [ 'page' ] );
249 $res = $this->db->select(
250 $revQuery['tables'],
251 [
252 'rev_user_text' => $revQuery['fields']['rev_user_text'],
253 'rev_user' => $revQuery['fields']['rev_user'],
254 ],
255 [
256 $this->db->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0',
257 $cond,
258 ],
259 __METHOD__,
260 [ 'DISTINCT' ],
261 $revQuery['joins']
262 );
263
264 foreach ( $res as $row ) {
265 $this->author_list .= "<contributor>" .
266 "<username>" .
267 htmlspecialchars( $row->rev_user_text ) .
268 "</username>" .
269 "<id>" .
270 ( (int)$row->rev_user ) .
271 "</id>" .
272 "</contributor>";
273 }
274 $this->author_list .= "</contributors>";
275 }
276
277 /**
278 * @param string $cond
279 * @param bool $orderRevs
280 * @throws MWException
281 * @throws Exception
282 */
283 protected function dumpFrom( $cond = '', $orderRevs = false ) {
284 if ( $this->history & self::LOGS ) {
285 $this->dumpLogs( $cond );
286 } else {
287 $this->dumpPages( $cond, $orderRevs );
288 }
289 }
290
291 /**
292 * @param string $cond
293 * @throws Exception
294 */
295 protected function dumpLogs( $cond ) {
296 $where = [];
297 # Hide private logs
298 $hideLogs = LogEventsList::getExcludeClause( $this->db );
299 if ( $hideLogs ) {
300 $where[] = $hideLogs;
301 }
302 # Add on any caller specified conditions
303 if ( $cond ) {
304 $where[] = $cond;
305 }
306 # Get logging table name for logging.* clause
307 $logging = $this->db->tableName( 'logging' );
308
309 $result = null; // Assuring $result is not undefined, if exception occurs early
310
311 $commentQuery = CommentStore::getStore()->getJoin( 'log_comment' );
312 $actorQuery = ActorMigration::newMigration()->getJoin( 'log_user' );
313
314 $lastLogId = 0;
315 while ( true ) {
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 ) ] ),
320 __METHOD__,
321 [
322 'ORDER BY' => 'log_id',
323 'USE INDEX' => [ 'logging' => 'PRIMARY' ],
324 'LIMIT' => self::BATCH_SIZE,
325 ],
326 [
327 'user' => [ 'JOIN', 'user_id = ' . $actorQuery['fields']['log_user'] ]
328 ] + $commentQuery['joins'] + $actorQuery['joins']
329 );
330
331 if ( !$result->numRows() ) {
332 break;
333 }
334
335 $lastLogId = $this->outputLogStream( $result );
336 }
337 }
338
339 /**
340 * @param string $cond
341 * @param bool $orderRevs
342 * @throws MWException
343 * @throws Exception
344 */
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!'
352 );
353 }
354
355 $revQuery = MediaWikiServicesAlias::getInstance()->getRevisionStore()->getQueryInfo(
356 [ 'page' ]
357 );
358 $slotQuery = MediaWikiServicesAlias::getInstance()->getRevisionStore()->getSlotsQueryInfo(
359 [ 'content' ]
360 );
361
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' ] ],
373 ];
374 unset( $join['page'] );
375
376 $fields = array_merge( $revQuery['fields'], $slotQuery['fields'] );
377 $fields[] = 'page_restrictions';
378
379 if ( $this->text != self::STUB ) {
380 $fields['_load_content'] = '1';
381 }
382
383 $conds = [];
384 if ( $cond !== '' ) {
385 $conds[] = $cond;
386 }
387 $opts = [ 'ORDER BY' => [ 'rev_page ASC', 'rev_id ASC' ] ];
388 $opts['USE INDEX'] = [];
389
390 $op = '>';
391 if ( is_array( $this->history ) ) {
392 # Time offset/limit for all pages/history...
393 # Set time order
394 if ( $this->history['dir'] == 'asc' ) {
395 $opts['ORDER BY'] = 'rev_timestamp ASC';
396 } else {
397 $op = '<';
398 $opts['ORDER BY'] = 'rev_timestamp DESC';
399 }
400 # Set offset
401 if ( !empty( $this->history['offset'] ) ) {
402 $conds[] = "rev_timestamp $op " .
403 $this->db->addQuotes( $this->db->timestamp( $this->history['offset'] ) );
404 }
405 # Set query limit
406 if ( !empty( $this->history['limit'] ) ) {
407 $maxRowCount = intval( $this->history['limit'] );
408 }
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' ];
417 }
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 );
422 }
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." );
432 }
433 } elseif ( $this->history & self::RANGE ) {
434 # Dump of revisions within a specified range. Condition already set in revsByRange().
435 } else {
436 # Unknown history specification parameter?
437 throw new MWException( __METHOD__ . " given invalid history dump type." );
438 }
439
440 $result = null; // Assuring $result is not undefined, if exception occurs early
441 $done = false;
442 $lastRow = null;
443 $revPage = 0;
444 $revId = 0;
445 $rowCount = 0;
446
447 $opts['LIMIT'] = self::BATCH_SIZE;
448
449 Hooks::run( 'ModifyExportQuery',
450 [ $this->db, &$tables, &$cond, &$opts, &$join ] );
451
452 while ( !$done ) {
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;
456 $done = true;
457 }
458
459 $queryConds = $conds;
460 $queryConds[] = 'rev_page>' . intval( $revPage ) . ' OR (rev_page=' .
461 intval( $revPage ) . ' AND rev_id' . $op . intval( $revId ) . ')';
462
463 # Do the query and process any results, remembering max ids for the next iteration.
464 $result = $this->db->select(
465 $tables,
466 $fields,
467 $queryConds,
468 __METHOD__,
469 $opts,
470 $join
471 );
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;
477 } else {
478 $done = true;
479 }
480
481 // If we are finished, close off final page element (if any).
482 if ( $done && $lastRow ) {
483 $this->finishPageStreamOutput( $lastRow );
484 }
485 }
486 }
487
488 /**
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.
492 *
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
496 */
497 protected function outputPageStreamBatch( $results, $lastRow ) {
498 $rowCarry = null;
499 while ( true ) {
500 $slotRows = $this->getSlotRowBatch( $results, $rowCarry );
501
502 if ( !$slotRows ) {
503 break;
504 }
505
506 // All revision info is present in all slot rows.
507 // Use the first slot row as the revision row.
508 $revRow = $slotRows[0];
509
510 if ( $this->limitNamespaces &&
511 !in_array( $revRow->page_namespace, $this->limitNamespaces ) ) {
512 $lastRow = $revRow;
513 continue;
514 }
515
516 if ( $lastRow === null ||
517 $lastRow->page_namespace !== $revRow->page_namespace ||
518 $lastRow->page_title !== $revRow->page_title ) {
519 if ( $lastRow !== null ) {
520 $output = '';
521 if ( $this->dumpUploads ) {
522 $output .= $this->writer->writeUploads( $lastRow, $this->dumpUploadFileContents );
523 }
524 $output .= $this->writer->closePage();
525 $this->sink->writeClosePage( $output );
526 }
527 $output = $this->writer->openPage( $revRow );
528 $this->sink->writeOpenPage( $revRow, $output );
529 }
530 $output = $this->writer->writeRevision( $revRow, $slotRows );
531 $this->sink->writeRevision( $revRow, $output );
532 $lastRow = $revRow;
533 }
534
535 if ( $rowCarry ) {
536 throw new LogicException( 'Error while processing a stream of slot rows' );
537 }
538
539 return $lastRow;
540 }
541
542 /**
543 * Returns all slot rows for a revision.
544 * Takes and returns a carry row from the last batch;
545 *
546 * @param IResultWrapper|array $results
547 * @param null|object &$carry A row carried over from the last call to getSlotRowBatch()
548 *
549 * @return object[]
550 */
551 protected function getSlotRowBatch( $results, &$carry = null ) {
552 $slotRows = [];
553 $prev = null;
554
555 if ( $carry ) {
556 $slotRows[] = $carry;
557 $prev = $carry;
558 $carry = null;
559 }
560
561 while ( $row = $results->fetchObject() ) {
562 if ( $prev && $prev->rev_id !== $row->rev_id ) {
563 $carry = $row;
564 break;
565 }
566 $slotRows[] = $row;
567 $prev = $row;
568 }
569
570 return $slotRows;
571 }
572
573 /**
574 * Final page stream output, after all batches are complete
575 *
576 * @param object $lastRow the last row output from the last batch (or null if none)
577 */
578 protected function finishPageStreamOutput( $lastRow ) {
579 $output = '';
580 if ( $this->dumpUploads ) {
581 $output .= $this->writer->writeUploads( $lastRow, $this->dumpUploadFileContents );
582 }
583 $output .= $this->author_list;
584 $output .= $this->writer->closePage();
585 $this->sink->writeClosePage( $output );
586 }
587
588 /**
589 * @param IResultWrapper $resultset
590 * @return int the log_id value of the last item output, or null if none
591 */
592 protected function outputLogStream( $resultset ) {
593 foreach ( $resultset as $row ) {
594 $output = $this->writer->writeLogItem( $row );
595 $this->sink->writeLogItem( $row, $output );
596 }
597 return isset( $row ) ? $row->log_id : null;
598 }
599 }