$wgMaxUploadSize may now be set to an array to specify the upload size limit per...
authorBryan Tong Minh <btongminh@users.mediawiki.org>
Thu, 6 Jan 2011 19:42:55 +0000 (19:42 +0000)
committerBryan Tong Minh <btongminh@users.mediawiki.org>
Thu, 6 Jan 2011 19:42:55 +0000 (19:42 +0000)
Backwards compatible, if not set to an array, applies to all uploads. If set to an array, per upload type maximums can be set, using the file and url keys. If the * key is set this value will be used as maximum for non-specified types.

RELEASE-NOTES
includes/DefaultSettings.php
includes/specials/SpecialUpload.php
includes/upload/UploadBase.php
includes/upload/UploadFromFile.php
includes/upload/UploadFromStash.php
includes/upload/UploadFromUrl.php

index f4ebccd..93b61e9 100644 (file)
@@ -31,6 +31,8 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
   note that this changes the cache key making all old entries in the parser cache invalid you
   can set $wgUseEditSectionTokens to false to disable this and keep your old parser cache entries.
   Note that this feature should reduce parser cache fragmentation when enabled.
+* $wgMaxUploadSize may now be set to an array to specify the upload size limit 
+  per upload type.
 
 === New features in 1.18 ===
 * Added a special page, disabled by default, that allows users with the
index 3da6ec7..5aa0193 100644 (file)
@@ -439,7 +439,19 @@ $wgAllowCopyUploads = false;
 $wgAllowAsyncCopyUploads = false;
 
 /**
- * Max size for uploads, in bytes. Applies to all uploads.
+ * Max size for uploads, in bytes. If not set to an array, applies to all 
+ * uploads. If set to an array, per upload type maximums can be set, using the
+ * file and url keys. If the * key is set this value will be used as maximum
+ * for non-specified types.
+ * 
+ * For example:
+ *     $wgUploadSize = array(
+ *             '*' => 250 * 1024,
+ *             'url' => 500 * 1024,
+ *     );
+ * Sets the maximum for all uploads to 250 kB except for upload-by-url, which
+ * will have a maximum of 500 kB.
+ * 
  */
 $wgMaxUploadSize = 1024*1024*100; # 100MB
 
