=== modified file 'sites/apartmentlines.com/modules/db_maintenance/db_maintenance.module' --- sites/apartmentlines.com/modules/db_maintenance/db_maintenance.module 2007-01-26 03:24:12 +0000 +++ sites/apartmentlines.com/modules/db_maintenance/db_maintenance.module 2007-01-27 23:58:33 +0000 @@ -62,11 +62,28 @@ function db_maintenance_menu($may_cache) 'access' => user_access('administer site configuration'), 'type' => MENU_NORMAL_ITEM, // optional ); + $items[] = array( + 'path' => 'db_maintenance/optimize', + 'title' => t('Optimize tables'), + 'callback' => 'db_maintenance_optimize_tables_page', + 'access' => user_access('administer site configuration'), + 'type' => MENU_CALLBACK, + ); } return $items; } /** + * Callback page for manually optimizing tables. + * + */ +function db_maintenance_optimize_tables_page() { + db_maintenance_optimize_tables(); + drupal_set_message(t('Tables optimized')); + drupal_goto('admin/settings/db_maintenance'); +} + +/** * Get a list of all the tables in a database. * * @param $db The name of the database connection to query for tables. @@ -87,10 +104,23 @@ function _db_maintenance_list_mysql_tabl } /** - * Perform the maintenance + * Implementation of hook_cron(). * */ function db_maintenance_cron() { + $last_run = variable_get('cron_last', time()); + $interval = time() - variable_get('db_maintenance_cron_frequency', 86400); + // Only run cron if enough time has elapsed. + if ($interval > $last_run) { + db_maintenance_optimize_tables(); + } +} + +/** + * Perform the maintenance. + * + */ +function db_maintenance_optimize_tables() { global $db_url; // Set the databases array if not already set in $db_url. @@ -141,6 +171,19 @@ function db_maintenance_admin_settings() '#default_value' => variable_get('db_maintenance_log', 0), '#description' => t('If enabled, a watchdog entry will be made each time tables are optimized, containing information which tables were involved.') ); + $options = array( + 3600 => t('Hourly'), + 86400 => t('Daily'), + 604800 => t('Weekly'), + 2592000 => t('Monthly'), + ); + $form['db_maintenance_cron_frequency'] = array( + '#type' => 'select', + '#title' => t('Optimize tables'), + '#options' => $options, + '#default_value' => variable_get('db_maintenance_cron_frequency', 86400), + '#description' => t('Select how often database tables should be optimized.') .' '. l(t('Optimize now.'), 'db_maintenance/optimize'), + ); // Set the databases array if not already set in $db_url. if (is_array(($db_url))) { $databases = $db_url; @@ -156,7 +199,7 @@ function db_maintenance_admin_settings() '#title' => t('Tables in the !db database', array('!db' => $db == 'default' ? 'Drupal' : $db)), '#options' => $options, '#default_value' => variable_get('db_maintenance_table_list_'. $db, ''), - '#description' => t('Selected tables will be optimized during cron runs'), + '#description' => t('Selected tables will be optimized during cron runs.'), '#multiple' => true, '#attributes' => array('size' => count($options)), );