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