Follow up to r54414 and r54356. Hopefully catches all of the edge cases of erroneous...
authorOverlordQ <overlordq@users.mediawiki.org>
Wed, 5 Aug 2009 01:33:18 +0000 (01:33 +0000)
committerOverlordQ <overlordq@users.mediawiki.org>
Wed, 5 Aug 2009 01:33:18 +0000 (01:33 +0000)
config/index.php
includes/GlobalFunctions.php
includes/Setup.php

index cb83d12..4f6cd28 100644 (file)
@@ -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 "<li>PHP is configured with no <tt>memory_limit</tt>.</li>\n";
 } else {
-       print "<li>PHP's <tt>memory_limit</tt> 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.<br /><b>" . htmlspecialchars( $memlimit ) . " seems too low, installation may fail!</b>";
-               } else {
-                       print "ok.";
-               }
+       print "<li>PHP's <tt>memory_limit</tt> is " . htmlspecialchars( $memlimit ). " bytes. ";
+       if( $newlimit >= $wgMemoryLimit ) {
+               print "Successfully set limit to " . htmlspecialchars( $newlimit ) . "... ";
+       } else {
+               print "<b>Failed raising limit, installation may fail.</b>";
        }
        print "</li>\n";
 }
index 3ee69a6..77daed3 100644 (file)
@@ -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;
 }
-
index 88354c5..7f43957 100644 (file)
@@ -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