5 * Copyright © 2003, 2005, 2006 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
26 use MediaWiki\MediaWikiServices
;
33 * Opens the XML output stream's root "<mediawiki>" element.
34 * This does not include an xml directive, so is safe to include
35 * as a subelement in a larger XML stream. Namespace and XML Schema
36 * references are included.
38 * Output will be encoded in UTF-8.
42 function openStream() {
43 $ver = WikiExporter
::schemaVersion();
44 return Xml
::element( 'mediawiki', [
45 'xmlns' => "http://www.mediawiki.org/xml/export-$ver/",
46 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
48 * When a new version of the schema is created, it needs staging on mediawiki.org.
49 * This requires a change in the operations/mediawiki-config git repo.
51 * Create a changeset like https://gerrit.wikimedia.org/r/#/c/149643/ in which
52 * you copy in the new xsd file.
54 * After it is reviewed, merged and deployed (sync-docroot), the index.html needs purging.
55 * echo "https://www.mediawiki.org/xml/index.html" | mwscript purgeList.php --wiki=aawiki
57 'xsi:schemaLocation' => "http://www.mediawiki.org/xml/export-$ver/ " .
58 "http://www.mediawiki.org/xml/export-$ver.xsd",
60 'xml:lang' => MediaWikiServices
::getInstance()->getContentLanguage()->getHtmlCode() ],
76 $this->namespaces() ];
77 return " <siteinfo>\n " .
78 implode( "\n ", $info ) .
87 return Xml
::element( 'sitename', [], $wgSitename );
95 return Xml
::element( 'dbname', [], $wgDBname );
101 function generator() {
103 return Xml
::element( 'generator', [], "MediaWiki $wgVersion" );
109 function homelink() {
110 return Xml
::element( 'base', [], Title
::newMainPage()->getCanonicalURL() );
116 function caseSetting() {
117 global $wgCapitalLinks;
118 // "case-insensitive" option is reserved for future
119 $sensitivity = $wgCapitalLinks ?
'first-letter' : 'case-sensitive';
120 return Xml
::element( 'case', [], $sensitivity );
126 function namespaces() {
127 $spaces = "<namespaces>\n";
129 MediaWikiServices
::getInstance()->getContentLanguage()->getFormattedNamespaces()
133 Xml
::element( 'namespace',
136 'case' => MWNamespace
::isCapitalized( $ns ) ?
'first-letter' : 'case-sensitive',
139 $spaces .= " </namespaces>";
144 * Closes the output stream with the closing root element.
145 * Call when finished dumping things.
149 function closeStream() {
150 return "</mediawiki>\n";
154 * Opens a "<page>" section on the output stream, with data
155 * from the given database row.
160 public function openPage( $row ) {
162 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
163 $out .= ' ' . Xml
::elementClean( 'title', [], self
::canonicalTitle( $title ) ) . "\n";
164 $out .= ' ' . Xml
::element( 'ns', [], strval( $row->page_namespace
) ) . "\n";
165 $out .= ' ' . Xml
::element( 'id', [], strval( $row->page_id
) ) . "\n";
166 if ( $row->page_is_redirect
) {
167 $page = WikiPage
::factory( $title );
168 $redirect = $page->getRedirectTarget();
169 if ( $redirect instanceof Title
&& $redirect->isValidRedirectTarget() ) {
171 $out .= Xml
::element( 'redirect', [ 'title' => self
::canonicalTitle( $redirect ) ] );
176 if ( $row->page_restrictions
!= '' ) {
177 $out .= ' ' . Xml
::element( 'restrictions', [],
178 strval( $row->page_restrictions
) ) . "\n";
181 Hooks
::run( 'XmlDumpWriterOpenPage', [ $this, &$out, $row, $title ] );
187 * Closes a "<page>" section on the output stream.
192 function closePage() {
197 * Dumps a "<revision>" section on the output stream, with
198 * data filled in from the given database row.
204 function writeRevision( $row ) {
205 $out = " <revision>\n";
206 $out .= " " . Xml
::element( 'id', null, strval( $row->rev_id
) ) . "\n";
207 if ( isset( $row->rev_parent_id
) && $row->rev_parent_id
) {
208 $out .= " " . Xml
::element( 'parentid', null, strval( $row->rev_parent_id
) ) . "\n";
211 $out .= $this->writeTimestamp( $row->rev_timestamp
);
213 if ( isset( $row->rev_deleted
) && ( $row->rev_deleted
& Revision
::DELETED_USER
) ) {
214 $out .= " " . Xml
::element( 'contributor', [ 'deleted' => 'deleted' ] ) . "\n";
216 $out .= $this->writeContributor( $row->rev_user
, $row->rev_user_text
);
219 if ( isset( $row->rev_minor_edit
) && $row->rev_minor_edit
) {
220 $out .= " <minor/>\n";
222 if ( isset( $row->rev_deleted
) && ( $row->rev_deleted
& Revision
::DELETED_COMMENT
) ) {
223 $out .= " " . Xml
::element( 'comment', [ 'deleted' => 'deleted' ] ) . "\n";
225 $comment = CommentStore
::getStore()->getComment( 'rev_comment', $row )->text
;
226 if ( $comment != '' ) {
227 $out .= " " . Xml
::elementClean( 'comment', [], strval( $comment ) ) . "\n";
231 if ( isset( $row->rev_content_model
) && !is_null( $row->rev_content_model
) ) {
232 $content_model = strval( $row->rev_content_model
);
234 // probably using $wgContentHandlerUseDB = false;
235 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
236 $content_model = ContentHandler
::getDefaultModelFor( $title );
239 $content_handler = ContentHandler
::getForModelID( $content_model );
241 if ( isset( $row->rev_content_format
) && !is_null( $row->rev_content_format
) ) {
242 $content_format = strval( $row->rev_content_format
);
244 // probably using $wgContentHandlerUseDB = false;
245 $content_format = $content_handler->getDefaultFormat();
248 $out .= " " . Xml
::element( 'model', null, strval( $content_model ) ) . "\n";
249 $out .= " " . Xml
::element( 'format', null, strval( $content_format ) ) . "\n";
252 if ( isset( $row->rev_deleted
) && ( $row->rev_deleted
& Revision
::DELETED_TEXT
) ) {
253 $out .= " " . Xml
::element( 'text', [ 'deleted' => 'deleted' ] ) . "\n";
254 } elseif ( isset( $row->old_text
) ) {
255 // Raw text from the database may have invalid chars
256 $text = strval( Revision
::getRevisionText( $row ) );
258 $text = $content_handler->exportTransform( $text, $content_format );
260 catch ( MWException
$ex ) {
261 // leave text as is; that's the way it goes
263 $out .= " " . Xml
::elementClean( 'text',
264 [ 'xml:space' => 'preserve', 'bytes' => intval( $row->rev_len
) ],
265 strval( $text ) ) . "\n";
268 $out .= " " . Xml
::element( 'text',
269 [ 'id' => $row->rev_text_id
, 'bytes' => intval( $row->rev_len
) ],
273 if ( isset( $row->rev_sha1
)
275 && !( $row->rev_deleted
& Revision
::DELETED_TEXT
)
277 $out .= " " . Xml
::element( 'sha1', null, strval( $row->rev_sha1
) ) . "\n";
279 $out .= " <sha1/>\n";
282 // Avoid PHP 7.1 warning from passing $this by reference
284 Hooks
::run( 'XmlDumpWriterWriteRevision', [ &$writer, &$out, $row, $text ] );
286 $out .= " </revision>\n";
292 * Dumps a "<logitem>" section on the output stream, with
293 * data filled in from the given database row.
299 function writeLogItem( $row ) {
300 $out = " <logitem>\n";
301 $out .= " " . Xml
::element( 'id', null, strval( $row->log_id
) ) . "\n";
303 $out .= $this->writeTimestamp( $row->log_timestamp
, " " );
305 if ( $row->log_deleted
& LogPage
::DELETED_USER
) {
306 $out .= " " . Xml
::element( 'contributor', [ 'deleted' => 'deleted' ] ) . "\n";
308 $out .= $this->writeContributor( $row->log_user
, $row->user_name
, " " );
311 if ( $row->log_deleted
& LogPage
::DELETED_COMMENT
) {
312 $out .= " " . Xml
::element( 'comment', [ 'deleted' => 'deleted' ] ) . "\n";
314 $comment = CommentStore
::getStore()->getComment( 'log_comment', $row )->text
;
315 if ( $comment != '' ) {
316 $out .= " " . Xml
::elementClean( 'comment', null, strval( $comment ) ) . "\n";
320 $out .= " " . Xml
::element( 'type', null, strval( $row->log_type
) ) . "\n";
321 $out .= " " . Xml
::element( 'action', null, strval( $row->log_action
) ) . "\n";
323 if ( $row->log_deleted
& LogPage
::DELETED_ACTION
) {
324 $out .= " " . Xml
::element( 'text', [ 'deleted' => 'deleted' ] ) . "\n";
326 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
327 $out .= " " . Xml
::elementClean( 'logtitle', null, self
::canonicalTitle( $title ) ) . "\n";
328 $out .= " " . Xml
::elementClean( 'params',
329 [ 'xml:space' => 'preserve' ],
330 strval( $row->log_params
) ) . "\n";
333 $out .= " </logitem>\n";
339 * @param string $timestamp
340 * @param string $indent Default to six spaces
343 function writeTimestamp( $timestamp, $indent = " " ) {
344 $ts = wfTimestamp( TS_ISO_8601
, $timestamp );
345 return $indent . Xml
::element( 'timestamp', null, $ts ) . "\n";
350 * @param string $text
351 * @param string $indent Default to six spaces
354 function writeContributor( $id, $text, $indent = " " ) {
355 $out = $indent . "<contributor>\n";
356 if ( $id ||
!IP
::isValid( $text ) ) {
357 $out .= $indent . " " . Xml
::elementClean( 'username', null, strval( $text ) ) . "\n";
358 $out .= $indent . " " . Xml
::element( 'id', null, strval( $id ) ) . "\n";
360 $out .= $indent . " " . Xml
::elementClean( 'ip', null, strval( $text ) ) . "\n";
362 $out .= $indent . "</contributor>\n";
367 * Warning! This data is potentially inconsistent. :(
369 * @param bool $dumpContents
372 function writeUploads( $row, $dumpContents = false ) {
373 if ( $row->page_namespace
== NS_FILE
) {
374 $img = wfLocalFile( $row->page_title
);
375 if ( $img && $img->exists() ) {
377 foreach ( array_reverse( $img->getHistory() ) as $ver ) {
378 $out .= $this->writeUpload( $ver, $dumpContents );
380 $out .= $this->writeUpload( $img, $dumpContents );
389 * @param bool $dumpContents
392 function writeUpload( $file, $dumpContents = false ) {
393 if ( $file->isOld() ) {
395 Xml
::element( 'archivename', null, $file->getArchiveName() ) . "\n";
399 if ( $dumpContents ) {
400 $be = $file->getRepo()->getBackend();
401 # Dump file as base64
402 # Uses only XML-safe characters, so does not need escaping
403 # @todo Too bad this loads the contents into memory (script might swap)
404 $contents = ' <contents encoding="base64">' .
405 chunk_split( base64_encode(
406 $be->getFileContents( [ 'src' => $file->getPath() ] ) ) ) .
411 if ( $file->isDeleted( File
::DELETED_COMMENT
) ) {
412 $comment = Xml
::element( 'comment', [ 'deleted' => 'deleted' ] );
414 $comment = Xml
::elementClean( 'comment', null, strval( $file->getDescription() ) );
416 return " <upload>\n" .
417 $this->writeTimestamp( $file->getTimestamp() ) .
418 $this->writeContributor( $file->getUser( 'id' ), $file->getUser( 'text' ) ) .
419 " " . $comment . "\n" .
420 " " . Xml
::element( 'filename', null, $file->getName() ) . "\n" .
422 " " . Xml
::element( 'src', null, $file->getCanonicalUrl() ) . "\n" .
423 " " . Xml
::element( 'size', null, $file->getSize() ) . "\n" .
424 " " . Xml
::element( 'sha1base36', null, $file->getSha1() ) . "\n" .
425 " " . Xml
::element( 'rel', null, $file->getRel() ) . "\n" .
431 * Return prefixed text form of title, but using the content language's
432 * canonical namespace. This skips any special-casing such as gendered
433 * user namespaces -- which while useful, are not yet listed in the
434 * XML "<siteinfo>" data so are unsafe in export.
436 * @param Title $title
440 public static function canonicalTitle( Title
$title ) {
441 if ( $title->isExternal() ) {
442 return $title->getPrefixedText();
445 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
446 getFormattedNsText( $title->getNamespace() );
448 // @todo Emit some kind of warning to the user if $title->getNamespace() !==
449 // NS_MAIN and $prefix === '' (viz. pages in an unregistered namespace)
451 if ( $prefix !== '' ) {
455 return $prefix . $title->getText();