You are here

cleaner.module in Cleaner 7

Same filename and directory in other branches
  1. 8.2 cleaner.module
  2. 8 cleaner.module
  3. 5 cleaner.module
  4. 6 cleaner.module

Allows the admin to set a schedule for clearing caches and other stuff.

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.

Basic hooks. hook_cleaner_settings(): Returns the useful part of a settings form as a keyed array, with the key being the module name. hook_cleaner_run(): Called when Cron triggers the site (run-time). There are no calling parameters or returns.

File

cleaner.module
View source
<?php

/**
 * @file
 * Allows the admin to set a schedule for clearing caches and other stuff.
 *
 * 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.
 *
 * Basic hooks.
 *  hook_cleaner_settings():
 *   Returns the useful part of a settings form as a keyed array,
 *   with the key being the module name.
 *  hook_cleaner_run():
 *   Called when Cron triggers the site (run-time).
 *   There are no calling parameters or returns.
 */

// Include a Cleaner class file.
require_once drupal_get_path('module', 'cleaner') . '/class.Cleaner.php';
function cleaner_init() {

  // dsm(dw_common_get_coordinates("Kyiv"));
}

/**
 * Implements hook_help().
 */
function cleaner_help($path, $args) {
  if ($path == '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;
  }
}

/**
 * Implements hook_menu().
 */
function cleaner_menu() {
  $items = array();
  $items['admin/config/system/cleaner'] = array(
    'title' => 'Cleaner',
    'description' => 'Configure time-based clean up functions.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'cleaner_settings_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
  );
  return $items;
}

/**
 * Define a form to control the settings.
 */
function cleaner_settings_form() {
  $form = array();
  foreach (module_invoke_all('cleaner_settings') 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);
}

/**
 * Implements hook_cleaner_settings().
 */
function cleaner_cleaner_settings() {

  // Add CSS to the admin settings page.
  $form = array(
    '#attached' => array(
      'css' => array(
        drupal_get_path('module', 'cleaner') . '/cleaner.css',
      ),
    ),
  );
  $yes_no = array(
    t('No'),
    t('Yes'),
  );
  $inline = array(
    'class' => array(
      'container-inline',
    ),
  );
  $interval = array(
    0 => t('Every time'),
  ) + Cleaner::$intervals;
  $form['cleaner_cron'] = array(
    '#type' => 'radios',
    '#title' => t('Run interval'),
    '#options' => $interval,
    '#default_value' => variable_get('cleaner_cron', 0),
    '#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.'),
    '#attributes' => $inline,
  );
  $form['cleaner_clear_cache'] = array(
    '#type' => 'radios',
    '#options' => $yes_no,
    '#title' => t('Clean up cache'),
    '#default_value' => variable_get('cleaner_clear_cache', 0),
    '#description' => Cleaner::cleanerGetCacheTablesTable(),
    '#attributes' => $inline,
  );
  $form['cleaner_additional_tables'] = array(
    '#type' => 'textfield',
    '#title' => t('Additional tables to clear'),
    '#default_value' => variable_get('cleaner_additional_tables', ''),
    '#description' => t('A comma separated list of table names which also needs to be cleared.'),
  );
  if (module_exists('dblog')) {
    $form['cleaner_empty_watchdog'] = array(
      '#type' => 'radios',
      '#options' => $yes_no,
      '#title' => t('Clean up Watchdog'),
      '#default_value' => variable_get('cleaner_empty_watchdog', 0),
      '#description' => t('There is a standard setting for controlling Watchdog contents. This is more useful for test sites.'),
      '#attributes' => $inline,
    );
  }
  else {

    // DBLog module not active, so make sure we don't try to clear it.
    variable_set('cleaner_empty_watchdog', 0);
  }
  $cookie = session_get_cookie_params();
  $select = db_select('sessions', 's')
    ->fields('s', array(
    'timestamp',
  ))
    ->condition('timestamp', REQUEST_TIME - $cookie['lifetime'], '<');
  $count = $select
    ->execute()
    ->rowCount();
  $form['cleaner_clean_sessions'] = array(
    '#type' => 'radios',
    '#options' => $yes_no,
    '#title' => t('Clean up Sessions table'),
    '#default_value' => variable_get('cleaner_clean_sessions', 0),
    '#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($cookie['lifetime']),
      '@count' => $count,
    )),
    '#attributes' => $inline,
  );
  $form['cleaner_clean_cssdir'] = array(
    '#type' => 'radios',
    '#options' => $yes_no,
    '#title' => t('Clean up CSS files'),
    '#default_value' => variable_get('cleaner_clean_cssdir', 0),
    '#description' => t('The CSS directory can become full with stale and outdated cache files.  This will delete all CSS cache files but the latest.'),
    '#attributes' => $inline,
  );
  if (variable_get('preprocess_css', 0) == 0) {
    $form['cleaner_clean_cssdir']['#description'] .= ' <span class="disabled">' . t('This feature is disabled.') . '</span>';
  }
  $form['cleaner_clean_jsdir'] = array(
    '#type' => 'radios',
    '#options' => $yes_no,
    '#title' => t('Clean up JS files'),
    '#default_value' => variable_get('cleaner_clean_jsdir', 0),
    '#description' => t('The JS directory can become full with stale and outdated cache files.  This will delete all JS cache files but the latest.'),
    '#attributes' => $inline,
  );
  if (variable_get('preprocess_js', 0) == 0) {
    $form['cleaner_clean_jsdir']['#description'] .= ' <span class="disabled">' . t('This feature is disabled.') . '</span>';
  }

  // We can only offer OPTIMIZE to MySQL users.
  if (db_driver() == 'mysql') {
    $form['cleaner_optimize_db'] = array(
      '#type' => 'radios',
      '#options' => $yes_no + array(
        '2' => 'Local only',
      ),
      '#title' => t('Optimize tables with "overhead" space'),
      '#default_value' => variable_get('cleaner_optimize_db', 0),
      '#description' => t('The module will compress (optimize) all database tables with unused space. <strong>NOTE</strong>: During an optimization, the table will locked against any other activity; on a high vloume site, this may be undesirable. "Local only" means do not replicate the optimization (if it is being done).'),
      '#attributes' => $inline,
    );
  }
  else {

    // If not MySQL, delete(reset) the variable.
    variable_del('cleaner_optimize_db');
  }
  return array(
    'cleaner' => $form,
  );
}

