59 private const WRITE_STUB_DELETED = 2;
65 public static $supportedSchemas = [
75 private $schemaVersion;
82 private $currentTitle =
null;
93 private $commentStore;
104 $contentMode = self::WRITE_CONTENT,
110 in_array( $contentMode, [ self::WRITE_CONTENT, self::WRITE_STUB ],
true ),
112 'must be one of the following constants: WRITE_CONTENT or WRITE_STUB.'
116 in_array( $schemaVersion, self::$supportedSchemas,
true ),
118 'must be one of the following schema versions: '
119 . implode(
',', self::$supportedSchemas )
122 $this->contentMode = $contentMode;
123 $this->schemaVersion = $schemaVersion;
125 $hookContainer ?? MediaWikiServices::getInstance()->getHookContainer()
127 $this->commentStore = $commentStore ?? MediaWikiServices::getInstance()->getCommentStore();
141 $ver = $this->schemaVersion;
142 return Xml::element(
'mediawiki', [
143 'xmlns' =>
"http://www.mediawiki.org/xml/export-$ver/",
144 'xmlns:xsi' =>
"http://www.w3.org/2001/XMLSchema-instance",
155 'xsi:schemaLocation' =>
"http://www.mediawiki.org/xml/export-$ver/ " .
156 "http://www.mediawiki.org/xml/export-$ver.xsd",
158 'xml:lang' => MediaWikiServices::getInstance()->getContentLanguage()->getHtmlCode() ],
167 private function siteInfo() {
173 $this->caseSetting(),
174 $this->namespaces() ];
175 return " <siteinfo>\n " .
176 implode(
"\n ", $info ) .
183 private function sitename() {
184 $sitename = MediaWikiServices::getInstance()->getMainConfig()->get(
185 MainConfigNames::Sitename );
186 return Xml::element(
'sitename', [], $sitename );
192 private function dbname() {
193 $dbname = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::DBname );
194 return Xml::element(
'dbname', [], $dbname );
200 private function generator() {
201 return Xml::element(
'generator', [],
'MediaWiki ' .
MW_VERSION );
207 private function homelink() {
208 return Xml::element(
'base', [], Title::newMainPage()->getCanonicalURL() );
214 private function caseSetting() {
215 $capitalLinks = MediaWikiServices::getInstance()->getMainConfig()->get(
216 MainConfigNames::CapitalLinks );
218 $sensitivity = $capitalLinks ?
'first-letter' :
'case-sensitive';
219 return Xml::element(
'case', [], $sensitivity );
225 private function namespaces() {
226 $spaces =
"<namespaces>\n";
227 $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
229 MediaWikiServices::getInstance()->getContentLanguage()->getFormattedNamespaces()
233 Xml::element(
'namespace',
236 'case' => $nsInfo->isCapitalized( $ns )
237 ?
'first-letter' :
'case-sensitive',
240 $spaces .=
" </namespaces>";
251 return "</mediawiki>\n";
263 $this->currentTitle = Title::newFromRow( $row );
264 $canonicalTitle = self::canonicalTitle( $this->currentTitle );
265 $out .=
' ' . Xml::elementClean(
'title', [], $canonicalTitle ) .
"\n";
266 $out .=
' ' . Xml::element(
'ns', [], strval( $row->page_namespace ) ) .
"\n";
267 $out .=
' ' . Xml::element(
'id', [], strval( $row->page_id ) ) .
"\n";
268 if ( $row->page_is_redirect ) {
269 $services = MediaWikiServices::getInstance();
270 $page = $services->getWikiPageFactory()->newFromTitle( $this->currentTitle );
271 $redirectStore = $services->getRedirectStore();
272 $redirect = $this->invokeLenient(
273 static function () use ( $page, $redirectStore ) {
274 return $redirectStore->getRedirectTarget( $page );
276 'Failed to get redirect target of page ' . $page->getId()
278 $redirect = Title::castFromLinkTarget( $redirect );
281 $out .= Xml::element(
'redirect', [
'title' => self::canonicalTitle( $redirect ) ] );
285 $this->hookRunner->onXmlDumpWriterOpenPage( $this, $out, $row, $this->currentTitle );
297 if ( $this->currentTitle !==
null ) {
298 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
301 $linkCache->clearLink( $this->currentTitle );
309 private function getRevisionStore() {
310 return MediaWikiServices::getInstance()->getRevisionStore();
316 private function getBlobStore() {
318 return MediaWikiServices::getInstance()->getBlobStore();
330 private function invokeLenient( $callback, $warning ) {
335 }
catch (
MWException | RuntimeException | InvalidArgumentException | ErrorException $ex ) {
336 MWDebug::warning( $warning .
': ' . $ex->getMessage() );
352 $rev = $this->getRevisionStore()->newRevisionFromRowAndSlots(
359 $out =
" <revision>\n";
360 $out .=
" " . Xml::element(
'id',
null, strval( $rev->getId() ) ) .
"\n";
362 if ( $rev->getParentId() ) {
363 $out .=
" " . Xml::element(
'parentid',
null, strval( $rev->getParentId() ) ) .
"\n";
368 if ( $rev->isDeleted( RevisionRecord::DELETED_USER ) ) {
369 $out .=
" " . Xml::element(
'contributor', [
'deleted' =>
'deleted' ] ) .
"\n";
372 $user = $rev->getUser();
374 $user ? $user->getId() : 0,
375 $user ? $user->getName() :
''
379 if ( $rev->isMinor() ) {
380 $out .=
" <minor/>\n";
382 if ( $rev->isDeleted( RevisionRecord::DELETED_COMMENT ) ) {
383 $out .=
" " . Xml::element(
'comment', [
'deleted' =>
'deleted' ] ) .
"\n";
385 if ( $rev->getComment()->text !=
'' ) {
387 . Xml::elementClean(
'comment', [], strval( $rev->getComment()->text ) )
392 $contentMode = $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ? self::WRITE_STUB_DELETED
393 : $this->contentMode;
395 $slots = $rev->getSlots()->getSlots();
399 $out .= $this->writeSlot( $slots[SlotRecord::MAIN], $contentMode );
401 foreach ( $slots as $role => $slot ) {
402 if ( $role === SlotRecord::MAIN ) {
405 $out .= $this->writeSlot( $slot, $contentMode );
408 if ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
409 $out .=
" <sha1/>\n";
411 $sha1 = $this->invokeLenient(
412 static function () use ( $rev ) {
413 return $rev->getSha1();
415 'failed to determine sha1 for revision ' . $rev->getId()
417 $out .=
" " . Xml::element(
'sha1',
null, strval( $sha1 ) ) .
"\n";
421 if ( $contentMode === self::WRITE_CONTENT ) {
423 $content = $this->invokeLenient(
424 static function () use ( $rev ) {
425 return $rev->getContent( SlotRecord::MAIN, RevisionRecord::RAW );
427 'Failed to load main slot content of revision ' . $rev->getId()
430 $text = $content ? $content->
serialize() :
'';
432 $this->hookRunner->onXmlDumpWriterWriteRevision( $this, $out, $row, $text, $rev );
434 $out .=
" </revision>\n";
445 private function writeSlot(
SlotRecord $slot, $contentMode ) {
446 $isMain = $slot->
getRole() === SlotRecord::MAIN;
449 if ( !$isV11 && !$isMain ) {
459 $out .=
' ' . Xml::openElement(
'content' ) .
"\n";
461 $out .= $indent . Xml::element(
'role',
null, strval( $slot->
getRole() ) ) .
"\n";
465 $out .= $indent . Xml::element(
'origin',
null, strval( $slot->
getOrigin() ) ) .
"\n";
469 $contentHandler = MediaWikiServices::getInstance()
470 ->getContentHandlerFactory()
471 ->getContentHandler( $contentModel );
472 $contentFormat = $contentHandler->getDefaultFormat();
476 $out .= $indent . Xml::element(
'model',
null, strval( $contentModel ) ) .
"\n";
477 $out .= $indent . Xml::element(
'format',
null, strval( $contentFormat ) ) .
"\n";
480 'bytes' => $this->invokeLenient(
481 static function () use ( $slot ) {
484 'failed to determine size for slot ' . $slot->
getRole() .
' of revision '
490 $textAttributes[
'sha1'] = $this->invokeLenient(
491 static function () use ( $slot ) {
494 'failed to determine sha1 for slot ' . $slot->
getRole() .
' of revision '
499 if ( $contentMode === self::WRITE_CONTENT ) {
500 $content = $this->invokeLenient(
501 static function () use ( $slot ) {
504 'failed to load content for slot ' . $slot->
getRole() .
' of revision '
508 if ( $content ===
null ) {
509 $out .= $indent . Xml::element(
'text', $textAttributes ) .
"\n";
511 $out .= $this->writeText( $content, $textAttributes, $indent );
513 } elseif ( $contentMode === self::WRITE_STUB_DELETED ) {
515 $textAttributes[
'deleted'] =
'deleted';
516 $out .= $indent . Xml::element(
'text', $textAttributes ) .
"\n";
520 $textAttributes[
'location'] = $slot->
getAddress();
534 [ $schema, $textId ] = $this->getBlobStore()->splitBlobAddress( $slot->
getAddress() );
535 }
catch ( InvalidArgumentException $ex ) {
536 MWDebug::warning(
'Bad content address for slot ' . $slot->
getRole()
537 .
' of revision ' . $slot->
getRevision() .
': ' . $ex->getMessage() );
541 if ( $schema ===
'tt' ) {
542 $textAttributes[
'id'] = $textId;
543 } elseif ( $schema ===
'es' ) {
544 $textAttributes[
'id'] = bin2hex( $textId );
548 $out .= $indent . Xml::element(
'text', $textAttributes ) .
"\n";
552 $out .=
' ' . Xml::closeElement(
'content' ) .
"\n";
565 private function writeText(
Content $content, $textAttributes, $indent ) {
567 $contentFormat = $contentHandler->getDefaultFormat();
572 $data = $content->getText();
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";
597 if ( $row->log_deleted & LogPage::DELETED_USER ) {
598 $out .=
" " . Xml::element(
'contributor', [
'deleted' =>
'deleted' ] ) .
"\n";
603 if ( $row->log_deleted & LogPage::DELETED_COMMENT ) {
604 $out .=
" " . Xml::element(
'comment', [
'deleted' =>
'deleted' ] ) .
"\n";
606 $comment = $this->commentStore->getComment(
'log_comment', $row )->text;
607 if ( $comment !=
'' ) {
608 $out .=
" " . Xml::elementClean(
'comment',
null, strval( $comment ) ) .
"\n";
612 $out .=
" " . Xml::element(
'type',
null, strval( $row->log_type ) ) .
"\n";
613 $out .=
" " . Xml::element(
'action',
null, strval( $row->log_action ) ) .
"\n";
615 if ( $row->log_deleted & LogPage::DELETED_ACTION ) {
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";
620 $out .=
" " . Xml::elementClean(
'params',
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";
652 $out .= $indent .
" " . Xml::elementClean(
'ip',
null, strval( $text ) ) .
"\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';
690 Xml::element(
'archivename',
null, $file->getArchiveName() ) .
"\n";
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() ] ) ) ) .
706 $uploader = $file->getUploader( File::FOR_PUBLIC );
708 $uploader = $this->
writeContributor( $uploader->getId(), $uploader->getName() );
710 $uploader = Xml::element(
'contributor', [
'deleted' =>
'deleted' ] ) .
"\n";
712 $comment = $file->getDescription( File::FOR_PUBLIC );
713 if ( ( $comment ??
'' ) !==
'' ) {
714 $comment = Xml::elementClean(
'comment',
null, $comment );
716 $comment = Xml::element(
'comment', [
'deleted' =>
'deleted' ] );
718 return " <upload>\n" .
721 " " . $comment .
"\n" .
722 " " . Xml::element(
'filename',
null, $file->getName() ) .
"\n" .
724 " " . Xml::element(
'src',
null, $file->getCanonicalUrl() ) .
"\n" .
725 " " . Xml::element(
'size',
null, (
string)( $file->getSize() ?: 0 ) ) .
"\n" .
726 " " .
Xml::
element(
'sha1base36', null, $file->getSha1() ) .
"\n" .
727 " " .
Xml::
element(
'rel', null, $file->getRel() ) .
"\n" .
747 $prefix = MediaWikiServices::getInstance()->getContentLanguage()->
753 if ( $prefix !==
'' ) {
757 return $prefix . $title->
getText();
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.