You are here

public function SearchApiViewsQuery::execute in Search API 7

Executes the query and fills the associated view object with according values.

Values to set: $view->result, $view->total_rows, $view->execute_time, $view->pager['current_page'].

Overrides views_plugin_query::execute

File

contrib/search_api_views/includes/query.inc, line 344
Contains SearchApiViewsQuery.

Class

SearchApiViewsQuery
Views query class using a Search API index as the data source.

Code

public function execute(&$view) {
  if ($this->errors || $this->abort) {
    if (error_displayable()) {
      foreach ($this->errors as $msg) {
        drupal_set_message(check_plain($msg), 'error');
      }
    }
    $view->result = array();
    $view->total_rows = 0;
    $view->execute_time = 0;
    return;
  }

  // Calculate the "skip result count" option, if it wasn't already set to
  // FALSE.
  $skip_result_count = $this->query
    ->getOption('skip result count', TRUE);
  if ($skip_result_count) {
    $skip_result_count = !$this->pager || !$this->pager
      ->use_count_query() && empty($view->get_total_rows);
    $this->query
      ->setOption('skip result count', $skip_result_count);
  }
  try {

    // Trigger pager pre_execute().
    if ($this->pager) {
      $this->pager
        ->pre_execute($this->query);
    }

    // Views passes sometimes NULL and sometimes the integer 0 for "All" in a
    // pager. If set to 0 items, a string "0" is passed. Therefore, we unset
    // the limit if an empty value OTHER than a string "0" was passed.
    if (!$this->limit && $this->limit !== '0') {
      $this->limit = NULL;
    }

    // Set the range. (We always set this, as there might even be an offset if
    // all items are shown.)
    $this->query
      ->range($this->offset, $this->limit);
    $start = microtime(TRUE);

    // Execute the search.
    $results = $this->query
      ->execute();
    $this->search_api_results = $results;

    // Store the results.
    if (!$skip_result_count) {
      $this->pager->total_items = $view->total_rows = $results['result count'];
      if (!empty($this->pager->options['offset'])) {
        $this->pager->total_items -= $this->pager->options['offset'];
      }
      $this->pager
        ->update_page_info();
    }
    $view->result = array();
    if (!empty($results['results'])) {
      $this
        ->addResults($results['results'], $view);
    }

    // We shouldn't use $results['performance']['complete'] here, since
    // extracting the results probably takes considerable time as well.
    $view->execute_time = microtime(TRUE) - $start;

    // Trigger pager post_execute().
    if ($this->pager) {
      $this->pager
        ->post_execute($view->result);
    }
  } catch (Exception $e) {
    $this->errors[] = $e
      ->getMessage();

    // Recursion to get the same error behaviour as above.
    $this
      ->execute($view);
  }
}