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
31 * Opens the XML output stream's root "<mediawiki>" element.
32 * This does not include an xml directive, so is safe to include
33 * as a subelement in a larger XML stream. Namespace and XML Schema
34 * references are included.
36 * Output will be encoded in UTF-8.
40 function openStream() {
41 global $wgLanguageCode;
42 $ver = WikiExporter
::schemaVersion();
43 return Xml
::element( 'mediawiki', [
44 'xmlns' => "http://www.mediawiki.org/xml/export-$ver/",
45 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
47 * When a new version of the schema is created, it needs staging on mediawiki.org.
48 * This requires a change in the operations/mediawiki-config git repo.
50 * Create a changeset like https://gerrit.wikimedia.org/r/#/c/149643/ in which
51 * you copy in the new xsd file.
53 * After it is reviewed, merged and deployed (sync-docroot), the index.html needs purging.
54 * echo "http://www.mediawiki.org/xml/index.html" | mwscript purgeList.php --wiki=aawiki
56 'xsi:schemaLocation' => "http://www.mediawiki.org/xml/export-$ver/ " .
57 "http://www.mediawiki.org/xml/export-$ver.xsd",
59 'xml:lang' => $wgLanguageCode ],
75 $this->namespaces() ];
76 return " <siteinfo>\n " .
77 implode( "\n ", $info ) .
86 return Xml
::element( 'sitename', [], $wgSitename );
94 return Xml
::element( 'dbname', [], $wgDBname );
100 function generator() {
102 return Xml
::element( 'generator', [], "MediaWiki $wgVersion" );
108 function homelink() {
109 return Xml
::element( 'base', [], Title
::newMainPage()->getCanonicalURL() );
115 function caseSetting() {
116 global $wgCapitalLinks;
117 // "case-insensitive" option is reserved for future
118 $sensitivity = $wgCapitalLinks ?
'first-letter' : 'case-sensitive';
119 return Xml
::element( 'case', [], $sensitivity );
125 function namespaces() {
127 $spaces = "<namespaces>\n";
128 foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
130 Xml
::element( 'namespace',
133 'case' => MWNamespace
::isCapitalized( $ns ) ?
'first-letter' : 'case-sensitive',
136 $spaces .= " </namespaces>";
141 * Closes the output stream with the closing root element.
142 * Call when finished dumping things.
146 function closeStream() {
147 return "</mediawiki>\n";
151 * Opens a "<page>" section on the output stream, with data
152 * from the given database row.
157 public function openPage( $row ) {
159 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
160 $out .= ' ' . Xml
::elementClean( 'title', [], self
::canonicalTitle( $title ) ) . "\n";
161 $out .= ' ' . Xml
::element( 'ns', [], strval( $row->page_namespace
) ) . "\n";
162 $out .= ' ' . Xml
::element( 'id', [], strval( $row->page_id
) ) . "\n";
163 if ( $row->page_is_redirect
) {
164 $page = WikiPage
::factory( $title );
165 $redirect = $page->getRedirectTarget();
166 if ( $redirect instanceof Title
&& $redirect->isValidRedirectTarget() ) {
168 $out .= Xml
::element( 'redirect', [ 'title' => self
::canonicalTitle( $redirect ) ] );
173 if ( $row->page_restrictions
!= '' ) {
174 $out .= ' ' . Xml
::element( 'restrictions', [],
175 strval( $row->page_restrictions
) ) . "\n";
178 Hooks
::run( 'XmlDumpWriterOpenPage', [ $this, &$out, $row, $title ] );
184 * Closes a "<page>" section on the output stream.
189 function closePage() {
194 * Dumps a "<revision>" section on the output stream, with
195 * data filled in from the given database row.
201 function writeRevision( $row ) {
203 $out = " <revision>\n";
204 $out .= " " . Xml
::element( 'id', null, strval( $row->rev_id
) ) . "\n";
205 if ( isset( $row->rev_parent_id
) && $row->rev_parent_id
) {
206 $out .= " " . Xml
::element( 'parentid', null, strval( $row->rev_parent_id
) ) . "\n";
209 $out .= $this->writeTimestamp( $row->rev_timestamp
);
211 if ( isset( $row->rev_deleted
) && ( $row->rev_deleted
& Revision
::DELETED_USER
) ) {
212 $out .= " " . Xml
::element( 'contributor', [ 'deleted' => 'deleted' ] ) . "\n";
214 $out .= $this->writeContributor( $row->rev_user
, $row->rev_user_text
);
217 if ( isset( $row->rev_minor_edit
) && $row->rev_minor_edit
) {
218 $out .= " <minor/>\n";
220 if ( isset( $row->rev_deleted
) && ( $row->rev_deleted
& Revision
::DELETED_COMMENT
) ) {
221 $out .= " " . Xml
::element( 'comment', [ 'deleted' => 'deleted' ] ) . "\n";
222 } elseif ( $row->rev_comment
!= '' ) {
223 $out .= " " . Xml
::elementClean( 'comment', [], strval( $row->rev_comment
) ) . "\n";
226 if ( isset( $row->rev_content_model
) && !is_null( $row->rev_content_model
) ) {
227 $content_model = strval( $row->rev_content_model
);
229 // probably using $wgContentHandlerUseDB = false;
230 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
231 $content_model = ContentHandler
::getDefaultModelFor( $title );
234 $content_handler = ContentHandler
::getForModelID( $content_model );
236 if ( isset( $row->rev_content_format
) && !is_null( $row->rev_content_format
) ) {
237 $content_format = strval( $row->rev_content_format
);
239 // probably using $wgContentHandlerUseDB = false;
240 $content_format = $content_handler->getDefaultFormat();
243 $out .= " " . Xml
::element( 'model', null, strval( $content_model ) ) . "\n";
244 $out .= " " . Xml
::element( 'format', null, strval( $content_format ) ) . "\n";
247 if ( isset( $row->rev_deleted
) && ( $row->rev_deleted
& Revision
::DELETED_TEXT
) ) {
248 $out .= " " . Xml
::element( 'text', [ 'deleted' => 'deleted' ] ) . "\n";
249 } elseif ( isset( $row->old_text
) ) {
250 // Raw text from the database may have invalid chars
251 $text = strval( Revision
::getRevisionText( $row ) );
252 $text = $content_handler->exportTransform( $text, $content_format );
253 $out .= " " . Xml
::elementClean( 'text',
254 [ 'xml:space' => 'preserve', 'bytes' => intval( $row->rev_len
) ],
255 strval( $text ) ) . "\n";
258 $out .= " " . Xml
::element( 'text',
259 [ 'id' => $row->rev_text_id
, 'bytes' => intval( $row->rev_len
) ],
263 if ( isset( $row->rev_sha1
)
265 && !( $row->rev_deleted
& Revision
::DELETED_TEXT
)
267 $out .= " " . Xml
::element( 'sha1', null, strval( $row->rev_sha1
) ) . "\n";
269 $out .= " <sha1/>\n";
272 Hooks
::run( 'XmlDumpWriterWriteRevision', [ &$this, &$out, $row, $text ] );
274 $out .= " </revision>\n";
280 * Dumps a "<logitem>" section on the output stream, with
281 * data filled in from the given database row.
287 function writeLogItem( $row ) {
289 $out = " <logitem>\n";
290 $out .= " " . Xml
::element( 'id', null, strval( $row->log_id
) ) . "\n";
292 $out .= $this->writeTimestamp( $row->log_timestamp
, " " );
294 if ( $row->log_deleted
& LogPage
::DELETED_USER
) {
295 $out .= " " . Xml
::element( 'contributor', [ 'deleted' => 'deleted' ] ) . "\n";
297 $out .= $this->writeContributor( $row->log_user
, $row->user_name
, " " );
300 if ( $row->log_deleted
& LogPage
::DELETED_COMMENT
) {
301 $out .= " " . Xml
::element( 'comment', [ 'deleted' => 'deleted' ] ) . "\n";
302 } elseif ( $row->log_comment
!= '' ) {
303 $out .= " " . Xml
::elementClean( 'comment', null, strval( $row->log_comment
) ) . "\n";
306 $out .= " " . Xml
::element( 'type', null, strval( $row->log_type
) ) . "\n";
307 $out .= " " . Xml
::element( 'action', null, strval( $row->log_action
) ) . "\n";
309 if ( $row->log_deleted
& LogPage
::DELETED_ACTION
) {
310 $out .= " " . Xml
::element( 'text', [ 'deleted' => 'deleted' ] ) . "\n";
312 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
313 $out .= " " . Xml
::elementClean( 'logtitle', null, self
::canonicalTitle( $title ) ) . "\n";
314 $out .= " " . Xml
::elementClean( 'params',
315 [ 'xml:space' => 'preserve' ],
316 strval( $row->log_params
) ) . "\n";
319 $out .= " </logitem>\n";
325 * @param string $timestamp
326 * @param string $indent Default to six spaces
329 function writeTimestamp( $timestamp, $indent = " " ) {
330 $ts = wfTimestamp( TS_ISO_8601
, $timestamp );
331 return $indent . Xml
::element( 'timestamp', null, $ts ) . "\n";
336 * @param string $text
337 * @param string $indent Default to six spaces
340 function writeContributor( $id, $text, $indent = " " ) {
341 $out = $indent . "<contributor>\n";
342 if ( $id ||
!IP
::isValid( $text ) ) {
343 $out .= $indent . " " . Xml
::elementClean( 'username', null, strval( $text ) ) . "\n";
344 $out .= $indent . " " . Xml
::element( 'id', null, strval( $id ) ) . "\n";
346 $out .= $indent . " " . Xml
::elementClean( 'ip', null, strval( $text ) ) . "\n";
348 $out .= $indent . "</contributor>\n";
353 * Warning! This data is potentially inconsistent. :(
355 * @param bool $dumpContents
358 function writeUploads( $row, $dumpContents = false ) {
359 if ( $row->page_namespace
== NS_FILE
) {
360 $img = wfLocalFile( $row->page_title
);
361 if ( $img && $img->exists() ) {
363 foreach ( array_reverse( $img->getHistory() ) as $ver ) {
364 $out .= $this->writeUpload( $ver, $dumpContents );
366 $out .= $this->writeUpload( $img, $dumpContents );
375 * @param bool $dumpContents
378 function writeUpload( $file, $dumpContents = false ) {
379 if ( $file->isOld() ) {
381 Xml
::element( 'archivename', null, $file->getArchiveName() ) . "\n";
385 if ( $dumpContents ) {
386 $be = $file->getRepo()->getBackend();
387 # Dump file as base64
388 # Uses only XML-safe characters, so does not need escaping
389 # @todo Too bad this loads the contents into memory (script might swap)
390 $contents = ' <contents encoding="base64">' .
391 chunk_split( base64_encode(
392 $be->getFileContents( [ 'src' => $file->getPath() ] ) ) ) .
397 if ( $file->isDeleted( File
::DELETED_COMMENT
) ) {
398 $comment = Xml
::element( 'comment', [ 'deleted' => 'deleted' ] );
400 $comment = Xml
::elementClean( 'comment', null, $file->getDescription() );
402 return " <upload>\n" .
403 $this->writeTimestamp( $file->getTimestamp() ) .
404 $this->writeContributor( $file->getUser( 'id' ), $file->getUser( 'text' ) ) .
405 " " . $comment . "\n" .
406 " " . Xml
::element( 'filename', null, $file->getName() ) . "\n" .
408 " " . Xml
::element( 'src', null, $file->getCanonicalUrl() ) . "\n" .
409 " " . Xml
::element( 'size', null, $file->getSize() ) . "\n" .
410 " " . Xml
::element( 'sha1base36', null, $file->getSha1() ) . "\n" .
411 " " . Xml
::element( 'rel', null, $file->getRel() ) . "\n" .
417 * Return prefixed text form of title, but using the content language's
418 * canonical namespace. This skips any special-casing such as gendered
419 * user namespaces -- which while useful, are not yet listed in the
420 * XML "<siteinfo>" data so are unsafe in export.
422 * @param Title $title
426 public static function canonicalTitle( Title
$title ) {
427 if ( $title->isExternal() ) {
428 return $title->getPrefixedText();
432 $prefix = $wgContLang->getFormattedNsText( $title->getNamespace() );
434 if ( $prefix !== '' ) {
438 return $prefix . $title->getText();