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
23 * @subpackage SpecialPage
27 require_once( 'WikiError.php' );
32 function wfSpecialImport( $page = '' ) {
33 global $wgUser, $wgOut, $wgRequest, $wgTitle, $wgImportSources;
36 # $wgOut->addWikiText( "Special:Import is not ready for this beta release, sorry." );
40 if( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit') {
41 switch( $wgRequest->getVal( "source" ) ) {
43 if( $wgUser->isAllowed( 'importupload' ) ) {
44 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
46 return $wgOut->permissionRequired( 'importupload' );
50 $source = ImportStreamSource
::newFromInterwiki(
51 $wgRequest->getVal( "interwiki" ),
52 $wgRequest->getText( "frompage" ) );
55 $source = new WikiError( "Unknown import source type" );
58 if( WikiError
::isError( $source ) ) {
59 $wgOut->addWikiText( wfEscapeWikiText( $source->getMessage() ) );
61 $importer = new WikiImporter( $source );
62 $result = $importer->doImport();
63 if( WikiError
::isError( $result ) ) {
64 $wgOut->addWikiText( wfMsg( "importfailed",
65 wfEscapeWikiText( $result->getMessage() ) ) );
68 $wgOut->addWikiText( wfMsg( "importsuccess" ) );
73 $action = $wgTitle->escapeLocalUrl( 'action=submit' );
75 if( $wgUser->isAllowed( 'importupload' ) ) {
76 $wgOut->addWikiText( wfMsg( "importtext" ) );
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" ) . "'/>
90 if( empty( $wgImportSources ) ) {
91 $wgOut->addWikiText( wfMsg( 'importnosources' ) );
95 if( !empty( $wgImportSources ) ) {
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'>
104 foreach( $wgImportSources as $interwiki ) {
105 $iw = htmlspecialchars( $interwiki );
106 $wgOut->addHTML( "<option value=\"$iw\">$iw</option>\n" );
110 <input name='frompage' />
111 <input type='submit' />
121 * @subpackage SpecialPage
125 var $timestamp = "20010115000000";
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 ) {
153 function setComment( $text ) {
154 $this->comment
= $text;
157 function setMinor( $minor ) {
158 $this->minor
= (bool)$minor;
161 function getTitle() {
165 function getTimestamp() {
166 return $this->timestamp
;
170 return $this->user_text
;
177 function getComment() {
178 return $this->comment
;
181 function getMinor() {
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() );
192 $userId = intval( $user->getId() );
193 $userText = $user->getName();
196 $userText = $this->getUser();
199 // avoid memory leak...?
200 $linkCache =& LinkCache
::singleton();
203 $article = new Article( $this->title
);
204 $pageId = $article->getId();
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" );
219 $revision = new Revision( array(
221 'text' => $this->getText(),
222 'comment' => $this->getComment(),
224 'user_text' => $userText,
225 'timestamp' => $this->timestamp
,
226 'minor_edit' => $this->minor
,
228 $revId = $revision->insertOn( $dbw );
229 $article->updateIfNewerOn( $dbw, $revision );
239 * @subpackage SpecialPage
243 var $mPageCallback = null;
244 var $mRevisionCallback = null;
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" );
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
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 );
286 function debug( $data ) {
287 #wfDebug( "IMPORT: $data\n" );
290 function notice( $data ) {
291 global $wgCommandLineMode;
292 if( $wgCommandLineMode ) {
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
305 function setPageCallback( $callback ) {
306 $previous = $this->mPageCallback
;
307 $this->mPageCallback
= $callback;
312 * Sets the action to perform as each page revision is reached.
313 * @param callable $callback
316 function setRevisionCallback( $callback ) {
317 $previous = $this->mRevisionCallback
;
318 $this->mRevisionCallback
= $callback;
323 * Default per-revision callback, performs the import.
324 * @param WikiRevision $revision
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
337 function debugRevisionHandler( &$revision ) {
338 $this->debug( "Got revision:" );
339 if( is_object( $revision->title
) ) {
340 $this->debug( "-- Title: " . $revision->title
->getPrefixedText() );
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
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" );
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 ) {
396 $this->debug( "in_siteinfo $name" );
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" );
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" );
430 $this->workRevision
= new WikiRevision
;
431 $this->workRevision
->setTitle( $this->workTitle
);
432 xml_set_element_handler( $parser, "in_revision", "out_revision" );
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
) {
468 $this->workTitle
= $this->appenddata
;
469 $this->pageCallback( $this->workTitle
);
472 $this->workRevision
->setText( $this->appenddata
);
475 $this->workRevision
->setUsername( $this->appenddata
);
478 $this->workRevision
->setUserIP( $this->appenddata
);
481 $this->workRevision
->setTimestamp( $this->appenddata
);
484 $this->workRevision
->setComment( $this->appenddata
);
487 $this->workRevision
->setMinor( true );
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" );
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" );
510 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
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 ) ) {
528 $wgOut->addHTML( "<li>" . $out . "</li>\n" );
532 function in_contributor( $parser, $name, $attribs ) {
533 $this->debug( "in_contributor $name" );
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" );
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;
569 function readChunk() {
570 if( $this->atEnd() ) {
574 return $this->mString
;
579 /** @package MediaWiki */
580 class ImportStreamSource
{
581 function ImportStreamSource( $handle ) {
582 $this->mHandle
= $handle;
586 return feof( $this->mHandle
);
589 function readChunk() {
590 return fread( $this->mHandle
, 32768 );
593 function newFromFile( $filename ) {
594 $file = @fopen
( $filename, 'rt' );
596 return new WikiError( "Couldn't open import file" );
598 return new ImportStreamSource( $file );
601 function newFromUpload( $fieldname = "xmlimport" ) {
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 );
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 );
628 function newFromInterwiki( $interwiki, $page ) {
629 $base = Title
::getInterwikiLink( $interwiki );
630 if( empty( $base ) ) {
631 return new WikiError( 'Bad interwiki link' );
633 $import = wfUrlencode( "Special:Export/$page" );
634 $url = str_replace( "$1", $import, $base );
635 return ImportStreamSource
::newFromURL( $url );