Second part of bug 4083: Special:Validation doesn't check wpEditToken
[mediawiki.git] / includes / Export.php
blob71a771ba883637ae19e0212f4ee5d27b7590bd49
1 <?php
2 # Copyright (C) 2003, 2005 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
19 /**
21 * @package MediaWiki
22 * @subpackage SpecialPage
25 /** */
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 );
38 /**
39 * @package MediaWiki
40 * @subpackage SpecialPage
42 class WikiExporter {
43 /**
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.
50 * @param Database $db
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 ) {
56 $this->db =& $db;
57 $this->history = $history;
58 $this->buffer = $buffer;
59 $this->writer = new XmlDumpWriter();
60 $this->sink = new DumpOutput();
61 $this->text = $text;
64 /**
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 ) {
72 $this->sink =& $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 );
85 /**
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.
90 function allPages() {
91 return $this->dumpFrom( '' );
94 /**
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 );
103 if( $end ) {
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" );
122 } else {
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';
148 } else {
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 );
157 if( $cond == '' ) {
158 // Optimization hack for full-database dump
159 $pageindex = 'FORCE INDEX (PRIMARY)';
160 $revindex = 'FORCE INDEX(page_timestamp)';
161 } else {
162 $pageindex = '';
163 $revindex = '';
165 if( $this->text == MW_EXPORT_STUB ) {
166 $sql = "SELECT * FROM
167 $page $pageindex,
168 $revision $revindex
169 WHERE $where $join
170 ORDER BY page_id";
171 } else {
172 $sql = "SELECT * FROM
173 $page $pageindex,
174 $revision $revindex,
175 $text
176 WHERE $where $join AND rev_text_id=old_id
177 ORDER BY page_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
201 * @access private
203 function outputStream( $resultset ) {
204 $last = null;
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 );
215 $last = $row;
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 );
224 $resultset->free();
228 class XmlDumpWriter {
231 * Returns the export schema version.
232 * @return string
234 function schemaVersion() {
235 return "0.3";
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.
246 * @return string
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",
256 'version' => $ver,
257 'xml:lang' => $wgContLanguageCode ),
258 null ) .
259 "\n" .
260 $this->siteInfo();
263 function siteInfo() {
264 $info = array(
265 $this->sitename(),
266 $this->homelink(),
267 $this->generator(),
268 $this->caseSetting(),
269 $this->namespaces() );
270 return " <siteinfo>\n " .
271 implode( "\n ", $info ) .
272 "\n </siteinfo>\n";
275 function sitename() {
276 global $wgSitename;
277 return wfElement( 'sitename', array(), $wgSitename );
280 function generator() {
281 global $wgVersion;
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() {
298 global $wgContLang;
299 $spaces = " <namespaces>\n";
300 foreach( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
301 $spaces .= ' ' . wfElement( 'namespace', array( 'key' => $ns ), $title ) . "\n";
303 $spaces .= " </namespaces>";
304 return $spaces;
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.
320 * @param object $row
321 * @return string
322 * @access private
324 function openPage( $row ) {
325 $out = " <page>\n";
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";
333 return $out;
337 * Closes a <page> section on the output stream.
339 * @access private
341 function closePage() {
342 return " </page>\n";
346 * Dumps a <revision> section on the output stream, with
347 * data filled in from the given database row.
349 * @param object $row
350 * @return string
351 * @access private
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 $out .= " <contributor>\n";
364 if( $row->rev_user ) {
365 $out .= " " . wfElementClean( 'username', null, strval( $row->rev_user_text ) ) . "\n";
366 $out .= " " . wfElement( 'id', null, strval( $row->rev_user ) ) . "\n";
367 } else {
368 $out .= " " . wfElementClean( 'ip', null, strval( $row->rev_user_text ) ) . "\n";
370 $out .= " </contributor>\n";
372 if( $row->rev_minor_edit ) {
373 $out .= " <minor/>\n";
375 if( $row->rev_comment != '' ) {
376 $out .= " " . wfElementClean( 'comment', null, strval( $row->rev_comment ) ) . "\n";
379 if( isset( $row->old_text ) ) {
380 // Raw text from the database may have invalid chars
381 $text = strval( Revision::getRevisionText( $row ) );
382 $out .= " " . wfElementClean( 'text',
383 array( 'xml:space' => 'preserve' ),
384 strval( $text ) ) . "\n";
385 } else {
386 // Stub output
387 $out .= " " . wfElement( 'text',
388 array( 'id' => $row->rev_text_id ),
389 "" ) . "\n";
392 $out .= " </revision>\n";
394 wfProfileOut( $fname );
395 return $out;
402 * Base class for output stream; prints to stdout or buffer or whereever.
404 class DumpOutput {
405 function writeOpenStream( $string ) {
406 $this->write( $string );
409 function writeCloseStream( $string ) {
410 $this->write( $string );
413 function writeOpenPage( $page, $string ) {
414 $this->write( $string );
417 function writeClosePage( $string ) {
418 $this->write( $string );
421 function writeRevision( $rev, $string ) {
422 $this->write( $string );
426 * Override to write to a different stream type.
427 * @return bool
429 function write( $string ) {
430 print $string;
435 * Stream outputter to send data to a file.
437 class DumpFileOutput extends DumpOutput {
438 var $handle;
440 function DumpFileOutput( $file ) {
441 $this->handle = fopen( $file, "wt" );
444 function write( $string ) {
445 fputs( $this->handle, $string );
450 * Stream outputter to send data to a file via some filter program.
451 * Even if compression is available in a library, using a separate
452 * program can allow us to make use of a multi-processor system.
454 class DumpPipeOutput extends DumpFileOutput {
455 function DumpPipeOutput( $command, $file = null ) {
456 if( !is_null( $file ) ) {
457 $command .= " > " . wfEscapeShellArg( $file );
459 $this->handle = popen( $command, "w" );
464 * Sends dump output via the gzip compressor.
466 class DumpGZipOutput extends DumpPipeOutput {
467 function DumpGZipOutput( $file ) {
468 parent::DumpPipeOutput( "gzip", $file );
473 * Sends dump output via the bgzip2 compressor.
475 class DumpBZip2Output extends DumpPipeOutput {
476 function DumpBZip2Output( $file ) {
477 parent::DumpPipeOutput( "bzip2", $file );
482 * Sends dump output via the p7zip compressor.
484 class Dump7ZipOutput extends DumpPipeOutput {
485 function Dump7ZipOutput( $file ) {
486 $command = "7za a -si " . wfEscapeShellArg( $file );
487 parent::DumpPipeOutput( $command );
494 * Dump output filter class.
495 * This just does output filtering and streaming; XML formatting is done
496 * higher up, so be careful in what you do.
498 class DumpFilter {
499 function DumpFilter( &$sink ) {
500 $this->sink =& $sink;
503 function writeOpenStream( $string ) {
504 $this->sink->writeOpenStream( $string );
507 function writeCloseStream( $string ) {
508 $this->sink->writeCloseStream( $string );
511 function writeOpenPage( $page, $string ) {
512 $this->sendingThisPage = $this->pass( $page, $string );
513 if( $this->sendingThisPage ) {
514 $this->sink->writeOpenPage( $page, $string );
518 function writeClosePage( $string ) {
519 if( $this->sendingThisPage ) {
520 $this->sink->writeClosePage( $string );
521 $this->sendingThisPage = false;
525 function writeRevision( $rev, $string ) {
526 if( $this->sendingThisPage ) {
527 $this->sink->writeRevision( $rev, $string );
532 * Override for page-based filter types.
533 * @return bool
535 function pass( $page, $string ) {
536 return true;
541 * Simple dump output filter to exclude all talk pages.
543 class DumpNotalkFilter extends DumpFilter {
544 function pass( $page ) {
545 return !Namespace::isTalk( $page->page_namespace );
550 * Dump output filter to include or exclude pages in a given set of namespaces.
552 class DumpNamespaceFilter extends DumpFilter {
553 var $invert = false;
554 var $namespaces = array();
556 function DumpNamespaceFilter( &$sink, $param ) {
557 parent::DumpFilter( $sink );
559 $constants = array(
560 "NS_MAIN" => NS_MAIN,
561 "NS_TALK" => NS_TALK,
562 "NS_USER" => NS_USER,
563 "NS_USER_TALK" => NS_USER_TALK,
564 "NS_PROJECT" => NS_PROJECT,
565 "NS_PROJECT_TALK" => NS_PROJECT_TALK,
566 "NS_IMAGE" => NS_IMAGE,
567 "NS_IMAGE_TALK" => NS_IMAGE_TALK,
568 "NS_MEDIAWIKI" => NS_MEDIAWIKI,
569 "NS_MEDIAWIKI_TALK" => NS_MEDIAWIKI_TALK,
570 "NS_TEMPLATE" => NS_TEMPLATE,
571 "NS_TEMPLATE_TALK" => NS_TEMPLATE_TALK,
572 "NS_HELP" => NS_HELP,
573 "NS_HELP_TALK" => NS_HELP_TALK,
574 "NS_CATEGORY" => NS_CATEGORY,
575 "NS_CATEGORY_TALK" => NS_CATEGORY_TALK );
577 if( $param{0} == '!' ) {
578 $this->invert = true;
579 $param = substr( $param, 1 );
582 foreach( explode( ',', $param ) as $key ) {
583 $key = trim( $key );
584 if( isset( $constants[$key] ) ) {
585 $ns = $constants[$key];
586 $this->namespaces[$ns] = true;
587 } elseif( is_numeric( $key ) ) {
588 $ns = intval( $key );
589 $this->namespaces[$ns] = true;
590 } else {
591 die( "Unrecognized namespace key '$key'\n" );
596 function pass( $page ) {
597 $match = isset( $this->namespaces[$page->page_namespace] );
598 return $this->invert xor $match;
604 * Dump output filter to include only the last revision in each page sequence.
606 class DumpLatestFilter extends DumpFilter {
607 var $page, $pageString, $rev, $revString;
609 function writeOpenPage( $page, $string ) {
610 $this->page = $page;
611 $this->pageString = $string;
614 function writeClosePage( $string ) {
615 if( $this->rev ) {
616 $this->sink->writeOpenPage( $this->page, $this->pageString );
617 $this->sink->writeRevision( $this->rev, $this->revString );
618 $this->sink->writeClosePage( $string );
620 $this->rev = null;
621 $this->revString = null;
622 $this->page = null;
623 $this->pageString = null;
626 function writeRevision( $rev, $string ) {
627 if( $rev->rev_id == $this->page->page_latest ) {
628 $this->rev = $rev;
629 $this->revString = $string;
635 * Base class for output stream; prints to stdout or buffer or whereever.
637 class DumpMultiWriter {
638 function DumpMultiWriter( $sinks ) {
639 $this->sinks = $sinks;
640 $this->count = count( $sinks );
643 function writeOpenStream( $string ) {
644 for( $i = 0; $i < $this->count; $i++ ) {
645 $this->sinks[$i]->writeOpenStream( $string );
649 function writeCloseStream( $string ) {
650 for( $i = 0; $i < $this->count; $i++ ) {
651 $this->sinks[$i]->writeCloseStream( $string );
655 function writeOpenPage( $page, $string ) {
656 for( $i = 0; $i < $this->count; $i++ ) {
657 $this->sinks[$i]->writeOpenPage( $page, $string );
661 function writeClosePage( $string ) {
662 for( $i = 0; $i < $this->count; $i++ ) {
663 $this->sinks[$i]->writeClosePage( $string );
667 function writeRevision( $rev, $string ) {
668 for( $i = 0; $i < $this->count; $i++ ) {
669 $this->sinks[$i]->writeRevision( $rev, $string );
674 function xmlsafe( $string ) {
675 $fname = 'xmlsafe';
676 wfProfileIn( $fname );
679 * The page may contain old data which has not been properly normalized.
680 * Invalid UTF-8 sequences or forbidden control characters will make our
681 * XML output invalid, so be sure to strip them out.
683 $string = UtfNormal::cleanUp( $string );
685 $string = htmlspecialchars( $string );
686 wfProfileOut( $fname );
687 return $string;