From 73cfe3ab84e1d6655286acd7ae58cee3ee511e78 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Sun, 28 Jun 2009 02:36:46 +0000 Subject: [PATCH] API CHANGE: Added increase_time_limit_to(), which respects safe_mode. API CHANGE: Added no-arg option to increase_memory_limit_to() git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@80241 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/Core.php | 29 ++++++++++++++++++++++++--- core/Requirements.php | 2 +- core/i18nTextCollectorTask.php | 2 +- core/model/DatabaseAdmin.php | 4 +--- email/QueuedEmailDispatchTask.php | 4 +--- filesystem/GD.php | 4 +--- forms/TableListField.php | 6 ++---- tests/MemoryLimitTest.php | 33 ++++++++++++++++++++++++++++--- 8 files changed, 63 insertions(+), 21 deletions(-) diff --git a/core/Core.php b/core/Core.php index 56e264241..d0b6365eb 100755 --- a/core/Core.php +++ b/core/Core.php @@ -293,11 +293,16 @@ function _t($entity, $string = "", $priority = 40, $context = "") { /** * Increase the memory limit to the given level if it's currently too low. - * @param A memory limit string, such as "64M" + * @param A memory limit string, such as "64M". If omitted, unlimited memory will be set. */ -function increase_memory_limit_to($memoryLimit) { +function increase_memory_limit_to($memoryLimit = -1) { + $curLimit = ini_get('memory_limit'); + + // Can't go higher than infinite + if($curLimit == -1) return; + // Increase the memory limit if it's too low - if(translate_memstring($memoryLimit) > translate_memstring(ini_get('memory_limit'))) { + if($memoryLimit == -1 || translate_memstring($memoryLimit) > translate_memstring($curLimit)) { ini_set('memory_limit', $memoryLimit); } } @@ -315,4 +320,22 @@ function translate_memstring($memString) { } } +/** + * Increase the time limit of this script. By default, the time will be unlimited. + * @param $timeLimit The time limit in seconds. If omitted, no time limit will be set. + */ +function increase_time_limit_to($timeLimit = null) { + if(!ini_get('safe_mode')) { + if(!$timeLimit) { + set_time_limit(0); + } else { + $currTimeLimit = ini_get('max_execution_time'); + if($currTimeLimit && $currTimeLimit < $timeLimit) { + set_time_limit($timeLimit); + } + } + } + +} + ?> diff --git a/core/Requirements.php b/core/Requirements.php index e3b749809..84b15d079 100644 --- a/core/Requirements.php +++ b/core/Requirements.php @@ -882,7 +882,7 @@ class Requirements_Backend { if(stripos($file, '.js') && $this->combine_js_with_jsmin) { require_once('thirdparty/jsmin/JSMin.php'); - set_time_limit(0); + increase_time_limit_to(); $fileContent = JSMin::minify($fileContent); } // write a header comment for each file for easier identification and debugging diff --git a/core/i18nTextCollectorTask.php b/core/i18nTextCollectorTask.php index 269282ea5..558150eb9 100644 --- a/core/i18nTextCollectorTask.php +++ b/core/i18nTextCollectorTask.php @@ -25,7 +25,7 @@ class i18nTextCollectorTask extends BuildTask { * @uses DataObject->collectI18nStatics() */ public function run($request) { - set_time_limit(0); + increase_time_limit_to(); $c = new i18nTextCollector(); $restrictModules = ($request->getVar('module')) ? explode(',', $request->getVar('module')) : null; return $c->run($restrictModules); diff --git a/core/model/DatabaseAdmin.php b/core/model/DatabaseAdmin.php index 2ee7c4504..5c4727aef 100644 --- a/core/model/DatabaseAdmin.php +++ b/core/model/DatabaseAdmin.php @@ -83,9 +83,7 @@ class DatabaseAdmin extends Controller { */ function build() { // The default time limit of 30 seconds is normally not enough - if(ini_get("safe_mode") != "1") { - set_time_limit(600); - } + increase_time_limit_to(600); // Get all our classes ManifestBuilder::create_manifest_file(); diff --git a/email/QueuedEmailDispatchTask.php b/email/QueuedEmailDispatchTask.php index 8f7f89dcf..fc43f8303 100644 --- a/email/QueuedEmailDispatchTask.php +++ b/email/QueuedEmailDispatchTask.php @@ -7,9 +7,7 @@ class QueuedEmailDispatchTask extends DailyTask { public function process() { - if(ini_get("safe_mode") != "1") { - set_time_limit(0); - } + increase_time_limit_to(); echo "SENDING QUEUED EMAILS\n"; diff --git a/filesystem/GD.php b/filesystem/GD.php index d71079c92..44027915e 100755 --- a/filesystem/GD.php +++ b/filesystem/GD.php @@ -12,9 +12,7 @@ class GD extends Object { function __construct($filename = null) { // If we're working with image resampling, things could take a while. Bump up the time-limit - if(ini_get("safe_mode") != "1") { - set_time_limit(300); - } + increase_time_limit_to(300); if($filename) { // We use getimagesize instead of extension checking, because sometimes extensions are wrong. diff --git a/forms/TableListField.php b/forms/TableListField.php index ab40980dc..ba84707c1 100755 --- a/forms/TableListField.php +++ b/forms/TableListField.php @@ -995,14 +995,12 @@ JS unset($this->cachedSourceItems); $oldShowPagination = $this->showPagination; $this->showPagination = false; - $oldLimit = ini_get('max_execution_time'); - set_time_limit(0); - + + increase_time_limit_to(); $result = $this->renderWith(array($this->template . '_printable', 'TableListField_printable')); $this->showPagination = $oldShowPagination; - set_time_limit($oldLimit); return $result; } diff --git a/tests/MemoryLimitTest.php b/tests/MemoryLimitTest.php index a8907c29e..c5ebbfb49 100644 --- a/tests/MemoryLimitTest.php +++ b/tests/MemoryLimitTest.php @@ -22,17 +22,44 @@ class MemoryLimitTest extends SapphireTest { increase_memory_limit_to('1G'); $this->assertEquals('1G', ini_get('memory_limit')); + + // No argument means unlimited + increase_memory_limit_to(); + $this->assertEquals(-1, ini_get('memory_limit')); + } + + function testIncreaseTimeLimitTo() { + set_time_limit(6000); + + // It can go up + increase_time_limit_to(7000); + $this->assertEquals(7000, ini_get('max_execution_time')); + + // But not down + increase_time_limit_to(5000); + $this->assertEquals(7000, ini_get('max_execution_time')); + + // 0/nothing means infinity + increase_time_limit_to(); + $this->assertEquals(0, ini_get('max_execution_time')); + + // Can't go down from there + increase_time_limit_to(10000); + $this->assertEquals(0, ini_get('max_execution_time')); + } /////////////////// - private $origLimit; + private $origMemLimit, $origTimeLimit; function setUp() { - $this->origLimit = ini_get('memory_limit'); + $this->origMemLimit = ini_get('memory_limit'); + $this->origTimeLimit = ini_get('max_execution_time'); } function tearDown() { - ini_set('memory_limit', $this->origLimit); + ini_set('memory_limit', $this->origMemLimit); + set_time_limit($this->origTimeLimit); } } \ No newline at end of file