Work around broken HHVM ini_get() for 'upload_max_filesize' and 'post_max_size'
authorBartosz Dziewoński <matma.rex@gmail.com>
Fri, 23 Oct 2015 16:03:43 +0000 (18:03 +0200)
committerOri.livneh <ori@wikimedia.org>
Sun, 8 Nov 2015 19:48:23 +0000 (19:48 +0000)
In HHVM, the settings 'upload_max_filesize' and 'post_max_size' are
not available via ini_get() due to some long-standing bug
(https://github.com/facebook/hhvm/issues/4993). Instead, one can use
'hhvm.server.upload.upload_max_file_size' and 'hhvm.server.max_post_size'
(in a typical PHP fashion, their names are subtly different than the
originals as to increase the potential for confusion).

Added a new method UploadBase::getMaxPhpUploadSize() to handle this.

Additionally:
* 'post_max_size' can be set to 0, which is equivalent to no limit.
  Handle this correctly.
* $wgMaxUploadSize can be an array structure, instead of just a number.
  Handle this correctly by using UploadBase::getMaxUploadSize().
* When no maximum is set, use PHP_INT_MAX rather than 1e100. It should
  be big enough, and the latter is a float, results in 0 when cast to
  int, and doesn't look as pretty when formatted in GB in the interface.

Bug: T116347
Change-Id: Idf707253eeae1b90792a7e26d2ab66d1317e67ae

RELEASE-NOTES-1.27
includes/Setup.php
includes/WebRequestUpload.php
includes/specials/SpecialUpload.php
includes/upload/UploadBase.php
includes/upload/UploadFromFile.php

index 76f8ba3..442e70d 100644 (file)
@@ -77,6 +77,8 @@ production.
 ==== External libraries ====
 
 === Bug fixes in 1.27 ===
+* Special:Upload will now display correct maximum allowed file size when running
+  under HHVM (T116347).
 
 === Action API changes in 1.27 ===
 * Added list=allrevisions.
index 69a041e..355b03a 100644 (file)
@@ -380,9 +380,12 @@ if ( $wgResourceLoaderMaxQueryLength === false ) {
 // upload size.
 $wgMinUploadChunkSize = min(
        $wgMinUploadChunkSize,
-       $wgMaxUploadSize,
-       wfShorthandToInteger( ini_get( 'upload_max_filesize' ), 1e100 ),
-       wfShorthandToInteger( ini_get( 'post_max_size' ), 1e100 ) - 1024 # Leave room for other parameters
+       UploadBase::getMaxUploadSize( 'file' ),
+       UploadBase::getMaxPhpUploadSize(),
+       ( wfShorthandToInteger(
+               ini_get( 'post_max_size' ) ?: ini_get( 'hhvm.server.max_post_size' ),
+               PHP_INT_MAX
+       ) ?: PHP_INT_MAX ) - 1024 // Leave some room for other POST parameters
 );
 
 /**
index e743d9d..c99b0e3 100644 (file)
@@ -127,7 +127,12 @@ class WebRequestUpload {
                }
 
                $contentLength = $this->request->getHeader( 'CONTENT_LENGTH' );
-               if ( $contentLength > wfShorthandToInteger( ini_get( 'post_max_size' ) ) ) {
+               $maxPostSize = wfShorthandToInteger(
+                       ini_get( 'post_max_size' ) ?: ini_get( 'hhvm.server.max_post_size' ),
+                       0
+               );
+
+               if ( $maxPostSize && $contentLength > $maxPostSize ) {
                        # post_max_size is exceeded
                        return true;
                }
index 6692bb6..c8c4642 100644 (file)
@@ -890,15 +890,10 @@ class UploadForm extends HTMLForm {
                        );
                }
 
-               $this->mMaxUploadSize['file'] = UploadBase::getMaxUploadSize( 'file' );
-               # Limit to upload_max_filesize unless we are running under HipHop and
-               # that setting doesn't exist
-               if ( !wfIsHHVM() ) {
-                       $this->mMaxUploadSize['file'] = min( $this->mMaxUploadSize['file'],
-                               wfShorthandToInteger( ini_get( 'upload_max_filesize' ) ),
-                               wfShorthandToInteger( ini_get( 'post_max_size' ) )
-                       );
-               }
+               $this->mMaxUploadSize['file'] = min(
+                       UploadBase::getMaxUploadSize( 'file' ),
+                       UploadBase::getMaxPhpUploadSize()
+               );
 
                $help = $this->msg( 'upload-maxfilesize',
                                $this->getContext()->getLanguage()->formatSize( $this->mMaxUploadSize['file'] )
index 17fcab8..f8624d0 100644 (file)
@@ -1919,6 +1919,9 @@ abstract class UploadBase {
        }
 
        /**
+        * Get the MediaWiki maximum uploaded file size for given type of upload, based on
+        * $wgMaxUploadSize.
+        *
         * @param null|string $forType
         * @return int
         */
@@ -1936,6 +1939,25 @@ abstract class UploadBase {
                }
        }
 
+       /**
+        * Get the PHP maximum uploaded file size, based on ini settings. If there is no limit or the
+        * limit can't be guessed, returns a very large number (PHP_INT_MAX).
+        *
+        * @since 1.27
+        * @return int
+        */
+       public static function getMaxPhpUploadSize() {
+               $phpMaxFileSize = wfShorthandToInteger(
+                       ini_get( 'upload_max_filesize' ) ?: ini_get( 'hhvm.server.upload.upload_max_file_size' ),
+                       PHP_INT_MAX
+               );
+               $phpMaxPostSize = wfShorthandToInteger(
+                       ini_get( 'post_max_size' ) ?: ini_get( 'hhvm.server.max_post_size' ),
+                       PHP_INT_MAX
+               ) ?: PHP_INT_MAX;
+               return min( $phpMaxFileSize, $phpMaxPostSize );
+       }
+
        /**
         * Get the current status of a chunked upload (used for polling)
         *
index 3a1e8bd..1607cb6 100644 (file)
@@ -86,8 +86,7 @@ class UploadFromFile extends UploadBase {
                                        'status' => UploadBase::FILE_TOO_LARGE,
                                        'max' => min(
                                                self::getMaxUploadSize( $this->getSourceType() ),
-                                               wfShorthandToInteger( ini_get( 'upload_max_filesize' ) ),
-                                               wfShorthandToInteger( ini_get( 'post_max_size' ) )
+                                               self::getMaxPhpUploadSize()
                                        ),
                                );
                        }