Log dump cleanup for WikiExporter::STREAM
[mediawiki.git] / includes / Export.php
blobf41cc450d3656628e687d15395eb773cc03bee09
1 <?php
2 # Copyright (C) 2003, 2005, 2006 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
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.
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.
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
20 /**
21 * @defgroup Dump Dump
24 /**
25 * @ingroup SpecialPage Dump
27 class WikiExporter {
28 var $list_authors = false ; # Return distinct author list (when not returning full history)
29 var $author_list = "" ;
31 var $dumpUploads = false;
33 const FULL = 0;
34 const CURRENT = 1;
35 const LOGS = 2;
37 const BUFFER = 0;
38 const STREAM = 1;
40 const TEXT = 0;
41 const STUB = 1;
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.
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
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;
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.
73 * @param $sink mixed
75 public function setOutputSink( &$sink ) {
76 $this->sink =& $sink;
79 public function openStream() {
80 $output = $this->writer->openStream();
81 $this->sink->writeOpenStream( $output );
84 public function closeStream() {
85 $output = $this->writer->closeStream();
86 $this->sink->writeCloseStream( $output );
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.
94 public function allPages() {
95 return $this->dumpFrom( '' );
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.
105 public function pagesByRange( $start, $end ) {
106 $condition = 'page_id >= ' . intval( $start );
107 if( $end ) {
108 $condition .= ' AND page_id < ' . intval( $end );
110 return $this->dumpFrom( $condition );
114 * @param $title Title
116 public function pageByTitle( $title ) {
117 return $this->dumpFrom(
118 'page_namespace=' . $title->getNamespace() .
119 ' AND page_title=' . $this->db->addQuotes( $title->getDBkey() ) );
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 );
131 public function pagesByName( $names ) {
132 foreach( $names as $name ) {
133 $this->pageByName( $name );
137 public function allLogs() {
138 return $this->dumpFrom( '' );
141 public function logsByRange( $start, $end ) {
142 $condition = 'log_id >= ' . intval( $start );
143 if( $end ) {
144 $condition .= ' AND log_id < ' . intval( $end );
146 return $this->dumpFrom( $condition );
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';
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>";
173 wfProfileOut( $fname );
174 $this->author_list .= "</contributors>";
177 protected function dumpFrom( $cond = '' ) {
178 $fname = 'WikiExporter::dumpFrom';
179 wfProfileIn( $fname );
181 # For logs dumps...
182 if( $this->history & self::LOGS ) {
183 if( $this->buffer == WikiExporter::STREAM ) {
184 $prev = $this->db->bufferResults( false );
186 $where = array( 'user_id = log_user' );
187 # Hide private logs
188 $hideLogs = LogEventsList::getExcludeClause( $this->db );
189 if( $hideLogs ) $where[] = $hideLogs;
190 # Add on any caller specified conditions
191 if( $cond ) $where[] = $cond;
192 # Get logging table name for logging.* clause
193 $logging = $this->db->tableName('logging');
194 $result = $this->db->select( array('logging','user'),
195 array( "{$logging}.*", 'user_name' ), // grab the user name
196 $where,
197 __METHOD__,
198 array( 'ORDER BY' => 'log_id', 'USE INDEX' => array('logging' => 'PRIMARY') )
200 $wrapper = $this->db->resultObject( $result );
201 if( $this->buffer == WikiExporter::STREAM ) {
202 $this->db->bufferResults( $prev );
204 $this->outputLogStream( $wrapper );
205 # For page dumps...
206 } else {
207 list($page,$revision,$text) = $this->db->tableNamesN('page','revision','text');
209 $order = 'ORDER BY page_id';
210 $limit = '';
212 if( $this->history == WikiExporter::FULL ) {
213 $join = 'page_id=rev_page';
214 } elseif( $this->history == WikiExporter::CURRENT ) {
215 if ( $this->list_authors && $cond != '' ) { // List authors, if so desired
216 $this->do_list_authors ( $page , $revision , $cond );
218 $join = 'page_id=rev_page AND page_latest=rev_id';
219 } elseif ( is_array( $this->history ) ) {
220 $join = 'page_id=rev_page';
221 if ( $this->history['dir'] == 'asc' ) {
222 $op = '>';
223 $order .= ', rev_timestamp';
224 } else {
225 $op = '<';
226 $order .= ', rev_timestamp DESC';
228 if ( !empty( $this->history['offset'] ) ) {
229 $join .= " AND rev_timestamp $op " . $this->db->addQuotes(
230 $this->db->timestamp( $this->history['offset'] ) );
232 if ( !empty( $this->history['limit'] ) ) {
233 $limitNum = intval( $this->history['limit'] );
234 if ( $limitNum > 0 ) {
235 $limit = "LIMIT $limitNum";
238 } else {
239 wfProfileOut( $fname );
240 return new WikiError( "$fname given invalid history dump type." );
242 $where = ( $cond == '' ) ? '' : "$cond AND";
244 if( $this->buffer == WikiExporter::STREAM ) {
245 $prev = $this->db->bufferResults( false );
247 if( $cond == '' ) {
248 // Optimization hack for full-database dump
249 $revindex = $pageindex = $this->db->useIndexClause("PRIMARY");
250 $straight = ' /*! STRAIGHT_JOIN */ ';
251 } else {
252 $pageindex = '';
253 $revindex = '';
254 $straight = '';
256 if( $this->text == WikiExporter::STUB ) {
257 $sql = "SELECT $straight * FROM
258 $page $pageindex,
259 $revision $revindex
260 WHERE $where $join
261 $order $limit";
262 } else {
263 $sql = "SELECT $straight * FROM
264 $page $pageindex,
265 $revision $revindex,
266 $text
267 WHERE $where $join AND rev_text_id=old_id
268 $order $limit";
270 $result = $this->db->query( $sql, $fname );
271 $wrapper = $this->db->resultObject( $result );
272 $this->outputPageStream( $wrapper );
274 if ( $this->list_authors ) {
275 $this->outputPageStream( $wrapper );
278 if( $this->buffer == WikiExporter::STREAM ) {
279 $this->db->bufferResults( $prev );
282 wfProfileOut( $fname );
286 * Runs through a query result set dumping page and revision records.
287 * The result set should be sorted/grouped by page to avoid duplicate
288 * page records in the output.
290 * The result set will be freed once complete. Should be safe for
291 * streaming (non-buffered) queries, as long as it was made on a
292 * separate database connection not managed by LoadBalancer; some
293 * blob storage types will make queries to pull source data.
295 * @param $resultset ResultWrapper
297 protected function outputPageStream( $resultset ) {
298 $last = null;
299 while( $row = $resultset->fetchObject() ) {
300 if( is_null( $last ) ||
301 $last->page_namespace != $row->page_namespace ||
302 $last->page_title != $row->page_title ) {
303 if( isset( $last ) ) {
304 $output = '';
305 if( $this->dumpUploads ) {
306 $output .= $this->writer->writeUploads( $last );
308 $output .= $this->writer->closePage();
309 $this->sink->writeClosePage( $output );
311 $output = $this->writer->openPage( $row );
312 $this->sink->writeOpenPage( $row, $output );
313 $last = $row;
315 $output = $this->writer->writeRevision( $row );
316 $this->sink->writeRevision( $row, $output );
318 if( isset( $last ) ) {
319 $output = '';
320 if( $this->dumpUploads ) {
321 $output .= $this->writer->writeUploads( $last );
323 $output .= $this->author_list;
324 $output .= $this->writer->closePage();
325 $this->sink->writeClosePage( $output );
327 $resultset->free();
330 protected function outputLogStream( $resultset ) {
331 while( $row = $resultset->fetchObject() ) {
332 $output = $this->writer->writeLogItem( $row );
333 $this->sink->writeLogItem( $row, $output );
335 $resultset->free();
340 * @ingroup Dump
342 class XmlDumpWriter {
345 * Returns the export schema version.
346 * @return string
348 function schemaVersion() {
349 return "0.3"; // FIXME: upgrade to 0.4 when updated XSD is ready, for the revision deletion bits
353 * Opens the XML output stream's root <mediawiki> element.
354 * This does not include an xml directive, so is safe to include
355 * as a subelement in a larger XML stream. Namespace and XML Schema
356 * references are included.
358 * Output will be encoded in UTF-8.
360 * @return string
362 function openStream() {
363 global $wgContLanguageCode;
364 $ver = $this->schemaVersion();
365 return Xml::element( 'mediawiki', array(
366 'xmlns' => "http://www.mediawiki.org/xml/export-$ver/",
367 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
368 'xsi:schemaLocation' => "http://www.mediawiki.org/xml/export-$ver/ " .
369 "http://www.mediawiki.org/xml/export-$ver.xsd",
370 'version' => $ver,
371 'xml:lang' => $wgContLanguageCode ),
372 null ) .
373 "\n" .
374 $this->siteInfo();
377 function siteInfo() {
378 $info = array(
379 $this->sitename(),
380 $this->homelink(),
381 $this->generator(),
382 $this->caseSetting(),
383 $this->namespaces() );
384 return " <siteinfo>\n " .
385 implode( "\n ", $info ) .
386 "\n </siteinfo>\n";
389 function sitename() {
390 global $wgSitename;
391 return Xml::element( 'sitename', array(), $wgSitename );
394 function generator() {
395 global $wgVersion;
396 return Xml::element( 'generator', array(), "MediaWiki $wgVersion" );
399 function homelink() {
400 return Xml::element( 'base', array(), Title::newMainPage()->getFullUrl() );
403 function caseSetting() {
404 global $wgCapitalLinks;
405 // "case-insensitive" option is reserved for future
406 $sensitivity = $wgCapitalLinks ? 'first-letter' : 'case-sensitive';
407 return Xml::element( 'case', array(), $sensitivity );
410 function namespaces() {
411 global $wgContLang;
412 $spaces = "<namespaces>\n";
413 foreach( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
414 $spaces .= ' ' . Xml::element( 'namespace', array( 'key' => $ns ), $title ) . "\n";
416 $spaces .= " </namespaces>";
417 return $spaces;
421 * Closes the output stream with the closing root element.
422 * Call when finished dumping things.
424 function closeStream() {
425 return "</mediawiki>\n";
430 * Opens a <page> section on the output stream, with data
431 * from the given database row.
433 * @param $row object
434 * @return string
435 * @access private
437 function openPage( $row ) {
438 $out = " <page>\n";
439 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
440 $out .= ' ' . Xml::elementClean( 'title', array(), $title->getPrefixedText() ) . "\n";
441 $out .= ' ' . Xml::element( 'id', array(), strval( $row->page_id ) ) . "\n";
442 if( '' != $row->page_restrictions ) {
443 $out .= ' ' . Xml::element( 'restrictions', array(),
444 strval( $row->page_restrictions ) ) . "\n";
446 return $out;
450 * Closes a <page> section on the output stream.
452 * @access private
454 function closePage() {
455 return " </page>\n";
459 * Dumps a <revision> section on the output stream, with
460 * data filled in from the given database row.
462 * @param $row object
463 * @return string
464 * @access private
466 function writeRevision( $row ) {
467 $fname = 'WikiExporter::dumpRev';
468 wfProfileIn( $fname );
470 $out = " <revision>\n";
471 $out .= " " . Xml::element( 'id', null, strval( $row->rev_id ) ) . "\n";
473 $out .= $this->writeTimestamp( $row->rev_timestamp );
475 if( $row->rev_deleted & Revision::DELETED_USER ) {
476 $out .= " " . Xml::element( 'contributor', array( 'deleted' => 'deleted' ) ) . "\n";
477 } else {
478 $out .= $this->writeContributor( $row->rev_user, $row->rev_user_text );
481 if( $row->rev_minor_edit ) {
482 $out .= " <minor/>\n";
484 if( $row->rev_deleted & Revision::DELETED_COMMENT ) {
485 $out .= " " . Xml::element( 'comment', array( 'deleted' => 'deleted' ) ) . "\n";
486 } elseif( $row->rev_comment != '' ) {
487 $out .= " " . Xml::elementClean( 'comment', null, strval( $row->rev_comment ) ) . "\n";
490 if( $row->rev_deleted & Revision::DELETED_TEXT ) {
491 $out .= " " . Xml::element( 'text', array( 'deleted' => 'deleted' ) ) . "\n";
492 } elseif( isset( $row->old_text ) ) {
493 // Raw text from the database may have invalid chars
494 $text = strval( Revision::getRevisionText( $row ) );
495 $out .= " " . Xml::elementClean( 'text',
496 array( 'xml:space' => 'preserve' ),
497 strval( $text ) ) . "\n";
498 } else {
499 // Stub output
500 $out .= " " . Xml::element( 'text',
501 array( 'id' => $row->rev_text_id ),
502 "" ) . "\n";
505 $out .= " </revision>\n";
507 wfProfileOut( $fname );
508 return $out;
512 * Dumps a <logitem> section on the output stream, with
513 * data filled in from the given database row.
515 * @param $row object
516 * @return string
517 * @access private
519 function writeLogItem( $row ) {
520 $fname = 'WikiExporter::writeLogItem';
521 wfProfileIn( $fname );
523 $out = " <logitem>\n";
524 $out .= " " . Xml::element( 'id', null, strval( $row->log_id ) ) . "\n";
526 $out .= $this->writeTimestamp( $row->log_timestamp );
528 if( $row->log_deleted & LogPage::DELETED_USER ) {
529 $out .= " " . Xml::element( 'contributor', array( 'deleted' => 'deleted' ) ) . "\n";
530 } else {
531 $out .= $this->writeContributor( $row->log_user, $row->user_name );
534 if( $row->log_deleted & LogPage::DELETED_COMMENT ) {
535 $out .= " " . Xml::element( 'comment', array( 'deleted' => 'deleted' ) ) . "\n";
536 } elseif( $row->log_comment != '' ) {
537 $out .= " " . Xml::elementClean( 'comment', null, strval( $row->log_comment ) ) . "\n";
540 $out .= " " . Xml::element( 'type', null, strval( $row->log_type ) ) . "\n";
541 $out .= " " . Xml::element( 'action', null, strval( $row->log_action ) ) . "\n";
543 if( $row->log_deleted & LogPage::DELETED_ACTION ) {
544 $out .= " " . Xml::element( 'text', array( 'deleted' => 'deleted' ) ) . "\n";
545 } else {
546 $title = Title::makeTitle( $row->log_namespace, $row->log_title );
547 $out .= " " . Xml::elementClean( 'logtitle', null, $title->getPrefixedText() ) . "\n";
548 $out .= " " . Xml::elementClean( 'params',
549 array( 'xml:space' => 'preserve' ),
550 strval( $row->log_params ) ) . "\n";
553 $out .= " </logitem>\n";
555 wfProfileOut( $fname );
556 return $out;
559 function writeTimestamp( $timestamp ) {
560 $ts = wfTimestamp( TS_ISO_8601, $timestamp );
561 return " " . Xml::element( 'timestamp', null, $ts ) . "\n";
564 function writeContributor( $id, $text ) {
565 $out = " <contributor>\n";
566 if( $id ) {
567 $out .= " " . Xml::elementClean( 'username', null, strval( $text ) ) . "\n";
568 $out .= " " . Xml::element( 'id', null, strval( $id ) ) . "\n";
569 } else {
570 $out .= " " . Xml::elementClean( 'ip', null, strval( $text ) ) . "\n";
572 $out .= " </contributor>\n";
573 return $out;
577 * Warning! This data is potentially inconsistent. :(
579 function writeUploads( $row ) {
580 if( $row->page_namespace == NS_IMAGE ) {
581 $img = wfFindFile( $row->page_title );
582 if( $img ) {
583 $out = '';
584 foreach( array_reverse( $img->getHistory() ) as $ver ) {
585 $out .= $this->writeUpload( $ver );
587 $out .= $this->writeUpload( $img );
588 return $out;
591 return '';
594 function writeUpload( $file ) {
595 return " <upload>\n" .
596 $this->writeTimestamp( $file->getTimestamp() ) .
597 $this->writeContributor( $file->getUser( 'id' ), $file->getUser( 'text' ) ) .
598 " " . Xml::elementClean( 'comment', null, $file->getDescription() ) . "\n" .
599 " " . Xml::element( 'filename', null, $file->getName() ) . "\n" .
600 " " . Xml::element( 'src', null, $file->getFullUrl() ) . "\n" .
601 " " . Xml::element( 'size', null, $file->getSize() ) . "\n" .
602 " </upload>\n";
609 * Base class for output stream; prints to stdout or buffer or whereever.
610 * @ingroup Dump
612 class DumpOutput {
613 function writeOpenStream( $string ) {
614 $this->write( $string );
617 function writeCloseStream( $string ) {
618 $this->write( $string );
621 function writeOpenPage( $page, $string ) {
622 $this->write( $string );
625 function writeClosePage( $string ) {
626 $this->write( $string );
629 function writeRevision( $rev, $string ) {
630 $this->write( $string );
633 function writeLogItem( $rev, $string ) {
634 $this->write( $string );
638 * Override to write to a different stream type.
639 * @return bool
641 function write( $string ) {
642 print $string;
647 * Stream outputter to send data to a file.
648 * @ingroup Dump
650 class DumpFileOutput extends DumpOutput {
651 var $handle;
653 function DumpFileOutput( $file ) {
654 $this->handle = fopen( $file, "wt" );
657 function write( $string ) {
658 fputs( $this->handle, $string );
663 * Stream outputter to send data to a file via some filter program.
664 * Even if compression is available in a library, using a separate
665 * program can allow us to make use of a multi-processor system.
666 * @ingroup Dump
668 class DumpPipeOutput extends DumpFileOutput {
669 function DumpPipeOutput( $command, $file = null ) {
670 if( !is_null( $file ) ) {
671 $command .= " > " . wfEscapeShellArg( $file );
673 $this->handle = popen( $command, "w" );
678 * Sends dump output via the gzip compressor.
679 * @ingroup Dump
681 class DumpGZipOutput extends DumpPipeOutput {
682 function DumpGZipOutput( $file ) {
683 parent::DumpPipeOutput( "gzip", $file );
688 * Sends dump output via the bgzip2 compressor.
689 * @ingroup Dump
691 class DumpBZip2Output extends DumpPipeOutput {
692 function DumpBZip2Output( $file ) {
693 parent::DumpPipeOutput( "bzip2", $file );
698 * Sends dump output via the p7zip compressor.
699 * @ingroup Dump
701 class Dump7ZipOutput extends DumpPipeOutput {
702 function Dump7ZipOutput( $file ) {
703 $command = "7za a -bd -si " . wfEscapeShellArg( $file );
704 // Suppress annoying useless crap from p7zip
705 // Unfortunately this could suppress real error messages too
706 $command .= ' >' . wfGetNull() . ' 2>&1';
707 parent::DumpPipeOutput( $command );
714 * Dump output filter class.
715 * This just does output filtering and streaming; XML formatting is done
716 * higher up, so be careful in what you do.
717 * @ingroup Dump
719 class DumpFilter {
720 function DumpFilter( &$sink ) {
721 $this->sink =& $sink;
724 function writeOpenStream( $string ) {
725 $this->sink->writeOpenStream( $string );
728 function writeCloseStream( $string ) {
729 $this->sink->writeCloseStream( $string );
732 function writeOpenPage( $page, $string ) {
733 $this->sendingThisPage = $this->pass( $page, $string );
734 if( $this->sendingThisPage ) {
735 $this->sink->writeOpenPage( $page, $string );
739 function writeClosePage( $string ) {
740 if( $this->sendingThisPage ) {
741 $this->sink->writeClosePage( $string );
742 $this->sendingThisPage = false;
746 function writeRevision( $rev, $string ) {
747 if( $this->sendingThisPage ) {
748 $this->sink->writeRevision( $rev, $string );
752 function writeLogItem( $rev, $string ) {
753 $this->sink->writeRevision( $rev, $string );
757 * Override for page-based filter types.
758 * @return bool
760 function pass( $page ) {
761 return true;
766 * Simple dump output filter to exclude all talk pages.
767 * @ingroup Dump
769 class DumpNotalkFilter extends DumpFilter {
770 function pass( $page ) {
771 return !MWNamespace::isTalk( $page->page_namespace );
776 * Dump output filter to include or exclude pages in a given set of namespaces.
777 * @ingroup Dump
779 class DumpNamespaceFilter extends DumpFilter {
780 var $invert = false;
781 var $namespaces = array();
783 function DumpNamespaceFilter( &$sink, $param ) {
784 parent::DumpFilter( $sink );
786 $constants = array(
787 "NS_MAIN" => NS_MAIN,
788 "NS_TALK" => NS_TALK,
789 "NS_USER" => NS_USER,
790 "NS_USER_TALK" => NS_USER_TALK,
791 "NS_PROJECT" => NS_PROJECT,
792 "NS_PROJECT_TALK" => NS_PROJECT_TALK,
793 "NS_FILE" => NS_FILE,
794 "NS_FILE_TALK" => NS_FILE_TALK,
795 "NS_IMAGE" => NS_IMAGE, // NS_IMAGE is an alias for NS_FILE
796 "NS_IMAGE_TALK" => NS_IMAGE_TALK,
797 "NS_MEDIAWIKI" => NS_MEDIAWIKI,
798 "NS_MEDIAWIKI_TALK" => NS_MEDIAWIKI_TALK,
799 "NS_TEMPLATE" => NS_TEMPLATE,
800 "NS_TEMPLATE_TALK" => NS_TEMPLATE_TALK,
801 "NS_HELP" => NS_HELP,
802 "NS_HELP_TALK" => NS_HELP_TALK,
803 "NS_CATEGORY" => NS_CATEGORY,
804 "NS_CATEGORY_TALK" => NS_CATEGORY_TALK );
806 if( $param{0} == '!' ) {
807 $this->invert = true;
808 $param = substr( $param, 1 );
811 foreach( explode( ',', $param ) as $key ) {
812 $key = trim( $key );
813 if( isset( $constants[$key] ) ) {
814 $ns = $constants[$key];
815 $this->namespaces[$ns] = true;
816 } elseif( is_numeric( $key ) ) {
817 $ns = intval( $key );
818 $this->namespaces[$ns] = true;
819 } else {
820 throw new MWException( "Unrecognized namespace key '$key'\n" );
825 function pass( $page ) {
826 $match = isset( $this->namespaces[$page->page_namespace] );
827 return $this->invert xor $match;
833 * Dump output filter to include only the last revision in each page sequence.
834 * @ingroup Dump
836 class DumpLatestFilter extends DumpFilter {
837 var $page, $pageString, $rev, $revString;
839 function writeOpenPage( $page, $string ) {
840 $this->page = $page;
841 $this->pageString = $string;
844 function writeClosePage( $string ) {
845 if( $this->rev ) {
846 $this->sink->writeOpenPage( $this->page, $this->pageString );
847 $this->sink->writeRevision( $this->rev, $this->revString );
848 $this->sink->writeClosePage( $string );
850 $this->rev = null;
851 $this->revString = null;
852 $this->page = null;
853 $this->pageString = null;
856 function writeRevision( $rev, $string ) {
857 if( $rev->rev_id == $this->page->page_latest ) {
858 $this->rev = $rev;
859 $this->revString = $string;
865 * Base class for output stream; prints to stdout or buffer or whereever.
866 * @ingroup Dump
868 class DumpMultiWriter {
869 function DumpMultiWriter( $sinks ) {
870 $this->sinks = $sinks;
871 $this->count = count( $sinks );
874 function writeOpenStream( $string ) {
875 for( $i = 0; $i < $this->count; $i++ ) {
876 $this->sinks[$i]->writeOpenStream( $string );
880 function writeCloseStream( $string ) {
881 for( $i = 0; $i < $this->count; $i++ ) {
882 $this->sinks[$i]->writeCloseStream( $string );
886 function writeOpenPage( $page, $string ) {
887 for( $i = 0; $i < $this->count; $i++ ) {
888 $this->sinks[$i]->writeOpenPage( $page, $string );
892 function writeClosePage( $string ) {
893 for( $i = 0; $i < $this->count; $i++ ) {
894 $this->sinks[$i]->writeClosePage( $string );
898 function writeRevision( $rev, $string ) {
899 for( $i = 0; $i < $this->count; $i++ ) {
900 $this->sinks[$i]->writeRevision( $rev, $string );
905 function xmlsafe( $string ) {
906 $fname = 'xmlsafe';
907 wfProfileIn( $fname );
910 * The page may contain old data which has not been properly normalized.
911 * Invalid UTF-8 sequences or forbidden control characters will make our
912 * XML output invalid, so be sure to strip them out.
914 $string = UtfNormal::cleanUp( $string );
916 $string = htmlspecialchars( $string );
917 wfProfileOut( $fname );
918 return $string;