Removed double hook explanation AfterFinalPageOutput
[lhc/web/wiklou.git] / includes / Export.php
1 <?php
2 /**
3 * Base classes for dumps and export
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 /**
27 * @defgroup Dump Dump
28 */
29
30 /**
31 * @ingroup SpecialPage Dump
32 */
33 class WikiExporter {
34 /** @var bool Return distinct author list (when not returning full history) */
35 public $list_authors = false;
36
37 /** @var bool */
38 public $dumpUploads = false;
39
40 /** @var bool */
41 public $dumpUploadFileContents = false;
42
43 /** @var string */
44 protected $author_list = "";
45
46 const FULL = 1;
47 const CURRENT = 2;
48 const STABLE = 4; // extension defined
49 const LOGS = 8;
50 const RANGE = 16;
51
52 const BUFFER = 0;
53 const STREAM = 1;
54
55 const TEXT = 0;
56 const STUB = 1;
57
58 /** @var int */
59 protected $buffer;
60
61 /** @var int */
62 protected $text;
63
64 /** @var DumpOutput */
65 protected $sink;
66
67 /**
68 * Returns the export schema version.
69 * @return string
70 */
71 public static function schemaVersion() {
72 return "0.8";
73 }
74
75 /**
76 * If using WikiExporter::STREAM to stream a large amount of data,
77 * provide a database connection which is not managed by
78 * LoadBalancer to read from: some history blob types will
79 * make additional queries to pull source data while the
80 * main query is still running.
81 *
82 * @param DatabaseBase $db
83 * @param int|array $history One of WikiExporter::FULL, WikiExporter::CURRENT,
84 * WikiExporter::RANGE or WikiExporter::STABLE, or an associative array:
85 * - offset: non-inclusive offset at which to start the query
86 * - limit: maximum number of rows to return
87 * - dir: "asc" or "desc" timestamp order
88 * @param int $buffer One of WikiExporter::BUFFER or WikiExporter::STREAM
89 * @param int $text One of WikiExporter::TEXT or WikiExporter::STUB
90 */
91 function __construct( $db, $history = WikiExporter::CURRENT,
92 $buffer = WikiExporter::BUFFER, $text = WikiExporter::TEXT ) {
93 $this->db = $db;
94 $this->history = $history;
95 $this->buffer = $buffer;
96 $this->writer = new XmlDumpWriter();
97 $this->sink = new DumpOutput();
98 $this->text = $text;
99 }
100
101 /**
102 * Set the DumpOutput or DumpFilter object which will receive
103 * various row objects and XML output for filtering. Filters
104 * can be chained or used as callbacks.
105 *
106 * @param DumpOutput $sink
107 */
108 public function setOutputSink( &$sink ) {
109 $this->sink =& $sink;
110 }
111
112 public function openStream() {
113 $output = $this->writer->openStream();
114 $this->sink->writeOpenStream( $output );
115 }
116
117 public function closeStream() {
118 $output = $this->writer->closeStream();
119 $this->sink->writeCloseStream( $output );
120 }
121
122 /**
123 * Dumps a series of page and revision records for all pages
124 * in the database, either including complete history or only
125 * the most recent version.
126 */
127 public function allPages() {
128 $this->dumpFrom( '' );
129 }
130
131 /**
132 * Dumps a series of page and revision records for those pages
133 * in the database falling within the page_id range given.
134 * @param int $start Inclusive lower limit (this id is included)
135 * @param int $end Exclusive upper limit (this id is not included)
136 * If 0, no upper limit.
137 */
138 public function pagesByRange( $start, $end ) {
139 $condition = 'page_id >= ' . intval( $start );
140 if ( $end ) {
141 $condition .= ' AND page_id < ' . intval( $end );
142 }
143 $this->dumpFrom( $condition );
144 }
145
146 /**
147 * Dumps a series of page and revision records for those pages
148 * in the database with revisions falling within the rev_id range given.
149 * @param int $start Inclusive lower limit (this id is included)
150 * @param int $end Exclusive upper limit (this id is not included)
151 * If 0, no upper limit.
152 */
153 public function revsByRange( $start, $end ) {
154 $condition = 'rev_id >= ' . intval( $start );
155 if ( $end ) {
156 $condition .= ' AND rev_id < ' . intval( $end );
157 }
158 $this->dumpFrom( $condition );
159 }
160
161 /**
162 * @param Title $title
163 */
164 public function pageByTitle( $title ) {
165 $this->dumpFrom(
166 'page_namespace=' . $title->getNamespace() .
167 ' AND page_title=' . $this->db->addQuotes( $title->getDBkey() ) );
168 }
169
170 /**
171 * @param string $name
172 * @throws MWException
173 */
174 public function pageByName( $name ) {
175 $title = Title::newFromText( $name );
176 if ( is_null( $title ) ) {
177 throw new MWException( "Can't export invalid title" );
178 } else {
179 $this->pageByTitle( $title );
180 }
181 }
182
183 /**
184 * @param array $names
185 */
186 public function pagesByName( $names ) {
187 foreach ( $names as $name ) {
188 $this->pageByName( $name );
189 }
190 }
191
192 public function allLogs() {
193 $this->dumpFrom( '' );
194 }
195
196 /**
197 * @param int $start
198 * @param int $end
199 */
200 public function logsByRange( $start, $end ) {
201 $condition = 'log_id >= ' . intval( $start );
202 if ( $end ) {
203 $condition .= ' AND log_id < ' . intval( $end );
204 }
205 $this->dumpFrom( $condition );
206 }
207
208 /**
209 * Generates the distinct list of authors of an article
210 * Not called by default (depends on $this->list_authors)
211 * Can be set by Special:Export when not exporting whole history
212 *
213 * @param array $cond
214 */
215 protected function do_list_authors( $cond ) {
216 wfProfileIn( __METHOD__ );
217 $this->author_list = "<contributors>";
218 // rev_deleted
219
220 $res = $this->db->select(
221 array( 'page', 'revision' ),
222 array( 'DISTINCT rev_user_text', 'rev_user' ),
223 array(
224 $this->db->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0',
225 $cond,
226 'page_id = rev_id',
227 ),
228 __METHOD__
229 );
230
231 foreach ( $res as $row ) {
232 $this->author_list .= "<contributor>" .
233 "<username>" .
234 htmlentities( $row->rev_user_text ) .
235 "</username>" .
236 "<id>" .
237 $row->rev_user .
238 "</id>" .
239 "</contributor>";
240 }
241 $this->author_list .= "</contributors>";
242 wfProfileOut( __METHOD__ );
243 }
244
245 /**
246 * @param string $cond
247 * @throws MWException
248 * @throws Exception
249 */
250 protected function dumpFrom( $cond = '' ) {
251 wfProfileIn( __METHOD__ );
252 # For logging dumps...
253 if ( $this->history & self::LOGS ) {
254 $where = array( 'user_id = log_user' );
255 # Hide private logs
256 $hideLogs = LogEventsList::getExcludeClause( $this->db );
257 if ( $hideLogs ) {
258 $where[] = $hideLogs;
259 }
260 # Add on any caller specified conditions
261 if ( $cond ) {
262 $where[] = $cond;
263 }
264 # Get logging table name for logging.* clause
265 $logging = $this->db->tableName( 'logging' );
266
267 if ( $this->buffer == WikiExporter::STREAM ) {
268 $prev = $this->db->bufferResults( false );
269 }
270 $wrapper = null; // Assuring $wrapper is not undefined, if exception occurs early
271 try {
272 $result = $this->db->select( array( 'logging', 'user' ),
273 array( "{$logging}.*", 'user_name' ), // grab the user name
274 $where,
275 __METHOD__,
276 array( 'ORDER BY' => 'log_id', 'USE INDEX' => array( 'logging' => 'PRIMARY' ) )
277 );
278 $wrapper = $this->db->resultObject( $result );
279 $this->outputLogStream( $wrapper );
280 if ( $this->buffer == WikiExporter::STREAM ) {
281 $this->db->bufferResults( $prev );
282 }
283 } catch ( Exception $e ) {
284 // Throwing the exception does not reliably free the resultset, and
285 // would also leave the connection in unbuffered mode.
286
287 // Freeing result
288 try {
289 if ( $wrapper ) {
290 $wrapper->free();
291 }
292 } catch ( Exception $e2 ) {
293 // Already in panic mode -> ignoring $e2 as $e has
294 // higher priority
295 }
296
297 // Putting database back in previous buffer mode
298 try {
299 if ( $this->buffer == WikiExporter::STREAM ) {
300 $this->db->bufferResults( $prev );
301 }
302 } catch ( Exception $e2 ) {
303 // Already in panic mode -> ignoring $e2 as $e has
304 // higher priority
305 }
306
307 // Inform caller about problem
308 wfProfileOut( __METHOD__ );
309 throw $e;
310 }
311 # For page dumps...
312 } else {
313 $tables = array( 'page', 'revision' );
314 $opts = array( 'ORDER BY' => 'page_id ASC' );
315 $opts['USE INDEX'] = array();
316 $join = array();
317 if ( is_array( $this->history ) ) {
318 # Time offset/limit for all pages/history...
319 $revJoin = 'page_id=rev_page';
320 # Set time order
321 if ( $this->history['dir'] == 'asc' ) {
322 $op = '>';
323 $opts['ORDER BY'] = 'rev_timestamp ASC';
324 } else {
325 $op = '<';
326 $opts['ORDER BY'] = 'rev_timestamp DESC';
327 }
328 # Set offset
329 if ( !empty( $this->history['offset'] ) ) {
330 $revJoin .= " AND rev_timestamp $op " .
331 $this->db->addQuotes( $this->db->timestamp( $this->history['offset'] ) );
332 }
333 $join['revision'] = array( 'INNER JOIN', $revJoin );
334 # Set query limit
335 if ( !empty( $this->history['limit'] ) ) {
336 $opts['LIMIT'] = intval( $this->history['limit'] );
337 }
338 } elseif ( $this->history & WikiExporter::FULL ) {
339 # Full history dumps...
340 $join['revision'] = array( 'INNER JOIN', 'page_id=rev_page' );
341 } elseif ( $this->history & WikiExporter::CURRENT ) {
342 # Latest revision dumps...
343 if ( $this->list_authors && $cond != '' ) { // List authors, if so desired
344 $this->do_list_authors( $cond );
345 }
346 $join['revision'] = array( 'INNER JOIN', 'page_id=rev_page AND page_latest=rev_id' );
347 } elseif ( $this->history & WikiExporter::STABLE ) {
348 # "Stable" revision dumps...
349 # Default JOIN, to be overridden...
350 $join['revision'] = array( 'INNER JOIN', 'page_id=rev_page AND page_latest=rev_id' );
351 # One, and only one hook should set this, and return false
352 if ( wfRunHooks( 'WikiExporter::dumpStableQuery', array( &$tables, &$opts, &$join ) ) ) {
353 wfProfileOut( __METHOD__ );
354 throw new MWException( __METHOD__ . " given invalid history dump type." );
355 }
356 } elseif ( $this->history & WikiExporter::RANGE ) {
357 # Dump of revisions within a specified range
358 $join['revision'] = array( 'INNER JOIN', 'page_id=rev_page' );
359 $opts['ORDER BY'] = array( 'rev_page ASC', 'rev_id ASC' );
360 } else {
361 # Unknown history specification parameter?
362 wfProfileOut( __METHOD__ );
363 throw new MWException( __METHOD__ . " given invalid history dump type." );
364 }
365 # Query optimization hacks
366 if ( $cond == '' ) {
367 $opts[] = 'STRAIGHT_JOIN';
368 $opts['USE INDEX']['page'] = 'PRIMARY';
369 }
370 # Build text join options
371 if ( $this->text != WikiExporter::STUB ) { // 1-pass
372 $tables[] = 'text';
373 $join['text'] = array( 'INNER JOIN', 'rev_text_id=old_id' );
374 }
375
376 if ( $this->buffer == WikiExporter::STREAM ) {
377 $prev = $this->db->bufferResults( false );
378 }
379
380 $wrapper = null; // Assuring $wrapper is not undefined, if exception occurs early
381 try {
382 wfRunHooks( 'ModifyExportQuery',
383 array( $this->db, &$tables, &$cond, &$opts, &$join ) );
384
385 # Do the query!
386 $result = $this->db->select( $tables, '*', $cond, __METHOD__, $opts, $join );
387 $wrapper = $this->db->resultObject( $result );
388 # Output dump results
389 $this->outputPageStream( $wrapper );
390
391 if ( $this->buffer == WikiExporter::STREAM ) {
392 $this->db->bufferResults( $prev );
393 }
394 } catch ( Exception $e ) {
395 // Throwing the exception does not reliably free the resultset, and
396 // would also leave the connection in unbuffered mode.
397
398 // Freeing result
399 try {
400 if ( $wrapper ) {
401 $wrapper->free();
402 }
403 } catch ( Exception $e2 ) {
404 // Already in panic mode -> ignoring $e2 as $e has
405 // higher priority
406 }
407
408 // Putting database back in previous buffer mode
409 try {
410 if ( $this->buffer == WikiExporter::STREAM ) {
411 $this->db->bufferResults( $prev );
412 }
413 } catch ( Exception $e2 ) {
414 // Already in panic mode -> ignoring $e2 as $e has
415 // higher priority
416 }
417
418 // Inform caller about problem
419 throw $e;
420 }
421 }
422 wfProfileOut( __METHOD__ );
423 }
424
425 /**
426 * Runs through a query result set dumping page and revision records.
427 * The result set should be sorted/grouped by page to avoid duplicate
428 * page records in the output.
429 *
430 * Should be safe for
431 * streaming (non-buffered) queries, as long as it was made on a
432 * separate database connection not managed by LoadBalancer; some
433 * blob storage types will make queries to pull source data.
434 *
435 * @param ResultWrapper $resultset
436 */
437 protected function outputPageStream( $resultset ) {
438 $last = null;
439 foreach ( $resultset as $row ) {
440 if ( $last === null ||
441 $last->page_namespace != $row->page_namespace ||
442 $last->page_title != $row->page_title ) {
443 if ( $last !== null ) {
444 $output = '';
445 if ( $this->dumpUploads ) {
446 $output .= $this->writer->writeUploads( $last, $this->dumpUploadFileContents );
447 }
448 $output .= $this->writer->closePage();
449 $this->sink->writeClosePage( $output );
450 }
451 $output = $this->writer->openPage( $row );
452 $this->sink->writeOpenPage( $row, $output );
453 $last = $row;
454 }
455 $output = $this->writer->writeRevision( $row );
456 $this->sink->writeRevision( $row, $output );
457 }
458 if ( $last !== null ) {
459 $output = '';
460 if ( $this->dumpUploads ) {
461 $output .= $this->writer->writeUploads( $last, $this->dumpUploadFileContents );
462 }
463 $output .= $this->author_list;
464 $output .= $this->writer->closePage();
465 $this->sink->writeClosePage( $output );
466 }
467 }
468
469 /**
470 * @param array $resultset
471 */
472 protected function outputLogStream( $resultset ) {
473 foreach ( $resultset as $row ) {
474 $output = $this->writer->writeLogItem( $row );
475 $this->sink->writeLogItem( $row, $output );
476 }
477 }
478 }
479
480 /**
481 * @ingroup Dump
482 */
483 class XmlDumpWriter {
484 /**
485 * Returns the export schema version.
486 * @deprecated since 1.20; use WikiExporter::schemaVersion() instead
487 * @return string
488 */
489 function schemaVersion() {
490 wfDeprecated( __METHOD__, '1.20' );
491 return WikiExporter::schemaVersion();
492 }
493
494 /**
495 * Opens the XML output stream's root "<mediawiki>" element.
496 * This does not include an xml directive, so is safe to include
497 * as a subelement in a larger XML stream. Namespace and XML Schema
498 * references are included.
499 *
500 * Output will be encoded in UTF-8.
501 *
502 * @return string
503 */
504 function openStream() {
505 global $wgLanguageCode;
506 $ver = WikiExporter::schemaVersion();
507 return Xml::element( 'mediawiki', array(
508 'xmlns' => "http://www.mediawiki.org/xml/export-$ver/",
509 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
510 'xsi:schemaLocation' => "http://www.mediawiki.org/xml/export-$ver/ " .
511 #TODO: how do we get a new version up there?
512 "http://www.mediawiki.org/xml/export-$ver.xsd",
513 'version' => $ver,
514 'xml:lang' => $wgLanguageCode ),
515 null ) .
516 "\n" .
517 $this->siteInfo();
518 }
519
520 /**
521 * @return string
522 */
523 function siteInfo() {
524 $info = array(
525 $this->sitename(),
526 $this->homelink(),
527 $this->generator(),
528 $this->caseSetting(),
529 $this->namespaces() );
530 return " <siteinfo>\n " .
531 implode( "\n ", $info ) .
532 "\n </siteinfo>\n";
533 }
534
535 /**
536 * @return string
537 */
538 function sitename() {
539 global $wgSitename;
540 return Xml::element( 'sitename', array(), $wgSitename );
541 }
542
543 /**
544 * @return string
545 */
546 function generator() {
547 global $wgVersion;
548 return Xml::element( 'generator', array(), "MediaWiki $wgVersion" );
549 }
550
551 /**
552 * @return string
553 */
554 function homelink() {
555 return Xml::element( 'base', array(), Title::newMainPage()->getCanonicalURL() );
556 }
557
558 /**
559 * @return string
560 */
561 function caseSetting() {
562 global $wgCapitalLinks;
563 // "case-insensitive" option is reserved for future
564 $sensitivity = $wgCapitalLinks ? 'first-letter' : 'case-sensitive';
565 return Xml::element( 'case', array(), $sensitivity );
566 }
567
568 /**
569 * @return string
570 */
571 function namespaces() {
572 global $wgContLang;
573 $spaces = "<namespaces>\n";
574 foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
575 $spaces .= ' ' .
576 Xml::element( 'namespace',
577 array(
578 'key' => $ns,
579 'case' => MWNamespace::isCapitalized( $ns ) ? 'first-letter' : 'case-sensitive',
580 ), $title ) . "\n";
581 }
582 $spaces .= " </namespaces>";
583 return $spaces;
584 }
585
586 /**
587 * Closes the output stream with the closing root element.
588 * Call when finished dumping things.
589 *
590 * @return string
591 */
592 function closeStream() {
593 return "</mediawiki>\n";
594 }
595
596 /**
597 * Opens a "<page>" section on the output stream, with data
598 * from the given database row.
599 *
600 * @param object $row
601 * @return string
602 */
603 public function openPage( $row ) {
604 $out = " <page>\n";
605 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
606 $out .= ' ' . Xml::elementClean( 'title', array(), self::canonicalTitle( $title ) ) . "\n";
607 $out .= ' ' . Xml::element( 'ns', array(), strval( $row->page_namespace ) ) . "\n";
608 $out .= ' ' . Xml::element( 'id', array(), strval( $row->page_id ) ) . "\n";
609 if ( $row->page_is_redirect ) {
610 $page = WikiPage::factory( $title );
611 $redirect = $page->getRedirectTarget();
612 if ( $redirect instanceof Title && $redirect->isValidRedirectTarget() ) {
613 $out .= ' ';
614 $out .= Xml::element( 'redirect', array( 'title' => self::canonicalTitle( $redirect ) ) );
615 $out .= "\n";
616 }
617 }
618
619 if ( $row->page_restrictions != '' ) {
620 $out .= ' ' . Xml::element( 'restrictions', array(),
621 strval( $row->page_restrictions ) ) . "\n";
622 }
623
624 wfRunHooks( 'XmlDumpWriterOpenPage', array( $this, &$out, $row, $title ) );
625
626 return $out;
627 }
628
629 /**
630 * Closes a "<page>" section on the output stream.
631 *
632 * @access private
633 * @return string
634 */
635 function closePage() {
636 return " </page>\n";
637 }
638
639 /**
640 * Dumps a "<revision>" section on the output stream, with
641 * data filled in from the given database row.
642 *
643 * @param object $row
644 * @return string
645 * @access private
646 */
647 function writeRevision( $row ) {
648 wfProfileIn( __METHOD__ );
649
650 $out = " <revision>\n";
651 $out .= " " . Xml::element( 'id', null, strval( $row->rev_id ) ) . "\n";
652 if ( isset( $row->rev_parent_id ) && $row->rev_parent_id ) {
653 $out .= " " . Xml::element( 'parentid', null, strval( $row->rev_parent_id ) ) . "\n";
654 }
655
656 $out .= $this->writeTimestamp( $row->rev_timestamp );
657
658 if ( isset( $row->rev_deleted ) && ( $row->rev_deleted & Revision::DELETED_USER ) ) {
659 $out .= " " . Xml::element( 'contributor', array( 'deleted' => 'deleted' ) ) . "\n";
660 } else {
661 $out .= $this->writeContributor( $row->rev_user, $row->rev_user_text );
662 }
663
664 if ( isset( $row->rev_minor_edit ) && $row->rev_minor_edit ) {
665 $out .= " <minor/>\n";
666 }
667 if ( isset( $row->rev_deleted ) && ( $row->rev_deleted & Revision::DELETED_COMMENT ) ) {
668 $out .= " " . Xml::element( 'comment', array( 'deleted' => 'deleted' ) ) . "\n";
669 } elseif ( $row->rev_comment != '' ) {
670 $out .= " " . Xml::elementClean( 'comment', array(), strval( $row->rev_comment ) ) . "\n";
671 }
672
673 $text = '';
674 if ( isset( $row->rev_deleted ) && ( $row->rev_deleted & Revision::DELETED_TEXT ) ) {
675 $out .= " " . Xml::element( 'text', array( 'deleted' => 'deleted' ) ) . "\n";
676 } elseif ( isset( $row->old_text ) ) {
677 // Raw text from the database may have invalid chars
678 $text = strval( Revision::getRevisionText( $row ) );
679 $out .= " " . Xml::elementClean( 'text',
680 array( 'xml:space' => 'preserve', 'bytes' => intval( $row->rev_len ) ),
681 strval( $text ) ) . "\n";
682 } else {
683 // Stub output
684 $out .= " " . Xml::element( 'text',
685 array( 'id' => $row->rev_text_id, 'bytes' => intval( $row->rev_len ) ),
686 "" ) . "\n";
687 }
688
689 if ( isset( $row->rev_sha1 )
690 && $row->rev_sha1
691 && !( $row->rev_deleted & Revision::DELETED_TEXT )
692 ) {
693 $out .= " " . Xml::element( 'sha1', null, strval( $row->rev_sha1 ) ) . "\n";
694 } else {
695 $out .= " <sha1/>\n";
696 }
697
698 if ( isset( $row->rev_content_model ) && !is_null( $row->rev_content_model ) ) {
699 $content_model = strval( $row->rev_content_model );
700 } else {
701 // probably using $wgContentHandlerUseDB = false;
702 // @todo test!
703 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
704 $content_model = ContentHandler::getDefaultModelFor( $title );
705 }
706
707 $out .= " " . Xml::element( 'model', null, strval( $content_model ) ) . "\n";
708
709 if ( isset( $row->rev_content_format ) && !is_null( $row->rev_content_format ) ) {
710 $content_format = strval( $row->rev_content_format );
711 } else {
712 // probably using $wgContentHandlerUseDB = false;
713 // @todo test!
714 $content_handler = ContentHandler::getForModelID( $content_model );
715 $content_format = $content_handler->getDefaultFormat();
716 }
717
718 $out .= " " . Xml::element( 'format', null, strval( $content_format ) ) . "\n";
719
720 wfRunHooks( 'XmlDumpWriterWriteRevision', array( &$this, &$out, $row, $text ) );
721
722 $out .= " </revision>\n";
723
724 wfProfileOut( __METHOD__ );
725 return $out;
726 }
727
728 /**
729 * Dumps a "<logitem>" section on the output stream, with
730 * data filled in from the given database row.
731 *
732 * @param object $row
733 * @return string
734 * @access private
735 */
736 function writeLogItem( $row ) {
737 wfProfileIn( __METHOD__ );
738
739 $out = " <logitem>\n";
740 $out .= " " . Xml::element( 'id', null, strval( $row->log_id ) ) . "\n";
741
742 $out .= $this->writeTimestamp( $row->log_timestamp, " " );
743
744 if ( $row->log_deleted & LogPage::DELETED_USER ) {
745 $out .= " " . Xml::element( 'contributor', array( 'deleted' => 'deleted' ) ) . "\n";
746 } else {
747 $out .= $this->writeContributor( $row->log_user, $row->user_name, " " );
748 }
749
750 if ( $row->log_deleted & LogPage::DELETED_COMMENT ) {
751 $out .= " " . Xml::element( 'comment', array( 'deleted' => 'deleted' ) ) . "\n";
752 } elseif ( $row->log_comment != '' ) {
753 $out .= " " . Xml::elementClean( 'comment', null, strval( $row->log_comment ) ) . "\n";
754 }
755
756 $out .= " " . Xml::element( 'type', null, strval( $row->log_type ) ) . "\n";
757 $out .= " " . Xml::element( 'action', null, strval( $row->log_action ) ) . "\n";
758
759 if ( $row->log_deleted & LogPage::DELETED_ACTION ) {
760 $out .= " " . Xml::element( 'text', array( 'deleted' => 'deleted' ) ) . "\n";
761 } else {
762 $title = Title::makeTitle( $row->log_namespace, $row->log_title );
763 $out .= " " . Xml::elementClean( 'logtitle', null, self::canonicalTitle( $title ) ) . "\n";
764 $out .= " " . Xml::elementClean( 'params',
765 array( 'xml:space' => 'preserve' ),
766 strval( $row->log_params ) ) . "\n";
767 }
768
769 $out .= " </logitem>\n";
770
771 wfProfileOut( __METHOD__ );
772 return $out;
773 }
774
775 /**
776 * @param string $timestamp
777 * @param string $indent Default to six spaces
778 * @return string
779 */
780 function writeTimestamp( $timestamp, $indent = " " ) {
781 $ts = wfTimestamp( TS_ISO_8601, $timestamp );
782 return $indent . Xml::element( 'timestamp', null, $ts ) . "\n";
783 }
784
785 /**
786 * @param int $id
787 * @param string $text
788 * @param string $indent Default to six spaces
789 * @return string
790 */
791 function writeContributor( $id, $text, $indent = " " ) {
792 $out = $indent . "<contributor>\n";
793 if ( $id || !IP::isValid( $text ) ) {
794 $out .= $indent . " " . Xml::elementClean( 'username', null, strval( $text ) ) . "\n";
795 $out .= $indent . " " . Xml::element( 'id', null, strval( $id ) ) . "\n";
796 } else {
797 $out .= $indent . " " . Xml::elementClean( 'ip', null, strval( $text ) ) . "\n";
798 }
799 $out .= $indent . "</contributor>\n";
800 return $out;
801 }
802
803 /**
804 * Warning! This data is potentially inconsistent. :(
805 * @param object $row
806 * @param bool $dumpContents
807 * @return string
808 */
809 function writeUploads( $row, $dumpContents = false ) {
810 if ( $row->page_namespace == NS_FILE ) {
811 $img = wfLocalFile( $row->page_title );
812 if ( $img && $img->exists() ) {
813 $out = '';
814 foreach ( array_reverse( $img->getHistory() ) as $ver ) {
815 $out .= $this->writeUpload( $ver, $dumpContents );
816 }
817 $out .= $this->writeUpload( $img, $dumpContents );
818 return $out;
819 }
820 }
821 return '';
822 }
823
824 /**
825 * @param File $file
826 * @param bool $dumpContents
827 * @return string
828 */
829 function writeUpload( $file, $dumpContents = false ) {
830 if ( $file->isOld() ) {
831 $archiveName = " " .
832 Xml::element( 'archivename', null, $file->getArchiveName() ) . "\n";
833 } else {
834 $archiveName = '';
835 }
836 if ( $dumpContents ) {
837 $be = $file->getRepo()->getBackend();
838 # Dump file as base64
839 # Uses only XML-safe characters, so does not need escaping
840 # @todo Too bad this loads the contents into memory (script might swap)
841 $contents = ' <contents encoding="base64">' .
842 chunk_split( base64_encode(
843 $be->getFileContents( array( 'src' => $file->getPath() ) ) ) ) .
844 " </contents>\n";
845 } else {
846 $contents = '';
847 }
848 if ( $file->isDeleted( File::DELETED_COMMENT ) ) {
849 $comment = Xml::element( 'comment', array( 'deleted' => 'deleted' ) );
850 } else {
851 $comment = Xml::elementClean( 'comment', null, $file->getDescription() );
852 }
853 return " <upload>\n" .
854 $this->writeTimestamp( $file->getTimestamp() ) .
855 $this->writeContributor( $file->getUser( 'id' ), $file->getUser( 'text' ) ) .
856 " " . $comment . "\n" .
857 " " . Xml::element( 'filename', null, $file->getName() ) . "\n" .
858 $archiveName .
859 " " . Xml::element( 'src', null, $file->getCanonicalURL() ) . "\n" .
860 " " . Xml::element( 'size', null, $file->getSize() ) . "\n" .
861 " " . Xml::element( 'sha1base36', null, $file->getSha1() ) . "\n" .
862 " " . Xml::element( 'rel', null, $file->getRel() ) . "\n" .
863 $contents .
864 " </upload>\n";
865 }
866
867 /**
868 * Return prefixed text form of title, but using the content language's
869 * canonical namespace. This skips any special-casing such as gendered
870 * user namespaces -- which while useful, are not yet listed in the
871 * XML "<siteinfo>" data so are unsafe in export.
872 *
873 * @param Title $title
874 * @return string
875 * @since 1.18
876 */
877 public static function canonicalTitle( Title $title ) {
878 if ( $title->isExternal() ) {
879 return $title->getPrefixedText();
880 }
881
882 global $wgContLang;
883 $prefix = str_replace( '_', ' ', $wgContLang->getNsText( $title->getNamespace() ) );
884
885 if ( $prefix !== '' ) {
886 $prefix .= ':';
887 }
888
889 return $prefix . $title->getText();
890 }
891 }
892
893 /**
894 * Base class for output stream; prints to stdout or buffer or wherever.
895 * @ingroup Dump
896 */
897 class DumpOutput {
898
899 /**
900 * @param string $string
901 */
902 function writeOpenStream( $string ) {
903 $this->write( $string );
904 }
905
906 /**
907 * @param string $string
908 */
909 function writeCloseStream( $string ) {
910 $this->write( $string );
911 }
912
913 /**
914 * @param object $page
915 * @param string $string
916 */
917 function writeOpenPage( $page, $string ) {
918 $this->write( $string );
919 }
920
921 /**
922 * @param string $string
923 */
924 function writeClosePage( $string ) {
925 $this->write( $string );
926 }
927
928 /**
929 * @param object $rev
930 * @param string $string
931 */
932 function writeRevision( $rev, $string ) {
933 $this->write( $string );
934 }
935
936 /**
937 * @param object $rev
938 * @param string $string
939 */
940 function writeLogItem( $rev, $string ) {
941 $this->write( $string );
942 }
943
944 /**
945 * Override to write to a different stream type.
946 * @param string $string
947 * @return bool
948 */
949 function write( $string ) {
950 print $string;
951 }
952
953 /**
954 * Close the old file, move it to a specified name,
955 * and reopen new file with the old name. Use this
956 * for writing out a file in multiple pieces
957 * at specified checkpoints (e.g. every n hours).
958 * @param string|array $newname File name. May be a string or an array with one element
959 */
960 function closeRenameAndReopen( $newname ) {
961 }
962
963 /**
964 * Close the old file, and move it to a specified name.
965 * Use this for the last piece of a file written out
966 * at specified checkpoints (e.g. every n hours).
967 * @param string|array $newname File name. May be a string or an array with one element
968 * @param bool $open If true, a new file with the old filename will be opened
969 * again for writing (default: false)
970 */
971 function closeAndRename( $newname, $open = false ) {
972 }
973
974 /**
975 * Returns the name of the file or files which are
976 * being written to, if there are any.
977 * @return null
978 */
979 function getFilenames() {
980 return null;
981 }
982 }
983
984 /**
985 * Stream outputter to send data to a file.
986 * @ingroup Dump
987 */
988 class DumpFileOutput extends DumpOutput {
989 protected $handle = false, $filename;
990
991 /**
992 * @param string $file
993 */
994 function __construct( $file ) {
995 $this->handle = fopen( $file, "wt" );
996 $this->filename = $file;
997 }
998
999 /**
1000 * @param string $string
1001 */
1002 function writeCloseStream( $string ) {
1003 parent::writeCloseStream( $string );
1004 if ( $this->handle ) {
1005 fclose( $this->handle );
1006 $this->handle = false;
1007 }
1008 }
1009
1010 /**
1011 * @param string $string
1012 */
1013 function write( $string ) {
1014 fputs( $this->handle, $string );
1015 }
1016
1017 /**
1018 * @param string $newname
1019 */
1020 function closeRenameAndReopen( $newname ) {
1021 $this->closeAndRename( $newname, true );
1022 }
1023
1024 /**
1025 * @param string $newname
1026 * @throws MWException
1027 */
1028 function renameOrException( $newname ) {
1029 if ( !rename( $this->filename, $newname ) ) {
1030 throw new MWException( __METHOD__ . ": rename of file {$this->filename} to $newname failed\n" );
1031 }
1032 }
1033
1034 /**
1035 * @param array $newname
1036 * @return string
1037 * @throws MWException
1038 */
1039 function checkRenameArgCount( $newname ) {
1040 if ( is_array( $newname ) ) {
1041 if ( count( $newname ) > 1 ) {
1042 throw new MWException( __METHOD__ . ": passed multiple arguments for rename of single file\n" );
1043 } else {
1044 $newname = $newname[0];
1045 }
1046 }
1047 return $newname;
1048 }
1049
1050 /**
1051 * @param string $newname
1052 * @param bool $open
1053 */
1054 function closeAndRename( $newname, $open = false ) {
1055 $newname = $this->checkRenameArgCount( $newname );
1056 if ( $newname ) {
1057 if ( $this->handle ) {
1058 fclose( $this->handle );
1059 $this->handle = false;
1060 }
1061 $this->renameOrException( $newname );
1062 if ( $open ) {
1063 $this->handle = fopen( $this->filename, "wt" );
1064 }
1065 }
1066 }
1067
1068 /**
1069 * @return string|null
1070 */
1071 function getFilenames() {
1072 return $this->filename;
1073 }
1074 }
1075
1076 /**
1077 * Stream outputter to send data to a file via some filter program.
1078 * Even if compression is available in a library, using a separate
1079 * program can allow us to make use of a multi-processor system.
1080 * @ingroup Dump
1081 */
1082 class DumpPipeOutput extends DumpFileOutput {
1083 protected $command, $filename;
1084 protected $procOpenResource = false;
1085
1086 /**
1087 * @param string $command
1088 * @param string $file
1089 */
1090 function __construct( $command, $file = null ) {
1091 if ( !is_null( $file ) ) {
1092 $command .= " > " . wfEscapeShellArg( $file );
1093 }
1094
1095 $this->startCommand( $command );
1096 $this->command = $command;
1097 $this->filename = $file;
1098 }
1099
1100 /**
1101 * @param string $string
1102 */
1103 function writeCloseStream( $string ) {
1104 parent::writeCloseStream( $string );
1105 if ( $this->procOpenResource ) {
1106 proc_close( $this->procOpenResource );
1107 $this->procOpenResource = false;
1108 }
1109 }
1110
1111 /**
1112 * @param string $command
1113 */
1114 function startCommand( $command ) {
1115 $spec = array(
1116 0 => array( "pipe", "r" ),
1117 );
1118 $pipes = array();
1119 $this->procOpenResource = proc_open( $command, $spec, $pipes );
1120 $this->handle = $pipes[0];
1121 }
1122
1123 /**
1124 * @param string $newname
1125 */
1126 function closeRenameAndReopen( $newname ) {
1127 $this->closeAndRename( $newname, true );
1128 }
1129
1130 /**
1131 * @param string $newname
1132 * @param bool $open
1133 */
1134 function closeAndRename( $newname, $open = false ) {
1135 $newname = $this->checkRenameArgCount( $newname );
1136 if ( $newname ) {
1137 if ( $this->handle ) {
1138 fclose( $this->handle );
1139 $this->handle = false;
1140 }
1141 if ( $this->procOpenResource ) {
1142 proc_close( $this->procOpenResource );
1143 $this->procOpenResource = false;
1144 }
1145 $this->renameOrException( $newname );
1146 if ( $open ) {
1147 $command = $this->command;
1148 $command .= " > " . wfEscapeShellArg( $this->filename );
1149 $this->startCommand( $command );
1150 }
1151 }
1152 }
1153 }
1154
1155 /**
1156 * Sends dump output via the gzip compressor.
1157 * @ingroup Dump
1158 */
1159 class DumpGZipOutput extends DumpPipeOutput {
1160 /**
1161 * @param string $file
1162 */
1163 function __construct( $file ) {
1164 parent::__construct( "gzip", $file );
1165 }
1166 }
1167
1168 /**
1169 * Sends dump output via the bgzip2 compressor.
1170 * @ingroup Dump
1171 */
1172 class DumpBZip2Output extends DumpPipeOutput {
1173 /**
1174 * @param string $file
1175 */
1176 function __construct( $file ) {
1177 parent::__construct( "bzip2", $file );
1178 }
1179 }
1180
1181 /**
1182 * Sends dump output via the p7zip compressor.
1183 * @ingroup Dump
1184 */
1185 class Dump7ZipOutput extends DumpPipeOutput {
1186 /**
1187 * @param string $file
1188 */
1189 function __construct( $file ) {
1190 $command = $this->setup7zCommand( $file );
1191 parent::__construct( $command );
1192 $this->filename = $file;
1193 }
1194
1195 /**
1196 * @param string $file
1197 * @return string
1198 */
1199 function setup7zCommand( $file ) {
1200 $command = "7za a -bd -si " . wfEscapeShellArg( $file );
1201 // Suppress annoying useless crap from p7zip
1202 // Unfortunately this could suppress real error messages too
1203 $command .= ' >' . wfGetNull() . ' 2>&1';
1204 return $command;
1205 }
1206
1207 /**
1208 * @param string $newname
1209 * @param bool $open
1210 */
1211 function closeAndRename( $newname, $open = false ) {
1212 $newname = $this->checkRenameArgCount( $newname );
1213 if ( $newname ) {
1214 fclose( $this->handle );
1215 proc_close( $this->procOpenResource );
1216 $this->renameOrException( $newname );
1217 if ( $open ) {
1218 $command = $this->setup7zCommand( $this->filename );
1219 $this->startCommand( $command );
1220 }
1221 }
1222 }
1223 }
1224
1225 /**
1226 * Dump output filter class.
1227 * This just does output filtering and streaming; XML formatting is done
1228 * higher up, so be careful in what you do.
1229 * @ingroup Dump
1230 */
1231 class DumpFilter {
1232 /**
1233 * @var DumpOutput
1234 * FIXME will need to be made protected whenever legacy code
1235 * is updated.
1236 */
1237 public $sink;
1238
1239 /**
1240 * @var bool
1241 */
1242 protected $sendingThisPage;
1243
1244 /**
1245 * @param DumpOutput $sink
1246 */
1247 function __construct( &$sink ) {
1248 $this->sink =& $sink;
1249 }
1250
1251 /**
1252 * @param string $string
1253 */
1254 function writeOpenStream( $string ) {
1255 $this->sink->writeOpenStream( $string );
1256 }
1257
1258 /**
1259 * @param string $string
1260 */
1261 function writeCloseStream( $string ) {
1262 $this->sink->writeCloseStream( $string );
1263 }
1264
1265 /**
1266 * @param object $page
1267 * @param string $string
1268 */
1269 function writeOpenPage( $page, $string ) {
1270 $this->sendingThisPage = $this->pass( $page, $string );
1271 if ( $this->sendingThisPage ) {
1272 $this->sink->writeOpenPage( $page, $string );
1273 }
1274 }
1275
1276 /**
1277 * @param string $string
1278 */
1279 function writeClosePage( $string ) {
1280 if ( $this->sendingThisPage ) {
1281 $this->sink->writeClosePage( $string );
1282 $this->sendingThisPage = false;
1283 }
1284 }
1285
1286 /**
1287 * @param object $rev
1288 * @param string $string
1289 */
1290 function writeRevision( $rev, $string ) {
1291 if ( $this->sendingThisPage ) {
1292 $this->sink->writeRevision( $rev, $string );
1293 }
1294 }
1295
1296 /**
1297 * @param object $rev
1298 * @param string $string
1299 */
1300 function writeLogItem( $rev, $string ) {
1301 $this->sink->writeRevision( $rev, $string );
1302 }
1303
1304 /**
1305 * @param string $newname
1306 */
1307 function closeRenameAndReopen( $newname ) {
1308 $this->sink->closeRenameAndReopen( $newname );
1309 }
1310
1311 /**
1312 * @param string $newname
1313 * @param bool $open
1314 */
1315 function closeAndRename( $newname, $open = false ) {
1316 $this->sink->closeAndRename( $newname, $open );
1317 }
1318
1319 /**
1320 * @return array
1321 */
1322 function getFilenames() {
1323 return $this->sink->getFilenames();
1324 }
1325
1326 /**
1327 * Override for page-based filter types.
1328 * @param object $page
1329 * @return bool
1330 */
1331 function pass( $page ) {
1332 return true;
1333 }
1334 }
1335
1336 /**
1337 * Simple dump output filter to exclude all talk pages.
1338 * @ingroup Dump
1339 */
1340 class DumpNotalkFilter extends DumpFilter {
1341 /**
1342 * @param object $page
1343 * @return bool
1344 */
1345 function pass( $page ) {
1346 return !MWNamespace::isTalk( $page->page_namespace );
1347 }
1348 }
1349
1350 /**
1351 * Dump output filter to include or exclude pages in a given set of namespaces.
1352 * @ingroup Dump
1353 */
1354 class DumpNamespaceFilter extends DumpFilter {
1355 /** @var bool */
1356 protected $invert = false;
1357
1358 /** @var array */
1359 protected $namespaces = array();
1360
1361 /**
1362 * @param DumpOutput $sink
1363 * @param array $param
1364 * @throws MWException
1365 */
1366 function __construct( &$sink, $param ) {
1367 parent::__construct( $sink );
1368
1369 $constants = array(
1370 "NS_MAIN" => NS_MAIN,
1371 "NS_TALK" => NS_TALK,
1372 "NS_USER" => NS_USER,
1373 "NS_USER_TALK" => NS_USER_TALK,
1374 "NS_PROJECT" => NS_PROJECT,
1375 "NS_PROJECT_TALK" => NS_PROJECT_TALK,
1376 "NS_FILE" => NS_FILE,
1377 "NS_FILE_TALK" => NS_FILE_TALK,
1378 "NS_IMAGE" => NS_IMAGE, // NS_IMAGE is an alias for NS_FILE
1379 "NS_IMAGE_TALK" => NS_IMAGE_TALK,
1380 "NS_MEDIAWIKI" => NS_MEDIAWIKI,
1381 "NS_MEDIAWIKI_TALK" => NS_MEDIAWIKI_TALK,
1382 "NS_TEMPLATE" => NS_TEMPLATE,
1383 "NS_TEMPLATE_TALK" => NS_TEMPLATE_TALK,
1384 "NS_HELP" => NS_HELP,
1385 "NS_HELP_TALK" => NS_HELP_TALK,
1386 "NS_CATEGORY" => NS_CATEGORY,
1387 "NS_CATEGORY_TALK" => NS_CATEGORY_TALK );
1388
1389 if ( $param { 0 } == '!' ) {
1390 $this->invert = true;
1391 $param = substr( $param, 1 );
1392 }
1393
1394 foreach ( explode( ',', $param ) as $key ) {
1395 $key = trim( $key );
1396 if ( isset( $constants[$key] ) ) {
1397 $ns = $constants[$key];
1398 $this->namespaces[$ns] = true;
1399 } elseif ( is_numeric( $key ) ) {
1400 $ns = intval( $key );
1401 $this->namespaces[$ns] = true;
1402 } else {
1403 throw new MWException( "Unrecognized namespace key '$key'\n" );
1404 }
1405 }
1406 }
1407
1408 /**
1409 * @param object $page
1410 * @return bool
1411 */
1412 function pass( $page ) {
1413 $match = isset( $this->namespaces[$page->page_namespace] );
1414 return $this->invert xor $match;
1415 }
1416 }
1417
1418 /**
1419 * Dump output filter to include only the last revision in each page sequence.
1420 * @ingroup Dump
1421 */
1422 class DumpLatestFilter extends DumpFilter {
1423 protected $page;
1424
1425 protected $pageString;
1426
1427 protected $rev;
1428
1429 protected $revString;
1430
1431 /**
1432 * @param object $page
1433 * @param string $string
1434 */
1435 function writeOpenPage( $page, $string ) {
1436 $this->page = $page;
1437 $this->pageString = $string;
1438 }
1439
1440 /**
1441 * @param string $string
1442 */
1443 function writeClosePage( $string ) {
1444 if ( $this->rev ) {
1445 $this->sink->writeOpenPage( $this->page, $this->pageString );
1446 $this->sink->writeRevision( $this->rev, $this->revString );
1447 $this->sink->writeClosePage( $string );
1448 }
1449 $this->rev = null;
1450 $this->revString = null;
1451 $this->page = null;
1452 $this->pageString = null;
1453 }
1454
1455 /**
1456 * @param object $rev
1457 * @param string $string
1458 */
1459 function writeRevision( $rev, $string ) {
1460 if ( $rev->rev_id == $this->page->page_latest ) {
1461 $this->rev = $rev;
1462 $this->revString = $string;
1463 }
1464 }
1465 }
1466
1467 /**
1468 * Base class for output stream; prints to stdout or buffer or wherever.
1469 * @ingroup Dump
1470 */
1471 class DumpMultiWriter {
1472
1473 /**
1474 * @param array $sinks
1475 */
1476 function __construct( $sinks ) {
1477 $this->sinks = $sinks;
1478 $this->count = count( $sinks );
1479 }
1480
1481 /**
1482 * @param string $string
1483 */
1484 function writeOpenStream( $string ) {
1485 for ( $i = 0; $i < $this->count; $i++ ) {
1486 $this->sinks[$i]->writeOpenStream( $string );
1487 }
1488 }
1489
1490 /**
1491 * @param string $string
1492 */
1493 function writeCloseStream( $string ) {
1494 for ( $i = 0; $i < $this->count; $i++ ) {
1495 $this->sinks[$i]->writeCloseStream( $string );
1496 }
1497 }
1498
1499 /**
1500 * @param object $page
1501 * @param string $string
1502 */
1503 function writeOpenPage( $page, $string ) {
1504 for ( $i = 0; $i < $this->count; $i++ ) {
1505 $this->sinks[$i]->writeOpenPage( $page, $string );
1506 }
1507 }
1508
1509 /**
1510 * @param string $string
1511 */
1512 function writeClosePage( $string ) {
1513 for ( $i = 0; $i < $this->count; $i++ ) {
1514 $this->sinks[$i]->writeClosePage( $string );
1515 }
1516 }
1517
1518 /**
1519 * @param object $rev
1520 * @param string $string
1521 */
1522 function writeRevision( $rev, $string ) {
1523 for ( $i = 0; $i < $this->count; $i++ ) {
1524 $this->sinks[$i]->writeRevision( $rev, $string );
1525 }
1526 }
1527
1528 /**
1529 * @param array $newnames
1530 */
1531 function closeRenameAndReopen( $newnames ) {
1532 $this->closeAndRename( $newnames, true );
1533 }
1534
1535 /**
1536 * @param array $newnames
1537 * @param bool $open
1538 */
1539 function closeAndRename( $newnames, $open = false ) {
1540 for ( $i = 0; $i < $this->count; $i++ ) {
1541 $this->sinks[$i]->closeAndRename( $newnames[$i], $open );
1542 }
1543 }
1544
1545 /**
1546 * @return array
1547 */
1548 function getFilenames() {
1549 $filenames = array();
1550 for ( $i = 0; $i < $this->count; $i++ ) {
1551 $filenames[] = $this->sinks[$i]->getFilenames();
1552 }
1553 return $filenames;
1554 }
1555 }
1556
1557 /**
1558 * @param string $string
1559 * @return string
1560 * @todo FIXME: Only used in OAI extension. Move over there.
1561 */
1562 function xmlsafe( $string ) {
1563 wfProfileIn( __FUNCTION__ );
1564
1565 /**
1566 * The page may contain old data which has not been properly normalized.
1567 * Invalid UTF-8 sequences or forbidden control characters will make our
1568 * XML output invalid, so be sure to strip them out.
1569 */
1570 $string = UtfNormal::cleanUp( $string );
1571
1572 $string = htmlspecialchars( $string );
1573 wfProfileOut( __FUNCTION__ );
1574 return $string;
1575 }