index 2db1e27..b0a6e93 100644 (file)
@@ -812,7 +812,6 @@ class UploadForm extends HTMLForm {
         */
        protected function getSourceSection() {
                global $wgLang, $wgUser, $wgRequest;
-               global $wgMaxUploadSize;
 
                if ( $this->mSessionKey ) {
                        return array(
@@ -855,7 +854,7 @@ class UploadForm extends HTMLForm {
                                                wfShorthandToInteger( min( 
                                                        wfShorthandToInteger(
                                                                ini_get( 'upload_max_filesize' )
-                                                       ), $wgMaxUploadSize
+                                                       ), UploadBase::getMaxUploadSize( 'file' )
                                                ) )
                                        )
                                ) . ' ' . wfMsgHtml( 'upload_source_file' ),
@@ -871,7 +870,7 @@ class UploadForm extends HTMLForm {
                                'radio' => &$radio,
                                'help' => wfMsgExt( 'upload-maxfilesize',
                                                array( 'parseinline', 'escapenoentities' ),
-                                               $wgLang->formatSize( $wgMaxUploadSize )
+                                               $wgLang->formatSize( UploadBase::getMaxUploadSize( 'url' ) )
                                        ) . ' ' . wfMsgHtml( 'upload_source_url' ),
                                'checked' => $selectedSourceType == 'url',
                        );
index d02f752..dd108ad 100644 (file)
@@ -142,6 +142,14 @@ abstract class UploadBase {
        }
 
        public function __construct() {}
+       
+       /**
+        * Returns the upload type. Should be overridden by child classes
+        * 
+        * @since 1.18
+        * @return string 
+        */
+       public function getSourceType() { return null; }
 
        /**
         * Initialize the path information
@@ -226,11 +234,11 @@ abstract class UploadBase {
                /**
                 * Honor $wgMaxUploadSize
                 */
-               global $wgMaxUploadSize;
-               if( $this->mFileSize > $wgMaxUploadSize ) {
+               $maxSize = self::getMaxUploadSize( $this->getSourceType() );
+               if( $this->mFileSize > $maxSize ) {
                        return array( 
                                'status' => self::FILE_TOO_LARGE,
-                               'max' => $wgMaxUploadSize,
+                               'max' => $maxSize,
                        );
                }
 
@@ -629,7 +637,8 @@ abstract class UploadBase {
        public function stashSessionFile( $key = null ) { 
                $stash = new UploadStash();
                $data = array( 
-                       'mFileProps' => $this->mFileProps
+                       'mFileProps' => $this->mFileProps,
+                       'mSourceType' => $this->getSourceType(),
                );
                $file = $stash->stashFile( $this->mTempPath, $data, $key );
                $this->mLocalFile = $file;
@@ -1220,4 +1229,19 @@ abstract class UploadBase {
                unset( $code['status'] );
                return Status::newFatal( $this->getVerificationErrorCode( $code ), $error );
        }
+       
+       public static function getMaxUploadSize( $forType = null ) {
+               global $wgMaxUploadSize;
+               
+               if ( is_array( $wgMaxUploadSize ) ) {
+                       if ( !is_null( $forType) && isset( $wgMaxUploadSize[$forType] ) ) {
+                               return $wgMaxUploadSize[$forType];
+                       } else {
+                               return $wgMaxUploadSize['*'];
+                       }
+               } else {
+                       return intval( $wgMaxUploadSize );
+               }
+               
+       }
 }
index e67ec19..e38fbcf 100644 (file)
@@ -33,16 +33,17 @@ class UploadFromFile extends UploadBase {
                return true;
        }
        
+       public function getSourceType() { return 'file'; }
+       
        public function verifyUpload() {
                # Check for a post_max_size or upload_max_size overflow, so that a 
                # proper error can be shown to the user
                if ( is_null( $this->mTempPath ) || $this->isEmptyFile() ) {
                        if ( $this->mUpload->isIniSizeOverflow() ) {
-                               global $wgMaxUploadSize;
                                return array( 
                                        'status' => UploadBase::FILE_TOO_LARGE,
                                        'max' => min( 
-                                               $wgMaxUploadSize
+                                               self::getMaxUploadSize( $this->getSourceType() )
                                                wfShorthandToInteger( ini_get( 'upload_max_filesize' ) ), 
                                                wfShorthandToInteger( ini_get( 'post_max_size' ) )
                                        ),
index 9b386b9..23957bb 100644 (file)
@@ -41,6 +41,8 @@ class UploadFromStash extends UploadBase {
                        $this->mSessionKey = $sessionKey;
                        $this->mVirtualTempPath = $sessionData['mTempPath'];
                        $this->mFileProps = $sessionData['mFileProps'];
+                       $this->mSourceType = isset( $sessionData['mSourceType'] ) ?
+                               $sessionData['mSourceType'] : null;
        }
 
        public function initializeFromRequest( &$request ) {
@@ -53,6 +55,10 @@ class UploadFromStash extends UploadBase {
                return $this->initialize( $desiredDestName, $sessionKey, $sessionData[$sessionKey] );
        }
 
+       public function getSourceType() { 
+               return $this->mSourceType; 
+       }
+
        /**
         * File has been previously verified so no need to do so again.
         */
index 01f201d..8e4ffbf 100644 (file)
@@ -78,6 +78,7 @@ class UploadFromUrl extends UploadBase {
                        && $wgUser->isAllowed( 'upload_by_url' );
        }
 
+       public function getSourceType() { return 'url'; }
 
        public function fetchFile() {
                if ( !Http::isValidURI( $this->mUrl ) ) {