? SolrPhpClient Index: apachesolr.admin.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr/apachesolr.admin.inc,v retrieving revision 1.1.2.46 diff -u -p -r1.1.2.46 apachesolr.admin.inc --- apachesolr.admin.inc 27 Dec 2009 16:47:37 -0000 1.1.2.46 +++ apachesolr.admin.inc 18 Feb 2010 11:54:42 -0000 @@ -327,6 +327,11 @@ function apachesolr_delete_index_form() '#value' => t('Index controls'), '#suffix' => '', ); + $form['batch'] = array( + '#type' => 'checkbox', + '#title' => t('Reindex immediately'), + '#description' => t('If checked, the index will be rebuilt immediately in this browser session using the batch API. Depending on the size of your index it may take a while. Leave unchecked to reindex on cron runs.'), + ); $form['reindex'] = array( '#type' => 'submit', '#value' => t('Re-index all content'), @@ -364,8 +369,13 @@ function apachesolr_clear_index($form, & * */ function apachesolr_clear_index_submit($form, &$form_state) { + if ($form_state['values']['batch']) { + $form_state['redirect'] = 'admin/settings/apachesolr/index/batch/confirm'; + } + else { $form_state['redirect'] = 'admin/settings/apachesolr/index/clear/confirm'; } +} /** * Confirmation form for "Re-index all content" button @@ -388,6 +398,25 @@ function apachesolr_clear_index_confirm_ /** + * Confirmation form for "Batch re-index all content" button + * @see apachesolr_batch_index_confirm_submit() + */ +function apachesolr_batch_index_confirm() { + $form = array(); + return confirm_form($form, t('Are you sure you want to batch re-index? This will completely delete any existing index, and may take a long time.'), 'admin/settings/apachesolr/index', NULL, t('Batch re-index'), t('Cancel')); +} + +/** + * Submit function for the "Batch re-index all content" confirmation form. + * + * @see apachesolr_clear_index_confirm() + */ +function apachesolr_batch_index_confirm_submit($form, &$form_state) { + $form_state['redirect'] = 'admin/settings/apachesolr/index'; + apachesolr_batch_reindex(); +} + +/** * Submit function for the "Delete the index" button * @see apachesolr_delete_index_form() * @@ -690,3 +719,91 @@ function apachesolr_mlt_delete_block_for $form_state['redirect'] = 'admin/build/block'; } +/** + * Batch reindex functions. + */ + +/** +* Control a batch reindex operation using the batch API. +*/ +function apachesolr_batch_reindex() { + $batch = array( + 'operations' => array( + array('apachesolr_batch_reindex_process', array()), + ), + 'finished' => 'apachesolr_batch_reindex_finished', + 'title' => t('Reindexing'), + 'init_message' => t('Batch reindexing is starting.'), + 'file' => drupal_get_path('module', 'apachesolr') . '/apachesolr.admin.inc', + //'progress_message' => t('Reindexed @current out of @total.'), + 'error_message' => t('Batch reindexing has encountered an error.'), + ); + batch_set($batch); +} + +/** +* Batch Operation Callback +*/ +function apachesolr_batch_reindex_process(&$context) { + if (empty($context['sandbox'])) { + try { + // Get the $solr object + $solr = apachesolr_get_solr(); + // If there is no server available, don't continue. + if (!$solr->ping()) { + throw new Exception(t('No Solr instance available during indexing.')); + } + } + catch (Exception $e) { + watchdog('Apache Solr', $e->getMessage(), NULL, WATCHDOG_ERROR); + return FALSE; + } + + $context['sandbox']['progress'] = 0; + $context['sandbox']['current_node'] = 0; + $context['results'] = array(0); + apachesolr_delete_index(); + } + // We can safely process the apachesolr_cron_limit nodes at a time without a timeout. + $limit = variable_get('apachesolr_cron_limit', 50); + + // Pull the total and remaining variables using apachesolr_search status function. + // This is used to push the progress bar for each group of nodes being indexed. + // Must set progress before calling the apachesolr_index_nodes() function to properly increment + // the progress else progress and total will never equal. + $status = module_invoke('apachesolr_search', 'search', 'status'); + $remaining = $status['remaining']; + $total = $status['total']; + $nodes_indexed = db_result(db_query('SELECT COUNT(nid) FROM {apachesolr_search_node}')); + $context['sandbox']['progress'] += min($limit, $remaining); + $context['results'][0] = $context['sandbox']['progress']; + $number_indexed = min($limit, $remaining); + $context['message'] = t('Reindexing @current of @total.', array('@current' => $context['sandbox']['progress'], '@total' => $total)); + $context['sandbox']['max'] = $total; + + // With each pass through the callback, retrieve the next group of nids. + $result = apachesolr_get_nodes_to_index('apachesolr_search', $limit); + apachesolr_index_nodes($result, 'apachesolr_search'); + + // Inform the batch engine that we are not finished, + // and provide an estimation of the completion level we reached. + if ($context['sandbox']['progress'] != $context['sandbox']['max']) { + $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max']; + } +} + +/** +* Batch 'finished' callback +*/ +function apachesolr_batch_reindex_finished($success, $results, $operations) { + if ($success) { + $message = t('Batch reindexing succesful. '); + $message .= format_plural($results[0], '1 node successfully indexed.', '@count items successfully indexed.'); + } + else { + // An error occurred. + // $operations contains the operations that remained unprocessed. + $message = t('An error occurred while batch reindexing.'); + } + drupal_set_message($message, $success ? 'status' : 'error'); +} \ No newline at end of file Index: apachesolr.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr/apachesolr.module,v retrieving revision 1.1.2.12.2.182 diff -u -p -r1.1.2.12.2.182 apachesolr.module --- apachesolr.module 2 Jan 2010 13:48:14 -0000 1.1.2.12.2.182 +++ apachesolr.module 18 Feb 2010 11:54:43 -0000 @@ -55,6 +55,14 @@ function apachesolr_menu() { 'file' => 'apachesolr.admin.inc', 'type' => MENU_CALLBACK, ); + $items['admin/settings/apachesolr/index/batch/confirm'] = array( + 'title' => 'Confirm the batch re-indexing of all content', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('apachesolr_batch_index_confirm'), + 'access arguments' => array('administer search'), + 'file' => 'apachesolr.admin.inc', + 'type' => MENU_CALLBACK, + ); $items['admin/settings/apachesolr/index/delete/confirm'] = array( 'title' => 'Confirm index deletion', 'page callback' => 'drupal_get_form',