Merge "resourceloader: Throw exception in ResourceLoaderImage::getPath"
[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 Wikimedia\Rdbms\IResultWrapper;
31 use Wikimedia\Rdbms\IDatabase;
32
33 /**
34 * @ingroup SpecialPage Dump
35 */
36 class WikiExporter {
37 /** @var bool Return distinct author list (when not returning full history) */
38 public $list_authors = false;
39
40 /** @var bool */
41 public $dumpUploads = false;
42
43 /** @var bool */
44 public $dumpUploadFileContents = false;
45
46 /** @var string */
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 TEXT = 0;
56 const STUB = 1;
57
58 const BATCH_SIZE = 50000;
59
60 /** @var int */
61 public $text;
62
63 /** @var DumpOutput */
64 public $sink;
65
66 /** @var XmlDumpWriter */
67 private $writer;
68
69 /**
70 * Returns the default export schema version, as defined by $wgXmlDumpSchemaVersion.
71 * @return string
72 */
73 public static function schemaVersion() {
74 global $wgXmlDumpSchemaVersion;
75 return $wgXmlDumpSchemaVersion;
76 }
77
78 /**
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
87 * to limit results
88 */
89 function __construct( $db, $history = self::CURRENT, $text = self::TEXT,
90 $limitNamespaces = null
91 ) {
92 $this->db = $db;
93 $this->history = $history;
94 $this->writer = new XmlDumpWriter( $text, self::schemaVersion() );
95 $this->sink = new DumpOutput();
96 $this->text = $text;
97 $this->limitNamespaces = $limitNamespaces;
98 }
99
100 /**
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
103 * constants.
104 */
105 public function setSchemaVersion( $schemaVersion ) {
106 $this->writer = new XmlDumpWriter( $this->text, $schemaVersion );
107 }
108
109 /**
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.
113 *
114 * @param DumpOutput &$sink
115 */
116 public function setOutputSink( &$sink ) {
117 $this->sink =& $sink;
118 }
119
120 public function openStream() {
121 $output = $this->writer->openStream();
122 $this->sink->writeOpenStream( $output );
123 }
124
125 public function closeStream() {
126 $output = $this->writer->closeStream();
127 $this->sink->writeCloseStream( $output );
128 }
129
130 /**
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.
134 */
135 public function allPages() {
136 $this->dumpFrom( '' );
137 }
138
139 /**
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
146 */
147 public function pagesByRange( $start, $end, $orderRevs ) {
148 if ( $orderRevs ) {
149 $condition = 'rev_page >= ' . intval( $start );
150 if ( $end ) {
151 $condition .= ' AND rev_page < ' . intval( $end );
152 }
153 } else {
154 $condition = 'page_id >= ' . intval( $start );
155 if ( $end ) {
156 $condition .= ' AND page_id < ' . intval( $end );
157 }
158 }
159 $this->dumpFrom( $condition, $orderRevs );
160 }
161
162 /**
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.
168 */
169 public function revsByRange( $start, $end ) {
170 $condition = 'rev_id >= ' . intval( $start );
171 if ( $end ) {
172 $condition .= ' AND rev_id < ' . intval( $end );
173 }
174 $this->dumpFrom( $condition );
175 }
176
177 /**
178 * @param Title $title
179 */
180 public function pageByTitle( $title ) {
181 $this->dumpFrom(
182 'page_namespace=' . $title->getNamespace() .
183 ' AND page_title=' . $this->db->addQuotes( $title->getDBkey() ) );
184 }
185
186 /**
187 * @param string $name
188 * @throws MWException
189 */
190 public function pageByName( $name ) {
191 $title = Title::newFromText( $name );
192 if ( is_null( $title ) ) {
193 throw new MWException( "Can't export invalid title" );
194 } else {
195 $this->pageByTitle( $title );
196 }
197 }
198
199 /**
200 * @param array $names
201 */
202 public function pagesByName( $names ) {
203 foreach ( $names as $name ) {
204 $this->pageByName( $name );
205 }
206 }
207
208 public function allLogs() {
209 $this->dumpFrom( '' );
210 }
211
212 /**
213 * @param int $start
214 * @param int $end
215 */
216 public function logsByRange( $start, $end ) {
217 $condition = 'log_id >= ' . intval( $start );
218 if ( $end ) {
219 $condition .= ' AND log_id < ' . intval( $end );
220 }
221 $this->dumpFrom( $condition );
222 }
223
224 /**
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
228 *
229 * @param array $cond
230 */
231 protected function do_list_authors( $cond ) {
232 $this->author_list = "<contributors>";
233 // rev_deleted
234
235 $revQuery = Revision::getQueryInfo( [ 'page' ] );
236 $res = $this->db->select(
237 $revQuery['tables'],
238 [
239 'rev_user_text' => $revQuery['fields']['rev_user_text'],
240 'rev_user' => $revQuery['fields']['rev_user'],
241 ],
242 [
243 $this->db->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0',
244 $cond,
245 ],
246 __METHOD__,
247 [ 'DISTINCT' ],
248 $revQuery['joins']
249 );
250
251 foreach ( $res as $row ) {
252 $this->author_list .= "<contributor>" .
253 "<username>" .
254 htmlspecialchars( $row->rev_user_text ) .
255 "</username>" .
256 "<id>" .
257 ( (int)$row->rev_user ) .
258 "</id>" .
259 "</contributor>";
260 }
261 $this->author_list .= "</contributors>";
262 }
263
264 /**
265 * @param string $cond
266 * @param bool $orderRevs
267 * @throws MWException
268 * @throws Exception
269 */
270 protected function dumpFrom( $cond = '', $orderRevs = false ) {
271 if ( $this->history & self::LOGS ) {
272 $this->dumpLogs( $cond );
273 } else {
274 $this->dumpPages( $cond, $orderRevs );
275 }
276 }
277
278 /**
279 * @param string $cond
280 * @throws Exception
281 */
282 protected function dumpLogs( $cond ) {
283 $where = [];
284 # Hide private logs
285 $hideLogs = LogEventsList::getExcludeClause( $this->db );
286 if ( $hideLogs ) {
287 $where[] = $hideLogs;
288 }
289 # Add on any caller specified conditions
290 if ( $cond ) {
291 $where[] = $cond;
292 }
293 # Get logging table name for logging.* clause
294 $logging = $this->db->tableName( 'logging' );
295
296 $result = null; // Assuring $result is not undefined, if exception occurs early
297
298 $commentQuery = CommentStore::getStore()->getJoin( 'log_comment' );
299 $actorQuery = ActorMigration::newMigration()->getJoin( 'log_user' );
300
301 $lastLogId = 0;
302 while ( true ) {
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 ) ] ),
307 __METHOD__,
308 [
309 'ORDER BY' => 'log_id',
310 'USE INDEX' => [ 'logging' => 'PRIMARY' ],
311 'LIMIT' => self::BATCH_SIZE,
312 ],
313 [
314 'user' => [ 'JOIN', 'user_id = ' . $actorQuery['fields']['log_user'] ]
315 ] + $commentQuery['joins'] + $actorQuery['joins']
316 );
317
318 if ( !$result->numRows() ) {
319 break;
320 }
321
322 $lastLogId = $this->outputLogStream( $result );
323 }
324 }
325
326 /**
327 * @param string $cond
328 * @param bool $orderRevs
329 * @throws MWException
330 * @throws Exception
331 */
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!'
339 );
340 }
341
342 $revOpts = [ 'page' ];
343
344 $revQuery = Revision::getQueryInfo( $revOpts );
345
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']
350 ];
351 unset( $join['page'] );
352
353 $fields = $revQuery['fields'];
354 $fields[] = 'page_restrictions';
355
356 if ( $this->text != self::STUB ) {
357 $fields['_load_content'] = '1';
358 }
359
360 $conds = [];
361 if ( $cond !== '' ) {
362 $conds[] = $cond;
363 }
364 $opts = [ 'ORDER BY' => [ 'rev_page ASC', 'rev_id ASC' ] ];
365 $opts['USE INDEX'] = [];
366
367 $op = '>';
368 if ( is_array( $this->history ) ) {
369 # Time offset/limit for all pages/history...
370 # Set time order
371 if ( $this->history['dir'] == 'asc' ) {
372 $opts['ORDER BY'] = 'rev_timestamp ASC';
373 } else {
374 $op = '<';
375 $opts['ORDER BY'] = 'rev_timestamp DESC';
376 }
377 # Set offset
378 if ( !empty( $this->history['offset'] ) ) {
379 $conds[] = "rev_timestamp $op " .
380 $this->db->addQuotes( $this->db->timestamp( $this->history['offset'] ) );
381 }
382 # Set query limit
383 if ( !empty( $this->history['limit'] ) ) {
384 $maxRowCount = intval( $this->history['limit'] );
385 }
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' ];
395 }
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 );
400 }
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." );
410 }
411 } elseif ( $this->history & self::RANGE ) {
412 # Dump of revisions within a specified range. Condition already set in revsByRange().
413 } else {
414 # Unknown history specification parameter?
415 throw new MWException( __METHOD__ . " given invalid history dump type." );
416 }
417
418 $result = null; // Assuring $result is not undefined, if exception occurs early
419 $done = false;
420 $lastRow = null;
421 $revPage = 0;
422 $revId = 0;
423 $rowCount = 0;
424
425 $opts['LIMIT'] = self::BATCH_SIZE;
426
427 Hooks::run( 'ModifyExportQuery',
428 [ $this->db, &$tables, &$cond, &$opts, &$join ] );
429
430 while ( !$done ) {
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;
434 $done = true;
435 }
436
437 $queryConds = $conds;
438 $queryConds[] = 'rev_page>' . intval( $revPage ) . ' OR (rev_page=' .
439 intval( $revPage ) . ' AND rev_id' . $op . intval( $revId ) . ')';
440
441 # Do the query and process any results, remembering max ids for the next iteration.
442 $result = $this->db->select(
443 $tables,
444 $fields,
445 $queryConds,
446 __METHOD__,
447 $opts,
448 $join
449 );
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;
455 } else {
456 $done = true;
457 }
458
459 // If we are finished, close off final page element (if any).
460 if ( $done && $lastRow ) {
461 $this->finishPageStreamOutput( $lastRow );
462 }
463 }
464 }
465
466 /**
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.
470 *
471 * @param IResultWrapper $results
472 * @param object $lastRow the last row output from the previous call (or null if none)
473 * @return object the last row processed
474 */
475 protected function outputPageStreamBatch( $results, $lastRow ) {
476 foreach ( $results as $row ) {
477 if ( $this->limitNamespaces &&
478 !in_array( $row->page_namespace, $this->limitNamespaces ) ) {
479 $lastRow = $row;
480 continue;
481 }
482 if ( $lastRow === null ||
483 $lastRow->page_namespace !== $row->page_namespace ||
484 $lastRow->page_title !== $row->page_title ) {
485 if ( $lastRow !== null ) {
486 $output = '';
487 if ( $this->dumpUploads ) {
488 $output .= $this->writer->writeUploads( $lastRow, $this->dumpUploadFileContents );
489 }
490 $output .= $this->writer->closePage();
491 $this->sink->writeClosePage( $output );
492 }
493 $output = $this->writer->openPage( $row );
494 $this->sink->writeOpenPage( $row, $output );
495 }
496 $output = $this->writer->writeRevision( $row );
497 $this->sink->writeRevision( $row, $output );
498 $lastRow = $row;
499 }
500
501 return $lastRow;
502 }
503
504 /**
505 * Final page stream output, after all batches are complete
506 *
507 * @param object $lastRow the last row output from the last batch (or null if none)
508 */
509 protected function finishPageStreamOutput( $lastRow ) {
510 $output = '';
511 if ( $this->dumpUploads ) {
512 $output .= $this->writer->writeUploads( $lastRow, $this->dumpUploadFileContents );
513 }
514 $output .= $this->author_list;
515 $output .= $this->writer->closePage();
516 $this->sink->writeClosePage( $output );
517 }
518
519 /**
520 * @param IResultWrapper $resultset
521 * @return int the log_id value of the last item output, or null if none
522 */
523 protected function outputLogStream( $resultset ) {
524 foreach ( $resultset as $row ) {
525 $output = $this->writer->writeLogItem( $row );
526 $this->sink->writeLogItem( $row, $output );
527 }
528 return isset( $row ) ? $row->log_id : null;
529 }
530 }