You are here

function google_appliance_search in Google Search Appliance 5

Same name and namespace in other branches
  1. 6.2 google_appliance.module \google_appliance_search()

Implementation of hook_search()

Parameters

string $op: Operation - name, reset, search, status

string $keys: Keyword string sent to the search

Return value

Array of search results (each is an assoc. array) that can be fed to a theme function

1 string reference to 'google_appliance_search'
google_appliance_menu in ./google_appliance.module
Implementation of hook_menu()

File

./google_appliance.module, line 283
GSA integration

Code

function google_appliance_search($op = 'search', $keys = null) {
  switch ($op) {
    case 'name':
      return t(variable_get('google_appliance_name', "Google Appliance"));
      break;
    case 'search':
      global $pager_page_array, $pager_total, $pager_total_items;
      $page = isset($_GET['page']) ? $_GET['page'] : '';

      // Convert comma-separated $page to an array, used by other functions.
      $pager_page_array = explode(',', $page);

      // $element indicates which of the pagers active this pager is working from.  $limit indicates how many per page.
      $element = 0;
      $limit = variable_get('google_appliance_limit_per_page', 10);
      $dir = drupal_get_path('module', 'google_appliance');
      include_once $dir . '/DrupalGoogleMini.php';
      $google_debug = variable_get('google_debug', 0);
      if ($google_debug >= 2) {
        $gm = new DrupalGoogleMini(true, 'dpr');
      }
      elseif ($google_debug == 1) {
        $gm = new DrupalGoogleMini(true);
      }
      else {
        $gm = new DrupalGoogleMini(false);
      }

      /**
       * If you have many searches for the same content
       * You can use this setting to keep the GSA from getting hit too often
       *
       */
      if ($cache = variable_get('google_appliance_cache_timeout', 0)) {
        cache_clear_all(null, 'cache_google');
        $gm->cache = true;
      }

      // initialize search object
      try {
        $gm
          ->setOutputEncoding('utf8');
        $gm
          ->setInputEncoding('utf8');
        $gm
          ->setMetaDataRequested('*');

        // get configuration from settings page
        $_tmp_host = variable_get('google_appliance_host_name', false);
        if (!$_tmp_host) {
          drupal_set_message(t('No host name has been configured for the search appliance. Please enter it on the <a href="@admin-url">Google Appliance settings page</a>', array(
            "@admin-url" => url("admin/settings/search/google_appliance"),
          )), 'error');
          return false;
        }
        $gm->baseUrl = $_tmp_host . "/search";
        $gm->collection = variable_get('google_appliance_collection', '');
        $gm
          ->setQueryPart('client', variable_get('google_appliance_client', ''));
        $gm
          ->setPageAndResultsPerPage($page, $limit);

        // set search parameters
        $gm
          ->setKeywords($keys);
        if (module_exists('i18n')) {
          if ($lang = i18n_get_lang()) {
            $gm
              ->setLanguageFilter(array(
              $lang,
            ));
          }
        }
      } catch (GoogleMiniCriteriaException $e) {
        $code = $e
          ->getCode();
        if ($message = variable_get('google_appliance_errorcode_' . $code, '')) {
          $user_message = $message;
        }
        else {
          $user_message = GoogleMiniException::getUserMessage($code);
        }
        $error_message = $e
          ->getMessage();
        if ($code > 0) {
          $output .= "<h2>" . $user_message . "</h2>";
          return $output;
        }
        else {
          watchdog('google_appliance', $error_message);
          drupal_set_message($error_message, 'error');
        }
      }

      // perform the search
      $results = array();
      try {
        $resultIterator = $gm
          ->query();
        google_appliance_static_response_cache($resultIterator);

        // Google never returns more than 1000 results, so we ignore any values larger than that.
        $pager_total_items[$element] = $resultIterator->totalResults < 1000 ? $resultIterator->totalResults : 1000;
        $pager_total[$element] = ceil($pager_total_items[$element] / $limit);
        $pager_page_array[$element] = max(0, min((int) $pager_page_array[$element], (int) $pager_total[$element] - 1));
        foreach ($resultIterator as $key => $result) {
          $results[] = theme('google_appliance_search_result_array', $result);
        }
      } catch (Exception $e) {
        if ($e
          ->getCode() > 0) {
          google_appliance_static_response_cache($resultIterator);
        }
        drupal_set_message($e
          ->getMessage());
        return false;
      }
      return $results;
      break;
    case 'reset':
    case 'status':

      // do nothing
      break;
  }
}