cleaner.module in Cleaner 5
Same filename and directory in other branches
Allows the admin to set a schedule for clearing caches and other stuff.
File
cleaner.moduleView source
<?php
/**
* @file
* Allows the admin to set a schedule for clearing caches and other stuff.
*/
/**
* Implementation of hook_help().
*/
function cleaner_help($section) {
switch ($section) {
case 'admin/help#cleaner':
$output = '<p>' . t('Cleaner runs when the Cron job tells it to. It will clean the cache tables and other things as configured.') . '</p>';
return $output;
}
}
/**
* Implementation of hook_menu().
*/
function cleaner_menu($may_cache) {
$items = array();
if (!$may_cache) {
$items[] = array(
'path' => 'admin/settings/cleaner',
'title' => t('Cleaner module settings'),
'description' => t('Configure time-based clean up functions.'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'cleaner_settings_form',
),
'access' => user_access('administer site configuration'),
);
}
return $items;
}
/**
* This module can be hooked into to extend its functionality.
* I'm not sure why you would want to do this rather than code your own, but it's there.
*
* hook_cleaner_settings()
* Returns the useful part of a settings form as a keyed array, with the key being the module name.
* @see cleaner_cleaner_settings().
*
* hook_cleaner_run()
* Called when Cron triggers the site (run-time).
* There are no calling parameters or returns.
*/
/**
* Define a form to control the settings.
*/
function cleaner_settings_form() {
// Pull in our form's style sheet.
drupal_add_css(drupal_get_path('module', 'cleaner') . '/cleaner.css');
$form = array();
$ret = module_invoke_all('cleaner_settings');
foreach ($ret as $module => $elements) {
$mod = drupal_ucfirst($module);
$form[$mod] = array(
'#type' => 'fieldset',
'#title' => $mod,
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form[$mod]['stuff'] = $elements;
}
return system_settings_form($form);
}
/**
* Implementation of hook_cleaner_settings().
*/
function cleaner_cleaner_settings() {
drupal_add_css(drupal_get_path('module', 'cleaner') . '/cleaner.css');
$form = $ret = array();
$yesno = array(
t('No'),
t('Yes'),
);
// Time intervals.
$min = 60;
$hour = 3600;
$day = 86400;
$interval = array(
0 => t('Every time'),
);
$interval += drupal_map_assoc(array(
15 * $min,
30 * $min,
$hour,
2 * $hour,
4 * $hour,
6 * $hour,
12 * $hour,
$day,
2 * $day,
3 * $day,
7 * $day,
), 'format_interval');
$form['cleaner_cron'] = array(
'#type' => 'radios',
'#title' => t('Run interval'),
'#options' => $interval,
'#default_value' => variable_get('cleaner_cron', $hour),
'#description' => t('This is how often the options below will occur. The actions will occur on the next Cron run after this interval expires. "Every time" means on every Cron run.'),
);
$list = array();
$result = db_query("SHOW TABLES LIKE 'cache\\_%'");
while ($table = db_result($result)) {
$list[] = $table;
}
$form['cleaner_clear_cache'] = array(
'#type' => 'radios',
'#options' => $yesno,
'#title' => t('Clear cache'),
'#default_value' => variable_get('cleaner_clear_cache', FALSE),
'#description' => t('The current cache tables are: @list.', array(
'@list' => implode(', ', $list),
)),
);
$form['cleaner_empty_watchdog'] = array(
'#type' => 'radios',
'#options' => $yesno,
'#title' => t('Empty Watchdog'),
'#default_value' => variable_get('cleaner_empty_watchdog', FALSE),
'#description' => t('There is a standard setting for controlling Watchdog contents. This is more useful for test sites.'),
);
$lifetime = session_get_cookie_params();
$lifetime = $lifetime['lifetime'];
$start = (isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time()) - $lifetime;
$count = db_result(db_query("SELECT COUNT(sid) FROM {sessions} WHERE timestamp < %d", $start));
$form['cleaner_clean_sessions'] = array(
'#type' => 'radios',
'#options' => $yesno,
'#title' => t('Clean up Sessions table'),
'#default_value' => variable_get('cleaner_clean_sessions', FALSE),
'#description' => t('The sessions table can quickly become full with old, abandoned sessions. This will delete all sessions older than @interval (as set by your site administrator). There are currently @count such sessions.', array(
'@interval' => format_interval($lifetime),
'@count' => $count,
)),
);
$ret['cleaner'] = $form;
return $ret;
}
/**
* Implementation of hook_cron().
*/
function cleaner_cron() {
$ret = module_invoke_all('cleaner_run');
}
/**
* Implementation of hook_cleaner_run().
*/
function cleaner_cleaner_run() {
if (variable_get('cleaner_clear_cache', FALSE)) {
// Clear out the cache tables.
$list = array();
$result = db_query("SHOW TABLES LIKE 'cache\\_%'");
while ($table = db_result($result)) {
// Note that use of '*' means the entire table is cleared, not just expired entries.
cache_clear_all('*', $table, TRUE);
$list[] = drupal_substr($table, 6);
}
watchdog('Cleaner', t('Cleared caches. (@list)', array(
'@list' => implode(', ', $list),
)));
}
if (variable_get('cleaner_empty_watchdog', FALSE)) {
// Clear out the watchdog table.
db_query("TRUNCATE {watchdog}");
watchdog('Cleaner', t('Cleared watchdog.'));
}
if (variable_get('cleaner_clean_sessions', FALSE)) {
// Delete sessions that are more than two weeks old.
db_query("DELETE FROM {sessions} WHERE timestamp < %d", $_SESSION['time'] - 14 * 24 * 60 * 60);
$count = db_affected_rows();
watchdog('Cleaner', t('Cleared @count sessions.', array(
'@count' => $count,
)));
}
}
Functions
Name | Description |
---|---|
cleaner_cleaner_run | Implementation of hook_cleaner_run(). |
cleaner_cleaner_settings | Implementation of hook_cleaner_settings(). |
cleaner_cron | Implementation of hook_cron(). |
cleaner_help | Implementation of hook_help(). |
cleaner_menu | Implementation of hook_menu(). |
cleaner_settings_form | Define a form to control the settings. |