2 # Copyright (C) 2003, 2005, 2006 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
22 * @subpackage SpecialPage
26 require_once( 'Revision.php' );
28 define( 'MW_EXPORT_FULL', 0 );
29 define( 'MW_EXPORT_CURRENT', 1 );
31 define( 'MW_EXPORT_BUFFER', 0 );
32 define( 'MW_EXPORT_STREAM', 1 );
34 define( 'MW_EXPORT_TEXT', 0 );
35 define( 'MW_EXPORT_STUB', 1 );
40 * @subpackage SpecialPage
44 var $list_authors = false ; # Return distinct author list (when not returning full history)
45 var $author_list = "" ;
48 * If using MW_EXPORT_STREAM to stream a large amount of data,
49 * provide a database connection which is not managed by
50 * LoadBalancer to read from: some history blob types will
51 * make additional queries to pull source data while the
52 * main query is still running.
55 * @param int $history one of MW_EXPORT_FULL or MW_EXPORT_CURRENT
56 * @param int $buffer one of MW_EXPORT_BUFFER or MW_EXPORT_STREAM
58 function WikiExporter( &$db, $history = MW_EXPORT_CURRENT
,
59 $buffer = MW_EXPORT_BUFFER
, $text = MW_EXPORT_TEXT
) {
61 $this->history
= $history;
62 $this->buffer
= $buffer;
63 $this->writer
= new XmlDumpWriter();
64 $this->sink
= new DumpOutput();
69 * Set the DumpOutput or DumpFilter object which will receive
70 * various row objects and XML output for filtering. Filters
71 * can be chained or used as callbacks.
73 * @param mixed $callback
75 function setOutputSink( &$sink ) {
79 function openStream() {
80 $output = $this->writer
->openStream();
81 $this->sink
->writeOpenStream( $output );
84 function closeStream() {
85 $output = $this->writer
->closeStream();
86 $this->sink
->writeCloseStream( $output );
90 * Dumps a series of page and revision records for all pages
91 * in the database, either including complete history or only
92 * the most recent version.
95 return $this->dumpFrom( '' );
99 * Dumps a series of page and revision records for those pages
100 * in the database falling within the page_id range given.
101 * @param int $start Inclusive lower limit (this id is included)
102 * @param int $end Exclusive upper limit (this id is not included)
103 * If 0, no upper limit.
105 function pagesByRange( $start, $end ) {
106 $condition = 'page_id >= ' . intval( $start );
108 $condition .= ' AND page_id < ' . intval( $end );
110 return $this->dumpFrom( $condition );
114 * @param Title $title
116 function pageByTitle( $title ) {
117 return $this->dumpFrom(
118 'page_namespace=' . $title->getNamespace() .
119 ' AND page_title=' . $this->db
->addQuotes( $title->getDbKey() ) );
122 function pageByName( $name ) {
123 $title = Title
::newFromText( $name );
124 if( is_null( $title ) ) {
125 return new WikiError( "Can't export invalid title" );
127 return $this->pageByTitle( $title );
131 function pagesByName( $names ) {
132 foreach( $names as $name ) {
133 $this->pageByName( $name );
138 // -------------------- private implementation below --------------------
140 # Generates the distinct list of authors of an article
141 # Not called by default (depends on $this->list_authors)
142 # Can be set by Special:Export when not exporting whole history
143 function do_list_authors ( $page , $revision , $cond ) {
144 $fname = "do_list_authors" ;
145 wfProfileIn( $fname );
146 $this->author_list
= "<contributors>";
147 $sql = "SELECT DISTINCT rev_user_text,rev_user FROM {$page},{$revision} WHERE page_id=rev_page AND " . $cond ;
148 $result = $this->db
->query( $sql, $fname );
149 $resultset = $this->db
->resultObject( $result );
150 while( $row = $resultset->fetchObject() ) {
151 $this->author_list
.= "<contributor>" .
153 htmlentities( $row->rev_user_text
) .
160 wfProfileOut( $fname );
161 $this->author_list
.= "</contributors>";
164 function dumpFrom( $cond = '' ) {
165 $fname = 'WikiExporter::dumpFrom';
166 wfProfileIn( $fname );
168 $page = $this->db
->tableName( 'page' );
169 $revision = $this->db
->tableName( 'revision' );
170 $text = $this->db
->tableName( 'text' );
172 if( $this->history
== MW_EXPORT_FULL
) {
173 $join = 'page_id=rev_page';
174 } elseif( $this->history
== MW_EXPORT_CURRENT
) {
175 if ( $this->list_authors
&& $cond != '' ) { // List authors, if so desired
176 $this->do_list_authors ( $page , $revision , $cond );
178 $join = 'page_id=rev_page AND page_latest=rev_id';
180 wfProfileOut( $fname );
181 return new WikiError( "$fname given invalid history dump type." );
183 $where = ( $cond == '' ) ?
'' : "$cond AND";
185 if( $this->buffer
== MW_EXPORT_STREAM
) {
186 $prev = $this->db
->bufferResults( false );
189 // Optimization hack for full-database dump
190 $pageindex = 'FORCE INDEX (PRIMARY)';
191 $revindex = 'FORCE INDEX (PRIMARY)';
192 $straight = ' /*! STRAIGHT_JOIN */ ';
198 if( $this->text
== MW_EXPORT_STUB
) {
199 $sql = "SELECT $straight * FROM
205 $sql = "SELECT $straight * FROM
209 WHERE $where $join AND rev_text_id=old_id
212 $result = $this->db
->query( $sql, $fname );
213 $wrapper = $this->db
->resultObject( $result );
214 $this->outputStream( $wrapper );
216 if ( $this->list_authors
) {
217 $this->outputStream( $wrapper );
220 if( $this->buffer
== MW_EXPORT_STREAM
) {
221 $this->db
->bufferResults( $prev );
224 wfProfileOut( $fname );
228 * Runs through a query result set dumping page and revision records.
229 * The result set should be sorted/grouped by page to avoid duplicate
230 * page records in the output.
232 * The result set will be freed once complete. Should be safe for
233 * streaming (non-buffered) queries, as long as it was made on a
234 * separate database connection not managed by LoadBalancer; some
235 * blob storage types will make queries to pull source data.
237 * @param ResultWrapper $resultset
240 function outputStream( $resultset ) {
242 while( $row = $resultset->fetchObject() ) {
243 if( is_null( $last ) ||
244 $last->page_namespace
!= $row->page_namespace ||
245 $last->page_title
!= $row->page_title
) {
246 if( isset( $last ) ) {
247 $output = $this->writer
->closePage();
248 $this->sink
->writeClosePage( $output );
250 $output = $this->writer
->openPage( $row );
251 $this->sink
->writeOpenPage( $row, $output );
254 $output = $this->writer
->writeRevision( $row );
255 $this->sink
->writeRevision( $row, $output );
257 if( isset( $last ) ) {
258 $output = $this->author_list
. $this->writer
->closePage();
259 $this->sink
->writeClosePage( $output );
265 class XmlDumpWriter
{
268 * Returns the export schema version.
271 function schemaVersion() {
272 return "0.3"; // FIXME: upgrade to 0.4 when updated XSD is ready, for the revision deletion bits
276 * Opens the XML output stream's root <mediawiki> element.
277 * This does not include an xml directive, so is safe to include
278 * as a subelement in a larger XML stream. Namespace and XML Schema
279 * references are included.
281 * Output will be encoded in UTF-8.
285 function openStream() {
286 global $wgContLanguageCode;
287 $ver = $this->schemaVersion();
288 return wfElement( 'mediawiki', array(
289 'xmlns' => "http://www.mediawiki.org/xml/export-$ver/",
290 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
291 'xsi:schemaLocation' => "http://www.mediawiki.org/xml/export-$ver/ " .
292 "http://www.mediawiki.org/xml/export-$ver.xsd",
294 'xml:lang' => $wgContLanguageCode ),
300 function siteInfo() {
305 $this->caseSetting(),
306 $this->namespaces() );
307 return " <siteinfo>\n " .
308 implode( "\n ", $info ) .
312 function sitename() {
314 return wfElement( 'sitename', array(), $wgSitename );
317 function generator() {
319 return wfElement( 'generator', array(), "MediaWiki $wgVersion" );
322 function homelink() {
323 $page = Title
::newFromText( wfMsgForContent( 'mainpage' ) );
324 return wfElement( 'base', array(), $page->getFullUrl() );
327 function caseSetting() {
328 global $wgCapitalLinks;
329 // "case-insensitive" option is reserved for future
330 $sensitivity = $wgCapitalLinks ?
'first-letter' : 'case-sensitive';
331 return wfElement( 'case', array(), $sensitivity );
334 function namespaces() {
336 $spaces = " <namespaces>\n";
337 foreach( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
338 $spaces .= ' ' . wfElement( 'namespace', array( 'key' => $ns ), $title ) . "\n";
340 $spaces .= " </namespaces>";
345 * Closes the output stream with the closing root element.
346 * Call when finished dumping things.
348 function closeStream() {
349 return "</mediawiki>\n";
354 * Opens a <page> section on the output stream, with data
355 * from the given database row.
361 function openPage( $row ) {
363 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
364 $out .= ' ' . wfElementClean( 'title', array(), $title->getPrefixedText() ) . "\n";
365 $out .= ' ' . wfElement( 'id', array(), strval( $row->page_id
) ) . "\n";
366 if( '' != $row->page_restrictions
) {
367 $out .= ' ' . wfElement( 'restrictions', array(),
368 strval( $row->page_restrictions
) ) . "\n";
374 * Closes a <page> section on the output stream.
378 function closePage() {
383 * Dumps a <revision> section on the output stream, with
384 * data filled in from the given database row.
390 function writeRevision( $row ) {
391 $fname = 'WikiExporter::dumpRev';
392 wfProfileIn( $fname );
394 $out = " <revision>\n";
395 $out .= " " . wfElement( 'id', null, strval( $row->rev_id
) ) . "\n";
397 $ts = wfTimestamp( TS_ISO_8601
, $row->rev_timestamp
);
398 $out .= " " . wfElement( 'timestamp', null, $ts ) . "\n";
400 if( $row->rev_deleted
& MW_REV_DELETED_USER
) {
401 $out .= " " . wfElement( 'contributor', array( 'deleted' => 'deleted' ) ) . "\n";
403 $out .= " <contributor>\n";
404 if( $row->rev_user
) {
405 $out .= " " . wfElementClean( 'username', null, strval( $row->rev_user_text
) ) . "\n";
406 $out .= " " . wfElement( 'id', null, strval( $row->rev_user
) ) . "\n";
408 $out .= " " . wfElementClean( 'ip', null, strval( $row->rev_user_text
) ) . "\n";
410 $out .= " </contributor>\n";
413 if( $row->rev_minor_edit
) {
414 $out .= " <minor/>\n";
416 if( $row->rev_deleted
& MW_REV_DELETED_COMMENT
) {
417 $out .= " " . wfElement( 'comment', array( 'deleted' => 'deleted' ) ) . "\n";
418 } elseif( $row->rev_comment
!= '' ) {
419 $out .= " " . wfElementClean( 'comment', null, strval( $row->rev_comment
) ) . "\n";
422 if( $row->rev_deleted
& MW_REV_DELETED_TEXT
) {
423 $out .= " " . wfElement( 'text', array( 'deleted' => 'deleted' ) ) . "\n";
424 } elseif( isset( $row->old_text
) ) {
425 // Raw text from the database may have invalid chars
426 $text = strval( Revision
::getRevisionText( $row ) );
427 $out .= " " . wfElementClean( 'text',
428 array( 'xml:space' => 'preserve' ),
429 strval( $text ) ) . "\n";
432 $out .= " " . wfElement( 'text',
433 array( 'id' => $row->rev_text_id
),
437 $out .= " </revision>\n";
439 wfProfileOut( $fname );
447 * Base class for output stream; prints to stdout or buffer or whereever.
450 function writeOpenStream( $string ) {
451 $this->write( $string );
454 function writeCloseStream( $string ) {
455 $this->write( $string );
458 function writeOpenPage( $page, $string ) {
459 $this->write( $string );
462 function writeClosePage( $string ) {
463 $this->write( $string );
466 function writeRevision( $rev, $string ) {
467 $this->write( $string );
471 * Override to write to a different stream type.
474 function write( $string ) {
480 * Stream outputter to send data to a file.
482 class DumpFileOutput
extends DumpOutput
{
485 function DumpFileOutput( $file ) {
486 $this->handle
= fopen( $file, "wt" );
489 function write( $string ) {
490 fputs( $this->handle
, $string );
495 * Stream outputter to send data to a file via some filter program.
496 * Even if compression is available in a library, using a separate
497 * program can allow us to make use of a multi-processor system.
499 class DumpPipeOutput
extends DumpFileOutput
{
500 function DumpPipeOutput( $command, $file = null ) {
501 if( !is_null( $file ) ) {
502 $command .= " > " . wfEscapeShellArg( $file );
504 $this->handle
= popen( $command, "w" );
509 * Sends dump output via the gzip compressor.
511 class DumpGZipOutput
extends DumpPipeOutput
{
512 function DumpGZipOutput( $file ) {
513 parent
::DumpPipeOutput( "gzip", $file );
518 * Sends dump output via the bgzip2 compressor.
520 class DumpBZip2Output
extends DumpPipeOutput
{
521 function DumpBZip2Output( $file ) {
522 parent
::DumpPipeOutput( "bzip2", $file );
527 * Sends dump output via the p7zip compressor.
529 class Dump7ZipOutput
extends DumpPipeOutput
{
530 function Dump7ZipOutput( $file ) {
531 $command = "7za a -bd -si " . wfEscapeShellArg( $file );
532 parent
::DumpPipeOutput( $command );
539 * Dump output filter class.
540 * This just does output filtering and streaming; XML formatting is done
541 * higher up, so be careful in what you do.
544 function DumpFilter( &$sink ) {
545 $this->sink
=& $sink;
548 function writeOpenStream( $string ) {
549 $this->sink
->writeOpenStream( $string );
552 function writeCloseStream( $string ) {
553 $this->sink
->writeCloseStream( $string );
556 function writeOpenPage( $page, $string ) {
557 $this->sendingThisPage
= $this->pass( $page, $string );
558 if( $this->sendingThisPage
) {
559 $this->sink
->writeOpenPage( $page, $string );
563 function writeClosePage( $string ) {
564 if( $this->sendingThisPage
) {
565 $this->sink
->writeClosePage( $string );
566 $this->sendingThisPage
= false;
570 function writeRevision( $rev, $string ) {
571 if( $this->sendingThisPage
) {
572 $this->sink
->writeRevision( $rev, $string );
577 * Override for page-based filter types.
580 function pass( $page, $string ) {
586 * Simple dump output filter to exclude all talk pages.
588 class DumpNotalkFilter
extends DumpFilter
{
589 function pass( $page ) {
590 return !Namespace::isTalk( $page->page_namespace
);
595 * Dump output filter to include or exclude pages in a given set of namespaces.
597 class DumpNamespaceFilter
extends DumpFilter
{
599 var $namespaces = array();
601 function DumpNamespaceFilter( &$sink, $param ) {
602 parent
::DumpFilter( $sink );
605 "NS_MAIN" => NS_MAIN
,
606 "NS_TALK" => NS_TALK
,
607 "NS_USER" => NS_USER
,
608 "NS_USER_TALK" => NS_USER_TALK
,
609 "NS_PROJECT" => NS_PROJECT
,
610 "NS_PROJECT_TALK" => NS_PROJECT_TALK
,
611 "NS_IMAGE" => NS_IMAGE
,
612 "NS_IMAGE_TALK" => NS_IMAGE_TALK
,
613 "NS_MEDIAWIKI" => NS_MEDIAWIKI
,
614 "NS_MEDIAWIKI_TALK" => NS_MEDIAWIKI_TALK
,
615 "NS_TEMPLATE" => NS_TEMPLATE
,
616 "NS_TEMPLATE_TALK" => NS_TEMPLATE_TALK
,
617 "NS_HELP" => NS_HELP
,
618 "NS_HELP_TALK" => NS_HELP_TALK
,
619 "NS_CATEGORY" => NS_CATEGORY
,
620 "NS_CATEGORY_TALK" => NS_CATEGORY_TALK
);
622 if( $param{0} == '!' ) {
623 $this->invert
= true;
624 $param = substr( $param, 1 );
627 foreach( explode( ',', $param ) as $key ) {
629 if( isset( $constants[$key] ) ) {
630 $ns = $constants[$key];
631 $this->namespaces
[$ns] = true;
632 } elseif( is_numeric( $key ) ) {
633 $ns = intval( $key );
634 $this->namespaces
[$ns] = true;
636 wfDie( "Unrecognized namespace key '$key'\n" );
641 function pass( $page ) {
642 $match = isset( $this->namespaces
[$page->page_namespace
] );
643 return $this->invert
xor $match;
649 * Dump output filter to include only the last revision in each page sequence.
651 class DumpLatestFilter
extends DumpFilter
{
652 var $page, $pageString, $rev, $revString;
654 function writeOpenPage( $page, $string ) {
656 $this->pageString
= $string;
659 function writeClosePage( $string ) {
661 $this->sink
->writeOpenPage( $this->page
, $this->pageString
);
662 $this->sink
->writeRevision( $this->rev
, $this->revString
);
663 $this->sink
->writeClosePage( $string );
666 $this->revString
= null;
668 $this->pageString
= null;
671 function writeRevision( $rev, $string ) {
672 if( $rev->rev_id
== $this->page
->page_latest
) {
674 $this->revString
= $string;
680 * Base class for output stream; prints to stdout or buffer or whereever.
682 class DumpMultiWriter
{
683 function DumpMultiWriter( $sinks ) {
684 $this->sinks
= $sinks;
685 $this->count
= count( $sinks );
688 function writeOpenStream( $string ) {
689 for( $i = 0; $i < $this->count
; $i++
) {
690 $this->sinks
[$i]->writeOpenStream( $string );
694 function writeCloseStream( $string ) {
695 for( $i = 0; $i < $this->count
; $i++
) {
696 $this->sinks
[$i]->writeCloseStream( $string );
700 function writeOpenPage( $page, $string ) {
701 for( $i = 0; $i < $this->count
; $i++
) {
702 $this->sinks
[$i]->writeOpenPage( $page, $string );
706 function writeClosePage( $string ) {
707 for( $i = 0; $i < $this->count
; $i++
) {
708 $this->sinks
[$i]->writeClosePage( $string );
712 function writeRevision( $rev, $string ) {
713 for( $i = 0; $i < $this->count
; $i++
) {
714 $this->sinks
[$i]->writeRevision( $rev, $string );
719 function xmlsafe( $string ) {
721 wfProfileIn( $fname );
724 * The page may contain old data which has not been properly normalized.
725 * Invalid UTF-8 sequences or forbidden control characters will make our
726 * XML output invalid, so be sure to strip them out.
728 $string = UtfNormal
::cleanUp( $string );
730 $string = htmlspecialchars( $string );
731 wfProfileOut( $fname );