3 * Base classes for dumps and export
5 * Copyright © 2003, 2005, 2006 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
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.
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.
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
31 * @ingroup SpecialPage Dump
34 var $list_authors = false ; # Return distinct author list (when not returning full history)
35 var $author_list = "" ;
37 var $dumpUploads = false;
38 var $dumpUploadFileContents = false;
42 const STABLE
= 4; // extension defined
52 * If using WikiExporter::STREAM to stream a large amount of data,
53 * provide a database connection which is not managed by
54 * LoadBalancer to read from: some history blob types will
55 * make additional queries to pull source data while the
56 * main query is still running.
59 * @param $history Mixed: one of WikiExporter::FULL or WikiExporter::CURRENT,
60 * or an associative array:
61 * offset: non-inclusive offset at which to start the query
62 * limit: maximum number of rows to return
63 * dir: "asc" or "desc" timestamp order
64 * @param $buffer Int: one of WikiExporter::BUFFER or WikiExporter::STREAM
65 * @param $text Int: one of WikiExporter::TEXT or WikiExporter::STUB
67 function __construct( &$db, $history = WikiExporter
::CURRENT
,
68 $buffer = WikiExporter
::BUFFER
, $text = WikiExporter
::TEXT
) {
70 $this->history
= $history;
71 $this->buffer
= $buffer;
72 $this->writer
= new XmlDumpWriter();
73 $this->sink
= new DumpOutput();
78 * Set the DumpOutput or DumpFilter object which will receive
79 * various row objects and XML output for filtering. Filters
80 * can be chained or used as callbacks.
84 public function setOutputSink( &$sink ) {
88 public function openStream() {
89 $output = $this->writer
->openStream();
90 $this->sink
->writeOpenStream( $output );
93 public function closeStream() {
94 $output = $this->writer
->closeStream();
95 $this->sink
->writeCloseStream( $output );
99 * Dumps a series of page and revision records for all pages
100 * in the database, either including complete history or only
101 * the most recent version.
103 public function allPages() {
104 return $this->dumpFrom( '' );
108 * Dumps a series of page and revision records for those pages
109 * in the database falling within the page_id range given.
110 * @param $start Int: inclusive lower limit (this id is included)
111 * @param $end Int: Exclusive upper limit (this id is not included)
112 * If 0, no upper limit.
114 public function pagesByRange( $start, $end ) {
115 $condition = 'page_id >= ' . intval( $start );
117 $condition .= ' AND page_id < ' . intval( $end );
119 return $this->dumpFrom( $condition );
123 * @param $title Title
125 public function pageByTitle( $title ) {
126 return $this->dumpFrom(
127 'page_namespace=' . $title->getNamespace() .
128 ' AND page_title=' . $this->db
->addQuotes( $title->getDBkey() ) );
131 public function pageByName( $name ) {
132 $title = Title
::newFromText( $name );
133 if ( is_null( $title ) ) {
134 throw new MWException( "Can't export invalid title" );
136 return $this->pageByTitle( $title );
140 public function pagesByName( $names ) {
141 foreach ( $names as $name ) {
142 $this->pageByName( $name );
146 public function allLogs() {
147 return $this->dumpFrom( '' );
150 public function logsByRange( $start, $end ) {
151 $condition = 'log_id >= ' . intval( $start );
153 $condition .= ' AND log_id < ' . intval( $end );
155 return $this->dumpFrom( $condition );
158 # Generates the distinct list of authors of an article
159 # Not called by default (depends on $this->list_authors)
160 # Can be set by Special:Export when not exporting whole history
161 protected function do_list_authors( $cond ) {
162 wfProfileIn( __METHOD__
);
163 $this->author_list
= "<contributors>";
166 $res = $this->db
->select(
167 array( 'page', 'revision' ),
168 array( 'DISTINCT rev_user_text', 'rev_user' ),
170 $this->db
->bitAnd( 'rev_deleted', Revision
::DELETED_USER
) . ' = 0',
177 foreach ( $res as $row ) {
178 $this->author_list
.= "<contributor>" .
180 htmlentities( $row->rev_user_text
) .
187 $this->author_list
.= "</contributors>";
188 wfProfileOut( __METHOD__
);
191 protected function dumpFrom( $cond = '' ) {
192 wfProfileIn( __METHOD__
);
193 # For logging dumps...
194 if ( $this->history
& self
::LOGS
) {
195 if ( $this->buffer
== WikiExporter
::STREAM
) {
196 $prev = $this->db
->bufferResults( false );
198 $where = array( 'user_id = log_user' );
200 $hideLogs = LogEventsList
::getExcludeClause( $this->db
);
201 if ( $hideLogs ) $where[] = $hideLogs;
202 # Add on any caller specified conditions
203 if ( $cond ) $where[] = $cond;
204 # Get logging table name for logging.* clause
205 $logging = $this->db
->tableName( 'logging' );
206 $result = $this->db
->select( array( 'logging', 'user' ),
207 array( "{$logging}.*", 'user_name' ), // grab the user name
210 array( 'ORDER BY' => 'log_id', 'USE INDEX' => array( 'logging' => 'PRIMARY' ) )
212 $wrapper = $this->db
->resultObject( $result );
213 $this->outputLogStream( $wrapper );
214 if ( $this->buffer
== WikiExporter
::STREAM
) {
215 $this->db
->bufferResults( $prev );
219 $tables = array( 'page', 'revision' );
220 $opts = array( 'ORDER BY' => 'page_id ASC' );
221 $opts['USE INDEX'] = array();
223 if ( is_array( $this->history
) ) {
224 # Time offset/limit for all pages/history...
225 $revJoin = 'page_id=rev_page';
227 if ( $this->history
['dir'] == 'asc' ) {
229 $opts['ORDER BY'] = 'rev_timestamp ASC';
232 $opts['ORDER BY'] = 'rev_timestamp DESC';
235 if ( !empty( $this->history
['offset'] ) ) {
236 $revJoin .= " AND rev_timestamp $op " .
237 $this->db
->addQuotes( $this->db
->timestamp( $this->history
['offset'] ) );
239 $join['revision'] = array( 'INNER JOIN', $revJoin );
241 if ( !empty( $this->history
['limit'] ) ) {
242 $opts['LIMIT'] = intval( $this->history
['limit'] );
244 } elseif ( $this->history
& WikiExporter
::FULL
) {
245 # Full history dumps...
246 $join['revision'] = array( 'INNER JOIN', 'page_id=rev_page' );
247 } elseif ( $this->history
& WikiExporter
::CURRENT
) {
248 # Latest revision dumps...
249 if ( $this->list_authors
&& $cond != '' ) { // List authors, if so desired
250 $this->do_list_authors( $cond );
252 $join['revision'] = array( 'INNER JOIN', 'page_id=rev_page AND page_latest=rev_id' );
253 } elseif ( $this->history
& WikiExporter
::STABLE
) {
254 # "Stable" revision dumps...
255 # Default JOIN, to be overridden...
256 $join['revision'] = array( 'INNER JOIN', 'page_id=rev_page AND page_latest=rev_id' );
257 # One, and only one hook should set this, and return false
258 if ( wfRunHooks( 'WikiExporter::dumpStableQuery', array( &$tables, &$opts, &$join ) ) ) {
259 wfProfileOut( __METHOD__
);
260 throw new MWException( __METHOD__
. " given invalid history dump type." );
263 # Uknown history specification parameter?
264 wfProfileOut( __METHOD__
);
265 throw new MWException( __METHOD__
. " given invalid history dump type." );
267 # Query optimization hacks
269 $opts[] = 'STRAIGHT_JOIN';
270 $opts['USE INDEX']['page'] = 'PRIMARY';
272 # Build text join options
273 if ( $this->text
!= WikiExporter
::STUB
) { // 1-pass
275 $join['text'] = array( 'INNER JOIN', 'rev_text_id=old_id' );
278 if ( $this->buffer
== WikiExporter
::STREAM
) {
279 $prev = $this->db
->bufferResults( false );
282 wfRunHooks( 'ModifyExportQuery',
283 array( $this->db
, &$tables, &$cond, &$opts, &$join ) );
286 $result = $this->db
->select( $tables, '*', $cond, __METHOD__
, $opts, $join );
287 $wrapper = $this->db
->resultObject( $result );
288 # Output dump results
289 $this->outputPageStream( $wrapper );
290 if ( $this->list_authors
) {
291 $this->outputPageStream( $wrapper );
294 if ( $this->buffer
== WikiExporter
::STREAM
) {
295 $this->db
->bufferResults( $prev );
298 wfProfileOut( __METHOD__
);
302 * Runs through a query result set dumping page and revision records.
303 * The result set should be sorted/grouped by page to avoid duplicate
304 * page records in the output.
306 * The result set will be freed once complete. Should be safe for
307 * streaming (non-buffered) queries, as long as it was made on a
308 * separate database connection not managed by LoadBalancer; some
309 * blob storage types will make queries to pull source data.
311 * @param $resultset ResultWrapper
313 protected function outputPageStream( $resultset ) {
315 foreach ( $resultset as $row ) {
316 if ( is_null( $last ) ||
317 $last->page_namespace
!= $row->page_namespace ||
318 $last->page_title
!= $row->page_title
) {
319 if ( isset( $last ) ) {
321 if ( $this->dumpUploads
) {
322 $output .= $this->writer
->writeUploads( $last, $this->dumpUploadFileContents
);
324 $output .= $this->writer
->closePage();
325 $this->sink
->writeClosePage( $output );
327 $output = $this->writer
->openPage( $row );
328 $this->sink
->writeOpenPage( $row, $output );
331 $output = $this->writer
->writeRevision( $row );
332 $this->sink
->writeRevision( $row, $output );
334 if ( isset( $last ) ) {
336 if ( $this->dumpUploads
) {
337 $output .= $this->writer
->writeUploads( $last, $this->dumpUploadFileContents
);
339 $output .= $this->author_list
;
340 $output .= $this->writer
->closePage();
341 $this->sink
->writeClosePage( $output );
345 protected function outputLogStream( $resultset ) {
346 foreach ( $resultset as $row ) {
347 $output = $this->writer
->writeLogItem( $row );
348 $this->sink
->writeLogItem( $row, $output );
356 class XmlDumpWriter
{
357 protected $firstPageWritten = 0, $lastPageWritten = 0, $pageInProgress = 0;
360 * Returns the export schema version.
363 function schemaVersion() {
368 * Opens the XML output stream's root <mediawiki> element.
369 * This does not include an xml directive, so is safe to include
370 * as a subelement in a larger XML stream. Namespace and XML Schema
371 * references are included.
373 * Output will be encoded in UTF-8.
377 function openStream() {
378 global $wgLanguageCode;
379 $ver = $this->schemaVersion();
380 return Xml
::element( 'mediawiki', array(
381 'xmlns' => "http://www.mediawiki.org/xml/export-$ver/",
382 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
383 'xsi:schemaLocation' => "http://www.mediawiki.org/xml/export-$ver/ " .
384 "http://www.mediawiki.org/xml/export-$ver.xsd",
386 'xml:lang' => $wgLanguageCode ),
392 function siteInfo() {
397 $this->caseSetting(),
398 $this->namespaces() );
399 return " <siteinfo>\n " .
400 implode( "\n ", $info ) .
404 function sitename() {
406 return Xml
::element( 'sitename', array(), $wgSitename );
409 function generator() {
411 return Xml
::element( 'generator', array(), "MediaWiki $wgVersion" );
414 function homelink() {
415 return Xml
::element( 'base', array(), Title
::newMainPage()->getCanonicalUrl() );
418 function caseSetting() {
419 global $wgCapitalLinks;
420 // "case-insensitive" option is reserved for future
421 $sensitivity = $wgCapitalLinks ?
'first-letter' : 'case-sensitive';
422 return Xml
::element( 'case', array(), $sensitivity );
425 function namespaces() {
427 $spaces = "<namespaces>\n";
428 foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
430 Xml
::element( 'namespace',
432 'case' => MWNamespace
::isCapitalized( $ns ) ?
'first-letter' : 'case-sensitive',
435 $spaces .= " </namespaces>";
440 * Closes the output stream with the closing root element.
441 * Call when finished dumping things.
445 function closeStream() {
446 return "</mediawiki>\n";
450 * Opens a <page> section on the output stream, with data
451 * from the given database row.
457 function openPage( $row ) {
459 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
460 $out .= ' ' . Xml
::elementClean( 'title', array(), $title->getPrefixedText() ) . "\n";
461 $out .= ' ' . Xml
::element( 'id', array(), strval( $row->page_id
) ) . "\n";
462 $this->pageInProgress
= $row->page_id
;
463 if ( $row->page_is_redirect
) {
464 $out .= ' ' . Xml
::element( 'redirect', array() ) . "\n";
466 if ( $row->page_restrictions
!= '' ) {
467 $out .= ' ' . Xml
::element( 'restrictions', array(),
468 strval( $row->page_restrictions
) ) . "\n";
471 wfRunHooks( 'XmlDumpWriterOpenPage', array( $this, &$out, $row, $title ) );
477 * Closes a <page> section on the output stream.
481 function closePage() {
483 if ( !$this->firstPageWritten
) {
484 $this->firstPageWritten
= $this->pageInProgress
;
486 $this->lastPageWritten
= $this->pageInProgress
;
490 * Dumps a <revision> section on the output stream, with
491 * data filled in from the given database row.
497 function writeRevision( $row ) {
498 wfProfileIn( __METHOD__
);
500 $out = " <revision>\n";
501 $out .= " " . Xml
::element( 'id', null, strval( $row->rev_id
) ) . "\n";
503 $out .= $this->writeTimestamp( $row->rev_timestamp
);
505 if ( $row->rev_deleted
& Revision
::DELETED_USER
) {
506 $out .= " " . Xml
::element( 'contributor', array( 'deleted' => 'deleted' ) ) . "\n";
508 $out .= $this->writeContributor( $row->rev_user
, $row->rev_user_text
);
511 if ( $row->rev_minor_edit
) {
512 $out .= " <minor/>\n";
514 if ( $row->rev_deleted
& Revision
::DELETED_COMMENT
) {
515 $out .= " " . Xml
::element( 'comment', array( 'deleted' => 'deleted' ) ) . "\n";
516 } elseif ( $row->rev_comment
!= '' ) {
517 $out .= " " . Xml
::elementClean( 'comment', null, strval( $row->rev_comment
) ) . "\n";
521 if ( $row->rev_deleted
& Revision
::DELETED_TEXT
) {
522 $out .= " " . Xml
::element( 'text', array( 'deleted' => 'deleted' ) ) . "\n";
523 } elseif ( isset( $row->old_text
) ) {
524 // Raw text from the database may have invalid chars
525 $text = strval( Revision
::getRevisionText( $row ) );
526 $out .= " " . Xml
::elementClean( 'text',
527 array( 'xml:space' => 'preserve', 'bytes' => $row->rev_len
),
528 strval( $text ) ) . "\n";
531 $out .= " " . Xml
::element( 'text',
532 array( 'id' => $row->rev_text_id
, 'bytes' => $row->rev_len
),
536 wfRunHooks( 'XmlDumpWriterWriteRevision', array( &$this, &$out, $row, $text ) );
538 $out .= " </revision>\n";
540 wfProfileOut( __METHOD__
);
545 * Dumps a <logitem> section on the output stream, with
546 * data filled in from the given database row.
552 function writeLogItem( $row ) {
553 wfProfileIn( __METHOD__
);
555 $out = " <logitem>\n";
556 $out .= " " . Xml
::element( 'id', null, strval( $row->log_id
) ) . "\n";
558 $out .= $this->writeTimestamp( $row->log_timestamp
);
560 if ( $row->log_deleted
& LogPage
::DELETED_USER
) {
561 $out .= " " . Xml
::element( 'contributor', array( 'deleted' => 'deleted' ) ) . "\n";
563 $out .= $this->writeContributor( $row->log_user
, $row->user_name
);
566 if ( $row->log_deleted
& LogPage
::DELETED_COMMENT
) {
567 $out .= " " . Xml
::element( 'comment', array( 'deleted' => 'deleted' ) ) . "\n";
568 } elseif ( $row->log_comment
!= '' ) {
569 $out .= " " . Xml
::elementClean( 'comment', null, strval( $row->log_comment
) ) . "\n";
572 $out .= " " . Xml
::element( 'type', null, strval( $row->log_type
) ) . "\n";
573 $out .= " " . Xml
::element( 'action', null, strval( $row->log_action
) ) . "\n";
575 if ( $row->log_deleted
& LogPage
::DELETED_ACTION
) {
576 $out .= " " . Xml
::element( 'text', array( 'deleted' => 'deleted' ) ) . "\n";
578 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
579 $out .= " " . Xml
::elementClean( 'logtitle', null, $title->getPrefixedText() ) . "\n";
580 $out .= " " . Xml
::elementClean( 'params',
581 array( 'xml:space' => 'preserve' ),
582 strval( $row->log_params
) ) . "\n";
585 $out .= " </logitem>\n";
587 wfProfileOut( __METHOD__
);
591 function writeTimestamp( $timestamp ) {
592 $ts = wfTimestamp( TS_ISO_8601
, $timestamp );
593 return " " . Xml
::element( 'timestamp', null, $ts ) . "\n";
596 function writeContributor( $id, $text ) {
597 $out = " <contributor>\n";
599 $out .= " " . Xml
::elementClean( 'username', null, strval( $text ) ) . "\n";
600 $out .= " " . Xml
::element( 'id', null, strval( $id ) ) . "\n";
602 $out .= " " . Xml
::elementClean( 'ip', null, strval( $text ) ) . "\n";
604 $out .= " </contributor>\n";
609 * Warning! This data is potentially inconsistent. :(
611 function writeUploads( $row, $dumpContents = false ) {
612 if ( $row->page_namespace
== NS_IMAGE
) {
613 $img = wfLocalFile( $row->page_title
);
614 if ( $img && $img->exists() ) {
616 foreach ( array_reverse( $img->getHistory() ) as $ver ) {
617 $out .= $this->writeUpload( $ver, $dumpContents );
619 $out .= $this->writeUpload( $img, $dumpContents );
628 * @param $dumpContents bool
631 function writeUpload( $file, $dumpContents = false ) {
632 if ( $file->isOld() ) {
634 Xml
::element( 'archivename', null, $file->getArchiveName() ) . "\n";
638 if ( $dumpContents ) {
639 # Dump file as base64
640 # Uses only XML-safe characters, so does not need escaping
641 $contents = ' <contents encoding="base64">' .
642 chunk_split( base64_encode( file_get_contents( $file->getPath() ) ) ) .
647 return " <upload>\n" .
648 $this->writeTimestamp( $file->getTimestamp() ) .
649 $this->writeContributor( $file->getUser( 'id' ), $file->getUser( 'text' ) ) .
650 " " . Xml
::elementClean( 'comment', null, $file->getDescription() ) . "\n" .
651 " " . Xml
::element( 'filename', null, $file->getName() ) . "\n" .
653 " " . Xml
::element( 'src', null, $file->getCanonicalUrl() ) . "\n" .
654 " " . Xml
::element( 'size', null, $file->getSize() ) . "\n" .
655 " " . Xml
::element( 'sha1base36', null, $file->getSha1() ) . "\n" .
656 " " . Xml
::element( 'rel', null, $file->getRel() ) . "\n" .
665 * Base class for output stream; prints to stdout or buffer or whereever.
669 function writeOpenStream( $string ) {
670 $this->write( $string );
673 function writeCloseStream( $string ) {
674 $this->write( $string );
677 function writeOpenPage( $page, $string ) {
678 $this->write( $string );
681 function writeClosePage( $string ) {
682 $this->write( $string );
685 function writeRevision( $rev, $string ) {
686 $this->write( $string );
689 function writeLogItem( $rev, $string ) {
690 $this->write( $string );
694 * Override to write to a different stream type.
697 function write( $string ) {
702 * Close the old file, move it to a specified name,
703 * and reopen new file with the old name. Use this
704 * for writing out a file in multiple pieces
705 * at specified checkpoints (e.g. every n hours).
706 * @param $newname mixed File name. May be a string or an array with one element
708 function closeRenameAndReopen( $newname ) {
713 function closeAndRename( $newname ) {
718 function rename( $newname ) {
723 function getFilename() {
729 * Stream outputter to send data to a file.
732 class DumpFileOutput
extends DumpOutput
{
736 function __construct( $file ) {
737 $this->handle
= fopen( $file, "wt" );
738 $this->filename
= $file;
741 function write( $string ) {
742 fputs( $this->handle
, $string );
745 function closeRenameAndReopen( $newname ) {
746 if ( is_array( $newname ) ) {
747 if ( count( $newname ) > 1 ) {
748 throw new MWException( __METHOD__
. ": passed multiple arguments for rename of single file\n" );
750 $newname = $newname[0];
754 fclose( $this->handle
);
755 rename( $this->filename
, $newname );
756 $this->handle
= fopen( $this->filename
, "wt" );
760 function closeAndRename( $newname ) {
761 if ( is_array( $newname ) ) {
762 if ( count( $newname ) > 1 ) {
763 throw new MWException( __METHOD__
. ": passed multiple arguments for rename of single file\n" );
765 $newname = $newname[0];
769 fclose( $this->handle
);
770 rename( $this->filename
, $newname );
774 function rename( $newname ) {
775 if ( is_array( $newname ) ) {
776 if ( count( $newname ) > 1 ) {
777 throw new MWException( __METHOD__
. ": passed multiple arguments for rename of single file\n" );
779 $newname = $newname[0];
783 rename( $this->filename
, $newname );
787 function getFilename() {
788 return $this->filename
;
793 * Stream outputter to send data to a file via some filter program.
794 * Even if compression is available in a library, using a separate
795 * program can allow us to make use of a multi-processor system.
798 class DumpPipeOutput
extends DumpFileOutput
{
799 protected $command, $filename;
801 function __construct( $command, $file = null ) {
802 if ( !is_null( $file ) ) {
803 $command .= " > " . wfEscapeShellArg( $file );
806 $this->startCommand( $command );
807 $this->command
= $command;
808 $this->filename
= $file;
811 function startCommand( $command ) {
813 0 => array( "pipe", "r" ),
816 $this->procOpenResource
= proc_open( $command, $spec, $pipes );
817 $this->handle
= $pipes[0];
821 * Close the old file, move it to a specified name,
822 * and reopen new file with the old name.
824 function closeRenameAndReopen( $newname ) {
825 if ( is_array( $newname ) ) {
826 if ( count( $newname ) > 1 ) {
827 throw new MWException( __METHOD__
. ": passed multiple arguments for rename of single file\n" );
829 $newname = $newname[0];
833 fclose( $this->handle
);
834 proc_close( $this->procOpenResource
);
835 rename( $this->filename
, $newname );
836 $command = $this->command
;
837 $command .= " > " . wfEscapeShellArg( $this->filename
);
838 $this->startCommand( $command );
842 function closeAndRename( $newname ) {
843 if ( is_array( $newname ) ) {
844 if ( count( $newname ) > 1 ) {
845 throw new MWException( __METHOD__
. ": passed multiple arguments for rename of single file\n" );
847 $newname = $newname[0];
851 # pclose( $this->handle );
852 fclose( $this->handle
);
853 proc_close( $this->procOpenResource
);
854 rename( $this->filename
, $newname );
858 function rename( $newname ) {
859 if ( is_array( $newname ) ) {
860 if ( count( $newname ) > 1 ) {
861 throw new MWException( __METHOD__
. ": passed multiple arguments for rename of single file\n" );
863 $newname = $newname[0];
867 rename( $this->filename
, $newname );
873 * Sends dump output via the gzip compressor.
876 class DumpGZipOutput
extends DumpPipeOutput
{
877 function __construct( $file ) {
878 parent
::__construct( "gzip", $file );
883 * Sends dump output via the bgzip2 compressor.
886 class DumpBZip2Output
extends DumpPipeOutput
{
887 function __construct( $file ) {
888 parent
::__construct( "bzip2", $file );
893 * Sends dump output via the p7zip compressor.
896 class Dump7ZipOutput
extends DumpPipeOutput
{
899 function __construct( $file ) {
900 $command = "7za a -bd -si " . wfEscapeShellArg( $file );
901 // Suppress annoying useless crap from p7zip
902 // Unfortunately this could suppress real error messages too
903 $command .= ' >' . wfGetNull() . ' 2>&1';
904 parent
::__construct( $command );
905 $this->filename
= $file;
908 function closeRenameAndReopen( $newname ) {
909 if ( is_array( $newname ) ) {
910 if ( count( $newname ) > 1 ) {
911 throw new MWException( __METHOD__
. ": passed multiple arguments for rename of single file\n" );
914 $newname = $newname[0];
918 fclose( $this->handle
);
919 proc_close( $this->procOpenResource
);
920 rename( $this->filename
, $newname );
921 $command = "7za a -bd -si " . wfEscapeShellArg( $file );
922 $command .= ' >' . wfGetNull() . ' 2>&1';
923 $this->startCommand( $command );
927 function closeAndRename( $newname ) {
928 if ( is_array( $newname ) ) {
929 if ( count( $newname ) > 1 ) {
930 throw new MWException( __METHOD__
. ": passed multiple arguments for rename of single file\n" );
932 $newname = $newname[0];
936 fclose( $this->handle
);
937 proc_close( $this->procOpenResource
);
938 rename( $this->filename
, $newname );
942 function rename( $newname ) {
943 if ( is_array( $newname ) ) {
944 if ( count( $newname ) > 1 ) {
945 throw new MWException( __METHOD__
. ": passed multiple arguments for rename of single file\n" );
948 $newname = $newname[0];
952 rename( $this->filename
, $newname );
960 * Dump output filter class.
961 * This just does output filtering and streaming; XML formatting is done
962 * higher up, so be careful in what you do.
966 function __construct( &$sink ) {
967 $this->sink
=& $sink;
970 function writeOpenStream( $string ) {
971 $this->sink
->writeOpenStream( $string );
974 function writeCloseStream( $string ) {
975 $this->sink
->writeCloseStream( $string );
978 function writeOpenPage( $page, $string ) {
979 $this->sendingThisPage
= $this->pass( $page, $string );
980 if ( $this->sendingThisPage
) {
981 $this->sink
->writeOpenPage( $page, $string );
985 function writeClosePage( $string ) {
986 if ( $this->sendingThisPage
) {
987 $this->sink
->writeClosePage( $string );
988 $this->sendingThisPage
= false;
992 function writeRevision( $rev, $string ) {
993 if ( $this->sendingThisPage
) {
994 $this->sink
->writeRevision( $rev, $string );
998 function writeLogItem( $rev, $string ) {
999 $this->sink
->writeRevision( $rev, $string );
1002 function closeRenameAndReopen( $newname ) {
1003 $this->sink
->closeRenameAndReopen( $newname );
1006 function closeAndRename( $newname ) {
1007 $this->sink
->closeAndRename( $newname );
1010 function rename( $newname ) {
1011 $this->sink
->rename( $newname );
1014 function getFilename() {
1015 return $this->sink
->getFilename();
1019 * Override for page-based filter types.
1022 function pass( $page ) {
1028 * Simple dump output filter to exclude all talk pages.
1031 class DumpNotalkFilter
extends DumpFilter
{
1032 function pass( $page ) {
1033 return !MWNamespace
::isTalk( $page->page_namespace
);
1038 * Dump output filter to include or exclude pages in a given set of namespaces.
1041 class DumpNamespaceFilter
extends DumpFilter
{
1042 var $invert = false;
1043 var $namespaces = array();
1045 function __construct( &$sink, $param ) {
1046 parent
::__construct( $sink );
1049 "NS_MAIN" => NS_MAIN
,
1050 "NS_TALK" => NS_TALK
,
1051 "NS_USER" => NS_USER
,
1052 "NS_USER_TALK" => NS_USER_TALK
,
1053 "NS_PROJECT" => NS_PROJECT
,
1054 "NS_PROJECT_TALK" => NS_PROJECT_TALK
,
1055 "NS_FILE" => NS_FILE
,
1056 "NS_FILE_TALK" => NS_FILE_TALK
,
1057 "NS_IMAGE" => NS_IMAGE
, // NS_IMAGE is an alias for NS_FILE
1058 "NS_IMAGE_TALK" => NS_IMAGE_TALK
,
1059 "NS_MEDIAWIKI" => NS_MEDIAWIKI
,
1060 "NS_MEDIAWIKI_TALK" => NS_MEDIAWIKI_TALK
,
1061 "NS_TEMPLATE" => NS_TEMPLATE
,
1062 "NS_TEMPLATE_TALK" => NS_TEMPLATE_TALK
,
1063 "NS_HELP" => NS_HELP
,
1064 "NS_HELP_TALK" => NS_HELP_TALK
,
1065 "NS_CATEGORY" => NS_CATEGORY
,
1066 "NS_CATEGORY_TALK" => NS_CATEGORY_TALK
);
1068 if ( $param { 0 } == '!' ) {
1069 $this->invert
= true;
1070 $param = substr( $param, 1 );
1073 foreach ( explode( ',', $param ) as $key ) {
1074 $key = trim( $key );
1075 if ( isset( $constants[$key] ) ) {
1076 $ns = $constants[$key];
1077 $this->namespaces
[$ns] = true;
1078 } elseif ( is_numeric( $key ) ) {
1079 $ns = intval( $key );
1080 $this->namespaces
[$ns] = true;
1082 throw new MWException( "Unrecognized namespace key '$key'\n" );
1087 function pass( $page ) {
1088 $match = isset( $this->namespaces
[$page->page_namespace
] );
1089 return $this->invert
xor $match;
1095 * Dump output filter to include only the last revision in each page sequence.
1098 class DumpLatestFilter
extends DumpFilter
{
1099 var $page, $pageString, $rev, $revString;
1101 function writeOpenPage( $page, $string ) {
1102 $this->page
= $page;
1103 $this->pageString
= $string;
1106 function writeClosePage( $string ) {
1108 $this->sink
->writeOpenPage( $this->page
, $this->pageString
);
1109 $this->sink
->writeRevision( $this->rev
, $this->revString
);
1110 $this->sink
->writeClosePage( $string );
1113 $this->revString
= null;
1115 $this->pageString
= null;
1118 function writeRevision( $rev, $string ) {
1119 if ( $rev->rev_id
== $this->page
->page_latest
) {
1121 $this->revString
= $string;
1127 * Base class for output stream; prints to stdout or buffer or whereever.
1130 class DumpMultiWriter
{
1131 function __construct( $sinks ) {
1132 $this->sinks
= $sinks;
1133 $this->count
= count( $sinks );
1136 function writeOpenStream( $string ) {
1137 for ( $i = 0; $i < $this->count
; $i++
) {
1138 $this->sinks
[$i]->writeOpenStream( $string );
1142 function writeCloseStream( $string ) {
1143 for ( $i = 0; $i < $this->count
; $i++
) {
1144 $this->sinks
[$i]->writeCloseStream( $string );
1148 function writeOpenPage( $page, $string ) {
1149 for ( $i = 0; $i < $this->count
; $i++
) {
1150 $this->sinks
[$i]->writeOpenPage( $page, $string );
1154 function writeClosePage( $string ) {
1155 for ( $i = 0; $i < $this->count
; $i++
) {
1156 $this->sinks
[$i]->writeClosePage( $string );
1160 function writeRevision( $rev, $string ) {
1161 for ( $i = 0; $i < $this->count
; $i++
) {
1162 $this->sinks
[$i]->writeRevision( $rev, $string );
1166 function closeRenameAndReopen( $newnames ) {
1167 for ( $i = 0; $i < $this->count
; $i++
) {
1168 $this->sinks
[$i]->closeRenameAndReopen( $newnames[$i] );
1172 function closeAndRename( $newname ) {
1173 for ( $i = 0; $i < $this->count
; $i++
) {
1174 $this->sinks
[$i]->closeAndRename( $newnames[$i] );
1177 function rename( $newnames ) {
1178 for ( $i = 0; $i < $this->count
; $i++
) {
1179 $this->sinks
[$i]->rename( $newnames[$i] );
1183 function getFilename() {
1184 $filenames = array();
1185 for ( $i = 0; $i < $this->count
; $i++
) {
1186 $filenames[] = $this->sinks
[$i]->getFilename();
1193 function xmlsafe( $string ) {
1194 wfProfileIn( __FUNCTION__
);
1197 * The page may contain old data which has not been properly normalized.
1198 * Invalid UTF-8 sequences or forbidden control characters will make our
1199 * XML output invalid, so be sure to strip them out.
1201 $string = UtfNormal
::cleanUp( $string );
1203 $string = htmlspecialchars( $string );
1204 wfProfileOut( __FUNCTION__
);