You are here

function DrupalGoogleMini::query in Google Search Appliance 6.2

Same name and namespace in other branches
  1. 5 DrupalGoogleMini.php \DrupalGoogleMini::query()

Overrides GoogleMini::query

File

./DrupalGoogleMini.php, line 26

Class

DrupalGoogleMini

Code

function query($iteratorClass = 'GoogleMiniResultIterator') {
  if (!db_table_exists('cache_google_appliance')) {
    $this->cache = FALSE;
  }
  if (!$this->cache) {
    return parent::query($iteratorClass);
  }
  else {
    $cached_result_obj = NULL;
    $cache_key = md5($this
      ->buildQuery());
    $_cached_result_xml = cache_get($cache_key, 'cache_google_appliance');
    $cached_result_xml = $_cached_result_xml->data;
    $google_debug = variable_get('google_appliance_debug', 0);
    if ($cached_result_xml) {
      try {
        $google_results = GoogleMini::resultFactory($cached_result_xml, $iteratorClass);
      } catch (Exception $e) {
        drupal_set_message($e
          ->getMessage(), 'error');
        watchdog('google_appliance', $e
          ->getMessage());
        return FALSE;
      }
      if ($google_debug >= 2) {
        if (function_exists('dpr')) {
          dpr("got cache for {$cache_key}");
        }
      }
      elseif ($google_debug == 1) {
        watchdog('google_appliance', "got cache for !cache_key at !url", array(
          '!cache_key' => $cache_key,
          '!url' => $_GET['q'],
        ));
      }
    }
    else {
      try {
        $google_results = parent::query($iteratorClass);
      } catch (Exception $e) {
        drupal_set_message($e
          ->getMessage(), 'error');
        watchdog('google_appliance', $e
          ->getMessage());
        return FALSE;
      }
      $timeout = variable_get('google_appliance_cache_timeout', 600);

      // 10 minutes by default
      cache_set($cache_key, $google_results->payload
        ->asXML(), 'cache_google_appliance', time() + $timeout);
      $google_debug = variable_get('google_debug', 0);
      if ($google_debug >= 2) {
        if (function_exists('dpr')) {
          dpr("setting cache for {$cache_key}");
        }
      }
      elseif ($google_debug == 1) {
        watchdog('google_appliance', "setting cache for !cache_key at !url", array(
          '!cache_key' => $cache_key,
          '!url' => $_GET['q'],
        ));
      }
    }
    return $google_results;
  }
}