Merge "build: Add "View history" scenario for Fresnel"
[lhc/web/wiklou.git] / includes / export / XmlDumpWriter.php
1 <?php
2 /**
3 * XmlDumpWriter
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 use MediaWiki\MediaWikiServices;
26 use MediaWiki\Revision\RevisionStore;
27 use MediaWiki\Storage\SqlBlobStore;
28
29 /**
30 * @ingroup Dump
31 */
32 class XmlDumpWriter {
33 /**
34 * @var string[] the schema versions supported for output
35 * @final
36 */
37 public static $supportedSchemas = [
38 XML_DUMP_SCHEMA_VERSION_10,
39 ];
40
41 /**
42 * Title of the currently processed page
43 *
44 * @var Title|null
45 */
46 private $currentTitle = null;
47
48 /**
49 * Opens the XML output stream's root "<mediawiki>" element.
50 * This does not include an xml directive, so is safe to include
51 * as a subelement in a larger XML stream. Namespace and XML Schema
52 * references are included.
53 *
54 * Output will be encoded in UTF-8.
55 *
56 * @return string
57 */
58 function openStream() {
59 $ver = WikiExporter::schemaVersion();
60 return Xml::element( 'mediawiki', [
61 'xmlns' => "http://www.mediawiki.org/xml/export-$ver/",
62 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
63 /*
64 * When a new version of the schema is created, it needs staging on mediawiki.org.
65 * This requires a change in the operations/mediawiki-config git repo.
66 *
67 * Create a changeset like https://gerrit.wikimedia.org/r/#/c/149643/ in which
68 * you copy in the new xsd file.
69 *
70 * After it is reviewed, merged and deployed (sync-docroot), the index.html needs purging.
71 * echo "https://www.mediawiki.org/xml/index.html" | mwscript purgeList.php --wiki=aawiki
72 */
73 'xsi:schemaLocation' => "http://www.mediawiki.org/xml/export-$ver/ " .
74 "http://www.mediawiki.org/xml/export-$ver.xsd",
75 'version' => $ver,
76 'xml:lang' => MediaWikiServices::getInstance()->getContentLanguage()->getHtmlCode() ],
77 null ) .
78 "\n" .
79 $this->siteInfo();
80 }
81
82 /**
83 * @return string
84 */
85 function siteInfo() {
86 $info = [
87 $this->sitename(),
88 $this->dbname(),
89 $this->homelink(),
90 $this->generator(),
91 $this->caseSetting(),
92 $this->namespaces() ];
93 return " <siteinfo>\n " .
94 implode( "\n ", $info ) .
95 "\n </siteinfo>\n";
96 }
97
98 /**
99 * @return string
100 */
101 function sitename() {
102 global $wgSitename;
103 return Xml::element( 'sitename', [], $wgSitename );
104 }
105
106 /**
107 * @return string
108 */
109 function dbname() {
110 global $wgDBname;
111 return Xml::element( 'dbname', [], $wgDBname );
112 }
113
114 /**
115 * @return string
116 */
117 function generator() {
118 global $wgVersion;
119 return Xml::element( 'generator', [], "MediaWiki $wgVersion" );
120 }
121
122 /**
123 * @return string
124 */
125 function homelink() {
126 return Xml::element( 'base', [], Title::newMainPage()->getCanonicalURL() );
127 }
128
129 /**
130 * @return string
131 */
132 function caseSetting() {
133 global $wgCapitalLinks;
134 // "case-insensitive" option is reserved for future
135 $sensitivity = $wgCapitalLinks ? 'first-letter' : 'case-sensitive';
136 return Xml::element( 'case', [], $sensitivity );
137 }
138
139 /**
140 * @return string
141 */
142 function namespaces() {
143 $spaces = "<namespaces>\n";
144 foreach (
145 MediaWikiServices::getInstance()->getContentLanguage()->getFormattedNamespaces()
146 as $ns => $title
147 ) {
148 $spaces .= ' ' .
149 Xml::element( 'namespace',
150 [
151 'key' => $ns,
152 'case' => MWNamespace::isCapitalized( $ns ) ? 'first-letter' : 'case-sensitive',
153 ], $title ) . "\n";
154 }
155 $spaces .= " </namespaces>";
156 return $spaces;
157 }
158
159 /**
160 * Closes the output stream with the closing root element.
161 * Call when finished dumping things.
162 *
163 * @return string
164 */
165 function closeStream() {
166 return "</mediawiki>\n";
167 }
168
169 /**
170 * Opens a "<page>" section on the output stream, with data
171 * from the given database row.
172 *
173 * @param object $row
174 * @return string
175 */
176 public function openPage( $row ) {
177 $out = " <page>\n";
178 $this->currentTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
179 $canonicalTitle = self::canonicalTitle( $this->currentTitle );
180 $out .= ' ' . Xml::elementClean( 'title', [], $canonicalTitle ) . "\n";
181 $out .= ' ' . Xml::element( 'ns', [], strval( $row->page_namespace ) ) . "\n";
182 $out .= ' ' . Xml::element( 'id', [], strval( $row->page_id ) ) . "\n";
183 if ( $row->page_is_redirect ) {
184 $page = WikiPage::factory( $this->currentTitle );
185 $redirect = $page->getRedirectTarget();
186 if ( $redirect instanceof Title && $redirect->isValidRedirectTarget() ) {
187 $out .= ' ';
188 $out .= Xml::element( 'redirect', [ 'title' => self::canonicalTitle( $redirect ) ] );
189 $out .= "\n";
190 }
191 }
192
193 if ( $row->page_restrictions != '' ) {
194 $out .= ' ' . Xml::element( 'restrictions', [],
195 strval( $row->page_restrictions ) ) . "\n";
196 }
197
198 Hooks::run( 'XmlDumpWriterOpenPage', [ $this, &$out, $row, $this->currentTitle ] );
199
200 return $out;
201 }
202
203 /**
204 * Closes a "<page>" section on the output stream.
205 *
206 * @private
207 * @return string
208 */
209 function closePage() {
210 return " </page>\n";
211 }
212
213 /**
214 * @return RevisionStore
215 */
216 private function getRevisionStore() {
217 return MediaWikiServices::getInstance()->getRevisionStore();
218 }
219
220 /**
221 * @return SqlBlobStore
222 */
223 private function getBlobStore() {
224 return MediaWikiServices::getInstance()->getBlobStore();
225 }
226
227 /**
228 * Dumps a "<revision>" section on the output stream, with
229 * data filled in from the given database row.
230 *
231 * @param object $row
232 * @return string
233 * @private
234 */
235 function writeRevision( $row ) {
236 $out = " <revision>\n";
237 $out .= " " . Xml::element( 'id', null, strval( $row->rev_id ) ) . "\n";
238 if ( isset( $row->rev_parent_id ) && $row->rev_parent_id ) {
239 $out .= " " . Xml::element( 'parentid', null, strval( $row->rev_parent_id ) ) . "\n";
240 }
241
242 $out .= $this->writeTimestamp( $row->rev_timestamp );
243
244 if ( isset( $row->rev_deleted ) && ( $row->rev_deleted & Revision::DELETED_USER ) ) {
245 $out .= " " . Xml::element( 'contributor', [ 'deleted' => 'deleted' ] ) . "\n";
246 } else {
247 $out .= $this->writeContributor( $row->rev_user, $row->rev_user_text );
248 }
249
250 if ( isset( $row->rev_minor_edit ) && $row->rev_minor_edit ) {
251 $out .= " <minor/>\n";
252 }
253 if ( isset( $row->rev_deleted ) && ( $row->rev_deleted & Revision::DELETED_COMMENT ) ) {
254 $out .= " " . Xml::element( 'comment', [ 'deleted' => 'deleted' ] ) . "\n";
255 } else {
256 $comment = CommentStore::getStore()->getComment( 'rev_comment', $row )->text;
257 if ( $comment != '' ) {
258 $out .= " " . Xml::elementClean( 'comment', [], strval( $comment ) ) . "\n";
259 }
260 }
261
262 // TODO: rev_content_model no longer exists with MCR, see T174031
263 if ( isset( $row->rev_content_model ) && !is_null( $row->rev_content_model ) ) {
264 $content_model = strval( $row->rev_content_model );
265 } else {
266 // probably using $wgContentHandlerUseDB = false;
267 $content_model = ContentHandler::getDefaultModelFor( $this->currentTitle );
268 }
269
270 $content_handler = ContentHandler::getForModelID( $content_model );
271
272 // TODO: rev_content_format no longer exists with MCR, see T174031
273 if ( isset( $row->rev_content_format ) && !is_null( $row->rev_content_format ) ) {
274 $content_format = strval( $row->rev_content_format );
275 } else {
276 // probably using $wgContentHandlerUseDB = false;
277 $content_format = $content_handler->getDefaultFormat();
278 }
279
280 $out .= " " . Xml::element( 'model', null, strval( $content_model ) ) . "\n";
281 $out .= " " . Xml::element( 'format', null, strval( $content_format ) ) . "\n";
282
283 $text = '';
284 if ( isset( $row->rev_deleted ) && ( $row->rev_deleted & Revision::DELETED_TEXT ) ) {
285 $out .= " " . Xml::element( 'text', [ 'deleted' => 'deleted' ] ) . "\n";
286 } elseif ( isset( $row->old_text ) ) {
287 // Raw text from the database may have invalid chars
288 $text = strval( Revision::getRevisionText( $row ) );
289 try {
290 $text = $content_handler->exportTransform( $text, $content_format );
291 }
292 catch ( Exception $ex ) {
293 if ( $ex instanceof MWException || $ex instanceof RuntimeException ) {
294 // leave text as is; that's the way it goes
295 wfLogWarning( 'exportTransform failed on text for revid ' . $row->rev_id . "\n" );
296 } else {
297 throw $ex;
298 }
299 }
300 $out .= " " . Xml::elementClean( 'text',
301 [ 'xml:space' => 'preserve', 'bytes' => intval( $row->rev_len ) ],
302 strval( $text ) ) . "\n";
303 } elseif ( isset( $row->_load_content ) ) {
304 // TODO: make this fully MCR aware, see T174031
305 $rev = $this->getRevisionStore()->newRevisionFromRow( $row, 0, $this->currentTitle );
306 $slot = $rev->getSlot( 'main' );
307 try {
308 $content = $slot->getContent();
309
310 if ( $content instanceof TextContent ) {
311 // HACK: For text based models, bypass the serialization step.
312 // This allows extensions (like Flow)that use incompatible combinations
313 // of serialization format and content model.
314 $text = $content->getNativeData();
315 } else {
316 $text = $content->serialize( $content_format );
317 }
318 $text = $content_handler->exportTransform( $text, $content_format );
319 $out .= " " . Xml::elementClean( 'text',
320 [ 'xml:space' => 'preserve', 'bytes' => intval( $slot->getSize() ) ],
321 strval( $text ) ) . "\n";
322 }
323 catch ( Exception $ex ) {
324 if ( $ex instanceof MWException || $ex instanceof RuntimeException ) {
325 // there's no provsion in the schema for an attribute that will let
326 // the user know this element was unavailable due to error; an empty
327 // tag is the best we can do
328 $out .= " " . Xml::element( 'text' ) . "\n";
329 wfLogWarning( 'failed to load content for revid ' . $row->rev_id . "\n" );
330 } else {
331 throw $ex;
332 }
333 }
334 } elseif ( isset( $row->rev_text_id ) ) {
335 // Stub output for pre-MCR schema
336 // TODO: MCR: rev_text_id only exists in the pre-MCR schema. Remove this when
337 // we drop support for the old schema.
338 $out .= " " . Xml::element( 'text',
339 [ 'id' => $row->rev_text_id, 'bytes' => intval( $row->rev_len ) ],
340 "" ) . "\n";
341 } else {
342 // Backwards-compatible stub output for MCR aware schema
343 // TODO: MCR: emit content addresses instead of text ids, see T174031, T199121
344 $rev = $this->getRevisionStore()->newRevisionFromRow( $row, 0, $this->currentTitle );
345 $slot = $rev->getSlot( 'main' );
346
347 // Note that this is currently the ONLY reason we have a BlobStore here at all.
348 // When removing this line, check whether the BlobStore has become unused.
349 $textId = $this->getBlobStore()->getTextIdFromAddress( $slot->getAddress() );
350 $out .= " " . Xml::element( 'text',
351 [ 'id' => $textId, 'bytes' => intval( $slot->getSize() ) ],
352 "" ) . "\n";
353 }
354
355 if ( isset( $row->rev_sha1 )
356 && $row->rev_sha1
357 && !( $row->rev_deleted & Revision::DELETED_TEXT )
358 ) {
359 $out .= " " . Xml::element( 'sha1', null, strval( $row->rev_sha1 ) ) . "\n";
360 } else {
361 $out .= " <sha1/>\n";
362 }
363
364 // Avoid PHP 7.1 warning from passing $this by reference
365 $writer = $this;
366 Hooks::run( 'XmlDumpWriterWriteRevision', [ &$writer, &$out, $row, $text ] );
367
368 $out .= " </revision>\n";
369
370 return $out;
371 }
372
373 /**
374 * Dumps a "<logitem>" section on the output stream, with
375 * data filled in from the given database row.
376 *
377 * @param object $row
378 * @return string
379 * @private
380 */
381 function writeLogItem( $row ) {
382 $out = " <logitem>\n";
383 $out .= " " . Xml::element( 'id', null, strval( $row->log_id ) ) . "\n";
384
385 $out .= $this->writeTimestamp( $row->log_timestamp, " " );
386
387 if ( $row->log_deleted & LogPage::DELETED_USER ) {
388 $out .= " " . Xml::element( 'contributor', [ 'deleted' => 'deleted' ] ) . "\n";
389 } else {
390 $out .= $this->writeContributor( $row->log_user, $row->user_name, " " );
391 }
392
393 if ( $row->log_deleted & LogPage::DELETED_COMMENT ) {
394 $out .= " " . Xml::element( 'comment', [ 'deleted' => 'deleted' ] ) . "\n";
395 } else {
396 $comment = CommentStore::getStore()->getComment( 'log_comment', $row )->text;
397 if ( $comment != '' ) {
398 $out .= " " . Xml::elementClean( 'comment', null, strval( $comment ) ) . "\n";
399 }
400 }
401
402 $out .= " " . Xml::element( 'type', null, strval( $row->log_type ) ) . "\n";
403 $out .= " " . Xml::element( 'action', null, strval( $row->log_action ) ) . "\n";
404
405 if ( $row->log_deleted & LogPage::DELETED_ACTION ) {
406 $out .= " " . Xml::element( 'text', [ 'deleted' => 'deleted' ] ) . "\n";
407 } else {
408 $title = Title::makeTitle( $row->log_namespace, $row->log_title );
409 $out .= " " . Xml::elementClean( 'logtitle', null, self::canonicalTitle( $title ) ) . "\n";
410 $out .= " " . Xml::elementClean( 'params',
411 [ 'xml:space' => 'preserve' ],
412 strval( $row->log_params ) ) . "\n";
413 }
414
415 $out .= " </logitem>\n";
416
417 return $out;
418 }
419
420 /**
421 * @param string $timestamp
422 * @param string $indent Default to six spaces
423 * @return string
424 */
425 function writeTimestamp( $timestamp, $indent = " " ) {
426 $ts = wfTimestamp( TS_ISO_8601, $timestamp );
427 return $indent . Xml::element( 'timestamp', null, $ts ) . "\n";
428 }
429
430 /**
431 * @param int $id
432 * @param string $text
433 * @param string $indent Default to six spaces
434 * @return string
435 */
436 function writeContributor( $id, $text, $indent = " " ) {
437 $out = $indent . "<contributor>\n";
438 if ( $id || !IP::isValid( $text ) ) {
439 $out .= $indent . " " . Xml::elementClean( 'username', null, strval( $text ) ) . "\n";
440 $out .= $indent . " " . Xml::element( 'id', null, strval( $id ) ) . "\n";
441 } else {
442 $out .= $indent . " " . Xml::elementClean( 'ip', null, strval( $text ) ) . "\n";
443 }
444 $out .= $indent . "</contributor>\n";
445 return $out;
446 }
447
448 /**
449 * Warning! This data is potentially inconsistent. :(
450 * @param object $row
451 * @param bool $dumpContents
452 * @return string
453 */
454 function writeUploads( $row, $dumpContents = false ) {
455 if ( $row->page_namespace == NS_FILE ) {
456 $img = wfLocalFile( $row->page_title );
457 if ( $img && $img->exists() ) {
458 $out = '';
459 foreach ( array_reverse( $img->getHistory() ) as $ver ) {
460 $out .= $this->writeUpload( $ver, $dumpContents );
461 }
462 $out .= $this->writeUpload( $img, $dumpContents );
463 return $out;
464 }
465 }
466 return '';
467 }
468
469 /**
470 * @param File $file
471 * @param bool $dumpContents
472 * @return string
473 */
474 function writeUpload( $file, $dumpContents = false ) {
475 if ( $file->isOld() ) {
476 $archiveName = " " .
477 Xml::element( 'archivename', null, $file->getArchiveName() ) . "\n";
478 } else {
479 $archiveName = '';
480 }
481 if ( $dumpContents ) {
482 $be = $file->getRepo()->getBackend();
483 # Dump file as base64
484 # Uses only XML-safe characters, so does not need escaping
485 # @todo Too bad this loads the contents into memory (script might swap)
486 $contents = ' <contents encoding="base64">' .
487 chunk_split( base64_encode(
488 $be->getFileContents( [ 'src' => $file->getPath() ] ) ) ) .
489 " </contents>\n";
490 } else {
491 $contents = '';
492 }
493 if ( $file->isDeleted( File::DELETED_COMMENT ) ) {
494 $comment = Xml::element( 'comment', [ 'deleted' => 'deleted' ] );
495 } else {
496 $comment = Xml::elementClean( 'comment', null, strval( $file->getDescription() ) );
497 }
498 return " <upload>\n" .
499 $this->writeTimestamp( $file->getTimestamp() ) .
500 $this->writeContributor( $file->getUser( 'id' ), $file->getUser( 'text' ) ) .
501 " " . $comment . "\n" .
502 " " . Xml::element( 'filename', null, $file->getName() ) . "\n" .
503 $archiveName .
504 " " . Xml::element( 'src', null, $file->getCanonicalUrl() ) . "\n" .
505 " " . Xml::element( 'size', null, $file->getSize() ) . "\n" .
506 " " . Xml::element( 'sha1base36', null, $file->getSha1() ) . "\n" .
507 " " . Xml::element( 'rel', null, $file->getRel() ) . "\n" .
508 $contents .
509 " </upload>\n";
510 }
511
512 /**
513 * Return prefixed text form of title, but using the content language's
514 * canonical namespace. This skips any special-casing such as gendered
515 * user namespaces -- which while useful, are not yet listed in the
516 * XML "<siteinfo>" data so are unsafe in export.
517 *
518 * @param Title $title
519 * @return string
520 * @since 1.18
521 */
522 public static function canonicalTitle( Title $title ) {
523 if ( $title->isExternal() ) {
524 return $title->getPrefixedText();
525 }
526
527 $prefix = MediaWikiServices::getInstance()->getContentLanguage()->
528 getFormattedNsText( $title->getNamespace() );
529
530 // @todo Emit some kind of warning to the user if $title->getNamespace() !==
531 // NS_MAIN and $prefix === '' (viz. pages in an unregistered namespace)
532
533 if ( $prefix !== '' ) {
534 $prefix .= ':';
535 }
536
537 return $prefix . $title->getText();
538 }
539 }