* Add form to RCLinked and add to sp:specialpages
[lhc/web/wiklou.git] / includes / Export.php
1 <?php
2 # Copyright (C) 2003, 2005, 2006 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20
21 /**
22 *
23 * @addtogroup SpecialPage
24 */
25 class WikiExporter {
26 var $list_authors = false ; # Return distinct author list (when not returning full history)
27 var $author_list = "" ;
28
29 var $dumpUploads = false;
30
31 const FULL = 0;
32 const CURRENT = 1;
33
34 const BUFFER = 0;
35 const STREAM = 1;
36
37 const TEXT = 0;
38 const STUB = 1;
39
40 /**
41 * If using WikiExporter::STREAM to stream a large amount of data,
42 * provide a database connection which is not managed by
43 * LoadBalancer to read from: some history blob types will
44 * make additional queries to pull source data while the
45 * main query is still running.
46 *
47 * @param Database $db
48 * @param mixed $history one of WikiExporter::FULL or WikiExporter::CURRENT, or an
49 * associative array:
50 * offset: non-inclusive offset at which to start the query
51 * limit: maximum number of rows to return
52 * dir: "asc" or "desc" timestamp order
53 * @param int $buffer one of WikiExporter::BUFFER or WikiExporter::STREAM
54 */
55 function __construct( &$db, $history = WikiExporter::CURRENT,
56 $buffer = WikiExporter::BUFFER, $text = WikiExporter::TEXT ) {
57 $this->db =& $db;
58 $this->history = $history;
59 $this->buffer = $buffer;
60 $this->writer = new XmlDumpWriter();
61 $this->sink = new DumpOutput();
62 $this->text = $text;
63 }
64
65 /**
66 * Set the DumpOutput or DumpFilter object which will receive
67 * various row objects and XML output for filtering. Filters
68 * can be chained or used as callbacks.
69 *
70 * @param mixed $callback
71 */
72 function setOutputSink( &$sink ) {
73 $this->sink =& $sink;
74 }
75
76 function openStream() {
77 $output = $this->writer->openStream();
78 $this->sink->writeOpenStream( $output );
79 }
80
81 function closeStream() {
82 $output = $this->writer->closeStream();
83 $this->sink->writeCloseStream( $output );
84 }
85
86 /**
87 * Dumps a series of page and revision records for all pages
88 * in the database, either including complete history or only
89 * the most recent version.
90 */
91 function allPages() {
92 return $this->dumpFrom( '' );
93 }
94
95 /**
96 * Dumps a series of page and revision records for those pages
97 * in the database falling within the page_id range given.
98 * @param int $start Inclusive lower limit (this id is included)
99 * @param int $end Exclusive upper limit (this id is not included)
100 * If 0, no upper limit.
101 */
102 function pagesByRange( $start, $end ) {
103 $condition = 'page_id >= ' . intval( $start );
104 if( $end ) {
105 $condition .= ' AND page_id < ' . intval( $end );
106 }
107 return $this->dumpFrom( $condition );
108 }
109
110 /**
111 * @param Title $title
112 */
113 function pageByTitle( $title ) {
114 return $this->dumpFrom(
115 'page_namespace=' . $title->getNamespace() .
116 ' AND page_title=' . $this->db->addQuotes( $title->getDBkey() ) );
117 }
118
119 function pageByName( $name ) {
120 $title = Title::newFromText( $name );
121 if( is_null( $title ) ) {
122 return new WikiError( "Can't export invalid title" );
123 } else {
124 return $this->pageByTitle( $title );
125 }
126 }
127
128 function pagesByName( $names ) {
129 foreach( $names as $name ) {
130 $this->pageByName( $name );
131 }
132 }
133
134
135 // -------------------- private implementation below --------------------
136
137 # Generates the distinct list of authors of an article
138 # Not called by default (depends on $this->list_authors)
139 # Can be set by Special:Export when not exporting whole history
140 function do_list_authors ( $page , $revision , $cond ) {
141 $fname = "do_list_authors" ;
142 wfProfileIn( $fname );
143 $this->author_list = "<contributors>";
144 //rev_deleted
145 $nothidden = '(rev_deleted & '.Revision::DELETED_USER.') = 0';
146
147 $sql = "SELECT DISTINCT rev_user_text,rev_user FROM {$page},{$revision} WHERE page_id=rev_page AND $nothidden AND " . $cond ;
148 $result = $this->db->query( $sql, $fname );
149 $resultset = $this->db->resultObject( $result );
150 while( $row = $resultset->fetchObject() ) {
151 $this->author_list .= "<contributor>" .
152 "<username>" .
153 htmlentities( $row->rev_user_text ) .
154 "</username>" .
155 "<id>" .
156 $row->rev_user .
157 "</id>" .
158 "</contributor>";
159 }
160 wfProfileOut( $fname );
161 $this->author_list .= "</contributors>";
162 }
163
164 function dumpFrom( $cond = '' ) {
165 $fname = 'WikiExporter::dumpFrom';
166 wfProfileIn( $fname );
167
168 $page = $this->db->tableName( 'page' );
169 $revision = $this->db->tableName( 'revision' );
170 $text = $this->db->tableName( 'text' );
171
172 $order = 'ORDER BY page_id';
173 $limit = '';
174
175 if( $this->history == WikiExporter::FULL ) {
176 $join = 'page_id=rev_page';
177 } elseif( $this->history == WikiExporter::CURRENT ) {
178 if ( $this->list_authors && $cond != '' ) { // List authors, if so desired
179 $this->do_list_authors ( $page , $revision , $cond );
180 }
181 $join = 'page_id=rev_page AND page_latest=rev_id';
182 } elseif ( is_array( $this->history ) ) {
183 $join = 'page_id=rev_page';
184 if ( $this->history['dir'] == 'asc' ) {
185 $op = '>';
186 $order .= ', rev_timestamp';
187 } else {
188 $op = '<';
189 $order .= ', rev_timestamp DESC';
190 }
191 if ( !empty( $this->history['offset'] ) ) {
192 $join .= " AND rev_timestamp $op " . $this->db->addQuotes(
193 $this->db->timestamp( $this->history['offset'] ) );
194 }
195 if ( !empty( $this->history['limit'] ) ) {
196 $limitNum = intval( $this->history['limit'] );
197 if ( $limitNum > 0 ) {
198 $limit = "LIMIT $limitNum";
199 }
200 }
201 } else {
202 wfProfileOut( $fname );
203 return new WikiError( "$fname given invalid history dump type." );
204 }
205 $where = ( $cond == '' ) ? '' : "$cond AND";
206
207 if( $this->buffer == WikiExporter::STREAM ) {
208 $prev = $this->db->bufferResults( false );
209 }
210 if( $cond == '' ) {
211 // Optimization hack for full-database dump
212 $revindex = $pageindex = $this->db->useIndexClause("PRIMARY");
213 $straight = ' /*! STRAIGHT_JOIN */ ';
214 } else {
215 $pageindex = '';
216 $revindex = '';
217 $straight = '';
218 }
219 if( $this->text == WikiExporter::STUB ) {
220 $sql = "SELECT $straight * FROM
221 $page $pageindex,
222 $revision $revindex
223 WHERE $where $join
224 $order $limit";
225 } else {
226 $sql = "SELECT $straight * FROM
227 $page $pageindex,
228 $revision $revindex,
229 $text
230 WHERE $where $join AND rev_text_id=old_id
231 $order $limit";
232 }
233 $result = $this->db->query( $sql, $fname );
234 $wrapper = $this->db->resultObject( $result );
235 $this->outputStream( $wrapper );
236
237 if ( $this->list_authors ) {
238 $this->outputStream( $wrapper );
239 }
240
241 if( $this->buffer == WikiExporter::STREAM ) {
242 $this->db->bufferResults( $prev );
243 }
244
245 wfProfileOut( $fname );
246 }
247
248 /**
249 * Runs through a query result set dumping page and revision records.
250 * The result set should be sorted/grouped by page to avoid duplicate
251 * page records in the output.
252 *
253 * The result set will be freed once complete. Should be safe for
254 * streaming (non-buffered) queries, as long as it was made on a
255 * separate database connection not managed by LoadBalancer; some
256 * blob storage types will make queries to pull source data.
257 *
258 * @param ResultWrapper $resultset
259 * @access private
260 */
261 function outputStream( $resultset ) {
262 $last = null;
263 while( $row = $resultset->fetchObject() ) {
264 if( is_null( $last ) ||
265 $last->page_namespace != $row->page_namespace ||
266 $last->page_title != $row->page_title ) {
267 if( isset( $last ) ) {
268 $output = '';
269 if( $this->dumpUploads ) {
270 $output .= $this->writer->writeUploads( $last );
271 }
272 $output .= $this->writer->closePage();
273 $this->sink->writeClosePage( $output );
274 }
275 $output = $this->writer->openPage( $row );
276 $this->sink->writeOpenPage( $row, $output );
277 $last = $row;
278 }
279 $output = $this->writer->writeRevision( $row );
280 $this->sink->writeRevision( $row, $output );
281 }
282 if( isset( $last ) ) {
283 $output = '';
284 if( $this->dumpUploads ) {
285 $output .= $this->writer->writeUploads( $last );
286 }
287 $output .= $this->author_list;
288 $output .= $this->writer->closePage();
289 $this->sink->writeClosePage( $output );
290 }
291 $resultset->free();
292 }
293 }
294
295 /**
296 * @addtogroup Dump
297 */
298 class XmlDumpWriter {
299
300 /**
301 * Returns the export schema version.
302 * @return string
303 */
304 function schemaVersion() {
305 return "0.3"; // FIXME: upgrade to 0.4 when updated XSD is ready, for the revision deletion bits
306 }
307
308 /**
309 * Opens the XML output stream's root <mediawiki> element.
310 * This does not include an xml directive, so is safe to include
311 * as a subelement in a larger XML stream. Namespace and XML Schema
312 * references are included.
313 *
314 * Output will be encoded in UTF-8.
315 *
316 * @return string
317 */
318 function openStream() {
319 global $wgContLanguageCode;
320 $ver = $this->schemaVersion();
321 return wfElement( 'mediawiki', array(
322 'xmlns' => "http://www.mediawiki.org/xml/export-$ver/",
323 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
324 'xsi:schemaLocation' => "http://www.mediawiki.org/xml/export-$ver/ " .
325 "http://www.mediawiki.org/xml/export-$ver.xsd",
326 'version' => $ver,
327 'xml:lang' => $wgContLanguageCode ),
328 null ) .
329 "\n" .
330 $this->siteInfo();
331 }
332
333 function siteInfo() {
334 $info = array(
335 $this->sitename(),
336 $this->homelink(),
337 $this->generator(),
338 $this->caseSetting(),
339 $this->namespaces() );
340 return " <siteinfo>\n " .
341 implode( "\n ", $info ) .
342 "\n </siteinfo>\n";
343 }
344
345 function sitename() {
346 global $wgSitename;
347 return wfElement( 'sitename', array(), $wgSitename );
348 }
349
350 function generator() {
351 global $wgVersion;
352 return wfElement( 'generator', array(), "MediaWiki $wgVersion" );
353 }
354
355 function homelink() {
356 return wfElement( 'base', array(), Title::newMainPage()->getFullUrl() );
357 }
358
359 function caseSetting() {
360 global $wgCapitalLinks;
361 // "case-insensitive" option is reserved for future
362 $sensitivity = $wgCapitalLinks ? 'first-letter' : 'case-sensitive';
363 return wfElement( 'case', array(), $sensitivity );
364 }
365
366 function namespaces() {
367 global $wgContLang;
368 $spaces = " <namespaces>\n";
369 foreach( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
370 $spaces .= ' ' . wfElement( 'namespace', array( 'key' => $ns ), $title ) . "\n";
371 }
372 $spaces .= " </namespaces>";
373 return $spaces;
374 }
375
376 /**
377 * Closes the output stream with the closing root element.
378 * Call when finished dumping things.
379 */
380 function closeStream() {
381 return "</mediawiki>\n";
382 }
383
384
385 /**
386 * Opens a <page> section on the output stream, with data
387 * from the given database row.
388 *
389 * @param object $row
390 * @return string
391 * @access private
392 */
393 function openPage( $row ) {
394 $out = " <page>\n";
395 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
396 $out .= ' ' . wfElementClean( 'title', array(), $title->getPrefixedText() ) . "\n";
397 $out .= ' ' . wfElement( 'id', array(), strval( $row->page_id ) ) . "\n";
398 if( '' != $row->page_restrictions ) {
399 $out .= ' ' . wfElement( 'restrictions', array(),
400 strval( $row->page_restrictions ) ) . "\n";
401 }
402 return $out;
403 }
404
405 /**
406 * Closes a <page> section on the output stream.
407 *
408 * @access private
409 */
410 function closePage() {
411 return " </page>\n";
412 }
413
414 /**
415 * Dumps a <revision> section on the output stream, with
416 * data filled in from the given database row.
417 *
418 * @param object $row
419 * @return string
420 * @access private
421 */
422 function writeRevision( $row ) {
423 $fname = 'WikiExporter::dumpRev';
424 wfProfileIn( $fname );
425
426 $out = " <revision>\n";
427 $out .= " " . wfElement( 'id', null, strval( $row->rev_id ) ) . "\n";
428
429 $out .= $this->writeTimestamp( $row->rev_timestamp );
430
431 if( $row->rev_deleted & Revision::DELETED_USER ) {
432 $out .= " " . wfElement( 'contributor', array( 'deleted' => 'deleted' ) ) . "\n";
433 } else {
434 $out .= $this->writeContributor( $row->rev_user, $row->rev_user_text );
435 }
436
437 if( $row->rev_minor_edit ) {
438 $out .= " <minor/>\n";
439 }
440 if( $row->rev_deleted & Revision::DELETED_COMMENT ) {
441 $out .= " " . wfElement( 'comment', array( 'deleted' => 'deleted' ) ) . "\n";
442 } elseif( $row->rev_comment != '' ) {
443 $out .= " " . wfElementClean( 'comment', null, strval( $row->rev_comment ) ) . "\n";
444 }
445
446 if( $row->rev_deleted & Revision::DELETED_TEXT ) {
447 $out .= " " . wfElement( 'text', array( 'deleted' => 'deleted' ) ) . "\n";
448 } elseif( isset( $row->old_text ) ) {
449 // Raw text from the database may have invalid chars
450 $text = strval( Revision::getRevisionText( $row ) );
451 $out .= " " . wfElementClean( 'text',
452 array( 'xml:space' => 'preserve' ),
453 strval( $text ) ) . "\n";
454 } else {
455 // Stub output
456 $out .= " " . wfElement( 'text',
457 array( 'id' => $row->rev_text_id ),
458 "" ) . "\n";
459 }
460
461 $out .= " </revision>\n";
462
463 wfProfileOut( $fname );
464 return $out;
465 }
466
467 function writeTimestamp( $timestamp ) {
468 $ts = wfTimestamp( TS_ISO_8601, $timestamp );
469 return " " . wfElement( 'timestamp', null, $ts ) . "\n";
470 }
471
472 function writeContributor( $id, $text ) {
473 $out = " <contributor>\n";
474 if( $id ) {
475 $out .= " " . wfElementClean( 'username', null, strval( $text ) ) . "\n";
476 $out .= " " . wfElement( 'id', null, strval( $id ) ) . "\n";
477 } else {
478 $out .= " " . wfElementClean( 'ip', null, strval( $text ) ) . "\n";
479 }
480 $out .= " </contributor>\n";
481 return $out;
482 }
483
484 /**
485 * Warning! This data is potentially inconsistent. :(
486 */
487 function writeUploads( $row ) {
488 if( $row->page_namespace == NS_IMAGE ) {
489 $img = wfFindFile( $row->page_title );
490 if( $img ) {
491 $out = '';
492 foreach( array_reverse( $img->getHistory() ) as $ver ) {
493 $out .= $this->writeUpload( $ver );
494 }
495 $out .= $this->writeUpload( $img );
496 return $out;
497 }
498 }
499 return '';
500 }
501
502 function writeUpload( $file ) {
503 return " <upload>\n" .
504 $this->writeTimestamp( $file->getTimestamp() ) .
505 $this->writeContributor( $file->getUser( 'id' ), $file->getUser( 'text' ) ) .
506 " " . wfElementClean( 'comment', null, $file->getDescription() ) . "\n" .
507 " " . wfElement( 'filename', null, $file->getName() ) . "\n" .
508 " " . wfElement( 'src', null, $file->getFullUrl() ) . "\n" .
509 " " . wfElement( 'size', null, $file->getSize() ) . "\n" .
510 " </upload>\n";
511 }
512
513 }
514
515
516 /**
517 * Base class for output stream; prints to stdout or buffer or whereever.
518 * @addtogroup Dump
519 */
520 class DumpOutput {
521 function writeOpenStream( $string ) {
522 $this->write( $string );
523 }
524
525 function writeCloseStream( $string ) {
526 $this->write( $string );
527 }
528
529 function writeOpenPage( $page, $string ) {
530 $this->write( $string );
531 }
532
533 function writeClosePage( $string ) {
534 $this->write( $string );
535 }
536
537 function writeRevision( $rev, $string ) {
538 $this->write( $string );
539 }
540
541 /**
542 * Override to write to a different stream type.
543 * @return bool
544 */
545 function write( $string ) {
546 print $string;
547 }
548 }
549
550 /**
551 * Stream outputter to send data to a file.
552 * @addtogroup Dump
553 */
554 class DumpFileOutput extends DumpOutput {
555 var $handle;
556
557 function DumpFileOutput( $file ) {
558 $this->handle = fopen( $file, "wt" );
559 }
560
561 function write( $string ) {
562 fputs( $this->handle, $string );
563 }
564 }
565
566 /**
567 * Stream outputter to send data to a file via some filter program.
568 * Even if compression is available in a library, using a separate
569 * program can allow us to make use of a multi-processor system.
570 * @addtogroup Dump
571 */
572 class DumpPipeOutput extends DumpFileOutput {
573 function DumpPipeOutput( $command, $file = null ) {
574 if( !is_null( $file ) ) {
575 $command .= " > " . wfEscapeShellArg( $file );
576 }
577 $this->handle = popen( $command, "w" );
578 }
579 }
580
581 /**
582 * Sends dump output via the gzip compressor.
583 * @addtogroup Dump
584 */
585 class DumpGZipOutput extends DumpPipeOutput {
586 function DumpGZipOutput( $file ) {
587 parent::DumpPipeOutput( "gzip", $file );
588 }
589 }
590
591 /**
592 * Sends dump output via the bgzip2 compressor.
593 * @addtogroup Dump
594 */
595 class DumpBZip2Output extends DumpPipeOutput {
596 function DumpBZip2Output( $file ) {
597 parent::DumpPipeOutput( "bzip2", $file );
598 }
599 }
600
601 /**
602 * Sends dump output via the p7zip compressor.
603 * @addtogroup Dump
604 */
605 class Dump7ZipOutput extends DumpPipeOutput {
606 function Dump7ZipOutput( $file ) {
607 $command = "7za a -bd -si " . wfEscapeShellArg( $file );
608 // Suppress annoying useless crap from p7zip
609 // Unfortunately this could suppress real error messages too
610 $command .= ' >' . wfGetNull() . ' 2>&1';
611 parent::DumpPipeOutput( $command );
612 }
613 }
614
615
616
617 /**
618 * Dump output filter class.
619 * This just does output filtering and streaming; XML formatting is done
620 * higher up, so be careful in what you do.
621 * @addtogroup Dump
622 */
623 class DumpFilter {
624 function DumpFilter( &$sink ) {
625 $this->sink =& $sink;
626 }
627
628 function writeOpenStream( $string ) {
629 $this->sink->writeOpenStream( $string );
630 }
631
632 function writeCloseStream( $string ) {
633 $this->sink->writeCloseStream( $string );
634 }
635
636 function writeOpenPage( $page, $string ) {
637 $this->sendingThisPage = $this->pass( $page, $string );
638 if( $this->sendingThisPage ) {
639 $this->sink->writeOpenPage( $page, $string );
640 }
641 }
642
643 function writeClosePage( $string ) {
644 if( $this->sendingThisPage ) {
645 $this->sink->writeClosePage( $string );
646 $this->sendingThisPage = false;
647 }
648 }
649
650 function writeRevision( $rev, $string ) {
651 if( $this->sendingThisPage ) {
652 $this->sink->writeRevision( $rev, $string );
653 }
654 }
655
656 /**
657 * Override for page-based filter types.
658 * @return bool
659 */
660 function pass( $page ) {
661 return true;
662 }
663 }
664
665 /**
666 * Simple dump output filter to exclude all talk pages.
667 * @addtogroup Dump
668 */
669 class DumpNotalkFilter extends DumpFilter {
670 function pass( $page ) {
671 return !MWNamespace::isTalk( $page->page_namespace );
672 }
673 }
674
675 /**
676 * Dump output filter to include or exclude pages in a given set of namespaces.
677 * @addtogroup Dump
678 */
679 class DumpNamespaceFilter extends DumpFilter {
680 var $invert = false;
681 var $namespaces = array();
682
683 function DumpNamespaceFilter( &$sink, $param ) {
684 parent::DumpFilter( $sink );
685
686 $constants = array(
687 "NS_MAIN" => NS_MAIN,
688 "NS_TALK" => NS_TALK,
689 "NS_USER" => NS_USER,
690 "NS_USER_TALK" => NS_USER_TALK,
691 "NS_PROJECT" => NS_PROJECT,
692 "NS_PROJECT_TALK" => NS_PROJECT_TALK,
693 "NS_IMAGE" => NS_IMAGE,
694 "NS_IMAGE_TALK" => NS_IMAGE_TALK,
695 "NS_MEDIAWIKI" => NS_MEDIAWIKI,
696 "NS_MEDIAWIKI_TALK" => NS_MEDIAWIKI_TALK,
697 "NS_TEMPLATE" => NS_TEMPLATE,
698 "NS_TEMPLATE_TALK" => NS_TEMPLATE_TALK,
699 "NS_HELP" => NS_HELP,
700 "NS_HELP_TALK" => NS_HELP_TALK,
701 "NS_CATEGORY" => NS_CATEGORY,
702 "NS_CATEGORY_TALK" => NS_CATEGORY_TALK );
703
704 if( $param{0} == '!' ) {
705 $this->invert = true;
706 $param = substr( $param, 1 );
707 }
708
709 foreach( explode( ',', $param ) as $key ) {
710 $key = trim( $key );
711 if( isset( $constants[$key] ) ) {
712 $ns = $constants[$key];
713 $this->namespaces[$ns] = true;
714 } elseif( is_numeric( $key ) ) {
715 $ns = intval( $key );
716 $this->namespaces[$ns] = true;
717 } else {
718 throw new MWException( "Unrecognized namespace key '$key'\n" );
719 }
720 }
721 }
722
723 function pass( $page ) {
724 $match = isset( $this->namespaces[$page->page_namespace] );
725 return $this->invert xor $match;
726 }
727 }
728
729
730 /**
731 * Dump output filter to include only the last revision in each page sequence.
732 * @addtogroup Dump
733 */
734 class DumpLatestFilter extends DumpFilter {
735 var $page, $pageString, $rev, $revString;
736
737 function writeOpenPage( $page, $string ) {
738 $this->page = $page;
739 $this->pageString = $string;
740 }
741
742 function writeClosePage( $string ) {
743 if( $this->rev ) {
744 $this->sink->writeOpenPage( $this->page, $this->pageString );
745 $this->sink->writeRevision( $this->rev, $this->revString );
746 $this->sink->writeClosePage( $string );
747 }
748 $this->rev = null;
749 $this->revString = null;
750 $this->page = null;
751 $this->pageString = null;
752 }
753
754 function writeRevision( $rev, $string ) {
755 if( $rev->rev_id == $this->page->page_latest ) {
756 $this->rev = $rev;
757 $this->revString = $string;
758 }
759 }
760 }
761
762 /**
763 * Base class for output stream; prints to stdout or buffer or whereever.
764 * @addtogroup Dump
765 */
766 class DumpMultiWriter {
767 function DumpMultiWriter( $sinks ) {
768 $this->sinks = $sinks;
769 $this->count = count( $sinks );
770 }
771
772 function writeOpenStream( $string ) {
773 for( $i = 0; $i < $this->count; $i++ ) {
774 $this->sinks[$i]->writeOpenStream( $string );
775 }
776 }
777
778 function writeCloseStream( $string ) {
779 for( $i = 0; $i < $this->count; $i++ ) {
780 $this->sinks[$i]->writeCloseStream( $string );
781 }
782 }
783
784 function writeOpenPage( $page, $string ) {
785 for( $i = 0; $i < $this->count; $i++ ) {
786 $this->sinks[$i]->writeOpenPage( $page, $string );
787 }
788 }
789
790 function writeClosePage( $string ) {
791 for( $i = 0; $i < $this->count; $i++ ) {
792 $this->sinks[$i]->writeClosePage( $string );
793 }
794 }
795
796 function writeRevision( $rev, $string ) {
797 for( $i = 0; $i < $this->count; $i++ ) {
798 $this->sinks[$i]->writeRevision( $rev, $string );
799 }
800 }
801 }
802
803 function xmlsafe( $string ) {
804 $fname = 'xmlsafe';
805 wfProfileIn( $fname );
806
807 /**
808 * The page may contain old data which has not been properly normalized.
809 * Invalid UTF-8 sequences or forbidden control characters will make our
810 * XML output invalid, so be sure to strip them out.
811 */
812 $string = UtfNormal::cleanUp( $string );
813
814 $string = htmlspecialchars( $string );
815 wfProfileOut( $fname );
816 return $string;
817 }
818
819