* Confirmation is now required when deleting old versions of files
[lhc/web/wiklou.git] / includes / SpecialUpload.php
index a187f65..1c738cc 100644 (file)
@@ -46,9 +46,11 @@ class UploadForm {
                global $wgAllowCopyUploads;
                $this->mDesiredDestName   = $request->getText( 'wpDestFile' );
                $this->mIgnoreWarning     = $request->getCheck( 'wpIgnoreWarning' );
+               $this->mComment           = $request->getText( 'wpUploadDescription' );
 
                if( !$request->wasPosted() ) {
-                       # GET requests just give the main form; no data except wpDestfile.
+                       # GET requests just give the main form; no data except destination
+                       # filename and description
                        return;
                }
 
@@ -59,7 +61,6 @@ class UploadForm {
                $this->mReUpload          = $request->getCheck( 'wpReUpload' );
                $this->mUploadClicked     = $request->getCheck( 'wpUpload' );
 
-               $this->mComment           = $request->getText( 'wpUploadDescription' );
                $this->mLicense           = $request->getText( 'wpLicense' );
                $this->mCopyrightStatus   = $request->getText( 'wpUploadCopyStatus' );
                $this->mCopyrightSource   = $request->getText( 'wpUploadSource' );
@@ -284,16 +285,17 @@ class UploadForm {
 
                # Chop off any directories in the given filename
                if( $this->mDesiredDestName ) {
-                       $basename = wfBaseName( $this->mDesiredDestName );
+                       $basename = $this->mDesiredDestName;
                } else {
-                       $basename = wfBaseName( $this->mSrcName );
+                       $basename = $this->mSrcName;
                }
+               $filtered = wfBaseName( $basename );
 
                /**
                 * We'll want to blacklist against *any* 'extension', and use
                 * only the final one for the whitelist.
                 */
-               list( $partname, $ext ) = $this->splitExtensions( $basename );
+               list( $partname, $ext ) = $this->splitExtensions( $filtered );
 
                if( count( $ext ) ) {
                        $finalExt = $ext[count( $ext ) - 1];
@@ -317,7 +319,7 @@ class UploadForm {
                 * Filter out illegal characters, and try to make a legible name
                 * out of it. We'll strip some silently that Title would die on.
                 */
-               $filtered = preg_replace ( "/[^".Title::legalChars()."]|:/", '-', $basename );
+               $filtered = preg_replace ( "/[^".Title::legalChars()."]|:/", '-', $filtered );
                $nt = Title::makeTitleSafe( NS_IMAGE, $filtered );
                if( is_null( $nt ) ) {
                        $this->uploadError( wfMsgWikiHtml( 'illegalfilename', htmlspecialchars( $filtered ) ) );
@@ -368,7 +370,7 @@ class UploadForm {
                        }
 
                        /**
-                        * Provide an opportunity for extensions to add futher checks
+                        * Provide an opportunity for extensions to add further checks
                         */
                        $error = '';
                        if( !wfRunHooks( 'UploadVerification',
@@ -388,7 +390,7 @@ class UploadForm {
                        if( $wgCapitalLinks ) {
                                $filtered = ucfirst( $filtered );
                        }
-                       if( $this->mDestName != $filtered ) {
+                       if( $basename != $filtered ) {
                                $warning .=  '<li>'.wfMsgHtml( 'badfilename', htmlspecialchars( $this->mDestName ) ).'</li>';
                        }
 
@@ -432,8 +434,8 @@ class UploadForm {
 
                $status = $this->mLocalFile->upload( $this->mTempPath, $this->mComment, $pageText, 
                        File::DELETE_SOURCE, $this->mFileProps );
-               if ( WikiError::isError( $status ) ) {
-                       $this->showError( $status );
+               if ( !$status->isGood() ) {
+                       $this->showError( $status->getWikiText() );
                } else {
                        if ( $this->mWatchthis ) {
                                global $wgUser;
@@ -556,6 +558,25 @@ class UploadForm {
                }
                return $s;
        }
+       
+       /**
+        * Render a preview of a given license for the AJAX preview on upload
+        *
+        * @param string $license
+        * @return string
+        */
+       public static function ajaxGetLicensePreview( $license ) {
+               global $wgParser, $wgUser;
+               $text = '{{' . $license . '}}';
+               $title = Title::makeTitle( NS_IMAGE, 'Sample.jpg' );
+               $options = ParserOptions::newFromUser( $wgUser );
+               
+               // Expand subst: first, then live templates...
+               $text = $wgParser->preSaveTransform( $text, $title, $wgUser, $options );
+               $output = $wgParser->parse( $text, $title, $options );
+               
+               return $output->getText();
+       }
 
        /**
         * Stash a file in a temporary directory for later processing
@@ -572,12 +593,12 @@ class UploadForm {
        function saveTempUploadedFile( $saveName, $tempName ) {
                global $wgOut;
                $repo = RepoGroup::singleton()->getLocalRepo();
-               $result = $repo->storeTemp( $saveName, $tempName );
-               if ( WikiError::isError( $result ) ) {
-                       $this->showError( $result );
+               $status = $repo->storeTemp( $saveName, $tempName );
+               if ( !$status->isGood() ) {
+                       $this->showError( $status->getWikiText() );
                        return false;
                } else {
-                       return $result;
+                       return $status->value;
                }
        }
 
@@ -647,7 +668,7 @@ class UploadForm {
         * @access private
         */
        function uploadWarning( $warning ) {
-               global $wgOut;
+               global $wgOut, $wgContLang;
                global $wgUseCopyrightUpload;
 
                $this->mSessionKey = $this->stashSession();
@@ -665,6 +686,8 @@ class UploadForm {
                $reup = wfMsgWikiHtml( 'reuploaddesc' );
                $titleObj = SpecialPage::getTitleFor( 'Upload' );
                $action = $titleObj->escapeLocalURL( 'action=submit' );
+               $align1 = $wgContLang->isRTL() ? 'left' : 'right';
+               $align2 = $wgContLang->isRTL() ? 'right' : 'left';
 
                if ( $wgUseCopyrightUpload )
                {
@@ -688,16 +711,16 @@ class UploadForm {
        <table border='0'>
                <tr>
                        <tr>
-                               <td align='right'>
+                               <td align='$align1'>
                                        <input tabindex='2' type='submit' name='wpUpload' value=\"$save\" />
                                </td>
-                               <td align='left'>$iw</td>
+                               <td align='$align2'>$iw</td>
                        </tr>
                        <tr>
-                               <td align='right'>
+                               <td align='$align1'>
                                        <input tabindex='2' type='submit' name='wpReUpload' value=\"{$reupload}\" />
                                </td>
-                               <td align='left'>$reup</td>
+                               <td align='$align2'>$reup</td>
                        </tr>
                </tr>
        </table></form>\n" );
@@ -711,18 +734,23 @@ class UploadForm {
         * @access private
         */
        function mainUploadForm( $msg='' ) {
-               global $wgOut, $wgUser;
-               global $wgUseCopyrightUpload, $wgAjaxUploadDestCheck, $wgUseAjax;
+               global $wgOut, $wgUser, $wgContLang;
+               global $wgUseCopyrightUpload, $wgUseAjax, $wgAjaxUploadDestCheck, $wgAjaxLicensePreview;
                global $wgRequest, $wgAllowCopyUploads, $wgEnableAPI;
-               global $wgStylePath;
+               global $wgStylePath, $wgStyleVersion;
 
-               $useAjax = $wgAjaxUploadDestCheck && $wgUseAjax;
-
-               $wgOut->addScript( 
-                       "<script type='text/javascript'>wgAjaxUploadDestCheck = " . 
-                               ($useAjax ? 'true' : 'false' ) . ";</script>\n" . 
-                       "<script type='text/javascript' src=\"$wgStylePath/common/upload.js?1\"></script>\n" 
-               );
+               $useAjaxDestCheck = $wgUseAjax && $wgAjaxUploadDestCheck;
+               $useAjaxLicensePreview = $wgUseAjax && $wgAjaxLicensePreview;
+               
+               $adc = wfBoolToStr( $useAjaxDestCheck );
+               $alp = wfBoolToStr( $useAjaxLicensePreview );
+               
+               $wgOut->addScript( "<script type=\"text/javascript\">
+wgAjaxUploadDestCheck = {$adc};
+wgAjaxLicensePreview = {$alp};
+</script>
+<script type=\"text/javascript\" src=\"{$wgStylePath}/common/upload.js?{$wgStyleVersion}\"></script>
+               " );
 
                if( !wfRunHooks( 'UploadForm:initial', array( &$this ) ) )
                {
@@ -794,7 +822,7 @@ class UploadForm {
                                "size='40' />" .
                                "<input type='hidden' name='wpSourceType' value='file' />" ;
                }
-               if ( $useAjax ) {
+               if ( $useAjaxDestCheck ) {
                        $warningRow = "<tr><td colspan='2' id='wpDestFile-warning'>&nbsp</td></tr>";
                        $destOnkeyup = 'onkeyup="wgUploadWarningObj.keypress();"';
                } else {
@@ -803,27 +831,29 @@ class UploadForm {
                }
 
                $encComment = htmlspecialchars( $this->mComment );
+               $align1 = $wgContLang->isRTL() ? 'left' : 'right';
+               $align2 = $wgContLang->isRTL() ? 'right' : 'left';
 
                $wgOut->addHTML( <<<EOT
        <form id='upload' method='post' enctype='multipart/form-data' action="$action">
                <table border='0'>
                <tr>
          {$this->uploadFormTextTop}
-                       <td align='right' valign='top'><label for='wpUploadFile'>{$sourcefilename}:</label></td>
-                       <td align='left'>
+                       <td align='$align1' valign='top'><label for='wpUploadFile'>{$sourcefilename}:</label></td>
+                       <td align='$align2'>
                                {$filename_form}
                        </td>
                </tr>
                <tr>
-                       <td align='right'><label for='wpDestFile'>{$destfilename}:</label></td>
-                       <td align='left'>
+                       <td align='$align1'><label for='wpDestFile'>{$destfilename}:</label></td>
+                       <td align='$align2'>
                                <input tabindex='2' type='text' name='wpDestFile' id='wpDestFile' size='40' 
                                        value="$encDestName" $destOnkeyup />
                        </td>
                </tr>
                <tr>
-                       <td align='right'><label for='wpUploadDescription'>{$summary}</label></td>
-                       <td align='left'>
+                       <td align='$align1'><label for='wpUploadDescription'>{$summary}</label></td>
+                       <td align='$align2'>
                                <textarea tabindex='3' name='wpUploadDescription' id='wpUploadDescription' rows='6' 
                                        cols='{$cols}'{$ew}>$encComment</textarea>
           {$this->uploadFormTextAfterSummary}
@@ -836,8 +866,8 @@ EOT
                if ( $licenseshtml != '' ) {
                        global $wgStylePath;
                        $wgOut->addHTML( "
-                       <td align='right'><label for='wpLicense'>$license:</label></td>
-                       <td align='left'>
+                       <td align='$align1'><label for='wpLicense'>$license:</label></td>
+                       <td align='$align2'>
                                <select name='wpLicense' id='wpLicense' tabindex='4'
                                        onchange='licenseSelectorCheck()'>
                                        <option value=''>$nolicense</option>
@@ -845,8 +875,14 @@ EOT
                                </select>
                        </td>
                        </tr>
-                       <tr>
-               ");
+                       <tr>" );
+                       if( $useAjaxLicensePreview ) {
+                               $wgOut->addHtml( "
+                                       <td></td>
+                                       <td id=\"mw-license-preview\"></td>
+                               </tr>
+                               <tr>" );
+                       }
                }
 
                if ( $wgUseCopyrightUpload ) {
@@ -856,12 +892,12 @@ EOT
                        $uploadsource = htmlspecialchars( $this->mCopyrightSource );
 
                        $wgOut->addHTML( "
-                               <td align='right' nowrap='nowrap'><label for='wpUploadCopyStatus'>$filestatus:</label></td>
+                               <td align='$align1' nowrap='nowrap'><label for='wpUploadCopyStatus'>$filestatus:</label></td>
                                        <td><input tabindex='5' type='text' name='wpUploadCopyStatus' id='wpUploadCopyStatus' 
                                          value=\"$copystatus\" size='40' /></td>
                        </tr>
                        <tr>
-                               <td align='right'><label for='wpUploadCopyStatus'>$filesource:</label></td>
+                               <td align='$align1'><label for='wpUploadCopyStatus'>$filesource:</label></td>
                                        <td><input tabindex='6' type='text' name='wpUploadSource' id='wpUploadCopyStatus' 
                                          value=\"$uploadsource\" size='40' /></td>
                        </tr>
@@ -881,11 +917,11 @@ EOT
        $warningRow
        <tr>
                <td></td>
-               <td align='left'><input tabindex='9' type='submit' name='wpUpload' value=\"{$ulb}\" /></td>
+               <td align='$align2'><input tabindex='9' type='submit' name='wpUpload' value=\"{$ulb}\" /></td>
        </tr>
        <tr>
                <td></td>
-               <td align='left'>
+               <td align='$align2'>
                " );
                $wgOut->addWikiText( wfMsgForContent( 'edittools' ) );
                $wgOut->addHTML( "
@@ -958,6 +994,7 @@ EOT
                global $wgVerifyMimeType;
                if ($wgVerifyMimeType) {
 
+                 wfDebug ( "\n\nmime: <$mime> extension: <$extension>\n\n");
                        #check mime type against file extension
                        if( !$this->verifyExtension( $mime, $extension ) ) {
                                return new WikiErrorMsg( 'uploadcorrupt' );
@@ -1322,15 +1359,15 @@ EOT
        }
 
        /**
-        * Display an error from a wikitext-formatted WikiError object
+        * Display an error with a wikitext description
         */
-       function showError( WikiError $error ) {
+       function showError( $description ) {
                global $wgOut;
                $wgOut->setPageTitle( wfMsg( "internalerror" ) );
                $wgOut->setRobotpolicy( "noindex,nofollow" );
                $wgOut->setArticleRelated( false );
                $wgOut->enableClientCache( false );
-               $wgOut->addWikiText( $error->getMessage() );
+               $wgOut->addWikiText( $description );
        }
 
        /**
@@ -1359,3 +1396,4 @@ EOT
        }
 }
 
+