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
26 require_once( 'WikiError.php' );
31 function wfSpecialImport( $page = '' ) {
32 global $wgUser, $wgOut, $wgLang, $wgRequest, $wgTitle;
33 global $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";
131 function setTitle( $text ) {
132 $this->title
= Title
::newFromText( $text );
135 function setTimestamp( $ts ) {
136 # 2003-08-05T18:30:02Z
137 $this->timestamp
= preg_replace( '/^(....)-(..)-(..)T(..):(..):(..)Z$/', '$1$2$3$4$5$6', $ts );
140 function setUsername( $user ) {
141 $this->user_text
= $user;
144 function setUserIP( $ip ) {
145 $this->user_text
= $ip;
148 function setText( $text ) {
152 function setComment( $text ) {
153 $this->comment
= $text;
156 function getTitle() {
160 function getTimestamp() {
161 return $this->timestamp
;
165 return $this->user_text
;
172 function getComment() {
173 return $this->comment
;
176 function importOldRevision() {
177 $fname = "WikiImporter::importOldRevision";
178 $dbw =& wfGetDB( DB_MASTER
);
180 # Sneak a single revision into place
181 $user = User
::newFromName( $this->getUser() );
183 $userId = IntVal( $user->getId() );
184 $userText = $user->getName();
187 $userText = $this->getUser();
190 $article = new Article( $this->title
);
191 $pageId = $article->getId();
193 # must create the page...
194 $pageId = $article->insertOn( $dbw );
197 # FIXME: Check for exact conflicts
198 # FIXME: Use original rev_id optionally
199 # FIXME: blah blah blah
201 #if( $numrows > 0 ) {
202 # return wfMsg( "importhistoryconflict" );
206 $revision = new Revision( array(
208 'text' => $this->getText(),
209 'comment' => $this->getComment(),
211 'user_text' => $userText,
212 'timestamp' => $this->timestamp
,
215 $revId = $revision->insertOn( $dbw );
216 $article->updateIfNewerOn( $dbw, $revision );
226 * @subpackage SpecialPage
230 var $mPageCallback = null;
231 var $mRevisionCallback = null;
234 function WikiImporter( $source ) {
235 $this->setRevisionCallback( array( &$this, "importRevision" ) );
236 $this->mSource
= $source;
239 function throwXmlError( $err ) {
240 $this->debug( "FAILURE: $err" );
245 function doImport() {
246 if( empty( $this->mSource
) ) {
247 return new WikiErrorMsg( "importnotext" );
250 $parser = xml_parser_create( "UTF-8" );
252 # case folding violates XML standard, turn it off
253 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
255 xml_set_object( $parser, &$this );
256 xml_set_element_handler( $parser, "in_start", "" );
259 $chunk = $this->mSource
->readChunk();
260 if( !xml_parse( $parser, $chunk, $this->mSource
->atEnd() ) ) {
261 return new WikiXmlError( $parser );
263 } while( $chunk !== false && !$this->mSource
->atEnd() );
264 xml_parser_free( $parser );
269 function debug( $data ) {
270 #wfDebug( "IMPORT: $data\n" );
273 function notice( $data ) {
274 global $wgCommandLineMode;
275 if( $wgCommandLineMode ) {
279 $wgOut->addHTML( "<li>$data</li>\n" );
284 * Sets the action to perform as each new page in the stream is reached.
285 * @param callable $callback
288 function setPageCallback( $callback ) {
289 $previous = $this->mPageCallback
;
290 $this->mPageCallback
= $callback;
295 * Sets the action to perform as each page revision is reached.
296 * @param callable $callback
299 function setRevisionCallback( $callback ) {
300 $previous = $this->mRevisionCallback
;
301 $this->mRevisionCallback
= $callback;
306 * Default per-revision callback, performs the import.
307 * @param WikiRevision $revision
310 function importRevision( &$revision ) {
311 $dbw =& wfGetDB( DB_MASTER
);
312 $dbw->deadlockLoop( array( &$revision, 'importOldRevision' ) );
316 * Alternate per-revision callback, for debugging.
317 * @param WikiRevision $revision
320 function debugRevisionHandler( &$revision ) {
321 $this->debug( "Got revision:" );
322 if( is_object( $revision->title
) ) {
323 $this->debug( "-- Title: " . $revision->title
->getPrefixedText() );
325 $this->debug( "-- Title: <invalid>" );
327 $this->debug( "-- User: " . $revision->user_text
);
328 $this->debug( "-- Timestamp: " . $revision->timestamp
);
329 $this->debug( "-- Comment: " . $revision->comment
);
330 $this->debug( "-- Text: " . $revision->text
);
334 * Notify the callback function when a new <page> is reached.
335 * @param Title $title
338 function pageCallback( $title ) {
339 if( is_callable( $this->mPageCallback
) ) {
340 call_user_func( $this->mPageCallback
, $title );
345 # XML parser callbacks from here out -- beware!
346 function donothing( $parser, $x, $y="" ) {
347 #$this->debug( "donothing" );
350 function in_start( $parser, $name, $attribs ) {
351 $this->debug( "in_start $name" );
352 if( $name != "mediawiki" ) {
353 return $this->throwXMLerror( "Expected <mediawiki>, got <$name>" );
355 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
358 function in_mediawiki( $parser, $name, $attribs ) {
359 $this->debug( "in_mediawiki $name" );
360 if( $name == 'siteinfo' ) {
361 xml_set_element_handler( $parser, "in_siteinfo", "out_siteinfo" );
362 } elseif( $name == 'page' ) {
363 xml_set_element_handler( $parser, "in_page", "out_page" );
365 return $this->throwXMLerror( "Expected <page>, got <$name>" );
368 function out_mediawiki( $parser, $name ) {
369 $this->debug( "out_mediawiki $name" );
370 if( $name != "mediawiki" ) {
371 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
373 xml_set_element_handler( $parser, "donothing", "donothing" );
377 function in_siteinfo( $parser, $name, $attribs ) {
379 $this->debug( "in_siteinfo $name" );
388 return $this->throwXMLerror( "Element <$name> not allowed in <siteinfo>." );
392 function out_siteinfo( $parser, $name ) {
393 if( $name == "siteinfo" ) {
394 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
399 function in_page( $parser, $name, $attribs ) {
400 $this->debug( "in_page $name" );
405 $this->appendfield
= $name;
406 $this->appenddata
= "";
407 $this->parenttag
= "page";
408 xml_set_element_handler( $parser, "in_nothing", "out_append" );
409 xml_set_character_data_handler( $parser, "char_append" );
412 $this->workRevision
= new WikiRevision
;
413 $this->workRevision
->setTitle( $this->workTitle
);
414 xml_set_element_handler( $parser, "in_revision", "out_revision" );
417 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
421 function out_page( $parser, $name ) {
422 $this->debug( "out_page $name" );
423 if( $name != "page" ) {
424 return $this->throwXMLerror( "Expected </page>, got </$name>" );
426 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
428 $this->workTitle
= NULL;
429 $this->workRevision
= NULL;
432 function in_nothing( $parser, $name, $attribs ) {
433 $this->debug( "in_nothing $name" );
434 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
436 function char_append( $parser, $data ) {
437 $this->debug( "char_append '$data'" );
438 $this->appenddata
.= $data;
440 function out_append( $parser, $name ) {
441 $this->debug( "out_append $name" );
442 if( $name != $this->appendfield
) {
443 return $this->throwXMLerror( "Expected </{$this->appendfield}>, got </$name>" );
445 xml_set_element_handler( $parser, "in_$this->parenttag", "out_$this->parenttag" );
446 xml_set_character_data_handler( $parser, "donothing" );
448 switch( $this->appendfield
) {
450 $this->workTitle
= $this->appenddata
;
451 $this->pageCallback( $this->workTitle
);
454 $this->workRevision
->setText( $this->appenddata
);
457 $this->workRevision
->setUsername( $this->appenddata
);
460 $this->workRevision
->setUserIP( $this->appenddata
);
463 $this->workRevision
->setTimestamp( $this->appenddata
);
466 $this->workRevision
->setComment( $this->appenddata
);
469 $this->debug( "Bad append: {$this->appendfield}" );
471 $this->appendfield
= "";
472 $this->appenddata
= "";
475 function in_revision( $parser, $name, $attribs ) {
476 $this->debug( "in_revision $name" );
482 $this->parenttag
= "revision";
483 $this->appendfield
= $name;
484 xml_set_element_handler( $parser, "in_nothing", "out_append" );
485 xml_set_character_data_handler( $parser, "char_append" );
488 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
491 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
495 function out_revision( $parser, $name ) {
496 $this->debug( "out_revision $name" );
497 if( $name != "revision" ) {
498 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
500 xml_set_element_handler( $parser, "in_page", "out_page" );
502 $out = call_user_func( $this->mRevisionCallback
,
503 &$this->workRevision
,
505 if( !empty( $out ) ) {
507 $wgOut->addHTML( "<li>" . $out . "</li>\n" );
511 function in_contributor( $parser, $name, $attribs ) {
512 $this->debug( "in_contributor $name" );
516 $this->parenttag
= "contributor";
517 $this->appendfield
= $name;
518 xml_set_element_handler( $parser, "in_nothing", "out_append" );
519 xml_set_character_data_handler( $parser, "char_append" );
522 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
526 function out_contributor( $parser, $name ) {
527 $this->debug( "out_contributor $name" );
528 if( $name != "contributor" ) {
529 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
531 xml_set_element_handler( $parser, "in_revision", "out_revision" );
536 class ImportStringSource
{
537 function ImportStringSource( $string ) {
538 $this->mString
= $string;
539 $this->mRead
= false;
546 function readChunk() {
547 if( $this->atEnd() ) {
551 return $this->mString
;
556 class ImportStreamSource
{
557 function ImportStreamSource( $handle ) {
558 $this->mHandle
= $handle;
562 return feof( $this->mHandle
);
565 function readChunk() {
566 return fread( $this->mHandle
, 32768 );
569 function newFromFile( $filename ) {
570 $file = @fopen
( $filename, 'rt' );
572 return new WikiError( "Couldn't open import file" );
574 return new ImportStreamSource( $file );
577 function newFromUpload( $fieldname = "xmlimport" ) {
580 $upload =& $_FILES[$fieldname];
582 if( !isset( $upload ) ) {
583 return new WikiErrorMsg( 'importnofile' );
585 if( !empty( $upload['error'] ) ) {
586 return new WikiErrorMsg( 'importuploaderror', $upload['error'] );
588 $fname = $upload['tmp_name'];
589 if( is_uploaded_file( $fname ) ) {
590 return ImportStreamSource
::newFromFile( $fname );
592 return new WikiErrorMsg( 'importnofile' );
596 function newFromURL( $url ) {
597 # fopen-wrappers are normally turned off for security.
598 ini_set( "allow_url_fopen", true );
599 $ret = ImportStreamSource
::newFromFile( $url );
600 ini_set( "allow_url_fopen", false );
604 function newFromInterwiki( $interwiki, $page ) {
605 $base = Title
::getInterwikiLink( $interwiki );
606 if( empty( $base ) ) {
607 return new WikiError( 'Bad interwiki link' );
609 $import = wfUrlencode( "Special:Export/$page" );
610 $url = str_replace( "$1", $import, $base );
611 return ImportStreamSource
::newFromURL( $url );