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";
133 function setTitle( $text ) {
134 $this->title
= Title
::newFromText( $text );
137 function setTimestamp( $ts ) {
138 # 2003-08-05T18:30:02Z
139 $this->timestamp
= preg_replace( '/^(....)-(..)-(..)T(..):(..):(..)Z$/', '$1$2$3$4$5$6', $ts );
142 function setUsername( $user ) {
143 $this->user_text
= $user;
146 function setUserIP( $ip ) {
147 $this->user_text
= $ip;
150 function setText( $text ) {
154 function setComment( $text ) {
155 $this->comment
= $text;
158 function setMinor( $minor ) {
159 $this->minor
= (bool)$minor;
162 function getTitle() {
166 function getTimestamp() {
167 return $this->timestamp
;
171 return $this->user_text
;
178 function getComment() {
179 return $this->comment
;
182 function getMinor() {
186 function importOldRevision() {
187 $fname = "WikiImporter::importOldRevision";
188 $dbw =& wfGetDB( DB_MASTER
);
190 # Sneak a single revision into place
191 $user = User
::newFromName( $this->getUser() );
193 $userId = intval( $user->getId() );
194 $userText = $user->getName();
197 $userText = $this->getUser();
200 // avoid memory leak...?
202 $wgLinkCache->clear();
204 $article = new Article( $this->title
);
205 $pageId = $article->getId();
207 # must create the page...
208 $pageId = $article->insertOn( $dbw );
211 # FIXME: Check for exact conflicts
212 # FIXME: Use original rev_id optionally
213 # FIXME: blah blah blah
215 #if( $numrows > 0 ) {
216 # return wfMsg( "importhistoryconflict" );
220 $revision = new Revision( array(
222 'text' => $this->getText(),
223 'comment' => $this->getComment(),
225 'user_text' => $userText,
226 'timestamp' => $this->timestamp
,
227 'minor_edit' => $this->minor
,
229 $revId = $revision->insertOn( $dbw );
230 $article->updateIfNewerOn( $dbw, $revision );
240 * @subpackage SpecialPage
244 var $mPageCallback = null;
245 var $mRevisionCallback = null;
248 function WikiImporter( $source ) {
249 $this->setRevisionCallback( array( &$this, "importRevision" ) );
250 $this->mSource
= $source;
253 function throwXmlError( $err ) {
254 $this->debug( "FAILURE: $err" );
255 wfDebug( "WikiImporter XML error: $err\n" );
260 function doImport() {
261 if( empty( $this->mSource
) ) {
262 return new WikiErrorMsg( "importnotext" );
265 $parser = xml_parser_create( "UTF-8" );
267 # case folding violates XML standard, turn it off
268 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
270 xml_set_object( $parser, &$this );
271 xml_set_element_handler( $parser, "in_start", "" );
273 $offset = 0; // for context extraction on error reporting
275 $chunk = $this->mSource
->readChunk();
276 if( !xml_parse( $parser, $chunk, $this->mSource
->atEnd() ) ) {
277 wfDebug( "WikiImporter::doImport encountered XML parsing error\n" );
278 return new WikiXmlError( $parser, 'XML import parse failure', $chunk, $offset );
280 $offset +
= strlen( $chunk );
281 } while( $chunk !== false && !$this->mSource
->atEnd() );
282 xml_parser_free( $parser );
287 function debug( $data ) {
288 #wfDebug( "IMPORT: $data\n" );
291 function notice( $data ) {
292 global $wgCommandLineMode;
293 if( $wgCommandLineMode ) {
297 $wgOut->addHTML( "<li>$data</li>\n" );
302 * Sets the action to perform as each new page in the stream is reached.
303 * @param callable $callback
306 function setPageCallback( $callback ) {
307 $previous = $this->mPageCallback
;
308 $this->mPageCallback
= $callback;
313 * Sets the action to perform as each page revision is reached.
314 * @param callable $callback
317 function setRevisionCallback( $callback ) {
318 $previous = $this->mRevisionCallback
;
319 $this->mRevisionCallback
= $callback;
324 * Default per-revision callback, performs the import.
325 * @param WikiRevision $revision
328 function importRevision( &$revision ) {
329 $dbw =& wfGetDB( DB_MASTER
);
330 $dbw->deadlockLoop( array( &$revision, 'importOldRevision' ) );
334 * Alternate per-revision callback, for debugging.
335 * @param WikiRevision $revision
338 function debugRevisionHandler( &$revision ) {
339 $this->debug( "Got revision:" );
340 if( is_object( $revision->title
) ) {
341 $this->debug( "-- Title: " . $revision->title
->getPrefixedText() );
343 $this->debug( "-- Title: <invalid>" );
345 $this->debug( "-- User: " . $revision->user_text
);
346 $this->debug( "-- Timestamp: " . $revision->timestamp
);
347 $this->debug( "-- Comment: " . $revision->comment
);
348 $this->debug( "-- Text: " . $revision->text
);
352 * Notify the callback function when a new <page> is reached.
353 * @param Title $title
356 function pageCallback( $title ) {
357 if( is_callable( $this->mPageCallback
) ) {
358 call_user_func( $this->mPageCallback
, $title );
363 # XML parser callbacks from here out -- beware!
364 function donothing( $parser, $x, $y="" ) {
365 #$this->debug( "donothing" );
368 function in_start( $parser, $name, $attribs ) {
369 $this->debug( "in_start $name" );
370 if( $name != "mediawiki" ) {
371 return $this->throwXMLerror( "Expected <mediawiki>, got <$name>" );
373 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
376 function in_mediawiki( $parser, $name, $attribs ) {
377 $this->debug( "in_mediawiki $name" );
378 if( $name == 'siteinfo' ) {
379 xml_set_element_handler( $parser, "in_siteinfo", "out_siteinfo" );
380 } elseif( $name == 'page' ) {
381 xml_set_element_handler( $parser, "in_page", "out_page" );
383 return $this->throwXMLerror( "Expected <page>, got <$name>" );
386 function out_mediawiki( $parser, $name ) {
387 $this->debug( "out_mediawiki $name" );
388 if( $name != "mediawiki" ) {
389 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
391 xml_set_element_handler( $parser, "donothing", "donothing" );
395 function in_siteinfo( $parser, $name, $attribs ) {
397 $this->debug( "in_siteinfo $name" );
407 return $this->throwXMLerror( "Element <$name> not allowed in <siteinfo>." );
411 function out_siteinfo( $parser, $name ) {
412 if( $name == "siteinfo" ) {
413 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
418 function in_page( $parser, $name, $attribs ) {
419 $this->debug( "in_page $name" );
424 $this->appendfield
= $name;
425 $this->appenddata
= "";
426 $this->parenttag
= "page";
427 xml_set_element_handler( $parser, "in_nothing", "out_append" );
428 xml_set_character_data_handler( $parser, "char_append" );
431 $this->workRevision
= new WikiRevision
;
432 $this->workRevision
->setTitle( $this->workTitle
);
433 xml_set_element_handler( $parser, "in_revision", "out_revision" );
436 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
440 function out_page( $parser, $name ) {
441 $this->debug( "out_page $name" );
442 if( $name != "page" ) {
443 return $this->throwXMLerror( "Expected </page>, got </$name>" );
445 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
447 $this->workTitle
= NULL;
448 $this->workRevision
= NULL;
451 function in_nothing( $parser, $name, $attribs ) {
452 $this->debug( "in_nothing $name" );
453 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
455 function char_append( $parser, $data ) {
456 $this->debug( "char_append '$data'" );
457 $this->appenddata
.= $data;
459 function out_append( $parser, $name ) {
460 $this->debug( "out_append $name" );
461 if( $name != $this->appendfield
) {
462 return $this->throwXMLerror( "Expected </{$this->appendfield}>, got </$name>" );
464 xml_set_element_handler( $parser, "in_$this->parenttag", "out_$this->parenttag" );
465 xml_set_character_data_handler( $parser, "donothing" );
467 switch( $this->appendfield
) {
469 $this->workTitle
= $this->appenddata
;
470 $this->pageCallback( $this->workTitle
);
473 $this->workRevision
->setText( $this->appenddata
);
476 $this->workRevision
->setUsername( $this->appenddata
);
479 $this->workRevision
->setUserIP( $this->appenddata
);
482 $this->workRevision
->setTimestamp( $this->appenddata
);
485 $this->workRevision
->setComment( $this->appenddata
);
488 $this->workRevision
->setMinor( true );
491 $this->debug( "Bad append: {$this->appendfield}" );
493 $this->appendfield
= "";
494 $this->appenddata
= "";
497 function in_revision( $parser, $name, $attribs ) {
498 $this->debug( "in_revision $name" );
505 $this->parenttag
= "revision";
506 $this->appendfield
= $name;
507 xml_set_element_handler( $parser, "in_nothing", "out_append" );
508 xml_set_character_data_handler( $parser, "char_append" );
511 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
514 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
518 function out_revision( $parser, $name ) {
519 $this->debug( "out_revision $name" );
520 if( $name != "revision" ) {
521 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
523 xml_set_element_handler( $parser, "in_page", "out_page" );
525 $out = call_user_func( $this->mRevisionCallback
,
526 &$this->workRevision
,
528 if( !empty( $out ) ) {
530 $wgOut->addHTML( "<li>" . $out . "</li>\n" );
534 function in_contributor( $parser, $name, $attribs ) {
535 $this->debug( "in_contributor $name" );
540 $this->parenttag
= "contributor";
541 $this->appendfield
= $name;
542 xml_set_element_handler( $parser, "in_nothing", "out_append" );
543 xml_set_character_data_handler( $parser, "char_append" );
546 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
550 function out_contributor( $parser, $name ) {
551 $this->debug( "out_contributor $name" );
552 if( $name != "contributor" ) {
553 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
555 xml_set_element_handler( $parser, "in_revision", "out_revision" );
560 /** @package MediaWiki */
561 class ImportStringSource
{
562 function ImportStringSource( $string ) {
563 $this->mString
= $string;
564 $this->mRead
= false;
571 function readChunk() {
572 if( $this->atEnd() ) {
576 return $this->mString
;
581 /** @package MediaWiki */
582 class ImportStreamSource
{
583 function ImportStreamSource( $handle ) {
584 $this->mHandle
= $handle;
588 return feof( $this->mHandle
);
591 function readChunk() {
592 return fread( $this->mHandle
, 32768 );
595 function newFromFile( $filename ) {
596 $file = @fopen
( $filename, 'rt' );
598 return new WikiError( "Couldn't open import file" );
600 return new ImportStreamSource( $file );
603 function newFromUpload( $fieldname = "xmlimport" ) {
606 $upload =& $_FILES[$fieldname];
608 if( !isset( $upload ) ) {
609 return new WikiErrorMsg( 'importnofile' );
611 if( !empty( $upload['error'] ) ) {
612 return new WikiErrorMsg( 'importuploaderror', $upload['error'] );
614 $fname = $upload['tmp_name'];
615 if( is_uploaded_file( $fname ) ) {
616 return ImportStreamSource
::newFromFile( $fname );
618 return new WikiErrorMsg( 'importnofile' );
622 function newFromURL( $url ) {
623 # fopen-wrappers are normally turned off for security.
624 ini_set( "allow_url_fopen", true );
625 $ret = ImportStreamSource
::newFromFile( $url );
626 ini_set( "allow_url_fopen", false );
630 function newFromInterwiki( $interwiki, $page ) {
631 $base = Title
::getInterwikiLink( $interwiki );
632 if( empty( $base ) ) {
633 return new WikiError( 'Bad interwiki link' );
635 $import = wfUrlencode( "Special:Export/$page" );
636 $url = str_replace( "$1", $import, $base );
637 return ImportStreamSource
::newFromURL( $url );