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
;
35 * Title of the currently processed page
39 private $currentTitle = null;
42 * Opens the XML output stream's root "<mediawiki>" element.
43 * This does not include an xml directive, so is safe to include
44 * as a subelement in a larger XML stream. Namespace and XML Schema
45 * references are included.
47 * Output will be encoded in UTF-8.
51 function openStream() {
52 $ver = WikiExporter
::schemaVersion();
53 return Xml
::element( 'mediawiki', [
54 'xmlns' => "http://www.mediawiki.org/xml/export-$ver/",
55 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
57 * When a new version of the schema is created, it needs staging on mediawiki.org.
58 * This requires a change in the operations/mediawiki-config git repo.
60 * Create a changeset like https://gerrit.wikimedia.org/r/#/c/149643/ in which
61 * you copy in the new xsd file.
63 * After it is reviewed, merged and deployed (sync-docroot), the index.html needs purging.
64 * echo "https://www.mediawiki.org/xml/index.html" | mwscript purgeList.php --wiki=aawiki
66 'xsi:schemaLocation' => "http://www.mediawiki.org/xml/export-$ver/ " .
67 "http://www.mediawiki.org/xml/export-$ver.xsd",
69 'xml:lang' => MediaWikiServices
::getInstance()->getContentLanguage()->getHtmlCode() ],
85 $this->namespaces() ];
86 return " <siteinfo>\n " .
87 implode( "\n ", $info ) .
96 return Xml
::element( 'sitename', [], $wgSitename );
104 return Xml
::element( 'dbname', [], $wgDBname );
110 function generator() {
112 return Xml
::element( 'generator', [], "MediaWiki $wgVersion" );
118 function homelink() {
119 return Xml
::element( 'base', [], Title
::newMainPage()->getCanonicalURL() );
125 function caseSetting() {
126 global $wgCapitalLinks;
127 // "case-insensitive" option is reserved for future
128 $sensitivity = $wgCapitalLinks ?
'first-letter' : 'case-sensitive';
129 return Xml
::element( 'case', [], $sensitivity );
135 function namespaces() {
136 $spaces = "<namespaces>\n";
138 MediaWikiServices
::getInstance()->getContentLanguage()->getFormattedNamespaces()
142 Xml
::element( 'namespace',
145 'case' => MWNamespace
::isCapitalized( $ns ) ?
'first-letter' : 'case-sensitive',
148 $spaces .= " </namespaces>";
153 * Closes the output stream with the closing root element.
154 * Call when finished dumping things.
158 function closeStream() {
159 return "</mediawiki>\n";
163 * Opens a "<page>" section on the output stream, with data
164 * from the given database row.
169 public function openPage( $row ) {
171 $this->currentTitle
= Title
::makeTitle( $row->page_namespace
, $row->page_title
);
172 $canonicalTitle = self
::canonicalTitle( $this->currentTitle
);
173 $out .= ' ' . Xml
::elementClean( 'title', [], $canonicalTitle ) . "\n";
174 $out .= ' ' . Xml
::element( 'ns', [], strval( $row->page_namespace
) ) . "\n";
175 $out .= ' ' . Xml
::element( 'id', [], strval( $row->page_id
) ) . "\n";
176 if ( $row->page_is_redirect
) {
177 $page = WikiPage
::factory( $this->currentTitle
);
178 $redirect = $page->getRedirectTarget();
179 if ( $redirect instanceof Title
&& $redirect->isValidRedirectTarget() ) {
181 $out .= Xml
::element( 'redirect', [ 'title' => self
::canonicalTitle( $redirect ) ] );
186 if ( $row->page_restrictions
!= '' ) {
187 $out .= ' ' . Xml
::element( 'restrictions', [],
188 strval( $row->page_restrictions
) ) . "\n";
191 Hooks
::run( 'XmlDumpWriterOpenPage', [ $this, &$out, $row, $this->currentTitle
] );
197 * Closes a "<page>" section on the output stream.
202 function closePage() {
207 * @return RevisionStore
209 private function getRevisionStore() {
210 return MediaWikiServices
::getInstance()->getRevisionStore();
214 * @return SqlBlobStore
216 private function getBlobStore() {
217 return MediaWikiServices
::getInstance()->getBlobStore();
221 * Dumps a "<revision>" section on the output stream, with
222 * data filled in from the given database row.
228 function writeRevision( $row ) {
229 $out = " <revision>\n";
230 $out .= " " . Xml
::element( 'id', null, strval( $row->rev_id
) ) . "\n";
231 if ( isset( $row->rev_parent_id
) && $row->rev_parent_id
) {
232 $out .= " " . Xml
::element( 'parentid', null, strval( $row->rev_parent_id
) ) . "\n";
235 $out .= $this->writeTimestamp( $row->rev_timestamp
);
237 if ( isset( $row->rev_deleted
) && ( $row->rev_deleted
& Revision
::DELETED_USER
) ) {
238 $out .= " " . Xml
::element( 'contributor', [ 'deleted' => 'deleted' ] ) . "\n";
240 $out .= $this->writeContributor( $row->rev_user
, $row->rev_user_text
);
243 if ( isset( $row->rev_minor_edit
) && $row->rev_minor_edit
) {
244 $out .= " <minor/>\n";
246 if ( isset( $row->rev_deleted
) && ( $row->rev_deleted
& Revision
::DELETED_COMMENT
) ) {
247 $out .= " " . Xml
::element( 'comment', [ 'deleted' => 'deleted' ] ) . "\n";
249 $comment = CommentStore
::getStore()->getComment( 'rev_comment', $row )->text
;
250 if ( $comment != '' ) {
251 $out .= " " . Xml
::elementClean( 'comment', [], strval( $comment ) ) . "\n";
255 // TODO: rev_content_model no longer exists with MCR, see T174031
256 if ( isset( $row->rev_content_model
) && !is_null( $row->rev_content_model
) ) {
257 $content_model = strval( $row->rev_content_model
);
259 // probably using $wgContentHandlerUseDB = false;
260 $content_model = ContentHandler
::getDefaultModelFor( $this->currentTitle
);
263 $content_handler = ContentHandler
::getForModelID( $content_model );
265 // TODO: rev_content_format no longer exists with MCR, see T174031
266 if ( isset( $row->rev_content_format
) && !is_null( $row->rev_content_format
) ) {
267 $content_format = strval( $row->rev_content_format
);
269 // probably using $wgContentHandlerUseDB = false;
270 $content_format = $content_handler->getDefaultFormat();
273 $out .= " " . Xml
::element( 'model', null, strval( $content_model ) ) . "\n";
274 $out .= " " . Xml
::element( 'format', null, strval( $content_format ) ) . "\n";
277 if ( isset( $row->rev_deleted
) && ( $row->rev_deleted
& Revision
::DELETED_TEXT
) ) {
278 $out .= " " . Xml
::element( 'text', [ 'deleted' => 'deleted' ] ) . "\n";
279 } elseif ( isset( $row->old_text
) ) {
280 // Raw text from the database may have invalid chars
281 $text = strval( Revision
::getRevisionText( $row ) );
282 $text = $content_handler->exportTransform( $text, $content_format );
283 $out .= " " . Xml
::elementClean( 'text',
284 [ 'xml:space' => 'preserve', 'bytes' => intval( $row->rev_len
) ],
285 strval( $text ) ) . "\n";
286 } elseif ( isset( $row->_load_content
) ) {
287 // TODO: make this fully MCR aware, see T174031
288 $rev = $this->getRevisionStore()->newRevisionFromRow( $row, 0, $this->currentTitle
);
289 $slot = $rev->getSlot( 'main' );
290 $content = $slot->getContent();
292 if ( $content instanceof TextContent
) {
293 // HACK: For text based models, bypass the serialization step.
294 // This allows extensions (like Flow)that use incompatible combinations
295 // of serialization format and content model.
296 $text = $content->getNativeData();
298 $text = $content->serialize( $content_format );
301 $text = $content_handler->exportTransform( $text, $content_format );
302 $out .= " " . Xml
::elementClean( 'text',
303 [ 'xml:space' => 'preserve', 'bytes' => intval( $slot->getSize() ) ],
304 strval( $text ) ) . "\n";
305 } elseif ( isset( $row->rev_text_id
) ) {
306 // Stub output for pre-MCR schema
307 // TODO: MCR: rev_text_id only exists in the pre-MCR schema. Remove this when
308 // we drop support for the old schema.
309 $out .= " " . Xml
::element( 'text',
310 [ 'id' => $row->rev_text_id
, 'bytes' => intval( $row->rev_len
) ],
313 // Backwards-compatible stub output for MCR aware schema
314 // TODO: MCR: emit content addresses instead of text ids, see T174031, T199121
315 $rev = $this->getRevisionStore()->newRevisionFromRow( $row, 0, $this->currentTitle
);
316 $slot = $rev->getSlot( 'main' );
318 // Note that this is currently the ONLY reason we have a BlobStore here at all.
319 // When removing this line, check whether the BlobStore has become unused.
320 $textId = $this->getBlobStore()->getTextIdFromAddress( $slot->getAddress() );
321 $out .= " " . Xml
::element( 'text',
322 [ 'id' => $textId, 'bytes' => intval( $slot->getSize() ) ],
326 if ( isset( $row->rev_sha1
)
328 && !( $row->rev_deleted
& Revision
::DELETED_TEXT
)
330 $out .= " " . Xml
::element( 'sha1', null, strval( $row->rev_sha1
) ) . "\n";
332 $out .= " <sha1/>\n";
335 // Avoid PHP 7.1 warning from passing $this by reference
337 Hooks
::run( 'XmlDumpWriterWriteRevision', [ &$writer, &$out, $row, $text ] );
339 $out .= " </revision>\n";
345 * Dumps a "<logitem>" section on the output stream, with
346 * data filled in from the given database row.
352 function writeLogItem( $row ) {
353 $out = " <logitem>\n";
354 $out .= " " . Xml
::element( 'id', null, strval( $row->log_id
) ) . "\n";
356 $out .= $this->writeTimestamp( $row->log_timestamp
, " " );
358 if ( $row->log_deleted
& LogPage
::DELETED_USER
) {
359 $out .= " " . Xml
::element( 'contributor', [ 'deleted' => 'deleted' ] ) . "\n";
361 $out .= $this->writeContributor( $row->log_user
, $row->user_name
, " " );
364 if ( $row->log_deleted
& LogPage
::DELETED_COMMENT
) {
365 $out .= " " . Xml
::element( 'comment', [ 'deleted' => 'deleted' ] ) . "\n";
367 $comment = CommentStore
::getStore()->getComment( 'log_comment', $row )->text
;
368 if ( $comment != '' ) {
369 $out .= " " . Xml
::elementClean( 'comment', null, strval( $comment ) ) . "\n";
373 $out .= " " . Xml
::element( 'type', null, strval( $row->log_type
) ) . "\n";
374 $out .= " " . Xml
::element( 'action', null, strval( $row->log_action
) ) . "\n";
376 if ( $row->log_deleted
& LogPage
::DELETED_ACTION
) {
377 $out .= " " . Xml
::element( 'text', [ 'deleted' => 'deleted' ] ) . "\n";
379 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
380 $out .= " " . Xml
::elementClean( 'logtitle', null, self
::canonicalTitle( $title ) ) . "\n";
381 $out .= " " . Xml
::elementClean( 'params',
382 [ 'xml:space' => 'preserve' ],
383 strval( $row->log_params
) ) . "\n";
386 $out .= " </logitem>\n";
392 * @param string $timestamp
393 * @param string $indent Default to six spaces
396 function writeTimestamp( $timestamp, $indent = " " ) {
397 $ts = wfTimestamp( TS_ISO_8601
, $timestamp );
398 return $indent . Xml
::element( 'timestamp', null, $ts ) . "\n";
403 * @param string $text
404 * @param string $indent Default to six spaces
407 function writeContributor( $id, $text, $indent = " " ) {
408 $out = $indent . "<contributor>\n";
409 if ( $id ||
!IP
::isValid( $text ) ) {
410 $out .= $indent . " " . Xml
::elementClean( 'username', null, strval( $text ) ) . "\n";
411 $out .= $indent . " " . Xml
::element( 'id', null, strval( $id ) ) . "\n";
413 $out .= $indent . " " . Xml
::elementClean( 'ip', null, strval( $text ) ) . "\n";
415 $out .= $indent . "</contributor>\n";
420 * Warning! This data is potentially inconsistent. :(
422 * @param bool $dumpContents
425 function writeUploads( $row, $dumpContents = false ) {
426 if ( $row->page_namespace
== NS_FILE
) {
427 $img = wfLocalFile( $row->page_title
);
428 if ( $img && $img->exists() ) {
430 foreach ( array_reverse( $img->getHistory() ) as $ver ) {
431 $out .= $this->writeUpload( $ver, $dumpContents );
433 $out .= $this->writeUpload( $img, $dumpContents );
442 * @param bool $dumpContents
445 function writeUpload( $file, $dumpContents = false ) {
446 if ( $file->isOld() ) {
448 Xml
::element( 'archivename', null, $file->getArchiveName() ) . "\n";
452 if ( $dumpContents ) {
453 $be = $file->getRepo()->getBackend();
454 # Dump file as base64
455 # Uses only XML-safe characters, so does not need escaping
456 # @todo Too bad this loads the contents into memory (script might swap)
457 $contents = ' <contents encoding="base64">' .
458 chunk_split( base64_encode(
459 $be->getFileContents( [ 'src' => $file->getPath() ] ) ) ) .
464 if ( $file->isDeleted( File
::DELETED_COMMENT
) ) {
465 $comment = Xml
::element( 'comment', [ 'deleted' => 'deleted' ] );
467 $comment = Xml
::elementClean( 'comment', null, strval( $file->getDescription() ) );
469 return " <upload>\n" .
470 $this->writeTimestamp( $file->getTimestamp() ) .
471 $this->writeContributor( $file->getUser( 'id' ), $file->getUser( 'text' ) ) .
472 " " . $comment . "\n" .
473 " " . Xml
::element( 'filename', null, $file->getName() ) . "\n" .
475 " " . Xml
::element( 'src', null, $file->getCanonicalUrl() ) . "\n" .
476 " " . Xml
::element( 'size', null, $file->getSize() ) . "\n" .
477 " " . Xml
::element( 'sha1base36', null, $file->getSha1() ) . "\n" .
478 " " . Xml
::element( 'rel', null, $file->getRel() ) . "\n" .
484 * Return prefixed text form of title, but using the content language's
485 * canonical namespace. This skips any special-casing such as gendered
486 * user namespaces -- which while useful, are not yet listed in the
487 * XML "<siteinfo>" data so are unsafe in export.
489 * @param Title $title
493 public static function canonicalTitle( Title
$title ) {
494 if ( $title->isExternal() ) {
495 return $title->getPrefixedText();
498 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
499 getFormattedNsText( $title->getNamespace() );
501 // @todo Emit some kind of warning to the user if $title->getNamespace() !==
502 // NS_MAIN and $prefix === '' (viz. pages in an unregistered namespace)
504 if ( $prefix !== '' ) {
508 return $prefix . $title->getText();