61229e0fbbb83a789d69d0c074bb77b57a47f40d
[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 function setOutputSink( &$sink ) {
76 $this->sink =& $sink;
77 }
78
79 function openStream() {
80 $output = $this->writer->openStream();
81 $this->sink->writeOpenStream( $output );
82 }
83
84 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 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 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 function allLogs() {
114 return $this->dumpFrom( '' );
115 }
116
117 function logsByRange( $start, $end ) {
118 $condition = 'log_id >= ' . intval( $start );
119 if( $end ) {
120 $condition .= ' AND log_id < ' . intval( $end );
121 }
122 return $this->dumpFrom( $condition );
123 }
124
125 /**
126 * @param $title Title
127 */
128 function pageByTitle( $title ) {
129 return $this->dumpFrom(
130 'page_namespace=' . $title->getNamespace() .
131 ' AND page_title=' . $this->db->addQuotes( $title->getDBkey() ) );
132 }
133
134 function pageByName( $name ) {
135 $title = Title::newFromText( $name );
136 if( is_null( $title ) ) {
137 return new WikiError( "Can't export invalid title" );
138 } else {
139 return $this->pageByTitle( $title );
140 }
141 }
142
143 function pagesByName( $names ) {
144 foreach( $names as $name ) {
145 $this->pageByName( $name );
146 }
147 }
148
149
150 // -------------------- private implementation below --------------------
151
152 # Generates the distinct list of authors of an article
153 # Not called by default (depends on $this->list_authors)
154 # Can be set by Special:Export when not exporting whole history
155 function do_list_authors ( $page , $revision , $cond ) {
156 $fname = "do_list_authors" ;
157 wfProfileIn( $fname );
158 $this->author_list = "<contributors>";
159 //rev_deleted
160 $nothidden = '(rev_deleted & '.Revision::DELETED_USER.') = 0';
161
162 $sql = "SELECT DISTINCT rev_user_text,rev_user FROM {$page},{$revision} WHERE page_id=rev_page AND $nothidden AND " . $cond ;
163 $result = $this->db->query( $sql, $fname );
164 $resultset = $this->db->resultObject( $result );
165 while( $row = $resultset->fetchObject() ) {
166 $this->author_list .= "<contributor>" .
167 "<username>" .
168 htmlentities( $row->rev_user_text ) .
169 "</username>" .
170 "<id>" .
171 $row->rev_user .
172 "</id>" .
173 "</contributor>";
174 }
175 wfProfileOut( $fname );
176 $this->author_list .= "</contributors>";
177 }
178
179 function dumpFrom( $cond = '' ) {
180 $fname = 'WikiExporter::dumpFrom';
181 wfProfileIn( $fname );
182
183 # For logs dumps...
184 if( $this->history & self::LOGS ) {
185 $where = array( 'user_id = log_user' );
186 # Hide private logs
187 $where[] = LogEventsList::getExcludeClause( $this->db );
188 if( $cond ) $where[] = $cond;
189 $result = $this->db->select( array('logging','user'),
190 '*',
191 $where,
192 $fname,
193 array( 'ORDER BY' => 'log_id')
194 );
195 $wrapper = $this->db->resultObject( $result );
196 $this->outputLogStream( $wrapper );
197 wfProfileOut( $fname );
198 return;
199 }
200 # For page dumps...
201 $page = $this->db->tableName( 'page' );
202 $revision = $this->db->tableName( 'revision' );
203 $text = $this->db->tableName( 'text' );
204
205 $order = 'ORDER BY page_id';
206 $limit = '';
207
208 if( $this->history == WikiExporter::FULL ) {
209 $join = 'page_id=rev_page';
210 } elseif( $this->history == WikiExporter::CURRENT ) {
211 if ( $this->list_authors && $cond != '' ) { // List authors, if so desired
212 $this->do_list_authors ( $page , $revision , $cond );
213 }
214 $join = 'page_id=rev_page AND page_latest=rev_id';
215 } elseif ( is_array( $this->history ) ) {
216 $join = 'page_id=rev_page';
217 if ( $this->history['dir'] == 'asc' ) {
218 $op = '>';
219 $order .= ', rev_timestamp';
220 } else {
221 $op = '<';
222 $order .= ', rev_timestamp DESC';
223 }
224 if ( !empty( $this->history['offset'] ) ) {
225 $join .= " AND rev_timestamp $op " . $this->db->addQuotes(
226 $this->db->timestamp( $this->history['offset'] ) );
227 }
228 if ( !empty( $this->history['limit'] ) ) {
229 $limitNum = intval( $this->history['limit'] );
230 if ( $limitNum > 0 ) {
231 $limit = "LIMIT $limitNum";
232 }
233 }
234 } else {
235 wfProfileOut( $fname );
236 return new WikiError( "$fname given invalid history dump type." );
237 }
238 $where = ( $cond == '' ) ? '' : "$cond AND";
239
240 if( $this->buffer == WikiExporter::STREAM ) {
241 $prev = $this->db->bufferResults( false );
242 }
243 if( $cond == '' ) {
244 // Optimization hack for full-database dump
245 $revindex = $pageindex = $this->db->useIndexClause("PRIMARY");
246 $straight = ' /*! STRAIGHT_JOIN */ ';
247 } else {
248 $pageindex = '';
249 $revindex = '';
250 $straight = '';
251 }
252 if( $this->text == WikiExporter::STUB ) {
253 $sql = "SELECT $straight * FROM
254 $page $pageindex,
255 $revision $revindex
256 WHERE $where $join
257 $order $limit";
258 } else {
259 $sql = "SELECT $straight * FROM
260 $page $pageindex,
261 $revision $revindex,
262 $text
263 WHERE $where $join AND rev_text_id=old_id
264 $order $limit";
265 }
266 $result = $this->db->query( $sql, $fname );
267 $wrapper = $this->db->resultObject( $result );
268 $this->outputPageStream( $wrapper );
269
270 if ( $this->list_authors ) {
271 $this->outputPageStream( $wrapper );
272 }
273
274 if( $this->buffer == WikiExporter::STREAM ) {
275 $this->db->bufferResults( $prev );
276 }
277
278 wfProfileOut( $fname );
279 }
280
281 /**
282 * Runs through a query result set dumping page and revision records.
283 * The result set should be sorted/grouped by page to avoid duplicate
284 * page records in the output.
285 *
286 * The result set will be freed once complete. Should be safe for
287 * streaming (non-buffered) queries, as long as it was made on a
288 * separate database connection not managed by LoadBalancer; some
289 * blob storage types will make queries to pull source data.
290 *
291 * @param $resultset ResultWrapper
292 * @access private
293 */
294 function outputPageStream( $resultset ) {
295 $last = null;
296 while( $row = $resultset->fetchObject() ) {
297 if( is_null( $last ) ||
298 $last->page_namespace != $row->page_namespace ||
299 $last->page_title != $row->page_title ) {
300 if( isset( $last ) ) {
301 $output = '';
302 if( $this->dumpUploads ) {
303 $output .= $this->writer->writeUploads( $last );
304 }
305 $output .= $this->writer->closePage();
306 $this->sink->writeClosePage( $output );
307 }
308 $output = $this->writer->openPage( $row );
309 $this->sink->writeOpenPage( $row, $output );
310 $last = $row;
311 }
312 $output = $this->writer->writeRevision( $row );
313 $this->sink->writeRevision( $row, $output );
314 }
315 if( isset( $last ) ) {
316 $output = '';
317 if( $this->dumpUploads ) {
318 $output .= $this->writer->writeUploads( $last );
319 }
320 $output .= $this->author_list;
321 $output .= $this->writer->closePage();
322 $this->sink->writeClosePage( $output );
323 }
324 $resultset->free();
325 }
326
327 function outputLogStream( $resultset ) {
328 while( $row = $resultset->fetchObject() ) {
329 $output = $this->writer->writeLogItem( $row );
330 $this->sink->writeLogItem( $row, $output );
331 }
332 $resultset->free();
333 }
334 }
335
336 /**
337 * @ingroup Dump
338 */
339 class XmlDumpWriter {
340
341 /**
342 * Returns the export schema version.
343 * @return string
344 */
345 function schemaVersion() {
346 return "0.3"; // FIXME: upgrade to 0.4 when updated XSD is ready, for the revision deletion bits
347 }
348
349 /**
350 * Opens the XML output stream's root <mediawiki> element.
351 * This does not include an xml directive, so is safe to include
352 * as a subelement in a larger XML stream. Namespace and XML Schema
353 * references are included.
354 *
355 * Output will be encoded in UTF-8.
356 *
357 * @return string
358 */
359 function openStream() {
360 global $wgContLanguageCode;
361 $ver = $this->schemaVersion();
362 return wfElement( 'mediawiki', array(
363 'xmlns' => "http://www.mediawiki.org/xml/export-$ver/",
364 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
365 'xsi:schemaLocation' => "http://www.mediawiki.org/xml/export-$ver/ " .
366 "http://www.mediawiki.org/xml/export-$ver.xsd",
367 'version' => $ver,
368 'xml:lang' => $wgContLanguageCode ),
369 null ) .
370 "\n" .
371 $this->siteInfo();
372 }
373
374 function siteInfo() {
375 $info = array(
376 $this->sitename(),
377 $this->homelink(),
378 $this->generator(),
379 $this->caseSetting(),
380 $this->namespaces() );
381 return " <siteinfo>\n " .
382 implode( "\n ", $info ) .
383 "\n </siteinfo>\n";
384 }
385
386 function sitename() {
387 global $wgSitename;
388 return wfElement( 'sitename', array(), $wgSitename );
389 }
390
391 function generator() {
392 global $wgVersion;
393 return wfElement( 'generator', array(), "MediaWiki $wgVersion" );
394 }
395
396 function homelink() {
397 return wfElement( 'base', array(), Title::newMainPage()->getFullUrl() );
398 }
399
400 function caseSetting() {
401 global $wgCapitalLinks;
402 // "case-insensitive" option is reserved for future
403 $sensitivity = $wgCapitalLinks ? 'first-letter' : 'case-sensitive';
404 return wfElement( 'case', array(), $sensitivity );
405 }
406
407 function namespaces() {
408 global $wgContLang;
409 $spaces = " <namespaces>\n";
410 foreach( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
411 $spaces .= ' ' . wfElement( 'namespace', array( 'key' => $ns ), $title ) . "\n";
412 }
413 $spaces .= " </namespaces>";
414 return $spaces;
415 }
416
417 /**
418 * Closes the output stream with the closing root element.
419 * Call when finished dumping things.
420 */
421 function closeStream() {
422 return "</mediawiki>\n";
423 }
424
425
426 /**
427 * Opens a <page> section on the output stream, with data
428 * from the given database row.
429 *
430 * @param $row object
431 * @return string
432 * @access private
433 */
434 function openPage( $row ) {
435 $out = " <page>\n";
436 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
437 $out .= ' ' . wfElementClean( 'title', array(), $title->getPrefixedText() ) . "\n";
438 $out .= ' ' . wfElement( 'id', array(), strval( $row->page_id ) ) . "\n";
439 if( '' != $row->page_restrictions ) {
440 $out .= ' ' . wfElement( 'restrictions', array(),
441 strval( $row->page_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( 'title', 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 }