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 * @ingroup SpecialPage
29 function wfSpecialImport( $page = '' ) {
30 global $wgUser, $wgOut, $wgRequest, $wgTitle, $wgImportSources;
31 global $wgImportTargetNamespace;
34 $namespace = $wgImportTargetNamespace;
39 $wgOut->readOnlyPage();
43 if( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit') {
45 $namespace = $wgRequest->getIntOrNull( 'namespace' );
47 switch( $wgRequest->getVal( "source" ) ) {
50 if( $wgUser->isAllowed( 'importupload' ) ) {
51 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
53 return $wgOut->permissionRequired( 'importupload' );
57 $interwiki = $wgRequest->getVal( 'interwiki' );
58 $history = $wgRequest->getCheck( 'interwikiHistory' );
59 $frompage = $wgRequest->getText( "frompage" );
60 $source = ImportStreamSource
::newFromInterwiki(
66 $source = new WikiErrorMsg( "importunknownsource" );
69 if( WikiError
::isError( $source ) ) {
70 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $source->getMessage() ) );
72 $wgOut->addWikiMsg( "importstart" );
74 $importer = new WikiImporter( $source );
75 if( !is_null( $namespace ) ) {
76 $importer->setTargetNamespace( $namespace );
78 $reporter = new ImportReporter( $importer, $isUpload, $interwiki );
81 $result = $importer->doImport();
82 $resultCount = $reporter->close();
84 if( WikiError
::isError( $result ) ) {
85 # No source or XML parse error
86 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $result->getMessage() ) );
87 } elseif( WikiError
::isError( $resultCount ) ) {
89 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $resultCount->getMessage() ) );
92 $wgOut->addWikiMsg( 'importsuccess' );
94 $wgOut->addWikiText( '<hr />' );
98 $action = $wgTitle->getLocalUrl( 'action=submit' );
100 if( $wgUser->isAllowed( 'importupload' ) ) {
101 $wgOut->addWikiMsg( "importtext" );
103 Xml
::openElement( 'fieldset' ).
104 Xml
::element( 'legend', null, wfMsg( 'import-upload' ) ) .
105 Xml
::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post', 'action' => $action ) ) .
106 Xml
::hidden( 'action', 'submit' ) .
107 Xml
::hidden( 'source', 'upload' ) .
108 Xml
::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
109 Xml
::submitButton( wfMsg( 'uploadbtn' ) ) .
110 Xml
::closeElement( 'form' ) .
111 Xml
::closeElement( 'fieldset' )
114 if( empty( $wgImportSources ) ) {
115 $wgOut->addWikiMsg( 'importnosources' );
119 if( !empty( $wgImportSources ) ) {
121 Xml
::openElement( 'fieldset' ) .
122 Xml
::element( 'legend', null, wfMsg( 'importinterwiki' ) ) .
123 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action ) ) .
124 wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
125 Xml
::hidden( 'action', 'submit' ) .
126 Xml
::hidden( 'source', 'interwiki' ) .
127 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
130 Xml
::openElement( 'select', array( 'name' => 'interwiki' ) )
132 foreach( $wgImportSources as $prefix ) {
133 $selected = ( $interwiki === $prefix ) ?
' selected="selected"' : '';
134 $wgOut->addHTML( Xml
::option( $prefix, $prefix, $selected ) );
137 Xml
::closeElement( 'select' ) .
140 Xml
::input( 'frompage', 50, $frompage ) .
147 Xml
::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $history ) .
154 Xml
::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
155 Xml
::namespaceSelector( $namespace, '' ) .
162 Xml
::submitButton( wfMsg( 'import-interwiki-submit' ) ) .
165 Xml
::closeElement( 'table' ).
166 Xml
::closeElement( 'form' ) .
167 Xml
::closeElement( 'fieldset' )
174 * @ingroup SpecialPage
176 class ImportReporter
{
177 function __construct( $importer, $upload, $interwiki ) {
178 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
179 $this->mPageCount
= 0;
180 $this->mIsUpload
= $upload;
181 $this->mInterwiki
= $interwiki;
186 $wgOut->addHtml( "<ul>\n" );
189 function reportPage( $title, $origTitle, $revisionCount, $successCount ) {
190 global $wgOut, $wgUser, $wgLang, $wgContLang;
192 $skin = $wgUser->getSkin();
196 $localCount = $wgLang->formatNum( $successCount );
197 $contentCount = $wgContLang->formatNum( $successCount );
199 if( $successCount > 0 ) {
200 $wgOut->addHtml( "<li>" . $skin->makeKnownLinkObj( $title ) . " " .
201 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
205 $log = new LogPage( 'import' );
206 if( $this->mIsUpload
) {
207 $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
209 $log->addEntry( 'upload', $title, $detail );
211 $interwiki = '[[:' . $this->mInterwiki
. ':' .
212 $origTitle->getPrefixedText() . ']]';
213 $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
214 $contentCount, $interwiki );
215 $log->addEntry( 'interwiki', $title, $detail );
218 $comment = $detail; // quick
219 $dbw = wfGetDB( DB_MASTER
);
220 $nullRevision = Revision
::newNullRevision( $dbw, $title->getArticleId(), $comment, true );
221 $nullRevision->insertOn( $dbw );
222 $article = new Article( $title );
224 $article->updateRevisionOn( $dbw, $nullRevision );
225 wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, false) );
227 $wgOut->addHtml( '<li>' . wfMsgHtml( 'import-nonewrevisions' ) . '</li>' );
233 if( $this->mPageCount
== 0 ) {
234 $wgOut->addHtml( "</ul>\n" );
235 return new WikiErrorMsg( "importnopages" );
237 $wgOut->addHtml( "</ul>\n" );
239 return $this->mPageCount
;
245 * @ingroup SpecialPage
250 var $timestamp = "20010115000000";
257 function setTitle( $title ) {
258 if( is_object( $title ) ) {
259 $this->title
= $title;
260 } elseif( is_null( $title ) ) {
261 throw new MWException( "WikiRevision given a null title in import. You may need to adjust \$wgLegalTitleChars." );
263 throw new MWException( "WikiRevision given non-object title in import." );
267 function setID( $id ) {
271 function setTimestamp( $ts ) {
272 # 2003-08-05T18:30:02Z
273 $this->timestamp
= wfTimestamp( TS_MW
, $ts );
276 function setUsername( $user ) {
277 $this->user_text
= $user;
280 function setUserIP( $ip ) {
281 $this->user_text
= $ip;
284 function setText( $text ) {
288 function setComment( $text ) {
289 $this->comment
= $text;
292 function setMinor( $minor ) {
293 $this->minor
= (bool)$minor;
296 function setSrc( $src ) {
300 function setFilename( $filename ) {
301 $this->filename
= $filename;
304 function setSize( $size ) {
305 $this->size
= intval( $size );
308 function getTitle() {
316 function getTimestamp() {
317 return $this->timestamp
;
321 return $this->user_text
;
328 function getComment() {
329 return $this->comment
;
332 function getMinor() {
340 function getFilename() {
341 return $this->filename
;
348 function importOldRevision() {
349 $dbw = wfGetDB( DB_MASTER
);
351 # Sneak a single revision into place
352 $user = User
::newFromName( $this->getUser() );
354 $userId = intval( $user->getId() );
355 $userText = $user->getName();
358 $userText = $this->getUser();
361 // avoid memory leak...?
362 $linkCache = LinkCache
::singleton();
365 $article = new Article( $this->title
);
366 $pageId = $article->getId();
368 # must create the page...
369 $pageId = $article->insertOn( $dbw );
374 $prior = Revision
::loadFromTimestamp( $dbw, $this->title
, $this->timestamp
);
375 if( !is_null( $prior ) ) {
376 // FIXME: this could fail slightly for multiple matches :P
377 wfDebug( __METHOD__
. ": skipping existing revision for [[" .
378 $this->title
->getPrefixedText() . "]], timestamp " .
379 $this->timestamp
. "\n" );
384 # FIXME: Use original rev_id optionally
385 # FIXME: blah blah blah
387 #if( $numrows > 0 ) {
388 # return wfMsg( "importhistoryconflict" );
392 $revision = new Revision( array(
394 'text' => $this->getText(),
395 'comment' => $this->getComment(),
397 'user_text' => $userText,
398 'timestamp' => $this->timestamp
,
399 'minor_edit' => $this->minor
,
401 $revId = $revision->insertOn( $dbw );
402 $changed = $article->updateIfNewerOn( $dbw, $revision );
405 wfDebug( __METHOD__
. ": running onArticleCreate\n" );
406 Article
::onArticleCreate( $this->title
);
408 wfDebug( __METHOD__
. ": running create updates\n" );
409 $article->createUpdates( $revision );
411 } elseif( $changed ) {
412 wfDebug( __METHOD__
. ": running onArticleEdit\n" );
413 Article
::onArticleEdit( $this->title
);
415 wfDebug( __METHOD__
. ": running edit updates\n" );
416 $article->editUpdates(
427 function importUpload() {
428 wfDebug( __METHOD__
. ": STUB\n" );
431 // from file revert...
432 $source = $this->file->getArchiveVirtualUrl( $this->oldimage );
433 $comment = $wgRequest->getText( 'wpComment' );
434 // TODO: Preserve file properties from database instead of reloading from file
435 $status = $this->file->upload( $source, $comment, $comment );
436 if( $status->isGood() ) {
440 // from file upload...
441 $this->mLocalFile = wfLocalFile( $nt );
442 $this->mDestName = $this->mLocalFile->getName();
444 $status = $this->mLocalFile->upload( $this->mTempPath, $this->mComment, $pageText,
445 File::DELETE_SOURCE, $this->mFileProps );
446 if ( !$status->isGood() ) {
447 $resultDetails = array( 'internal' => $status->getWikiText() );
450 // @fixme upload() uses $wgUser, which is wrong here
451 // it may also create a page without our desire, also wrong potentially.
452 // and, it will record a *current* upload, but we might want an archive version here
454 $file = wfLocalFile( $this->getTitle() );
457 wfDebug( "IMPORT: Bad file. :(\n" );
461 $source = $this->downloadSource();
463 wfDebug( "IMPORT: Could not fetch remote file. :(\n" );
467 $status = $file->upload( $source,
469 $this->getComment(), // Initial page, if none present...
472 $this->getTimestamp() );
474 if( $status->isGood() ) {
476 wfDebug( "IMPORT: is ok?\n" );
480 wfDebug( "IMPORT: is bad? " . $status->getXml() . "\n" );
485 function downloadSource() {
486 global $wgEnableUploads;
487 if( !$wgEnableUploads ) {
491 $tempo = tempnam( wfTempDir(), 'download' );
492 $f = fopen( $tempo, 'wb' );
494 wfDebug( "IMPORT: couldn't write to temp file $tempo\n" );
499 $src = $this->getSrc();
500 $data = Http
::get( $src );
502 wfDebug( "IMPORT: couldn't fetch source $src\n" );
517 * implements Special:Import
518 * @ingroup SpecialPage
523 var $mPageCallback = null;
524 var $mPageOutCallback = null;
525 var $mRevisionCallback = null;
526 var $mUploadCallback = null;
527 var $mTargetNamespace = null;
529 var $tagStack = array();
531 function __construct( $source ) {
532 $this->setRevisionCallback( array( $this, "importRevision" ) );
533 $this->setUploadCallback( array( $this, "importUpload" ) );
534 $this->mSource
= $source;
537 function throwXmlError( $err ) {
538 $this->debug( "FAILURE: $err" );
539 wfDebug( "WikiImporter XML error: $err\n" );
544 function doImport() {
545 if( empty( $this->mSource
) ) {
546 return new WikiErrorMsg( "importnotext" );
549 $parser = xml_parser_create( "UTF-8" );
551 # case folding violates XML standard, turn it off
552 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
554 xml_set_object( $parser, $this );
555 xml_set_element_handler( $parser, "in_start", "" );
557 $offset = 0; // for context extraction on error reporting
559 $chunk = $this->mSource
->readChunk();
560 if( !xml_parse( $parser, $chunk, $this->mSource
->atEnd() ) ) {
561 wfDebug( "WikiImporter::doImport encountered XML parsing error\n" );
562 return new WikiXmlError( $parser, wfMsgHtml( 'import-parse-failure' ), $chunk, $offset );
564 $offset +
= strlen( $chunk );
565 } while( $chunk !== false && !$this->mSource
->atEnd() );
566 xml_parser_free( $parser );
571 function debug( $data ) {
572 if( $this->mDebug
) {
573 wfDebug( "IMPORT: $data\n" );
577 function notice( $data ) {
578 global $wgCommandLineMode;
579 if( $wgCommandLineMode ) {
583 $wgOut->addHTML( "<li>" . htmlspecialchars( $data ) . "</li>\n" );
590 function setDebug( $debug ) {
591 $this->mDebug
= $debug;
595 * Sets the action to perform as each new page in the stream is reached.
596 * @param $callback callback
599 function setPageCallback( $callback ) {
600 $previous = $this->mPageCallback
;
601 $this->mPageCallback
= $callback;
606 * Sets the action to perform as each page in the stream is completed.
607 * Callback accepts the page title (as a Title object), a second object
608 * with the original title form (in case it's been overridden into a
609 * local namespace), and a count of revisions.
611 * @param $callback callback
614 function setPageOutCallback( $callback ) {
615 $previous = $this->mPageOutCallback
;
616 $this->mPageOutCallback
= $callback;
621 * Sets the action to perform as each page revision is reached.
622 * @param $callback callback
625 function setRevisionCallback( $callback ) {
626 $previous = $this->mRevisionCallback
;
627 $this->mRevisionCallback
= $callback;
632 * Sets the action to perform as each file upload version is reached.
633 * @param $callback callback
636 function setUploadCallback( $callback ) {
637 $previous = $this->mUploadCallback
;
638 $this->mUploadCallback
= $callback;
643 * Set a target namespace to override the defaults
645 function setTargetNamespace( $namespace ) {
646 if( is_null( $namespace ) ) {
647 // Don't override namespaces
648 $this->mTargetNamespace
= null;
649 } elseif( $namespace >= 0 ) {
650 // FIXME: Check for validity
651 $this->mTargetNamespace
= intval( $namespace );
658 * Default per-revision callback, performs the import.
659 * @param $revision WikiRevision
662 function importRevision( $revision ) {
663 $dbw = wfGetDB( DB_MASTER
);
664 return $dbw->deadlockLoop( array( $revision, 'importOldRevision' ) );
670 function importUpload( $revision ) {
671 //$dbw = wfGetDB( DB_MASTER );
672 //return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
677 * Alternate per-revision callback, for debugging.
678 * @param $revision WikiRevision
681 function debugRevisionHandler( &$revision ) {
682 $this->debug( "Got revision:" );
683 if( is_object( $revision->title
) ) {
684 $this->debug( "-- Title: " . $revision->title
->getPrefixedText() );
686 $this->debug( "-- Title: <invalid>" );
688 $this->debug( "-- User: " . $revision->user_text
);
689 $this->debug( "-- Timestamp: " . $revision->timestamp
);
690 $this->debug( "-- Comment: " . $revision->comment
);
691 $this->debug( "-- Text: " . $revision->text
);
695 * Notify the callback function when a new <page> is reached.
696 * @param $title Title
699 function pageCallback( $title ) {
700 if( is_callable( $this->mPageCallback
) ) {
701 call_user_func( $this->mPageCallback
, $title );
706 * Notify the callback function when a </page> is closed.
707 * @param $title Title
708 * @param $origTitle Title
709 * @param $revisionCount int
710 * @param $successCount Int: number of revisions for which callback returned true
713 function pageOutCallback( $title, $origTitle, $revisionCount, $successCount ) {
714 if( is_callable( $this->mPageOutCallback
) ) {
715 call_user_func( $this->mPageOutCallback
, $title, $origTitle,
716 $revisionCount, $successCount );
721 # XML parser callbacks from here out -- beware!
722 function donothing( $parser, $x, $y="" ) {
723 #$this->debug( "donothing" );
726 function in_start( $parser, $name, $attribs ) {
727 $this->debug( "in_start $name" );
728 if( $name != "mediawiki" ) {
729 return $this->throwXMLerror( "Expected <mediawiki>, got <$name>" );
731 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
734 function in_mediawiki( $parser, $name, $attribs ) {
735 $this->debug( "in_mediawiki $name" );
736 if( $name == 'siteinfo' ) {
737 xml_set_element_handler( $parser, "in_siteinfo", "out_siteinfo" );
738 } elseif( $name == 'page' ) {
739 $this->push( $name );
740 $this->workRevisionCount
= 0;
741 $this->workSuccessCount
= 0;
742 $this->uploadCount
= 0;
743 $this->uploadSuccessCount
= 0;
744 xml_set_element_handler( $parser, "in_page", "out_page" );
746 return $this->throwXMLerror( "Expected <page>, got <$name>" );
749 function out_mediawiki( $parser, $name ) {
750 $this->debug( "out_mediawiki $name" );
751 if( $name != "mediawiki" ) {
752 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
754 xml_set_element_handler( $parser, "donothing", "donothing" );
758 function in_siteinfo( $parser, $name, $attribs ) {
760 $this->debug( "in_siteinfo $name" );
770 return $this->throwXMLerror( "Element <$name> not allowed in <siteinfo>." );
774 function out_siteinfo( $parser, $name ) {
775 if( $name == "siteinfo" ) {
776 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
781 function in_page( $parser, $name, $attribs ) {
782 $this->debug( "in_page $name" );
787 $this->appendfield
= $name;
788 $this->appenddata
= "";
789 xml_set_element_handler( $parser, "in_nothing", "out_append" );
790 xml_set_character_data_handler( $parser, "char_append" );
793 $this->push( "revision" );
794 if( is_object( $this->pageTitle
) ) {
795 $this->workRevision
= new WikiRevision
;
796 $this->workRevision
->setTitle( $this->pageTitle
);
797 $this->workRevisionCount++
;
799 // Skipping items due to invalid page title
800 $this->workRevision
= null;
802 xml_set_element_handler( $parser, "in_revision", "out_revision" );
805 $this->push( "upload" );
806 if( is_object( $this->pageTitle
) ) {
807 $this->workRevision
= new WikiRevision
;
808 $this->workRevision
->setTitle( $this->pageTitle
);
809 $this->uploadCount++
;
811 // Skipping items due to invalid page title
812 $this->workRevision
= null;
814 xml_set_element_handler( $parser, "in_upload", "out_upload" );
817 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
821 function out_page( $parser, $name ) {
822 $this->debug( "out_page $name" );
824 if( $name != "page" ) {
825 return $this->throwXMLerror( "Expected </page>, got </$name>" );
827 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
829 $this->pageOutCallback( $this->pageTitle
, $this->origTitle
,
830 $this->workRevisionCount
, $this->workSuccessCount
);
832 $this->workTitle
= null;
833 $this->workRevision
= null;
834 $this->workRevisionCount
= 0;
835 $this->workSuccessCount
= 0;
836 $this->pageTitle
= null;
837 $this->origTitle
= null;
840 function in_nothing( $parser, $name, $attribs ) {
841 $this->debug( "in_nothing $name" );
842 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
844 function char_append( $parser, $data ) {
845 $this->debug( "char_append '$data'" );
846 $this->appenddata
.= $data;
848 function out_append( $parser, $name ) {
849 $this->debug( "out_append $name" );
850 if( $name != $this->appendfield
) {
851 return $this->throwXMLerror( "Expected </{$this->appendfield}>, got </$name>" );
854 switch( $this->appendfield
) {
856 $this->workTitle
= $this->appenddata
;
857 $this->origTitle
= Title
::newFromText( $this->workTitle
);
858 if( !is_null( $this->mTargetNamespace
) && !is_null( $this->origTitle
) ) {
859 $this->pageTitle
= Title
::makeTitle( $this->mTargetNamespace
,
860 $this->origTitle
->getDBkey() );
862 $this->pageTitle
= Title
::newFromText( $this->workTitle
);
864 if( is_null( $this->pageTitle
) ) {
865 // Invalid page title? Ignore the page
866 $this->notice( "Skipping invalid page title '$this->workTitle'" );
868 $this->pageCallback( $this->workTitle
);
872 if ( $this->parentTag() == 'revision' ) {
873 if( $this->workRevision
)
874 $this->workRevision
->setID( $this->appenddata
);
878 if( $this->workRevision
)
879 $this->workRevision
->setText( $this->appenddata
);
882 if( $this->workRevision
)
883 $this->workRevision
->setUsername( $this->appenddata
);
886 if( $this->workRevision
)
887 $this->workRevision
->setUserIP( $this->appenddata
);
890 if( $this->workRevision
)
891 $this->workRevision
->setTimestamp( $this->appenddata
);
894 if( $this->workRevision
)
895 $this->workRevision
->setComment( $this->appenddata
);
898 if( $this->workRevision
)
899 $this->workRevision
->setMinor( true );
902 if( $this->workRevision
)
903 $this->workRevision
->setFilename( $this->appenddata
);
906 if( $this->workRevision
)
907 $this->workRevision
->setSrc( $this->appenddata
);
910 if( $this->workRevision
)
911 $this->workRevision
->setSize( intval( $this->appenddata
) );
914 $this->debug( "Bad append: {$this->appendfield}" );
916 $this->appendfield
= "";
917 $this->appenddata
= "";
919 $parent = $this->parentTag();
920 xml_set_element_handler( $parser, "in_$parent", "out_$parent" );
921 xml_set_character_data_handler( $parser, "donothing" );
924 function in_revision( $parser, $name, $attribs ) {
925 $this->debug( "in_revision $name" );
932 $this->appendfield
= $name;
933 xml_set_element_handler( $parser, "in_nothing", "out_append" );
934 xml_set_character_data_handler( $parser, "char_append" );
937 $this->push( "contributor" );
938 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
941 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
945 function out_revision( $parser, $name ) {
946 $this->debug( "out_revision $name" );
948 if( $name != "revision" ) {
949 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
951 xml_set_element_handler( $parser, "in_page", "out_page" );
953 if( $this->workRevision
) {
954 $ok = call_user_func_array( $this->mRevisionCallback
,
955 array( $this->workRevision
, $this ) );
957 $this->workSuccessCount++
;
962 function in_upload( $parser, $name, $attribs ) {
963 $this->debug( "in_upload $name" );
971 $this->appendfield
= $name;
972 xml_set_element_handler( $parser, "in_nothing", "out_append" );
973 xml_set_character_data_handler( $parser, "char_append" );
976 $this->push( "contributor" );
977 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
980 return $this->throwXMLerror( "Element <$name> not allowed in an <upload>." );
984 function out_upload( $parser, $name ) {
985 $this->debug( "out_revision $name" );
987 if( $name != "upload" ) {
988 return $this->throwXMLerror( "Expected </upload>, got </$name>" );
990 xml_set_element_handler( $parser, "in_page", "out_page" );
992 if( $this->workRevision
) {
993 $ok = call_user_func_array( $this->mUploadCallback
,
994 array( $this->workRevision
, $this ) );
996 $this->workUploadSuccessCount++
;
1001 function in_contributor( $parser, $name, $attribs ) {
1002 $this->debug( "in_contributor $name" );
1007 $this->appendfield
= $name;
1008 xml_set_element_handler( $parser, "in_nothing", "out_append" );
1009 xml_set_character_data_handler( $parser, "char_append" );
1012 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
1016 function out_contributor( $parser, $name ) {
1017 $this->debug( "out_contributor $name" );
1019 if( $name != "contributor" ) {
1020 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
1022 $parent = $this->parentTag();
1023 xml_set_element_handler( $parser, "in_$parent", "out_$parent" );
1026 private function push( $name ) {
1027 array_push( $this->tagStack
, $name );
1028 $this->debug( "PUSH $name" );
1031 private function pop() {
1032 $name = array_pop( $this->tagStack
);
1033 $this->debug( "POP $name" );
1037 private function parentTag() {
1038 $name = $this->tagStack
[count( $this->tagStack
) - 1];
1039 $this->debug( "PARENT $name" );
1046 * @todo document (e.g. one-sentence class description).
1047 * @ingroup SpecialPage
1049 class ImportStringSource
{
1050 function __construct( $string ) {
1051 $this->mString
= $string;
1052 $this->mRead
= false;
1056 return $this->mRead
;
1059 function readChunk() {
1060 if( $this->atEnd() ) {
1063 $this->mRead
= true;
1064 return $this->mString
;
1070 * @todo document (e.g. one-sentence class description).
1071 * @ingroup SpecialPage
1073 class ImportStreamSource
{
1074 function __construct( $handle ) {
1075 $this->mHandle
= $handle;
1079 return feof( $this->mHandle
);
1082 function readChunk() {
1083 return fread( $this->mHandle
, 32768 );
1086 static function newFromFile( $filename ) {
1087 $file = @fopen
( $filename, 'rt' );
1089 return new WikiErrorMsg( "importcantopen" );
1091 return new ImportStreamSource( $file );
1094 static function newFromUpload( $fieldname = "xmlimport" ) {
1095 $upload =& $_FILES[$fieldname];
1097 if( !isset( $upload ) ||
!$upload['name'] ) {
1098 return new WikiErrorMsg( 'importnofile' );
1100 if( !empty( $upload['error'] ) ) {
1101 switch($upload['error']){
1102 case 1: # The uploaded file exceeds the upload_max_filesize directive in php.ini.
1103 return new WikiErrorMsg( 'importuploaderrorsize' );
1104 case 2: # The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
1105 return new WikiErrorMsg( 'importuploaderrorsize' );
1106 case 3: # The uploaded file was only partially uploaded
1107 return new WikiErrorMsg( 'importuploaderrorpartial' );
1108 case 6: #Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.
1109 return new WikiErrorMsg( 'importuploaderrortemp' );
1110 # case else: # Currently impossible
1114 $fname = $upload['tmp_name'];
1115 if( is_uploaded_file( $fname ) ) {
1116 return ImportStreamSource
::newFromFile( $fname );
1118 return new WikiErrorMsg( 'importnofile' );
1122 static function newFromURL( $url, $method = 'GET' ) {
1123 wfDebug( __METHOD__
. ": opening $url\n" );
1124 # Use the standard HTTP fetch function; it times out
1125 # quicker and sorts out user-agent problems which might
1126 # otherwise prevent importing from large sites, such
1127 # as the Wikimedia cluster, etc.
1128 $data = Http
::request( $method, $url );
1129 if( $data !== false ) {
1131 fwrite( $file, $data );
1134 return new ImportStreamSource( $file );
1136 return new WikiErrorMsg( 'importcantopen' );
1140 public static function newFromInterwiki( $interwiki, $page, $history=false ) {
1142 return new WikiErrorMsg( 'import-noarticle' );
1144 $link = Title
::newFromText( "$interwiki:Special:Export/$page" );
1145 if( is_null( $link ) ||
$link->getInterwiki() == '' ) {
1146 return new WikiErrorMsg( 'importbadinterwiki' );
1148 $params = $history ?
'history=1' : '';
1149 $url = $link->getFullUrl( $params );
1150 # For interwikis, use POST to avoid redirects.
1151 return ImportStreamSource
::newFromURL( $url, "POST" );