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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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
126 var $timestamp = "20010115000000";
133 function setTitle( $text ) {
134 $this->title
= Title
::newFromText( $text );
137 function setID( $id ) {
141 function setTimestamp( $ts ) {
142 # 2003-08-05T18:30:02Z
143 $this->timestamp
= wfTimestamp( TS_MW
, $ts );
146 function setUsername( $user ) {
147 $this->user_text
= $user;
150 function setUserIP( $ip ) {
151 $this->user_text
= $ip;
154 function setText( $text ) {
158 function setComment( $text ) {
159 $this->comment
= $text;
162 function setMinor( $minor ) {
163 $this->minor
= (bool)$minor;
166 function getTitle() {
174 function getTimestamp() {
175 return $this->timestamp
;
179 return $this->user_text
;
186 function getComment() {
187 return $this->comment
;
190 function getMinor() {
194 function importOldRevision() {
195 $fname = "WikiImporter::importOldRevision";
196 $dbw =& wfGetDB( DB_MASTER
);
198 # Sneak a single revision into place
199 $user = User
::newFromName( $this->getUser() );
201 $userId = intval( $user->getId() );
202 $userText = $user->getName();
205 $userText = $this->getUser();
208 // avoid memory leak...?
209 $linkCache =& LinkCache
::singleton();
212 $article = new Article( $this->title
);
213 $pageId = $article->getId();
215 # must create the page...
216 $pageId = $article->insertOn( $dbw );
219 # FIXME: Check for exact conflicts
220 # FIXME: Use original rev_id optionally
221 # FIXME: blah blah blah
223 #if( $numrows > 0 ) {
224 # return wfMsg( "importhistoryconflict" );
228 $revision = new Revision( array(
230 'text' => $this->getText(),
231 'comment' => $this->getComment(),
233 'user_text' => $userText,
234 'timestamp' => $this->timestamp
,
235 'minor_edit' => $this->minor
,
237 $revId = $revision->insertOn( $dbw );
238 $article->updateIfNewerOn( $dbw, $revision );
248 * @subpackage SpecialPage
252 var $mPageCallback = null;
253 var $mRevisionCallback = null;
256 function WikiImporter( $source ) {
257 $this->setRevisionCallback( array( &$this, "importRevision" ) );
258 $this->mSource
= $source;
261 function throwXmlError( $err ) {
262 $this->debug( "FAILURE: $err" );
263 wfDebug( "WikiImporter XML error: $err\n" );
268 function doImport() {
269 if( empty( $this->mSource
) ) {
270 return new WikiErrorMsg( "importnotext" );
273 $parser = xml_parser_create( "UTF-8" );
275 # case folding violates XML standard, turn it off
276 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
278 xml_set_object( $parser, $this );
279 xml_set_element_handler( $parser, "in_start", "" );
281 $offset = 0; // for context extraction on error reporting
283 $chunk = $this->mSource
->readChunk();
284 if( !xml_parse( $parser, $chunk, $this->mSource
->atEnd() ) ) {
285 wfDebug( "WikiImporter::doImport encountered XML parsing error\n" );
286 return new WikiXmlError( $parser, 'XML import parse failure', $chunk, $offset );
288 $offset +
= strlen( $chunk );
289 } while( $chunk !== false && !$this->mSource
->atEnd() );
290 xml_parser_free( $parser );
295 function debug( $data ) {
296 #wfDebug( "IMPORT: $data\n" );
299 function notice( $data ) {
300 global $wgCommandLineMode;
301 if( $wgCommandLineMode ) {
305 $wgOut->addHTML( "<li>$data</li>\n" );
310 * Sets the action to perform as each new page in the stream is reached.
311 * @param callable $callback
314 function setPageCallback( $callback ) {
315 $previous = $this->mPageCallback
;
316 $this->mPageCallback
= $callback;
321 * Sets the action to perform as each page revision is reached.
322 * @param callable $callback
325 function setRevisionCallback( $callback ) {
326 $previous = $this->mRevisionCallback
;
327 $this->mRevisionCallback
= $callback;
332 * Default per-revision callback, performs the import.
333 * @param WikiRevision $revision
336 function importRevision( &$revision ) {
337 $dbw =& wfGetDB( DB_MASTER
);
338 $dbw->deadlockLoop( array( &$revision, 'importOldRevision' ) );
342 * Alternate per-revision callback, for debugging.
343 * @param WikiRevision $revision
346 function debugRevisionHandler( &$revision ) {
347 $this->debug( "Got revision:" );
348 if( is_object( $revision->title
) ) {
349 $this->debug( "-- Title: " . $revision->title
->getPrefixedText() );
351 $this->debug( "-- Title: <invalid>" );
353 $this->debug( "-- User: " . $revision->user_text
);
354 $this->debug( "-- Timestamp: " . $revision->timestamp
);
355 $this->debug( "-- Comment: " . $revision->comment
);
356 $this->debug( "-- Text: " . $revision->text
);
360 * Notify the callback function when a new <page> is reached.
361 * @param Title $title
364 function pageCallback( $title ) {
365 if( is_callable( $this->mPageCallback
) ) {
366 call_user_func( $this->mPageCallback
, $title );
371 # XML parser callbacks from here out -- beware!
372 function donothing( $parser, $x, $y="" ) {
373 #$this->debug( "donothing" );
376 function in_start( $parser, $name, $attribs ) {
377 $this->debug( "in_start $name" );
378 if( $name != "mediawiki" ) {
379 return $this->throwXMLerror( "Expected <mediawiki>, got <$name>" );
381 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
384 function in_mediawiki( $parser, $name, $attribs ) {
385 $this->debug( "in_mediawiki $name" );
386 if( $name == 'siteinfo' ) {
387 xml_set_element_handler( $parser, "in_siteinfo", "out_siteinfo" );
388 } elseif( $name == 'page' ) {
389 xml_set_element_handler( $parser, "in_page", "out_page" );
391 return $this->throwXMLerror( "Expected <page>, got <$name>" );
394 function out_mediawiki( $parser, $name ) {
395 $this->debug( "out_mediawiki $name" );
396 if( $name != "mediawiki" ) {
397 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
399 xml_set_element_handler( $parser, "donothing", "donothing" );
403 function in_siteinfo( $parser, $name, $attribs ) {
405 $this->debug( "in_siteinfo $name" );
415 return $this->throwXMLerror( "Element <$name> not allowed in <siteinfo>." );
419 function out_siteinfo( $parser, $name ) {
420 if( $name == "siteinfo" ) {
421 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
426 function in_page( $parser, $name, $attribs ) {
427 $this->debug( "in_page $name" );
432 $this->appendfield
= $name;
433 $this->appenddata
= "";
434 $this->parenttag
= "page";
435 xml_set_element_handler( $parser, "in_nothing", "out_append" );
436 xml_set_character_data_handler( $parser, "char_append" );
439 $this->workRevision
= new WikiRevision
;
440 $this->workRevision
->setTitle( $this->workTitle
);
441 xml_set_element_handler( $parser, "in_revision", "out_revision" );
444 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
448 function out_page( $parser, $name ) {
449 $this->debug( "out_page $name" );
450 if( $name != "page" ) {
451 return $this->throwXMLerror( "Expected </page>, got </$name>" );
453 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
455 $this->workTitle
= NULL;
456 $this->workRevision
= NULL;
459 function in_nothing( $parser, $name, $attribs ) {
460 $this->debug( "in_nothing $name" );
461 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
463 function char_append( $parser, $data ) {
464 $this->debug( "char_append '$data'" );
465 $this->appenddata
.= $data;
467 function out_append( $parser, $name ) {
468 $this->debug( "out_append $name" );
469 if( $name != $this->appendfield
) {
470 return $this->throwXMLerror( "Expected </{$this->appendfield}>, got </$name>" );
472 xml_set_element_handler( $parser, "in_$this->parenttag", "out_$this->parenttag" );
473 xml_set_character_data_handler( $parser, "donothing" );
475 switch( $this->appendfield
) {
477 $this->workTitle
= $this->appenddata
;
478 $this->pageCallback( $this->workTitle
);
481 if ( $this->parenttag
== 'revision' ) {
482 $this->workRevision
->setID( $this->appenddata
);
486 $this->workRevision
->setText( $this->appenddata
);
489 $this->workRevision
->setUsername( $this->appenddata
);
492 $this->workRevision
->setUserIP( $this->appenddata
);
495 $this->workRevision
->setTimestamp( $this->appenddata
);
498 $this->workRevision
->setComment( $this->appenddata
);
501 $this->workRevision
->setMinor( true );
504 $this->debug( "Bad append: {$this->appendfield}" );
506 $this->appendfield
= "";
507 $this->appenddata
= "";
510 function in_revision( $parser, $name, $attribs ) {
511 $this->debug( "in_revision $name" );
518 $this->parenttag
= "revision";
519 $this->appendfield
= $name;
520 xml_set_element_handler( $parser, "in_nothing", "out_append" );
521 xml_set_character_data_handler( $parser, "char_append" );
524 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
527 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
531 function out_revision( $parser, $name ) {
532 $this->debug( "out_revision $name" );
533 if( $name != "revision" ) {
534 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
536 xml_set_element_handler( $parser, "in_page", "out_page" );
538 $out = call_user_func_array( $this->mRevisionCallback
,
539 array( &$this->workRevision
, &$this ) );
540 if( !empty( $out ) ) {
542 $wgOut->addHTML( "<li>" . $out . "</li>\n" );
546 function in_contributor( $parser, $name, $attribs ) {
547 $this->debug( "in_contributor $name" );
552 $this->parenttag
= "contributor";
553 $this->appendfield
= $name;
554 xml_set_element_handler( $parser, "in_nothing", "out_append" );
555 xml_set_character_data_handler( $parser, "char_append" );
558 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
562 function out_contributor( $parser, $name ) {
563 $this->debug( "out_contributor $name" );
564 if( $name != "contributor" ) {
565 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
567 xml_set_element_handler( $parser, "in_revision", "out_revision" );
572 /** @package MediaWiki */
573 class ImportStringSource
{
574 function ImportStringSource( $string ) {
575 $this->mString
= $string;
576 $this->mRead
= false;
583 function readChunk() {
584 if( $this->atEnd() ) {
588 return $this->mString
;
593 /** @package MediaWiki */
594 class ImportStreamSource
{
595 function ImportStreamSource( $handle ) {
596 $this->mHandle
= $handle;
600 return feof( $this->mHandle
);
603 function readChunk() {
604 return fread( $this->mHandle
, 32768 );
607 function newFromFile( $filename ) {
608 $file = @fopen
( $filename, 'rt' );
610 return new WikiError( "Couldn't open import file" );
612 return new ImportStreamSource( $file );
615 function newFromUpload( $fieldname = "xmlimport" ) {
616 $upload =& $_FILES[$fieldname];
618 if( !isset( $upload ) ||
!$upload['name'] ) {
619 return new WikiErrorMsg( 'importnofile' );
621 if( !empty( $upload['error'] ) ) {
622 return new WikiErrorMsg( 'importuploaderror', $upload['error'] );
624 $fname = $upload['tmp_name'];
625 if( is_uploaded_file( $fname ) ) {
626 return ImportStreamSource
::newFromFile( $fname );
628 return new WikiErrorMsg( 'importnofile' );
632 function newFromURL( $url ) {
633 # fopen-wrappers are normally turned off for security.
634 ini_set( "allow_url_fopen", true );
635 $ret = ImportStreamSource
::newFromFile( $url );
636 ini_set( "allow_url_fopen", false );
640 function newFromInterwiki( $interwiki, $page ) {
641 $base = Title
::getInterwikiLink( $interwiki );
642 if( empty( $base ) ) {
643 return new WikiError( 'Bad interwiki link' );
645 $import = wfUrlencode( "Special:Export/$page" );
646 $url = str_replace( "$1", $import, $base );
647 return ImportStreamSource
::newFromURL( $url );