34 use Wikimedia\Assert\Assert;
35 use Wikimedia\IPUtils;
96 in_array(
$contentMode, [ self::WRITE_CONTENT, self::WRITE_STUB ],
true ),
98 'must be one of the following constants: WRITE_CONTENT or WRITE_STUB.'
104 'must be one of the following schema versions: '
105 . implode(
',', self::$supportedSchemas )
110 $this->hookRunner =
new HookRunner( MediaWikiServices::getInstance()->getHookContainer() );
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() ],
158 return " <siteinfo>\n " .
159 implode(
"\n ", $info ) .
167 $sitename = MediaWikiServices::getInstance()->getMainConfig()->get(
168 MainConfigNames::Sitename );
176 $dbname = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::DBname );
198 $capitalLinks = MediaWikiServices::getInstance()->getMainConfig()->get(
199 MainConfigNames::CapitalLinks );
201 $sensitivity = $capitalLinks ?
'first-letter' :
'case-sensitive';
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";
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();
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 );
292 return MediaWikiServices::getInstance()->getRevisionStore();
300 return MediaWikiServices::getInstance()->getBlobStore();
319 }
catch ( Exception $ex ) {
320 if ( $ex instanceof
MWException || $ex instanceof RuntimeException ||
321 $ex instanceof InvalidArgumentException ) {
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 !=
'' ) {
385 $slots = $rev->getSlots()->getSlots();
391 foreach ( $slots as $role => $slot ) {
392 if ( $role === SlotRecord::MAIN ) {
398 if ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
399 $out .=
" <sha1/>\n";
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";
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";
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";
473 static function () use ( $slot ) {
476 'failed to determine size for slot ' . $slot->
getRole() .
' of revision '
483 static function () use ( $slot ) {
486 'failed to determine sha1 for slot ' . $slot->
getRole() .
' of revision '
493 static function () use ( $slot ) {
496 'failed to load content for slot ' . $slot->
getRole() .
' of revision '
501 $out .= $indent .
Xml::element(
'text', $textAttributes ) .
"\n";
505 } elseif (
$contentMode === self::WRITE_STUB_DELETED ) {
507 $textAttributes[
'deleted'] =
'deleted';
508 $out .= $indent .
Xml::element(
'text', $textAttributes ) .
"\n";
512 $textAttributes[
'location'] = $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";
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";
599 if ( $comment !=
'' ) {
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";
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";
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 ) {
678 if (
$file->isOld() ) {
680 '@phan-var OldLocalFile $file';
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() ] ) ) ) .
700 $uploader = $this->
writeContributor( $uploader->getId(), $uploader->getName() );
702 $uploader =
Xml::element(
'contributor', [
'deleted' =>
'deleted' ] ) .
"\n";
705 if ( ( $comment ??
'' ) !==
'' ) {
708 $comment =
Xml::element(
'comment', [
'deleted' =>
'deleted' ] );
710 return " <upload>\n" .
713 " " . $comment .
"\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.
static newMainPage(MessageLocalizer $localizer=null)
Create a new Title for the Main Page.
isValidRedirectTarget()
Check if this Title is a valid redirect target.
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
static newFromRow( $row)
Make a Title object from a DB row.
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
const WRITE_STUB_DELETED
Only output subs for revision content, indicating that the content has been deleted/suppressed.
static canonicalTitle(Title $title)
Return prefixed text form of title, but using the content language's canonical namespace.
int $contentMode
Whether to output revision content or just stubs.
const WRITE_STUB
Only output subs for revision content.
string $schemaVersion
which schema version the generated XML should comply to.
writeUpload( $file, $dumpContents=false)
invokeLenient( $callback, $warning)
Invokes the given callback, catching and logging any storage related exceptions.
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.
writeText(Content $content, $textAttributes, $indent)
Title null $currentTitle
Title of the currently processed page.
writeSlot(SlotRecord $slot, $contentMode)
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 content objects.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.