fix some doxygen warnings
[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 .= ' ' . Xml::element( 'namespace', array( 'key' => $ns ), $title ) . "\n";
418 }
419 $spaces .= " </namespaces>";
420 return $spaces;
421 }
422
423 /**
424 * Closes the output stream with the closing root element.
425 * Call when finished dumping things.
426 */
427 function closeStream() {
428 return "</mediawiki>\n";
429 }
430
431
432 /**
433 * Opens a <page> section on the output stream, with data
434 * from the given database row.
435 *
436 * @param $row object
437 * @return string
438 * @access private
439 */
440 function openPage( $row ) {
441 $out = " <page>\n";
442 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
443 $out .= ' ' . Xml::elementClean( 'title', array(), $title->getPrefixedText() ) . "\n";
444 $out .= ' ' . Xml::element( 'id', array(), strval( $row->page_id ) ) . "\n";
445 if( $row->page_is_redirect ) {
446 $out .= ' ' . Xml::element( 'redirect', array() ) . "\n";
447 }
448 if( '' != $row->page_restrictions ) {
449 $out .= ' ' . Xml::element( 'restrictions', array(),
450 strval( $row->page_restrictions ) ) . "\n";
451 }
452
453 wfRunHooks( 'XmlDumpWriterOpenPage', array( $this, &$out, $row, $title ) );
454
455 return $out;
456 }
457
458 /**
459 * Closes a <page> section on the output stream.
460 *
461 * @access private
462 */
463 function closePage() {
464 return " </page>\n";
465 }
466
467 /**
468 * Dumps a <revision> section on the output stream, with
469 * data filled in from the given database row.
470 *
471 * @param $row object
472 * @return string
473 * @access private
474 */
475 function writeRevision( $row ) {
476 $fname = 'WikiExporter::dumpRev';
477 wfProfileIn( $fname );
478
479 $out = " <revision>\n";
480 $out .= " " . Xml::element( 'id', null, strval( $row->rev_id ) ) . "\n";
481
482 $out .= $this->writeTimestamp( $row->rev_timestamp );
483
484 if( $row->rev_deleted & Revision::DELETED_USER ) {
485 $out .= " " . Xml::element( 'contributor', array( 'deleted' => 'deleted' ) ) . "\n";
486 } else {
487 $out .= $this->writeContributor( $row->rev_user, $row->rev_user_text );
488 }
489
490 if( $row->rev_minor_edit ) {
491 $out .= " <minor/>\n";
492 }
493 if( $row->rev_deleted & Revision::DELETED_COMMENT ) {
494 $out .= " " . Xml::element( 'comment', array( 'deleted' => 'deleted' ) ) . "\n";
495 } elseif( $row->rev_comment != '' ) {
496 $out .= " " . Xml::elementClean( 'comment', null, strval( $row->rev_comment ) ) . "\n";
497 }
498
499 if( $row->rev_deleted & Revision::DELETED_TEXT ) {
500 $out .= " " . Xml::element( 'text', array( 'deleted' => 'deleted' ) ) . "\n";
501 } elseif( isset( $row->old_text ) ) {
502 // Raw text from the database may have invalid chars
503 $text = strval( Revision::getRevisionText( $row ) );
504 $out .= " " . Xml::elementClean( 'text',
505 array( 'xml:space' => 'preserve' ),
506 strval( $text ) ) . "\n";
507 } else {
508 // Stub output
509 $out .= " " . Xml::element( 'text',
510 array( 'id' => $row->rev_text_id ),
511 "" ) . "\n";
512 }
513
514 wfRunHooks( 'XmlDumpWriterWriteRevision', array( &$this, &$out, $row, $text ) );
515
516 $out .= " </revision>\n";
517
518 wfProfileOut( $fname );
519 return $out;
520 }
521
522 /**
523 * Dumps a <logitem> section on the output stream, with
524 * data filled in from the given database row.
525 *
526 * @param $row object
527 * @return string
528 * @access private
529 */
530 function writeLogItem( $row ) {
531 $fname = 'WikiExporter::writeLogItem';
532 wfProfileIn( $fname );
533
534 $out = " <logitem>\n";
535 $out .= " " . Xml::element( 'id', null, strval( $row->log_id ) ) . "\n";
536
537 $out .= $this->writeTimestamp( $row->log_timestamp );
538
539 if( $row->log_deleted & LogPage::DELETED_USER ) {
540 $out .= " " . Xml::element( 'contributor', array( 'deleted' => 'deleted' ) ) . "\n";
541 } else {
542 $out .= $this->writeContributor( $row->log_user, $row->user_name );
543 }
544
545 if( $row->log_deleted & LogPage::DELETED_COMMENT ) {
546 $out .= " " . Xml::element( 'comment', array( 'deleted' => 'deleted' ) ) . "\n";
547 } elseif( $row->log_comment != '' ) {
548 $out .= " " . Xml::elementClean( 'comment', null, strval( $row->log_comment ) ) . "\n";
549 }
550
551 $out .= " " . Xml::element( 'type', null, strval( $row->log_type ) ) . "\n";
552 $out .= " " . Xml::element( 'action', null, strval( $row->log_action ) ) . "\n";
553
554 if( $row->log_deleted & LogPage::DELETED_ACTION ) {
555 $out .= " " . Xml::element( 'text', array( 'deleted' => 'deleted' ) ) . "\n";
556 } else {
557 $title = Title::makeTitle( $row->log_namespace, $row->log_title );
558 $out .= " " . Xml::elementClean( 'logtitle', null, $title->getPrefixedText() ) . "\n";
559 $out .= " " . Xml::elementClean( 'params',
560 array( 'xml:space' => 'preserve' ),
561 strval( $row->log_params ) ) . "\n";
562 }
563
564 $out .= " </logitem>\n";
565
566 wfProfileOut( $fname );
567 return $out;
568 }
569
570 function writeTimestamp( $timestamp ) {
571 $ts = wfTimestamp( TS_ISO_8601, $timestamp );
572 return " " . Xml::element( 'timestamp', null, $ts ) . "\n";
573 }
574
575 function writeContributor( $id, $text ) {
576 $out = " <contributor>\n";
577 if( $id ) {
578 $out .= " " . Xml::elementClean( 'username', null, strval( $text ) ) . "\n";
579 $out .= " " . Xml::element( 'id', null, strval( $id ) ) . "\n";
580 } else {
581 $out .= " " . Xml::elementClean( 'ip', null, strval( $text ) ) . "\n";
582 }
583 $out .= " </contributor>\n";
584 return $out;
585 }
586
587 /**
588 * Warning! This data is potentially inconsistent. :(
589 */
590 function writeUploads( $row ) {
591 if( $row->page_namespace == NS_IMAGE ) {
592 $img = wfFindFile( $row->page_title );
593 if( $img ) {
594 $out = '';
595 foreach( array_reverse( $img->getHistory() ) as $ver ) {
596 $out .= $this->writeUpload( $ver );
597 }
598 $out .= $this->writeUpload( $img );
599 return $out;
600 }
601 }
602 return '';
603 }
604
605 function writeUpload( $file ) {
606 return " <upload>\n" .
607 $this->writeTimestamp( $file->getTimestamp() ) .
608 $this->writeContributor( $file->getUser( 'id' ), $file->getUser( 'text' ) ) .
609 " " . Xml::elementClean( 'comment', null, $file->getDescription() ) . "\n" .
610 " " . Xml::element( 'filename', null, $file->getName() ) . "\n" .
611 " " . Xml::element( 'src', null, $file->getFullUrl() ) . "\n" .
612 " " . Xml::element( 'size', null, $file->getSize() ) . "\n" .
613 " </upload>\n";
614 }
615
616 }
617
618
619 /**
620 * Base class for output stream; prints to stdout or buffer or whereever.
621 * @ingroup Dump
622 */
623 class DumpOutput {
624 function writeOpenStream( $string ) {
625 $this->write( $string );
626 }
627
628 function writeCloseStream( $string ) {
629 $this->write( $string );
630 }
631
632 function writeOpenPage( $page, $string ) {
633 $this->write( $string );
634 }
635
636 function writeClosePage( $string ) {
637 $this->write( $string );
638 }
639
640 function writeRevision( $rev, $string ) {
641 $this->write( $string );
642 }
643
644 function writeLogItem( $rev, $string ) {
645 $this->write( $string );
646 }
647
648 /**
649 * Override to write to a different stream type.
650 * @return bool
651 */
652 function write( $string ) {
653 print $string;
654 }
655 }
656
657 /**
658 * Stream outputter to send data to a file.
659 * @ingroup Dump
660 */
661 class DumpFileOutput extends DumpOutput {
662 var $handle;
663
664 function DumpFileOutput( $file ) {
665 $this->handle = fopen( $file, "wt" );
666 }
667
668 function write( $string ) {
669 fputs( $this->handle, $string );
670 }
671 }
672
673 /**
674 * Stream outputter to send data to a file via some filter program.
675 * Even if compression is available in a library, using a separate
676 * program can allow us to make use of a multi-processor system.
677 * @ingroup Dump
678 */
679 class DumpPipeOutput extends DumpFileOutput {
680 function DumpPipeOutput( $command, $file = null ) {
681 if( !is_null( $file ) ) {
682 $command .= " > " . wfEscapeShellArg( $file );
683 }
684 $this->handle = popen( $command, "w" );
685 }
686 }
687
688 /**
689 * Sends dump output via the gzip compressor.
690 * @ingroup Dump
691 */
692 class DumpGZipOutput extends DumpPipeOutput {
693 function DumpGZipOutput( $file ) {
694 parent::DumpPipeOutput( "gzip", $file );
695 }
696 }
697
698 /**
699 * Sends dump output via the bgzip2 compressor.
700 * @ingroup Dump
701 */
702 class DumpBZip2Output extends DumpPipeOutput {
703 function DumpBZip2Output( $file ) {
704 parent::DumpPipeOutput( "bzip2", $file );
705 }
706 }
707
708 /**
709 * Sends dump output via the p7zip compressor.
710 * @ingroup Dump
711 */
712 class Dump7ZipOutput extends DumpPipeOutput {
713 function Dump7ZipOutput( $file ) {
714 $command = "7za a -bd -si " . wfEscapeShellArg( $file );
715 // Suppress annoying useless crap from p7zip
716 // Unfortunately this could suppress real error messages too
717 $command .= ' >' . wfGetNull() . ' 2>&1';
718 parent::DumpPipeOutput( $command );
719 }
720 }
721
722
723
724 /**
725 * Dump output filter class.
726 * This just does output filtering and streaming; XML formatting is done
727 * higher up, so be careful in what you do.
728 * @ingroup Dump
729 */
730 class DumpFilter {
731 function DumpFilter( &$sink ) {
732 $this->sink =& $sink;
733 }
734
735 function writeOpenStream( $string ) {
736 $this->sink->writeOpenStream( $string );
737 }
738
739 function writeCloseStream( $string ) {
740 $this->sink->writeCloseStream( $string );
741 }
742
743 function writeOpenPage( $page, $string ) {
744 $this->sendingThisPage = $this->pass( $page, $string );
745 if( $this->sendingThisPage ) {
746 $this->sink->writeOpenPage( $page, $string );
747 }
748 }
749
750 function writeClosePage( $string ) {
751 if( $this->sendingThisPage ) {
752 $this->sink->writeClosePage( $string );
753 $this->sendingThisPage = false;
754 }
755 }
756
757 function writeRevision( $rev, $string ) {
758 if( $this->sendingThisPage ) {
759 $this->sink->writeRevision( $rev, $string );
760 }
761 }
762
763 function writeLogItem( $rev, $string ) {
764 $this->sink->writeRevision( $rev, $string );
765 }
766
767 /**
768 * Override for page-based filter types.
769 * @return bool
770 */
771 function pass( $page ) {
772 return true;
773 }
774 }
775
776 /**
777 * Simple dump output filter to exclude all talk pages.
778 * @ingroup Dump
779 */
780 class DumpNotalkFilter extends DumpFilter {
781 function pass( $page ) {
782 return !MWNamespace::isTalk( $page->page_namespace );
783 }
784 }
785
786 /**
787 * Dump output filter to include or exclude pages in a given set of namespaces.
788 * @ingroup Dump
789 */
790 class DumpNamespaceFilter extends DumpFilter {
791 var $invert = false;
792 var $namespaces = array();
793
794 function DumpNamespaceFilter( &$sink, $param ) {
795 parent::DumpFilter( $sink );
796
797 $constants = array(
798 "NS_MAIN" => NS_MAIN,
799 "NS_TALK" => NS_TALK,
800 "NS_USER" => NS_USER,
801 "NS_USER_TALK" => NS_USER_TALK,
802 "NS_PROJECT" => NS_PROJECT,
803 "NS_PROJECT_TALK" => NS_PROJECT_TALK,
804 "NS_FILE" => NS_FILE,
805 "NS_FILE_TALK" => NS_FILE_TALK,
806 "NS_IMAGE" => NS_IMAGE, // NS_IMAGE is an alias for NS_FILE
807 "NS_IMAGE_TALK" => NS_IMAGE_TALK,
808 "NS_MEDIAWIKI" => NS_MEDIAWIKI,
809 "NS_MEDIAWIKI_TALK" => NS_MEDIAWIKI_TALK,
810 "NS_TEMPLATE" => NS_TEMPLATE,
811 "NS_TEMPLATE_TALK" => NS_TEMPLATE_TALK,
812 "NS_HELP" => NS_HELP,
813 "NS_HELP_TALK" => NS_HELP_TALK,
814 "NS_CATEGORY" => NS_CATEGORY,
815 "NS_CATEGORY_TALK" => NS_CATEGORY_TALK );
816
817 if( $param{0} == '!' ) {
818 $this->invert = true;
819 $param = substr( $param, 1 );
820 }
821
822 foreach( explode( ',', $param ) as $key ) {
823 $key = trim( $key );
824 if( isset( $constants[$key] ) ) {
825 $ns = $constants[$key];
826 $this->namespaces[$ns] = true;
827 } elseif( is_numeric( $key ) ) {
828 $ns = intval( $key );
829 $this->namespaces[$ns] = true;
830 } else {
831 throw new MWException( "Unrecognized namespace key '$key'\n" );
832 }
833 }
834 }
835
836 function pass( $page ) {
837 $match = isset( $this->namespaces[$page->page_namespace] );
838 return $this->invert xor $match;
839 }
840 }
841
842
843 /**
844 * Dump output filter to include only the last revision in each page sequence.
845 * @ingroup Dump
846 */
847 class DumpLatestFilter extends DumpFilter {
848 var $page, $pageString, $rev, $revString;
849
850 function writeOpenPage( $page, $string ) {
851 $this->page = $page;
852 $this->pageString = $string;
853 }
854
855 function writeClosePage( $string ) {
856 if( $this->rev ) {
857 $this->sink->writeOpenPage( $this->page, $this->pageString );
858 $this->sink->writeRevision( $this->rev, $this->revString );
859 $this->sink->writeClosePage( $string );
860 }
861 $this->rev = null;
862 $this->revString = null;
863 $this->page = null;
864 $this->pageString = null;
865 }
866
867 function writeRevision( $rev, $string ) {
868 if( $rev->rev_id == $this->page->page_latest ) {
869 $this->rev = $rev;
870 $this->revString = $string;
871 }
872 }
873 }
874
875 /**
876 * Base class for output stream; prints to stdout or buffer or whereever.
877 * @ingroup Dump
878 */
879 class DumpMultiWriter {
880 function DumpMultiWriter( $sinks ) {
881 $this->sinks = $sinks;
882 $this->count = count( $sinks );
883 }
884
885 function writeOpenStream( $string ) {
886 for( $i = 0; $i < $this->count; $i++ ) {
887 $this->sinks[$i]->writeOpenStream( $string );
888 }
889 }
890
891 function writeCloseStream( $string ) {
892 for( $i = 0; $i < $this->count; $i++ ) {
893 $this->sinks[$i]->writeCloseStream( $string );
894 }
895 }
896
897 function writeOpenPage( $page, $string ) {
898 for( $i = 0; $i < $this->count; $i++ ) {
899 $this->sinks[$i]->writeOpenPage( $page, $string );
900 }
901 }
902
903 function writeClosePage( $string ) {
904 for( $i = 0; $i < $this->count; $i++ ) {
905 $this->sinks[$i]->writeClosePage( $string );
906 }
907 }
908
909 function writeRevision( $rev, $string ) {
910 for( $i = 0; $i < $this->count; $i++ ) {
911 $this->sinks[$i]->writeRevision( $rev, $string );
912 }
913 }
914 }
915
916 function xmlsafe( $string ) {
917 $fname = 'xmlsafe';
918 wfProfileIn( $fname );
919
920 /**
921 * The page may contain old data which has not been properly normalized.
922 * Invalid UTF-8 sequences or forbidden control characters will make our
923 * XML output invalid, so be sure to strip them out.
924 */
925 $string = UtfNormal::cleanUp( $string );
926
927 $string = htmlspecialchars( $string );
928 wfProfileOut( $fname );
929 return $string;
930 }