--- xapian.module.orig	2008-06-01 09:33:44.000000000 -0400
+++ xapian.module	2008-06-01 10:54:19.000000000 -0400
@@ -93,6 +93,14 @@
     'access' => user_access('access administration pages'),
     'type' => MENU_NORMAL_ITEM,
   );
+  $items[] = array(
+    'path' => 'admin/settings/xapian/wipe',
+    'title' => t('Rebuild index'),
+    'callback' => 'drupal_get_form',
+    'callback arguments' => array('xapian_reindex_all_confirm'),
+    'access' => user_access('administer search'),
+    'type' => MENU_CALLBACK,
+  );
 
   return $items;
 }
@@ -111,13 +119,13 @@
   // Create a database fieldset
   $form['database'] = array(
     '#type' => 'fieldset',
-    '#title' => t('Database')
+    '#title' => t('Xapian database')
   );
   
   // Database type
   $form['database']['xapian_database_type'] = array(
     '#type' => 'radios',
-    '#title' => t('Database Type'),
+    '#title' => t('Type'),
     '#default_value' => $database_type,
     '#options' => array(t('Local'), t('Remote')),
   );
@@ -131,9 +139,10 @@
   );
   $form['database']['local_database']['xapian_database_path'] = array(
     '#type' => 'textfield',
-    '#title' => t('Path to database'),
+    '#title' => t('Path to Xapian database'),
     '#default_value' => variable_get('xapian_database_path', file_directory_path() .'/xapian_database'),
     '#required' => ($database_type == 0),
+    '#description' => t('Directory where your local Xapian database will be created.  Specify a directory writable by your web server process.'),
   );
 
   // Remote database settings
@@ -148,6 +157,7 @@
     '#title' => t('Database server'),
     '#default_value' => variable_get('xapian_database_hostname', ''),
     '#required' => ($database_type == 1),
+    '#description' => t('IP address or host name of remote server running xapian-tcpsrv.'),
   );
   $form['database']['remote_database']['xapian_database_port'] = array(
     '#type' => 'textfield',
@@ -155,6 +165,40 @@
     '#default_value' => variable_get('xapian_database_port', '6431'),
     '#required' => ($database_type == 1),
     '#validate' => ($database_type == 1) ? array('_xapian_validate_port' => array('xapian_database_port')) : array(),
+    '#description' => t('Remote port that xapian-tcpsrv is listening on.'),
+  );
+
+  // Reindexing settings
+  $form['performance'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Performance')
+  );
+  $form['performance']['xapian_index_immediately'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Index immediately'),
+    '#default_value' => variable_get('xapian_index_immediately', TRUE),
+    '#description' => t('Enable this option to index content immediately as it is created and updated.  Disable this option to delay indexing until cron runs.  Your should disable this option on larger websites.'),
+  );
+  if (variable_get('xapian_index_immediately', TRUE)) {
+    $count_result = db_query("SELECT COUNT(*) FROM {xapian_reindex_queue}");
+    $nodes = db_result($count_result);
+
+    $form['performance']['count'] = array(
+      '#value' => t('<p>There are %count items waiting to be indexed.</p>', array('%count' => $nodes)),
+    );
+    $form['performance']['index'] = array(
+      '#type' => 'submit',
+      '#value' => t('Re-index site'),
+    );
+  }
+  $items = drupal_map_assoc(array(10, 20, 500, 100, 500, 1000, 5000, 10000));
+  $form['performance']['xapian_indexing_throttle'] = array(
+    '#type' => 'select',
+    '#title' => t('Items to index per cron run'),
+    '#description' => t('The maximum number of items that will be indexed in one cron run.  Set this number lower if your cron is timing out or if PHP is running out of memory.'),
+    '#options' => $items,
+    '#default_value' => variable_get('xapian_indexing_throttle', 100),
+    '#validate' => array('_xapian_validate_positive_integer' => array('xapian_indexing_throttle')),
   );
 
   // Display settings
@@ -183,50 +227,15 @@
     ),
   );
 
