33use Wikimedia\Assert\Assert;
91 $contentMode = self::WRITE_CONTENT,
95 in_array(
$contentMode, [ self::WRITE_CONTENT, self::WRITE_STUB ],
true ),
97 'must be one of the following constants: WRITE_CONTENT or WRITE_STUB.'
103 'must be one of the following schema versions: '
104 . implode(
',', self::$supportedSchemas )
109 $this->hookRunner =
new HookRunner( MediaWikiServices::getInstance()->getHookContainer() );
123 $ver = $this->schemaVersion;
124 return Xml::element(
'mediawiki', [
125 'xmlns' =>
"http://www.mediawiki.org/xml/export-$ver/",
126 'xmlns:xsi' =>
"http://www.w3.org/2001/XMLSchema-instance",
137 'xsi:schemaLocation' =>
"http://www.mediawiki.org/xml/export-$ver/ " .
138 "http://www.mediawiki.org/xml/export-$ver.xsd",
140 'xml:lang' => MediaWikiServices::getInstance()->getContentLanguage()->getHtmlCode() ],
157 return " <siteinfo>\n " .
158 implode(
"\n ", $info ) .
167 return Xml::element(
'sitename', [],
$wgSitename );
175 return Xml::element(
'dbname', [],
$wgDBname );
182 return Xml::element(
'generator', [],
'MediaWiki ' .
MW_VERSION );
189 return Xml::element(
'base', [], Title::newMainPage()->getCanonicalURL() );
199 return Xml::element(
'case', [], $sensitivity );
206 $spaces =
"<namespaces>\n";
207 $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
209 MediaWikiServices::getInstance()->getContentLanguage()->getFormattedNamespaces()
213 Xml::element(
'namespace',
216 'case' => $nsInfo->isCapitalized( $ns )
217 ?
'first-letter' :
'case-sensitive',
220 $spaces .=
" </namespaces>";
231 return "</mediawiki>\n";
243 $this->currentTitle = Title::newFromRow( $row );
244 $canonicalTitle = self::canonicalTitle( $this->currentTitle );
245 $out .=
' ' . Xml::elementClean(
'title', [], $canonicalTitle ) .
"\n";
246 $out .=
' ' . Xml::element(
'ns', [], strval( $row->page_namespace ) ) .
"\n";
247 $out .=
' ' . Xml::element(
'id', [], strval( $row->page_id ) ) .
"\n";
248 if ( $row->page_is_redirect ) {
249 $page = WikiPage::factory( $this->currentTitle );
251 function () use ( $page ) {
252 return $page->getRedirectTarget();
254 'Failed to get redirect target of page ' . $page->getId()
258 $out .= Xml::element(
'redirect', [
'title' => self::canonicalTitle( $redirect ) ] );
263 if ( $row->page_restrictions !=
'' ) {
264 $out .=
' ' . Xml::element(
'restrictions', [],
265 strval( $row->page_restrictions ) ) .
"\n";
268 $this->hookRunner->onXmlDumpWriterOpenPage( $this, $out, $row, $this->currentTitle );
280 if ( $this->currentTitle !==
null ) {
281 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
284 $linkCache->clearLink( $this->currentTitle );
293 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 ) {
322 MWDebug::warning( $warning .
': ' . $ex->getMessage() );
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();
391 foreach ( $slots as $role => $slot ) {
392 if ( $role === SlotRecord::MAIN ) {
398 if ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
399 $out .=
" <sha1/>\n";
402 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 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 ) {
451 $out .=
' ' . Xml::openElement(
'content' ) .
"\n";
453 $out .= $indent . Xml::element(
'role',
null, strval( $slot->
getRole() ) ) .
"\n";
457 $out .= $indent . Xml::element(
'origin',
null, strval( $slot->
getOrigin() ) ) .
"\n";
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 function () use ( $slot ) {
476 'failed to determine size for slot ' . $slot->
getRole() .
' of revision '
483 function () use ( $slot ) {
486 'failed to determine sha1 for slot ' . $slot->
getRole() .
' of revision '
493 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 ) {
527 MWDebug::warning(
'Bad content address for slot ' . $slot->
getRole()
528 .
' of revision ' . $slot->
getRevision() .
': ' . $ex->getMessage() );
532 if ( is_int( $textId ) ) {
533 $textAttributes[
'id'] = $textId;
537 $out .= $indent . Xml::element(
'text', $textAttributes ) .
"\n";
541 $out .=
' ' . Xml::closeElement(
'content' ) .
"\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";
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 ) {
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 if (
$file->isDeleted( File::DELETED_COMMENT ) ) {
699 $comment = Xml::element(
'comment', [
'deleted' =>
'deleted' ] );
701 $comment = Xml::elementClean(
'comment',
null, strval(
$file->getDescription() ) );
703 return " <upload>\n" .
706 " " . $comment .
"\n" .
707 " " . Xml::element(
'filename',
null,
$file->getName() ) .
"\n" .
709 " " . Xml::element(
'src',
null,
$file->getCanonicalUrl() ) .
"\n" .
710 " " . Xml::element(
'size',
null,
$file->getSize() ) .
"\n" .
711 " " . Xml::element(
'sha1base36',
null,
$file->getSha1() ) .
"\n" .
712 " " . Xml::element(
'rel',
null,
$file->getRel() ) .
"\n" .
728 if (
$title->isExternal() ) {
729 return $title->getPrefixedText();
732 $prefix = MediaWikiServices::getInstance()->getContentLanguage()->
733 getFormattedNsText(
$title->getNamespace() );
738 if ( $prefix !==
'' ) {
742 return $prefix .
$title->getText();
$wgCapitalLinks
Set this to false to avoid forcing the first letter of links to capitals.
$wgDBname
Current wiki database name.
$wgSitename
Name of the site.
const MW_VERSION
The running version of MediaWiki.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
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
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=" ")
const XML_DUMP_SCHEMA_VERSION_11
const XML_DUMP_SCHEMA_VERSION_10
Base interface for content objects.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.