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