/**
 * Implements hook_cron().
 */
function cleaner_cron() {
  $interval = variable_get('cleaner_cron', 0);
  $last = variable_get('cleaner_last_cron', 0);
  if (REQUEST_TIME >= $last + $interval) {
    module_invoke_all('cleaner_run');
  }
  variable_set('cleaner_last_cron', REQUEST_TIME);
}

/**
 * Implements hook_cleaner_run().
 *
 * This hook run all actions, which has been set via admin settings page.
 */
function cleaner_cleaner_run() {

  // Watchdog.
  if (variable_get('cleaner_empty_watchdog', 0)) {
    Cleaner::cleanerWatchdogClear();
  }

  // Cache.
  if (variable_get('cleaner_clear_cache', 0)) {
    Cleaner::cleanerCacheClear();
  }

  // Additional tables.
  if (variable_get('cleaner_additional_tables', '')) {
    Cleaner::cleanerAdditionalTablesClear();
  }

  // Sessions.
  if (variable_get('cleaner_clean_sessions', 0)) {
    Cleaner::cleanerSessionsClear();
  }

  // CSS.
  if (variable_get('cleaner_clean_cssdir', 0)) {
    Cleaner::cleanerCssClear();
  }

  // JS.
  if (variable_get('cleaner_clean_jsdir', 0)) {
    Cleaner::cleanerJsClear();
  }

  // MySQL optimizing.
  if ($opt = variable_get('cleaner_optimize_db', 0)) {
    Cleaner::cleanerMysqlOptimizing($opt);
  }
}

Functions

Namesort descending Description
cleaner_cleaner_run Implements hook_cleaner_run().
cleaner_cleaner_settings Implements hook_cleaner_settings().
cleaner_cron Implements hook_cron().
cleaner_help Implements hook_help().
cleaner_init
cleaner_menu Implements hook_menu().
cleaner_settings_form Define a form to control the settings.