diff --git a/openoutreach.install b/openoutreach.install index 2314609..e2f122b 100644 --- a/openoutreach.install +++ b/openoutreach.install @@ -4,6 +4,11 @@ * @file * Install function for the openoutreach profile. */ + +/** + * Minimum value of PHP memory_limit for SimpleTest. + */ +define('OPENOUTREACH_MINIMUM_PHP_MEMORY_LIMIT', '128M'); /** * Implements hook_install(). @@ -143,6 +148,57 @@ function openoutreach_install() { } /** + * Implements hook_install(). + */ +function simpletest_requirements($phase) { + $requirements = array(); + $t = get_t(); + + // Check the current memory limit. If it is set too low and many Open + // Outreach apps are enabled, page loads at certain paths may fail and + // throw a fatal error. + $memory_limit = ini_get('memory_limit'); + if (!openoutreach_check_memory_limit(OPENOUTREACH_MINIMUM_PHP_MEMORY_LIMIT, $memory_limit)) { + $requirements['php_memory_limit']['severity'] = REQUIREMENT_WARNING; + $requirements['php_memory_limit']['description'] = $t('The recommended PHP memory limit for running Open Outreach is at least %memory_minimum_limit. The current value is %memory_limit. Follow these steps to increase the PHP memory limit. Alternately, if you are using a commercial host, you may wish to switch to a host that better supports Drupal and Open Outreach. See the Open Outreach hosting listings for some options.', array('%memory_limit' => $memory_limit, '%memory_minimum_limit' => OPENOUTREACH_MINIMUM_PHP_MEMORY_LIMIT, '@limit_url' => 'http://drupal.org/node/207036', '@hosting_url' => 'http://openoutreach.org/hosting')); + } + + return $requirements; +} + +/** + * Compares the memory required for an operation to the available memory. + * + * @todo: use drupal_check_memory_limit() when upgrading to Drupal 8. + * + * @param $required + * The memory required for the operation, expressed as a number of bytes with + * optional SI or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G, 6GiB, 8bytes, + * 9mbytes). + * @param $memory_limit + * (optional) The memory limit for the operation, expressed as a number of + * bytes with optional SI or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G, + * 6GiB, 8bytes, 9mbytes). If no value is passed, the current PHP + * memory_limit will be used. Defaults to NULL. + * + * @return + * TRUE if there is sufficient memory to allow the operation, or FALSE + * otherwise. + */ +function openoutreach_check_memory_limit($required, $memory_limit = NULL) { + if (!isset($memory_limit)) { + $memory_limit = ini_get('memory_limit'); + } + + // There is sufficient memory if: + // - No memory limit is set. + // - The memory limit is set to unlimited (-1). + // - The memory limit is greater than or equal to the memory required for + // the operation. + return ((!$memory_limit) || ($memory_limit == -1) || (parse_size($memory_limit) >= parse_size($required))); +} + +/** * Enable openoutreach_core and apps modules. */ function openoutreach_update_7000() {