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
25 use MediaWiki\MediaWikiServices
;
26 use MediaWiki\Revision\RevisionStore
;
27 use MediaWiki\Storage\SqlBlobStore
;
34 * @var string[] the schema versions supported for output
37 public static $supportedSchemas = [
38 XML_DUMP_SCHEMA_VERSION_10
,
42 * Title of the currently processed page
46 private $currentTitle = null;
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.
54 * Output will be encoded in UTF-8.
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",
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.
67 * Create a changeset like https://gerrit.wikimedia.org/r/#/c/149643/ in which
68 * you copy in the new xsd file.
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
73 'xsi:schemaLocation' => "http://www.mediawiki.org/xml/export-$ver/ " .
74 "http://www.mediawiki.org/xml/export-$ver.xsd",
76 'xml:lang' => MediaWikiServices
::getInstance()->getContentLanguage()->getHtmlCode() ],
92 $this->namespaces() ];
93 return " <siteinfo>\n " .
94 implode( "\n ", $info ) .
101 function sitename() {
103 return Xml
::element( 'sitename', [], $wgSitename );
111 return Xml
::element( 'dbname', [], $wgDBname );
117 function generator() {
119 return Xml
::element( 'generator', [], "MediaWiki $wgVersion" );
125 function homelink() {
126 return Xml
::element( 'base', [], Title
::newMainPage()->getCanonicalURL() );
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 );
142 function namespaces() {
143 $spaces = "<namespaces>\n";
144 $nsInfo = MediaWikiServices
::getInstance()->getNamespaceInfo();
146 MediaWikiServices
::getInstance()->getContentLanguage()->getFormattedNamespaces()
150 Xml
::element( 'namespace',
153 'case' => $nsInfo->isCapitalized( $ns )
154 ?
'first-letter' : 'case-sensitive',
157 $spaces .= " </namespaces>";
162 * Closes the output stream with the closing root element.
163 * Call when finished dumping things.
167 function closeStream() {
168 return "</mediawiki>\n";
172 * Opens a "<page>" section on the output stream, with data
173 * from the given database row.
178 public function openPage( $row ) {
180 $this->currentTitle
= Title
::makeTitle( $row->page_namespace
, $row->page_title
);
181 $canonicalTitle = self
::canonicalTitle( $this->currentTitle
);
182 $out .= ' ' . Xml
::elementClean( 'title', [], $canonicalTitle ) . "\n";
183 $out .= ' ' . Xml
::element( 'ns', [], strval( $row->page_namespace
) ) . "\n";
184 $out .= ' ' . Xml
::element( 'id', [], strval( $row->page_id
) ) . "\n";
185 if ( $row->page_is_redirect
) {
186 $page = WikiPage
::factory( $this->currentTitle
);
187 $redirect = $page->getRedirectTarget();
188 if ( $redirect instanceof Title
&& $redirect->isValidRedirectTarget() ) {
190 $out .= Xml
::element( 'redirect', [ 'title' => self
::canonicalTitle( $redirect ) ] );
195 if ( $row->page_restrictions
!= '' ) {
196 $out .= ' ' . Xml
::element( 'restrictions', [],
197 strval( $row->page_restrictions
) ) . "\n";
200 Hooks
::run( 'XmlDumpWriterOpenPage', [ $this, &$out, $row, $this->currentTitle
] );
206 * Closes a "<page>" section on the output stream.
211 function closePage() {
212 if ( $this->currentTitle
!== null ) {
213 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
214 // In rare cases, link cache has the same key for some pages which
215 // might be read as part of the same batch. T220424 and T220316
216 $linkCache->clearLink( $this->currentTitle
);
222 * @return RevisionStore
224 private function getRevisionStore() {
225 return MediaWikiServices
::getInstance()->getRevisionStore();
229 * @return SqlBlobStore
231 private function getBlobStore() {
232 return MediaWikiServices
::getInstance()->getBlobStore();
236 * Dumps a "<revision>" section on the output stream, with
237 * data filled in from the given database row.
243 function writeRevision( $row ) {
244 $out = " <revision>\n";
245 $out .= " " . Xml
::element( 'id', null, strval( $row->rev_id
) ) . "\n";
246 if ( isset( $row->rev_parent_id
) && $row->rev_parent_id
) {
247 $out .= " " . Xml
::element( 'parentid', null, strval( $row->rev_parent_id
) ) . "\n";
250 $out .= $this->writeTimestamp( $row->rev_timestamp
);
252 if ( isset( $row->rev_deleted
) && ( $row->rev_deleted
& Revision
::DELETED_USER
) ) {
253 $out .= " " . Xml
::element( 'contributor', [ 'deleted' => 'deleted' ] ) . "\n";
255 $out .= $this->writeContributor( $row->rev_user
, $row->rev_user_text
);
258 if ( isset( $row->rev_minor_edit
) && $row->rev_minor_edit
) {
259 $out .= " <minor/>\n";
261 if ( isset( $row->rev_deleted
) && ( $row->rev_deleted
& Revision
::DELETED_COMMENT
) ) {
262 $out .= " " . Xml
::element( 'comment', [ 'deleted' => 'deleted' ] ) . "\n";
264 $comment = CommentStore
::getStore()->getComment( 'rev_comment', $row )->text
;
265 if ( $comment != '' ) {
266 $out .= " " . Xml
::elementClean( 'comment', [], strval( $comment ) ) . "\n";
270 // TODO: rev_content_model no longer exists with MCR, see T174031
271 if ( isset( $row->rev_content_model
) && !is_null( $row->rev_content_model
) ) {
272 $content_model = strval( $row->rev_content_model
);
274 // probably using $wgContentHandlerUseDB = false;
275 $content_model = ContentHandler
::getDefaultModelFor( $this->currentTitle
);
278 $content_handler = ContentHandler
::getForModelID( $content_model );
280 // TODO: rev_content_format no longer exists with MCR, see T174031
281 if ( isset( $row->rev_content_format
) && !is_null( $row->rev_content_format
) ) {
282 $content_format = strval( $row->rev_content_format
);
284 // probably using $wgContentHandlerUseDB = false;
285 $content_format = $content_handler->getDefaultFormat();
288 $out .= " " . Xml
::element( 'model', null, strval( $content_model ) ) . "\n";
289 $out .= " " . Xml
::element( 'format', null, strval( $content_format ) ) . "\n";
292 if ( isset( $row->rev_deleted
) && ( $row->rev_deleted
& Revision
::DELETED_TEXT
) ) {
293 $out .= " " . Xml
::element( 'text', [ 'deleted' => 'deleted' ] ) . "\n";
294 } elseif ( isset( $row->old_text
) ) {
295 // Raw text from the database may have invalid chars
296 $text = strval( Revision
::getRevisionText( $row ) );
298 $text = $content_handler->exportTransform( $text, $content_format );
300 catch ( Exception
$ex ) {
301 if ( $ex instanceof MWException ||
$ex instanceof RuntimeException
) {
302 // leave text as is; that's the way it goes
303 wfLogWarning( 'exportTransform failed on text for revid ' . $row->rev_id
. "\n" );
308 $out .= " " . Xml
::elementClean( 'text',
309 [ 'xml:space' => 'preserve', 'bytes' => intval( $row->rev_len
) ],
310 strval( $text ) ) . "\n";
311 } elseif ( isset( $row->_load_content
) ) {
312 // TODO: make this fully MCR aware, see T174031
313 $rev = $this->getRevisionStore()->newRevisionFromRow( $row, 0, $this->currentTitle
);
314 $slot = $rev->getSlot( 'main' );
316 $content = $slot->getContent();
318 if ( $content instanceof TextContent
) {
319 // HACK: For text based models, bypass the serialization step.
320 // This allows extensions (like Flow)that use incompatible combinations
321 // of serialization format and content model.
322 $text = $content->getNativeData();
324 $text = $content->serialize( $content_format );
326 $text = $content_handler->exportTransform( $text, $content_format );
327 $out .= " " . Xml
::elementClean( 'text',
328 [ 'xml:space' => 'preserve', 'bytes' => intval( $slot->getSize() ) ],
329 strval( $text ) ) . "\n";
331 catch ( Exception
$ex ) {
332 if ( $ex instanceof MWException ||
$ex instanceof RuntimeException
) {
333 // there's no provsion in the schema for an attribute that will let
334 // the user know this element was unavailable due to error; an empty
335 // tag is the best we can do
336 $out .= " " . Xml
::element( 'text' ) . "\n";
337 wfLogWarning( 'failed to load content for revid ' . $row->rev_id
. "\n" );
342 } elseif ( isset( $row->rev_text_id
) ) {
343 // Stub output for pre-MCR schema
344 // TODO: MCR: rev_text_id only exists in the pre-MCR schema. Remove this when
345 // we drop support for the old schema.
346 $out .= " " . Xml
::element( 'text',
347 [ 'id' => $row->rev_text_id
, 'bytes' => intval( $row->rev_len
) ],
350 // Backwards-compatible stub output for MCR aware schema
351 // TODO: MCR: emit content addresses instead of text ids, see T174031, T199121
352 $rev = $this->getRevisionStore()->newRevisionFromRow( $row, 0, $this->currentTitle
);
353 $slot = $rev->getSlot( 'main' );
355 // Note that this is currently the ONLY reason we have a BlobStore here at all.
356 // When removing this line, check whether the BlobStore has become unused.
357 $textId = $this->getBlobStore()->getTextIdFromAddress( $slot->getAddress() );
358 $out .= " " . Xml
::element( 'text',
359 [ 'id' => $textId, 'bytes' => intval( $slot->getSize() ) ],
363 if ( isset( $row->rev_sha1
)
365 && !( $row->rev_deleted
& Revision
::DELETED_TEXT
)
367 $out .= " " . Xml
::element( 'sha1', null, strval( $row->rev_sha1
) ) . "\n";
369 $out .= " <sha1/>\n";
372 // Avoid PHP 7.1 warning from passing $this by reference
374 Hooks
::run( 'XmlDumpWriterWriteRevision', [ &$writer, &$out, $row, $text ] );
376 $out .= " </revision>\n";
382 * Dumps a "<logitem>" section on the output stream, with
383 * data filled in from the given database row.
389 function writeLogItem( $row ) {
390 $out = " <logitem>\n";
391 $out .= " " . Xml
::element( 'id', null, strval( $row->log_id
) ) . "\n";
393 $out .= $this->writeTimestamp( $row->log_timestamp
, " " );
395 if ( $row->log_deleted
& LogPage
::DELETED_USER
) {
396 $out .= " " . Xml
::element( 'contributor', [ 'deleted' => 'deleted' ] ) . "\n";
398 $out .= $this->writeContributor( $row->log_user
, $row->user_name
, " " );
401 if ( $row->log_deleted
& LogPage
::DELETED_COMMENT
) {
402 $out .= " " . Xml
::element( 'comment', [ 'deleted' => 'deleted' ] ) . "\n";
404 $comment = CommentStore
::getStore()->getComment( 'log_comment', $row )->text
;
405 if ( $comment != '' ) {
406 $out .= " " . Xml
::elementClean( 'comment', null, strval( $comment ) ) . "\n";
410 $out .= " " . Xml
::element( 'type', null, strval( $row->log_type
) ) . "\n";
411 $out .= " " . Xml
::element( 'action', null, strval( $row->log_action
) ) . "\n";
413 if ( $row->log_deleted
& LogPage
::DELETED_ACTION
) {
414 $out .= " " . Xml
::element( 'text', [ 'deleted' => 'deleted' ] ) . "\n";
416 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
417 $out .= " " . Xml
::elementClean( 'logtitle', null, self
::canonicalTitle( $title ) ) . "\n";
418 $out .= " " . Xml
::elementClean( 'params',
419 [ 'xml:space' => 'preserve' ],
420 strval( $row->log_params
) ) . "\n";
423 $out .= " </logitem>\n";
429 * @param string $timestamp
430 * @param string $indent Default to six spaces
433 function writeTimestamp( $timestamp, $indent = " " ) {
434 $ts = wfTimestamp( TS_ISO_8601
, $timestamp );
435 return $indent . Xml
::element( 'timestamp', null, $ts ) . "\n";
440 * @param string $text
441 * @param string $indent Default to six spaces
444 function writeContributor( $id, $text, $indent = " " ) {
445 $out = $indent . "<contributor>\n";
446 if ( $id ||
!IP
::isValid( $text ) ) {
447 $out .= $indent . " " . Xml
::elementClean( 'username', null, strval( $text ) ) . "\n";
448 $out .= $indent . " " . Xml
::element( 'id', null, strval( $id ) ) . "\n";
450 $out .= $indent . " " . Xml
::elementClean( 'ip', null, strval( $text ) ) . "\n";
452 $out .= $indent . "</contributor>\n";
457 * Warning! This data is potentially inconsistent. :(
459 * @param bool $dumpContents
462 function writeUploads( $row, $dumpContents = false ) {
463 if ( $row->page_namespace
== NS_FILE
) {
464 $img = wfLocalFile( $row->page_title
);
465 if ( $img && $img->exists() ) {
467 foreach ( array_reverse( $img->getHistory() ) as $ver ) {
468 $out .= $this->writeUpload( $ver, $dumpContents );
470 $out .= $this->writeUpload( $img, $dumpContents );
479 * @param bool $dumpContents
482 function writeUpload( $file, $dumpContents = false ) {
483 if ( $file->isOld() ) {
485 Xml
::element( 'archivename', null, $file->getArchiveName() ) . "\n";
489 if ( $dumpContents ) {
490 $be = $file->getRepo()->getBackend();
491 # Dump file as base64
492 # Uses only XML-safe characters, so does not need escaping
493 # @todo Too bad this loads the contents into memory (script might swap)
494 $contents = ' <contents encoding="base64">' .
495 chunk_split( base64_encode(
496 $be->getFileContents( [ 'src' => $file->getPath() ] ) ) ) .
501 if ( $file->isDeleted( File
::DELETED_COMMENT
) ) {
502 $comment = Xml
::element( 'comment', [ 'deleted' => 'deleted' ] );
504 $comment = Xml
::elementClean( 'comment', null, strval( $file->getDescription() ) );
506 return " <upload>\n" .
507 $this->writeTimestamp( $file->getTimestamp() ) .
508 $this->writeContributor( $file->getUser( 'id' ), $file->getUser( 'text' ) ) .
509 " " . $comment . "\n" .
510 " " . Xml
::element( 'filename', null, $file->getName() ) . "\n" .
512 " " . Xml
::element( 'src', null, $file->getCanonicalUrl() ) . "\n" .
513 " " . Xml
::element( 'size', null, $file->getSize() ) . "\n" .
514 " " . Xml
::element( 'sha1base36', null, $file->getSha1() ) . "\n" .
515 " " . Xml
::element( 'rel', null, $file->getRel() ) . "\n" .
521 * Return prefixed text form of title, but using the content language's
522 * canonical namespace. This skips any special-casing such as gendered
523 * user namespaces -- which while useful, are not yet listed in the
524 * XML "<siteinfo>" data so are unsafe in export.
526 * @param Title $title
530 public static function canonicalTitle( Title
$title ) {
531 if ( $title->isExternal() ) {
532 return $title->getPrefixedText();
535 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
536 getFormattedNsText( $title->getNamespace() );
538 // @todo Emit some kind of warning to the user if $title->getNamespace() !==
539 // NS_MAIN and $prefix === '' (viz. pages in an unregistered namespace)
541 if ( $prefix !== '' ) {
545 return $prefix . $title->getText();