37 use Wikimedia\Assert\Assert;
38 use Wikimedia\IPUtils;
55 private const WRITE_STUB_DELETED = 2;
71 private $schemaVersion;
78 private $currentTitle =
null;
89 private $commentStore;
100 $contentMode = self::WRITE_CONTENT,
106 in_array( $contentMode, [ self::WRITE_CONTENT, self::WRITE_STUB ],
true ),
108 'must be one of the following constants: WRITE_CONTENT or WRITE_STUB.'
112 in_array( $schemaVersion, self::$supportedSchemas,
true ),
114 'must be one of the following schema versions: '
115 . implode(
',', self::$supportedSchemas )
118 $this->contentMode = $contentMode;
119 $this->schemaVersion = $schemaVersion;
121 $hookContainer ?? MediaWikiServices::getInstance()->getHookContainer()
123 $this->commentStore = $commentStore ?? MediaWikiServices::getInstance()->getCommentStore();
137 $ver = $this->schemaVersion;
139 'xmlns' =>
"http://www.mediawiki.org/xml/export-$ver/",
140 'xmlns:xsi' =>
"http://www.w3.org/2001/XMLSchema-instance",
151 'xsi:schemaLocation' =>
"http://www.mediawiki.org/xml/export-$ver/ " .
152 "http://www.mediawiki.org/xml/export-$ver.xsd",
154 'xml:lang' => MediaWikiServices::getInstance()->getContentLanguage()->getHtmlCode() ],
163 private function siteInfo() {
169 $this->caseSetting(),
170 $this->namespaces() ];
171 return " <siteinfo>\n " .
172 implode(
"\n ", $info ) .
179 private function sitename() {
180 $sitename = MediaWikiServices::getInstance()->getMainConfig()->get(
181 MainConfigNames::Sitename );
188 private function dbname() {
189 $dbname = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::DBname );
196 private function generator() {
203 private function homelink() {
204 return Xml::element(
'base', [], Title::newMainPage()->getCanonicalURL() );
210 private function caseSetting() {
211 $capitalLinks = MediaWikiServices::getInstance()->getMainConfig()->get(
212 MainConfigNames::CapitalLinks );
214 $sensitivity = $capitalLinks ?
'first-letter' :
'case-sensitive';
221 private function namespaces() {
222 $spaces =
"<namespaces>\n";
223 $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
225 MediaWikiServices::getInstance()->getContentLanguage()->getFormattedNamespaces()
232 'case' => $nsInfo->isCapitalized( $ns )
233 ?
'first-letter' :
'case-sensitive',
236 $spaces .=
" </namespaces>";
247 return "</mediawiki>\n";
259 $this->currentTitle = Title::newFromRow( $row );
262 $out .=
' ' .
Xml::element(
'ns', [], strval( $row->page_namespace ) ) .
"\n";
263 $out .=
' ' .
Xml::element(
'id', [], strval( $row->page_id ) ) .
"\n";
264 if ( $row->page_is_redirect ) {
265 $services = MediaWikiServices::getInstance();
266 $page = $services->getWikiPageFactory()->newFromTitle( $this->currentTitle );
267 $redirectStore = $services->getRedirectStore();
268 $redirect = $this->invokeLenient(
269 static function () use ( $page, $redirectStore ) {
270 return $redirectStore->getRedirectTarget( $page );
272 'Failed to get redirect target of page ' . $page->getId()
276 $out .=
Xml::element(
'redirect', [
'title' => self::canonicalTitle( $redirect ) ] );
280 $this->hookRunner->onXmlDumpWriterOpenPage( $this, $out, $row, $this->currentTitle );
292 if ( $this->currentTitle !==
null ) {
293 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
296 $linkCache->clearLink( $this->currentTitle );
304 private function getRevisionStore() {
305 return MediaWikiServices::getInstance()->getRevisionStore();
311 private function getBlobStore() {
313 return MediaWikiServices::getInstance()->getBlobStore();
327 private function invokeLenient( $callback, $warning ) {
332 }
catch ( Exception $ex ) {
333 if ( $ex instanceof
MWException || $ex instanceof RuntimeException ||
334 $ex instanceof InvalidArgumentException ) {
355 $rev = $this->getRevisionStore()->newRevisionFromRowAndSlots(
362 $out =
" <revision>\n";
363 $out .=
" " .
Xml::element(
'id',
null, strval( $rev->getId() ) ) .
"\n";
365 if ( $rev->getParentId() ) {
366 $out .=
" " .
Xml::element(
'parentid',
null, strval( $rev->getParentId() ) ) .
"\n";
371 if ( $rev->isDeleted( RevisionRecord::DELETED_USER ) ) {
372 $out .=
" " .
Xml::element(
'contributor', [
'deleted' =>
'deleted' ] ) .
"\n";
375 $user = $rev->getUser();
377 $user ? $user->getId() : 0,
378 $user ? $user->getName() :
''
382 if ( $rev->isMinor() ) {
383 $out .=
" <minor/>\n";
385 if ( $rev->isDeleted( RevisionRecord::DELETED_COMMENT ) ) {
386 $out .=
" " .
Xml::element(
'comment', [
'deleted' =>
'deleted' ] ) .
"\n";
388 if ( $rev->getComment()->text !=
'' ) {
395 $contentMode = $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ? self::WRITE_STUB_DELETED
396 : $this->contentMode;
398 $slots = $rev->getSlots()->getSlots();
402 $out .= $this->writeSlot( $slots[SlotRecord::MAIN], $contentMode );
404 foreach ( $slots as $role => $slot ) {
405 if ( $role === SlotRecord::MAIN ) {
408 $out .= $this->writeSlot( $slot, $contentMode );
411 if ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
412 $out .=
" <sha1/>\n";
414 $sha1 = $this->invokeLenient(
415 static function () use ( $rev ) {
416 return $rev->getSha1();
418 'failed to determine sha1 for revision ' . $rev->getId()
420 $out .=
" " .
Xml::element(
'sha1',
null, strval( $sha1 ) ) .
"\n";
424 if ( $contentMode === self::WRITE_CONTENT ) {
427 static function () use ( $rev ) {
428 return $rev->getContent( SlotRecord::MAIN, RevisionRecord::RAW );
430 'Failed to load main slot content of revision ' . $rev->getId()
435 $this->hookRunner->onXmlDumpWriterWriteRevision( $this, $out, $row, $text, $rev );
437 $out .=
" </revision>\n";
448 private function writeSlot(
SlotRecord $slot, $contentMode ) {
449 $isMain = $slot->
getRole() === SlotRecord::MAIN;
452 if ( !$isV11 && !$isMain ) {
472 $contentHandler = MediaWikiServices::getInstance()
473 ->getContentHandlerFactory()
474 ->getContentHandler( $contentModel );
475 $contentFormat = $contentHandler->getDefaultFormat();
479 $out .= $indent .
Xml::element(
'model',
null, strval( $contentModel ) ) .
"\n";
480 $out .= $indent .
Xml::element(
'format',
null, strval( $contentFormat ) ) .
"\n";
483 'bytes' => $this->invokeLenient(
484 static function () use ( $slot ) {
487 'failed to determine size for slot ' . $slot->
getRole() .
' of revision '
493 $textAttributes[
'sha1'] = $this->invokeLenient(
494 static function () use ( $slot ) {
497 'failed to determine sha1 for slot ' . $slot->
getRole() .
' of revision '
502 if ( $contentMode === self::WRITE_CONTENT ) {
504 static function () use ( $slot ) {
507 'failed to load content for slot ' . $slot->
getRole() .
' of revision '
512 $out .= $indent .
Xml::element(
'text', $textAttributes ) .
"\n";
514 $out .= $this->writeText(
$content, $textAttributes, $indent );
516 } elseif ( $contentMode === self::WRITE_STUB_DELETED ) {
518 $textAttributes[
'deleted'] =
'deleted';
519 $out .= $indent .
Xml::element(
'text', $textAttributes ) .
"\n";
523 $textAttributes[
'location'] = $slot->
getAddress();
536 $textId = $this->getBlobStore()->getTextIdFromAddress( $slot->
getAddress() );
537 }
catch ( InvalidArgumentException $ex ) {
539 .
' of revision ' . $slot->
getRevision() .
': ' . $ex->getMessage() );
543 if ( is_int( $textId ) ) {
544 $textAttributes[
'id'] = $textId;
548 $out .= $indent .
Xml::element(
'text', $textAttributes ) .
"\n";
565 private function writeText(
Content $content, $textAttributes, $indent ) {
566 $contentHandler =
$content->getContentHandler();
567 $contentFormat = $contentHandler->getDefaultFormat();
574 $data =
$content->serialize( $contentFormat );
577 $data = $contentHandler->exportTransform( $data, $contentFormat );
579 $textAttributes[
'bytes'] = strlen( $data );
580 $textAttributes[
'xml:space'] =
'preserve';
581 return $indent .
Xml::elementClean(
'text', $textAttributes, strval( $data ) ) .
"\n";
592 $out =
" <logitem>\n";
593 $out .=
" " .
Xml::element(
'id',
null, strval( $row->log_id ) ) .
"\n";
598 $out .=
" " .
Xml::element(
'contributor', [
'deleted' =>
'deleted' ] ) .
"\n";
604 $out .=
" " .
Xml::element(
'comment', [
'deleted' =>
'deleted' ] ) .
"\n";
606 $comment = $this->commentStore->getComment(
'log_comment', $row )->text;
607 if ( $comment !=
'' ) {
612 $out .=
" " .
Xml::element(
'type',
null, strval( $row->log_type ) ) .
"\n";
613 $out .=
" " .
Xml::element(
'action',
null, strval( $row->log_action ) ) .
"\n";
616 $out .=
" " .
Xml::element(
'text', [
'deleted' =>
'deleted' ] ) .
"\n";
618 $title = Title::makeTitle( $row->log_namespace, $row->log_title );
619 $out .=
" " .
Xml::elementClean(
'logtitle',
null, self::canonicalTitle( $title ) ) .
"\n";
621 [
'xml:space' =>
'preserve' ],
622 strval( $row->log_params ) ) .
"\n";
625 $out .=
" </logitem>\n";
637 return $indent .
Xml::element(
'timestamp',
null, $ts ) .
"\n";
647 $out = $indent .
"<contributor>\n";
648 if ( $id || !IPUtils::isValid( $text ) ) {
649 $out .= $indent .
" " .
Xml::elementClean(
'username',
null, strval( $text ) ) .
"\n";
650 $out .= $indent .
" " .
Xml::element(
'id',
null, strval( $id ) ) .
"\n";
654 $out .= $indent .
"</contributor>\n";
665 if ( $row->page_namespace ==
NS_FILE ) {
666 $img = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()
667 ->newFile( $row->page_title );
668 if ( $img && $img->exists() ) {
670 foreach ( array_reverse( $img->getHistory() ) as $ver ) {
671 $out .= $this->writeUpload( $ver, $dumpContents );
673 $out .= $this->writeUpload( $img, $dumpContents );
685 private function writeUpload(
$file, $dumpContents =
false ) {
686 if (
$file->isOld() ) {
688 '@phan-var OldLocalFile $file';
694 if ( $dumpContents ) {
695 $be =
$file->getRepo()->getBackend();
696 # Dump file as base64
697 # Uses only XML-safe characters, so does not need escaping
698 # @todo Too bad this loads the contents into memory (script might swap)
699 $contents =
' <contents encoding="base64">' .
700 chunk_split( base64_encode(
701 $be->getFileContents( [
'src' =>
$file->getPath() ] ) ) ) .
708 $uploader = $this->
writeContributor( $uploader->getId(), $uploader->getName() );
710 $uploader =
Xml::element(
'contributor', [
'deleted' =>
'deleted' ] ) .
"\n";
713 if ( ( $comment ??
'' ) !==
'' ) {
716 $comment =
Xml::element(
'comment', [
'deleted' =>
'deleted' ] );
718 return " <upload>\n" .
721 " " . $comment .
"\n" .
747 $prefix = MediaWikiServices::getInstance()->getContentLanguage()->
753 if ( $prefix !==
'' ) {
757 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.
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=" ")
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.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.