39use Wikimedia\Assert\Assert;
57 private const WRITE_STUB_DELETED = 2;
63 public static $supportedSchemas = [
73 private $schemaVersion;
80 private $currentTitle =
null;
91 private $commentStore;
102 $contentMode = self::WRITE_CONTENT,
108 in_array( $contentMode, [ self::WRITE_CONTENT, self::WRITE_STUB ],
true ),
110 'must be one of the following constants: WRITE_CONTENT or WRITE_STUB.'
114 in_array( $schemaVersion, self::$supportedSchemas,
true ),
116 'must be one of the following schema versions: '
117 . implode(
',', self::$supportedSchemas )
120 $this->contentMode = $contentMode;
121 $this->schemaVersion = $schemaVersion;
123 $hookContainer ?? MediaWikiServices::getInstance()->getHookContainer()
125 $this->commentStore = $commentStore ?? MediaWikiServices::getInstance()->getCommentStore();
139 $ver = $this->schemaVersion;
140 return Xml::element(
'mediawiki', [
141 'xmlns' =>
"http://www.mediawiki.org/xml/export-$ver/",
142 'xmlns:xsi' =>
"http://www.w3.org/2001/XMLSchema-instance",
153 'xsi:schemaLocation' =>
"http://www.mediawiki.org/xml/export-$ver/ " .
154 "http://www.mediawiki.org/xml/export-$ver.xsd",
156 'xml:lang' => MediaWikiServices::getInstance()->getContentLanguage()->getHtmlCode() ],
165 private function siteInfo() {
171 $this->caseSetting(),
172 $this->namespaces() ];
173 return " <siteinfo>\n " .
174 implode(
"\n ", $info ) .
181 private function sitename() {
182 $sitename = MediaWikiServices::getInstance()->getMainConfig()->get(
183 MainConfigNames::Sitename );
190 private function dbname() {
191 $dbname = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::DBname );
198 private function generator() {
205 private function homelink() {
206 return Xml::element(
'base', [], Title::newMainPage()->getCanonicalURL() );
212 private function caseSetting() {
213 $capitalLinks = MediaWikiServices::getInstance()->getMainConfig()->get(
214 MainConfigNames::CapitalLinks );
216 $sensitivity = $capitalLinks ?
'first-letter' :
'case-sensitive';
223 private function namespaces() {
224 $spaces =
"<namespaces>\n";
225 $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
227 MediaWikiServices::getInstance()->getContentLanguage()->getFormattedNamespaces()
234 'case' => $nsInfo->isCapitalized( $ns )
235 ?
'first-letter' :
'case-sensitive',
238 $spaces .=
" </namespaces>";
249 return "</mediawiki>\n";
261 $this->currentTitle = Title::newFromRow( $row );
262 $canonicalTitle = self::canonicalTitle( $this->currentTitle );
263 $out .=
' ' . Xml::elementClean(
'title', [], $canonicalTitle ) .
"\n";
264 $out .=
' ' . Xml::element(
'ns', [], strval( $row->page_namespace ) ) .
"\n";
265 $out .=
' ' . Xml::element(
'id', [], strval( $row->page_id ) ) .
"\n";
266 if ( $row->page_is_redirect ) {
267 $services = MediaWikiServices::getInstance();
268 $page = $services->getWikiPageFactory()->newFromTitle( $this->currentTitle );
269 $redirectStore = $services->getRedirectStore();
270 $redirect = $this->invokeLenient(
271 static function () use ( $page, $redirectStore ) {
272 return $redirectStore->getRedirectTarget( $page );
274 'Failed to get redirect target of page ' . $page->getId()
278 $out .= Xml::element(
'redirect', [
'title' => self::canonicalTitle( $redirect ) ] );
282 $this->hookRunner->onXmlDumpWriterOpenPage( $this, $out, $row, $this->currentTitle );
294 if ( $this->currentTitle !==
null ) {
295 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
298 $linkCache->clearLink( $this->currentTitle );
306 private function getRevisionStore() {
307 return MediaWikiServices::getInstance()->getRevisionStore();
313 private function getBlobStore() {
315 return MediaWikiServices::getInstance()->getBlobStore();
327 private function invokeLenient( $callback, $warning ) {
332 }
catch (
MWException | RuntimeException | InvalidArgumentException | ErrorException $ex ) {
333 MWDebug::warning( $warning .
': ' . $ex->getMessage() );
349 $rev = $this->getRevisionStore()->newRevisionFromRowAndSlots(
356 $out =
" <revision>\n";
357 $out .=
" " . Xml::element(
'id',
null, strval( $rev->getId() ) ) .
"\n";
359 if ( $rev->getParentId() ) {
360 $out .=
" " . Xml::element(
'parentid',
null, strval( $rev->getParentId() ) ) .
"\n";
365 if ( $rev->isDeleted( RevisionRecord::DELETED_USER ) ) {
366 $out .=
" " . Xml::element(
'contributor', [
'deleted' =>
'deleted' ] ) .
"\n";
369 $user = $rev->getUser();
371 $user ? $user->getId() : 0,
372 $user ? $user->getName() :
''
376 if ( $rev->isMinor() ) {
377 $out .=
" <minor/>\n";
379 if ( $rev->isDeleted( RevisionRecord::DELETED_COMMENT ) ) {
380 $out .=
" " . Xml::element(
'comment', [
'deleted' =>
'deleted' ] ) .
"\n";
382 if ( $rev->getComment()->text !=
'' ) {
384 . Xml::elementClean(
'comment', [], strval( $rev->getComment()->text ) )
389 $contentMode = $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ? self::WRITE_STUB_DELETED
390 : $this->contentMode;
392 $slots = $rev->getSlots()->getSlots();
396 $out .= $this->writeSlot( $slots[SlotRecord::MAIN], $contentMode );
398 foreach ( $slots as $role => $slot ) {
399 if ( $role === SlotRecord::MAIN ) {
402 $out .= $this->writeSlot( $slot, $contentMode );
405 if ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
406 $out .=
" <sha1/>\n";
408 $sha1 = $this->invokeLenient(
409 static function () use ( $rev ) {
410 return $rev->getSha1();
412 'failed to determine sha1 for revision ' . $rev->getId()
414 $out .=
" " . Xml::element(
'sha1',
null, strval( $sha1 ) ) .
"\n";
418 if ( $contentMode === self::WRITE_CONTENT ) {
420 $content = $this->invokeLenient(
421 static function () use ( $rev ) {
422 return $rev->getContent( SlotRecord::MAIN, RevisionRecord::RAW );
424 'Failed to load main slot content of revision ' . $rev->getId()
427 $text = $content ? $content->
serialize() :
'';
429 $this->hookRunner->onXmlDumpWriterWriteRevision( $this, $out, $row, $text, $rev );
431 $out .=
" </revision>\n";
442 private function writeSlot(
SlotRecord $slot, $contentMode ) {
443 $isMain = $slot->
getRole() === SlotRecord::MAIN;
446 if ( !$isV11 && !$isMain ) {
466 $contentHandler = MediaWikiServices::getInstance()
467 ->getContentHandlerFactory()
468 ->getContentHandler( $contentModel );
469 $contentFormat = $contentHandler->getDefaultFormat();
473 $out .= $indent .
Xml::element(
'model',
null, strval( $contentModel ) ) .
"\n";
474 $out .= $indent .
Xml::element(
'format',
null, strval( $contentFormat ) ) .
"\n";
477 'bytes' => $this->invokeLenient(
478 static function () use ( $slot ) {
481 'failed to determine size for slot ' . $slot->
getRole() .
' of revision '
487 $textAttributes[
'sha1'] = $this->invokeLenient(
488 static function () use ( $slot ) {
491 'failed to determine sha1 for slot ' . $slot->
getRole() .
' of revision '
496 if ( $contentMode === self::WRITE_CONTENT ) {
497 $content = $this->invokeLenient(
498 static function () use ( $slot ) {
501 'failed to load content for slot ' . $slot->
getRole() .
' of revision '
505 if ( $content ===
null ) {
506 $out .= $indent .
Xml::element(
'text', $textAttributes ) .
"\n";
508 $out .= $this->writeText( $content, $textAttributes, $indent );
510 } elseif ( $contentMode === self::WRITE_STUB_DELETED ) {
512 $textAttributes[
'deleted'] =
'deleted';
513 $out .= $indent .
Xml::element(
'text', $textAttributes ) .
"\n";
517 $textAttributes[
'location'] = $slot->
getAddress();
531 [ $schema, $textId ] = $this->getBlobStore()->splitBlobAddress( $slot->
getAddress() );
532 }
catch ( InvalidArgumentException $ex ) {
533 MWDebug::warning(
'Bad content address for slot ' . $slot->
getRole()
534 .
' of revision ' . $slot->
getRevision() .
': ' . $ex->getMessage() );
538 if ( $schema ===
'tt' ) {
539 $textAttributes[
'id'] = $textId;
540 } elseif ( $schema ===
'es' ) {
541 $textAttributes[
'id'] = bin2hex( $textId );
545 $out .= $indent .
Xml::element(
'text', $textAttributes ) .
"\n";
562 private function writeText(
Content $content, $textAttributes, $indent ) {
564 $contentFormat = $contentHandler->getDefaultFormat();
569 $data = $content->getText();
571 $data = $content->
serialize( $contentFormat );
574 $data = $contentHandler->exportTransform( $data, $contentFormat );
576 $textAttributes[
'bytes'] = strlen( $data );
577 $textAttributes[
'xml:space'] =
'preserve';
578 return $indent .
Xml::elementClean(
'text', $textAttributes, strval( $data ) ) .
"\n";
589 $out =
" <logitem>\n";
590 $out .=
" " . Xml::element(
'id',
null, strval( $row->log_id ) ) .
"\n";
594 if ( $row->log_deleted & LogPage::DELETED_USER ) {
595 $out .=
" " . Xml::element(
'contributor', [
'deleted' =>
'deleted' ] ) .
"\n";
600 if ( $row->log_deleted & LogPage::DELETED_COMMENT ) {
601 $out .=
" " . Xml::element(
'comment', [
'deleted' =>
'deleted' ] ) .
"\n";
603 $comment = $this->commentStore->getComment(
'log_comment', $row )->text;
604 if ( $comment !=
'' ) {
605 $out .=
" " . Xml::elementClean(
'comment',
null, strval( $comment ) ) .
"\n";
609 $out .=
" " . Xml::element(
'type',
null, strval( $row->log_type ) ) .
"\n";
610 $out .=
" " . Xml::element(
'action',
null, strval( $row->log_action ) ) .
"\n";
612 if ( $row->log_deleted & LogPage::DELETED_ACTION ) {
613 $out .=
" " . Xml::element(
'text', [
'deleted' =>
'deleted' ] ) .
"\n";
615 $title = Title::makeTitle( $row->log_namespace, $row->log_title );
616 $out .=
" " . Xml::elementClean(
'logtitle',
null, self::canonicalTitle( $title ) ) .
"\n";
617 $out .=
" " . Xml::elementClean(
'params',
618 [
'xml:space' =>
'preserve' ],
619 strval( $row->log_params ) ) .
"\n";
622 $out .=
" </logitem>\n";
634 return $indent . Xml::element(
'timestamp',
null, $ts ) .
"\n";
644 $out = $indent .
"<contributor>\n";
645 if ( $id || !IPUtils::isValid( $text ) ) {
646 $out .= $indent .
" " . Xml::elementClean(
'username',
null, strval( $text ) ) .
"\n";
647 $out .= $indent .
" " . Xml::element(
'id',
null, strval( $id ) ) .
"\n";
649 $out .= $indent .
" " . Xml::elementClean(
'ip',
null, strval( $text ) ) .
"\n";
651 $out .= $indent .
"</contributor>\n";
662 if ( $row->page_namespace ==
NS_FILE ) {
663 $img = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()
664 ->newFile( $row->page_title );
665 if ( $img && $img->exists() ) {
667 foreach ( array_reverse( $img->getHistory() ) as $ver ) {
668 $out .= $this->writeUpload( $ver, $dumpContents );
670 $out .= $this->writeUpload( $img, $dumpContents );
682 private function writeUpload( $file, $dumpContents =
false ) {
683 if ( $file->isOld() ) {
685 '@phan-var OldLocalFile $file';
687 Xml::element(
'archivename',
null, $file->getArchiveName() ) .
"\n";
691 if ( $dumpContents ) {
692 $be = $file->getRepo()->getBackend();
693 # Dump file as base64
694 # Uses only XML-safe characters, so does not need escaping
695 # @todo Too bad this loads the contents into memory (script might swap)
696 $contents =
' <contents encoding="base64">' .
697 chunk_split( base64_encode(
698 $be->getFileContents( [
'src' => $file->getPath() ] ) ) ) .
703 $uploader = $file->getUploader( File::FOR_PUBLIC );
705 $uploader = $this->
writeContributor( $uploader->getId(), $uploader->getName() );
707 $uploader =
Xml::element(
'contributor', [
'deleted' =>
'deleted' ] ) .
"\n";
709 $comment = $file->getDescription( File::FOR_PUBLIC );
710 if ( ( $comment ??
'' ) !==
'' ) {
713 $comment =
Xml::element(
'comment', [
'deleted' =>
'deleted' ] );
715 return " <upload>\n" .
718 " " . $comment .
"\n" .
719 " " .
Xml::element(
'filename',
null, $file->getName() ) .
"\n" .
721 " " .
Xml::element(
'src',
null, $file->getCanonicalUrl() ) .
"\n" .
722 " " .
Xml::element(
'size',
null, (
string)( $file->getSize() ?: 0 ) ) .
"\n" .
723 " " .
Xml::
element(
'sha1base36', null, $file->getSha1() ) .
"\n" .
724 " " .
Xml::
element(
'rel', null, $file->getRel() ) .
"\n" .
744 $prefix = MediaWikiServices::getInstance()->getContentLanguage()->
750 if ( $prefix !==
'' ) {
754 return $prefix . $title->
getText();
const MW_VERSION
The running version of MediaWiki.
const XML_DUMP_SCHEMA_VERSION_11
const XML_DUMP_SCHEMA_VERSION_10
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
A class containing constants representing the names of configuration variables.
Content object implementation for representing flat text.
closeStream()
Closes the output stream with the closing root element.
static string[] $supportedSchemas
the schema versions supported for output @final
static canonicalTitle(Title $title)
Return prefixed text form of title, but using the content language's canonical namespace.
const WRITE_STUB
Only output subs for revision content.
writeLogItem( $row)
Dumps a "<logitem>" section on the output stream, with data filled in from the given database row.
writeTimestamp( $timestamp, $indent=" ")
const WRITE_CONTENT
Output serialized revision content.
writeUploads( $row, $dumpContents=false)
Warning! This data is potentially inconsistent.
closePage()
Closes a "<page>" section on the output stream.
openStream()
Opens the XML output stream's root "<mediawiki>" element.
writeRevision( $row, $slotRows=null)
Dumps a "<revision>" section on the output stream, with data filled in from the given database row.
openPage( $row)
Opens a "<page>" section on the output stream, with data from the given database row.
__construct( $contentMode=self::WRITE_CONTENT, $schemaVersion=XML_DUMP_SCHEMA_VERSION_11, ?HookContainer $hookContainer=null, ?CommentStore $commentStore=null)
writeContributor( $id, $text, $indent=" ")
Module of static functions for generating XML.
static closeElement( $element)
Shortcut to close an XML element.
static openElement( $element, $attribs=null)
This opens an XML element.
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
static elementClean( $element, $attribs=[], $contents='')
Format an XML element as with self::element(), but run text through the content language's normalize(...
Base interface for representing page content.
getContentHandler()
Convenience method that returns the ContentHandler singleton for handling the content model that this...
serialize( $format=null)
Convenience method for serializing this Content object.