2015-12-28 22:23:22 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* XmlDumpWriter
|
|
|
|
|
*
|
|
|
|
|
* Copyright © 2003, 2005, 2006 Brion Vibber <brion@pobox.com>
|
|
|
|
|
* https://www.mediawiki.org/
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
2018-07-29 12:24:54 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2018-07-03 12:39:22 +00:00
|
|
|
use MediaWiki\Revision\RevisionStore;
|
|
|
|
|
use MediaWiki\Storage\SqlBlobStore;
|
2018-07-29 12:24:54 +00:00
|
|
|
|
2015-12-28 22:23:22 +00:00
|
|
|
/**
|
|
|
|
|
* @ingroup Dump
|
|
|
|
|
*/
|
|
|
|
|
class XmlDumpWriter {
|
2018-12-14 11:24:44 +00:00
|
|
|
/**
|
|
|
|
|
* @var string[] the schema versions supported for output
|
|
|
|
|
* @final
|
|
|
|
|
*/
|
|
|
|
|
public static $supportedSchemas = [
|
|
|
|
|
XML_DUMP_SCHEMA_VERSION_10,
|
|
|
|
|
];
|
2018-07-03 12:39:22 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Title of the currently processed page
|
|
|
|
|
*
|
|
|
|
|
* @var Title|null
|
|
|
|
|
*/
|
|
|
|
|
private $currentTitle = null;
|
|
|
|
|
|
2015-12-28 22:23:22 +00:00
|
|
|
/**
|
|
|
|
|
* Opens the XML output stream's root "<mediawiki>" element.
|
|
|
|
|
* This does not include an xml directive, so is safe to include
|
|
|
|
|
* as a subelement in a larger XML stream. Namespace and XML Schema
|
|
|
|
|
* references are included.
|
|
|
|
|
*
|
|
|
|
|
* Output will be encoded in UTF-8.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function openStream() {
|
|
|
|
|
$ver = WikiExporter::schemaVersion();
|
2016-02-17 09:09:32 +00:00
|
|
|
return Xml::element( 'mediawiki', [
|
2015-12-28 22:23:22 +00:00
|
|
|
'xmlns' => "http://www.mediawiki.org/xml/export-$ver/",
|
|
|
|
|
'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
|
|
|
|
|
/*
|
|
|
|
|
* When a new version of the schema is created, it needs staging on mediawiki.org.
|
|
|
|
|
* This requires a change in the operations/mediawiki-config git repo.
|
|
|
|
|
*
|
|
|
|
|
* Create a changeset like https://gerrit.wikimedia.org/r/#/c/149643/ in which
|
|
|
|
|
* you copy in the new xsd file.
|
|
|
|
|
*
|
|
|
|
|
* After it is reviewed, merged and deployed (sync-docroot), the index.html needs purging.
|
2016-10-13 05:34:26 +00:00
|
|
|
* echo "https://www.mediawiki.org/xml/index.html" | mwscript purgeList.php --wiki=aawiki
|
2015-12-28 22:23:22 +00:00
|
|
|
*/
|
|
|
|
|
'xsi:schemaLocation' => "http://www.mediawiki.org/xml/export-$ver/ " .
|
|
|
|
|
"http://www.mediawiki.org/xml/export-$ver.xsd",
|
2018-07-29 12:24:54 +00:00
|
|
|
'version' => $ver,
|
|
|
|
|
'xml:lang' => MediaWikiServices::getInstance()->getContentLanguage()->getHtmlCode() ],
|
2015-12-28 22:23:22 +00:00
|
|
|
null ) .
|
|
|
|
|
"\n" .
|
|
|
|
|
$this->siteInfo();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function siteInfo() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$info = [
|
2015-12-28 22:23:22 +00:00
|
|
|
$this->sitename(),
|
|
|
|
|
$this->dbname(),
|
|
|
|
|
$this->homelink(),
|
|
|
|
|
$this->generator(),
|
|
|
|
|
$this->caseSetting(),
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->namespaces() ];
|
2015-12-28 22:23:22 +00:00
|
|
|
return " <siteinfo>\n " .
|
|
|
|
|
implode( "\n ", $info ) .
|
|
|
|
|
"\n </siteinfo>\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function sitename() {
|
|
|
|
|
global $wgSitename;
|
2016-02-17 09:09:32 +00:00
|
|
|
return Xml::element( 'sitename', [], $wgSitename );
|
2015-12-28 22:23:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function dbname() {
|
|
|
|
|
global $wgDBname;
|
2016-02-17 09:09:32 +00:00
|
|
|
return Xml::element( 'dbname', [], $wgDBname );
|
2015-12-28 22:23:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function generator() {
|
|
|
|
|
global $wgVersion;
|
2016-02-17 09:09:32 +00:00
|
|
|
return Xml::element( 'generator', [], "MediaWiki $wgVersion" );
|
2015-12-28 22:23:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function homelink() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return Xml::element( 'base', [], Title::newMainPage()->getCanonicalURL() );
|
2015-12-28 22:23:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function caseSetting() {
|
|
|
|
|
global $wgCapitalLinks;
|
|
|
|
|
// "case-insensitive" option is reserved for future
|
|
|
|
|
$sensitivity = $wgCapitalLinks ? 'first-letter' : 'case-sensitive';
|
2016-02-17 09:09:32 +00:00
|
|
|
return Xml::element( 'case', [], $sensitivity );
|
2015-12-28 22:23:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function namespaces() {
|
|
|
|
|
$spaces = "<namespaces>\n";
|
2018-08-05 17:58:51 +00:00
|
|
|
$nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
|
2018-07-29 12:24:54 +00:00
|
|
|
foreach (
|
|
|
|
|
MediaWikiServices::getInstance()->getContentLanguage()->getFormattedNamespaces()
|
|
|
|
|
as $ns => $title
|
|
|
|
|
) {
|
2015-12-28 22:23:22 +00:00
|
|
|
$spaces .= ' ' .
|
|
|
|
|
Xml::element( 'namespace',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2015-12-28 22:23:22 +00:00
|
|
|
'key' => $ns,
|
2018-08-05 17:58:51 +00:00
|
|
|
'case' => $nsInfo->isCapitalized( $ns )
|
|
|
|
|
? 'first-letter' : 'case-sensitive',
|
2016-02-17 09:09:32 +00:00
|
|
|
], $title ) . "\n";
|
2015-12-28 22:23:22 +00:00
|
|
|
}
|
|
|
|
|
$spaces .= " </namespaces>";
|
|
|
|
|
return $spaces;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Closes the output stream with the closing root element.
|
|
|
|
|
* Call when finished dumping things.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function closeStream() {
|
|
|
|
|
return "</mediawiki>\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Opens a "<page>" section on the output stream, with data
|
|
|
|
|
* from the given database row.
|
|
|
|
|
*
|
|
|
|
|
* @param object $row
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function openPage( $row ) {
|
|
|
|
|
$out = " <page>\n";
|
2018-07-03 12:39:22 +00:00
|
|
|
$this->currentTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
|
|
|
|
|
$canonicalTitle = self::canonicalTitle( $this->currentTitle );
|
|
|
|
|
$out .= ' ' . Xml::elementClean( 'title', [], $canonicalTitle ) . "\n";
|
2016-02-17 09:09:32 +00:00
|
|
|
$out .= ' ' . Xml::element( 'ns', [], strval( $row->page_namespace ) ) . "\n";
|
|
|
|
|
$out .= ' ' . Xml::element( 'id', [], strval( $row->page_id ) ) . "\n";
|
2015-12-28 22:23:22 +00:00
|
|
|
if ( $row->page_is_redirect ) {
|
2018-07-03 12:39:22 +00:00
|
|
|
$page = WikiPage::factory( $this->currentTitle );
|
2015-12-28 22:23:22 +00:00
|
|
|
$redirect = $page->getRedirectTarget();
|
|
|
|
|
if ( $redirect instanceof Title && $redirect->isValidRedirectTarget() ) {
|
|
|
|
|
$out .= ' ';
|
2016-02-17 09:09:32 +00:00
|
|
|
$out .= Xml::element( 'redirect', [ 'title' => self::canonicalTitle( $redirect ) ] );
|
2015-12-28 22:23:22 +00:00
|
|
|
$out .= "\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $row->page_restrictions != '' ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$out .= ' ' . Xml::element( 'restrictions', [],
|
2015-12-28 22:23:22 +00:00
|
|
|
strval( $row->page_restrictions ) ) . "\n";
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-03 12:39:22 +00:00
|
|
|
Hooks::run( 'XmlDumpWriterOpenPage', [ $this, &$out, $row, $this->currentTitle ] );
|
2015-12-28 22:23:22 +00:00
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Closes a "<page>" section on the output stream.
|
|
|
|
|
*
|
2019-02-07 09:21:19 +00:00
|
|
|
* @private
|
2015-12-28 22:23:22 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function closePage() {
|
2019-04-08 06:45:55 +00:00
|
|
|
if ( $this->currentTitle !== null ) {
|
|
|
|
|
$linkCache = MediaWikiServices::getInstance()->getLinkCache();
|
|
|
|
|
// In rare cases, link cache has the same key for some pages which
|
|
|
|
|
// might be read as part of the same batch. T220424 and T220316
|
|
|
|
|
$linkCache->clearLink( $this->currentTitle );
|
|
|
|
|
}
|
2015-12-28 22:23:22 +00:00
|
|
|
return " </page>\n";
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-03 12:39:22 +00:00
|
|
|
/**
|
|
|
|
|
* @return RevisionStore
|
|
|
|
|
*/
|
|
|
|
|
private function getRevisionStore() {
|
|
|
|
|
return MediaWikiServices::getInstance()->getRevisionStore();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return SqlBlobStore
|
|
|
|
|
*/
|
|
|
|
|
private function getBlobStore() {
|
|
|
|
|
return MediaWikiServices::getInstance()->getBlobStore();
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-28 22:23:22 +00:00
|
|
|
/**
|
|
|
|
|
* Dumps a "<revision>" section on the output stream, with
|
|
|
|
|
* data filled in from the given database row.
|
|
|
|
|
*
|
|
|
|
|
* @param object $row
|
|
|
|
|
* @return string
|
2019-02-07 09:21:19 +00:00
|
|
|
* @private
|
2015-12-28 22:23:22 +00:00
|
|
|
*/
|
|
|
|
|
function writeRevision( $row ) {
|
|
|
|
|
$out = " <revision>\n";
|
|
|
|
|
$out .= " " . Xml::element( 'id', null, strval( $row->rev_id ) ) . "\n";
|
|
|
|
|
if ( isset( $row->rev_parent_id ) && $row->rev_parent_id ) {
|
|
|
|
|
$out .= " " . Xml::element( 'parentid', null, strval( $row->rev_parent_id ) ) . "\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$out .= $this->writeTimestamp( $row->rev_timestamp );
|
|
|
|
|
|
|
|
|
|
if ( isset( $row->rev_deleted ) && ( $row->rev_deleted & Revision::DELETED_USER ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$out .= " " . Xml::element( 'contributor', [ 'deleted' => 'deleted' ] ) . "\n";
|
2015-12-28 22:23:22 +00:00
|
|
|
} else {
|
2019-05-27 13:03:29 +00:00
|
|
|
// empty values get written out as uid 0, see T224221
|
|
|
|
|
$out .= $this->writeContributor( $row->rev_user ?: 0, $row->rev_user_text );
|
2015-12-28 22:23:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isset( $row->rev_minor_edit ) && $row->rev_minor_edit ) {
|
|
|
|
|
$out .= " <minor/>\n";
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $row->rev_deleted ) && ( $row->rev_deleted & Revision::DELETED_COMMENT ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$out .= " " . Xml::element( 'comment', [ 'deleted' => 'deleted' ] ) . "\n";
|
2017-06-06 17:39:14 +00:00
|
|
|
} else {
|
2018-01-24 23:41:01 +00:00
|
|
|
$comment = CommentStore::getStore()->getComment( 'rev_comment', $row )->text;
|
2017-06-06 17:39:14 +00:00
|
|
|
if ( $comment != '' ) {
|
|
|
|
|
$out .= " " . Xml::elementClean( 'comment', [], strval( $comment ) ) . "\n";
|
|
|
|
|
}
|
2015-12-28 22:23:22 +00:00
|
|
|
}
|
|
|
|
|
|
2018-07-03 12:39:22 +00:00
|
|
|
// TODO: rev_content_model no longer exists with MCR, see T174031
|
2015-12-28 22:23:22 +00:00
|
|
|
if ( isset( $row->rev_content_model ) && !is_null( $row->rev_content_model ) ) {
|
|
|
|
|
$content_model = strval( $row->rev_content_model );
|
|
|
|
|
} else {
|
|
|
|
|
// probably using $wgContentHandlerUseDB = false;
|
2018-07-03 12:39:22 +00:00
|
|
|
$content_model = ContentHandler::getDefaultModelFor( $this->currentTitle );
|
2015-12-28 22:23:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$content_handler = ContentHandler::getForModelID( $content_model );
|
|
|
|
|
|
2018-07-03 12:39:22 +00:00
|
|
|
// TODO: rev_content_format no longer exists with MCR, see T174031
|
2015-12-28 22:23:22 +00:00
|
|
|
if ( isset( $row->rev_content_format ) && !is_null( $row->rev_content_format ) ) {
|
|
|
|
|
$content_format = strval( $row->rev_content_format );
|
|
|
|
|
} else {
|
|
|
|
|
// probably using $wgContentHandlerUseDB = false;
|
|
|
|
|
$content_format = $content_handler->getDefaultFormat();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$out .= " " . Xml::element( 'model', null, strval( $content_model ) ) . "\n";
|
|
|
|
|
$out .= " " . Xml::element( 'format', null, strval( $content_format ) ) . "\n";
|
|
|
|
|
|
|
|
|
|
$text = '';
|
|
|
|
|
if ( isset( $row->rev_deleted ) && ( $row->rev_deleted & Revision::DELETED_TEXT ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$out .= " " . Xml::element( 'text', [ 'deleted' => 'deleted' ] ) . "\n";
|
2015-12-28 22:23:22 +00:00
|
|
|
} elseif ( isset( $row->old_text ) ) {
|
|
|
|
|
// Raw text from the database may have invalid chars
|
|
|
|
|
$text = strval( Revision::getRevisionText( $row ) );
|
2019-03-13 23:16:24 +00:00
|
|
|
try {
|
|
|
|
|
$text = $content_handler->exportTransform( $text, $content_format );
|
|
|
|
|
}
|
2019-03-27 07:39:00 +00:00
|
|
|
catch ( Exception $ex ) {
|
|
|
|
|
if ( $ex instanceof MWException || $ex instanceof RuntimeException ) {
|
|
|
|
|
// leave text as is; that's the way it goes
|
|
|
|
|
wfLogWarning( 'exportTransform failed on text for revid ' . $row->rev_id . "\n" );
|
|
|
|
|
} else {
|
|
|
|
|
throw $ex;
|
|
|
|
|
}
|
2019-03-13 23:16:24 +00:00
|
|
|
}
|
2015-12-28 22:23:22 +00:00
|
|
|
$out .= " " . Xml::elementClean( 'text',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'xml:space' => 'preserve', 'bytes' => intval( $row->rev_len ) ],
|
2015-12-28 22:23:22 +00:00
|
|
|
strval( $text ) ) . "\n";
|
2018-07-03 12:39:22 +00:00
|
|
|
} elseif ( isset( $row->_load_content ) ) {
|
|
|
|
|
// TODO: make this fully MCR aware, see T174031
|
|
|
|
|
$rev = $this->getRevisionStore()->newRevisionFromRow( $row, 0, $this->currentTitle );
|
|
|
|
|
$slot = $rev->getSlot( 'main' );
|
2019-03-27 07:39:00 +00:00
|
|
|
try {
|
|
|
|
|
$content = $slot->getContent();
|
2018-07-03 12:39:22 +00:00
|
|
|
|
2019-03-27 07:39:00 +00:00
|
|
|
if ( $content instanceof TextContent ) {
|
|
|
|
|
// HACK: For text based models, bypass the serialization step.
|
|
|
|
|
// This allows extensions (like Flow)that use incompatible combinations
|
|
|
|
|
// of serialization format and content model.
|
|
|
|
|
$text = $content->getNativeData();
|
|
|
|
|
} else {
|
|
|
|
|
$text = $content->serialize( $content_format );
|
|
|
|
|
}
|
|
|
|
|
$text = $content_handler->exportTransform( $text, $content_format );
|
|
|
|
|
$out .= " " . Xml::elementClean( 'text',
|
|
|
|
|
[ 'xml:space' => 'preserve', 'bytes' => intval( $slot->getSize() ) ],
|
|
|
|
|
strval( $text ) ) . "\n";
|
|
|
|
|
}
|
|
|
|
|
catch ( Exception $ex ) {
|
|
|
|
|
if ( $ex instanceof MWException || $ex instanceof RuntimeException ) {
|
|
|
|
|
// there's no provsion in the schema for an attribute that will let
|
|
|
|
|
// the user know this element was unavailable due to error; an empty
|
|
|
|
|
// tag is the best we can do
|
|
|
|
|
$out .= " " . Xml::element( 'text' ) . "\n";
|
|
|
|
|
wfLogWarning( 'failed to load content for revid ' . $row->rev_id . "\n" );
|
|
|
|
|
} else {
|
|
|
|
|
throw $ex;
|
|
|
|
|
}
|
2018-07-03 12:39:22 +00:00
|
|
|
}
|
|
|
|
|
} elseif ( isset( $row->rev_text_id ) ) {
|
|
|
|
|
// Stub output for pre-MCR schema
|
|
|
|
|
// TODO: MCR: rev_text_id only exists in the pre-MCR schema. Remove this when
|
|
|
|
|
// we drop support for the old schema.
|
2015-12-28 22:23:22 +00:00
|
|
|
$out .= " " . Xml::element( 'text',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'id' => $row->rev_text_id, 'bytes' => intval( $row->rev_len ) ],
|
2015-12-28 22:23:22 +00:00
|
|
|
"" ) . "\n";
|
2018-07-03 12:39:22 +00:00
|
|
|
} else {
|
|
|
|
|
// Backwards-compatible stub output for MCR aware schema
|
|
|
|
|
// TODO: MCR: emit content addresses instead of text ids, see T174031, T199121
|
|
|
|
|
$rev = $this->getRevisionStore()->newRevisionFromRow( $row, 0, $this->currentTitle );
|
|
|
|
|
$slot = $rev->getSlot( 'main' );
|
|
|
|
|
|
|
|
|
|
// Note that this is currently the ONLY reason we have a BlobStore here at all.
|
|
|
|
|
// When removing this line, check whether the BlobStore has become unused.
|
|
|
|
|
$textId = $this->getBlobStore()->getTextIdFromAddress( $slot->getAddress() );
|
|
|
|
|
$out .= " " . Xml::element( 'text',
|
|
|
|
|
[ 'id' => $textId, 'bytes' => intval( $slot->getSize() ) ],
|
|
|
|
|
"" ) . "\n";
|
2015-12-28 22:23:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isset( $row->rev_sha1 )
|
|
|
|
|
&& $row->rev_sha1
|
|
|
|
|
&& !( $row->rev_deleted & Revision::DELETED_TEXT )
|
|
|
|
|
) {
|
|
|
|
|
$out .= " " . Xml::element( 'sha1', null, strval( $row->rev_sha1 ) ) . "\n";
|
|
|
|
|
} else {
|
|
|
|
|
$out .= " <sha1/>\n";
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-01 04:01:54 +00:00
|
|
|
// Avoid PHP 7.1 warning from passing $this by reference
|
|
|
|
|
$writer = $this;
|
|
|
|
|
Hooks::run( 'XmlDumpWriterWriteRevision', [ &$writer, &$out, $row, $text ] );
|
2015-12-28 22:23:22 +00:00
|
|
|
|
|
|
|
|
$out .= " </revision>\n";
|
|
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Dumps a "<logitem>" section on the output stream, with
|
|
|
|
|
* data filled in from the given database row.
|
|
|
|
|
*
|
|
|
|
|
* @param object $row
|
|
|
|
|
* @return string
|
2019-02-07 09:21:19 +00:00
|
|
|
* @private
|
2015-12-28 22:23:22 +00:00
|
|
|
*/
|
|
|
|
|
function writeLogItem( $row ) {
|
|
|
|
|
$out = " <logitem>\n";
|
|
|
|
|
$out .= " " . Xml::element( 'id', null, strval( $row->log_id ) ) . "\n";
|
|
|
|
|
|
|
|
|
|
$out .= $this->writeTimestamp( $row->log_timestamp, " " );
|
|
|
|
|
|
|
|
|
|
if ( $row->log_deleted & LogPage::DELETED_USER ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$out .= " " . Xml::element( 'contributor', [ 'deleted' => 'deleted' ] ) . "\n";
|
2015-12-28 22:23:22 +00:00
|
|
|
} else {
|
|
|
|
|
$out .= $this->writeContributor( $row->log_user, $row->user_name, " " );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $row->log_deleted & LogPage::DELETED_COMMENT ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$out .= " " . Xml::element( 'comment', [ 'deleted' => 'deleted' ] ) . "\n";
|
2017-06-06 17:39:14 +00:00
|
|
|
} else {
|
2018-01-24 23:41:01 +00:00
|
|
|
$comment = CommentStore::getStore()->getComment( 'log_comment', $row )->text;
|
2017-06-06 17:39:14 +00:00
|
|
|
if ( $comment != '' ) {
|
|
|
|
|
$out .= " " . Xml::elementClean( 'comment', null, strval( $comment ) ) . "\n";
|
|
|
|
|
}
|
2015-12-28 22:23:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$out .= " " . Xml::element( 'type', null, strval( $row->log_type ) ) . "\n";
|
|
|
|
|
$out .= " " . Xml::element( 'action', null, strval( $row->log_action ) ) . "\n";
|
|
|
|
|
|
|
|
|
|
if ( $row->log_deleted & LogPage::DELETED_ACTION ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$out .= " " . Xml::element( 'text', [ 'deleted' => 'deleted' ] ) . "\n";
|
2015-12-28 22:23:22 +00:00
|
|
|
} else {
|
|
|
|
|
$title = Title::makeTitle( $row->log_namespace, $row->log_title );
|
|
|
|
|
$out .= " " . Xml::elementClean( 'logtitle', null, self::canonicalTitle( $title ) ) . "\n";
|
|
|
|
|
$out .= " " . Xml::elementClean( 'params',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'xml:space' => 'preserve' ],
|
2015-12-28 22:23:22 +00:00
|
|
|
strval( $row->log_params ) ) . "\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$out .= " </logitem>\n";
|
|
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $timestamp
|
|
|
|
|
* @param string $indent Default to six spaces
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function writeTimestamp( $timestamp, $indent = " " ) {
|
|
|
|
|
$ts = wfTimestamp( TS_ISO_8601, $timestamp );
|
|
|
|
|
return $indent . Xml::element( 'timestamp', null, $ts ) . "\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int $id
|
|
|
|
|
* @param string $text
|
|
|
|
|
* @param string $indent Default to six spaces
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function writeContributor( $id, $text, $indent = " " ) {
|
|
|
|
|
$out = $indent . "<contributor>\n";
|
|
|
|
|
if ( $id || !IP::isValid( $text ) ) {
|
|
|
|
|
$out .= $indent . " " . Xml::elementClean( 'username', null, strval( $text ) ) . "\n";
|
|
|
|
|
$out .= $indent . " " . Xml::element( 'id', null, strval( $id ) ) . "\n";
|
|
|
|
|
} else {
|
|
|
|
|
$out .= $indent . " " . Xml::elementClean( 'ip', null, strval( $text ) ) . "\n";
|
|
|
|
|
}
|
|
|
|
|
$out .= $indent . "</contributor>\n";
|
|
|
|
|
return $out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Warning! This data is potentially inconsistent. :(
|
|
|
|
|
* @param object $row
|
|
|
|
|
* @param bool $dumpContents
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function writeUploads( $row, $dumpContents = false ) {
|
|
|
|
|
if ( $row->page_namespace == NS_FILE ) {
|
|
|
|
|
$img = wfLocalFile( $row->page_title );
|
|
|
|
|
if ( $img && $img->exists() ) {
|
|
|
|
|
$out = '';
|
|
|
|
|
foreach ( array_reverse( $img->getHistory() ) as $ver ) {
|
|
|
|
|
$out .= $this->writeUpload( $ver, $dumpContents );
|
|
|
|
|
}
|
|
|
|
|
$out .= $this->writeUpload( $img, $dumpContents );
|
|
|
|
|
return $out;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param File $file
|
|
|
|
|
* @param bool $dumpContents
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function writeUpload( $file, $dumpContents = false ) {
|
|
|
|
|
if ( $file->isOld() ) {
|
|
|
|
|
$archiveName = " " .
|
|
|
|
|
Xml::element( 'archivename', null, $file->getArchiveName() ) . "\n";
|
|
|
|
|
} else {
|
|
|
|
|
$archiveName = '';
|
|
|
|
|
}
|
|
|
|
|
if ( $dumpContents ) {
|
|
|
|
|
$be = $file->getRepo()->getBackend();
|
|
|
|
|
# Dump file as base64
|
|
|
|
|
# Uses only XML-safe characters, so does not need escaping
|
|
|
|
|
# @todo Too bad this loads the contents into memory (script might swap)
|
|
|
|
|
$contents = ' <contents encoding="base64">' .
|
|
|
|
|
chunk_split( base64_encode(
|
2016-02-17 09:09:32 +00:00
|
|
|
$be->getFileContents( [ 'src' => $file->getPath() ] ) ) ) .
|
2015-12-28 22:23:22 +00:00
|
|
|
" </contents>\n";
|
|
|
|
|
} else {
|
|
|
|
|
$contents = '';
|
|
|
|
|
}
|
|
|
|
|
if ( $file->isDeleted( File::DELETED_COMMENT ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$comment = Xml::element( 'comment', [ 'deleted' => 'deleted' ] );
|
2015-12-28 22:23:22 +00:00
|
|
|
} else {
|
2017-09-18 00:00:07 +00:00
|
|
|
$comment = Xml::elementClean( 'comment', null, strval( $file->getDescription() ) );
|
2015-12-28 22:23:22 +00:00
|
|
|
}
|
|
|
|
|
return " <upload>\n" .
|
|
|
|
|
$this->writeTimestamp( $file->getTimestamp() ) .
|
|
|
|
|
$this->writeContributor( $file->getUser( 'id' ), $file->getUser( 'text' ) ) .
|
|
|
|
|
" " . $comment . "\n" .
|
|
|
|
|
" " . Xml::element( 'filename', null, $file->getName() ) . "\n" .
|
|
|
|
|
$archiveName .
|
2016-03-19 00:08:06 +00:00
|
|
|
" " . Xml::element( 'src', null, $file->getCanonicalUrl() ) . "\n" .
|
2015-12-28 22:23:22 +00:00
|
|
|
" " . Xml::element( 'size', null, $file->getSize() ) . "\n" .
|
|
|
|
|
" " . Xml::element( 'sha1base36', null, $file->getSha1() ) . "\n" .
|
|
|
|
|
" " . Xml::element( 'rel', null, $file->getRel() ) . "\n" .
|
|
|
|
|
$contents .
|
|
|
|
|
" </upload>\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return prefixed text form of title, but using the content language's
|
|
|
|
|
* canonical namespace. This skips any special-casing such as gendered
|
|
|
|
|
* user namespaces -- which while useful, are not yet listed in the
|
|
|
|
|
* XML "<siteinfo>" data so are unsafe in export.
|
|
|
|
|
*
|
|
|
|
|
* @param Title $title
|
|
|
|
|
* @return string
|
|
|
|
|
* @since 1.18
|
|
|
|
|
*/
|
|
|
|
|
public static function canonicalTitle( Title $title ) {
|
|
|
|
|
if ( $title->isExternal() ) {
|
|
|
|
|
return $title->getPrefixedText();
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-29 12:24:54 +00:00
|
|
|
$prefix = MediaWikiServices::getInstance()->getContentLanguage()->
|
|
|
|
|
getFormattedNsText( $title->getNamespace() );
|
2015-12-28 22:23:22 +00:00
|
|
|
|
2016-12-26 01:58:16 +00:00
|
|
|
// @todo Emit some kind of warning to the user if $title->getNamespace() !==
|
|
|
|
|
// NS_MAIN and $prefix === '' (viz. pages in an unregistered namespace)
|
|
|
|
|
|
2015-12-28 22:23:22 +00:00
|
|
|
if ( $prefix !== '' ) {
|
|
|
|
|
$prefix .= ':';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $prefix . $title->getText();
|
|
|
|
|
}
|
|
|
|
|
}
|