function google_appliance_search_view in Google Search Appliance 7
Same name and namespace in other branches
- 6.2 google_appliance.module \google_appliance_search_view()
Top level search execution (menu callback)
- performs a query of the GSA device for a SINGLE PAGE of search results
@arg $query the search query from url [ arg(1) ] @arg $sort optional sort parameter, either "rel" (default) or "date" [ arg(2) ]
Return value
themed results: error messages or results listing
1 string reference to 'google_appliance_search_view'
- google_appliance_menu in ./
google_appliance.module - Implements hook_menu().
File
- ./
google_appliance.module, line 484 - Google Appliance module enables searching via a dedicated Google Search Appliance hardware device. See README.txt and the help page at admin/help/google_appliance.
Code
function google_appliance_search_view($query = '', $sort = NULL) {
$query = urldecode($query);
// get the results page form
$form = drupal_get_form('google_appliance_search_form', $query);
// When POSTing back to the search form on a search results page, the original
// URL is accessed (which re-runs that search) and then the redirect for
// the new search takes place, executing the second (correct) search.
// Prevent hitting the device twice per search by checking if we've POSTed yet.
if (!isset($_POST['form_id']) && $query != '') {
// grab module settings
$settings = _google_appliance_get_settings();
// response processing structures
$search_query_data = array();
// cURL arguments for the query
$response_data = array();
// processd response
// get the sort parameter from the url
$sort_param = '';
if ($sort != NULL && $sort == 'date') {
$sort_param = 'date:D:S:d1';
}
// determine how deep into the results to start viewing (paging)
if (isset($_GET['page'])) {
$results_view_start = check_plain($_GET['page']) * $settings['results_per_page'];
}
else {
$results_view_start = 0;
}
// build cURL request
$search_query_data['gsa_host'] = check_plain($settings['hostname'] . '/search');
$search_query_data['gsa_query_params'] = array(
'site' => check_plain($settings['collection']),
'oe' => 'utf8',
'ie' => 'utf8',
'getfields' => '*',
'client' => check_plain($settings['frontend']),
'start' => $results_view_start,
'num' => check_plain($settings['results_per_page']),
'filter' => check_plain($settings['autofilter']),
'q' => $query,
'output' => 'xml_no_dtd',
'sort' => $sort_param,
'access' => 'p',
);
// Alter request according to language filter settings.
if (module_exists('locale') && $settings['language_filter_toggle']) {
$search_query_data['gsa_query_params']['lr'] = _google_appliance_get_lr($settings['language_filter_options']);
}
// allow implementation of hook_google_appliance_query_alter() by other modules
drupal_alter('google_appliance_query', $search_query_data);
// build debug info in case we need to display it
if ($settings['query_inspection'] == '1') {
$search_query_data['debug_info'][] = t('GSA host: @host', array(
'@host' => $search_query_data['gsa_host'],
));
$search_query_data['debug_info'][] = t('Query Parameters: <pre>@qp</pre>', array(
'@qp' => print_r($search_query_data['gsa_query_params'], TRUE),
));
}
$curl_options = array();
// Use drupal proxy if any.
$drupal_proxy_server = variable_get('proxy_server', '');
$drupal_proxy_port = variable_get('proxy_port', '');
$drupal_proxy_username = variable_get('proxy_username', '');
$drupal_proxy_password = variable_get('proxy_password', '');
$drupal_proxy_user_agent = variable_get('proxy_user_agent', NULL);
// NULL as default value.
$drupal_proxy_exceptions = variable_get('proxy_exceptions', array());
// Add drupal proxy to curl_options.
if ($drupal_proxy_server != '') {
// Parse gsa_hostname to obtain the host without the scheme (http/https).
// Check if a proxy should be used for gsa_hostname using _drupal_http_use_proxy
// If parsing fails (and it should not), use the proxy. The idea here is that we should always use the proxy
// except for some exceptional hosts. If we are not able to confirm that this is indeed an exception
// better to use the proxy.
$gsa_hostname_components = @parse_url($gsa_hostname);
if ($gsa_hostname_components === false || _drupal_http_use_proxy($gsa_hostname_components['host'])) {
$curl_options[CURLOPT_PROXY] = $drupal_proxy_server;
// Add port, if provided.
if ($drupal_proxy_port != '') {
$curl_options[CURLOPT_PROXY] .= ':' . $drupal_proxy_port;
}
// Some proxies reject requests with any User-Agent headers, while others
// require a specific one.
if ($drupal_proxy_user_agent !== NULL) {
$curl_options[CURLOPT_USERAGENT] = $drupal_proxy_user_agent;
}
}
// Set authentication, if needed, checking if proxy_username is not empty.
if ($drupal_proxy_username != '') {
// The option CURLOPT_PROXYUSERPWD has format [username]:[password].
$curl_options[CURLOPT_PROXYUSERPWD] = $drupal_proxy_username . ':' . $drupal_proxy_password;
}
}
// allow implementation of hook_google_appliance_curl_alter() by other modules
drupal_alter('google_appliance_curl', $curl_options);
// query the GSA for search results
$gsa_response = _curl_get($search_query_data['gsa_host'], $search_query_data['gsa_query_params'], $curl_options, $settings['timeout']);
// check for handshake errors
if ($gsa_response['is_error'] == TRUE) {
// cURL returned an error (can't connect)
$response_data['error']['curl_error'] = $gsa_response['response'];
// displaying useful error messages depends upon the use of the array key
// 'curl_error' ... the actual error code/response is displayed elsewhere.
// @see google_appliance.theme.inc
}
else {
// cURL gave us something back -> attempt to parse
$response_data = google_appliance_parse_device_response_xml($gsa_response['response']);
}
// render results
$search_query_data['gsa_query_params']['urlencoded_q'] = urlencode($query);
$template_data = array(
'search_query_data' => $search_query_data,
'response_data' => $response_data,
);
return theme('google_appliance_results', $template_data);
}
else {
// return the form so the (real) second search is performed
return $form;
}
}