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
29 function wfSpecialImport( $page = '' ) {
30 global $wgUser, $wgOut, $wgRequest, $wgTitle, $wgImportSources;
31 global $wgImportTargetNamespace;
34 $namespace = $wgImportTargetNamespace;
38 if( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit') {
40 $namespace = $wgRequest->getIntOrNull( 'namespace' );
42 switch( $wgRequest->getVal( "source" ) ) {
45 if( $wgUser->isAllowed( 'importupload' ) ) {
46 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
48 return $wgOut->permissionRequired( 'importupload' );
52 $interwiki = $wgRequest->getVal( 'interwiki' );
53 $history = $wgRequest->getCheck( 'interwikiHistory' );
54 $frompage = $wgRequest->getText( "frompage" );
55 $source = ImportStreamSource
::newFromInterwiki(
61 $source = new WikiErrorMsg( "importunknownsource" );
64 if( WikiError
::isError( $source ) ) {
65 $wgOut->addWikiText( wfEscapeWikiText( $source->getMessage() ) );
67 $wgOut->addWikiText( wfMsg( "importstart" ) );
69 $importer = new WikiImporter( $source );
70 if( !is_null( $namespace ) ) {
71 $importer->setTargetNamespace( $namespace );
73 $reporter = new ImportReporter( $importer, $isUpload, $interwiki );
76 $result = $importer->doImport();
79 if( WikiError
::isError( $result ) ) {
80 $wgOut->addWikiText( wfMsg( "importfailed",
81 wfEscapeWikiText( $result->getMessage() ) ) );
84 $wgOut->addWikiText( wfMsg( "importsuccess" ) );
89 $action = $wgTitle->escapeLocalUrl( 'action=submit' );
91 if( $wgUser->isAllowed( 'importupload' ) ) {
92 $wgOut->addWikiText( wfMsg( "importtext" ) );
95 <legend>" . wfMsgHtml('upload') . "</legend>
96 <form enctype='multipart/form-data' method='post' action=\"$action\">
97 <input type='hidden' name='action' value='submit' />
98 <input type='hidden' name='source' value='upload' />
99 <input type='hidden' name='MAX_FILE_SIZE' value='2000000' />
100 <input type='file' name='xmlimport' value='' size='30' />
101 <input type='submit' value=\"" . wfMsgHtml( "uploadbtn" ) . "\" />
106 if( empty( $wgImportSources ) ) {
107 $wgOut->addWikiText( wfMsg( 'importnosources' ) );
111 if( !empty( $wgImportSources ) ) {
114 <legend>" . wfMsgHtml('importinterwiki') . "</legend>
115 <form method='post' action=\"$action\">" .
116 $wgOut->parse( wfMsg( 'import-interwiki-text' ) ) . "
117 <input type='hidden' name='action' value='submit' />
118 <input type='hidden' name='source' value='interwiki' />
122 <select name='interwiki'>" );
123 foreach( $wgImportSources as $prefix ) {
124 $iw = htmlspecialchars( $prefix );
125 $selected = ($interwiki === $prefix) ?
' selected="selected"' : '';
126 $wgOut->addHTML( "<option value=\"$iw\"$selected>$iw</option>\n" );
132 wfInput( 'frompage', 50, $frompage ) .
138 wfCheckLabel( wfMsg( 'import-interwiki-history' ),
139 'interwikiHistory', 'interwikiHistory', $history ) .
145 " . wfMsgHtml( 'import-interwiki-namespace' ) . " " .
146 HTMLnamespaceselector( $namespace, '' ) . "
152 wfSubmitButton( wfMsg( 'import-interwiki-submit' ) ) .
165 class ImportReporter
{
166 function __construct( $importer, $upload, $interwiki ) {
167 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
168 $this->mPageCount
= 0;
169 $this->mIsUpload
= $upload;
170 $this->mInterwiki
= $interwiki;
175 $wgOut->addHtml( "<ul>\n" );
178 function reportPage( $title, $origTitle, $revisionCount ) {
179 global $wgOut, $wgUser, $wgLang, $wgContLang;
181 $skin = $wgUser->getSkin();
185 $localCount = $wgLang->formatNum( $revisionCount );
186 $contentCount = $wgContLang->formatNum( $revisionCount );
188 $wgOut->addHtml( "<li>" . $skin->makeKnownLinkObj( $title ) .
190 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
193 $log = new LogPage( 'import' );
194 if( $this->mIsUpload
) {
195 $detail = wfMsgForContent( 'import-logentry-upload-detail',
197 $log->addEntry( 'upload', $title, $detail );
199 $interwiki = '[[:' . $this->mInterwiki
. ':' .
200 $origTitle->getPrefixedText() . ']]';
201 $detail = wfMsgForContent( 'import-logentry-interwiki-detail',
202 $contentCount, $interwiki );
203 $log->addEntry( 'interwiki', $title, $detail );
206 $comment = $detail; // quick
207 $dbw = wfGetDB( DB_MASTER
);
208 $nullRevision = Revision
::newNullRevision(
209 $dbw, $title->getArticleId(), $comment, true );
210 $nullRevId = $nullRevision->insertOn( $dbw );
215 if( $this->mPageCount
== 0 ) {
216 $wgOut->addHtml( "<li>" . wfMsgHtml( 'importnopages' ) . "</li>\n" );
218 $wgOut->addHtml( "</ul>\n" );
225 * @subpackage SpecialPage
230 var $timestamp = "20010115000000";
237 function setTitle( $title ) {
238 if( is_object( $title ) ) {
239 $this->title
= $title;
240 } elseif( is_null( $title ) ) {
241 throw new MWException( "WikiRevision given a null title in import." );
243 throw new MWException( "WikiRevision given non-object title in import." );
247 function setID( $id ) {
251 function setTimestamp( $ts ) {
252 # 2003-08-05T18:30:02Z
253 $this->timestamp
= wfTimestamp( TS_MW
, $ts );
256 function setUsername( $user ) {
257 $this->user_text
= $user;
260 function setUserIP( $ip ) {
261 $this->user_text
= $ip;
264 function setText( $text ) {
268 function setComment( $text ) {
269 $this->comment
= $text;
272 function setMinor( $minor ) {
273 $this->minor
= (bool)$minor;
276 function getTitle() {
284 function getTimestamp() {
285 return $this->timestamp
;
289 return $this->user_text
;
296 function getComment() {
297 return $this->comment
;
300 function getMinor() {
304 function importOldRevision() {
305 $fname = "WikiImporter::importOldRevision";
306 $dbw =& wfGetDB( DB_MASTER
);
308 # Sneak a single revision into place
309 $user = User
::newFromName( $this->getUser() );
311 $userId = intval( $user->getId() );
312 $userText = $user->getName();
315 $userText = $this->getUser();
318 // avoid memory leak...?
319 $linkCache =& LinkCache
::singleton();
322 $article = new Article( $this->title
);
323 $pageId = $article->getId();
325 # must create the page...
326 $pageId = $article->insertOn( $dbw );
332 # FIXME: Check for exact conflicts
333 # FIXME: Use original rev_id optionally
334 # FIXME: blah blah blah
336 #if( $numrows > 0 ) {
337 # return wfMsg( "importhistoryconflict" );
341 $revision = new Revision( array(
343 'text' => $this->getText(),
344 'comment' => $this->getComment(),
346 'user_text' => $userText,
347 'timestamp' => $this->timestamp
,
348 'minor_edit' => $this->minor
,
350 $revId = $revision->insertOn( $dbw );
351 $changed = $article->updateIfNewerOn( $dbw, $revision );
354 wfDebug( __METHOD__
. ": running onArticleCreate\n" );
355 Article
::onArticleCreate( $this->title
);
358 wfDebug( __METHOD__
. ": running onArticleEdit\n" );
359 Article
::onArticleEdit( $this->title
);
362 if( $created ||
$changed ) {
363 wfDebug( __METHOD__
. ": running edit updates\n" );
364 $article->editUpdates(
380 * @subpackage SpecialPage
384 var $mPageCallback = null;
385 var $mPageOutCallback = null;
386 var $mRevisionCallback = null;
387 var $mTargetNamespace = null;
390 function WikiImporter( $source ) {
391 $this->setRevisionCallback( array( &$this, "importRevision" ) );
392 $this->mSource
= $source;
395 function throwXmlError( $err ) {
396 $this->debug( "FAILURE: $err" );
397 wfDebug( "WikiImporter XML error: $err\n" );
402 function doImport() {
403 if( empty( $this->mSource
) ) {
404 return new WikiErrorMsg( "importnotext" );
407 $parser = xml_parser_create( "UTF-8" );
409 # case folding violates XML standard, turn it off
410 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
412 xml_set_object( $parser, $this );
413 xml_set_element_handler( $parser, "in_start", "" );
415 $offset = 0; // for context extraction on error reporting
417 $chunk = $this->mSource
->readChunk();
418 if( !xml_parse( $parser, $chunk, $this->mSource
->atEnd() ) ) {
419 wfDebug( "WikiImporter::doImport encountered XML parsing error\n" );
420 return new WikiXmlError( $parser, 'XML import parse failure', $chunk, $offset );
422 $offset +
= strlen( $chunk );
423 } while( $chunk !== false && !$this->mSource
->atEnd() );
424 xml_parser_free( $parser );
429 function debug( $data ) {
430 #wfDebug( "IMPORT: $data\n" );
433 function notice( $data ) {
434 global $wgCommandLineMode;
435 if( $wgCommandLineMode ) {
439 $wgOut->addHTML( "<li>$data</li>\n" );
444 * Sets the action to perform as each new page in the stream is reached.
445 * @param callable $callback
448 function setPageCallback( $callback ) {
449 $previous = $this->mPageCallback
;
450 $this->mPageCallback
= $callback;
455 * Sets the action to perform as each page in the stream is completed.
456 * Callback accepts the page title (as a Title object), a second object
457 * with the original title form (in case it's been overridden into a
458 * local namespace), and a count of revisions.
460 * @param callable $callback
463 function setPageOutCallback( $callback ) {
464 $previous = $this->mPageOutCallback
;
465 $this->mPageOutCallback
= $callback;
470 * Sets the action to perform as each page revision is reached.
471 * @param callable $callback
474 function setRevisionCallback( $callback ) {
475 $previous = $this->mRevisionCallback
;
476 $this->mRevisionCallback
= $callback;
481 * Set a target namespace to override the defaults
483 function setTargetNamespace( $namespace ) {
484 if( is_null( $namespace ) ) {
485 // Don't override namespaces
486 $this->mTargetNamespace
= null;
487 } elseif( $namespace >= 0 ) {
488 // FIXME: Check for validity
489 $this->mTargetNamespace
= intval( $namespace );
496 * Default per-revision callback, performs the import.
497 * @param WikiRevision $revision
500 function importRevision( &$revision ) {
501 $dbw =& wfGetDB( DB_MASTER
);
502 $dbw->deadlockLoop( array( &$revision, 'importOldRevision' ) );
506 * Alternate per-revision callback, for debugging.
507 * @param WikiRevision $revision
510 function debugRevisionHandler( &$revision ) {
511 $this->debug( "Got revision:" );
512 if( is_object( $revision->title
) ) {
513 $this->debug( "-- Title: " . $revision->title
->getPrefixedText() );
515 $this->debug( "-- Title: <invalid>" );
517 $this->debug( "-- User: " . $revision->user_text
);
518 $this->debug( "-- Timestamp: " . $revision->timestamp
);
519 $this->debug( "-- Comment: " . $revision->comment
);
520 $this->debug( "-- Text: " . $revision->text
);
524 * Notify the callback function when a new <page> is reached.
525 * @param Title $title
528 function pageCallback( $title ) {
529 if( is_callable( $this->mPageCallback
) ) {
530 call_user_func( $this->mPageCallback
, $title );
535 * Notify the callback function when a </page> is closed.
536 * @param Title $title
537 * @param Title $origTitle
538 * @param int $revisionCount
541 function pageOutCallback( $title, $origTitle, $revisionCount ) {
542 if( is_callable( $this->mPageOutCallback
) ) {
543 call_user_func( $this->mPageOutCallback
, $title, $origTitle,
549 # XML parser callbacks from here out -- beware!
550 function donothing( $parser, $x, $y="" ) {
551 #$this->debug( "donothing" );
554 function in_start( $parser, $name, $attribs ) {
555 $this->debug( "in_start $name" );
556 if( $name != "mediawiki" ) {
557 return $this->throwXMLerror( "Expected <mediawiki>, got <$name>" );
559 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
562 function in_mediawiki( $parser, $name, $attribs ) {
563 $this->debug( "in_mediawiki $name" );
564 if( $name == 'siteinfo' ) {
565 xml_set_element_handler( $parser, "in_siteinfo", "out_siteinfo" );
566 } elseif( $name == 'page' ) {
567 $this->workRevisionCount
= 0;
568 xml_set_element_handler( $parser, "in_page", "out_page" );
570 return $this->throwXMLerror( "Expected <page>, got <$name>" );
573 function out_mediawiki( $parser, $name ) {
574 $this->debug( "out_mediawiki $name" );
575 if( $name != "mediawiki" ) {
576 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
578 xml_set_element_handler( $parser, "donothing", "donothing" );
582 function in_siteinfo( $parser, $name, $attribs ) {
584 $this->debug( "in_siteinfo $name" );
594 return $this->throwXMLerror( "Element <$name> not allowed in <siteinfo>." );
598 function out_siteinfo( $parser, $name ) {
599 if( $name == "siteinfo" ) {
600 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
605 function in_page( $parser, $name, $attribs ) {
606 $this->debug( "in_page $name" );
611 $this->appendfield
= $name;
612 $this->appenddata
= "";
613 $this->parenttag
= "page";
614 xml_set_element_handler( $parser, "in_nothing", "out_append" );
615 xml_set_character_data_handler( $parser, "char_append" );
618 $this->workRevision
= new WikiRevision
;
619 $this->workRevision
->setTitle( $this->pageTitle
);
620 $this->workRevisionCount++
;
621 xml_set_element_handler( $parser, "in_revision", "out_revision" );
624 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
628 function out_page( $parser, $name ) {
629 $this->debug( "out_page $name" );
630 if( $name != "page" ) {
631 return $this->throwXMLerror( "Expected </page>, got </$name>" );
633 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
635 $this->pageOutCallback( $this->pageTitle
, $this->origTitle
,
636 $this->workRevisionCount
);
638 $this->workTitle
= null;
639 $this->workRevision
= null;
640 $this->workRevisionCount
= 0;
641 $this->pageTitle
= null;
642 $this->origTitle
= null;
645 function in_nothing( $parser, $name, $attribs ) {
646 $this->debug( "in_nothing $name" );
647 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
649 function char_append( $parser, $data ) {
650 $this->debug( "char_append '$data'" );
651 $this->appenddata
.= $data;
653 function out_append( $parser, $name ) {
654 $this->debug( "out_append $name" );
655 if( $name != $this->appendfield
) {
656 return $this->throwXMLerror( "Expected </{$this->appendfield}>, got </$name>" );
658 xml_set_element_handler( $parser, "in_$this->parenttag", "out_$this->parenttag" );
659 xml_set_character_data_handler( $parser, "donothing" );
661 switch( $this->appendfield
) {
663 $this->workTitle
= $this->appenddata
;
664 $this->origTitle
= Title
::newFromText( $this->workTitle
);
665 if( !is_null( $this->mTargetNamespace
) && !is_null( $this->origTitle
) ) {
666 $this->pageTitle
= Title
::makeTitle( $this->mTargetNamespace
,
667 $this->origTitle
->getDbKey() );
669 $this->pageTitle
= Title
::newFromText( $this->workTitle
);
671 $this->pageCallback( $this->workTitle
);
674 if ( $this->parenttag
== 'revision' ) {
675 $this->workRevision
->setID( $this->appenddata
);
679 $this->workRevision
->setText( $this->appenddata
);
682 $this->workRevision
->setUsername( $this->appenddata
);
685 $this->workRevision
->setUserIP( $this->appenddata
);
688 $this->workRevision
->setTimestamp( $this->appenddata
);
691 $this->workRevision
->setComment( $this->appenddata
);
694 $this->workRevision
->setMinor( true );
697 $this->debug( "Bad append: {$this->appendfield}" );
699 $this->appendfield
= "";
700 $this->appenddata
= "";
703 function in_revision( $parser, $name, $attribs ) {
704 $this->debug( "in_revision $name" );
711 $this->parenttag
= "revision";
712 $this->appendfield
= $name;
713 xml_set_element_handler( $parser, "in_nothing", "out_append" );
714 xml_set_character_data_handler( $parser, "char_append" );
717 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
720 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
724 function out_revision( $parser, $name ) {
725 $this->debug( "out_revision $name" );
726 if( $name != "revision" ) {
727 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
729 xml_set_element_handler( $parser, "in_page", "out_page" );
731 $out = call_user_func_array( $this->mRevisionCallback
,
732 array( &$this->workRevision
, &$this ) );
733 if( !empty( $out ) ) {
735 $wgOut->addHTML( "<li>" . $out . "</li>\n" );
739 function in_contributor( $parser, $name, $attribs ) {
740 $this->debug( "in_contributor $name" );
745 $this->parenttag
= "contributor";
746 $this->appendfield
= $name;
747 xml_set_element_handler( $parser, "in_nothing", "out_append" );
748 xml_set_character_data_handler( $parser, "char_append" );
751 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
755 function out_contributor( $parser, $name ) {
756 $this->debug( "out_contributor $name" );
757 if( $name != "contributor" ) {
758 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
760 xml_set_element_handler( $parser, "in_revision", "out_revision" );
765 /** @package MediaWiki */
766 class ImportStringSource
{
767 function ImportStringSource( $string ) {
768 $this->mString
= $string;
769 $this->mRead
= false;
776 function readChunk() {
777 if( $this->atEnd() ) {
781 return $this->mString
;
786 /** @package MediaWiki */
787 class ImportStreamSource
{
788 function ImportStreamSource( $handle ) {
789 $this->mHandle
= $handle;
793 return feof( $this->mHandle
);
796 function readChunk() {
797 return fread( $this->mHandle
, 32768 );
800 function newFromFile( $filename ) {
801 $file = @fopen
( $filename, 'rt' );
803 return new WikiErrorMsg( "importcantopen" );
805 return new ImportStreamSource( $file );
808 function newFromUpload( $fieldname = "xmlimport" ) {
809 $upload =& $_FILES[$fieldname];
811 if( !isset( $upload ) ||
!$upload['name'] ) {
812 return new WikiErrorMsg( 'importnofile' );
814 if( !empty( $upload['error'] ) ) {
815 return new WikiErrorMsg( 'importuploaderror', $upload['error'] );
817 $fname = $upload['tmp_name'];
818 if( is_uploaded_file( $fname ) ) {
819 return ImportStreamSource
::newFromFile( $fname );
821 return new WikiErrorMsg( 'importnofile' );
825 function newFromURL( $url ) {
826 wfDebug( __METHOD__
. ": opening $url\n" );
827 # fopen-wrappers are normally turned off for security.
828 ini_set( "allow_url_fopen", true );
829 $ret = ImportStreamSource
::newFromFile( $url );
830 ini_set( "allow_url_fopen", false );
834 function newFromInterwiki( $interwiki, $page, $history=false ) {
835 $base = Title
::getInterwikiLink( $interwiki );
836 $link = Title
::newFromText( "$interwiki:Special:Export/$page" );
837 if( empty( $base ) ||
empty( $link ) ) {
838 return new WikiErrorMsg( 'importbadinterwiki' );
840 $params = $history ?
'history=1' : '';
841 $url = $link->getFullUrl( $params );
842 return ImportStreamSource
::newFromURL( $url );