Second part of bug 4083: Special:Validation doesn't check wpEditToken
[mediawiki.git] / includes / SpecialImport.php
blobe0289afc3634abb90625187f6737635c4d4fae12
1 <?php
2 /**
3 * MediaWiki page data importer
4 * Copyright (C) 2003,2005 Brion Vibber <brion@pobox.com>
5 * http://www.mediawiki.org/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * http://www.gnu.org/copyleft/gpl.html
22 * @package MediaWiki
23 * @subpackage SpecialPage
26 /** */
27 require_once( 'WikiError.php' );
29 /**
30 * Constructor
32 function wfSpecialImport( $page = '' ) {
33 global $wgUser, $wgOut, $wgRequest, $wgTitle, $wgImportSources;
35 ###
36 # $wgOut->addWikiText( "Special:Import is not ready for this beta release, sorry." );
37 # return;
38 ###
40 if( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit') {
41 switch( $wgRequest->getVal( "source" ) ) {
42 case "upload":
43 if( $wgUser->isAllowed( 'importupload' ) ) {
44 $source = ImportStreamSource::newFromUpload( "xmlimport" );
45 } else {
46 return $wgOut->permissionRequired( 'importupload' );
48 break;
49 case "interwiki":
50 $source = ImportStreamSource::newFromInterwiki(
51 $wgRequest->getVal( "interwiki" ),
52 $wgRequest->getText( "frompage" ) );
53 break;
54 default:
55 $source = new WikiError( "Unknown import source type" );
58 if( WikiError::isError( $source ) ) {
59 $wgOut->addWikiText( wfEscapeWikiText( $source->getMessage() ) );
60 } else {
61 $importer = new WikiImporter( $source );
62 $result = $importer->doImport();
63 if( WikiError::isError( $result ) ) {
64 $wgOut->addWikiText( wfMsg( "importfailed",
65 wfEscapeWikiText( $result->getMessage() ) ) );
66 } else {
67 # Success!
68 $wgOut->addWikiText( wfMsg( "importsuccess" ) );
73 $action = $wgTitle->escapeLocalUrl( 'action=submit' );
75 if( $wgUser->isAllowed( 'importupload' ) ) {
76 $wgOut->addWikiText( wfMsg( "importtext" ) );
77 $wgOut->addHTML( "
78 <fieldset>
79 <legend>" . wfMsgHtml('upload') . "</legend>
80 <form enctype='multipart/form-data' method='post' action=\"$action\">
81 <input type='hidden' name='action' value='submit' />
82 <input type='hidden' name='source' value='upload' />
83 <input type='hidden' name='MAX_FILE_SIZE' value='2000000' />
84 <input type='file' name='xmlimport' value='' size='30' />
85 <input type='submit' value='" . wfMsgHtml( "uploadbtn" ) . "'/>
86 </form>
87 </fieldset>
88 " );
89 } else {
90 if( empty( $wgImportSources ) ) {
91 $wgOut->addWikiText( wfMsg( 'importnosources' ) );
95 if( !empty( $wgImportSources ) ) {
96 $wgOut->addHTML( "
97 <fieldset>
98 <legend>" . wfMsgHtml('importinterwiki') . "</legend>
99 <form method='post' action=\"$action\">
100 <input type='hidden' name='action' value='submit' />
101 <input type='hidden' name='source' value='interwiki' />
102 <select name='interwiki'>
103 " );
104 foreach( $wgImportSources as $interwiki ) {
105 $iw = htmlspecialchars( $interwiki );
106 $wgOut->addHTML( "<option value=\"$iw\">$iw</option>\n" );
108 $wgOut->addHTML( "
109 </select>
110 <input name='frompage' />
111 <input type='submit' />
112 </form>
113 </fieldset>
114 " );
120 * @package MediaWiki
121 * @subpackage SpecialPage
123 class WikiRevision {
124 var $title = NULL;
125 var $timestamp = "20010115000000";
126 var $user = 0;
127 var $user_text = "";
128 var $text = "";
129 var $comment = "";
130 var $minor = false;
132 function setTitle( $text ) {
133 $this->title = Title::newFromText( $text );
136 function setTimestamp( $ts ) {
137 # 2003-08-05T18:30:02Z
138 $this->timestamp = wfTimestamp( TS_MW, $ts );
141 function setUsername( $user ) {
142 $this->user_text = $user;
145 function setUserIP( $ip ) {
146 $this->user_text = $ip;
149 function setText( $text ) {
150 $this->text = $text;
153 function setComment( $text ) {
154 $this->comment = $text;
157 function setMinor( $minor ) {
158 $this->minor = (bool)$minor;
161 function getTitle() {
162 return $this->title;
165 function getTimestamp() {
166 return $this->timestamp;
169 function getUser() {
170 return $this->user_text;
173 function getText() {
174 return $this->text;
177 function getComment() {
178 return $this->comment;
181 function getMinor() {
182 return $this->minor;
185 function importOldRevision() {
186 $fname = "WikiImporter::importOldRevision";
187 $dbw =& wfGetDB( DB_MASTER );
189 # Sneak a single revision into place
190 $user = User::newFromName( $this->getUser() );
191 if( $user ) {
192 $userId = intval( $user->getId() );
193 $userText = $user->getName();
194 } else {
195 $userId = 0;
196 $userText = $this->getUser();
199 // avoid memory leak...?
200 $linkCache =& LinkCache::singleton();
201 $linkCache->clear();
203 $article = new Article( $this->title );
204 $pageId = $article->getId();
205 if( $pageId == 0 ) {
206 # must create the page...
207 $pageId = $article->insertOn( $dbw );
210 # FIXME: Check for exact conflicts
211 # FIXME: Use original rev_id optionally
212 # FIXME: blah blah blah
214 #if( $numrows > 0 ) {
215 # return wfMsg( "importhistoryconflict" );
218 # Insert the row
219 $revision = new Revision( array(
220 'page' => $pageId,
221 'text' => $this->getText(),
222 'comment' => $this->getComment(),
223 'user' => $userId,
224 'user_text' => $userText,
225 'timestamp' => $this->timestamp,
226 'minor_edit' => $this->minor,
227 ) );
228 $revId = $revision->insertOn( $dbw );
229 $article->updateIfNewerOn( $dbw, $revision );
231 return true;
238 * @package MediaWiki
239 * @subpackage SpecialPage
241 class WikiImporter {
242 var $mSource = null;
243 var $mPageCallback = null;
244 var $mRevisionCallback = null;
245 var $lastfield;
247 function WikiImporter( $source ) {
248 $this->setRevisionCallback( array( &$this, "importRevision" ) );
249 $this->mSource = $source;
252 function throwXmlError( $err ) {
253 $this->debug( "FAILURE: $err" );
254 wfDebug( "WikiImporter XML error: $err\n" );
257 # --------------
259 function doImport() {
260 if( empty( $this->mSource ) ) {
261 return new WikiErrorMsg( "importnotext" );
264 $parser = xml_parser_create( "UTF-8" );
266 # case folding violates XML standard, turn it off
267 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
269 xml_set_object( $parser, $this );
270 xml_set_element_handler( $parser, "in_start", "" );
272 $offset = 0; // for context extraction on error reporting
273 do {
274 $chunk = $this->mSource->readChunk();
275 if( !xml_parse( $parser, $chunk, $this->mSource->atEnd() ) ) {
276 wfDebug( "WikiImporter::doImport encountered XML parsing error\n" );
277 return new WikiXmlError( $parser, 'XML import parse failure', $chunk, $offset );
279 $offset += strlen( $chunk );
280 } while( $chunk !== false && !$this->mSource->atEnd() );
281 xml_parser_free( $parser );
283 return true;
286 function debug( $data ) {
287 #wfDebug( "IMPORT: $data\n" );
290 function notice( $data ) {
291 global $wgCommandLineMode;
292 if( $wgCommandLineMode ) {
293 print "$data\n";
294 } else {
295 global $wgOut;
296 $wgOut->addHTML( "<li>$data</li>\n" );
301 * Sets the action to perform as each new page in the stream is reached.
302 * @param callable $callback
303 * @return callable
305 function setPageCallback( $callback ) {
306 $previous = $this->mPageCallback;
307 $this->mPageCallback = $callback;
308 return $previous;
312 * Sets the action to perform as each page revision is reached.
313 * @param callable $callback
314 * @return callable
316 function setRevisionCallback( $callback ) {
317 $previous = $this->mRevisionCallback;
318 $this->mRevisionCallback = $callback;
319 return $previous;
323 * Default per-revision callback, performs the import.
324 * @param WikiRevision $revision
325 * @access private
327 function importRevision( &$revision ) {
328 $dbw =& wfGetDB( DB_MASTER );
329 $dbw->deadlockLoop( array( &$revision, 'importOldRevision' ) );
333 * Alternate per-revision callback, for debugging.
334 * @param WikiRevision $revision
335 * @access private
337 function debugRevisionHandler( &$revision ) {
338 $this->debug( "Got revision:" );
339 if( is_object( $revision->title ) ) {
340 $this->debug( "-- Title: " . $revision->title->getPrefixedText() );
341 } else {
342 $this->debug( "-- Title: <invalid>" );
344 $this->debug( "-- User: " . $revision->user_text );
345 $this->debug( "-- Timestamp: " . $revision->timestamp );
346 $this->debug( "-- Comment: " . $revision->comment );
347 $this->debug( "-- Text: " . $revision->text );
351 * Notify the callback function when a new <page> is reached.
352 * @param Title $title
353 * @access private
355 function pageCallback( $title ) {
356 if( is_callable( $this->mPageCallback ) ) {
357 call_user_func( $this->mPageCallback, $title );
362 # XML parser callbacks from here out -- beware!
363 function donothing( $parser, $x, $y="" ) {
364 #$this->debug( "donothing" );
367 function in_start( $parser, $name, $attribs ) {
368 $this->debug( "in_start $name" );
369 if( $name != "mediawiki" ) {
370 return $this->throwXMLerror( "Expected <mediawiki>, got <$name>" );
372 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
375 function in_mediawiki( $parser, $name, $attribs ) {
376 $this->debug( "in_mediawiki $name" );
377 if( $name == 'siteinfo' ) {
378 xml_set_element_handler( $parser, "in_siteinfo", "out_siteinfo" );
379 } elseif( $name == 'page' ) {
380 xml_set_element_handler( $parser, "in_page", "out_page" );
381 } else {
382 return $this->throwXMLerror( "Expected <page>, got <$name>" );
385 function out_mediawiki( $parser, $name ) {
386 $this->debug( "out_mediawiki $name" );
387 if( $name != "mediawiki" ) {
388 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
390 xml_set_element_handler( $parser, "donothing", "donothing" );
394 function in_siteinfo( $parser, $name, $attribs ) {
395 // no-ops for now
396 $this->debug( "in_siteinfo $name" );
397 switch( $name ) {
398 case "sitename":
399 case "base":
400 case "generator":
401 case "case":
402 case "namespaces":
403 case "namespace":
404 break;
405 default:
406 return $this->throwXMLerror( "Element <$name> not allowed in <siteinfo>." );
410 function out_siteinfo( $parser, $name ) {
411 if( $name == "siteinfo" ) {
412 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
417 function in_page( $parser, $name, $attribs ) {
418 $this->debug( "in_page $name" );
419 switch( $name ) {
420 case "id":
421 case "title":
422 case "restrictions":
423 $this->appendfield = $name;
424 $this->appenddata = "";
425 $this->parenttag = "page";
426 xml_set_element_handler( $parser, "in_nothing", "out_append" );
427 xml_set_character_data_handler( $parser, "char_append" );
428 break;
429 case "revision":
430 $this->workRevision = new WikiRevision;
431 $this->workRevision->setTitle( $this->workTitle );
432 xml_set_element_handler( $parser, "in_revision", "out_revision" );
433 break;
434 default:
435 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
439 function out_page( $parser, $name ) {
440 $this->debug( "out_page $name" );
441 if( $name != "page" ) {
442 return $this->throwXMLerror( "Expected </page>, got </$name>" );
444 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
446 $this->workTitle = NULL;
447 $this->workRevision = NULL;
450 function in_nothing( $parser, $name, $attribs ) {
451 $this->debug( "in_nothing $name" );
452 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
454 function char_append( $parser, $data ) {
455 $this->debug( "char_append '$data'" );
456 $this->appenddata .= $data;
458 function out_append( $parser, $name ) {
459 $this->debug( "out_append $name" );
460 if( $name != $this->appendfield ) {
461 return $this->throwXMLerror( "Expected </{$this->appendfield}>, got </$name>" );
463 xml_set_element_handler( $parser, "in_$this->parenttag", "out_$this->parenttag" );
464 xml_set_character_data_handler( $parser, "donothing" );
466 switch( $this->appendfield ) {
467 case "title":
468 $this->workTitle = $this->appenddata;
469 $this->pageCallback( $this->workTitle );
470 break;
471 case "text":
472 $this->workRevision->setText( $this->appenddata );
473 break;
474 case "username":
475 $this->workRevision->setUsername( $this->appenddata );
476 break;
477 case "ip":
478 $this->workRevision->setUserIP( $this->appenddata );
479 break;
480 case "timestamp":
481 $this->workRevision->setTimestamp( $this->appenddata );
482 break;
483 case "comment":
484 $this->workRevision->setComment( $this->appenddata );
485 break;
486 case "minor":
487 $this->workRevision->setMinor( true );
488 break;
489 default:
490 $this->debug( "Bad append: {$this->appendfield}" );
492 $this->appendfield = "";
493 $this->appenddata = "";
496 function in_revision( $parser, $name, $attribs ) {
497 $this->debug( "in_revision $name" );
498 switch( $name ) {
499 case "id":
500 case "timestamp":
501 case "comment":
502 case "minor":
503 case "text":
504 $this->parenttag = "revision";
505 $this->appendfield = $name;
506 xml_set_element_handler( $parser, "in_nothing", "out_append" );
507 xml_set_character_data_handler( $parser, "char_append" );
508 break;
509 case "contributor":
510 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
511 break;
512 default:
513 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
517 function out_revision( $parser, $name ) {
518 $this->debug( "out_revision $name" );
519 if( $name != "revision" ) {
520 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
522 xml_set_element_handler( $parser, "in_page", "out_page" );
524 $out = call_user_func_array( $this->mRevisionCallback,
525 array( &$this->workRevision, &$this ) );
526 if( !empty( $out ) ) {
527 global $wgOut;
528 $wgOut->addHTML( "<li>" . $out . "</li>\n" );
532 function in_contributor( $parser, $name, $attribs ) {
533 $this->debug( "in_contributor $name" );
534 switch( $name ) {
535 case "username":
536 case "ip":
537 case "id":
538 $this->parenttag = "contributor";
539 $this->appendfield = $name;
540 xml_set_element_handler( $parser, "in_nothing", "out_append" );
541 xml_set_character_data_handler( $parser, "char_append" );
542 break;
543 default:
544 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
548 function out_contributor( $parser, $name ) {
549 $this->debug( "out_contributor $name" );
550 if( $name != "contributor" ) {
551 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
553 xml_set_element_handler( $parser, "in_revision", "out_revision" );
558 /** @package MediaWiki */
559 class ImportStringSource {
560 function ImportStringSource( $string ) {
561 $this->mString = $string;
562 $this->mRead = false;
565 function atEnd() {
566 return $this->mRead;
569 function readChunk() {
570 if( $this->atEnd() ) {
571 return false;
572 } else {
573 $this->mRead = true;
574 return $this->mString;
579 /** @package MediaWiki */
580 class ImportStreamSource {
581 function ImportStreamSource( $handle ) {
582 $this->mHandle = $handle;
585 function atEnd() {
586 return feof( $this->mHandle );
589 function readChunk() {
590 return fread( $this->mHandle, 32768 );
593 function newFromFile( $filename ) {
594 $file = @fopen( $filename, 'rt' );
595 if( !$file ) {
596 return new WikiError( "Couldn't open import file" );
598 return new ImportStreamSource( $file );
601 function newFromUpload( $fieldname = "xmlimport" ) {
602 global $wgOut;
604 $upload =& $_FILES[$fieldname];
606 if( !isset( $upload ) ) {
607 return new WikiErrorMsg( 'importnofile' );
609 if( !empty( $upload['error'] ) ) {
610 return new WikiErrorMsg( 'importuploaderror', $upload['error'] );
612 $fname = $upload['tmp_name'];
613 if( is_uploaded_file( $fname ) ) {
614 return ImportStreamSource::newFromFile( $fname );
615 } else {
616 return new WikiErrorMsg( 'importnofile' );
620 function newFromURL( $url ) {
621 # fopen-wrappers are normally turned off for security.
622 ini_set( "allow_url_fopen", true );
623 $ret = ImportStreamSource::newFromFile( $url );
624 ini_set( "allow_url_fopen", false );
625 return $ret;
628 function newFromInterwiki( $interwiki, $page ) {
629 $base = Title::getInterwikiLink( $interwiki );
630 if( empty( $base ) ) {
631 return new WikiError( 'Bad interwiki link' );
632 } else {
633 $import = wfUrlencode( "Special:Export/$page" );
634 $url = str_replace( "$1", $import, $base );
635 return ImportStreamSource::newFromURL( $url );