diff --git a/admin/settings/server.php b/admin/settings/server.php index 417fde19179..c9ca4e72821 100644 --- a/admin/settings/server.php +++ b/admin/settings/server.php @@ -242,6 +242,14 @@ $ADMIN->add('server', new admin_externalpage('phpinfo', get_string('phpinfo'), " // "performance" settingpage $temp = new admin_settingpage('performance', get_string('performance', 'admin')); + +$temp->add(new admin_setting_special_selectsetup('memorylimit', get_string('memorylimit', 'admin'), + get_string('configmemorylimit', 'admin'), '128M', + array( '64M' => '64M', + '128M' => '128M', + '256M' => '256M', + '1024M' => '1024M' + ))); $temp->add(new admin_setting_special_selectsetup('cachetype', get_string('cachetype', 'admin'), get_string('configcachetype', 'admin'), '', array( '' => get_string('none'), diff --git a/backup/lib.php b/backup/lib.php index dd3e742c8b1..4ae77716551 100644 --- a/backup/lib.php +++ b/backup/lib.php @@ -699,9 +699,12 @@ mtrace($debuginfo.'Required function check failed (see backup_required_functions)'); return false; } - @ini_set("max_execution_time","3000"); - raise_memory_limit("192M"); + if (empty($CFG->memorylimit)) { + raise_memory_limit('128M'); + } else { + raise_memory_limit($CFG->memorylimit); + } if (!$backup_unique_code = restore_precheck($destinationcourse,$pathtofile,$errorstr,true)) { mtrace($debuginfo.'Failed restore_precheck (error was '.$errorstr.')'); diff --git a/backup/restore.php b/backup/restore.php index f151df2b03e..b241e013831 100644 --- a/backup/restore.php +++ b/backup/restore.php @@ -147,7 +147,11 @@ //Adjust some php variables to the execution of this script @ini_set("max_execution_time","3000"); - raise_memory_limit("192M"); + if (empty($CFG->memorylimit)) { + raise_memory_limit('128M'); + } else { + raise_memory_limit($CFG->memorylimit); + } //Call the form, depending the step we are diff --git a/backup/try.php b/backup/try.php index 34273d77b7c..7c3d2d9e4d6 100644 --- a/backup/try.php +++ b/backup/try.php @@ -19,7 +19,11 @@ //Adjust some php variables to the execution of this script @ini_set("max_execution_time","3000"); - raise_memory_limit("192M"); + if (empty($CFG->memorylimit)) { + raise_memory_limit('128M'); + } else { + raise_memory_limit($CFG->memorylimit); + } echo "
\n";
 
diff --git a/install.php b/install.php
index a6b37753c52..716097fb905 100644
--- a/install.php
+++ b/install.php
@@ -1082,7 +1082,7 @@ function check_memory_limit() {
     }
 
     /// Otherwise, see if we can change it ourselves
-    @ini_set('memory_limit', '40M');
+    raise_memory_limit('40M');
     return ((int)str_replace('M', '', get_memory_limit()) >= 40);
 }
 
diff --git a/lang/en_utf8/admin.php b/lang/en_utf8/admin.php
index 85e86cb25ea..e0677e4bb5d 100644
--- a/lang/en_utf8/admin.php
+++ b/lang/en_utf8/admin.php
@@ -171,6 +171,7 @@ $string['configmaxeditingtime'] = 'This specifies the amount of time people have
 $string['configmaxevents'] = 'Events to Lookahead';
 $string['configmemcachedhosts'] = 'For memcached. Comma-separated list of hosts that are running the memcached daemon. Use IP addresses to avoid DNS latency. memcached does not behave well if you add/remove hosts on a running setup.';
 $string['configmemcachedpconn'] = 'For memcached. Use persistent connections. Use carefully -- it can make Apache/PHP crash after a restart of the memcached daemon.';
+$string['configmemorylimit'] = 'This sets the maximum amount of memory that a script is allowed to allocate. This option is applied to search indexing, backup/restore and admin/health scripts.';
 $string['configmessaging'] = 'Should the messaging system between site users be enabled?';
 $string['configminpassworddigits'] = 'Passwords must have at least these many digits.';
 $string['configminpasswordlength'] = 'Passwords must be at least these many characters long.';
@@ -506,6 +507,7 @@ $string['mediapluginswfnote'] = 'As a default security measure, normal users sho
 $string['mediapluginwmv'] = 'Enable .wmv filter';
 $string['memcachedhosts'] = 'memcached hosts';
 $string['memcachedpconn'] = 'memcached use persistent connections';
+$string['memorylimit'] = 'PHP memory limit';
 $string['messaging'] = 'Enable messaging system';
 $string['minpasswordlength'] = 'Password Length';
 $string['minpassworddigits'] = 'Digits';
diff --git a/lib/setuplib.php b/lib/setuplib.php
index 618e6fe1caa..f83f33c8970 100644
--- a/lib/setuplib.php
+++ b/lib/setuplib.php
@@ -83,6 +83,42 @@ function raise_memory_limit ($newlimit) {
     return false;
 }
 
+/**
+ * Function to reduce the memory limit to a new value.
+ * Will respect the memory limit if it is lower, thus allowing
+ * settings in php.ini, apache conf or command line switches
+ * to override it
+ *
+ * The memory limit should be expressed with a string (eg:'64M')
+ *
+ * @param string $newlimit the new memory limit
+ * @return bool
+ */
+function reduce_memory_limit ($newlimit) {
+    if (empty($newlimit)) {
+        return false;
+    }
+    $cur = @ini_get('memory_limit');
+    if (empty($cur)) {
+        // if php is compiled without --enable-memory-limits
+        // apparently memory_limit is set to ''
+        $cur=0;
+    } else {
+        if ($cur == -1){
+            return true; // unlimited mem!
+        }
+        $cur = get_real_size($cur);
+    }
+
+    $new = get_real_size($newlimit);
+    // -1 is smaller, but it means unlimited
+    if ($new < $cur && $new != -1) {
+        ini_set('memory_limit', $newlimit);
+        return true;
+    }
+    return false;
+}
+
 /**
  * Converts numbers like 10M into bytes.
  *
diff --git a/search/cron_php5.php b/search/cron_php5.php
index 4a0e9f9f2c9..9c3f939c705 100644
--- a/search/cron_php5.php
+++ b/search/cron_php5.php
@@ -7,10 +7,12 @@
 
 try{
     // overrides php limits
-    $maxtimelimit = ini_get('max_execution_time');
     ini_set('max_execution_time', 300);
-    $maxmemoryamount = ini_get('memory_limit');
-    ini_set('memory_limit', '48M');
+    if (empty($CFG->memorylimit)) {
+        raise_memory_limit('128M');
+    } else {
+        raise_memory_limit($CFG->memorylimit);
+    }
 
     mtrace("\n--DELETE----");
     require_once("$CFG->dirroot/search/delete.php");
@@ -21,13 +23,9 @@ try{
     mtrace("------------");
     //mtrace("cron finished.
"); mtrace('done'); - - // set back normal values for php limits - ini_set('max_execution_time', $maxtimelimit); - ini_set('memory_limit', $maxmemoryamount); } catch(Exception $ex){ mtrace('Fatal exception from Lucene subsystem. Search engine may not have been updated.'); mtrace($ex); } -?> \ No newline at end of file +?>