Index: db_maintenance.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/db_maintenance/db_maintenance.module,v
retrieving revision 1.9.2.1
diff -u -F^f -r1.9.2.1 db_maintenance.module
--- db_maintenance.module	25 Jan 2007 06:56:39 -0000	1.9.2.1
+++ db_maintenance.module	28 Jan 2007 01:51:34 -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('db_maintenance_cron_last', 0);
+  $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.
@@ -122,6 +152,7 @@ function db_maintenance_cron() {
       }
     }
   }
+  variable_set('db_maintenance_cron_last', time());
 }
 
 /**
@@ -141,6 +172,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 +200,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)),
     );
