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";
145 MediaWikiServices
::getInstance()->getContentLanguage()->getFormattedNamespaces()
149 Xml
::element( 'namespace',
152 'case' => MWNamespace
::isCapitalized( $ns ) ?
'first-letter' : 'case-sensitive',
155 $spaces .= " </namespaces>";
160 * Closes the output stream with the closing root element.
161 * Call when finished dumping things.
165 function closeStream() {
166 return "</mediawiki>\n";
170 * Opens a "<page>" section on the output stream, with data
171 * from the given database row.
176 public function openPage( $row ) {
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() ) {
188 $out .= Xml
::element( 'redirect', [ 'title' => self
::canonicalTitle( $redirect ) ] );
193 if ( $row->page_restrictions
!= '' ) {
194 $out .= ' ' . Xml
::element( 'restrictions', [],
195 strval( $row->page_restrictions
) ) . "\n";
198 Hooks
::run( 'XmlDumpWriterOpenPage', [ $this, &$out, $row, $this->currentTitle
] );
204 * Closes a "<page>" section on the output stream.
209 function closePage() {
210 if ( $this->currentTitle
!== null ) {
211 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
212 // In rare cases, link cache has the same key for some pages which
213 // might be read as part of the same batch. T220424 and T220316
214 $linkCache->clearLink( $this->currentTitle
);
220 * @return RevisionStore
222 private function getRevisionStore() {
223 return MediaWikiServices
::getInstance()->getRevisionStore();
227 * @return SqlBlobStore
229 private function getBlobStore() {
230 return MediaWikiServices
::getInstance()->getBlobStore();
234 * Dumps a "<revision>" section on the output stream, with
235 * data filled in from the given database row.
241 function writeRevision( $row ) {
242 $out = " <revision>\n";
243 $out .= " " . Xml
::element( 'id', null, strval( $row->rev_id
) ) . "\n";
244 if ( isset( $row->rev_parent_id
) && $row->rev_parent_id
) {
245 $out .= " " . Xml
::element( 'parentid', null, strval( $row->rev_parent_id
) ) . "\n";
248 $out .= $this->writeTimestamp( $row->rev_timestamp
);
250 if ( isset( $row->rev_deleted
) && ( $row->rev_deleted
& Revision
::DELETED_USER
) ) {
251 $out .= " " . Xml
::element( 'contributor', [ 'deleted' => 'deleted' ] ) . "\n";
253 $out .= $this->writeContributor( $row->rev_user
, $row->rev_user_text
);
256 if ( isset( $row->rev_minor_edit
) && $row->rev_minor_edit
) {
257 $out .= " <minor/>\n";
259 if ( isset( $row->rev_deleted
) && ( $row->rev_deleted
& Revision
::DELETED_COMMENT
) ) {
260 $out .= " " . Xml
::element( 'comment', [ 'deleted' => 'deleted' ] ) . "\n";
262 $comment = CommentStore
::getStore()->getComment( 'rev_comment', $row )->text
;
263 if ( $comment != '' ) {
264 $out .= " " . Xml
::elementClean( 'comment', [], strval( $comment ) ) . "\n";
268 // TODO: rev_content_model no longer exists with MCR, see T174031
269 if ( isset( $row->rev_content_model
) && !is_null( $row->rev_content_model
) ) {
270 $content_model = strval( $row->rev_content_model
);
272 // probably using $wgContentHandlerUseDB = false;
273 $content_model = ContentHandler
::getDefaultModelFor( $this->currentTitle
);
276 $content_handler = ContentHandler
::getForModelID( $content_model );
278 // TODO: rev_content_format no longer exists with MCR, see T174031
279 if ( isset( $row->rev_content_format
) && !is_null( $row->rev_content_format
) ) {
280 $content_format = strval( $row->rev_content_format
);
282 // probably using $wgContentHandlerUseDB = false;
283 $content_format = $content_handler->getDefaultFormat();
286 $out .= " " . Xml
::element( 'model', null, strval( $content_model ) ) . "\n";
287 $out .= " " . Xml
::element( 'format', null, strval( $content_format ) ) . "\n";
290 if ( isset( $row->rev_deleted
) && ( $row->rev_deleted
& Revision
::DELETED_TEXT
) ) {
291 $out .= " " . Xml
::element( 'text', [ 'deleted' => 'deleted' ] ) . "\n";
292 } elseif ( isset( $row->old_text
) ) {
293 // Raw text from the database may have invalid chars
294 $text = strval( Revision
::getRevisionText( $row ) );
296 $text = $content_handler->exportTransform( $text, $content_format );
298 catch ( Exception
$ex ) {
299 if ( $ex instanceof MWException ||
$ex instanceof RuntimeException
) {
300 // leave text as is; that's the way it goes
301 wfLogWarning( 'exportTransform failed on text for revid ' . $row->rev_id
. "\n" );
306 $out .= " " . Xml
::elementClean( 'text',
307 [ 'xml:space' => 'preserve', 'bytes' => intval( $row->rev_len
) ],
308 strval( $text ) ) . "\n";
309 } elseif ( isset( $row->_load_content
) ) {
310 // TODO: make this fully MCR aware, see T174031
311 $rev = $this->getRevisionStore()->newRevisionFromRow( $row, 0, $this->currentTitle
);
312 $slot = $rev->getSlot( 'main' );
314 $content = $slot->getContent();
316 if ( $content instanceof TextContent
) {
317 // HACK: For text based models, bypass the serialization step.
318 // This allows extensions (like Flow)that use incompatible combinations
319 // of serialization format and content model.
320 $text = $content->getNativeData();
322 $text = $content->serialize( $content_format );
324 $text = $content_handler->exportTransform( $text, $content_format );
325 $out .= " " . Xml
::elementClean( 'text',
326 [ 'xml:space' => 'preserve', 'bytes' => intval( $slot->getSize() ) ],
327 strval( $text ) ) . "\n";
329 catch ( Exception
$ex ) {
330 if ( $ex instanceof MWException ||
$ex instanceof RuntimeException
) {
331 // there's no provsion in the schema for an attribute that will let
332 // the user know this element was unavailable due to error; an empty
333 // tag is the best we can do
334 $out .= " " . Xml
::element( 'text' ) . "\n";
335 wfLogWarning( 'failed to load content for revid ' . $row->rev_id
. "\n" );
340 } elseif ( isset( $row->rev_text_id
) ) {
341 // Stub output for pre-MCR schema
342 // TODO: MCR: rev_text_id only exists in the pre-MCR schema. Remove this when
343 // we drop support for the old schema.
344 $out .= " " . Xml
::element( 'text',
345 [ 'id' => $row->rev_text_id
, 'bytes' => intval( $row->rev_len
) ],
348 // Backwards-compatible stub output for MCR aware schema
349 // TODO: MCR: emit content addresses instead of text ids, see T174031, T199121
350 $rev = $this->getRevisionStore()->newRevisionFromRow( $row, 0, $this->currentTitle
);
351 $slot = $rev->getSlot( 'main' );
353 // Note that this is currently the ONLY reason we have a BlobStore here at all.
354 // When removing this line, check whether the BlobStore has become unused.
355 $textId = $this->getBlobStore()->getTextIdFromAddress( $slot->getAddress() );
356 $out .= " " . Xml
::element( 'text',
357 [ 'id' => $textId, 'bytes' => intval( $slot->getSize() ) ],
361 if ( isset( $row->rev_sha1
)
363 && !( $row->rev_deleted
& Revision
::DELETED_TEXT
)
365 $out .= " " . Xml
::element( 'sha1', null, strval( $row->rev_sha1
) ) . "\n";
367 $out .= " <sha1/>\n";
370 // Avoid PHP 7.1 warning from passing $this by reference
372 Hooks
::run( 'XmlDumpWriterWriteRevision', [ &$writer, &$out, $row, $text ] );
374 $out .= " </revision>\n";
380 * Dumps a "<logitem>" section on the output stream, with
381 * data filled in from the given database row.
387 function writeLogItem( $row ) {
388 $out = " <logitem>\n";
389 $out .= " " . Xml
::element( 'id', null, strval( $row->log_id
) ) . "\n";
391 $out .= $this->writeTimestamp( $row->log_timestamp
, " " );
393 if ( $row->log_deleted
& LogPage
::DELETED_USER
) {
394 $out .= " " . Xml
::element( 'contributor', [ 'deleted' => 'deleted' ] ) . "\n";
396 $out .= $this->writeContributor( $row->log_user
, $row->user_name
, " " );
399 if ( $row->log_deleted
& LogPage
::DELETED_COMMENT
) {
400 $out .= " " . Xml
::element( 'comment', [ 'deleted' => 'deleted' ] ) . "\n";
402 $comment = CommentStore
::getStore()->getComment( 'log_comment', $row )->text
;
403 if ( $comment != '' ) {
404 $out .= " " . Xml
::elementClean( 'comment', null, strval( $comment ) ) . "\n";
408 $out .= " " . Xml
::element( 'type', null, strval( $row->log_type
) ) . "\n";
409 $out .= " " . Xml
::element( 'action', null, strval( $row->log_action
) ) . "\n";
411 if ( $row->log_deleted
& LogPage
::DELETED_ACTION
) {
412 $out .= " " . Xml
::element( 'text', [ 'deleted' => 'deleted' ] ) . "\n";
414 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
415 $out .= " " . Xml
::elementClean( 'logtitle', null, self
::canonicalTitle( $title ) ) . "\n";
416 $out .= " " . Xml
::elementClean( 'params',
417 [ 'xml:space' => 'preserve' ],
418 strval( $row->log_params
) ) . "\n";
421 $out .= " </logitem>\n";
427 * @param string $timestamp
428 * @param string $indent Default to six spaces
431 function writeTimestamp( $timestamp, $indent = " " ) {
432 $ts = wfTimestamp( TS_ISO_8601
, $timestamp );
433 return $indent . Xml
::element( 'timestamp', null, $ts ) . "\n";
438 * @param string $text
439 * @param string $indent Default to six spaces
442 function writeContributor( $id, $text, $indent = " " ) {
443 $out = $indent . "<contributor>\n";
444 if ( $id ||
!IP
::isValid( $text ) ) {
445 $out .= $indent . " " . Xml
::elementClean( 'username', null, strval( $text ) ) . "\n";
446 $out .= $indent . " " . Xml
::element( 'id', null, strval( $id ) ) . "\n";
448 $out .= $indent . " " . Xml
::elementClean( 'ip', null, strval( $text ) ) . "\n";
450 $out .= $indent . "</contributor>\n";
455 * Warning! This data is potentially inconsistent. :(
457 * @param bool $dumpContents
460 function writeUploads( $row, $dumpContents = false ) {
461 if ( $row->page_namespace
== NS_FILE
) {
462 $img = wfLocalFile( $row->page_title
);
463 if ( $img && $img->exists() ) {
465 foreach ( array_reverse( $img->getHistory() ) as $ver ) {
466 $out .= $this->writeUpload( $ver, $dumpContents );
468 $out .= $this->writeUpload( $img, $dumpContents );
477 * @param bool $dumpContents
480 function writeUpload( $file, $dumpContents = false ) {
481 if ( $file->isOld() ) {
483 Xml
::element( 'archivename', null, $file->getArchiveName() ) . "\n";
487 if ( $dumpContents ) {
488 $be = $file->getRepo()->getBackend();
489 # Dump file as base64
490 # Uses only XML-safe characters, so does not need escaping
491 # @todo Too bad this loads the contents into memory (script might swap)
492 $contents = ' <contents encoding="base64">' .
493 chunk_split( base64_encode(
494 $be->getFileContents( [ 'src' => $file->getPath() ] ) ) ) .
499 if ( $file->isDeleted( File
::DELETED_COMMENT
) ) {
500 $comment = Xml
::element( 'comment', [ 'deleted' => 'deleted' ] );
502 $comment = Xml
::elementClean( 'comment', null, strval( $file->getDescription() ) );
504 return " <upload>\n" .
505 $this->writeTimestamp( $file->getTimestamp() ) .
506 $this->writeContributor( $file->getUser( 'id' ), $file->getUser( 'text' ) ) .
507 " " . $comment . "\n" .
508 " " . Xml
::element( 'filename', null, $file->getName() ) . "\n" .
510 " " . Xml
::element( 'src', null, $file->getCanonicalUrl() ) . "\n" .
511 " " . Xml
::element( 'size', null, $file->getSize() ) . "\n" .
512 " " . Xml
::element( 'sha1base36', null, $file->getSha1() ) . "\n" .
513 " " . Xml
::element( 'rel', null, $file->getRel() ) . "\n" .
519 * Return prefixed text form of title, but using the content language's
520 * canonical namespace. This skips any special-casing such as gendered
521 * user namespaces -- which while useful, are not yet listed in the
522 * XML "<siteinfo>" data so are unsafe in export.
524 * @param Title $title
528 public static function canonicalTitle( Title
$title ) {
529 if ( $title->isExternal() ) {
530 return $title->getPrefixedText();
533 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
534 getFormattedNsText( $title->getNamespace() );
536 // @todo Emit some kind of warning to the user if $title->getNamespace() !==
537 // NS_MAIN and $prefix === '' (viz. pages in an unregistered namespace)
539 if ( $prefix !== '' ) {
543 return $prefix . $title->getText();