-  // Reindexing settings
-  $form['performance'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Performance')
-  );
-  $form['performance']['xapian_queue_reindex'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Queue reindexing of changed nodes'),
-    '#default_value' => variable_get('xapian_queue_reindex', FALSE),
-  );
-  if (variable_get('xapian_queue_reindex', FALSE)) {
-    $count_result = db_query("SELECT COUNT(*) FROM {xapian_reindex_queue}");
-    $nodes = db_result($count_result);
-
-    $form['performance']['count'] = array(
-      '#value' => t('<p>There are %count items in the reindex queue.</p>', array('%count' => $nodes)),
-    );
-    $form['performance']['reindex'] = array(
-      '#type' => 'submit',
-      '#value' => t('Add all nodes to queue'),
-    );
-  }
-  $form['performance']['xapian_reindex_cron'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Automatically reindex queued nodes with cron'),
-    '#default_value' => variable_get('xapian_reindex_cron', TRUE),
-  );
-  $form['performance']['xapian_reindex_cron_max'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Nodes per cron run'),
-    '#description' => t('The maximum number of nodes that can be reindexed per cron run'),
-    '#default_value' => variable_get('xapian_reindex_cron_max', 100),
-    '#validate' => array('_xapian_validate_positive_integer' => array('xapian_reindex_cron_max')),
-  );
-
   // Logging options
   $form['diagnostic'] = array(
     '#type' => 'fieldset',
-    '#title' => t('Diagnostics')
+    '#title' => t('Logging')
   );
   $form['diagnostic']['xapian_log_queries'] = array(
     '#type' => 'checkbox',
-    '#title' => t('Query Logging'),
-    '#description' => t('Write query descriptions and time taken to the log'),
+    '#title' => t('Log searches'),
+    '#description' => t('Log search queries and time taken for search to the watchdog log.'),
     '#default_value' => variable_get('xapian_log_queries', FALSE),
   );
   
@@ -240,8 +249,8 @@
  * @param array $form_values
  */
 function xapian_admin_validate($form_id, $form_values) {
-  if (t('Add all nodes to queue') == $form_values['op']) {
-    xapian_reindex_all();
+  if (t('Re-index site') == $form_values['op']) {
+    drupal_goto('admin/settings/xapian/wipe');
   }
 }
 
@@ -655,7 +664,7 @@
  *
  * @param object $node
  */
-function _xapian_queue_reindex($node) {
+function _xapian_queue_index($node) {
   db_query('INSERT INTO {xapian_reindex_queue} (nid) VALUES (%d)', $node->nid);
 }
 
@@ -665,15 +674,13 @@
  * @param object $node
  */
 function xapian_index_node(&$node) {
-  $reindex_type = variable_get('xapian_queue_reindex', FALSE);
-  switch ($reindex_type) {
-    case TRUE:
-      _xapian_queue_reindex($node);
-      break;
-    case FALSE:
-      _xapian_reindex_node($node);
-      // Flush the changes to disk.
-      _xapian_init_database(TRUE, TRUE);
+  if (variable_get('xapian_index_immediately', TRUE) == TRUE) {
+    _xapian_reindex_node($node);
+    // Flush the changes to disk.
+    _xapian_init_database(TRUE, TRUE);
+  }
+  else {
+    _xapian_queue_index($node);
   }
 }
 
@@ -706,7 +713,7 @@
   
   $sql = 'SELECT nid FROM {xapian_reindex_queue} q ORDER BY COALESCE(q.priority, 0) DESC, q.added ASC';
   
-  $result = db_query_range($sql, 0, variable_get('xapian_reindex_cron_max', 100));
+  $result = db_query_range($sql, 0, variable_get('xapian_indexing_throttle', 100));
 
   if (!$result) {
     watchdog('xapian', t('Unable to read reindex queue.'));
@@ -738,23 +745,34 @@
 }
 
 /**
- * Implementation of hook_cron that calls the reindex function
+ * Implementation of hook_cron, index all queued content.
  *
  */
 function xapian_cron() {
-  if (variable_get('xapian_reindex_cron', TRUE)) {
+  // If one or more ids exist in the index queue, index them.
+  $id = db_result(db_query('SELECT id FROM {xapian_reindex_queue} LIMIT 1'));
+  if ($id) {
     xapian_reindex_queued_nodes();
   }
 }
 
 /**
- * Insert the nid of every node into the reindex queue
- *
+ * Confirm recreation of search index.
+ */
+function xapian_reindex_all_confirm() {
+  return confirm_form(array(), t('Are you sure you want to re-index the site?'), 'admin/settings/xapian', t(' The search index is not cleared but systematically updated to reflect the new settings. Searching will continue to work but if you are not indexing immediately new content won\'t be indexed until all existing content has been re-indexed. This action cannot be undone.'), t('Re-index site'), t('Cancel'));
+}
+
+/**
+ * Reindex all site content.
  */
-function xapian_reindex_all() {
-  db_query('TRUNCATE {xapian_reindex_queue}');
-  db_query('INSERT INTO {xapian_reindex_queue} (nid) SELECT nid FROM {node}');
-  drupal_set_message(t('All nodes added to reindex queue'));
+function xapian_reindex_all_confirm_submit($form_id, &$form) {
+  if ($form['confirm']) {
+    db_query('TRUNCATE {xapian_reindex_queue}');
+    db_query('INSERT INTO {xapian_reindex_queue} (nid) SELECT nid FROM {node}');
+    drupal_set_message(t('The search index will be rebuilt.'));
+    return 'admin/settings/xapian';
+  }
 }
 
 /**
