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