You are here

function cleaner_cleaner_run in Cleaner 6

Same name and namespace in other branches
  1. 8 cleaner.module \cleaner_cleaner_run()
  2. 5 cleaner.module \cleaner_cleaner_run()
  3. 7 cleaner.module \cleaner_cleaner_run()

Implementation of hook_cleaner_run().

File

./cleaner.module, line 223
Allows the admin to set a schedule for clearing caches and other stuff.

Code

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', 'Cleared caches. (@list)', array(
      '@list' => implode(', ', $list),
    ), WATCHDOG_NOTICE, url('admin/settings/cleaner'));
  }
  if (variable_get('cleaner_empty_watchdog', FALSE)) {

    // Clear out the watchdog table.
    db_query("TRUNCATE {watchdog}");
    watchdog('Cleaner', 'Cleared watchdog.', NULL, WATCHDOG_NOTICE, url('admin/settings/cleaner'));
  }
  if (variable_get('cleaner_clean_sessions', 0)) {

    // Delete sessions that are older than the lifetime.
    $cookie = session_get_cookie_params();
    $lifetime = $cookie['lifetime'];
    $start = (isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time()) - $lifetime;
    db_query("DELETE FROM {sessions} WHERE timestamp < %d", $start);
    $count = db_affected_rows();
    if ($count) {
      watchdog('Cleaner', 'Cleared @count sessions.', array(
        '@count' => $count,
      ), WATCHDOG_NOTICE, url('admin/settings/cleaner'));
    }
  }
  if (variable_get('cleaner_clean_cssdir', FALSE)) {

    // Delete css files older than 1 hour.
    cleaner_delete_files('css');
  }
  if (variable_get('cleaner_clean_jsdir', FALSE)) {

    // Delete js files older than 1 hour.
    cleaner_delete_files('js');
  }
  if ($opt = variable_get('cleaner_optimize_db', FALSE)) {

    // Optimize tables.
    cleaner_optimize_table($opt);
  }
}