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, $wgLang, $wgRequest, $wgTitle;
34 global $wgImportSources;
37 # $wgOut->addWikiText( "Special:Import is not ready for this beta release, sorry." );
41 if( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit') {
42 switch( $wgRequest->getVal( "source" ) ) {
44 if( $wgUser->isAllowed( 'importupload' ) ) {
45 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
47 return $wgOut->permissionRequired( 'importupload' );
51 $source = ImportStreamSource
::newFromInterwiki(
52 $wgRequest->getVal( "interwiki" ),
53 $wgRequest->getText( "frompage" ) );
56 $source = new WikiError( "Unknown import source type" );
59 if( WikiError
::isError( $source ) ) {
60 $wgOut->addWikiText( wfEscapeWikiText( $source->getMessage() ) );
62 $importer = new WikiImporter( $source );
63 $result = $importer->doImport();
64 if( WikiError
::isError( $result ) ) {
65 $wgOut->addWikiText( wfMsg( "importfailed",
66 wfEscapeWikiText( $result->getMessage() ) ) );
69 $wgOut->addWikiText( wfMsg( "importsuccess" ) );
74 $action = $wgTitle->escapeLocalUrl( 'action=submit' );
76 if( $wgUser->isAllowed( 'importupload' ) ) {
77 $wgOut->addWikiText( wfMsg( "importtext" ) );
80 <legend>" . wfMsgHtml('upload') . "</legend>
81 <form enctype='multipart/form-data' method='post' action=\"$action\">
82 <input type='hidden' name='action' value='submit' />
83 <input type='hidden' name='source' value='upload' />
84 <input type='hidden' name='MAX_FILE_SIZE' value='2000000' />
85 <input type='file' name='xmlimport' value='' size='30' />
86 <input type='submit' value='" . wfMsgHtml( "uploadbtn" ) . "'/>
91 if( empty( $wgImportSources ) ) {
92 $wgOut->addWikiText( wfMsg( 'importnosources' ) );
96 if( !empty( $wgImportSources ) ) {
99 <legend>" . wfMsgHtml('importinterwiki') . "</legend>
100 <form method='post' action=\"$action\">
101 <input type='hidden' name='action' value='submit' />
102 <input type='hidden' name='source' value='interwiki' />
103 <select name='interwiki'>
105 foreach( $wgImportSources as $interwiki ) {
106 $iw = htmlspecialchars( $interwiki );
107 $wgOut->addHTML( "<option value=\"$iw\">$iw</option>\n" );
111 <input name='frompage' />
112 <input type='submit' />
122 * @subpackage SpecialPage
126 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
= preg_replace( '/^(....)-(..)-(..)T(..):(..):(..)Z$/', '$1$2$3$4$5$6', $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 getTitle() {
161 function getTimestamp() {
162 return $this->timestamp
;
166 return $this->user_text
;
173 function getComment() {
174 return $this->comment
;
177 function importOldRevision() {
178 $fname = "WikiImporter::importOldRevision";
179 $dbw =& wfGetDB( DB_MASTER
);
181 # Sneak a single revision into place
182 $user = User
::newFromName( $this->getUser() );
184 $userId = IntVal( $user->getId() );
185 $userText = $user->getName();
188 $userText = $this->getUser();
191 $article = new Article( $this->title
);
192 $pageId = $article->getId();
194 # must create the page...
195 $pageId = $article->insertOn( $dbw );
198 # FIXME: Check for exact conflicts
199 # FIXME: Use original rev_id optionally
200 # FIXME: blah blah blah
202 #if( $numrows > 0 ) {
203 # return wfMsg( "importhistoryconflict" );
207 $revision = new Revision( array(
209 'text' => $this->getText(),
210 'comment' => $this->getComment(),
212 'user_text' => $userText,
213 'timestamp' => $this->timestamp
,
216 $revId = $revision->insertOn( $dbw );
217 $article->updateIfNewerOn( $dbw, $revision );
227 * @subpackage SpecialPage
231 var $mPageCallback = null;
232 var $mRevisionCallback = null;
235 function WikiImporter( $source ) {
236 $this->setRevisionCallback( array( &$this, "importRevision" ) );
237 $this->mSource
= $source;
240 function throwXmlError( $err ) {
241 $this->debug( "FAILURE: $err" );
246 function doImport() {
247 if( empty( $this->mSource
) ) {
248 return new WikiErrorMsg( "importnotext" );
251 $parser = xml_parser_create( "UTF-8" );
253 # case folding violates XML standard, turn it off
254 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
256 xml_set_object( $parser, &$this );
257 xml_set_element_handler( $parser, "in_start", "" );
260 $chunk = $this->mSource
->readChunk();
261 if( !xml_parse( $parser, $chunk, $this->mSource
->atEnd() ) ) {
262 return new WikiXmlError( $parser );
264 } while( $chunk !== false && !$this->mSource
->atEnd() );
265 xml_parser_free( $parser );
270 function debug( $data ) {
271 #wfDebug( "IMPORT: $data\n" );
274 function notice( $data ) {
275 global $wgCommandLineMode;
276 if( $wgCommandLineMode ) {
280 $wgOut->addHTML( "<li>$data</li>\n" );
285 * Sets the action to perform as each new page in the stream is reached.
286 * @param callable $callback
289 function setPageCallback( $callback ) {
290 $previous = $this->mPageCallback
;
291 $this->mPageCallback
= $callback;
296 * Sets the action to perform as each page revision is reached.
297 * @param callable $callback
300 function setRevisionCallback( $callback ) {
301 $previous = $this->mRevisionCallback
;
302 $this->mRevisionCallback
= $callback;
307 * Default per-revision callback, performs the import.
308 * @param WikiRevision $revision
311 function importRevision( &$revision ) {
312 $dbw =& wfGetDB( DB_MASTER
);
313 $dbw->deadlockLoop( array( &$revision, 'importOldRevision' ) );
317 * Alternate per-revision callback, for debugging.
318 * @param WikiRevision $revision
321 function debugRevisionHandler( &$revision ) {
322 $this->debug( "Got revision:" );
323 if( is_object( $revision->title
) ) {
324 $this->debug( "-- Title: " . $revision->title
->getPrefixedText() );
326 $this->debug( "-- Title: <invalid>" );
328 $this->debug( "-- User: " . $revision->user_text
);
329 $this->debug( "-- Timestamp: " . $revision->timestamp
);
330 $this->debug( "-- Comment: " . $revision->comment
);
331 $this->debug( "-- Text: " . $revision->text
);
335 * Notify the callback function when a new <page> is reached.
336 * @param Title $title
339 function pageCallback( $title ) {
340 if( is_callable( $this->mPageCallback
) ) {
341 call_user_func( $this->mPageCallback
, $title );
346 # XML parser callbacks from here out -- beware!
347 function donothing( $parser, $x, $y="" ) {
348 #$this->debug( "donothing" );
351 function in_start( $parser, $name, $attribs ) {
352 $this->debug( "in_start $name" );
353 if( $name != "mediawiki" ) {
354 return $this->throwXMLerror( "Expected <mediawiki>, got <$name>" );
356 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
359 function in_mediawiki( $parser, $name, $attribs ) {
360 $this->debug( "in_mediawiki $name" );
361 if( $name == 'siteinfo' ) {
362 xml_set_element_handler( $parser, "in_siteinfo", "out_siteinfo" );
363 } elseif( $name == 'page' ) {
364 xml_set_element_handler( $parser, "in_page", "out_page" );
366 return $this->throwXMLerror( "Expected <page>, got <$name>" );
369 function out_mediawiki( $parser, $name ) {
370 $this->debug( "out_mediawiki $name" );
371 if( $name != "mediawiki" ) {
372 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
374 xml_set_element_handler( $parser, "donothing", "donothing" );
378 function in_siteinfo( $parser, $name, $attribs ) {
380 $this->debug( "in_siteinfo $name" );
389 return $this->throwXMLerror( "Element <$name> not allowed in <siteinfo>." );
393 function out_siteinfo( $parser, $name ) {
394 if( $name == "siteinfo" ) {
395 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
400 function in_page( $parser, $name, $attribs ) {
401 $this->debug( "in_page $name" );
406 $this->appendfield
= $name;
407 $this->appenddata
= "";
408 $this->parenttag
= "page";
409 xml_set_element_handler( $parser, "in_nothing", "out_append" );
410 xml_set_character_data_handler( $parser, "char_append" );
413 $this->workRevision
= new WikiRevision
;
414 $this->workRevision
->setTitle( $this->workTitle
);
415 xml_set_element_handler( $parser, "in_revision", "out_revision" );
418 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
422 function out_page( $parser, $name ) {
423 $this->debug( "out_page $name" );
424 if( $name != "page" ) {
425 return $this->throwXMLerror( "Expected </page>, got </$name>" );
427 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
429 $this->workTitle
= NULL;
430 $this->workRevision
= NULL;
433 function in_nothing( $parser, $name, $attribs ) {
434 $this->debug( "in_nothing $name" );
435 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
437 function char_append( $parser, $data ) {
438 $this->debug( "char_append '$data'" );
439 $this->appenddata
.= $data;
441 function out_append( $parser, $name ) {
442 $this->debug( "out_append $name" );
443 if( $name != $this->appendfield
) {
444 return $this->throwXMLerror( "Expected </{$this->appendfield}>, got </$name>" );
446 xml_set_element_handler( $parser, "in_$this->parenttag", "out_$this->parenttag" );
447 xml_set_character_data_handler( $parser, "donothing" );
449 switch( $this->appendfield
) {
451 $this->workTitle
= $this->appenddata
;
452 $this->pageCallback( $this->workTitle
);
455 $this->workRevision
->setText( $this->appenddata
);
458 $this->workRevision
->setUsername( $this->appenddata
);
461 $this->workRevision
->setUserIP( $this->appenddata
);
464 $this->workRevision
->setTimestamp( $this->appenddata
);
467 $this->workRevision
->setComment( $this->appenddata
);
470 $this->debug( "Bad append: {$this->appendfield}" );
472 $this->appendfield
= "";
473 $this->appenddata
= "";
476 function in_revision( $parser, $name, $attribs ) {
477 $this->debug( "in_revision $name" );
483 $this->parenttag
= "revision";
484 $this->appendfield
= $name;
485 xml_set_element_handler( $parser, "in_nothing", "out_append" );
486 xml_set_character_data_handler( $parser, "char_append" );
489 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
492 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
496 function out_revision( $parser, $name ) {
497 $this->debug( "out_revision $name" );
498 if( $name != "revision" ) {
499 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
501 xml_set_element_handler( $parser, "in_page", "out_page" );
503 $out = call_user_func( $this->mRevisionCallback
,
504 &$this->workRevision
,
506 if( !empty( $out ) ) {
508 $wgOut->addHTML( "<li>" . $out . "</li>\n" );
512 function in_contributor( $parser, $name, $attribs ) {
513 $this->debug( "in_contributor $name" );
517 $this->parenttag
= "contributor";
518 $this->appendfield
= $name;
519 xml_set_element_handler( $parser, "in_nothing", "out_append" );
520 xml_set_character_data_handler( $parser, "char_append" );
523 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
527 function out_contributor( $parser, $name ) {
528 $this->debug( "out_contributor $name" );
529 if( $name != "contributor" ) {
530 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
532 xml_set_element_handler( $parser, "in_revision", "out_revision" );
537 /** @package MediaWiki */
538 class ImportStringSource
{
539 function ImportStringSource( $string ) {
540 $this->mString
= $string;
541 $this->mRead
= false;
548 function readChunk() {
549 if( $this->atEnd() ) {
553 return $this->mString
;
558 /** @package MediaWiki */
559 class ImportStreamSource
{
560 function ImportStreamSource( $handle ) {
561 $this->mHandle
= $handle;
565 return feof( $this->mHandle
);
568 function readChunk() {
569 return fread( $this->mHandle
, 32768 );
572 function newFromFile( $filename ) {
573 $file = @fopen
( $filename, 'rt' );
575 return new WikiError( "Couldn't open import file" );
577 return new ImportStreamSource( $file );
580 function newFromUpload( $fieldname = "xmlimport" ) {
583 $upload =& $_FILES[$fieldname];
585 if( !isset( $upload ) ) {
586 return new WikiErrorMsg( 'importnofile' );
588 if( !empty( $upload['error'] ) ) {
589 return new WikiErrorMsg( 'importuploaderror', $upload['error'] );
591 $fname = $upload['tmp_name'];
592 if( is_uploaded_file( $fname ) ) {
593 return ImportStreamSource
::newFromFile( $fname );
595 return new WikiErrorMsg( 'importnofile' );
599 function newFromURL( $url ) {
600 # fopen-wrappers are normally turned off for security.
601 ini_set( "allow_url_fopen", true );
602 $ret = ImportStreamSource
::newFromFile( $url );
603 ini_set( "allow_url_fopen", false );
607 function newFromInterwiki( $interwiki, $page ) {
608 $base = Title
::getInterwikiLink( $interwiki );
609 if( empty( $base ) ) {
610 return new WikiError( 'Bad interwiki link' );
612 $import = wfUrlencode( "Special:Export/$page" );
613 $url = str_replace( "$1", $import, $base );
614 return ImportStreamSource
::newFromURL( $url );