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