* Rollback should not work if the wiki is read-only
[lhc/web/wiklou.git] / includes / SpecialImport.php
index 36e0273..d22e2a5 100644 (file)
@@ -30,14 +30,17 @@ function wfSpecialImport( $page = '' ) {
        global $wgImportTargetNamespace;
 
        $interwiki = false;
-       $articleName = '';
        $namespace = $wgImportTargetNamespace;
        $frompage = '';
        $history = true;
 
+       if ( wfReadOnly() ) {
+               $wgOut->readOnlyPage();
+               return;
+       }
+
        if( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit') {
                $isUpload = false;
-               $articleName = htmlspecialchars( $wgRequest->getText( 'articlename' ) );
                $namespace = $wgRequest->getIntOrNull( 'namespace' );
 
                switch( $wgRequest->getVal( "source" ) ) {
@@ -63,7 +66,7 @@ function wfSpecialImport( $page = '' ) {
                }
 
                if( WikiError::isError( $source ) ) {
-                       $wgOut->addWikiText( '<p class="error">' . wfMsg( "importfailed", wfEscapeWikiText( $source->getMessage() ) ) . '</p>' );
+                       $wgOut->addWikiText( wfEscapeWikiText( $source->getMessage() ) );
                } else {
                        $wgOut->addWikiText( wfMsg( "importstart" ) );
 
@@ -71,44 +74,37 @@ function wfSpecialImport( $page = '' ) {
                        if( !is_null( $namespace ) ) {
                                $importer->setTargetNamespace( $namespace );
                        }
-                       if( !is_null( $articleName ) ) {
-                               $importer->setTargetArticleName( $articleName );
-                       }
-                       $reporter = new ImportReporter( $importer, $isUpload, $interwiki, $frompage );
+                       $reporter = new ImportReporter( $importer, $isUpload, $interwiki );
 
                        $reporter->open();
                        $result = $importer->doImport();
-                       $resultCount = $reporter->close();
+                       $reporter->close();
 
                        if( WikiError::isError( $result ) ) {
-                               # No source or XML parse error
-                               $wgOut->addWikiText( '<p class="error">' . wfMsg( "importfailed", wfEscapeWikiText( $result->getMessage() ) ) . '</p>' );
-                       } elseif( WikiError::isError( $resultCount ) ) {
-                               # Zero revisions
-                               $wgOut->addWikiText( '<p class="error">' . wfMsg( "importfailed", wfEscapeWikiText( $resultCount->getMessage() ) ) . '</p>' );
+                               $wgOut->addWikiText( wfMsg( "importfailed",
+                                       wfEscapeWikiText( $result->getMessage() ) ) );
                        } else {
                                # Success!
                                $wgOut->addWikiText( wfMsg( "importsuccess" ) );
                        }
-                       $wgOut->addWikiText( '<hr />' );
                }
        }
 
-       $action = $wgTitle->escapeLocalUrl( 'action=submit' );
+       $action = $wgTitle->getLocalUrl( 'action=submit' );
 
        if( $wgUser->isAllowed( 'importupload' ) ) {
                $wgOut->addWikiText( wfMsg( "importtext" ) );
-               $wgOut->addHTML( 
-                       Xml::openElement( 'fieldset' ).
-                       Xml::element( 'legend', null, wfMsg( 'upload' ) ) .
-                       Xml::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post', 'action' => $action ) ) .
-                       Xml::hidden( 'action', 'submit' ) .
-                       Xml::hidden( 'source', 'upload' ) .
-                       Xml::hidden( 'MAX_FILE_SIZE', '2000000' ) .
-                       "<input type='file' name='xmlimport' value='' size='30' />" . // No Xml function for type=file? Todo?
-                       Xml::submitButton( wfMsg( 'uploadbtn' ) ) . 
-                       Xml::closeElement( 'form' ) .
-                       Xml::closeElement( 'fieldset' ) );
+               $wgOut->addHTML( "
+<fieldset>
+       <legend>" . wfMsgHtml('upload') . "</legend>
+       <form enctype='multipart/form-data' method='post' action=\"$action\">
+               <input type='hidden' name='action' value='submit' />
+               <input type='hidden' name='source' value='upload' />
+               <input type='file' name='xmlimport' value='' size='30' />
+               <input type='submit' value=\"" . wfMsgHtml( "uploadbtn" ) . "\" />
+       </form>
+</fieldset>
+" );
        } else {
                if( empty( $wgImportSources ) ) {
                        $wgOut->addWikiText( wfMsg( 'importnosources' ) );
@@ -116,57 +112,53 @@ function wfSpecialImport( $page = '' ) {
        }
 
        if( !empty( $wgImportSources ) ) {
-               $wgOut->addHTML(
-                       Xml::openElement( 'fieldset' ) .
-                       Xml::element( 'legend', null, wfMsg( 'importinterwiki' ) ) .
-                       Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action ) ) .
-                       wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
-                       Xml::hidden( 'action', 'submit' ) .
-                       Xml::hidden( 'source', 'interwiki' ) .
-                       Xml::openElement( 'table' ) .
-                       "<tr>
-                               <td>" .
-                                       Xml::openElement( 'select', array( 'name' => 'interwiki' ) ) );
+               $wgOut->addHTML( "
+<fieldset>
+       <legend>" . wfMsgHtml('importinterwiki') . "</legend>
+       <form method='post' action=\"$action\">" .
+               $wgOut->parse( wfMsg( 'import-interwiki-text' ) ) . "
+               <input type='hidden' name='action' value='submit' />
+               <input type='hidden' name='source' value='interwiki' />
+               <table>
+                       <tr>
+                               <td>
+                                       <select name='interwiki'>" );
                foreach( $wgImportSources as $prefix ) {
                        $iw = htmlspecialchars( $prefix );
                        $selected = ($interwiki === $prefix) ? ' selected="selected"' : '';
-                       $wgOut->addHTML( Xml::option( $iw, $iw, $selected ) );
+                       $wgOut->addHTML( "<option value=\"$iw\"$selected>$iw</option>\n" );
                }
-               $wgOut->addHTML( 
-                                       Xml::closeElement( 'select' ) .
-                               "</td>
+               $wgOut->addHTML( "
+                                       </select>
+                               </td>
                                <td>" .
-                                       Xml::input( 'frompage', 50, $frompage ) .
+                                       wfInput( 'frompage', 50, $frompage ) .
                                "</td>
                        </tr>
                        <tr>
                                <td></td>
                                <td>" .
-                                       Xml::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $history ) .
+                                       wfCheckLabel( wfMsg( 'import-interwiki-history' ),
+                                               'interwikiHistory', 'interwikiHistory', $history ) .
                                "</td>
                        </tr>
                        <tr>
                                <td></td>
-                               <td>" .
-                                       Xml::inputLabel( wfMsg( 'import-articlename' ), 'articlename', 'articlename', 50, $articleName ) .
-                               "</td>
+                               <td>
+                                       " . wfMsgHtml( 'import-interwiki-namespace' ) . " " .
+                                               HTMLnamespaceselector( $namespace, '' ) . "
+                               </td>
                        </tr>
                        <tr>
                                <td></td>
                                <td>" .
-                                       Xml::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
-                                       Xml::namespaceSelector( $namespace, '' ) .
+                                       wfSubmitButton( wfMsg( 'import-interwiki-submit' ) ) .
                                "</td>
                        </tr>
-                       <tr>
-                               <td></td>
-                               <td>" .
-                                       Xml::submitButton( wfMsg( 'import-interwiki-submit' ) ) . 
-                               "</td>
-                       </tr>" .
-                       Xml::closeElement( 'table' ).
-                       Xml::closeElement( 'form' ) .
-                       Xml::closeElement( 'fieldset' ) );
+               </table>
+       </form>
+</fieldset>
+" );
        }
 }
 
@@ -175,12 +167,11 @@ function wfSpecialImport( $page = '' ) {
  * @addtogroup SpecialPage
  */
 class ImportReporter {
-       function __construct( $importer, $upload, $interwiki, $frompage ) {
+       function __construct( $importer, $upload, $interwiki ) {
                $importer->setPageOutCallback( array( $this, 'reportPage' ) );
                $this->mPageCount = 0;
                $this->mIsUpload = $upload;
                $this->mInterwiki = $interwiki;
-               $this->mFrompage = $frompage;
        }
 
        function open() {
@@ -198,24 +189,21 @@ class ImportReporter {
                $localCount = $wgLang->formatNum( $successCount );
                $contentCount = $wgContLang->formatNum( $successCount );
 
-               if( $successCount > 0 ) {
-                       $wgOut->addHtml( "<li>" . $skin->makeKnownLinkObj( $title ) .
-                               " " .
-                               wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
-                               "</li>\n" );
+               $wgOut->addHtml( "<li>" . $skin->makeKnownLinkObj( $title ) .
+                       " " .
+                       wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
+                       "</li>\n" );
 
+               if( $successCount > 0 ) {
                        $log = new LogPage( 'import' );
                        if( $this->mIsUpload ) {
-                               $detail = wfMsgForContent( 'import-logentry-upload-detail',
+                               $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
                                        $contentCount );
                                $log->addEntry( 'upload', $title, $detail );
                        } else {
-                               // Show the source article name in log
-                               $origin = $this->mFrompage != $origTitle->getPrefixedText()
-                                       ? $this->mFrompage
-                                       : $origTitle->getPrefixedText();
-                               $interwiki = '[[:' . $this->mInterwiki . ':' . $origin . ']]';
-                               $detail = wfMsgForContent( 'import-logentry-interwiki-detail',
+                               $interwiki = '[[:' . $this->mInterwiki . ':' .
+                                       $origTitle->getPrefixedText() . ']]';
+                               $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
                                        $contentCount, $interwiki );
                                $log->addEntry( 'interwiki', $title, $detail );
                        }
@@ -225,21 +213,18 @@ class ImportReporter {
                        $nullRevision = Revision::newNullRevision(
                                $dbw, $title->getArticleId(), $comment, true );
                        $nullRevision->insertOn( $dbw );
-               } else {
-                       $wgOut->addHtml( '<li>' . wfMsgExt( 'import-nonewrevisions', array( 'parse', ) ) . '</li>' );
+                       # Update page record
+                       $article = new Article( $title );
+                       $article->updateRevisionOn( $dbw, $nullRevision );
                }
-
        }
 
        function close() {
                global $wgOut;
                if( $this->mPageCount == 0 ) {
-                       $wgOut->addHtml( "</ul>\n" );
-                       return new WikiErrorMsg( "importnopages" );
+                       $wgOut->addHtml( "<li>" . wfMsgHtml( 'importnopages' ) . "</li>\n" );
                }
                $wgOut->addHtml( "</ul>\n" );
-
-               return $this->mPageCount;
        }
 }
 
@@ -415,7 +400,6 @@ class WikiImporter {
        var $mPageOutCallback = null;
        var $mRevisionCallback = null;
        var $mTargetNamespace = null;
-       var $mTargetArticleName = null;
        var $lastfield;
 
        function WikiImporter( $source ) {
@@ -448,7 +432,7 @@ class WikiImporter {
                        $chunk = $this->mSource->readChunk();
                        if( !xml_parse( $parser, $chunk, $this->mSource->atEnd() ) ) {
                                wfDebug( "WikiImporter::doImport encountered XML parsing error\n" );
-                               return new WikiXmlError( $parser, wfMsgHtml( 'import-parse-failure' ), $chunk, $offset );
+                               return new WikiXmlError( $parser, 'XML import parse failure', $chunk, $offset );
                        }
                        $offset += strlen( $chunk );
                } while( $chunk !== false && !$this->mSource->atEnd() );
@@ -523,13 +507,6 @@ class WikiImporter {
                }
        }
 
-       /**
-        * Set a target articlename to override the defaults
-        */
-       function setTargetArticleName( $articleName ) {
-               $this->mTargetArticleName = is_null( $articleName ) ? null : $articleName;
-       }
-
        /**
         * Default per-revision callback, performs the import.
         * @param WikiRevision $revision
@@ -707,15 +684,10 @@ class WikiImporter {
                switch( $this->appendfield ) {
                case "title":
                        $this->workTitle = $this->appenddata;
-
-                       if( $this->mTargetArticleName != '' ) {
-                               // Import article with a different target article name
-                               $this->workTitle = $this->mTargetArticleName;
-                       }
                        $this->origTitle = Title::newFromText( $this->workTitle );
                        if( !is_null( $this->mTargetNamespace ) && !is_null( $this->origTitle ) ) {
                                $this->pageTitle = Title::makeTitle( $this->mTargetNamespace,
-                                       $this->origTitle->getDbKey() );
+                                       $this->origTitle->getDBkey() );
                        } else {
                                $this->pageTitle = Title::newFromText( $this->workTitle );
                        }
@@ -882,7 +854,18 @@ class ImportStreamSource {
                        return new WikiErrorMsg( 'importnofile' );
                }
                if( !empty( $upload['error'] ) ) {
-                       return new WikiErrorMsg( 'importuploaderror', $upload['error'] );
+                       switch($upload['error']){
+                               case 1: # The uploaded file exceeds the upload_max_filesize directive in php.ini. 
+                                       return new WikiErrorMsg( 'importuploaderrorsize' );
+                               case 2: # The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
+                                       return new WikiErrorMsg( 'importuploaderrorsize' );
+                               case 3: # The uploaded file was only partially uploaded
+                                       return new WikiErrorMsg( 'importuploaderrorpartial' );
+                           case 6: #Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.
+                               return new WikiErrorMsg( 'importuploaderrortemp' );
+                           # case else: # Currently impossible
+                       }
+                       
                }
                $fname = $upload['tmp_name'];
                if( is_uploaded_file( $fname ) ) {
@@ -911,9 +894,6 @@ class ImportStreamSource {
        }
 
        public static function newFromInterwiki( $interwiki, $page, $history=false ) {
-               if( $page == '' ) {
-                       return new WikiErrorMsg( 'import-noarticle' );
-               }                       
                $link = Title::newFromText( "$interwiki:Special:Export/$page" );
                if( is_null( $link ) || $link->getInterwiki() == '' ) {
                        return new WikiErrorMsg( 'importbadinterwiki' );