From 30e44a06fc1f349b6487e458fcda99d540c0183e Mon Sep 17 00:00:00 2001 From: OverlordQ Date: Wed, 5 Aug 2009 01:33:18 +0000 Subject: [PATCH] Follow up to r54414 and r54356. Hopefully catches all of the edge cases of erroneous input. On a side note, if PHP is configured with too low a limit it can die before it even reaches the code in Setup.php --- config/index.php | 18 +++++++-------- includes/GlobalFunctions.php | 44 ++++++++++++++++++++++++++++++++---- includes/Setup.php | 11 +-------- 3 files changed, 48 insertions(+), 25 deletions(-) diff --git a/config/index.php b/config/index.php index cb83d12390..4f6cd28a80 100644 --- a/config/index.php +++ b/config/index.php @@ -466,18 +466,16 @@ if( !function_exists( 'preg_match' ) ) Perl-compatible regular expression functions." ); $memlimit = ini_get( "memory_limit" ); -if( empty( $memlimit ) || $memlimit == -1 ) { +$newlimit = wfMemoryLimit(); +global $wgMemoryLimit; +if( $memlimit == -1 ) { print "
  • PHP is configured with no memory_limit.
  • \n"; } else { - print "
  • PHP's memory_limit is " . htmlspecialchars( $memlimit ) . ". "; - global $wgMemoryLimit; - if( wfParseMemoryLimit( $memlimit ) < wfParseMemoryLimit( $wgMemoryLimit ) ) { - print "Attempting to raise limit to " . htmlspecialchars( $wgMemoryLimit ) . "... "; - if( false === ini_set( "memory_limit", $wgMemoryLimit ) ) { - print "failed.
    " . htmlspecialchars( $memlimit ) . " seems too low, installation may fail!"; - } else { - print "ok."; - } + print "
  • PHP's memory_limit is " . htmlspecialchars( $memlimit ). " bytes. "; + if( $newlimit >= $wgMemoryLimit ) { + print "Successfully set limit to " . htmlspecialchars( $newlimit ) . "... "; + } else { + print "Failed raising limit, installation may fail."; } print "
  • \n"; } diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 3ee69a6dcc..77daed3859 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -3193,10 +3193,44 @@ function wfObjectToArray( $object, $recursive = true ) { return $array; } -/* Parse PHP's silly format for memory limits */ -function wfParseMemoryLimit( $memlimit ) { - $last = strtolower($memlimit[strlen($memlimit)-1]); - $val = intval( $memlimit ); +/** + * Set PHP's memory limit to the larger of php.ini or $wgMemoryLimit; + * @return Integer value memory was set to. + */ + +function wfMemoryLimit () { + global $wgMemoryLimit; + $memlimit = wfShorthandToInteger( ini_get( "memory_limit" ) ); + $conflimit = wfShorthandToInteger( $wgMemoryLimit ); + if( $memlimit != -1 ) { + if( $conflimit == -1 ) { + wfDebug( "Removing PHP's memory limit\n" ); + wfSuppressWarnings(); + ini_set( "memory_limit", $conflimit ); + wfRestoreWarnings(); + return $conflimit; + } else { + $max = max( $memlimit, $conflimit ); + wfDebug( "Raising PHP's memory limit to $max bytes\n" ); + wfSuppressWarnings(); + ini_set( "memory_limit", $max ); + wfRestoreWarnings(); + return $max; + } + } + return $memlimit; +} + +/** + * Converts shorthand byte notation to integer form + * @param $string String + * @return Integer + */ +function wfShorthandToInteger ( $string = '' ) { + $string = trim($string); + if( empty($string) ) { return -1; } + $last = strtolower($string[strlen($string)-1]); + $val = intval($string); switch($last) { case 'g': $val *= 1024; @@ -3205,6 +3239,7 @@ function wfParseMemoryLimit( $memlimit ) { case 'k': $val *= 1024; } + return $val; } @@ -3233,4 +3268,3 @@ function wfBCP47( $code ) { $langCode = implode ( '-' , $codeBCP ); return $langCode; } - diff --git a/includes/Setup.php b/includes/Setup.php index 88354c5ea8..7f4395707c 100644 --- a/includes/Setup.php +++ b/includes/Setup.php @@ -150,16 +150,7 @@ require_once( "$IP/includes/StubObject.php" ); wfProfileOut( $fname.'-includes' ); wfProfileIn( $fname.'-misc1' ); # Raise the memory limit if it's too low -global $wgMemoryLimit; -$memlimit = ini_get( "memory_limit" ); -if( !( empty( $memlimit ) || $memlimit == -1 ) ) { - if( wfParseMemoryLimit( $memlimit ) < wfParseMemoryLimit( $wgMemoryLimit ) ) { - wfDebug( "\n\nRaise PHP's memory limit from $memlimit to $wgMemoryLimit\n" ); - wfDisableWarnings(); - ini_set( "memory_limit", $wgMemoryLimit ); - wfEnableWarnings(); - } -} +wfMemoryLimit(); $wgIP = false; # Load on demand # Can't stub this one, it sets up $_GET and $_REQUEST in its constructor -- 2.20.1