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