don't die producing xml files if rev text export conversion fails
[lhc/web/wiklou.git] / includes / export / XmlDumpWriter.php
1 <?php
2 /**
3 * XmlDumpWriter
4 *
5 * Copyright © 2003, 2005, 2006 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
7 *
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.
12 *
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.
17 *
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
22 *
23 * @file
24 */
25
26 use MediaWiki\MediaWikiServices;
27
28 /**
29 * @ingroup Dump
30 */
31 class XmlDumpWriter {
32 /**
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.
37 *
38 * Output will be encoded in UTF-8.
39 *
40 * @return string
41 */
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",
47 /*
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.
50 *
51 * Create a changeset like https://gerrit.wikimedia.org/r/#/c/149643/ in which
52 * you copy in the new xsd file.
53 *
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
56 */
57 'xsi:schemaLocation' => "http://www.mediawiki.org/xml/export-$ver/ " .
58 "http://www.mediawiki.org/xml/export-$ver.xsd",
59 'version' => $ver,
60 'xml:lang' => MediaWikiServices::getInstance()->getContentLanguage()->getHtmlCode() ],
61 null ) .
62 "\n" .
63 $this->siteInfo();
64 }
65
66 /**
67 * @return string
68 */
69 function siteInfo() {
70 $info = [
71 $this->sitename(),
72 $this->dbname(),
73 $this->homelink(),
74 $this->generator(),
75 $this->caseSetting(),
76 $this->namespaces() ];
77 return " <siteinfo>\n " .
78 implode( "\n ", $info ) .
79 "\n </siteinfo>\n";
80 }
81
82 /**
83 * @return string
84 */
85 function sitename() {
86 global $wgSitename;
87 return Xml::element( 'sitename', [], $wgSitename );
88 }
89
90 /**
91 * @return string
92 */
93 function dbname() {
94 global $wgDBname;
95 return Xml::element( 'dbname', [], $wgDBname );
96 }
97
98 /**
99 * @return string
100 */
101 function generator() {
102 global $wgVersion;
103 return Xml::element( 'generator', [], "MediaWiki $wgVersion" );
104 }
105
106 /**
107 * @return string
108 */
109 function homelink() {
110 return Xml::element( 'base', [], Title::newMainPage()->getCanonicalURL() );
111 }
112
113 /**
114 * @return string
115 */
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 );
121 }
122
123 /**
124 * @return string
125 */
126 function namespaces() {
127 $spaces = "<namespaces>\n";
128 foreach (
129 MediaWikiServices::getInstance()->getContentLanguage()->getFormattedNamespaces()
130 as $ns => $title
131 ) {
132 $spaces .= ' ' .
133 Xml::element( 'namespace',
134 [
135 'key' => $ns,
136 'case' => MWNamespace::isCapitalized( $ns ) ? 'first-letter' : 'case-sensitive',
137 ], $title ) . "\n";
138 }
139 $spaces .= " </namespaces>";
140 return $spaces;
141 }
142
143 /**
144 * Closes the output stream with the closing root element.
145 * Call when finished dumping things.
146 *
147 * @return string
148 */
149 function closeStream() {
150 return "</mediawiki>\n";
151 }
152
153 /**
154 * Opens a "<page>" section on the output stream, with data
155 * from the given database row.
156 *
157 * @param object $row
158 * @return string
159 */
160 public function openPage( $row ) {
161 $out = " <page>\n";
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() ) {
170 $out .= ' ';
171 $out .= Xml::element( 'redirect', [ 'title' => self::canonicalTitle( $redirect ) ] );
172 $out .= "\n";
173 }
174 }
175
176 if ( $row->page_restrictions != '' ) {
177 $out .= ' ' . Xml::element( 'restrictions', [],
178 strval( $row->page_restrictions ) ) . "\n";
179 }
180
181 Hooks::run( 'XmlDumpWriterOpenPage', [ $this, &$out, $row, $title ] );
182
183 return $out;
184 }
185
186 /**
187 * Closes a "<page>" section on the output stream.
188 *
189 * @private
190 * @return string
191 */
192 function closePage() {
193 return " </page>\n";
194 }
195
196 /**
197 * Dumps a "<revision>" section on the output stream, with
198 * data filled in from the given database row.
199 *
200 * @param object $row
201 * @return string
202 * @private
203 */
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";
209 }
210
211 $out .= $this->writeTimestamp( $row->rev_timestamp );
212
213 if ( isset( $row->rev_deleted ) && ( $row->rev_deleted & Revision::DELETED_USER ) ) {
214 $out .= " " . Xml::element( 'contributor', [ 'deleted' => 'deleted' ] ) . "\n";
215 } else {
216 $out .= $this->writeContributor( $row->rev_user, $row->rev_user_text );
217 }
218
219 if ( isset( $row->rev_minor_edit ) && $row->rev_minor_edit ) {
220 $out .= " <minor/>\n";
221 }
222 if ( isset( $row->rev_deleted ) && ( $row->rev_deleted & Revision::DELETED_COMMENT ) ) {
223 $out .= " " . Xml::element( 'comment', [ 'deleted' => 'deleted' ] ) . "\n";
224 } else {
225 $comment = CommentStore::getStore()->getComment( 'rev_comment', $row )->text;
226 if ( $comment != '' ) {
227 $out .= " " . Xml::elementClean( 'comment', [], strval( $comment ) ) . "\n";
228 }
229 }
230
231 if ( isset( $row->rev_content_model ) && !is_null( $row->rev_content_model ) ) {
232 $content_model = strval( $row->rev_content_model );
233 } else {
234 // probably using $wgContentHandlerUseDB = false;
235 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
236 $content_model = ContentHandler::getDefaultModelFor( $title );
237 }
238
239 $content_handler = ContentHandler::getForModelID( $content_model );
240
241 if ( isset( $row->rev_content_format ) && !is_null( $row->rev_content_format ) ) {
242 $content_format = strval( $row->rev_content_format );
243 } else {
244 // probably using $wgContentHandlerUseDB = false;
245 $content_format = $content_handler->getDefaultFormat();
246 }
247
248 $out .= " " . Xml::element( 'model', null, strval( $content_model ) ) . "\n";
249 $out .= " " . Xml::element( 'format', null, strval( $content_format ) ) . "\n";
250
251 $text = '';
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 ) );
257 try {
258 $text = $content_handler->exportTransform( $text, $content_format );
259 }
260 catch ( MWException $ex ) {
261 // leave text as is; that's the way it goes
262 }
263 $out .= " " . Xml::elementClean( 'text',
264 [ 'xml:space' => 'preserve', 'bytes' => intval( $row->rev_len ) ],
265 strval( $text ) ) . "\n";
266 } else {
267 // Stub output
268 $out .= " " . Xml::element( 'text',
269 [ 'id' => $row->rev_text_id, 'bytes' => intval( $row->rev_len ) ],
270 "" ) . "\n";
271 }
272
273 if ( isset( $row->rev_sha1 )
274 && $row->rev_sha1
275 && !( $row->rev_deleted & Revision::DELETED_TEXT )
276 ) {
277 $out .= " " . Xml::element( 'sha1', null, strval( $row->rev_sha1 ) ) . "\n";
278 } else {
279 $out .= " <sha1/>\n";
280 }
281
282 // Avoid PHP 7.1 warning from passing $this by reference
283 $writer = $this;
284 Hooks::run( 'XmlDumpWriterWriteRevision', [ &$writer, &$out, $row, $text ] );
285
286 $out .= " </revision>\n";
287
288 return $out;
289 }
290
291 /**
292 * Dumps a "<logitem>" section on the output stream, with
293 * data filled in from the given database row.
294 *
295 * @param object $row
296 * @return string
297 * @private
298 */
299 function writeLogItem( $row ) {
300 $out = " <logitem>\n";
301 $out .= " " . Xml::element( 'id', null, strval( $row->log_id ) ) . "\n";
302
303 $out .= $this->writeTimestamp( $row->log_timestamp, " " );
304
305 if ( $row->log_deleted & LogPage::DELETED_USER ) {
306 $out .= " " . Xml::element( 'contributor', [ 'deleted' => 'deleted' ] ) . "\n";
307 } else {
308 $out .= $this->writeContributor( $row->log_user, $row->user_name, " " );
309 }
310
311 if ( $row->log_deleted & LogPage::DELETED_COMMENT ) {
312 $out .= " " . Xml::element( 'comment', [ 'deleted' => 'deleted' ] ) . "\n";
313 } else {
314 $comment = CommentStore::getStore()->getComment( 'log_comment', $row )->text;
315 if ( $comment != '' ) {
316 $out .= " " . Xml::elementClean( 'comment', null, strval( $comment ) ) . "\n";
317 }
318 }
319
320 $out .= " " . Xml::element( 'type', null, strval( $row->log_type ) ) . "\n";
321 $out .= " " . Xml::element( 'action', null, strval( $row->log_action ) ) . "\n";
322
323 if ( $row->log_deleted & LogPage::DELETED_ACTION ) {
324 $out .= " " . Xml::element( 'text', [ 'deleted' => 'deleted' ] ) . "\n";
325 } else {
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";
331 }
332
333 $out .= " </logitem>\n";
334
335 return $out;
336 }
337
338 /**
339 * @param string $timestamp
340 * @param string $indent Default to six spaces
341 * @return string
342 */
343 function writeTimestamp( $timestamp, $indent = " " ) {
344 $ts = wfTimestamp( TS_ISO_8601, $timestamp );
345 return $indent . Xml::element( 'timestamp', null, $ts ) . "\n";
346 }
347
348 /**
349 * @param int $id
350 * @param string $text
351 * @param string $indent Default to six spaces
352 * @return string
353 */
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";
359 } else {
360 $out .= $indent . " " . Xml::elementClean( 'ip', null, strval( $text ) ) . "\n";
361 }
362 $out .= $indent . "</contributor>\n";
363 return $out;
364 }
365
366 /**
367 * Warning! This data is potentially inconsistent. :(
368 * @param object $row
369 * @param bool $dumpContents
370 * @return string
371 */
372 function writeUploads( $row, $dumpContents = false ) {
373 if ( $row->page_namespace == NS_FILE ) {
374 $img = wfLocalFile( $row->page_title );
375 if ( $img && $img->exists() ) {
376 $out = '';
377 foreach ( array_reverse( $img->getHistory() ) as $ver ) {
378 $out .= $this->writeUpload( $ver, $dumpContents );
379 }
380 $out .= $this->writeUpload( $img, $dumpContents );
381 return $out;
382 }
383 }
384 return '';
385 }
386
387 /**
388 * @param File $file
389 * @param bool $dumpContents
390 * @return string
391 */
392 function writeUpload( $file, $dumpContents = false ) {
393 if ( $file->isOld() ) {
394 $archiveName = " " .
395 Xml::element( 'archivename', null, $file->getArchiveName() ) . "\n";
396 } else {
397 $archiveName = '';
398 }
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() ] ) ) ) .
407 " </contents>\n";
408 } else {
409 $contents = '';
410 }
411 if ( $file->isDeleted( File::DELETED_COMMENT ) ) {
412 $comment = Xml::element( 'comment', [ 'deleted' => 'deleted' ] );
413 } else {
414 $comment = Xml::elementClean( 'comment', null, strval( $file->getDescription() ) );
415 }
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" .
421 $archiveName .
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" .
426 $contents .
427 " </upload>\n";
428 }
429
430 /**
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.
435 *
436 * @param Title $title
437 * @return string
438 * @since 1.18
439 */
440 public static function canonicalTitle( Title $title ) {
441 if ( $title->isExternal() ) {
442 return $title->getPrefixedText();
443 }
444
445 $prefix = MediaWikiServices::getInstance()->getContentLanguage()->
446 getFormattedNsText( $title->getNamespace() );
447
448 // @todo Emit some kind of warning to the user if $title->getNamespace() !==
449 // NS_MAIN and $prefix === '' (viz. pages in an unregistered namespace)
450
451 if ( $prefix !== '' ) {
452 $prefix .= ':';
453 }
454
455 return $prefix . $title->getText();
456 }
457 }