Consistent letter case
[mediawiki.git] / includes / Export.php
blobf05b660ec41851a632bda5d6f513eed979c3f219
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 = 1;
34 const CURRENT = 2;
35 const STABLE = 4; // extension defined
36 const LOGS = 8;
38 const BUFFER = 0;
39 const STREAM = 1;
41 const TEXT = 0;
42 const STUB = 1;
44 /**
45 * If using WikiExporter::STREAM to stream a large amount of data,
46 * provide a database connection which is not managed by
47 * LoadBalancer to read from: some history blob types will
48 * make additional queries to pull source data while the
49 * main query is still running.
51 * @param $db Database
52 * @param $history Mixed: one of WikiExporter::FULL or WikiExporter::CURRENT,
53 * or an associative array:
54 * offset: non-inclusive offset at which to start the query
55 * limit: maximum number of rows to return
56 * dir: "asc" or "desc" timestamp order
57 * @param $buffer Int: one of WikiExporter::BUFFER or WikiExporter::STREAM
59 function __construct( &$db, $history = WikiExporter::CURRENT,
60 $buffer = WikiExporter::BUFFER, $text = WikiExporter::TEXT ) {
61 $this->db =& $db;
62 $this->history = $history;
63 $this->buffer = $buffer;
64 $this->writer = new XmlDumpWriter();
65 $this->sink = new DumpOutput();
66 $this->text = $text;
69 /**
70 * Set the DumpOutput or DumpFilter object which will receive
71 * various row objects and XML output for filtering. Filters
72 * can be chained or used as callbacks.
74 * @param $sink mixed
76 public function setOutputSink( &$sink ) {
77 $this->sink =& $sink;
80 public function openStream() {
81 $output = $this->writer->openStream();
82 $this->sink->writeOpenStream( $output );
85 public function closeStream() {
86 $output = $this->writer->closeStream();
87 $this->sink->writeCloseStream( $output );
90 /**
91 * Dumps a series of page and revision records for all pages
92 * in the database, either including complete history or only
93 * the most recent version.
95 public function allPages() {
96 return $this->dumpFrom( '' );
99 /**
100 * Dumps a series of page and revision records for those pages
101 * in the database falling within the page_id range given.
102 * @param $start Int: inclusive lower limit (this id is included)
103 * @param $end Int: Exclusive upper limit (this id is not included)
104 * If 0, no upper limit.
106 public function pagesByRange( $start, $end ) {
107 $condition = 'page_id >= ' . intval( $start );
108 if( $end ) {
109 $condition .= ' AND page_id < ' . intval( $end );
111 return $this->dumpFrom( $condition );
115 * @param $title Title
117 public function pageByTitle( $title ) {
118 return $this->dumpFrom(
119 'page_namespace=' . $title->getNamespace() .
120 ' AND page_title=' . $this->db->addQuotes( $title->getDBkey() ) );
123 public function pageByName( $name ) {
124 $title = Title::newFromText( $name );
125 if( is_null( $title ) ) {
126 return new WikiError( "Can't export invalid title" );
127 } else {
128 return $this->pageByTitle( $title );
132 public function pagesByName( $names ) {
133 foreach( $names as $name ) {
134 $this->pageByName( $name );
138 public function allLogs() {
139 return $this->dumpFrom( '' );
142 public function logsByRange( $start, $end ) {
143 $condition = 'log_id >= ' . intval( $start );
144 if( $end ) {
145 $condition .= ' AND log_id < ' . intval( $end );
147 return $this->dumpFrom( $condition );
150 # Generates the distinct list of authors of an article
151 # Not called by default (depends on $this->list_authors)
152 # Can be set by Special:Export when not exporting whole history
153 protected function do_list_authors( $page , $revision , $cond ) {
154 $fname = "do_list_authors" ;
155 wfProfileIn( $fname );
156 $this->author_list = "<contributors>";
157 //rev_deleted
158 $nothidden = '('.$this->db->bitAnd('rev_deleted', Revision::DELETED_USER) . ') = 0';
160 $sql = "SELECT DISTINCT rev_user_text,rev_user FROM {$page},{$revision}
161 WHERE page_id=rev_page AND $nothidden AND " . $cond ;
162 $result = $this->db->query( $sql, $fname );
163 $resultset = $this->db->resultObject( $result );
164 while( $row = $resultset->fetchObject() ) {
165 $this->author_list .= "<contributor>" .
166 "<username>" .
167 htmlentities( $row->rev_user_text ) .
168 "</username>" .
169 "<id>" .
170 $row->rev_user .
171 "</id>" .
172 "</contributor>";
174 wfProfileOut( $fname );
175 $this->author_list .= "</contributors>";
178 protected function dumpFrom( $cond = '' ) {
179 wfProfileIn( __METHOD__ );
180 # For logging dumps...
181 if( $this->history & self::LOGS ) {
182 if( $this->buffer == WikiExporter::STREAM ) {
183 $prev = $this->db->bufferResults( false );
185 $where = array( 'user_id = log_user' );
186 # Hide private logs
187 $hideLogs = LogEventsList::getExcludeClause( $this->db );
188 if( $hideLogs ) $where[] = $hideLogs;
189 # Add on any caller specified conditions
190 if( $cond ) $where[] = $cond;
191 # Get logging table name for logging.* clause
192 $logging = $this->db->tableName('logging');
193 $result = $this->db->select( array('logging','user'),
194 array( "{$logging}.*", 'user_name' ), // grab the user name
195 $where,
196 __METHOD__,
197 array( 'ORDER BY' => 'log_id', 'USE INDEX' => array('logging' => 'PRIMARY') )
199 $wrapper = $this->db->resultObject( $result );
200 $this->outputLogStream( $wrapper );
201 if( $this->buffer == WikiExporter::STREAM ) {
202 $this->db->bufferResults( $prev );
204 # For page dumps...
205 } else {
206 $tables = array( 'page', 'revision' );
207 $opts = array( 'ORDER BY' => 'page_id ASC' );
208 $opts['USE INDEX'] = array();
209 $join = array();
210 # Full history dumps...
211 if( $this->history & WikiExporter::FULL ) {
212 $join['revision'] = array('INNER JOIN','page_id=rev_page');
213 # Latest revision dumps...
214 } elseif( $this->history & WikiExporter::CURRENT ) {
215 if( $this->list_authors && $cond != '' ) { // List authors, if so desired
216 list($page,$revision) = $this->db->tableNamesN('page','revision');
217 $this->do_list_authors( $page, $revision, $cond );
219 $join['revision'] = array('INNER JOIN','page_id=rev_page AND page_latest=rev_id');
220 # "Stable" revision dumps...
221 } elseif( $this->history & WikiExporter::STABLE ) {
222 # Default JOIN, to be overridden...
223 $join['revision'] = array('INNER JOIN','page_id=rev_page AND page_latest=rev_id');
224 # One, and only one hook should set this, and return false
225 if( wfRunHooks( 'WikiExporter::dumpStableQuery', array(&$tables,&$opts,&$join) ) ) {
226 wfProfileOut( __METHOD__ );
227 return new WikiError( __METHOD__." given invalid history dump type." );
229 # Time offset/limit for all pages/history...
230 } elseif( is_array( $this->history ) ) {
231 $revJoin = 'page_id=rev_page';
232 # Set time order
233 if( $this->history['dir'] == 'asc' ) {
234 $op = '>';
235 $opts['ORDER BY'] = 'rev_timestamp ASC';
236 } else {
237 $op = '<';
238 $opts['ORDER BY'] = 'rev_timestamp DESC';
240 # Set offset
241 if( !empty( $this->history['offset'] ) ) {
242 $revJoin .= " AND rev_timestamp $op " .
243 $this->db->addQuotes( $this->db->timestamp( $this->history['offset'] ) );
245 $join['revision'] = array('INNER JOIN',$revJoin);
246 # Set query limit
247 if( !empty( $this->history['limit'] ) ) {
248 $opts['LIMIT'] = intval( $this->history['limit'] );
250 # Uknown history specification parameter?
251 } else {
252 wfProfileOut( __METHOD__ );
253 return new WikiError( __METHOD__." given invalid history dump type." );
255 # Query optimization hacks
256 if( $cond == '' ) {
257 $opts[] = 'STRAIGHT_JOIN';
258 $opts['USE INDEX']['page'] = 'PRIMARY';
260 # Build text join options
261 if( $this->text != WikiExporter::STUB ) { // 1-pass
262 $tables[] = 'text';
263 $join['text'] = array('INNER JOIN','rev_text_id=old_id');
266 if( $this->buffer == WikiExporter::STREAM ) {
267 $prev = $this->db->bufferResults( false );
270 wfRunHooks( 'ModifyExportQuery',
271 array( $this->db, &$tables, &$cond, &$opts, &$join ) );
273 # Do the query!
274 $result = $this->db->select( $tables, '*', $cond, __METHOD__, $opts, $join );
275 $wrapper = $this->db->resultObject( $result );
276 # Output dump results
277 $this->outputPageStream( $wrapper );
278 if( $this->list_authors ) {
279 $this->outputPageStream( $wrapper );
282 if( $this->buffer == WikiExporter::STREAM ) {
283 $this->db->bufferResults( $prev );
286 wfProfileOut( __METHOD__ );
290 * Runs through a query result set dumping page and revision records.
291 * The result set should be sorted/grouped by page to avoid duplicate
292 * page records in the output.
294 * The result set will be freed once complete. Should be safe for
295 * streaming (non-buffered) queries, as long as it was made on a
296 * separate database connection not managed by LoadBalancer; some
297 * blob storage types will make queries to pull source data.
299 * @param $resultset ResultWrapper
301 protected function outputPageStream( $resultset ) {
302 $last = null;
303 while( $row = $resultset->fetchObject() ) {
304 if( is_null( $last ) ||
305 $last->page_namespace != $row->page_namespace ||
306 $last->page_title != $row->page_title ) {
307 if( isset( $last ) ) {
308 $output = '';
309 if( $this->dumpUploads ) {
310 $output .= $this->writer->writeUploads( $last );
312 $output .= $this->writer->closePage();
313 $this->sink->writeClosePage( $output );
315 $output = $this->writer->openPage( $row );
316 $this->sink->writeOpenPage( $row, $output );
317 $last = $row;
319 $output = $this->writer->writeRevision( $row );
320 $this->sink->writeRevision( $row, $output );
322 if( isset( $last ) ) {
323 $output = '';
324 if( $this->dumpUploads ) {
325 $output .= $this->writer->writeUploads( $last );
327 $output .= $this->author_list;
328 $output .= $this->writer->closePage();
329 $this->sink->writeClosePage( $output );
333 protected function outputLogStream( $resultset ) {
334 while( $row = $resultset->fetchObject() ) {
335 $output = $this->writer->writeLogItem( $row );
336 $this->sink->writeLogItem( $row, $output );
342 * @ingroup Dump
344 class XmlDumpWriter {
347 * Returns the export schema version.
348 * @return string
350 function schemaVersion() {
351 return "0.3"; // FIXME: upgrade to 0.4 when updated XSD is ready, for the revision deletion bits
355 * Opens the XML output stream's root <mediawiki> element.
356 * This does not include an xml directive, so is safe to include
357 * as a subelement in a larger XML stream. Namespace and XML Schema
358 * references are included.
360 * Output will be encoded in UTF-8.
362 * @return string
364 function openStream() {
365 global $wgContLanguageCode;
366 $ver = $this->schemaVersion();
367 return Xml::element( 'mediawiki', array(
368 'xmlns' => "http://www.mediawiki.org/xml/export-$ver/",
369 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
370 'xsi:schemaLocation' => "http://www.mediawiki.org/xml/export-$ver/ " .
371 "http://www.mediawiki.org/xml/export-$ver.xsd",
372 'version' => $ver,
373 'xml:lang' => $wgContLanguageCode ),
374 null ) .
375 "\n" .
376 $this->siteInfo();
379 function siteInfo() {
380 $info = array(
381 $this->sitename(),
382 $this->homelink(),
383 $this->generator(),
384 $this->caseSetting(),
385 $this->namespaces() );
386 return " <siteinfo>\n " .
387 implode( "\n ", $info ) .
388 "\n </siteinfo>\n";
391 function sitename() {
392 global $wgSitename;
393 return Xml::element( 'sitename', array(), $wgSitename );
396 function generator() {
397 global $wgVersion;
398 return Xml::element( 'generator', array(), "MediaWiki $wgVersion" );
401 function homelink() {
402 return Xml::element( 'base', array(), Title::newMainPage()->getFullUrl() );
405 function caseSetting() {
406 global $wgCapitalLinks;
407 // "case-insensitive" option is reserved for future
408 $sensitivity = $wgCapitalLinks ? 'first-letter' : 'case-sensitive';
409 return Xml::element( 'case', array(), $sensitivity );
412 function namespaces() {
413 global $wgContLang;
414 $spaces = "<namespaces>\n";
415 foreach( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
416 $spaces .= ' ' . Xml::element( 'namespace', array( 'key' => $ns ), $title ) . "\n";
418 $spaces .= " </namespaces>";
419 return $spaces;
423 * Closes the output stream with the closing root element.
424 * Call when finished dumping things.
426 function closeStream() {
427 return "</mediawiki>\n";
432 * Opens a <page> section on the output stream, with data
433 * from the given database row.
435 * @param $row object
436 * @return string
437 * @access private
439 function openPage( $row ) {
440 $out = " <page>\n";
441 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
442 $out .= ' ' . Xml::elementClean( 'title', array(), $title->getPrefixedText() ) . "\n";
443 $out .= ' ' . Xml::element( 'id', array(), strval( $row->page_id ) ) . "\n";
444 if( $row->page_is_redirect ) {
445 $out .= ' ' . Xml::element( 'redirect', array() ). "\n";
447 if( '' != $row->page_restrictions ) {
448 $out .= ' ' . Xml::element( 'restrictions', array(),
449 strval( $row->page_restrictions ) ) . "\n";
452 wfRunHooks( 'XmlDumpWriterOpenPage', array( $this, &$out, $row, $title ) );
454 return $out;
458 * Closes a <page> section on the output stream.
460 * @access private
462 function closePage() {
463 return " </page>\n";
467 * Dumps a <revision> section on the output stream, with
468 * data filled in from the given database row.
470 * @param $row object
471 * @return string
472 * @access private
474 function writeRevision( $row ) {
475 $fname = 'WikiExporter::dumpRev';
476 wfProfileIn( $fname );
478 $out = " <revision>\n";
479 $out .= " " . Xml::element( 'id', null, strval( $row->rev_id ) ) . "\n";
481 $out .= $this->writeTimestamp( $row->rev_timestamp );
483 if( $row->rev_deleted & Revision::DELETED_USER ) {
484 $out .= " " . Xml::element( 'contributor', array( 'deleted' => 'deleted' ) ) . "\n";
485 } else {
486 $out .= $this->writeContributor( $row->rev_user, $row->rev_user_text );
489 if( $row->rev_minor_edit ) {
490 $out .= " <minor/>\n";
492 if( $row->rev_deleted & Revision::DELETED_COMMENT ) {
493 $out .= " " . Xml::element( 'comment', array( 'deleted' => 'deleted' ) ) . "\n";
494 } elseif( $row->rev_comment != '' ) {
495 $out .= " " . Xml::elementClean( 'comment', null, strval( $row->rev_comment ) ) . "\n";
498 if( $row->rev_deleted & Revision::DELETED_TEXT ) {
499 $out .= " " . Xml::element( 'text', array( 'deleted' => 'deleted' ) ) . "\n";
500 } elseif( isset( $row->old_text ) ) {
501 // Raw text from the database may have invalid chars
502 $text = strval( Revision::getRevisionText( $row ) );
503 $out .= " " . Xml::elementClean( 'text',
504 array( 'xml:space' => 'preserve' ),
505 strval( $text ) ) . "\n";
506 } else {
507 // Stub output
508 $out .= " " . Xml::element( 'text',
509 array( 'id' => $row->rev_text_id ),
510 "" ) . "\n";
513 wfRunHooks( 'XmlDumpWriterWriteRevision', array( &$this, &$out, $row, $text ) );
515 $out .= " </revision>\n";
517 wfProfileOut( $fname );
518 return $out;
522 * Dumps a <logitem> section on the output stream, with
523 * data filled in from the given database row.
525 * @param $row object
526 * @return string
527 * @access private
529 function writeLogItem( $row ) {
530 $fname = 'WikiExporter::writeLogItem';
531 wfProfileIn( $fname );
533 $out = " <logitem>\n";
534 $out .= " " . Xml::element( 'id', null, strval( $row->log_id ) ) . "\n";
536 $out .= $this->writeTimestamp( $row->log_timestamp );
538 if( $row->log_deleted & LogPage::DELETED_USER ) {
539 $out .= " " . Xml::element( 'contributor', array( 'deleted' => 'deleted' ) ) . "\n";
540 } else {
541 $out .= $this->writeContributor( $row->log_user, $row->user_name );
544 if( $row->log_deleted & LogPage::DELETED_COMMENT ) {
545 $out .= " " . Xml::element( 'comment', array( 'deleted' => 'deleted' ) ) . "\n";
546 } elseif( $row->log_comment != '' ) {
547 $out .= " " . Xml::elementClean( 'comment', null, strval( $row->log_comment ) ) . "\n";
550 $out .= " " . Xml::element( 'type', null, strval( $row->log_type ) ) . "\n";
551 $out .= " " . Xml::element( 'action', null, strval( $row->log_action ) ) . "\n";
553 if( $row->log_deleted & LogPage::DELETED_ACTION ) {
554 $out .= " " . Xml::element( 'text', array( 'deleted' => 'deleted' ) ) . "\n";
555 } else {
556 $title = Title::makeTitle( $row->log_namespace, $row->log_title );
557 $out .= " " . Xml::elementClean( 'logtitle', null, $title->getPrefixedText() ) . "\n";
558 $out .= " " . Xml::elementClean( 'params',
559 array( 'xml:space' => 'preserve' ),
560 strval( $row->log_params ) ) . "\n";
563 $out .= " </logitem>\n";
565 wfProfileOut( $fname );
566 return $out;
569 function writeTimestamp( $timestamp ) {
570 $ts = wfTimestamp( TS_ISO_8601, $timestamp );
571 return " " . Xml::element( 'timestamp', null, $ts ) . "\n";
574 function writeContributor( $id, $text ) {
575 $out = " <contributor>\n";
576 if( $id ) {
577 $out .= " " . Xml::elementClean( 'username', null, strval( $text ) ) . "\n";
578 $out .= " " . Xml::element( 'id', null, strval( $id ) ) . "\n";
579 } else {
580 $out .= " " . Xml::elementClean( 'ip', null, strval( $text ) ) . "\n";
582 $out .= " </contributor>\n";
583 return $out;
587 * Warning! This data is potentially inconsistent. :(
589 function writeUploads( $row ) {
590 if( $row->page_namespace == NS_IMAGE ) {
591 $img = wfFindFile( $row->page_title );
592 if( $img ) {
593 $out = '';
594 foreach( array_reverse( $img->getHistory() ) as $ver ) {
595 $out .= $this->writeUpload( $ver );
597 $out .= $this->writeUpload( $img );
598 return $out;
601 return '';
604 function writeUpload( $file ) {
605 return " <upload>\n" .
606 $this->writeTimestamp( $file->getTimestamp() ) .
607 $this->writeContributor( $file->getUser( 'id' ), $file->getUser( 'text' ) ) .
608 " " . Xml::elementClean( 'comment', null, $file->getDescription() ) . "\n" .
609 " " . Xml::element( 'filename', null, $file->getName() ) . "\n" .
610 " " . Xml::element( 'src', null, $file->getFullUrl() ) . "\n" .
611 " " . Xml::element( 'size', null, $file->getSize() ) . "\n" .
612 " </upload>\n";
619 * Base class for output stream; prints to stdout or buffer or whereever.
620 * @ingroup Dump
622 class DumpOutput {
623 function writeOpenStream( $string ) {
624 $this->write( $string );
627 function writeCloseStream( $string ) {
628 $this->write( $string );
631 function writeOpenPage( $page, $string ) {
632 $this->write( $string );
635 function writeClosePage( $string ) {
636 $this->write( $string );
639 function writeRevision( $rev, $string ) {
640 $this->write( $string );
643 function writeLogItem( $rev, $string ) {
644 $this->write( $string );
648 * Override to write to a different stream type.
649 * @return bool
651 function write( $string ) {
652 print $string;
657 * Stream outputter to send data to a file.
658 * @ingroup Dump
660 class DumpFileOutput extends DumpOutput {
661 var $handle;
663 function DumpFileOutput( $file ) {
664 $this->handle = fopen( $file, "wt" );
667 function write( $string ) {
668 fputs( $this->handle, $string );
673 * Stream outputter to send data to a file via some filter program.
674 * Even if compression is available in a library, using a separate
675 * program can allow us to make use of a multi-processor system.
676 * @ingroup Dump
678 class DumpPipeOutput extends DumpFileOutput {
679 function DumpPipeOutput( $command, $file = null ) {
680 if( !is_null( $file ) ) {
681 $command .= " > " . wfEscapeShellArg( $file );
683 $this->handle = popen( $command, "w" );
688 * Sends dump output via the gzip compressor.
689 * @ingroup Dump
691 class DumpGZipOutput extends DumpPipeOutput {
692 function DumpGZipOutput( $file ) {
693 parent::DumpPipeOutput( "gzip", $file );
698 * Sends dump output via the bgzip2 compressor.
699 * @ingroup Dump
701 class DumpBZip2Output extends DumpPipeOutput {
702 function DumpBZip2Output( $file ) {
703 parent::DumpPipeOutput( "bzip2", $file );
708 * Sends dump output via the p7zip compressor.
709 * @ingroup Dump
711 class Dump7ZipOutput extends DumpPipeOutput {
712 function Dump7ZipOutput( $file ) {
713 $command = "7za a -bd -si " . wfEscapeShellArg( $file );
714 // Suppress annoying useless crap from p7zip
715 // Unfortunately this could suppress real error messages too
716 $command .= ' >' . wfGetNull() . ' 2>&1';
717 parent::DumpPipeOutput( $command );
724 * Dump output filter class.
725 * This just does output filtering and streaming; XML formatting is done
726 * higher up, so be careful in what you do.
727 * @ingroup Dump
729 class DumpFilter {
730 function DumpFilter( &$sink ) {
731 $this->sink =& $sink;
734 function writeOpenStream( $string ) {
735 $this->sink->writeOpenStream( $string );
738 function writeCloseStream( $string ) {
739 $this->sink->writeCloseStream( $string );
742 function writeOpenPage( $page, $string ) {
743 $this->sendingThisPage = $this->pass( $page, $string );
744 if( $this->sendingThisPage ) {
745 $this->sink->writeOpenPage( $page, $string );
749 function writeClosePage( $string ) {
750 if( $this->sendingThisPage ) {
751 $this->sink->writeClosePage( $string );
752 $this->sendingThisPage = false;
756 function writeRevision( $rev, $string ) {
757 if( $this->sendingThisPage ) {
758 $this->sink->writeRevision( $rev, $string );
762 function writeLogItem( $rev, $string ) {
763 $this->sink->writeRevision( $rev, $string );
767 * Override for page-based filter types.
768 * @return bool
770 function pass( $page ) {
771 return true;
776 * Simple dump output filter to exclude all talk pages.
777 * @ingroup Dump
779 class DumpNotalkFilter extends DumpFilter {
780 function pass( $page ) {
781 return !MWNamespace::isTalk( $page->page_namespace );
786 * Dump output filter to include or exclude pages in a given set of namespaces.
787 * @ingroup Dump
789 class DumpNamespaceFilter extends DumpFilter {
790 var $invert = false;
791 var $namespaces = array();
793 function DumpNamespaceFilter( &$sink, $param ) {
794 parent::DumpFilter( $sink );
796 $constants = array(
797 "NS_MAIN" => NS_MAIN,
798 "NS_TALK" => NS_TALK,
799 "NS_USER" => NS_USER,
800 "NS_USER_TALK" => NS_USER_TALK,
801 "NS_PROJECT" => NS_PROJECT,
802 "NS_PROJECT_TALK" => NS_PROJECT_TALK,
803 "NS_FILE" => NS_FILE,
804 "NS_FILE_TALK" => NS_FILE_TALK,
805 "NS_IMAGE" => NS_IMAGE, // NS_IMAGE is an alias for NS_FILE
806 "NS_IMAGE_TALK" => NS_IMAGE_TALK,
807 "NS_MEDIAWIKI" => NS_MEDIAWIKI,
808 "NS_MEDIAWIKI_TALK" => NS_MEDIAWIKI_TALK,
809 "NS_TEMPLATE" => NS_TEMPLATE,
810 "NS_TEMPLATE_TALK" => NS_TEMPLATE_TALK,
811 "NS_HELP" => NS_HELP,
812 "NS_HELP_TALK" => NS_HELP_TALK,
813 "NS_CATEGORY" => NS_CATEGORY,
814 "NS_CATEGORY_TALK" => NS_CATEGORY_TALK );
816 if( $param{0} == '!' ) {
817 $this->invert = true;
818 $param = substr( $param, 1 );
821 foreach( explode( ',', $param ) as $key ) {
822 $key = trim( $key );
823 if( isset( $constants[$key] ) ) {
824 $ns = $constants[$key];
825 $this->namespaces[$ns] = true;
826 } elseif( is_numeric( $key ) ) {
827 $ns = intval( $key );
828 $this->namespaces[$ns] = true;
829 } else {
830 throw new MWException( "Unrecognized namespace key '$key'\n" );
835 function pass( $page ) {
836 $match = isset( $this->namespaces[$page->page_namespace] );
837 return $this->invert xor $match;
843 * Dump output filter to include only the last revision in each page sequence.
844 * @ingroup Dump
846 class DumpLatestFilter extends DumpFilter {
847 var $page, $pageString, $rev, $revString;
849 function writeOpenPage( $page, $string ) {
850 $this->page = $page;
851 $this->pageString = $string;
854 function writeClosePage( $string ) {
855 if( $this->rev ) {
856 $this->sink->writeOpenPage( $this->page, $this->pageString );
857 $this->sink->writeRevision( $this->rev, $this->revString );
858 $this->sink->writeClosePage( $string );
860 $this->rev = null;
861 $this->revString = null;
862 $this->page = null;
863 $this->pageString = null;
866 function writeRevision( $rev, $string ) {
867 if( $rev->rev_id == $this->page->page_latest ) {
868 $this->rev = $rev;
869 $this->revString = $string;
875 * Base class for output stream; prints to stdout or buffer or whereever.
876 * @ingroup Dump
878 class DumpMultiWriter {
879 function DumpMultiWriter( $sinks ) {
880 $this->sinks = $sinks;
881 $this->count = count( $sinks );
884 function writeOpenStream( $string ) {
885 for( $i = 0; $i < $this->count; $i++ ) {
886 $this->sinks[$i]->writeOpenStream( $string );
890 function writeCloseStream( $string ) {
891 for( $i = 0; $i < $this->count; $i++ ) {
892 $this->sinks[$i]->writeCloseStream( $string );
896 function writeOpenPage( $page, $string ) {
897 for( $i = 0; $i < $this->count; $i++ ) {
898 $this->sinks[$i]->writeOpenPage( $page, $string );
902 function writeClosePage( $string ) {
903 for( $i = 0; $i < $this->count; $i++ ) {
904 $this->sinks[$i]->writeClosePage( $string );
908 function writeRevision( $rev, $string ) {
909 for( $i = 0; $i < $this->count; $i++ ) {
910 $this->sinks[$i]->writeRevision( $rev, $string );
915 function xmlsafe( $string ) {
916 $fname = 'xmlsafe';
917 wfProfileIn( $fname );
920 * The page may contain old data which has not been properly normalized.
921 * Invalid UTF-8 sequences or forbidden control characters will make our
922 * XML output invalid, so be sure to strip them out.
924 $string = UtfNormal::cleanUp( $string );
926 $string = htmlspecialchars( $string );
927 wfProfileOut( $fname );
928 return $string;