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
22 * @addtogroup SpecialPage
28 function wfSpecialImport( $page = '' ) {
29 global $wgUser, $wgOut, $wgRequest, $wgTitle, $wgImportSources;
30 global $wgImportTargetNamespace;
33 $namespace = $wgImportTargetNamespace;
38 $wgOut->readOnlyPage();
42 if( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit') {
44 $namespace = $wgRequest->getIntOrNull( 'namespace' );
46 switch( $wgRequest->getVal( "source" ) ) {
49 if( $wgUser->isAllowed( 'importupload' ) ) {
50 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
52 return $wgOut->permissionRequired( 'importupload' );
56 $interwiki = $wgRequest->getVal( 'interwiki' );
57 $history = $wgRequest->getCheck( 'interwikiHistory' );
58 $frompage = $wgRequest->getText( "frompage" );
59 $source = ImportStreamSource
::newFromInterwiki(
65 $source = new WikiErrorMsg( "importunknownsource" );
68 if( WikiError
::isError( $source ) ) {
69 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $source->getMessage() ) );
71 $wgOut->addWikiMsg( "importstart" );
73 $importer = new WikiImporter( $source );
74 if( !is_null( $namespace ) ) {
75 $importer->setTargetNamespace( $namespace );
77 $reporter = new ImportReporter( $importer, $isUpload, $interwiki );
80 $result = $importer->doImport();
81 $resultCount = $reporter->close();
83 if( WikiError
::isError( $result ) ) {
84 # No source or XML parse error
85 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $result->getMessage() ) );
86 } elseif( WikiError
::isError( $resultCount ) ) {
88 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $resultCount->getMessage() ) );
91 $wgOut->addWikiMsg( 'importsuccess' );
93 $wgOut->addWikiText( '<hr />' );
97 $action = $wgTitle->getLocalUrl( 'action=submit' );
99 if( $wgUser->isAllowed( 'importupload' ) ) {
100 $wgOut->addWikiMsg( "importtext" );
102 Xml
::openElement( 'fieldset' ).
103 Xml
::element( 'legend', null, wfMsg( 'upload' ) ) .
104 Xml
::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post', 'action' => $action ) ) .
105 Xml
::hidden( 'action', 'submit' ) .
106 Xml
::hidden( 'source', 'upload' ) .
107 Xml
::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
108 Xml
::submitButton( wfMsg( 'uploadbtn' ) ) .
109 Xml
::closeElement( 'form' ) .
110 Xml
::closeElement( 'fieldset' )
113 if( empty( $wgImportSources ) ) {
114 $wgOut->addWikiMsg( 'importnosources' );
118 if( !empty( $wgImportSources ) ) {
120 Xml
::openElement( 'fieldset' ) .
121 Xml
::element( 'legend', null, wfMsg( 'importinterwiki' ) ) .
122 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action ) ) .
123 wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
124 Xml
::hidden( 'action', 'submit' ) .
125 Xml
::hidden( 'source', 'interwiki' ) .
126 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
129 Xml
::openElement( 'select', array( 'name' => 'interwiki' ) )
131 foreach( $wgImportSources as $prefix ) {
132 $selected = ( $interwiki === $prefix ) ?
' selected="selected"' : '';
133 $wgOut->addHTML( Xml
::option( $prefix, $prefix, $selected ) );
136 Xml
::closeElement( 'select' ) .
139 Xml
::input( 'frompage', 50, $frompage ) .
146 Xml
::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $history ) .
153 Xml
::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
154 Xml
::namespaceSelector( $namespace, '' ) .
161 Xml
::submitButton( wfMsg( 'import-interwiki-submit' ) ) .
164 Xml
::closeElement( 'table' ).
165 Xml
::closeElement( 'form' ) .
166 Xml
::closeElement( 'fieldset' )
173 * @addtogroup SpecialPage
175 class ImportReporter
{
176 function __construct( $importer, $upload, $interwiki ) {
177 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
178 $this->mPageCount
= 0;
179 $this->mIsUpload
= $upload;
180 $this->mInterwiki
= $interwiki;
185 $wgOut->addHtml( "<ul>\n" );
188 function reportPage( $title, $origTitle, $revisionCount, $successCount ) {
189 global $wgOut, $wgUser, $wgLang, $wgContLang;
191 $skin = $wgUser->getSkin();
195 $localCount = $wgLang->formatNum( $successCount );
196 $contentCount = $wgContLang->formatNum( $successCount );
198 if( $successCount > 0 ) {
199 $wgOut->addHtml( "<li>" . $skin->makeKnownLinkObj( $title ) . " " .
200 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
204 $log = new LogPage( 'import' );
205 if( $this->mIsUpload
) {
206 $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
208 $log->addEntry( 'upload', $title, $detail );
210 $interwiki = '[[:' . $this->mInterwiki
. ':' .
211 $origTitle->getPrefixedText() . ']]';
212 $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
213 $contentCount, $interwiki );
214 $log->addEntry( 'interwiki', $title, $detail );
217 $comment = $detail; // quick
218 $dbw = wfGetDB( DB_MASTER
);
219 $nullRevision = Revision
::newNullRevision(
220 $dbw, $title->getArticleId(), $comment, true );
221 $nullRevision->insertOn( $dbw );
223 $article = new Article( $title );
224 $article->updateRevisionOn( $dbw, $nullRevision );
226 $wgOut->addHtml( '<li>' . wfMsgHtml( 'import-nonewrevisions' ) . '</li>' );
232 if( $this->mPageCount
== 0 ) {
233 $wgOut->addHtml( "</ul>\n" );
234 return new WikiErrorMsg( "importnopages" );
236 $wgOut->addHtml( "</ul>\n" );
238 return $this->mPageCount
;
244 * @addtogroup SpecialPage
249 var $timestamp = "20010115000000";
256 function setTitle( $title ) {
257 if( is_object( $title ) ) {
258 $this->title
= $title;
259 } elseif( is_null( $title ) ) {
260 throw new MWException( "WikiRevision given a null title in import. You may need to adjust \$wgLegalTitleChars." );
262 throw new MWException( "WikiRevision given non-object title in import." );
266 function setID( $id ) {
270 function setTimestamp( $ts ) {
271 # 2003-08-05T18:30:02Z
272 $this->timestamp
= wfTimestamp( TS_MW
, $ts );
275 function setUsername( $user ) {
276 $this->user_text
= $user;
279 function setUserIP( $ip ) {
280 $this->user_text
= $ip;
283 function setText( $text ) {
287 function setComment( $text ) {
288 $this->comment
= $text;
291 function setMinor( $minor ) {
292 $this->minor
= (bool)$minor;
295 function setSrc( $src ) {
299 function setFilename( $filename ) {
300 $this->filename
= $filename;
303 function setSize( $size ) {
304 $this->size
= intval( $size );
307 function getTitle() {
315 function getTimestamp() {
316 return $this->timestamp
;
320 return $this->user_text
;
327 function getComment() {
328 return $this->comment
;
331 function getMinor() {
339 function getFilename() {
340 return $this->filename
;
347 function importOldRevision() {
348 $dbw = wfGetDB( DB_MASTER
);
350 # Sneak a single revision into place
351 $user = User
::newFromName( $this->getUser() );
353 $userId = intval( $user->getId() );
354 $userText = $user->getName();
357 $userText = $this->getUser();
360 // avoid memory leak...?
361 $linkCache =& LinkCache
::singleton();
364 $article = new Article( $this->title
);
365 $pageId = $article->getId();
367 # must create the page...
368 $pageId = $article->insertOn( $dbw );
373 $prior = Revision
::loadFromTimestamp( $dbw, $this->title
, $this->timestamp
);
374 if( !is_null( $prior ) ) {
375 // FIXME: this could fail slightly for multiple matches :P
376 wfDebug( __METHOD__
. ": skipping existing revision for [[" .
377 $this->title
->getPrefixedText() . "]], timestamp " .
378 $this->timestamp
. "\n" );
383 # FIXME: Use original rev_id optionally
384 # FIXME: blah blah blah
386 #if( $numrows > 0 ) {
387 # return wfMsg( "importhistoryconflict" );
391 $revision = new Revision( array(
393 'text' => $this->getText(),
394 'comment' => $this->getComment(),
396 'user_text' => $userText,
397 'timestamp' => $this->timestamp
,
398 'minor_edit' => $this->minor
,
400 $revId = $revision->insertOn( $dbw );
401 $changed = $article->updateIfNewerOn( $dbw, $revision );
404 wfDebug( __METHOD__
. ": running onArticleCreate\n" );
405 Article
::onArticleCreate( $this->title
);
407 wfDebug( __METHOD__
. ": running create updates\n" );
408 $article->createUpdates( $revision );
410 } elseif( $changed ) {
411 wfDebug( __METHOD__
. ": running onArticleEdit\n" );
412 Article
::onArticleEdit( $this->title
);
414 wfDebug( __METHOD__
. ": running edit updates\n" );
415 $article->editUpdates(
426 function importUpload() {
427 wfDebug( __METHOD__
. ": STUB\n" );
430 // from file revert...
431 $source = $this->file->getArchiveVirtualUrl( $this->oldimage );
432 $comment = $wgRequest->getText( 'wpComment' );
433 // TODO: Preserve file properties from database instead of reloading from file
434 $status = $this->file->upload( $source, $comment, $comment );
435 if( $status->isGood() ) {
439 // from file upload...
440 $this->mLocalFile = wfLocalFile( $nt );
441 $this->mDestName = $this->mLocalFile->getName();
443 $status = $this->mLocalFile->upload( $this->mTempPath, $this->mComment, $pageText,
444 File::DELETE_SOURCE, $this->mFileProps );
445 if ( !$status->isGood() ) {
446 $resultDetails = array( 'internal' => $status->getWikiText() );
449 // @fixme upload() uses $wgUser, which is wrong here
450 // it may also create a page without our desire, also wrong potentially.
451 // and, it will record a *current* upload, but we might want an archive version here
453 $file = wfLocalFile( $this->getTitle() );
456 wfDebug( "IMPORT: Bad file. :(\n" );
460 $source = $this->downloadSource();
462 wfDebug( "IMPORT: Could not fetch remote file. :(\n" );
466 $status = $file->upload( $source,
468 $this->getComment(), // Initial page, if none present...
471 $this->getTimestamp() );
473 if( $status->isGood() ) {
475 wfDebug( "IMPORT: is ok?\n" );
479 wfDebug( "IMPORT: is bad? " . $status->getXml() . "\n" );
484 function downloadSource() {
485 global $wgEnableUploads;
486 if( !$wgEnableUploads ) {
490 $tempo = tempnam( wfTempDir(), 'download' );
491 $f = fopen( $tempo, 'wb' );
493 wfDebug( "IMPORT: couldn't write to temp file $tempo\n" );
498 $src = $this->getSrc();
499 $data = Http
::get( $src );
501 wfDebug( "IMPORT: couldn't fetch source $src\n" );
516 * implements Special:Import
517 * @addtogroup SpecialPage
522 var $mPageCallback = null;
523 var $mPageOutCallback = null;
524 var $mRevisionCallback = null;
525 var $mUploadCallback = null;
526 var $mTargetNamespace = null;
528 var $tagStack = array();
530 function __construct( $source ) {
531 $this->setRevisionCallback( array( $this, "importRevision" ) );
532 $this->setUploadCallback( array( $this, "importUpload" ) );
533 $this->mSource
= $source;
536 function throwXmlError( $err ) {
537 $this->debug( "FAILURE: $err" );
538 wfDebug( "WikiImporter XML error: $err\n" );
543 function doImport() {
544 if( empty( $this->mSource
) ) {
545 return new WikiErrorMsg( "importnotext" );
548 $parser = xml_parser_create( "UTF-8" );
550 # case folding violates XML standard, turn it off
551 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
553 xml_set_object( $parser, $this );
554 xml_set_element_handler( $parser, "in_start", "" );
556 $offset = 0; // for context extraction on error reporting
558 $chunk = $this->mSource
->readChunk();
559 if( !xml_parse( $parser, $chunk, $this->mSource
->atEnd() ) ) {
560 wfDebug( "WikiImporter::doImport encountered XML parsing error\n" );
561 return new WikiXmlError( $parser, wfMsgHtml( 'import-parse-failure' ), $chunk, $offset );
563 $offset +
= strlen( $chunk );
564 } while( $chunk !== false && !$this->mSource
->atEnd() );
565 xml_parser_free( $parser );
570 function debug( $data ) {
571 if( $this->mDebug
) {
572 wfDebug( "IMPORT: $data\n" );
576 function notice( $data ) {
577 global $wgCommandLineMode;
578 if( $wgCommandLineMode ) {
582 $wgOut->addHTML( "<li>" . htmlspecialchars( $data ) . "</li>\n" );
589 function setDebug( $debug ) {
590 $this->mDebug
= $debug;
594 * Sets the action to perform as each new page in the stream is reached.
595 * @param callable $callback
598 function setPageCallback( $callback ) {
599 $previous = $this->mPageCallback
;
600 $this->mPageCallback
= $callback;
605 * Sets the action to perform as each page in the stream is completed.
606 * Callback accepts the page title (as a Title object), a second object
607 * with the original title form (in case it's been overridden into a
608 * local namespace), and a count of revisions.
610 * @param callable $callback
613 function setPageOutCallback( $callback ) {
614 $previous = $this->mPageOutCallback
;
615 $this->mPageOutCallback
= $callback;
620 * Sets the action to perform as each page revision is reached.
621 * @param callable $callback
624 function setRevisionCallback( $callback ) {
625 $previous = $this->mRevisionCallback
;
626 $this->mRevisionCallback
= $callback;
631 * Sets the action to perform as each file upload version is reached.
632 * @param callable callback
635 function setUploadCallback( $callback ) {
636 $previous = $this->mUploadCallback
;
637 $this->mUploadCallback
= $callback;
642 * Set a target namespace to override the defaults
644 function setTargetNamespace( $namespace ) {
645 if( is_null( $namespace ) ) {
646 // Don't override namespaces
647 $this->mTargetNamespace
= null;
648 } elseif( $namespace >= 0 ) {
649 // FIXME: Check for validity
650 $this->mTargetNamespace
= intval( $namespace );
657 * Default per-revision callback, performs the import.
658 * @param WikiRevision $revision
661 function importRevision( $revision ) {
662 $dbw = wfGetDB( DB_MASTER
);
663 return $dbw->deadlockLoop( array( $revision, 'importOldRevision' ) );
669 function importUpload( $revision ) {
670 //$dbw = wfGetDB( DB_MASTER );
671 //return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
676 * Alternate per-revision callback, for debugging.
677 * @param WikiRevision $revision
680 function debugRevisionHandler( &$revision ) {
681 $this->debug( "Got revision:" );
682 if( is_object( $revision->title
) ) {
683 $this->debug( "-- Title: " . $revision->title
->getPrefixedText() );
685 $this->debug( "-- Title: <invalid>" );
687 $this->debug( "-- User: " . $revision->user_text
);
688 $this->debug( "-- Timestamp: " . $revision->timestamp
);
689 $this->debug( "-- Comment: " . $revision->comment
);
690 $this->debug( "-- Text: " . $revision->text
);
694 * Notify the callback function when a new <page> is reached.
695 * @param Title $title
698 function pageCallback( $title ) {
699 if( is_callable( $this->mPageCallback
) ) {
700 call_user_func( $this->mPageCallback
, $title );
705 * Notify the callback function when a </page> is closed.
706 * @param Title $title
707 * @param Title $origTitle
708 * @param int $revisionCount
709 * @param int $successCount number of revisions for which callback returned true
712 function pageOutCallback( $title, $origTitle, $revisionCount, $successCount ) {
713 if( is_callable( $this->mPageOutCallback
) ) {
714 call_user_func( $this->mPageOutCallback
, $title, $origTitle,
715 $revisionCount, $successCount );
720 # XML parser callbacks from here out -- beware!
721 function donothing( $parser, $x, $y="" ) {
722 #$this->debug( "donothing" );
725 function in_start( $parser, $name, $attribs ) {
726 $this->debug( "in_start $name" );
727 if( $name != "mediawiki" ) {
728 return $this->throwXMLerror( "Expected <mediawiki>, got <$name>" );
730 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
733 function in_mediawiki( $parser, $name, $attribs ) {
734 $this->debug( "in_mediawiki $name" );
735 if( $name == 'siteinfo' ) {
736 xml_set_element_handler( $parser, "in_siteinfo", "out_siteinfo" );
737 } elseif( $name == 'page' ) {
738 $this->push( $name );
739 $this->workRevisionCount
= 0;
740 $this->workSuccessCount
= 0;
741 $this->uploadCount
= 0;
742 $this->uploadSuccessCount
= 0;
743 xml_set_element_handler( $parser, "in_page", "out_page" );
745 return $this->throwXMLerror( "Expected <page>, got <$name>" );
748 function out_mediawiki( $parser, $name ) {
749 $this->debug( "out_mediawiki $name" );
750 if( $name != "mediawiki" ) {
751 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
753 xml_set_element_handler( $parser, "donothing", "donothing" );
757 function in_siteinfo( $parser, $name, $attribs ) {
759 $this->debug( "in_siteinfo $name" );
769 return $this->throwXMLerror( "Element <$name> not allowed in <siteinfo>." );
773 function out_siteinfo( $parser, $name ) {
774 if( $name == "siteinfo" ) {
775 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
780 function in_page( $parser, $name, $attribs ) {
781 $this->debug( "in_page $name" );
786 $this->appendfield
= $name;
787 $this->appenddata
= "";
788 xml_set_element_handler( $parser, "in_nothing", "out_append" );
789 xml_set_character_data_handler( $parser, "char_append" );
792 $this->push( "revision" );
793 if( is_object( $this->pageTitle
) ) {
794 $this->workRevision
= new WikiRevision
;
795 $this->workRevision
->setTitle( $this->pageTitle
);
796 $this->workRevisionCount++
;
798 // Skipping items due to invalid page title
799 $this->workRevision
= null;
801 xml_set_element_handler( $parser, "in_revision", "out_revision" );
804 $this->push( "upload" );
805 if( is_object( $this->pageTitle
) ) {
806 $this->workRevision
= new WikiRevision
;
807 $this->workRevision
->setTitle( $this->pageTitle
);
808 $this->uploadCount++
;
810 // Skipping items due to invalid page title
811 $this->workRevision
= null;
813 xml_set_element_handler( $parser, "in_upload", "out_upload" );
816 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
820 function out_page( $parser, $name ) {
821 $this->debug( "out_page $name" );
823 if( $name != "page" ) {
824 return $this->throwXMLerror( "Expected </page>, got </$name>" );
826 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
828 $this->pageOutCallback( $this->pageTitle
, $this->origTitle
,
829 $this->workRevisionCount
, $this->workSuccessCount
);
831 $this->workTitle
= null;
832 $this->workRevision
= null;
833 $this->workRevisionCount
= 0;
834 $this->workSuccessCount
= 0;
835 $this->pageTitle
= null;
836 $this->origTitle
= null;
839 function in_nothing( $parser, $name, $attribs ) {
840 $this->debug( "in_nothing $name" );
841 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
843 function char_append( $parser, $data ) {
844 $this->debug( "char_append '$data'" );
845 $this->appenddata
.= $data;
847 function out_append( $parser, $name ) {
848 $this->debug( "out_append $name" );
849 if( $name != $this->appendfield
) {
850 return $this->throwXMLerror( "Expected </{$this->appendfield}>, got </$name>" );
853 switch( $this->appendfield
) {
855 $this->workTitle
= $this->appenddata
;
856 $this->origTitle
= Title
::newFromText( $this->workTitle
);
857 if( !is_null( $this->mTargetNamespace
) && !is_null( $this->origTitle
) ) {
858 $this->pageTitle
= Title
::makeTitle( $this->mTargetNamespace
,
859 $this->origTitle
->getDBkey() );
861 $this->pageTitle
= Title
::newFromText( $this->workTitle
);
863 if( is_null( $this->pageTitle
) ) {
864 // Invalid page title? Ignore the page
865 $this->notice( "Skipping invalid page title '$this->workTitle'" );
867 $this->pageCallback( $this->workTitle
);
871 if ( $this->parentTag() == 'revision' ) {
872 if( $this->workRevision
)
873 $this->workRevision
->setID( $this->appenddata
);
877 if( $this->workRevision
)
878 $this->workRevision
->setText( $this->appenddata
);
881 if( $this->workRevision
)
882 $this->workRevision
->setUsername( $this->appenddata
);
885 if( $this->workRevision
)
886 $this->workRevision
->setUserIP( $this->appenddata
);
889 if( $this->workRevision
)
890 $this->workRevision
->setTimestamp( $this->appenddata
);
893 if( $this->workRevision
)
894 $this->workRevision
->setComment( $this->appenddata
);
897 if( $this->workRevision
)
898 $this->workRevision
->setMinor( true );
901 if( $this->workRevision
)
902 $this->workRevision
->setFilename( $this->appenddata
);
905 if( $this->workRevision
)
906 $this->workRevision
->setSrc( $this->appenddata
);
909 if( $this->workRevision
)
910 $this->workRevision
->setSize( intval( $this->appenddata
) );
913 $this->debug( "Bad append: {$this->appendfield}" );
915 $this->appendfield
= "";
916 $this->appenddata
= "";
918 $parent = $this->parentTag();
919 xml_set_element_handler( $parser, "in_$parent", "out_$parent" );
920 xml_set_character_data_handler( $parser, "donothing" );
923 function in_revision( $parser, $name, $attribs ) {
924 $this->debug( "in_revision $name" );
931 $this->appendfield
= $name;
932 xml_set_element_handler( $parser, "in_nothing", "out_append" );
933 xml_set_character_data_handler( $parser, "char_append" );
936 $this->push( "contributor" );
937 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
940 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
944 function out_revision( $parser, $name ) {
945 $this->debug( "out_revision $name" );
947 if( $name != "revision" ) {
948 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
950 xml_set_element_handler( $parser, "in_page", "out_page" );
952 if( $this->workRevision
) {
953 $ok = call_user_func_array( $this->mRevisionCallback
,
954 array( $this->workRevision
, $this ) );
956 $this->workSuccessCount++
;
961 function in_upload( $parser, $name, $attribs ) {
962 $this->debug( "in_upload $name" );
970 $this->appendfield
= $name;
971 xml_set_element_handler( $parser, "in_nothing", "out_append" );
972 xml_set_character_data_handler( $parser, "char_append" );
975 $this->push( "contributor" );
976 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
979 return $this->throwXMLerror( "Element <$name> not allowed in an <upload>." );
983 function out_upload( $parser, $name ) {
984 $this->debug( "out_revision $name" );
986 if( $name != "upload" ) {
987 return $this->throwXMLerror( "Expected </upload>, got </$name>" );
989 xml_set_element_handler( $parser, "in_page", "out_page" );
991 if( $this->workRevision
) {
992 $ok = call_user_func_array( $this->mUploadCallback
,
993 array( $this->workRevision
, $this ) );
995 $this->workUploadSuccessCount++
;
1000 function in_contributor( $parser, $name, $attribs ) {
1001 $this->debug( "in_contributor $name" );
1006 $this->appendfield
= $name;
1007 xml_set_element_handler( $parser, "in_nothing", "out_append" );
1008 xml_set_character_data_handler( $parser, "char_append" );
1011 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
1015 function out_contributor( $parser, $name ) {
1016 $this->debug( "out_contributor $name" );
1018 if( $name != "contributor" ) {
1019 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
1021 $parent = $this->parentTag();
1022 xml_set_element_handler( $parser, "in_$parent", "out_$parent" );
1025 private function push( $name ) {
1026 array_push( $this->tagStack
, $name );
1027 $this->debug( "PUSH $name" );
1030 private function pop() {
1031 $name = array_pop( $this->tagStack
);
1032 $this->debug( "POP $name" );
1036 private function parentTag() {
1037 $name = $this->tagStack
[count( $this->tagStack
) - 1];
1038 $this->debug( "PARENT $name" );
1045 * @todo document (e.g. one-sentence class description).
1046 * @addtogroup SpecialPage
1048 class ImportStringSource
{
1049 function __construct( $string ) {
1050 $this->mString
= $string;
1051 $this->mRead
= false;
1055 return $this->mRead
;
1058 function readChunk() {
1059 if( $this->atEnd() ) {
1062 $this->mRead
= true;
1063 return $this->mString
;
1069 * @todo document (e.g. one-sentence class description).
1070 * @addtogroup SpecialPage
1072 class ImportStreamSource
{
1073 function __construct( $handle ) {
1074 $this->mHandle
= $handle;
1078 return feof( $this->mHandle
);
1081 function readChunk() {
1082 return fread( $this->mHandle
, 32768 );
1085 static function newFromFile( $filename ) {
1086 $file = @fopen
( $filename, 'rt' );
1088 return new WikiErrorMsg( "importcantopen" );
1090 return new ImportStreamSource( $file );
1093 static function newFromUpload( $fieldname = "xmlimport" ) {
1094 $upload =& $_FILES[$fieldname];
1096 if( !isset( $upload ) ||
!$upload['name'] ) {
1097 return new WikiErrorMsg( 'importnofile' );
1099 if( !empty( $upload['error'] ) ) {
1100 switch($upload['error']){
1101 case 1: # The uploaded file exceeds the upload_max_filesize directive in php.ini.
1102 return new WikiErrorMsg( 'importuploaderrorsize' );
1103 case 2: # The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
1104 return new WikiErrorMsg( 'importuploaderrorsize' );
1105 case 3: # The uploaded file was only partially uploaded
1106 return new WikiErrorMsg( 'importuploaderrorpartial' );
1107 case 6: #Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.
1108 return new WikiErrorMsg( 'importuploaderrortemp' );
1109 # case else: # Currently impossible
1113 $fname = $upload['tmp_name'];
1114 if( is_uploaded_file( $fname ) ) {
1115 return ImportStreamSource
::newFromFile( $fname );
1117 return new WikiErrorMsg( 'importnofile' );
1121 function newFromURL( $url, $method = 'GET' ) {
1122 wfDebug( __METHOD__
. ": opening $url\n" );
1123 # Use the standard HTTP fetch function; it times out
1124 # quicker and sorts out user-agent problems which might
1125 # otherwise prevent importing from large sites, such
1126 # as the Wikimedia cluster, etc.
1127 $data = Http
::request( $method, $url );
1128 if( $data !== false ) {
1130 fwrite( $file, $data );
1133 return new ImportStreamSource( $file );
1135 return new WikiErrorMsg( 'importcantopen' );
1139 public static function newFromInterwiki( $interwiki, $page, $history=false ) {
1141 return new WikiErrorMsg( 'import-noarticle' );
1143 $link = Title
::newFromText( "$interwiki:Special:Export/$page" );
1144 if( is_null( $link ) ||
$link->getInterwiki() == '' ) {
1145 return new WikiErrorMsg( 'importbadinterwiki' );
1147 $params = $history ?
'history=1' : '';
1148 $url = $link->getFullUrl( $params );
1149 # For interwikis, use POST to avoid redirects.
1150 return ImportStreamSource
::newFromURL( $url, "POST" );