r54416 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r54415‎ | r54416 | r54417 >
Date:01:33, 5 August 2009
Author:overlordq
Status:resolved (Comments)
Tags:
Comment:
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
Modified paths:
  • /trunk/phase3/config/index.php (modified) (history)
  • /trunk/phase3/includes/GlobalFunctions.php (modified) (history)
  • /trunk/phase3/includes/Setup.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/GlobalFunctions.php
@@ -3193,10 +3193,44 @@
31943194 return $array;
31953195 }
31963196
3197 -/* Parse PHP's silly format for memory limits */
3198 -function wfParseMemoryLimit( $memlimit ) {
3199 - $last = strtolower($memlimit[strlen($memlimit)-1]);
3200 - $val = intval( $memlimit );
 3197+/**
 3198+ * Set PHP's memory limit to the larger of php.ini or $wgMemoryLimit;
 3199+ * @return Integer value memory was set to.
 3200+ */
 3201+
 3202+function wfMemoryLimit () {
 3203+ global $wgMemoryLimit;
 3204+ $memlimit = wfShorthandToInteger( ini_get( "memory_limit" ) );
 3205+ $conflimit = wfShorthandToInteger( $wgMemoryLimit );
 3206+ if( $memlimit != -1 ) {
 3207+ if( $conflimit == -1 ) {
 3208+ wfDebug( "Removing PHP's memory limit\n" );
 3209+ wfSuppressWarnings();
 3210+ ini_set( "memory_limit", $conflimit );
 3211+ wfRestoreWarnings();
 3212+ return $conflimit;
 3213+ } else {
 3214+ $max = max( $memlimit, $conflimit );
 3215+ wfDebug( "Raising PHP's memory limit to $max bytes\n" );
 3216+ wfSuppressWarnings();
 3217+ ini_set( "memory_limit", $max );
 3218+ wfRestoreWarnings();
 3219+ return $max;
 3220+ }
 3221+ }
 3222+ return $memlimit;
 3223+}
 3224+
 3225+/**
 3226+ * Converts shorthand byte notation to integer form
 3227+ * @param $string String
 3228+ * @return Integer
 3229+ */
 3230+function wfShorthandToInteger ( $string = '' ) {
 3231+ $string = trim($string);
 3232+ if( empty($string) ) { return -1; }
 3233+ $last = strtolower($string[strlen($string)-1]);
 3234+ $val = intval($string);
32013235 switch($last) {
32023236 case 'g':
32033237 $val *= 1024;
@@ -3205,6 +3239,7 @@
32063240 case 'k':
32073241 $val *= 1024;
32083242 }
 3243+
32093244 return $val;
32103245 }
32113246
@@ -3233,4 +3268,3 @@
32343269 $langCode = implode ( '-' , $codeBCP );
32353270 return $langCode;
32363271 }
3237 -
Index: trunk/phase3/includes/Setup.php
@@ -150,16 +150,7 @@
151151 wfProfileOut( $fname.'-includes' );
152152 wfProfileIn( $fname.'-misc1' );
153153 # Raise the memory limit if it's too low
154 -global $wgMemoryLimit;
155 -$memlimit = ini_get( "memory_limit" );
156 -if( !( empty( $memlimit ) || $memlimit == -1 ) ) {
157 - if( wfParseMemoryLimit( $memlimit ) < wfParseMemoryLimit( $wgMemoryLimit ) ) {
158 - wfDebug( "\n\nRaise PHP's memory limit from $memlimit to $wgMemoryLimit\n" );
159 - wfDisableWarnings();
160 - ini_set( "memory_limit", $wgMemoryLimit );
161 - wfEnableWarnings();
162 - }
163 -}
 154+wfMemoryLimit();
164155
165156 $wgIP = false; # Load on demand
166157 # Can't stub this one, it sets up $_GET and $_REQUEST in its constructor
Index: trunk/phase3/config/index.php
@@ -466,18 +466,16 @@
467467 Perl-compatible regular expression functions." );
468468
469469 $memlimit = ini_get( "memory_limit" );
470 -if( empty( $memlimit ) || $memlimit == -1 ) {
 470+$newlimit = wfMemoryLimit();
 471+global $wgMemoryLimit;
 472+if( $memlimit == -1 ) {
471473 print "<li>PHP is configured with no <tt>memory_limit</tt>.</li>\n";
472474 } else {
473 - print "<li>PHP's <tt>memory_limit</tt> is " . htmlspecialchars( $memlimit ) . ". ";
474 - global $wgMemoryLimit;
475 - if( wfParseMemoryLimit( $memlimit ) < wfParseMemoryLimit( $wgMemoryLimit ) ) {
476 - print "Attempting to raise limit to " . htmlspecialchars( $wgMemoryLimit ) . "... ";
477 - if( false === ini_set( "memory_limit", $wgMemoryLimit ) ) {
478 - print "failed.<br /><b>" . htmlspecialchars( $memlimit ) . " seems too low, installation may fail!</b>";
479 - } else {
480 - print "ok.";
481 - }
 475+ print "<li>PHP's <tt>memory_limit</tt> is " . htmlspecialchars( $memlimit ). " bytes. ";
 476+ if( $newlimit >= $wgMemoryLimit ) {
 477+ print "Successfully set limit to " . htmlspecialchars( $newlimit ) . "... ";
 478+ } else {
 479+ print "<b>Failed raising limit, installation may fail.</b>";
482480 }
483481 print "</li>\n";
484482 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r54420Followup to r54416...overlordq02:34, 5 August 2009

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r54356Better patch for parsing PHP's memory limits from OverlordQ.demon11:18, 4 August 2009
r54414Followup to r54406: enable/disable functions were being called in the wrong o...emufarmers00:59, 5 August 2009

Comments

#Comment by OverlordQ (talk | contribs)   02:00, 5 August 2009

Fixme, logic in config still does weird things. IE: Says it raised it when it stayed the same, etc.

Status & tagging log