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