34use Wikimedia\Assert\Assert;
52 private const WRITE_STUB_DELETED = 2;
58 public static $supportedSchemas = [
68 private $schemaVersion;
75 private $currentTitle =
null;
92 $contentMode = self::WRITE_CONTENT,
96 in_array( $contentMode, [ self::WRITE_CONTENT, self::WRITE_STUB ],
true ),
98 'must be one of the following constants: WRITE_CONTENT or WRITE_STUB.'
102 in_array( $schemaVersion, self::$supportedSchemas,
true ),
104 'must be one of the following schema versions: '
105 . implode(
',', self::$supportedSchemas )
108 $this->contentMode = $contentMode;
109 $this->schemaVersion = $schemaVersion;
110 $this->hookRunner =
new HookRunner( MediaWikiServices::getInstance()->getHookContainer() );
124 $ver = $this->schemaVersion;
125 return Xml::element(
'mediawiki', [
126 'xmlns' =>
"http://www.mediawiki.org/xml/export-$ver/",
127 'xmlns:xsi' =>
"http://www.w3.org/2001/XMLSchema-instance",
138 'xsi:schemaLocation' =>
"http://www.mediawiki.org/xml/export-$ver/ " .
139 "http://www.mediawiki.org/xml/export-$ver.xsd",
141 'xml:lang' => MediaWikiServices::getInstance()->getContentLanguage()->getHtmlCode() ],
150 private function siteInfo() {
156 $this->caseSetting(),
157 $this->namespaces() ];
158 return " <siteinfo>\n " .
159 implode(
"\n ", $info ) .
166 private function sitename() {
167 $sitename = MediaWikiServices::getInstance()->getMainConfig()->get(
168 MainConfigNames::Sitename );
175 private function dbname() {
176 $dbname = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::DBname );
183 private function generator() {
190 private function homelink() {
191 return Xml::element(
'base', [], Title::newMainPage()->getCanonicalURL() );
197 private function caseSetting() {
198 $capitalLinks = MediaWikiServices::getInstance()->getMainConfig()->get(
199 MainConfigNames::CapitalLinks );
201 $sensitivity = $capitalLinks ?
'first-letter' :
'case-sensitive';
208 private function namespaces() {
209 $spaces =
"<namespaces>\n";
210 $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
212 MediaWikiServices::getInstance()->getContentLanguage()->getFormattedNamespaces()
219 'case' => $nsInfo->isCapitalized( $ns )
220 ?
'first-letter' :
'case-sensitive',
223 $spaces .=
" </namespaces>";
234 return "</mediawiki>\n";
246 $this->currentTitle = Title::newFromRow( $row );
247 $canonicalTitle = self::canonicalTitle( $this->currentTitle );
248 $out .=
' ' . Xml::elementClean(
'title', [], $canonicalTitle ) .
"\n";
249 $out .=
' ' . Xml::element(
'ns', [], strval( $row->page_namespace ) ) .
"\n";
250 $out .=
' ' . Xml::element(
'id', [], strval( $row->page_id ) ) .
"\n";
251 if ( $row->page_is_redirect ) {
252 $services = MediaWikiServices::getInstance();
253 $page = $services->getWikiPageFactory()->newFromTitle( $this->currentTitle );
254 $redirectStore = $services->getRedirectStore();
255 $redirect = $this->invokeLenient(
256 static function () use ( $page, $redirectStore ) {
257 return $redirectStore->getRedirectTarget( $page );
259 'Failed to get redirect target of page ' . $page->getId()
263 $out .= Xml::element(
'redirect', [
'title' => self::canonicalTitle( $redirect ) ] );
267 $this->hookRunner->onXmlDumpWriterOpenPage( $this, $out, $row, $this->currentTitle );
279 if ( $this->currentTitle !==
null ) {
280 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
283 $linkCache->clearLink( $this->currentTitle );
291 private function getRevisionStore() {
292 return MediaWikiServices::getInstance()->getRevisionStore();
298 private function getBlobStore() {
300 return MediaWikiServices::getInstance()->getBlobStore();
314 private function invokeLenient( $callback, $warning ) {
319 }
catch ( Exception $ex ) {
320 if ( $ex instanceof
MWException || $ex instanceof RuntimeException ||
321 $ex instanceof InvalidArgumentException ) {
342 $rev = $this->getRevisionStore()->newRevisionFromRowAndSlots(
349 $out =
" <revision>\n";
350 $out .=
" " . Xml::element(
'id',
null, strval( $rev->getId() ) ) .
"\n";
352 if ( $rev->getParentId() ) {
353 $out .=
" " . Xml::element(
'parentid',
null, strval( $rev->getParentId() ) ) .
"\n";
358 if ( $rev->isDeleted( RevisionRecord::DELETED_USER ) ) {
359 $out .=
" " . Xml::element(
'contributor', [
'deleted' =>
'deleted' ] ) .
"\n";
362 $user = $rev->getUser();
364 $user ? $user->getId() : 0,
365 $user ? $user->getName() :
''
369 if ( $rev->isMinor() ) {
370 $out .=
" <minor/>\n";
372 if ( $rev->isDeleted( RevisionRecord::DELETED_COMMENT ) ) {
373 $out .=
" " . Xml::element(
'comment', [
'deleted' =>
'deleted' ] ) .
"\n";
375 if ( $rev->getComment()->text !=
'' ) {
377 . Xml::elementClean(
'comment', [], strval( $rev->getComment()->text ) )
382 $contentMode = $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ? self::WRITE_STUB_DELETED
383 : $this->contentMode;
385 $slots = $rev->getSlots()->getSlots();
389 $out .= $this->writeSlot( $slots[SlotRecord::MAIN], $contentMode );
391 foreach ( $slots as $role => $slot ) {
392 if ( $role === SlotRecord::MAIN ) {
395 $out .= $this->writeSlot( $slot, $contentMode );
398 if ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
399 $out .=
" <sha1/>\n";
401 $sha1 = $this->invokeLenient(
402 static function () use ( $rev ) {
403 return $rev->getSha1();
405 'failed to determine sha1 for revision ' . $rev->getId()
407 $out .=
" " . Xml::element(
'sha1',
null, strval( $sha1 ) ) .
"\n";
413 if ( $contentMode === self::WRITE_CONTENT ) {
416 static function () use ( $rev ) {
417 return $rev->getContent( SlotRecord::MAIN, RevisionRecord::RAW );
419 'Failed to load main slot content of revision ' . $rev->getId()
424 $this->hookRunner->onXmlDumpWriterWriteRevision( $writer, $out, $row, $text, $rev );
426 $out .=
" </revision>\n";
437 private function writeSlot(
SlotRecord $slot, $contentMode ) {
438 $isMain = $slot->
getRole() === SlotRecord::MAIN;
441 if ( !$isV11 && !$isMain ) {
461 $contentHandler = MediaWikiServices::getInstance()
462 ->getContentHandlerFactory()
463 ->getContentHandler( $contentModel );
464 $contentFormat = $contentHandler->getDefaultFormat();
468 $out .= $indent .
Xml::element(
'model',
null, strval( $contentModel ) ) .
"\n";
469 $out .= $indent .
Xml::element(
'format',
null, strval( $contentFormat ) ) .
"\n";
472 'bytes' => $this->invokeLenient(
473 static function () use ( $slot ) {
476 'failed to determine size for slot ' . $slot->
getRole() .
' of revision '
482 $textAttributes[
'sha1'] = $this->invokeLenient(
483 static function () use ( $slot ) {
486 'failed to determine sha1 for slot ' . $slot->
getRole() .
' of revision '
491 if ( $contentMode === self::WRITE_CONTENT ) {
493 static function () use ( $slot ) {
496 'failed to load content for slot ' . $slot->
getRole() .
' of revision '
501 $out .= $indent .
Xml::element(
'text', $textAttributes ) .
"\n";
503 $out .= $this->writeText(
$content, $textAttributes, $indent );
505 } elseif ( $contentMode === self::WRITE_STUB_DELETED ) {
507 $textAttributes[
'deleted'] =
'deleted';
508 $out .= $indent .
Xml::element(
'text', $textAttributes ) .
"\n";
512 $textAttributes[
'location'] = $slot->
getAddress();
525 $textId = $this->getBlobStore()->getTextIdFromAddress( $slot->
getAddress() );
526 }
catch ( InvalidArgumentException $ex ) {
528 .
' of revision ' . $slot->
getRevision() .
': ' . $ex->getMessage() );
532 if ( is_int( $textId ) ) {
533 $textAttributes[
'id'] = $textId;
537 $out .= $indent .
Xml::element(
'text', $textAttributes ) .
"\n";
554 private function writeText(
Content $content, $textAttributes, $indent ) {
557 $contentHandler =
$content->getContentHandler();
558 $contentFormat = $contentHandler->getDefaultFormat();
565 $data =
$content->serialize( $contentFormat );
568 $data = $contentHandler->exportTransform( $data, $contentFormat );
569 $textAttributes[
'bytes'] = $size = strlen( $data );
570 $textAttributes[
'xml:space'] =
'preserve';
571 $out .= $indent .
Xml::elementClean(
'text', $textAttributes, strval( $data ) ) .
"\n";
584 $out =
" <logitem>\n";
585 $out .=
" " . Xml::element(
'id',
null, strval( $row->log_id ) ) .
"\n";
590 $out .=
" " . Xml::element(
'contributor', [
'deleted' =>
'deleted' ] ) .
"\n";
596 $out .=
" " . Xml::element(
'comment', [
'deleted' =>
'deleted' ] ) .
"\n";
598 $comment = CommentStore::getStore()->getComment(
'log_comment', $row )->text;
599 if ( $comment !=
'' ) {
600 $out .=
" " . Xml::elementClean(
'comment',
null, strval( $comment ) ) .
"\n";
604 $out .=
" " . Xml::element(
'type',
null, strval( $row->log_type ) ) .
"\n";
605 $out .=
" " . Xml::element(
'action',
null, strval( $row->log_action ) ) .
"\n";
608 $out .=
" " . Xml::element(
'text', [
'deleted' =>
'deleted' ] ) .
"\n";
610 $title = Title::makeTitle( $row->log_namespace, $row->log_title );
611 $out .=
" " . Xml::elementClean(
'logtitle',
null, self::canonicalTitle(
$title ) ) .
"\n";
612 $out .=
" " . Xml::elementClean(
'params',
613 [
'xml:space' =>
'preserve' ],
614 strval( $row->log_params ) ) .
"\n";
617 $out .=
" </logitem>\n";
629 return $indent . Xml::element(
'timestamp',
null, $ts ) .
"\n";
639 $out = $indent .
"<contributor>\n";
640 if ( $id || !IPUtils::isValid( $text ) ) {
641 $out .= $indent .
" " . Xml::elementClean(
'username',
null, strval( $text ) ) .
"\n";
642 $out .= $indent .
" " . Xml::element(
'id',
null, strval( $id ) ) .
"\n";
644 $out .= $indent .
" " . Xml::elementClean(
'ip',
null, strval( $text ) ) .
"\n";
646 $out .= $indent .
"</contributor>\n";
657 if ( $row->page_namespace ==
NS_FILE ) {
658 $img = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()
659 ->newFile( $row->page_title );
660 if ( $img && $img->exists() ) {
662 foreach ( array_reverse( $img->getHistory() ) as $ver ) {
663 $out .= $this->writeUpload( $ver, $dumpContents );
665 $out .= $this->writeUpload( $img, $dumpContents );
677 private function writeUpload(
$file, $dumpContents =
false ) {
678 if (
$file->isOld() ) {
680 '@phan-var OldLocalFile $file';
682 Xml::element(
'archivename',
null,
$file->getArchiveName() ) .
"\n";
686 if ( $dumpContents ) {
687 $be =
$file->getRepo()->getBackend();
688 # Dump file as base64
689 # Uses only XML-safe characters, so does not need escaping
690 # @todo Too bad this loads the contents into memory (script might swap)
691 $contents =
' <contents encoding="base64">' .
692 chunk_split( base64_encode(
693 $be->getFileContents( [
'src' =>
$file->getPath() ] ) ) ) .
698 $uploader =
$file->getUploader( File::FOR_PUBLIC );
700 $uploader = $this->
writeContributor( $uploader->getId(), $uploader->getName() );
702 $uploader =
Xml::element(
'contributor', [
'deleted' =>
'deleted' ] ) .
"\n";
704 $comment =
$file->getDescription( File::FOR_PUBLIC );
705 if ( ( $comment ??
'' ) !==
'' ) {
708 $comment =
Xml::element(
'comment', [
'deleted' =>
'deleted' ] );
710 return " <upload>\n" .
713 " " . $comment .
"\n" .
718 " " .
Xml::element(
'sha1base36', null,
$file->getSha1() ) .
"\n" .
719 " " .
Xml::element(
'rel', null,
$file->getRel() ) .
"\n" .
735 if (
$title->isExternal() ) {
736 return $title->getPrefixedText();
739 $prefix = MediaWikiServices::getInstance()->getContentLanguage()->
740 getFormattedNsText(
$title->getNamespace() );
745 if ( $prefix !==
'' ) {
749 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.
static warning( $msg, $callerOffset=1, $level=E_USER_NOTICE, $log='auto')
Adds a warning entry to the log.
A class containing constants representing the names of configuration variables.
Content object implementation for representing flat text.
Represents a title within MediaWiki.
isValidRedirectTarget()
Check if this Title is a valid redirect target.
closeStream()
Closes the output stream with the closing root element.
__construct( $contentMode=self::WRITE_CONTENT, $schemaVersion=XML_DUMP_SCHEMA_VERSION_11)
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.
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 content objects.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.