'Google Appliance Settings', 'description' => 'Configuration for the Google Appliance search', 'page callback' => 'drupal_get_form', 'page arguments' => array('google_appliance_admin_settings'), 'access arguments' => array('administer search'), 'type' => MENU_NORMAL_ITEM, ); return $items; } /** * Implementation of hook_block(). */ function google_appliance_block($op = 'list', $delta = 0, $edit = array()) { switch ($op) { case 'list': $blocks['recommended_links']['info'] = t("Recommended Links"); $blocks['recommended_links']['title'] = t('Recommended Links'); $blocks['recommended_links']['pages'] = '*search/google_appliance*'; $blocks['recommended_links']['visibility'] = 1; return $blocks; break; case 'view': switch ($delta) { case 'recommended_links': if ($result =& google_appliance_static_response_cache()) { $matches = $result->getKeyMatches(); if (!$matches) { return; } $links = array(); foreach ($matches as $link => $title) { $links[] = l($title, $link); } if (count($links)) { $block['content'] = theme('item_list', $links); } else { return FALSE; } } break; } return $block; break; } } function google_appliance_admin_settings() { $form = array(); // initial required config fields $form['config_init'] = array( '#title' => t("Initial Configuration"), '#type' => 'fieldset', ); $form['config_init']['google_appliance_name'] = array( '#type' => 'textfield', '#size' => 30, '#title' => t("Search Name"), '#description' => t('The name of this search, to appear as sub-navigation on the search page.'), '#default_value' => variable_get('google_appliance_name', 'Google Appliance'), '#required' => TRUE, ); $form['config_init']['google_appliance_host_name'] = array( '#type' => 'textfield', '#size' => 50, '#title' => t("Host Name"), '#description' => t('Your Google Search Appliance host name or IP address (with http:// or https://), which were assigned when the appliance was set up.
You do not need to include "/search" at the end, or a trailing slash, but you should include a port number if needed.
Example: http://mygooglebox.com'), '#default_value' => variable_get('google_appliance_host_name', ''), '#required' => TRUE, ); $form['config_init']['google_appliance_collection'] = array( '#type' => 'textfield', '#size' => 20, '#title' => t("Collection"), '#description' => t('The name of the collection of indexed content to search.'), '#default_value' => variable_get('google_appliance_collection', ''), '#required' => TRUE, ); $form['config_init']['google_appliance_client'] = array( '#type' => 'textfield', '#size' => 20, '#title' => t("Client"), '#description' => t('The name of a valid front-end, defined when you set up the appliance.'), '#default_value' => variable_get('google_appliance_client', ''), '#required' => TRUE, ); $form['config_init']['google_appliance_cache_timeout'] = array( '#type' => 'textfield', '#size' => 20, '#title' => t("Cache Timeout"), '#description' => t('If you wish to use caching of results (to reduce load on mini, enter a timeout here'), '#default_value' => variable_get('google_appliance_cache_timeout', ''), ); $form['config_init']['google_debug'] = array( '#type' => 'textfield', '#size' => 20, '#title' => t("Debug Level"), '#description' => t('1 = watchdog, 2 = dpr(needs devel module), 3 = more dpr\'s'), '#default_value' => variable_get('google_debug', ''), ); $form['config_init']['google_appliance_limit_per_page'] = array( '#type' => 'textfield', '#size' => 5, '#title' => t("Number of results per page"), '#description' => t('If you enter 0, it will return the max allowed by the appliance (100)'), '#default_value' => variable_get('google_appliance_limit_per_page', 10), ); // error message config $form['config_messages'] = array( '#title' => t("Error Messages"), '#type' => 'fieldset', '#collapsible' => TRUE, ); $form['config_messages']['google_appliance_errorcode_1'] = array( '#title' => t("No results found"), '#type' => 'textfield', '#size' => 100, '#maxlength' => 255, '#required' => TRUE, '#description' => t('If there are no results for the search criteria.'), '#default_value' => variable_get('google_appliance_errorcode_1', 'No results were found that matched your criteria. Please try broadening your search.'), ); $form['config_messages']['google_appliance_errorcode_2'] = array( '#title' => t("More than 1,000 results"), '#type' => 'textfield', '#size' => 100, '#maxlength' => 255, '#required' => TRUE, '#description' => t('If there are more than 1,000 results for the search criteria.'), '#default_value' => variable_get('google_appliance_errorcode_2', 'Sorry, but our search does not return more than 1,000 records, please refine your criteria.'), ); $form['config_messages']['google_appliance_errorcode_neg_99'] = array( '#title' => t("Cannot perform search"), '#type' => 'textfield', '#size' => 100, '#maxlength' => 255, '#required' => TRUE, '#description' => t('If the search cannot perform due to a query error.'), '#default_value' => variable_get('google_appliance_errorcode_neg_99', 'We apologize, but your search cannot be completed at this time, please try again later.'), ); $form['config_messages']['google_appliance_errorcode_neg_100'] = array( '#title' => t("Cannot connect to Google Appliance"), '#type' => 'textfield', '#size' => 100, '#maxlength' => 255, '#required' => TRUE, '#description' => t('If the search cannot connect to the Google Appliance server.'), '#default_value' => variable_get('google_appliance_errorcode_neg_100','We apologize, but the connection to our search engine appears to be down at the moment, please try again later.'), ); $form['config_search_block']['google_appliance_control_search'] = array( '#type' => 'checkbox', '#title' => t('Default Search Handler.'), '#description' => t('Allows the GSA to be the default search handler for the search block'), '#default_value' => variable_get('google_appliance_control_search',false), ); return system_settings_form($form); } /** * Implementation of hook_search(). * * @param string $op * Operation - name, reset, search, status * @param string $keys * Keyword string sent to the search * @return * Array of search results (each is an assoc. array) that can be fed to a theme function */ 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 Google Appliance settings page', 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 .= "

" . $user_message . "

"; 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; } } /** * Implementation of hook_theme(). */ function google_appliance_theme() { return array( 'google_appliance_search_result_array' => array( 'arguments' => array( 'result' => array() ) ), ); } function theme_google_appliance_search_result_array($result) { return array( 'link' => $result->U, 'user' => $result->getMetaData('user'), 'type' => $result->getMetaData('type'), 'title' => strip_tags($result->T), 'date' => strtotime($result->getMetaData('date')), 'snippet' => decode_entities((string)$result->S), ); } /** * Implementation of hook_form_alter(). */ function google_appliance_form_alter(&$form, $form_state, $form_id) { switch($form_id) { case 'block_admin_configure': $module = $form['module']['#value']; $delta = $form['delta']['#value']; $var_name = $module . '-' .$delta; $ga_blocksettings = google_appliance_blocksettings_get(); $form['block_settings']['google_appliance'] = array( '#type' => 'fieldset', '#title' => t("Google Appliance Settings"), '#description' => t(''), '#collapsed' => TRUE, '#collapsible' => TRUE, '#tree' => TRUE, '#weight' => -1, ); $form['block_settings']['google_appliance']['hide'] = array( '#type' => 'radios', '#title' => t("Do you want to hide this block from the GSA crawler?"), '#description' => t('Select No if you want this block content to be crawled with the page content.'), '#options' => array(1 => t('Yes'), 0 => t('No')), '#default_value' => isset($ga_blocksettings[$var_name]) ? $ga_blocksettings[$var_name] : 1, '#collapsed' => TRUE, '#collapsible' => TRUE, '#tree' => TRUE, ); $form['#submit'][] = 'google_appliance_block_save'; break; case 'search_block_form': if (variable_get('google_appliance_control_search',false)) { $form['#submit'][] = 'google_appliance_search_block_form_submit'; } break; } } function google_appliance_search_block_form_submit($form, &$form_state) { $form_id = $form['form_id']['#value']; $form_state['redirect'] = 'search/google_appliance/'. trim($form_state['values'][$form_id]); } function google_appliance_block_save(&$form, $form_state) { $var_name = $form_state['values']['module'] . '-' . $form_state['values']['delta']; $block_settings = google_appliance_blocksettings_get(); if (!isset($form_state['values']['google_appliance']['hide'])) { unset($block_settings[$var_name]); } else { $block_settings[$var_name] = $form_state['values']['google_appliance']['hide']; } google_appliance_blocksettings_set($block_settings); } function google_appliance_blocksettings_set($new_val) { variable_set('google_appliance_block_settings', $new_val); } function google_appliance_blocksettings_get() { static $google_appliance; if (empty($google_appliance)) { $google_appliance = variable_get('google_appliance_block_settings', array()); } return $google_appliance; } function google_appliance_block_nogoogle($block) { $gsa_block_settings = google_appliance_blocksettings_get(); $var_name = $block->module . '-' . $block->delta; if (!isset($gsa_block_settings[$var_name]) || $gsa_block_settings[$var_name]) { return TRUE; } } /** * It is important to hold on to the Google Appliance response object for the duration of the * page request so that we can use it for things like the keymatch block * * Stolen from apachesolr module * * */ function &google_appliance_static_response_cache($response = NULL) { static $_response; if (!empty($response)) { $_response = drupal_clone($response); } return $_response; } function google_appliance_simpletest() { $dir = drupal_get_path('module', 'google_appliance'). '/tests'; $tests = file_scan_directory($dir, '\.test$'); return array_keys($tests); }