a9f29cbc795c864442c47d2886a7eb745985a71f
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
53 * If using WikiExporter::STREAM to stream a large amount of data,
54 * provide a database connection which is not managed by
55 * LoadBalancer to read from: some history blob types will
56 * make additional queries to pull source data while the
57 * main query is still running.
60 * @param $history Mixed: one of WikiExporter::FULL, WikiExporter::CURRENT,
61 * WikiExporter::RANGE or WikiExporter::STABLE,
62 * or an associative array:
63 * offset: non-inclusive offset at which to start the query
64 * limit: maximum number of rows to return
65 * dir: "asc" or "desc" timestamp order
66 * @param $buffer Int: one of WikiExporter::BUFFER or WikiExporter::STREAM
67 * @param $text Int: one of WikiExporter::TEXT or WikiExporter::STUB
69 function __construct( &$db, $history = WikiExporter
::CURRENT
,
70 $buffer = WikiExporter
::BUFFER
, $text = WikiExporter
::TEXT
) {
72 $this->history
= $history;
73 $this->buffer
= $buffer;
74 $this->writer
= new XmlDumpWriter();
75 $this->sink
= new DumpOutput();
80 * Set the DumpOutput or DumpFilter object which will receive
81 * various row objects and XML output for filtering. Filters
82 * can be chained or used as callbacks.
86 public function setOutputSink( &$sink ) {
90 public function openStream() {
91 $output = $this->writer
->openStream();
92 $this->sink
->writeOpenStream( $output );
95 public function closeStream() {
96 $output = $this->writer
->closeStream();
97 $this->sink
->writeCloseStream( $output );
101 * Dumps a series of page and revision records for all pages
102 * in the database, either including complete history or only
103 * the most recent version.
105 public function allPages() {
106 return $this->dumpFrom( '' );
110 * Dumps a series of page and revision records for those pages
111 * in the database falling within the page_id range given.
112 * @param $start Int: inclusive lower limit (this id is included)
113 * @param $end Int: Exclusive upper limit (this id is not included)
114 * If 0, no upper limit.
116 public function pagesByRange( $start, $end ) {
117 $condition = 'page_id >= ' . intval( $start );
119 $condition .= ' AND page_id < ' . intval( $end );
121 return $this->dumpFrom( $condition );
125 * Dumps a series of page and revision records for those pages
126 * in the database with revisions falling within the rev_id range given.
127 * @param $start Int: inclusive lower limit (this id is included)
128 * @param $end Int: Exclusive upper limit (this id is not included)
129 * If 0, no upper limit.
131 public function revsByRange( $start, $end ) {
132 $condition = 'rev_id >= ' . intval( $start );
134 $condition .= ' AND rev_id < ' . intval( $end );
136 return $this->dumpFrom( $condition );
140 * @param $title Title
142 public function pageByTitle( $title ) {
143 return $this->dumpFrom(
144 'page_namespace=' . $title->getNamespace() .
145 ' AND page_title=' . $this->db
->addQuotes( $title->getDBkey() ) );
148 public function pageByName( $name ) {
149 $title = Title
::newFromText( $name );
150 if ( is_null( $title ) ) {
151 throw new MWException( "Can't export invalid title" );
153 return $this->pageByTitle( $title );
157 public function pagesByName( $names ) {
158 foreach ( $names as $name ) {
159 $this->pageByName( $name );
163 public function allLogs() {
164 return $this->dumpFrom( '' );
167 public function logsByRange( $start, $end ) {
168 $condition = 'log_id >= ' . intval( $start );
170 $condition .= ' AND log_id < ' . intval( $end );
172 return $this->dumpFrom( $condition );
175 # Generates the distinct list of authors of an article
176 # Not called by default (depends on $this->list_authors)
177 # Can be set by Special:Export when not exporting whole history
178 protected function do_list_authors( $cond ) {
179 wfProfileIn( __METHOD__
);
180 $this->author_list
= "<contributors>";
183 $res = $this->db
->select(
184 array( 'page', 'revision' ),
185 array( 'DISTINCT rev_user_text', 'rev_user' ),
187 $this->db
->bitAnd( 'rev_deleted', Revision
::DELETED_USER
) . ' = 0',
194 foreach ( $res as $row ) {
195 $this->author_list
.= "<contributor>" .
197 htmlentities( $row->rev_user_text
) .
204 $this->author_list
.= "</contributors>";
205 wfProfileOut( __METHOD__
);
208 protected function dumpFrom( $cond = '' ) {
209 wfProfileIn( __METHOD__
);
210 # For logging dumps...
211 if ( $this->history
& self
::LOGS
) {
212 if ( $this->buffer
== WikiExporter
::STREAM
) {
213 $prev = $this->db
->bufferResults( false );
215 $where = array( 'user_id = log_user' );
217 $hideLogs = LogEventsList
::getExcludeClause( $this->db
);
218 if ( $hideLogs ) $where[] = $hideLogs;
219 # Add on any caller specified conditions
220 if ( $cond ) $where[] = $cond;
221 # Get logging table name for logging.* clause
222 $logging = $this->db
->tableName( 'logging' );
223 $result = $this->db
->select( array( 'logging', 'user' ),
224 array( "{$logging}.*", 'user_name' ), // grab the user name
227 array( 'ORDER BY' => 'log_id', 'USE INDEX' => array( 'logging' => 'PRIMARY' ) )
229 $wrapper = $this->db
->resultObject( $result );
230 $this->outputLogStream( $wrapper );
231 if ( $this->buffer
== WikiExporter
::STREAM
) {
232 $this->db
->bufferResults( $prev );
236 $tables = array( 'page', 'revision' );
237 $opts = array( 'ORDER BY' => 'page_id ASC' );
238 $opts['USE INDEX'] = array();
240 if ( is_array( $this->history
) ) {
241 # Time offset/limit for all pages/history...
242 $revJoin = 'page_id=rev_page';
244 if ( $this->history
['dir'] == 'asc' ) {
246 $opts['ORDER BY'] = 'rev_timestamp ASC';
249 $opts['ORDER BY'] = 'rev_timestamp DESC';
252 if ( !empty( $this->history
['offset'] ) ) {
253 $revJoin .= " AND rev_timestamp $op " .
254 $this->db
->addQuotes( $this->db
->timestamp( $this->history
['offset'] ) );
256 $join['revision'] = array( 'INNER JOIN', $revJoin );
258 if ( !empty( $this->history
['limit'] ) ) {
259 $opts['LIMIT'] = intval( $this->history
['limit'] );
261 } elseif ( $this->history
& WikiExporter
::FULL
) {
262 # Full history dumps...
263 $join['revision'] = array( 'INNER JOIN', 'page_id=rev_page' );
264 } elseif ( $this->history
& WikiExporter
::CURRENT
) {
265 # Latest revision dumps...
266 if ( $this->list_authors
&& $cond != '' ) { // List authors, if so desired
267 $this->do_list_authors( $cond );
269 $join['revision'] = array( 'INNER JOIN', 'page_id=rev_page AND page_latest=rev_id' );
270 } elseif ( $this->history
& WikiExporter
::STABLE
) {
271 # "Stable" revision dumps...
272 # Default JOIN, to be overridden...
273 $join['revision'] = array( 'INNER JOIN', 'page_id=rev_page AND page_latest=rev_id' );
274 # One, and only one hook should set this, and return false
275 if ( wfRunHooks( 'WikiExporter::dumpStableQuery', array( &$tables, &$opts, &$join ) ) ) {
276 wfProfileOut( __METHOD__
);
277 throw new MWException( __METHOD__
. " given invalid history dump type." );
279 } elseif ( $this->history
& WikiExporter
::RANGE
) {
280 # Dump of revisions within a specified range
281 $join['revision'] = array( 'INNER JOIN', 'page_id=rev_page' );
282 $opts['ORDER BY'] = 'rev_page ASC, rev_id ASC';
284 # Uknown history specification parameter?
285 wfProfileOut( __METHOD__
);
286 throw new MWException( __METHOD__
. " given invalid history dump type." );
288 # Query optimization hacks
290 $opts[] = 'STRAIGHT_JOIN';
291 $opts['USE INDEX']['page'] = 'PRIMARY';
293 # Build text join options
294 if ( $this->text
!= WikiExporter
::STUB
) { // 1-pass
296 $join['text'] = array( 'INNER JOIN', 'rev_text_id=old_id' );
299 if ( $this->buffer
== WikiExporter
::STREAM
) {
300 $prev = $this->db
->bufferResults( false );
303 wfRunHooks( 'ModifyExportQuery',
304 array( $this->db
, &$tables, &$cond, &$opts, &$join ) );
307 $result = $this->db
->select( $tables, '*', $cond, __METHOD__
, $opts, $join );
308 $wrapper = $this->db
->resultObject( $result );
309 # Output dump results
310 $this->outputPageStream( $wrapper );
311 if ( $this->list_authors
) {
312 $this->outputPageStream( $wrapper );
315 if ( $this->buffer
== WikiExporter
::STREAM
) {
316 $this->db
->bufferResults( $prev );
319 wfProfileOut( __METHOD__
);
323 * Runs through a query result set dumping page and revision records.
324 * The result set should be sorted/grouped by page to avoid duplicate
325 * page records in the output.
327 * The result set will be freed once complete. Should be safe for
328 * streaming (non-buffered) queries, as long as it was made on a
329 * separate database connection not managed by LoadBalancer; some
330 * blob storage types will make queries to pull source data.
332 * @param $resultset ResultWrapper
334 protected function outputPageStream( $resultset ) {
336 foreach ( $resultset as $row ) {
337 if ( is_null( $last ) ||
338 $last->page_namespace
!= $row->page_namespace ||
339 $last->page_title
!= $row->page_title
) {
340 if ( isset( $last ) ) {
342 if ( $this->dumpUploads
) {
343 $output .= $this->writer
->writeUploads( $last, $this->dumpUploadFileContents
);
345 $output .= $this->writer
->closePage();
346 $this->sink
->writeClosePage( $output );
348 $output = $this->writer
->openPage( $row );
349 $this->sink
->writeOpenPage( $row, $output );
352 $output = $this->writer
->writeRevision( $row );
353 $this->sink
->writeRevision( $row, $output );
355 if ( isset( $last ) ) {
357 if ( $this->dumpUploads
) {
358 $output .= $this->writer
->writeUploads( $last, $this->dumpUploadFileContents
);
360 $output .= $this->author_list
;
361 $output .= $this->writer
->closePage();
362 $this->sink
->writeClosePage( $output );
366 protected function outputLogStream( $resultset ) {
367 foreach ( $resultset as $row ) {
368 $output = $this->writer
->writeLogItem( $row );
369 $this->sink
->writeLogItem( $row, $output );
377 class XmlDumpWriter
{
379 * Returns the export schema version.
382 function schemaVersion() {
387 * Opens the XML output stream's root <mediawiki> element.
388 * This does not include an xml directive, so is safe to include
389 * as a subelement in a larger XML stream. Namespace and XML Schema
390 * references are included.
392 * Output will be encoded in UTF-8.
396 function openStream() {
397 global $wgLanguageCode;
398 $ver = $this->schemaVersion();
399 return Xml
::element( 'mediawiki', array(
400 'xmlns' => "http://www.mediawiki.org/xml/export-$ver/",
401 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
402 'xsi:schemaLocation' => "http://www.mediawiki.org/xml/export-$ver/ " .
403 "http://www.mediawiki.org/xml/export-$ver.xsd",
405 'xml:lang' => $wgLanguageCode ),
411 function siteInfo() {
416 $this->caseSetting(),
417 $this->namespaces() );
418 return " <siteinfo>\n " .
419 implode( "\n ", $info ) .
423 function sitename() {
425 return Xml
::element( 'sitename', array(), $wgSitename );
428 function generator() {
430 return Xml
::element( 'generator', array(), "MediaWiki $wgVersion" );
433 function homelink() {
434 return Xml
::element( 'base', array(), Title
::newMainPage()->getCanonicalUrl() );
437 function caseSetting() {
438 global $wgCapitalLinks;
439 // "case-insensitive" option is reserved for future
440 $sensitivity = $wgCapitalLinks ?
'first-letter' : 'case-sensitive';
441 return Xml
::element( 'case', array(), $sensitivity );
444 function namespaces() {
446 $spaces = "<namespaces>\n";
447 foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
449 Xml
::element( 'namespace',
451 'case' => MWNamespace
::isCapitalized( $ns ) ?
'first-letter' : 'case-sensitive',
454 $spaces .= " </namespaces>";
459 * Closes the output stream with the closing root element.
460 * Call when finished dumping things.
464 function closeStream() {
465 return "</mediawiki>\n";
469 * Opens a <page> section on the output stream, with data
470 * from the given database row.
476 function openPage( $row ) {
478 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
479 $out .= ' ' . Xml
::elementClean( 'title', array(), $title->getPrefixedText() ) . "\n";
480 $out .= ' ' . Xml
::element( 'id', array(), strval( $row->page_id
) ) . "\n";
481 if ( $row->page_is_redirect
) {
482 $out .= ' ' . Xml
::element( 'redirect', array() ) . "\n";
484 if ( $row->page_restrictions
!= '' ) {
485 $out .= ' ' . Xml
::element( 'restrictions', array(),
486 strval( $row->page_restrictions
) ) . "\n";
489 wfRunHooks( 'XmlDumpWriterOpenPage', array( $this, &$out, $row, $title ) );
495 * Closes a <page> section on the output stream.
499 function closePage() {
504 * Dumps a <revision> section on the output stream, with
505 * data filled in from the given database row.
511 function writeRevision( $row ) {
512 wfProfileIn( __METHOD__
);
514 $out = " <revision>\n";
515 $out .= " " . Xml
::element( 'id', null, strval( $row->rev_id
) ) . "\n";
517 $out .= $this->writeTimestamp( $row->rev_timestamp
);
519 if ( $row->rev_deleted
& Revision
::DELETED_USER
) {
520 $out .= " " . Xml
::element( 'contributor', array( 'deleted' => 'deleted' ) ) . "\n";
522 $out .= $this->writeContributor( $row->rev_user
, $row->rev_user_text
);
525 if ( $row->rev_minor_edit
) {
526 $out .= " <minor/>\n";
528 if ( $row->rev_deleted
& Revision
::DELETED_COMMENT
) {
529 $out .= " " . Xml
::element( 'comment', array( 'deleted' => 'deleted' ) ) . "\n";
530 } elseif ( $row->rev_comment
!= '' ) {
531 $out .= " " . Xml
::elementClean( 'comment', null, strval( $row->rev_comment
) ) . "\n";
535 if ( $row->rev_deleted
& Revision
::DELETED_TEXT
) {
536 $out .= " " . Xml
::element( 'text', array( 'deleted' => 'deleted' ) ) . "\n";
537 } elseif ( isset( $row->old_text
) ) {
538 // Raw text from the database may have invalid chars
539 $text = strval( Revision
::getRevisionText( $row ) );
540 $out .= " " . Xml
::elementClean( 'text',
541 array( 'xml:space' => 'preserve', 'bytes' => $row->rev_len
),
542 strval( $text ) ) . "\n";
545 $out .= " " . Xml
::element( 'text',
546 array( 'id' => $row->rev_text_id
, 'bytes' => $row->rev_len
),
550 wfRunHooks( 'XmlDumpWriterWriteRevision', array( &$this, &$out, $row, $text ) );
552 $out .= " </revision>\n";
554 wfProfileOut( __METHOD__
);
559 * Dumps a <logitem> section on the output stream, with
560 * data filled in from the given database row.
566 function writeLogItem( $row ) {
567 wfProfileIn( __METHOD__
);
569 $out = " <logitem>\n";
570 $out .= " " . Xml
::element( 'id', null, strval( $row->log_id
) ) . "\n";
572 $out .= $this->writeTimestamp( $row->log_timestamp
);
574 if ( $row->log_deleted
& LogPage
::DELETED_USER
) {
575 $out .= " " . Xml
::element( 'contributor', array( 'deleted' => 'deleted' ) ) . "\n";
577 $out .= $this->writeContributor( $row->log_user
, $row->user_name
);
580 if ( $row->log_deleted
& LogPage
::DELETED_COMMENT
) {
581 $out .= " " . Xml
::element( 'comment', array( 'deleted' => 'deleted' ) ) . "\n";
582 } elseif ( $row->log_comment
!= '' ) {
583 $out .= " " . Xml
::elementClean( 'comment', null, strval( $row->log_comment
) ) . "\n";
586 $out .= " " . Xml
::element( 'type', null, strval( $row->log_type
) ) . "\n";
587 $out .= " " . Xml
::element( 'action', null, strval( $row->log_action
) ) . "\n";
589 if ( $row->log_deleted
& LogPage
::DELETED_ACTION
) {
590 $out .= " " . Xml
::element( 'text', array( 'deleted' => 'deleted' ) ) . "\n";
592 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
593 $out .= " " . Xml
::elementClean( 'logtitle', null, $title->getPrefixedText() ) . "\n";
594 $out .= " " . Xml
::elementClean( 'params',
595 array( 'xml:space' => 'preserve' ),
596 strval( $row->log_params
) ) . "\n";
599 $out .= " </logitem>\n";
601 wfProfileOut( __METHOD__
);
605 function writeTimestamp( $timestamp ) {
606 $ts = wfTimestamp( TS_ISO_8601
, $timestamp );
607 return " " . Xml
::element( 'timestamp', null, $ts ) . "\n";
610 function writeContributor( $id, $text ) {
611 $out = " <contributor>\n";
613 $out .= " " . Xml
::elementClean( 'username', null, strval( $text ) ) . "\n";
614 $out .= " " . Xml
::element( 'id', null, strval( $id ) ) . "\n";
616 $out .= " " . Xml
::elementClean( 'ip', null, strval( $text ) ) . "\n";
618 $out .= " </contributor>\n";
623 * Warning! This data is potentially inconsistent. :(
625 function writeUploads( $row, $dumpContents = false ) {
626 if ( $row->page_namespace
== NS_IMAGE
) {
627 $img = wfLocalFile( $row->page_title
);
628 if ( $img && $img->exists() ) {
630 foreach ( array_reverse( $img->getHistory() ) as $ver ) {
631 $out .= $this->writeUpload( $ver, $dumpContents );
633 $out .= $this->writeUpload( $img, $dumpContents );
642 * @param $dumpContents bool
645 function writeUpload( $file, $dumpContents = false ) {
646 if ( $file->isOld() ) {
648 Xml
::element( 'archivename', null, $file->getArchiveName() ) . "\n";
652 if ( $dumpContents ) {
653 # Dump file as base64
654 # Uses only XML-safe characters, so does not need escaping
655 $contents = ' <contents encoding="base64">' .
656 chunk_split( base64_encode( file_get_contents( $file->getPath() ) ) ) .
661 return " <upload>\n" .
662 $this->writeTimestamp( $file->getTimestamp() ) .
663 $this->writeContributor( $file->getUser( 'id' ), $file->getUser( 'text' ) ) .
664 " " . Xml
::elementClean( 'comment', null, $file->getDescription() ) . "\n" .
665 " " . Xml
::element( 'filename', null, $file->getName() ) . "\n" .
667 " " . Xml
::element( 'src', null, $file->getCanonicalUrl() ) . "\n" .
668 " " . Xml
::element( 'size', null, $file->getSize() ) . "\n" .
669 " " . Xml
::element( 'sha1base36', null, $file->getSha1() ) . "\n" .
670 " " . Xml
::element( 'rel', null, $file->getRel() ) . "\n" .
679 * Base class for output stream; prints to stdout or buffer or whereever.
683 function writeOpenStream( $string ) {
684 $this->write( $string );
687 function writeCloseStream( $string ) {
688 $this->write( $string );
691 function writeOpenPage( $page, $string ) {
692 $this->write( $string );
695 function writeClosePage( $string ) {
696 $this->write( $string );
699 function writeRevision( $rev, $string ) {
700 $this->write( $string );
703 function writeLogItem( $rev, $string ) {
704 $this->write( $string );
708 * Override to write to a different stream type.
711 function write( $string ) {
716 * Close the old file, move it to a specified name,
717 * and reopen new file with the old name. Use this
718 * for writing out a file in multiple pieces
719 * at specified checkpoints (e.g. every n hours).
720 * @param $newname mixed File name. May be a string or an array with one element
722 function closeRenameAndReopen( $newname ) {
727 * Close the old file, and move it to a specified name.
728 * Use this for the last piece of a file written out
729 * at specified checkpoints (e.g. every n hours).
730 * @param $newname mixed File name. May be a string or an array with one element
731 * @param $open bool If true, a new file with the old filename will be opened again for writing (default: false)
733 function closeAndRename( $newname, $open = false ) {
738 * Returns the name of the file or files which are
739 * being written to, if there are any.
741 function getFilenames() {
747 * Stream outputter to send data to a file.
750 class DumpFileOutput
extends DumpOutput
{
751 protected $handle, $filename;
753 function __construct( $file ) {
754 $this->handle
= fopen( $file, "wt" );
755 $this->filename
= $file;
758 function write( $string ) {
759 fputs( $this->handle
, $string );
762 function closeRenameAndReopen( $newname ) {
763 $this->closeAndRename( $newname, true );
766 function renameOrException( $newname ) {
767 if (! rename( $this->filename
, $newname ) ) {
768 throw new MWException( __METHOD__
. ": rename of file {$this->filename} to $newname failed\n" );
772 function checkRenameArgCount( $newname ) {
773 if ( is_array( $newname ) ) {
774 if ( count( $newname ) > 1 ) {
775 throw new MWException( __METHOD__
. ": passed multiple arguments for rename of single file\n" );
777 $newname = $newname[0];
783 function closeAndRename( $newname, $open = false ) {
784 $newname = $this->checkRenameArgCount( $newname );
786 fclose( $this->handle
);
787 $this->renameOrException( $newname );
789 $this->handle
= fopen( $this->filename
, "wt" );
794 function getFilenames() {
795 return $this->filename
;
800 * Stream outputter to send data to a file via some filter program.
801 * Even if compression is available in a library, using a separate
802 * program can allow us to make use of a multi-processor system.
805 class DumpPipeOutput
extends DumpFileOutput
{
806 protected $command, $filename;
808 function __construct( $command, $file = null ) {
809 if ( !is_null( $file ) ) {
810 $command .= " > " . wfEscapeShellArg( $file );
813 $this->startCommand( $command );
814 $this->command
= $command;
815 $this->filename
= $file;
818 function startCommand( $command ) {
820 0 => array( "pipe", "r" ),
823 $this->procOpenResource
= proc_open( $command, $spec, $pipes );
824 $this->handle
= $pipes[0];
827 function closeRenameAndReopen( $newname ) {
828 $this->closeAndRename( $newname, true );
831 function closeAndRename( $newname, $open = false ) {
832 $newname = $this->checkRenameArgCount( $newname );
834 fclose( $this->handle
);
835 proc_close( $this->procOpenResource
);
836 $this->renameOrException( $newname );
838 $command = $this->command
;
839 $command .= " > " . wfEscapeShellArg( $this->filename
);
840 $this->startCommand( $command );
848 * Sends dump output via the gzip compressor.
851 class DumpGZipOutput
extends DumpPipeOutput
{
852 function __construct( $file ) {
853 parent
::__construct( "gzip", $file );
858 * Sends dump output via the bgzip2 compressor.
861 class DumpBZip2Output
extends DumpPipeOutput
{
862 function __construct( $file ) {
863 parent
::__construct( "bzip2", $file );
868 * Sends dump output via the p7zip compressor.
871 class Dump7ZipOutput
extends DumpPipeOutput
{
874 function __construct( $file ) {
875 $command = $this->setup7zCommand( $file );
876 parent
::__construct( $command );
877 $this->filename
= $file;
880 function setup7zCommand( $file ) {
881 $command = "7za a -bd -si " . wfEscapeShellArg( $file );
882 // Suppress annoying useless crap from p7zip
883 // Unfortunately this could suppress real error messages too
884 $command .= ' >' . wfGetNull() . ' 2>&1';
888 function closeRenameAndReopen( $newname ) {
889 $this->closeAndRename( $newname, true );
892 function closeAndRename( $newname, $open = false ) {
893 $newname = $this->checkRenameArgCount( $newname );
895 fclose( $this->handle
);
896 proc_close( $this->procOpenResource
);
897 $this->renameOrException( $newname );
899 $command = $this->setup7zCommand( $file );
900 $this->startCommand( $command );
909 * Dump output filter class.
910 * This just does output filtering and streaming; XML formatting is done
911 * higher up, so be careful in what you do.
915 function __construct( &$sink ) {
916 $this->sink
=& $sink;
919 function writeOpenStream( $string ) {
920 $this->sink
->writeOpenStream( $string );
923 function writeCloseStream( $string ) {
924 $this->sink
->writeCloseStream( $string );
927 function writeOpenPage( $page, $string ) {
928 $this->sendingThisPage
= $this->pass( $page, $string );
929 if ( $this->sendingThisPage
) {
930 $this->sink
->writeOpenPage( $page, $string );
934 function writeClosePage( $string ) {
935 if ( $this->sendingThisPage
) {
936 $this->sink
->writeClosePage( $string );
937 $this->sendingThisPage
= false;
941 function writeRevision( $rev, $string ) {
942 if ( $this->sendingThisPage
) {
943 $this->sink
->writeRevision( $rev, $string );
947 function writeLogItem( $rev, $string ) {
948 $this->sink
->writeRevision( $rev, $string );
951 function closeRenameAndReopen( $newname ) {
952 $this->sink
->closeRenameAndReopen( $newname );
955 function closeAndRename( $newname, $open = false ) {
956 $this->sink
->closeAndRename( $newname, $open );
959 function getFilenames() {
960 return $this->sink
->getFilenames();
964 * Override for page-based filter types.
967 function pass( $page ) {
973 * Simple dump output filter to exclude all talk pages.
976 class DumpNotalkFilter
extends DumpFilter
{
977 function pass( $page ) {
978 return !MWNamespace
::isTalk( $page->page_namespace
);
983 * Dump output filter to include or exclude pages in a given set of namespaces.
986 class DumpNamespaceFilter
extends DumpFilter
{
988 var $namespaces = array();
990 function __construct( &$sink, $param ) {
991 parent
::__construct( $sink );
994 "NS_MAIN" => NS_MAIN
,
995 "NS_TALK" => NS_TALK
,
996 "NS_USER" => NS_USER
,
997 "NS_USER_TALK" => NS_USER_TALK
,
998 "NS_PROJECT" => NS_PROJECT
,
999 "NS_PROJECT_TALK" => NS_PROJECT_TALK
,
1000 "NS_FILE" => NS_FILE
,
1001 "NS_FILE_TALK" => NS_FILE_TALK
,
1002 "NS_IMAGE" => NS_IMAGE
, // NS_IMAGE is an alias for NS_FILE
1003 "NS_IMAGE_TALK" => NS_IMAGE_TALK
,
1004 "NS_MEDIAWIKI" => NS_MEDIAWIKI
,
1005 "NS_MEDIAWIKI_TALK" => NS_MEDIAWIKI_TALK
,
1006 "NS_TEMPLATE" => NS_TEMPLATE
,
1007 "NS_TEMPLATE_TALK" => NS_TEMPLATE_TALK
,
1008 "NS_HELP" => NS_HELP
,
1009 "NS_HELP_TALK" => NS_HELP_TALK
,
1010 "NS_CATEGORY" => NS_CATEGORY
,
1011 "NS_CATEGORY_TALK" => NS_CATEGORY_TALK
);
1013 if ( $param { 0 } == '!' ) {
1014 $this->invert
= true;
1015 $param = substr( $param, 1 );
1018 foreach ( explode( ',', $param ) as $key ) {
1019 $key = trim( $key );
1020 if ( isset( $constants[$key] ) ) {
1021 $ns = $constants[$key];
1022 $this->namespaces
[$ns] = true;
1023 } elseif ( is_numeric( $key ) ) {
1024 $ns = intval( $key );
1025 $this->namespaces
[$ns] = true;
1027 throw new MWException( "Unrecognized namespace key '$key'\n" );
1032 function pass( $page ) {
1033 $match = isset( $this->namespaces
[$page->page_namespace
] );
1034 return $this->invert
xor $match;
1040 * Dump output filter to include only the last revision in each page sequence.
1043 class DumpLatestFilter
extends DumpFilter
{
1044 var $page, $pageString, $rev, $revString;
1046 function writeOpenPage( $page, $string ) {
1047 $this->page
= $page;
1048 $this->pageString
= $string;
1051 function writeClosePage( $string ) {
1053 $this->sink
->writeOpenPage( $this->page
, $this->pageString
);
1054 $this->sink
->writeRevision( $this->rev
, $this->revString
);
1055 $this->sink
->writeClosePage( $string );
1058 $this->revString
= null;
1060 $this->pageString
= null;
1063 function writeRevision( $rev, $string ) {
1064 if ( $rev->rev_id
== $this->page
->page_latest
) {
1066 $this->revString
= $string;
1072 * Base class for output stream; prints to stdout or buffer or whereever.
1075 class DumpMultiWriter
{
1076 function __construct( $sinks ) {
1077 $this->sinks
= $sinks;
1078 $this->count
= count( $sinks );
1081 function writeOpenStream( $string ) {
1082 for ( $i = 0; $i < $this->count
; $i++
) {
1083 $this->sinks
[$i]->writeOpenStream( $string );
1087 function writeCloseStream( $string ) {
1088 for ( $i = 0; $i < $this->count
; $i++
) {
1089 $this->sinks
[$i]->writeCloseStream( $string );
1093 function writeOpenPage( $page, $string ) {
1094 for ( $i = 0; $i < $this->count
; $i++
) {
1095 $this->sinks
[$i]->writeOpenPage( $page, $string );
1099 function writeClosePage( $string ) {
1100 for ( $i = 0; $i < $this->count
; $i++
) {
1101 $this->sinks
[$i]->writeClosePage( $string );
1105 function writeRevision( $rev, $string ) {
1106 for ( $i = 0; $i < $this->count
; $i++
) {
1107 $this->sinks
[$i]->writeRevision( $rev, $string );
1111 function closeRenameAndReopen( $newnames ) {
1112 $this->closeAndRename( $newnames, true );
1115 function closeAndRename( $newnames, $open = false ) {
1116 for ( $i = 0; $i < $this->count
; $i++
) {
1117 $this->sinks
[$i]->closeAndRename( $newnames[$i], $open );
1121 function getFilenames() {
1122 $filenames = array();
1123 for ( $i = 0; $i < $this->count
; $i++
) {
1124 $filenames[] = $this->sinks
[$i]->getFilenames();
1131 function xmlsafe( $string ) {
1132 wfProfileIn( __FUNCTION__
);
1135 * The page may contain old data which has not been properly normalized.
1136 * Invalid UTF-8 sequences or forbidden control characters will make our
1137 * XML output invalid, so be sure to strip them out.
1139 $string = UtfNormal
::cleanUp( $string );
1141 $string = htmlspecialchars( $string );
1142 wfProfileOut( __FUNCTION__
);