31use Wikimedia\Assert\Assert;
91 in_array(
$contentMode, [ self::WRITE_CONTENT, self::WRITE_STUB ] ),
93 'must be one of the following constants: WRITE_CONTENT or WRITE_STUB.'
99 'must be one of the following schema versions: '
100 . implode(
',', self::$supportedSchemas )
119 return Xml::element(
'mediawiki', [
120 'xmlns' =>
"http://www.mediawiki.org/xml/export-$ver/",
121 'xmlns:xsi' =>
"http://www.w3.org/2001/XMLSchema-instance",
132 'xsi:schemaLocation' =>
"http://www.mediawiki.org/xml/export-$ver/ " .
133 "http://www.mediawiki.org/xml/export-$ver.xsd",
135 'xml:lang' => MediaWikiServices::getInstance()->getContentLanguage()->getHtmlCode() ],
152 return " <siteinfo>\n " .
153 implode(
"\n ", $info ) .
162 return Xml::element(
'sitename', [],
$wgSitename );
170 return Xml::element(
'dbname', [],
$wgDBname );
178 return Xml::element(
'generator', [],
"MediaWiki $wgVersion" );
185 return Xml::element(
'base', [], Title::newMainPage()->getCanonicalURL() );
195 return Xml::element(
'case', [], $sensitivity );
202 $spaces =
"<namespaces>\n";
203 $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
205 MediaWikiServices::getInstance()->getContentLanguage()->getFormattedNamespaces()
209 Xml::element(
'namespace',
212 'case' => $nsInfo->isCapitalized( $ns )
213 ?
'first-letter' :
'case-sensitive',
216 $spaces .=
" </namespaces>";
227 return "</mediawiki>\n";
239 $this->currentTitle = Title::newFromRow( $row );
241 $out .=
' ' . Xml::elementClean(
'title', [], $canonicalTitle ) .
"\n";
242 $out .=
' ' . Xml::element(
'ns', [], strval( $row->page_namespace ) ) .
"\n";
243 $out .=
' ' . Xml::element(
'id', [], strval( $row->page_id ) ) .
"\n";
244 if ( $row->page_is_redirect ) {
245 $page = WikiPage::factory( $this->currentTitle );
246 $redirect = $page->getRedirectTarget();
249 $out .= Xml::element(
'redirect', [
'title' => self::canonicalTitle( $redirect ) ] );
254 if ( $row->page_restrictions !=
'' ) {
255 $out .=
' ' . Xml::element(
'restrictions', [],
256 strval( $row->page_restrictions ) ) .
"\n";
259 Hooks::run(
'XmlDumpWriterOpenPage', [ $this, &$out, $row, $this->currentTitle ] );
271 if ( $this->currentTitle !==
null ) {
272 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
275 $linkCache->clearLink( $this->currentTitle );
284 return MediaWikiServices::getInstance()->getRevisionStore();
291 return MediaWikiServices::getInstance()->getBlobStore();
309 return call_user_func_array( [ $obj, $method ],
$args );
312 }
catch ( Exception $ex ) {
313 if ( $ex instanceof
MWException || $ex instanceof RuntimeException ||
314 $ex instanceof InvalidArgumentException ) {
315 MWDebug::warning( $warning .
': ' . $ex->getMessage() );
343 $out =
" <revision>\n";
344 $out .=
" " . Xml::element(
'id',
null, strval( $rev->getId() ) ) .
"\n";
346 if ( $rev->getParentId() ) {
347 $out .=
" " . Xml::element(
'parentid',
null, strval( $rev->getParentId() ) ) .
"\n";
352 if ( $rev->isDeleted( RevisionRecord::DELETED_USER ) ) {
353 $out .=
" " . Xml::element(
'contributor', [
'deleted' =>
'deleted' ] ) .
"\n";
356 $user = $rev->getUser();
358 $user ? $user->getId() : 0,
359 $user ? $user->getName() :
''
363 if ( $rev->isMinor() ) {
364 $out .=
" <minor/>\n";
366 if ( $rev->isDeleted( RevisionRecord::DELETED_COMMENT ) ) {
367 $out .=
" " . Xml::element(
'comment', [
'deleted' =>
'deleted' ] ) .
"\n";
369 if ( $rev->getComment()->text !=
'' ) {
371 . Xml::elementClean(
'comment', [], strval( $rev->getComment()->text ) )
379 foreach ( $rev->getSlots()->getSlots() as $slot ) {
383 if ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
384 $out .=
" <sha1/>\n";
390 'failed to determine sha1 for revision ' . $rev->getId()
392 $out .=
" " . Xml::element(
'sha1',
null, strval( $sha1 ) ) .
"\n";
403 [ SlotRecord::MAIN, RevisionRecord::RAW ],
404 'Failed to load main slot content of revision ' . $rev->getId()
409 Hooks::run(
'XmlDumpWriterWriteRevision', [ &$writer, &$out, $row, $text, $rev ] );
411 $out .=
" </revision>\n";
423 $isMain = $slot->
getRole() === SlotRecord::MAIN;
426 if ( !$isV11 && !$isMain ) {
436 $out .=
' ' . Xml::openElement(
'content' ) .
"\n";
438 $out .= $indent . Xml::element(
'role',
null, strval( $slot->
getRole() ) ) .
"\n";
442 $out .= $indent . Xml::element(
'origin',
null, strval( $slot->
getOrigin() ) ) .
"\n";
446 $contentHandler = ContentHandler::getForModelID( $contentModel );
447 $contentFormat = $contentHandler->getDefaultFormat();
451 $out .= $indent . Xml::element(
'model',
null, strval( $contentModel ) ) .
"\n";
452 $out .= $indent . Xml::element(
'format',
null, strval( $contentFormat ) ) .
"\n";
455 'xml:space' =>
'preserve',
460 'failed to determine size for slot ' . $slot->
getRole() .
' of revision '
470 'failed to determine sha1 for slot ' . $slot->
getRole() .
' of revision '
480 'failed to load content for slot ' . $slot->
getRole() .
' of revision '
485 $out .= $indent . Xml::element(
'text', $textAttributes ) .
"\n";
489 } elseif (
$contentMode === self::WRITE_STUB_DELETED ) {
491 $textAttributes[
'deleted'] =
'deleted';
492 $out .= $indent . Xml::element(
'text', $textAttributes ) .
"\n";
496 $textAttributes[
'location'] = $slot->
getAddress();
509 }
catch ( InvalidArgumentException $ex ) {
510 MWDebug::warning(
'Bad content address for slot ' . $slot->
getRole()
511 .
' of revision ' . $slot->
getRevision() .
': ' . $ex->getMessage() );
516 $textAttributes[
'id'] = $textId;
519 $out .= $indent . Xml::element(
'text', $textAttributes ) .
"\n";
523 $out .=
' ' . Xml::closeElement(
'content' ) .
"\n";
539 $contentHandler =
$content->getContentHandler();
540 $contentFormat = $contentHandler->getDefaultFormat();
547 $data =
$content->serialize( $contentFormat );
550 $data = $contentHandler->exportTransform( $data, $contentFormat );
551 $textAttributes[
'bytes'] = $size = strlen( $data );
552 $out .= $indent . Xml::elementClean(
'text', $textAttributes, strval( $data ) ) .
"\n";
566 $out =
" <logitem>\n";
567 $out .=
" " . Xml::element(
'id',
null, strval( $row->log_id ) ) .
"\n";
572 $out .=
" " . Xml::element(
'contributor', [
'deleted' =>
'deleted' ] ) .
"\n";
578 $out .=
" " . Xml::element(
'comment', [
'deleted' =>
'deleted' ] ) .
"\n";
580 $comment = CommentStore::getStore()->getComment(
'log_comment', $row )->text;
581 if ( $comment !=
'' ) {
582 $out .=
" " . Xml::elementClean(
'comment',
null, strval( $comment ) ) .
"\n";
586 $out .=
" " . Xml::element(
'type',
null, strval( $row->log_type ) ) .
"\n";
587 $out .=
" " . Xml::element(
'action',
null, strval( $row->log_action ) ) .
"\n";
590 $out .=
" " . Xml::element(
'text', [
'deleted' =>
'deleted' ] ) .
"\n";
592 $title = Title::makeTitle( $row->log_namespace, $row->log_title );
593 $out .=
" " . Xml::elementClean(
'logtitle',
null, self::canonicalTitle(
$title ) ) .
"\n";
594 $out .=
" " . Xml::elementClean(
'params',
595 [
'xml:space' =>
'preserve' ],
596 strval( $row->log_params ) ) .
"\n";
599 $out .=
" </logitem>\n";
611 return $indent . Xml::element(
'timestamp',
null, $ts ) .
"\n";
621 $out = $indent .
"<contributor>\n";
622 if ( $id || !IP::isValid( $text ) ) {
623 $out .= $indent .
" " . Xml::elementClean(
'username',
null, strval( $text ) ) .
"\n";
624 $out .= $indent .
" " . Xml::element(
'id',
null, strval( $id ) ) .
"\n";
626 $out .= $indent .
" " . Xml::elementClean(
'ip',
null, strval( $text ) ) .
"\n";
628 $out .= $indent .
"</contributor>\n";
639 if ( $row->page_namespace ==
NS_FILE ) {
640 $img = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()
641 ->newFile( $row->page_title );
642 if ( $img && $img->exists() ) {
644 foreach ( array_reverse( $img->getHistory() ) as $ver ) {
660 if (
$file->isOld() ) {
662 '@phan-var OldLocalFile $file';
664 Xml::element(
'archivename',
null,
$file->getArchiveName() ) .
"\n";
668 if ( $dumpContents ) {
669 $be =
$file->getRepo()->getBackend();
670 # Dump file as base64
671 # Uses only XML-safe characters, so does not need escaping
672 # @todo Too bad this loads the contents into memory (script might swap)
673 $contents =
' <contents encoding="base64">' .
674 chunk_split( base64_encode(
675 $be->getFileContents( [
'src' =>
$file->getPath() ] ) ) ) .
680 if (
$file->isDeleted( File::DELETED_COMMENT ) ) {
681 $comment = Xml::element(
'comment', [
'deleted' =>
'deleted' ] );
683 $comment = Xml::elementClean(
'comment',
null, strval(
$file->getDescription() ) );
685 return " <upload>\n" .
688 " " . $comment .
"\n" .
689 " " . Xml::element(
'filename',
null,
$file->getName() ) .
"\n" .
691 " " . Xml::element(
'src',
null,
$file->getCanonicalUrl() ) .
"\n" .
692 " " . Xml::element(
'size',
null,
$file->getSize() ) .
"\n" .
693 " " . Xml::element(
'sha1base36',
null,
$file->getSha1() ) .
"\n" .
694 " " . Xml::element(
'rel',
null,
$file->getRel() ) .
"\n" .
710 if (
$title->isExternal() ) {
711 return $title->getPrefixedText();
714 $prefix = MediaWikiServices::getInstance()->getContentLanguage()->
715 getFormattedNsText(
$title->getNamespace() );
720 if ( $prefix !==
'' ) {
724 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.
$wgVersion
MediaWiki version number.
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)
XmlDumpWriter constructor.
static string[] $supportedSchemas
the schema versions supported for output @final
invokeLenient( $obj, $method, $args=[], $warning)
Invokes the given method on the given object, catching and logging any storage related exceptions.
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)
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.