Newer
Older
catch
committed
<?php
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* @file
* Install, update and uninstall functions for the memcache module.
*/
/**
* Implements hook_enable().
*/
function memcache_enable() {
$error = FALSE;
$memcache = extension_loaded('memcache');
$memcached = extension_loaded('memcached');
if (!$memcache && !$memcached) {
$error = TRUE;
}
if (!function_exists('dmemcache_object')) {
// dmemcache.inc isn't loaded.
$error = TRUE;
}
else {
// Make a test connection to all configured memcache servers.
$memcache_servers = variable_get('memcache_servers', array('127.0.0.1:11211' => 'default'));
$memcache = dmemcache_instance();
foreach ($memcache_servers as $server => $bin) {
if (dmemcache_connect($memcache, $server, FALSE) === FALSE) {
$error = TRUE;
}
else {
dmemcache_close($memcache);
}
}
}
if ($error) {
drupal_set_message(t('There are problems with your Memcache configuration. Please review %readme and visit the Drupal admin !status page for more information.', array('%readme' => 'README.txt', '!status' => l(t('status report'), 'admin/reports/status'))), 'error');
}
}
catch
committed
/**
* Implements hook_requirements().
*/
function memcache_requirements($phase) {
$requirements = array();
$t = get_t();
$memcache = extension_loaded('memcache');
$memcached = extension_loaded('memcached');
if ($phase == 'install' || $phase == 'runtime') {
Jeremy Andrews
committed
$requirements['memcache_extension']['title'] = $t('Memcache');
catch
committed
if (!$memcache && !$memcached) {
$requirements['memcache_extension']['severity'] = REQUIREMENT_ERROR;
Jeremy Andrews
committed
$requirements['memcache_extension']['value'] = $t('Required PHP extension not found. Install the <a href="http://php.net/manual/en/book.memcache.php">memcache</a> (recommended) or <a href="http://php.net/manual/en/book.memcached.php">memcached</a> extension.');
catch
committed
}
}
if ($phase == 'runtime') {
Jeremy Andrews
committed
$errors = array();
if (!$memcache && !$memcached) {
$errors[] = $t('Required PHP extension not found. Install the <a href="http://php.net/manual/en/book.memcache.php">memcache</a> (recommended) or <a href="http://php.net/manual/en/book.memcached.php">memcached</a> extension.');
}
if (!function_exists('dmemcache_set')) {
// dmemcache.inc isn't loaded.
$errors[] = $t('Failed to load required file %dmemcache.', array('%dmemcache' => drupal_get_path('module', 'memcache') . '/' . 'dmemcache.inc'));
catch
committed
}
Jeremy Andrews
committed
else {
$extension = dmemcache_extension();
if ($extension == 'Memcache') {
$requirements['memcache_extension']['value'] = phpversion('memcache') . _memcache_statistics_link();
}
elseif ($extension == 'Memcached') {
Jeremy Andrews
committed
$requirements['memcache_extension']['value'] = phpversion('memcached') . _memcache_statistics_link();
}
// Make a test connection to all configured memcache servers.
$memcache_servers = variable_get('memcache_servers', array('127.0.0.1:11211' => 'default'));
$memcache = dmemcache_instance();
foreach ($memcache_servers as $server => $bin) {
if (dmemcache_connect($memcache, $server, FALSE) === FALSE) {
$errors[] = $t('Failed to connect to memcached server instance at %server.', array('%server' => $server));
}
else {
dmemcache_close($memcache);
}
}
// Set up a temporary bin to see if we can store a value and then
// successfully retreive it.
try {
Jeremy Andrews
committed
$cid = 'memcache_requirements_test';
Jeremy Andrews
committed
$value = 'OK';
Jeremy Andrews
committed
// Temporarily store a test value in memcache.
cache_set($cid, $value, 'cache', 60);
Jeremy Andrews
committed
// Retreive the test value from memcache.
Jeremy Andrews
committed
$data = cache_get($cid);
Jeremy Andrews
committed
if (!isset($data->data) || $data->data !== $value) {
Jeremy Andrews
committed
$errors[] = $t('Failed to store to then retrieve data from memcache.');
}
else {
// Test a delete as well.
cache_clear_all($cid, 'cache');
Jeremy Andrews
committed
}
}
catch (Exception $e) {
// An unexpected exception occurred.
Jeremy Andrews
committed
$errors[] = $t('Unexpected failure when testing memcache configuration.');
Jeremy Andrews
committed
}
catch
committed
}
Jeremy Andrews
committed
if (empty($errors)) {
$requirements['memcache_extension']['severity'] = REQUIREMENT_OK;
catch
committed
}
else {
Jeremy Andrews
committed
$requirements['memcache_extension']['severity'] = REQUIREMENT_ERROR;
$requirements['memcache_extension']['description'] = $t('There is a problem with your memcache configuration, check the Drupal logs for additional errors. Please review %readme for help resolving the following !issue: !errors', array('%readme' => 'README.txt', '!issue' => format_plural(count($errors), 'issue', 'issues'), '!errors' => '<ul><li>' . implode('<li>', $errors)));
catch
committed
}
}
return $requirements;
}
Jeremy Andrews
committed
/**
* Add "(more information)" link after memcache version if memcache_admin
* module is enabled and user has access to memcache statistics.
*/
function _memcache_statistics_link() {
$t = get_t();
if (module_exists('memcache_admin') && user_access('access memcache statistics')) {
return ' (' . l($t('more information'), 'admin/reports/memcache') . ')';
}
else {
return '';
}
}
/**
* Remove the memcache_widlcard_flushes variable since its structure has changed.
*/
function memcache_update_7000() {
variable_del('memcache_wildcard_flushes');