You are here

function _search_api_batch_indexing_callback in Search API 7

Batch API callback for the indexing functionality.

Parameters

SearchApiIndex $index: The index for which items should be indexed.

integer $batch_size: Number of items to index per batch.

integer $limit: Maximum number of items to index.

boolean $drush: Boolean specifying whether this was called from drush or not.

$context: An array (or object implementing ArrayAccess) containing the batch context.

1 string reference to '_search_api_batch_indexing_callback'
_search_api_batch_indexing_create in ./search_api.module
Creates and sets a batch for indexing items.

File

./search_api.module, line 3404
Provides a flexible framework for implementing search services.

Code

function _search_api_batch_indexing_callback(SearchApiIndex $index, $batch_size, $limit, $drush, &$context) {

  // Persistent data among batch runs.
  if (!isset($context['sandbox']['limit'])) {
    $context['sandbox']['limit'] = $limit;
    $context['sandbox']['batch_size'] = $batch_size;
    $context['sandbox']['progress'] = 0;
  }

  // Persistent data for results.
  if (!isset($context['results']['indexed'])) {
    $context['results']['indexed'] = 0;
    $context['results']['not indexed'] = 0;
    $context['results']['drush'] = $drush;
  }

  // Number of items to index for this run.
  $to_index = min($context['sandbox']['limit'] - $context['sandbox']['progress'], $context['sandbox']['batch_size']);

  // Index the items.
  try {
    $indexed = search_api_index_items($index, $to_index);
    $context['results']['indexed'] += $indexed;
  } catch (SearchApiException $e) {
    watchdog_exception('search_api', $e);
    $vars['@message'] = $e
      ->getMessage();
    $context['message'] = t('An error occurred during indexing: @message.', $vars);
    $context['finished'] = 1;
    $context['results']['not indexed'] += $context['sandbox']['limit'] - $context['sandbox']['progress'];
    return;
  }

  // Display progress message.
  if ($indexed > 0) {
    $format_plural = $context['results']['drush'] === TRUE ? '_search_api_drush_format_plural' : 'format_plural';
    $context['message'] = $format_plural($context['results']['indexed'], 'Successfully indexed 1 item.', 'Successfully indexed @count items.');
  }

  // Some items couldn't be indexed.
  if ($indexed !== $to_index) {
    $context['results']['not indexed'] += $to_index - $indexed;
  }
  $context['sandbox']['progress'] += $to_index;

  // Everything has been indexed.
  if ($indexed === 0 || $context['sandbox']['progress'] >= $context['sandbox']['limit']) {
    $context['finished'] = 1;
  }
  else {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['limit'];
  }
}