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 * If using MW_EXPORT_STREAM to stream a large amount of data,
45 * provide a database connection which is not managed by
46 * LoadBalancer to read from: some history blob types will
47 * make additional queries to pull source data while the
48 * main query is still running.
51 * @param int $history one of MW_EXPORT_FULL or MW_EXPORT_CURRENT
52 * @param int $buffer one of MW_EXPORT_BUFFER or MW_EXPORT_STREAM
54 function WikiExporter( &$db, $history = MW_EXPORT_CURRENT
,
55 $buffer = MW_EXPORT_BUFFER
, $text = MW_EXPORT_TEXT
) {
57 $this->history
= $history;
58 $this->buffer
= $buffer;
59 $this->writer
= new XmlDumpWriter();
60 $this->sink
= new DumpOutput();
65 * Set the DumpOutput or DumpFilter object which will receive
66 * various row objects and XML output for filtering. Filters
67 * can be chained or used as callbacks.
69 * @param mixed $callback
71 function setOutputSink( &$sink ) {
75 function openStream() {
76 $output = $this->writer
->openStream();
77 $this->sink
->writeOpenStream( $output );
80 function closeStream() {
81 $output = $this->writer
->closeStream();
82 $this->sink
->writeCloseStream( $output );
86 * Dumps a series of page and revision records for all pages
87 * in the database, either including complete history or only
88 * the most recent version.
91 return $this->dumpFrom( '' );
95 * Dumps a series of page and revision records for those pages
96 * in the database falling within the page_id range given.
97 * @param int $start Inclusive lower limit (this id is included)
98 * @param int $end Exclusive upper limit (this id is not included)
99 * If 0, no upper limit.
101 function pagesByRange( $start, $end ) {
102 $condition = 'page_id >= ' . intval( $start );
104 $condition .= ' AND page_id < ' . intval( $end );
106 return $this->dumpFrom( $condition );
110 * @param Title $title
112 function pageByTitle( $title ) {
113 return $this->dumpFrom(
114 'page_namespace=' . $title->getNamespace() .
115 ' AND page_title=' . $this->db
->addQuotes( $title->getDbKey() ) );
118 function pageByName( $name ) {
119 $title = Title
::newFromText( $name );
120 if( is_null( $title ) ) {
121 return new WikiError( "Can't export invalid title" );
123 return $this->pageByTitle( $title );
127 function pagesByName( $names ) {
128 foreach( $names as $name ) {
129 $this->pageByName( $name );
134 // -------------------- private implementation below --------------------
136 function dumpFrom( $cond = '' ) {
137 $fname = 'WikiExporter::dumpFrom';
138 wfProfileIn( $fname );
140 $page = $this->db
->tableName( 'page' );
141 $revision = $this->db
->tableName( 'revision' );
142 $text = $this->db
->tableName( 'text' );
144 if( $this->history
== MW_EXPORT_FULL
) {
145 $join = 'page_id=rev_page';
146 } elseif( $this->history
== MW_EXPORT_CURRENT
) {
147 $join = 'page_id=rev_page AND page_latest=rev_id';
149 wfProfileOut( $fname );
150 return new WikiError( "$fname given invalid history dump type." );
152 $where = ( $cond == '' ) ?
'' : "$cond AND";
154 if( $this->buffer
== MW_EXPORT_STREAM
) {
155 $prev = $this->db
->bufferResults( false );
158 // Optimization hack for full-database dump
159 $pageindex = 'FORCE INDEX (PRIMARY)';
160 $revindex = 'FORCE INDEX(page_timestamp)';
165 if( $this->text
== MW_EXPORT_STUB
) {
166 $sql = "SELECT * FROM
172 $sql = "SELECT * FROM
176 WHERE $where $join AND rev_text_id=old_id
179 $result = $this->db
->query( $sql, $fname );
180 $wrapper = $this->db
->resultObject( $result );
181 $this->outputStream( $wrapper );
183 if( $this->buffer
== MW_EXPORT_STREAM
) {
184 $this->db
->bufferResults( $prev );
187 wfProfileOut( $fname );
191 * Runs through a query result set dumping page and revision records.
192 * The result set should be sorted/grouped by page to avoid duplicate
193 * page records in the output.
195 * The result set will be freed once complete. Should be safe for
196 * streaming (non-buffered) queries, as long as it was made on a
197 * separate database connection not managed by LoadBalancer; some
198 * blob storage types will make queries to pull source data.
200 * @param ResultWrapper $resultset
203 function outputStream( $resultset ) {
205 while( $row = $resultset->fetchObject() ) {
206 if( is_null( $last ) ||
207 $last->page_namespace
!= $row->page_namespace ||
208 $last->page_title
!= $row->page_title
) {
209 if( isset( $last ) ) {
210 $output = $this->writer
->closePage();
211 $this->sink
->writeClosePage( $output );
213 $output = $this->writer
->openPage( $row );
214 $this->sink
->writeOpenPage( $row, $output );
217 $output = $this->writer
->writeRevision( $row );
218 $this->sink
->writeRevision( $row, $output );
220 if( isset( $last ) ) {
221 $output = $this->writer
->closePage();
222 $this->sink
->writeClosePage( $output );
228 class XmlDumpWriter
{
231 * Returns the export schema version.
234 function schemaVersion() {
235 return "0.3"; // FIXME: upgrade to 0.4 when updated XSD is ready, for the revision deletion bits
239 * Opens the XML output stream's root <mediawiki> element.
240 * This does not include an xml directive, so is safe to include
241 * as a subelement in a larger XML stream. Namespace and XML Schema
242 * references are included.
244 * Output will be encoded in UTF-8.
248 function openStream() {
249 global $wgContLanguageCode;
250 $ver = $this->schemaVersion();
251 return wfElement( 'mediawiki', array(
252 'xmlns' => "http://www.mediawiki.org/xml/export-$ver/",
253 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
254 'xsi:schemaLocation' => "http://www.mediawiki.org/xml/export-$ver/ " .
255 "http://www.mediawiki.org/xml/export-$ver.xsd",
257 'xml:lang' => $wgContLanguageCode ),
263 function siteInfo() {
268 $this->caseSetting(),
269 $this->namespaces() );
270 return " <siteinfo>\n " .
271 implode( "\n ", $info ) .
275 function sitename() {
277 return wfElement( 'sitename', array(), $wgSitename );
280 function generator() {
282 return wfElement( 'generator', array(), "MediaWiki $wgVersion" );
285 function homelink() {
286 $page = Title
::newFromText( wfMsgForContent( 'mainpage' ) );
287 return wfElement( 'base', array(), $page->getFullUrl() );
290 function caseSetting() {
291 global $wgCapitalLinks;
292 // "case-insensitive" option is reserved for future
293 $sensitivity = $wgCapitalLinks ?
'first-letter' : 'case-sensitive';
294 return wfElement( 'case', array(), $sensitivity );
297 function namespaces() {
299 $spaces = " <namespaces>\n";
300 foreach( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
301 $spaces .= ' ' . wfElement( 'namespace', array( 'key' => $ns ), $title ) . "\n";
303 $spaces .= " </namespaces>";
308 * Closes the output stream with the closing root element.
309 * Call when finished dumping things.
311 function closeStream() {
312 return "</mediawiki>\n";
317 * Opens a <page> section on the output stream, with data
318 * from the given database row.
324 function openPage( $row ) {
326 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
327 $out .= ' ' . wfElementClean( 'title', array(), $title->getPrefixedText() ) . "\n";
328 $out .= ' ' . wfElement( 'id', array(), strval( $row->page_id
) ) . "\n";
329 if( '' != $row->page_restrictions
) {
330 $out .= ' ' . wfElement( 'restrictions', array(),
331 strval( $row->page_restrictions
) ) . "\n";
337 * Closes a <page> section on the output stream.
341 function closePage() {
346 * Dumps a <revision> section on the output stream, with
347 * data filled in from the given database row.
353 function writeRevision( $row ) {
354 $fname = 'WikiExporter::dumpRev';
355 wfProfileIn( $fname );
357 $out = " <revision>\n";
358 $out .= " " . wfElement( 'id', null, strval( $row->rev_id
) ) . "\n";
360 $ts = wfTimestamp( TS_ISO_8601
, $row->rev_timestamp
);
361 $out .= " " . wfElement( 'timestamp', null, $ts ) . "\n";
363 if( $row->rev_deleted
& MW_REV_DELETED_USER
) {
364 $out .= " " . wfElement( 'contributor', array( 'deleted' => 'deleted' ) ) . "\n";
366 $out .= " <contributor>\n";
367 if( $row->rev_user
) {
368 $out .= " " . wfElementClean( 'username', null, strval( $row->rev_user_text
) ) . "\n";
369 $out .= " " . wfElement( 'id', null, strval( $row->rev_user
) ) . "\n";
371 $out .= " " . wfElementClean( 'ip', null, strval( $row->rev_user_text
) ) . "\n";
373 $out .= " </contributor>\n";
376 if( $row->rev_minor_edit
) {
377 $out .= " <minor/>\n";
379 if( $row->rev_deleted
& MW_REV_DELETED_COMMENT
) {
380 $out .= " " . wfElement( 'comment', array( 'deleted' => 'deleted' ) ) . "\n";
381 } elseif( $row->rev_comment
!= '' ) {
382 $out .= " " . wfElementClean( 'comment', null, strval( $row->rev_comment
) ) . "\n";
385 if( $row->rev_deleted
& MW_REV_DELETED_TEXT
) {
386 $out .= " " . wfElement( 'text', array( 'deleted' => 'deleted' ) ) . "\n";
387 } elseif( isset( $row->old_text
) ) {
388 // Raw text from the database may have invalid chars
389 $text = strval( Revision
::getRevisionText( $row ) );
390 $out .= " " . wfElementClean( 'text',
391 array( 'xml:space' => 'preserve' ),
392 strval( $text ) ) . "\n";
395 $out .= " " . wfElement( 'text',
396 array( 'id' => $row->rev_text_id
),
400 $out .= " </revision>\n";
402 wfProfileOut( $fname );
410 * Base class for output stream; prints to stdout or buffer or whereever.
413 function writeOpenStream( $string ) {
414 $this->write( $string );
417 function writeCloseStream( $string ) {
418 $this->write( $string );
421 function writeOpenPage( $page, $string ) {
422 $this->write( $string );
425 function writeClosePage( $string ) {
426 $this->write( $string );
429 function writeRevision( $rev, $string ) {
430 $this->write( $string );
434 * Override to write to a different stream type.
437 function write( $string ) {
443 * Stream outputter to send data to a file.
445 class DumpFileOutput
extends DumpOutput
{
448 function DumpFileOutput( $file ) {
449 $this->handle
= fopen( $file, "wt" );
452 function write( $string ) {
453 fputs( $this->handle
, $string );
458 * Stream outputter to send data to a file via some filter program.
459 * Even if compression is available in a library, using a separate
460 * program can allow us to make use of a multi-processor system.
462 class DumpPipeOutput
extends DumpFileOutput
{
463 function DumpPipeOutput( $command, $file = null ) {
464 if( !is_null( $file ) ) {
465 $command .= " > " . wfEscapeShellArg( $file );
467 $this->handle
= popen( $command, "w" );
472 * Sends dump output via the gzip compressor.
474 class DumpGZipOutput
extends DumpPipeOutput
{
475 function DumpGZipOutput( $file ) {
476 parent
::DumpPipeOutput( "gzip", $file );
481 * Sends dump output via the bgzip2 compressor.
483 class DumpBZip2Output
extends DumpPipeOutput
{
484 function DumpBZip2Output( $file ) {
485 parent
::DumpPipeOutput( "bzip2", $file );
490 * Sends dump output via the p7zip compressor.
492 class Dump7ZipOutput
extends DumpPipeOutput
{
493 function Dump7ZipOutput( $file ) {
494 $command = "7za a -bd -si " . wfEscapeShellArg( $file );
495 parent
::DumpPipeOutput( $command );
502 * Dump output filter class.
503 * This just does output filtering and streaming; XML formatting is done
504 * higher up, so be careful in what you do.
507 function DumpFilter( &$sink ) {
508 $this->sink
=& $sink;
511 function writeOpenStream( $string ) {
512 $this->sink
->writeOpenStream( $string );
515 function writeCloseStream( $string ) {
516 $this->sink
->writeCloseStream( $string );
519 function writeOpenPage( $page, $string ) {
520 $this->sendingThisPage
= $this->pass( $page, $string );
521 if( $this->sendingThisPage
) {
522 $this->sink
->writeOpenPage( $page, $string );
526 function writeClosePage( $string ) {
527 if( $this->sendingThisPage
) {
528 $this->sink
->writeClosePage( $string );
529 $this->sendingThisPage
= false;
533 function writeRevision( $rev, $string ) {
534 if( $this->sendingThisPage
) {
535 $this->sink
->writeRevision( $rev, $string );
540 * Override for page-based filter types.
543 function pass( $page, $string ) {
549 * Simple dump output filter to exclude all talk pages.
551 class DumpNotalkFilter
extends DumpFilter
{
552 function pass( $page ) {
553 return !Namespace::isTalk( $page->page_namespace
);
558 * Dump output filter to include or exclude pages in a given set of namespaces.
560 class DumpNamespaceFilter
extends DumpFilter
{
562 var $namespaces = array();
564 function DumpNamespaceFilter( &$sink, $param ) {
565 parent
::DumpFilter( $sink );
568 "NS_MAIN" => NS_MAIN
,
569 "NS_TALK" => NS_TALK
,
570 "NS_USER" => NS_USER
,
571 "NS_USER_TALK" => NS_USER_TALK
,
572 "NS_PROJECT" => NS_PROJECT
,
573 "NS_PROJECT_TALK" => NS_PROJECT_TALK
,
574 "NS_IMAGE" => NS_IMAGE
,
575 "NS_IMAGE_TALK" => NS_IMAGE_TALK
,
576 "NS_MEDIAWIKI" => NS_MEDIAWIKI
,
577 "NS_MEDIAWIKI_TALK" => NS_MEDIAWIKI_TALK
,
578 "NS_TEMPLATE" => NS_TEMPLATE
,
579 "NS_TEMPLATE_TALK" => NS_TEMPLATE_TALK
,
580 "NS_HELP" => NS_HELP
,
581 "NS_HELP_TALK" => NS_HELP_TALK
,
582 "NS_CATEGORY" => NS_CATEGORY
,
583 "NS_CATEGORY_TALK" => NS_CATEGORY_TALK
);
585 if( $param{0} == '!' ) {
586 $this->invert
= true;
587 $param = substr( $param, 1 );
590 foreach( explode( ',', $param ) as $key ) {
592 if( isset( $constants[$key] ) ) {
593 $ns = $constants[$key];
594 $this->namespaces
[$ns] = true;
595 } elseif( is_numeric( $key ) ) {
596 $ns = intval( $key );
597 $this->namespaces
[$ns] = true;
599 wfDie( "Unrecognized namespace key '$key'\n" );
604 function pass( $page ) {
605 $match = isset( $this->namespaces
[$page->page_namespace
] );
606 return $this->invert
xor $match;
612 * Dump output filter to include only the last revision in each page sequence.
614 class DumpLatestFilter
extends DumpFilter
{
615 var $page, $pageString, $rev, $revString;
617 function writeOpenPage( $page, $string ) {
619 $this->pageString
= $string;
622 function writeClosePage( $string ) {
624 $this->sink
->writeOpenPage( $this->page
, $this->pageString
);
625 $this->sink
->writeRevision( $this->rev
, $this->revString
);
626 $this->sink
->writeClosePage( $string );
629 $this->revString
= null;
631 $this->pageString
= null;
634 function writeRevision( $rev, $string ) {
635 if( $rev->rev_id
== $this->page
->page_latest
) {
637 $this->revString
= $string;
643 * Base class for output stream; prints to stdout or buffer or whereever.
645 class DumpMultiWriter
{
646 function DumpMultiWriter( $sinks ) {
647 $this->sinks
= $sinks;
648 $this->count
= count( $sinks );
651 function writeOpenStream( $string ) {
652 for( $i = 0; $i < $this->count
; $i++
) {
653 $this->sinks
[$i]->writeOpenStream( $string );
657 function writeCloseStream( $string ) {
658 for( $i = 0; $i < $this->count
; $i++
) {
659 $this->sinks
[$i]->writeCloseStream( $string );
663 function writeOpenPage( $page, $string ) {
664 for( $i = 0; $i < $this->count
; $i++
) {
665 $this->sinks
[$i]->writeOpenPage( $page, $string );
669 function writeClosePage( $string ) {
670 for( $i = 0; $i < $this->count
; $i++
) {
671 $this->sinks
[$i]->writeClosePage( $string );
675 function writeRevision( $rev, $string ) {
676 for( $i = 0; $i < $this->count
; $i++
) {
677 $this->sinks
[$i]->writeRevision( $rev, $string );
682 function xmlsafe( $string ) {
684 wfProfileIn( $fname );
687 * The page may contain old data which has not been properly normalized.
688 * Invalid UTF-8 sequences or forbidden control characters will make our
689 * XML output invalid, so be sure to strip them out.
691 $string = UtfNormal
::cleanUp( $string );
693 $string = htmlspecialchars( $string );
694 wfProfileOut( $fname );