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() {
214 * @return RevisionStore
216 private function getRevisionStore() {
217 return MediaWikiServices
::getInstance()->getRevisionStore();
221 * @return SqlBlobStore
223 private function getBlobStore() {
224 return MediaWikiServices
::getInstance()->getBlobStore();
228 * Dumps a "<revision>" section on the output stream, with
229 * data filled in from the given database row.
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";
242 $out .= $this->writeTimestamp( $row->rev_timestamp
);
244 if ( isset( $row->rev_deleted
) && ( $row->rev_deleted
& Revision
::DELETED_USER
) ) {
245 $out .= " " . Xml
::element( 'contributor', [ 'deleted' => 'deleted' ] ) . "\n";
247 $out .= $this->writeContributor( $row->rev_user
, $row->rev_user_text
);
250 if ( isset( $row->rev_minor_edit
) && $row->rev_minor_edit
) {
251 $out .= " <minor/>\n";
253 if ( isset( $row->rev_deleted
) && ( $row->rev_deleted
& Revision
::DELETED_COMMENT
) ) {
254 $out .= " " . Xml
::element( 'comment', [ 'deleted' => 'deleted' ] ) . "\n";
256 $comment = CommentStore
::getStore()->getComment( 'rev_comment', $row )->text
;
257 if ( $comment != '' ) {
258 $out .= " " . Xml
::elementClean( 'comment', [], strval( $comment ) ) . "\n";
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
);
266 // probably using $wgContentHandlerUseDB = false;
267 $content_model = ContentHandler
::getDefaultModelFor( $this->currentTitle
);
270 $content_handler = ContentHandler
::getForModelID( $content_model );
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
);
276 // probably using $wgContentHandlerUseDB = false;
277 $content_format = $content_handler->getDefaultFormat();
280 $out .= " " . Xml
::element( 'model', null, strval( $content_model ) ) . "\n";
281 $out .= " " . Xml
::element( 'format', null, strval( $content_format ) ) . "\n";
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 $text = $content_handler->exportTransform( $text, $content_format );
290 $out .= " " . Xml
::elementClean( 'text',
291 [ 'xml:space' => 'preserve', 'bytes' => intval( $row->rev_len
) ],
292 strval( $text ) ) . "\n";
293 } elseif ( isset( $row->_load_content
) ) {
294 // TODO: make this fully MCR aware, see T174031
295 $rev = $this->getRevisionStore()->newRevisionFromRow( $row, 0, $this->currentTitle
);
296 $slot = $rev->getSlot( 'main' );
297 $content = $slot->getContent();
299 if ( $content instanceof TextContent
) {
300 // HACK: For text based models, bypass the serialization step.
301 // This allows extensions (like Flow)that use incompatible combinations
302 // of serialization format and content model.
303 $text = $content->getNativeData();
305 $text = $content->serialize( $content_format );
308 $text = $content_handler->exportTransform( $text, $content_format );
309 $out .= " " . Xml
::elementClean( 'text',
310 [ 'xml:space' => 'preserve', 'bytes' => intval( $slot->getSize() ) ],
311 strval( $text ) ) . "\n";
312 } elseif ( isset( $row->rev_text_id
) ) {
313 // Stub output for pre-MCR schema
314 // TODO: MCR: rev_text_id only exists in the pre-MCR schema. Remove this when
315 // we drop support for the old schema.
316 $out .= " " . Xml
::element( 'text',
317 [ 'id' => $row->rev_text_id
, 'bytes' => intval( $row->rev_len
) ],
320 // Backwards-compatible stub output for MCR aware schema
321 // TODO: MCR: emit content addresses instead of text ids, see T174031, T199121
322 $rev = $this->getRevisionStore()->newRevisionFromRow( $row, 0, $this->currentTitle
);
323 $slot = $rev->getSlot( 'main' );
325 // Note that this is currently the ONLY reason we have a BlobStore here at all.
326 // When removing this line, check whether the BlobStore has become unused.
327 $textId = $this->getBlobStore()->getTextIdFromAddress( $slot->getAddress() );
328 $out .= " " . Xml
::element( 'text',
329 [ 'id' => $textId, 'bytes' => intval( $slot->getSize() ) ],
333 if ( isset( $row->rev_sha1
)
335 && !( $row->rev_deleted
& Revision
::DELETED_TEXT
)
337 $out .= " " . Xml
::element( 'sha1', null, strval( $row->rev_sha1
) ) . "\n";
339 $out .= " <sha1/>\n";
342 // Avoid PHP 7.1 warning from passing $this by reference
344 Hooks
::run( 'XmlDumpWriterWriteRevision', [ &$writer, &$out, $row, $text ] );
346 $out .= " </revision>\n";
352 * Dumps a "<logitem>" section on the output stream, with
353 * data filled in from the given database row.
359 function writeLogItem( $row ) {
360 $out = " <logitem>\n";
361 $out .= " " . Xml
::element( 'id', null, strval( $row->log_id
) ) . "\n";
363 $out .= $this->writeTimestamp( $row->log_timestamp
, " " );
365 if ( $row->log_deleted
& LogPage
::DELETED_USER
) {
366 $out .= " " . Xml
::element( 'contributor', [ 'deleted' => 'deleted' ] ) . "\n";
368 $out .= $this->writeContributor( $row->log_user
, $row->user_name
, " " );
371 if ( $row->log_deleted
& LogPage
::DELETED_COMMENT
) {
372 $out .= " " . Xml
::element( 'comment', [ 'deleted' => 'deleted' ] ) . "\n";
374 $comment = CommentStore
::getStore()->getComment( 'log_comment', $row )->text
;
375 if ( $comment != '' ) {
376 $out .= " " . Xml
::elementClean( 'comment', null, strval( $comment ) ) . "\n";
380 $out .= " " . Xml
::element( 'type', null, strval( $row->log_type
) ) . "\n";
381 $out .= " " . Xml
::element( 'action', null, strval( $row->log_action
) ) . "\n";
383 if ( $row->log_deleted
& LogPage
::DELETED_ACTION
) {
384 $out .= " " . Xml
::element( 'text', [ 'deleted' => 'deleted' ] ) . "\n";
386 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
387 $out .= " " . Xml
::elementClean( 'logtitle', null, self
::canonicalTitle( $title ) ) . "\n";
388 $out .= " " . Xml
::elementClean( 'params',
389 [ 'xml:space' => 'preserve' ],
390 strval( $row->log_params
) ) . "\n";
393 $out .= " </logitem>\n";
399 * @param string $timestamp
400 * @param string $indent Default to six spaces
403 function writeTimestamp( $timestamp, $indent = " " ) {
404 $ts = wfTimestamp( TS_ISO_8601
, $timestamp );
405 return $indent . Xml
::element( 'timestamp', null, $ts ) . "\n";
410 * @param string $text
411 * @param string $indent Default to six spaces
414 function writeContributor( $id, $text, $indent = " " ) {
415 $out = $indent . "<contributor>\n";
416 if ( $id ||
!IP
::isValid( $text ) ) {
417 $out .= $indent . " " . Xml
::elementClean( 'username', null, strval( $text ) ) . "\n";
418 $out .= $indent . " " . Xml
::element( 'id', null, strval( $id ) ) . "\n";
420 $out .= $indent . " " . Xml
::elementClean( 'ip', null, strval( $text ) ) . "\n";
422 $out .= $indent . "</contributor>\n";
427 * Warning! This data is potentially inconsistent. :(
429 * @param bool $dumpContents
432 function writeUploads( $row, $dumpContents = false ) {
433 if ( $row->page_namespace
== NS_FILE
) {
434 $img = wfLocalFile( $row->page_title
);
435 if ( $img && $img->exists() ) {
437 foreach ( array_reverse( $img->getHistory() ) as $ver ) {
438 $out .= $this->writeUpload( $ver, $dumpContents );
440 $out .= $this->writeUpload( $img, $dumpContents );
449 * @param bool $dumpContents
452 function writeUpload( $file, $dumpContents = false ) {
453 if ( $file->isOld() ) {
455 Xml
::element( 'archivename', null, $file->getArchiveName() ) . "\n";
459 if ( $dumpContents ) {
460 $be = $file->getRepo()->getBackend();
461 # Dump file as base64
462 # Uses only XML-safe characters, so does not need escaping
463 # @todo Too bad this loads the contents into memory (script might swap)
464 $contents = ' <contents encoding="base64">' .
465 chunk_split( base64_encode(
466 $be->getFileContents( [ 'src' => $file->getPath() ] ) ) ) .
471 if ( $file->isDeleted( File
::DELETED_COMMENT
) ) {
472 $comment = Xml
::element( 'comment', [ 'deleted' => 'deleted' ] );
474 $comment = Xml
::elementClean( 'comment', null, strval( $file->getDescription() ) );
476 return " <upload>\n" .
477 $this->writeTimestamp( $file->getTimestamp() ) .
478 $this->writeContributor( $file->getUser( 'id' ), $file->getUser( 'text' ) ) .
479 " " . $comment . "\n" .
480 " " . Xml
::element( 'filename', null, $file->getName() ) . "\n" .
482 " " . Xml
::element( 'src', null, $file->getCanonicalUrl() ) . "\n" .
483 " " . Xml
::element( 'size', null, $file->getSize() ) . "\n" .
484 " " . Xml
::element( 'sha1base36', null, $file->getSha1() ) . "\n" .
485 " " . Xml
::element( 'rel', null, $file->getRel() ) . "\n" .
491 * Return prefixed text form of title, but using the content language's
492 * canonical namespace. This skips any special-casing such as gendered
493 * user namespaces -- which while useful, are not yet listed in the
494 * XML "<siteinfo>" data so are unsafe in export.
496 * @param Title $title
500 public static function canonicalTitle( Title
$title ) {
501 if ( $title->isExternal() ) {
502 return $title->getPrefixedText();
505 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
506 getFormattedNsText( $title->getNamespace() );
508 // @todo Emit some kind of warning to the user if $title->getNamespace() !==
509 // NS_MAIN and $prefix === '' (viz. pages in an unregistered namespace)
511 if ( $prefix !== '' ) {
515 return $prefix . $title->getText();