Follow-up r74841:
authorMax Semenik <maxsem@users.mediawiki.org>
Sat, 16 Oct 2010 18:33:45 +0000 (18:33 +0000)
committerMax Semenik <maxsem@users.mediawiki.org>
Sat, 16 Oct 2010 18:33:45 +0000 (18:33 +0000)
* Moved command line parsing up so that the first use of --memory-limit actually works
* Now limit of 0 means that we shouldn't attempt to change it at all

maintenance/Maintenance.php

index 969b5cc..e4164e5 100644 (file)
@@ -351,7 +351,7 @@ abstract class Maintenance {
                $this->addOption( 'conf', 'Location of LocalSettings.php, if not default', false, true );
                $this->addOption( 'wiki', 'For specifying the wiki ID', false, true );
                $this->addOption( 'globals', 'Output globals at the end of processing for debugging' );
-               $this->addOption( 'memory-limit', 'Set a specific memory limit for the script' );
+               $this->addOption( 'memory-limit', 'Set a specific memory limit for the script, -1 for no limit or 0 to avoid changing it' );
                // If we support a DB, show the options
                if ( $this->getDbType() > 0 ) {
                        $this->addOption( 'dbuser', 'The DB user to use for this script', false, true );
@@ -431,9 +431,12 @@ abstract class Maintenance {
                        // command-line mode is on, regardless of PHP version.
                }
 
+               $this->loadParamsAndArgs();
+               $this->maybeHelp();
+
                # Set the memory limit
                # Note we need to set it again later in cache LocalSettings changed it
-               ini_set( 'memory_limit', $this->memoryLimit() );
+               $this->adjustMemoryLimit();
 
                # Set max execution time to 0 (no limit). PHP.net says that
                # "When running PHP from the command line the default setting is 0."
@@ -454,8 +457,6 @@ abstract class Maintenance {
                # Turn off output buffering if it's on
                @ob_end_flush();
 
-               $this->loadParamsAndArgs();
-               $this->maybeHelp();
                $this->validateParamsAndArgs();
        }
 
@@ -471,6 +472,15 @@ abstract class Maintenance {
                return $this->getOption( 'memory-limit', -1 );
        }
 
+       /**
+        * Adjusts PHP's memory limit to better suit our needs, if needed.
+        */
+       protected function adjustMemoryLimit() {
+               if ( $this->memoryLimit() != 0 ) {
+                       ini_set( 'memory_limit', $this->memoryLimit() );
+               }
+       }
+
        /**
         * Clear all params and arguments.
         */
@@ -706,7 +716,7 @@ abstract class Maintenance {
 
                $wgShowSQLErrors = true;
                @set_time_limit( 0 );
-               ini_set( 'memory_limit', $this->memoryLimit() );
+               $this->adjustMemoryLimit();
 
                $wgProfiling = false; // only for Profiler.php mode; avoids OOM errors
        }