From: Bryan Tong Minh Date: Thu, 6 Jan 2011 19:42:55 +0000 (+0000) Subject: $wgMaxUploadSize may now be set to an array to specify the upload size limit per... X-Git-Tag: 1.31.0-rc.0~32749 X-Git-Url: http://git.cyclocoop.org/url?a=commitdiff_plain;h=db544af120d640a39f6819bbfb2318f23f820fd9;p=lhc%2Fweb%2Fwiklou.git $wgMaxUploadSize may now be set to an array to specify the upload size limit per upload type. 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. --- diff --git a/RELEASE-NOTES b/RELEASE-NOTES index f4ebccd852..93b61e908d 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -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 diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 3da6ec7dfd..5aa0193c90 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -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 diff --git a/includes/specials/SpecialUpload.php b/includes/specials/SpecialUpload.php index 2db1e27666..b0a6e93f12 100644 --- a/includes/specials/SpecialUpload.php +++ b/includes/specials/SpecialUpload.php @@ -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', ); diff --git a/includes/upload/UploadBase.php b/includes/upload/UploadBase.php index d02f752054..dd108adf47 100644 --- a/includes/upload/UploadBase.php +++ b/includes/upload/UploadBase.php @@ -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 ); + } + + } } diff --git a/includes/upload/UploadFromFile.php b/includes/upload/UploadFromFile.php index e67ec191b0..e38fbcf4da 100644 --- a/includes/upload/UploadFromFile.php +++ b/includes/upload/UploadFromFile.php @@ -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' ) ) ), diff --git a/includes/upload/UploadFromStash.php b/includes/upload/UploadFromStash.php index 9b386b9b27..23957bb6fd 100644 --- a/includes/upload/UploadFromStash.php +++ b/includes/upload/UploadFromStash.php @@ -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. */ diff --git a/includes/upload/UploadFromUrl.php b/includes/upload/UploadFromUrl.php index 01f201dbf2..8e4ffbf36a 100644 --- a/includes/upload/UploadFromUrl.php +++ b/includes/upload/UploadFromUrl.php @@ -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 ) ) {