View source
<?php
define('GOOGLE_APPLIANCE_NAME_DEFAULT', 'Google Appliance');
define('GOOGLE_APPLIANCE_RESULTS_PER_PAGE_DEFAULT', 10);
define('GOOGLE_APPLIANCE_MAX_RESULTS_FOR_MINI', 1000);
define('GOOGLE_APPLIANCE_TYPE_GSA', 'Google Search Appliance');
define('GOOGLE_APPLIANCE_TYPE_MINI', 'Google Mini');
define('GOOGLE_APPLIANCE_TYPE', variable_get('google_appliance_type', GOOGLE_APPLIANCE_TYPE_MINI));
function google_appliance_init() {
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
theme('google_appliance_add_meta_tags', $node);
}
}
function google_appliance_perm() {
return array(
'search google appliance',
'search any google appliance client/collection',
'administer google appliance search',
);
}
function google_appliance_get_settings($reset = FALSE) {
static $settings;
if ($reset or empty($settings)) {
$google_appliance_name = trim(variable_get('google_appliance_name', GOOGLE_APPLIANCE_NAME_DEFAULT));
$google_appliance_path = trim(variable_get('google_appliance_path', 'google-appliance'));
$default_client = trim(variable_get('google_appliance_default_client', 'default_frontend'));
$default_collection = trim(variable_get('google_appliance_default_collection', 'default_collection'));
$default_tab_enabled = variable_get('google_appliance_default_tab_enabled', TRUE);
$default_search_path = trim(variable_get('google_appliance_default_search_path', 'google-appliance'));
$search_tabs = trim(variable_get('google_appliance_search_tabs', ''));
$tabs_array = array();
if ($search_tabs) {
foreach (preg_split('/[\\n\\r]+/', $search_tabs) as $tab) {
if ($tab = trim($tab)) {
$tabs_array[] = $tab;
}
}
}
$default_tab = implode('|', array(
$google_appliance_name,
$default_search_path,
$default_client,
$default_collection,
));
if ($default_tab_enabled) {
array_unshift($tabs_array, $default_tab);
}
$settings = array(
'google_appliance_name' => $google_appliance_name,
'google_appliance_path' => $google_appliance_path,
'default_client' => $default_client,
'default_collection' => $default_collection,
'search_tabs' => $search_tabs,
'tabs_array' => $tabs_array,
'default_tab' => $default_tab,
'default_tab_enabled' => $default_tab_enabled,
'default_search_path' => $default_search_path,
);
}
return $settings;
}
function google_appliance_menu() {
$settings = google_appliance_get_settings();
$google_appliance_name = $settings['google_appliance_name'];
$default_client = $settings['default_client'];
$default_collection = $settings['default_collection'];
$tabs_array = $settings['tabs_array'];
$default_tab = $settings['default_tab'];
$default_search_path = $settings['default_search_path'];
$search_module_disabled = !array_key_exists('search', module_list());
$items = array();
$items['admin/settings/google-appliance'] = array(
'title' => t('Google Appliance Settings'),
'description' => t('Configuration for the @name search', array(
'@name' => $google_appliance_name,
)),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'google_appliance_admin_settings',
),
'access arguments' => array(
'administer google appliance search',
),
'type' => MENU_NORMAL_ITEM,
);
if ($search_module_disabled) {
$items['search'] = array(
'title' => t('Search'),
'load arguments' => array(
'%map',
'%index',
),
'page callback' => 'google_appliance_search_view',
'page arguments' => array(
'search/' . $default_search_path,
$default_client,
$default_collection,
NULL,
),
'access arguments' => array(
'search google appliance',
),
'type' => MENU_CALLBACK,
);
}
$items['google-appliance'] = array(
'title' => t($google_appliance_name),
'load arguments' => array(
'%map',
'%index',
),
'page callback' => 'google_appliance_search_view',
'page arguments' => array(
'google-appliance',
$default_client,
$default_collection,
1,
),
'access arguments' => array(
'search google appliance',
),
'type' => MENU_CALLBACK,
);
$items['google-appliance/%google_appliance_client/%google_appliance_collection/%google_appliance_menu_tail'] = array(
'title callback' => 'google_appliance_catch_all_task_title',
'title arguments' => array(
2,
),
'load arguments' => array(
'%map',
'%index',
),
'page callback' => 'google_appliance_search_view',
'page arguments' => array(
'google-appliance',
1,
2,
3,
),
'access callback' => 'google_appliance_catch_all_task_access',
'access arguments' => array(
'search any google appliance client/collection',
1,
2,
),
'type' => MENU_LOCAL_TASK,
);
foreach ($tabs_array as $this_tab) {
$tab = _google_appliance_explode_tab($this_tab);
$is_default = $tab['client'] == $default_client && $tab['collection'] == $default_collection;
$client_arg = google_appliance_client_to_arg($tab['client']);
$collection_arg = google_appliance_collection_to_arg($tab['collection']);
$path_base = 'google-appliance/' . $client_arg . '/' . $collection_arg;
$item = array(
'title' => t($tab['title']),
'load arguments' => array(
'%map',
'%index',
),
'page callback' => 'google_appliance_search_view',
'page arguments' => array(
$path_base,
$tab['client'],
$tab['collection'],
3,
),
'access arguments' => array(
'search google appliance',
),
'type' => MENU_LOCAL_TASK,
'parent' => 'google-appliance',
);
google_appliance_local_task($items, $path_base, 'google-appliance', $item);
$path_base = 'search/' . $tab['path'];
$item = array(
'title' => t($tab['title']),
'load arguments' => array(
'%map',
'%index',
),
'page callback' => 'google_appliance_search_view',
'page arguments' => array(
$path_base,
$tab['client'],
$tab['collection'],
2,
),
'access arguments' => array(
'search google appliance',
),
'type' => MENU_LOCAL_TASK,
);
google_appliance_local_task($items, $path_base, 'search', $item);
}
return $items;
}
function google_appliance_local_task(&$items, $path_base, $parent, $item) {
$items[$path_base . '/%google_appliance_menu_tail'] = $item + array(
'tab_parent' => $parent,
);
$items[$path_base] = $item + array(
'tab_parent' => $path_base . '/%',
);
}
function google_appliance_menu_alter(&$items) {
$settings = google_appliance_get_settings();
$google_appliance_path = $settings['google_appliance_path'];
if (array_key_exists('search/google_appliance/%menu_tail', $items)) {
unset($items['search/google_appliance/%menu_tail']);
}
if ($google_appliance_path != 'google-appliance') {
$fields = array(
'parent',
'tab_parent',
);
foreach ($items as $path => $item) {
if (strpos($path, 'google-appliance') === 0) {
unset($items[$path]);
$change = array();
$change[] =& $path;
foreach ($fields as $field) {
if (isset($item[$field])) {
$change[] =& $item[$field];
}
}
foreach (array_keys($change) as $key) {
$change[$key] = preg_replace('/^google-appliance/', $google_appliance_path, $change[$key]);
}
$items[$path] = $item;
}
}
}
}
function google_appliance_menu_tail_to_arg($arg, $map, $index) {
return menu_tail_to_arg($arg, $map, $index);
}
function google_appliance_menu_tail_load($arg, $map, $index) {
return menu_tail_to_arg($arg, $map, $index);
}
function google_appliance_hyphens_underscores($arg, $replace = array(
'-' => '_',
)) {
if (variable_get('google_appliance_collection_underscores_hyphens', FALSE)) {
$arg = strtr($arg, $replace);
}
return $arg;
}
function google_appliance_client_to_arg($client) {
return google_appliance_hyphens_underscores($client, array(
'_' => '-',
));
}
function google_appliance_collection_to_arg($collection) {
return google_appliance_hyphens_underscores($collection, array(
'_' => '-',
));
}
function google_appliance_client_load($client) {
return google_appliance_hyphens_underscores($client, array(
'-' => '_',
));
}
function google_appliance_collection_load($collection) {
return google_appliance_hyphens_underscores($collection, array(
'-' => '_',
));
}
function google_appliance_catch_all_task_access($permission, $client = NULL, $collection = NULL) {
if (!$client || !$collection) {
return FALSE;
}
$settings = google_appliance_get_settings();
foreach ($settings['tabs_array'] as $this_tab) {
$tab = _google_appliance_explode_tab($this_tab);
if ($tab['client'] == $client && $tab['collection'] == $collection) {
return FALSE;
}
}
return user_access($permission);
}
function google_appliance_catch_all_task_title($collection) {
return ucfirst(strtolower(preg_replace('/[\\W_]+/', ' ', $collection)));
}
function _google_appliance_explode_tab($tab) {
list($title, $path, $client, $collection) = explode("|", $tab);
return array(
'title' => $title,
'path' => $path,
'client' => $client,
'collection' => $collection,
);
}
function google_appliance_theme() {
$registry = array(
'google_appliance_add_meta_tags' => array(
'arguments' => array(
'results' => array(),
),
),
'google_appliance_search_result_array' => array(
'arguments' => array(
'result' => array(),
),
),
'google_appliance_search_view' => array(
'arguments' => array(
'keys' => array(),
'collection' => array(),
),
),
'google_appliance_search_form' => array(
'arguments' => array(
'form' => array(),
),
),
'google_appliance_keymatches' => array(
'arguments' => array(
'keymatches' => NULL,
),
),
'google_appliance_synonyms' => array(
'arguments' => array(
'synonyms' => NULL,
),
),
'google_appliance_cached_link' => array(
'arguments' => array(
'link' => NULL,
'cid' => NULL,
),
),
'google_appliance_theme_form' => array(
'arguments' => array(
'form' => NULL,
),
'template' => 'google-appliance-theme-form',
),
);
$search_module_disabled = !array_key_exists('search', module_list());
if ($search_module_disabled) {
$registry += array(
'search_result' => array(
'path' => 'modules/search',
'file' => 'search.pages.inc',
'arguments' => array(
'result' => NULL,
'type' => NULL,
),
'template' => 'search-result',
),
'search_results' => array(
'path' => 'modules/search',
'file' => 'search.pages.inc',
'arguments' => array(
'results' => NULL,
'type' => NULL,
),
'template' => 'search-results',
),
);
}
return $registry;
}
function google_appliance_theme_registry_alter(&$theme_registry) {
$search_module_disabled = !array_key_exists('search', module_list());
if ($search_module_disabled) {
$theme_registry['search_result']['path'] = 'modules/search';
$theme_registry['search_result']['file'] = 'search.pages.inc';
$theme_registry['search_results']['path'] = 'modules/search';
$theme_registry['search_results']['file'] = 'search.pages.inc';
}
}
function google_appliance_preprocess_search_results(&$variables) {
$results = $variables['results'];
if (empty($results[0]['google_appliance'])) {
return;
}
$element = 0;
$limit = variable_get('google_appliance_limit_per_page', GOOGLE_APPLIANCE_RESULTS_PER_PAGE_DEFAULT);
$variables['pager'] = theme('pager', NULL, $limit, $element);
global $pager_total_items;
$variables['total_results'] = $pager_total_items[$element];
$range_from = (int) $results[0]['attributes']['N'];
$range_to = (int) $results[count($results) - 1]['attributes']['N'];
$substitutions = array(
'@from' => $range_from,
'@to' => $range_to,
'@total' => $pager_total_items[$element],
);
if ($range_from != $range_to) {
$range = t("Showing results @from to @to of @total", $substitutions);
}
else {
$range = t("Showing result @from of @total", $substitutions);
}
$variables['range_from'] = $range_from;
$variables['range_to'] = $range_to;
$variables['range'] = $range;
if (empty($variables['search_results'])) {
$variables['search_results'] = '';
foreach ($results as $result) {
$variables['search_results'] .= theme('search_result', $result, $variables['type']);
}
}
$variables['template_files'][] = 'search-results-google-appliance';
}
function google_appliance_preprocess_search_result(&$variables) {
static $mime_types;
$result = $variables['result'];
if (empty($result['google_appliance'])) {
return;
}
$variables['attributes'] = $result['attributes'];
if (!isset($mime_types)) {
$mime_types = google_appliance_mime_types();
}
if (!empty($result['attributes']['MIME'])) {
if (!empty($mime_types[$result['attributes']['MIME']])) {
$variables['mime'] = $mime_types[$result['attributes']['MIME']];
}
}
$variables['meta_data'] = $result['meta_data'];
$variables['template_files'][] = 'search-result-google-appliance';
}
function google_appliance_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$google_appliance_name = variable_get('google_appliance_name', GOOGLE_APPLIANCE_NAME_DEFAULT);
return array(
'google_search' => array(
'info' => t('@name Search', array(
'@name' => $google_appliance_name,
)),
'title' => t('Search'),
),
'recommended_links' => array(
'info' => t('Recommended Links'),
'title' => t('Recommended Links'),
'pages' => '*search/google_appliance*',
'visibility' => 1,
),
);
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;
case 'google_search':
$block['content'] = drupal_get_form('google_appliance_search_form', "");
$block['subject'] = t('Search');
return $block;
}
return $block;
break;
}
}
function google_appliance_admin_settings() {
$settings = google_appliance_get_settings();
$google_appliance_name = $settings['google_appliance_name'];
$google_appliance_path = $settings['google_appliance_path'];
$default_client = $settings['default_client'];
$default_collection = $settings['default_collection'];
$search_tabs = $settings['search_tabs'];
$default_tab_enabled = $settings['default_tab_enabled'];
$default_search_path = $settings['default_search_path'];
$form = array();
$introduction = <<<END_INTRO
<p>This module provides two ways of accessing search results
from your Google Appliance:</p>
<ol>
<li>search/<i>tab path</i>/<i>search terms...</i></li>
<li>google-appliance/<i>client</i>/<i>collection</i>/<i>search terms...</i></li>
</ol>
<p>The former integrates with the standard search module
if it is enabled, and replaces it otherwise. It provides a
search tab for the default client and collection, and also
for any additional 'Search tabs' defined.</p>
<p>The latter provides the same set of tabs, but will also
work with any valid client and collection combination, even
if a search tab has not been defined (provided that permission
to do this has been assigned).</p>
<p>All search result themeing is done via the standard search
module themes (even when the search module is disabled).</p>
<p>See the README.txt file for more information.</p>
END_INTRO;
$form["introduction"] = array(
"#type" => "markup",
"#value" => t($introduction),
"#weight" => -2,
);
$form["config_init"] = array(
"#title" => t("Initial Configuration"),
"#type" => "fieldset",
"#weight" => -1,
);
$form["config_init"]["google_appliance_name"] = array(
"#type" => "textfield",
"#size" => 30,
"#title" => t("Search Name"),
"#description" => t('The name of this search, used as the default tab name, and the title of the "google-appliance/*" search pages.'),
"#default_value" => $google_appliance_name,
"#required" => TRUE,
"#weight" => -10,
);
$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.<br />You do <b>not</b> need to include "/search" at the end, or a trailing slash, but you should include a port number if needed.<br/> Example: http://mygooglebox.com'),
"#default_value" => variable_get('google_appliance_host_name', ''),
"#required" => TRUE,
"#weight" => -9,
);
$form["config_init"]["google_appliance_default_collection"] = array(
"#type" => "textfield",
"#size" => 20,
"#title" => t("Default collection"),
"#description" => t('The name of the collection of indexed content to search.'),
"#default_value" => $default_collection,
"#required" => TRUE,
"#weight" => -8,
);
$form["config_init"]["google_appliance_default_client"] = array(
"#type" => "textfield",
"#size" => 20,
"#title" => t("Default client (front end)"),
"#description" => t('The name of a valid front-end, defined when you set up the appliance.'),
"#default_value" => $default_client,
"#required" => TRUE,
"#weight" => -7,
);
$form["config_init"]["google_appliance_default_tab_enabled"] = array(
"#type" => "checkbox",
"#title" => t("Enable the default search tab"),
"#description" => t('Provide a default search tab for the default client and collection.'),
"#default_value" => $default_tab_enabled,
"#weight" => -6,
);
$form["config_init"]["google_appliance_default_search_path"] = array(
"#type" => "textfield",
"#size" => 30,
"#title" => t("Default search path"),
"#description" => t('The default search address will be <strong>search/<em>default_search_path</em></strong><br/>Note that optional search.module tabs supply their own <em>path</em>.'),
"#default_value" => $default_search_path,
"#required" => TRUE,
"#weight" => -5,
);
$form["config_init"]["google_appliance_search_tabs"] = array(
"#type" => "textarea",
"#rows" => 4,
"#title" => t("Search tabs"),
"#description" => t('Optional search tabs. These will appear on both search.module and "google-appliance/*" search pages.<br/>Format: title|path_arg|client|collection<br/>The search.module will use <b>search/<em>path_arg</em></b> for the tab\'s search path.'),
"#default_value" => $search_tabs,
"#weight" => -4,
);
$form["config_init"]["google_appliance_path"] = array(
"#type" => "textfield",
"#size" => 30,
"#title" => t("Base path"),
"#description" => t('The base ("google-appliance") component of search paths of the form: <strong><i>google-appliance/client/collection/search terms</i></strong>'),
"#default_value" => $google_appliance_path,
"#required" => TRUE,
"#weight" => -3,
);
$form["config_init"]["google_appliance_collection_underscores_hyphens"] = array(
"#type" => "checkbox",
"#title" => t("Use hyphens in the URLs, but underscores in the Client/Collection names?"),
"#description" => t('e.g. Search default_client + default_collection when visiting <strong>google-appliance/default-client/default-collection</strong><br/>n.b. Client and Collection names may contain both hyphens and underscores, so you can still achieve the same effect without using this option.'),
"#default_value" => variable_get('google_appliance_collection_underscores_hyphens', FALSE),
"#weight" => -2,
);
$form["config_init"]["google_appliance_cache_timeout"] = array(
"#type" => "textfield",
"#size" => 20,
"#title" => t("Cache Timeout"),
"#description" => t('If you wish to cache the search results to reduce the load on the Google Appliance, enter a timeout value here (in seconds).'),
"#default_value" => variable_get('google_appliance_cache_timeout', ''),
"#weight" => -1,
);
$form["config_init"]["google_appliance_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_appliance_debug', 0),
"#weight" => 0,
);
$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', GOOGLE_APPLIANCE_RESULTS_PER_PAGE_DEFAULT),
"#weight" => 1,
);
$form["config_init"]["google_appliance_type"] = array(
"#type" => "select",
"#title" => t("Type of Appliance"),
"#options" => array(
GOOGLE_APPLIANCE_TYPE_MINI => t('Google Mini'),
GOOGLE_APPLIANCE_TYPE_GSA => t('Google Search Appliance'),
),
"#default_value" => variable_get('google_appliance_type', GOOGLE_APPLIANCE_TYPE_MINI),
"#weight" => 2,
);
$form["config_messages"] = array(
"#title" => t("Error Messages"),
"#type" => "fieldset",
"#collapsible" => TRUE,
"#weight" => 0,
);
$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.'),
"#weight" => -1,
);
$form["config_messages"]["google_appliance_errorcode_2"] = array(
"#title" => t("More than @max results", array(
"@max" => number_format(GOOGLE_APPLIANCE_MAX_RESULTS_FOR_MINI),
)),
"#type" => "textfield",
"#size" => 100,
"#maxlength" => 255,
"#required" => TRUE,
"#description" => t('If there are more than @max results for the search criteria.', array(
"@max" => number_format(GOOGLE_APPLIANCE_MAX_RESULTS_FOR_MINI),
)),
"#default_value" => variable_get('google_appliance_errorcode_2', t('Sorry, our search does not return more than @max records. Please refine your criteria.', array(
"@max" => number_format(GOOGLE_APPLIANCE_MAX_RESULTS_FOR_MINI),
))),
"#weight" => 0,
);
$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', 'Sorry, your search cannot be completed at this time. Please try again later.'),
"#weight" => 1,
);
$form["config_messages"]["google_appliance_errorcode_neg_100"] = array(
"#title" => t("Cannot connect to @name", array(
'@name' => $google_appliance_name,
)),
"#type" => "textfield",
"#size" => 100,
"#maxlength" => 255,
"#required" => TRUE,
"#description" => t('If the search cannot connect to the @name server.', array(
'@name' => $google_appliance_name,
)),
"#default_value" => variable_get('google_appliance_errorcode_neg_100', 'Sorry, the connection to our search engine appears to be down at the moment. Please try again later.'),
"#weight" => 2,
);
$form = system_settings_form($form);
$form['#submit'][] = 'google_appliance_admin_settings_submit';
return $form;
}
function google_appliance_admin_settings_validate($form, &$form_state) {
foreach (element_children($form['config_init']) as $field) {
$value = $form_state['values'][$field];
if ($value != check_plain($value)) {
$field_title = $form['config_init'][$field]['#title'];
form_set_error($field, t('!field must be plain text.', array(
'!field' => $field_title,
)));
}
}
$field = 'google_appliance_cache_timeout';
$field_title = $form['config_init'][$field]['#title'];
$timeout = $form_state['values'][$field];
if (!empty($timeout)) {
if (!is_numeric($timeout) or $timeout < 0 or (double) $timeout !== (double) (int) $timeout) {
form_set_error($field, t('!field must be a positive integer, or else blank.', array(
'!field' => $field_title,
)));
}
else {
$form_state['values'][$field] = (int) $timeout;
}
}
}
function google_appliance_admin_settings_submit($form, &$form_state) {
$fields = array(
'google_appliance_name',
'google_appliance_path',
'google_appliance_default_client',
'google_appliance_default_collection',
'google_appliance_default_tab_enabled',
'google_appliance_default_search_path',
'google_appliance_search_tabs',
);
foreach ($fields as $field) {
if ($form_state['values'][$field] != $form["config_init"][$field]['#default_value']) {
drupal_set_message(t("Rebuilding menus."));
google_appliance_get_settings(TRUE);
menu_rebuild();
break;
}
}
}
function google_appliance_search($op = 'search', $keys = NULL, $options = array(), $collection = '', $client = '') {
switch ($op) {
case 'name':
return t(variable_get('google_appliance_name', GOOGLE_APPLIANCE_NAME_DEFAULT));
case 'search':
$dir = drupal_get_path('module', 'google_appliance');
include_once $dir . '/DrupalGoogleMini.php';
$google_debug = variable_get('google_appliance_debug', 0);
if ($google_debug >= 2) {
$gm = new DrupalGoogleMini(TRUE, 'dpr');
}
elseif ($google_debug == 1) {
$gm = new DrupalGoogleMini(TRUE);
}
else {
$gm = new DrupalGoogleMini(FALSE);
}
$init = _google_appliance_search_initialise($gm, $client, $collection, $keys, $options);
if ($init !== NULL) {
return $init;
}
return _google_appliance_search($gm);
}
}
function _google_appliance_search_initialise(&$gm, $client = NULL, $collection = NULL, $keys = NULL, $options = array()) {
try {
$gm
->setOutputEncoding('utf8');
$gm
->setInputEncoding('utf8');
$gm
->setMetaDataRequested('*');
$google_appliance_host = variable_get('google_appliance_host_name', FALSE);
if (!$google_appliance_host) {
$google_appliance_name = variable_get('google_appliance_name', GOOGLE_APPLIANCE_NAME_DEFAULT);
drupal_set_message(t('No host name has been configured for the search appliance. Please enter it on the !admin_settings_page', array(
'!admin_settings_page' => l($google_appliance_name . ' settings page', 'admin/settings/search/google_appliance'),
)), 'error');
return FALSE;
}
$gm->baseUrl = $google_appliance_host . "/search";
$gm->collection = $collection ? $collection : variable_get('google_appliance_default_collection', '');
$gm
->setQueryPart('client', $client ? $client : variable_get('google_appliance_default_client', ''));
$gm
->setQueryPart('filter', 0);
foreach ($options as $opt_name => $opt_value) {
$gm
->setQueryPart($opt_name, $opt_value);
}
$page = isset($_GET['page']) ? $_GET['page'] : 0;
$limit = variable_get('google_appliance_limit_per_page', GOOGLE_APPLIANCE_RESULTS_PER_PAGE_DEFAULT);
$gm
->setPageAndResultsPerPage($page, $limit);
$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');
}
}
}
function _google_appliance_search(&$gm) {
$results = array();
try {
if ($cache = variable_get('google_appliance_cache_timeout', 0)) {
cache_clear_all(NULL, 'cache_google_appliance');
$gm->cache = TRUE;
}
$result_iterator = $gm
->query();
google_appliance_static_response_cache($result_iterator);
google_appliance_cache_data('keymatches', $result_iterator
->getKeyMatches());
$synonyms = array();
$keys = urldecode($gm
->getQueryPart('q'));
$base_path = str_replace($keys, '', $_GET['q']);
foreach ($result_iterator
->getSynonyms() as $synonym) {
$url = $base_path . $synonym;
$synonyms[$synonym] = $url;
}
google_appliance_cache_data('synonyms', $synonyms);
$total_results = (int) $result_iterator->totalResults;
if (GOOGLE_APPLIANCE_TYPE == GOOGLE_APPLIANCE_TYPE_MINI and $total_results > GOOGLE_APPLIANCE_MAX_RESULTS_FOR_MINI) {
$total_results = GOOGLE_APPLIANCE_MAX_RESULTS_FOR_MINI;
}
$limit = $gm
->getQueryPart('num');
$page = $gm
->getQueryPart('start') / $limit;
google_appliance_configure_pager($total_results, $page, $limit);
foreach ($result_iterator as $key => $result) {
if (is_numeric($key)) {
$result_array = theme('google_appliance_search_result_array', $result, $keys);
$results[] = $result_array;
}
}
} catch (Exception $e) {
if ($e
->getCode() > 0) {
google_appliance_static_response_cache($result_iterator);
}
drupal_set_message($e
->getMessage());
return FALSE;
}
return $results;
}
function &google_appliance_static_response_cache($response = NULL) {
static $_response;
if (!empty($response)) {
$_response = drupal_clone($response);
}
return $_response;
}
function google_appliance_cache_data($type, $data = NULL) {
static $cache = array();
if ($data === NULL) {
return $cache[$type];
}
else {
$cache[$type] = $data;
}
}
function google_appliance_configure_pager($total_results, $page = NULL, $limit = NULL, $element = 0) {
global $pager_page_array, $pager_total, $pager_total_items;
if (is_null($page)) {
$page = isset($_GET['page']) ? $_GET['page'] : '';
}
if (is_null($limit)) {
$limit = variable_get('google_appliance_limit_per_page', GOOGLE_APPLIANCE_RESULTS_PER_PAGE_DEFAULT);
}
$pager_page_array = explode(',', $page);
$pager_total_items[$element] = $total_results;
$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));
}
function google_appliance_search_view($search_base, $client, $collection, $keys = NULL, $title = NULL) {
$form = drupal_get_form('google_appliance_search_form', NULL, $search_base, $client, $collection, $keys);
if (!isset($_POST['form_id'])) {
$search_view = array(
'client' => $client,
'collection' => $collection,
'keys' => $keys,
'search_base' => $search_base,
'form' => $form,
'results' => google_appliance_search('search', $keys, array(), $collection, $client),
'keymatches' => google_appliance_cache_data('keymatches'),
'synonyms' => google_appliance_cache_data('synonyms'),
);
return theme('google_appliance_search_view', $search_view, $title);
}
else {
return $form;
}
}
function theme_google_appliance_search_view($search_view, $title = 'Search results') {
$output = "";
if (!empty($search_view['keys'])) {
$output .= theme('google_appliance_theme_form', $search_view['form']);
if (!is_array($search_view['results'])) {
$output .= $search_view['results'];
return $output;
}
if ($search_view['results']) {
foreach ($search_view['results'] as $key => $result) {
$search_view['results'][$key]['title'] = drupal_html_to_text($result['title']);
}
foreach ($search_view['results'] as $key => $result) {
$search_view['results'][$key]['title'] = drupal_html_to_text($result['title']);
}
$search_results = theme('search_results', $search_view['results'], 'google-appliance');
$results = theme('box', t($title), $search_results);
}
if ($search_view['keymatches']) {
$output .= theme('google_appliance_keymatches', $search_view['keymatches']);
}
if ($search_view['synonyms']) {
$output .= theme('google_appliance_synonyms', $search_view['synonyms']);
}
if ($results) {
$output .= $results;
}
else {
$message = variable_get('google_appliance_errorcode_1', t('Your search yielded no results'));
$search_help = google_appliance_help('search#noresults', drupal_help_arg());
$output .= theme('box', $message, $search_help);
}
}
else {
$output .= $search_view['form'];
}
return $output;
}
function theme_google_appliance_search_form($form) {
return drupal_render_form('google_appliance_search_form', $form);
}
function theme_google_appliance_add_meta_tags($node) {
$meta_data = array();
$vocabs = taxonomy_get_vocabularies();
if (module_exists('nat') && $node->nat) {
$node->taxonomy = array_merge($node->nat, $node->taxonomy);
}
if (!empty($node->taxonomy) && is_array($node->taxonomy)) {
foreach ($node->taxonomy as $term) {
$tagname = 'category-' . strtolower($vocabs[$term->vid]->name);
$meta_data[] = array(
$tagname,
$term->name,
);
}
}
$meta_data[] = array(
'modified',
date('Y-m-d h:i:s', $node->changed),
);
$meta_data[] = array(
'created',
date('Y-m-d h:i:s', $node->created),
);
$meta_data[] = array(
'status',
$node->status,
);
if ($node->language) {
$meta_data[] = array(
'content-language',
$node->language,
);
}
$meta_data[] = array(
'type',
$node->type,
);
$node->uid = empty($node->uid) ? 0 : $node->uid;
$user = user_load(array(
'uid' => 0,
));
$meta_data[] = array(
'author',
empty($user->name) ? 'anonymous' : $user->name,
);
$meta_data = array_merge($meta_data, module_invoke_all('google_appliance_meta_tags', $node));
foreach ($meta_data as $data) {
list($name, $content) = $data;
$content = strip_tags($content);
if ($content !== NULL) {
drupal_set_html_head(t('<meta name="@name" content="!content" />', array(
'@name' => google_appliance_sgml_id_name($name),
'!content' => htmlentities($content, ENT_QUOTES),
)));
}
}
}
function theme_google_appliance_search_result_array($result, $keys) {
$result = $result->result;
$attributes = array();
foreach ($result
->attributes() as $name => $value) {
$attributes[$name] = (string) $value;
}
$meta_data = array();
if (isset($result->MT)) {
foreach ($result->MT as $meta) {
$value = (string) $meta['V'];
$name = (string) $meta['N'];
$names = explode(':', $name, 2);
if (count($names) == 2) {
$meta_data[$names[0]][$names[1]][] = $value;
}
else {
$meta_data[$name][] = $value;
}
}
}
$link = (string) $result->U;
$cid = (string) $result->HAS->C
->attributes()->CID;
return array(
'google_appliance' => TRUE,
'attributes' => $attributes,
'meta_data' => $meta_data,
'link' => $link,
'title' => strip_tags($result->T),
'snippet' => decode_entities((string) $result->S),
'type' => $meta_data['type'][0],
'user' => $meta_data['author'][0],
'date' => strtotime($meta_data['modified'][0]),
'extra' => array(
'cached_link' => theme('google_appliance_cached_link', $link, $cid, $keys),
),
);
}
function theme_google_appliance_keymatches($keymatches) {
$list = array();
foreach ($keymatches as $url => $title) {
$link = l($title, $url);
$list[] = $link . '<div class="url">' . $url . '</div>';
}
if ($list) {
return theme('item_list', $list, NULL, 'ul', array(
'class' => 'google-appliance-keymatches',
));
}
}
function theme_google_appliance_synonyms($synonyms) {
$list = array();
foreach ($synonyms as $synonym => $url) {
$list[] = t("You could also try: !link", array(
'!link' => l($synonym, $url),
));
}
if ($list) {
return theme('item_list', $list, NULL, 'ul', array(
'class' => 'google-appliance-synonyms',
));
}
}
function theme_google_appliance_cached_link($link, $cid, $keys) {
$google_appliance_host_name = variable_get('google_appliance_host_name', '');
$google_appliance_default_client = variable_get('google_appliance_default_client', '');
$keys = '+' . str_replace(' ', '+', $keys);
if (isset($google_appliance_host_name)) {
preg_match('/^\\w+\\:\\/\\/(.+)/', $link, $matches);
if (!empty($matches)) {
$url = 'http://' . $google_appliance_host_name . '/search?q=cache:' . $cid . ':' . $matches[1] . $keys . '&proxystylesheet=' . $google_appliance_default_client;
return l('cached version', $url);
}
}
}
function google_appliance_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'block_admin_configure') {
$module = $form['module']['#value'];
$delta = $form['delta']['#value'];
$var_name = $module . '-' . $delta;
$ga_blocksettings = variable_get('google_appliance_block_settings', array());
$google_appliance_name = variable_get('google_appliance_name', GOOGLE_APPLIANCE_NAME_DEFAULT);
$form['block_settings']['google_appliance'] = array(
'#type' => 'fieldset',
'#title' => t("@name Settings", array(
'@name' => $google_appliance_name,
)),
'#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';
return $form;
}
}
function google_appliance_block_save($form, $form_state) {
$var_name = $form_state['values']['module'] . '-' . $form_state['values']['delta'];
$block_settings = variable_get('google_appliance_block_settings', array());
if (!isset($form_state['values']['google_appliance']['hide'])) {
unset($block_settings[$var_name]);
}
else {
$block_settings[$var_name] = $form_state['values']['google_appliance']['hide'];
}
variable_set('google_appliance_block_settings', $block_settings);
}
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;
}
}
function google_appliance_search_form(&$form_state, $prompt = NULL, $search_base = NULL, $client = NULL, $collection = NULL, $keys = '') {
$settings = google_appliance_get_settings();
if (is_null($prompt)) {
$prompt = t('Enter your keywords');
}
if (!$client || !$collection) {
if (arg(0) == 'google-appliance' && arg(1) && arg(2)) {
$client = $client ? $client : arg(1);
$collection = $collection ? $collection : arg(2);
}
$client = $client ? $client : variable_get('google_appliance_default_client', 'default_frontend');
$collection = $collection ? $collection : variable_get('google_appliance_default_collection', 'default_frontend');
}
if (!$search_base) {
$search_base = 'search/' . $settings['default_search_path'];
}
elseif ($search_base == 'google-appliance') {
if (arg(1) && arg(2)) {
$search_base = $settings['google_appliance_path'] . '/' . arg(1) . '/' . arg(2);
}
else {
$client_arg = google_appliance_client_to_arg($settings['default_client']);
$collection_arg = google_appliance_collection_to_arg($settings['default_collection']);
$search_base = $settings['google_appliance_path'] . '/' . $client_arg . '/' . $collection_arg;
}
}
$form['#google_appliance_search_base'] = $search_base;
$form['#attributes'] = array(
'class' => 'search-form',
);
$form['keys'] = array(
'#type' => 'textfield',
'#title' => $prompt,
'#default_value' => $keys,
'#size' => $prompt ? 40 : 20,
'#maxlength' => 255,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Search'),
);
return $form;
}
function google_appliance_search_form_submit($form, &$form_state) {
$form_state['redirect'] = $form['#google_appliance_search_base'] . '/' . $form_state['values']['keys'];
}
function google_appliance_help($path, $arg) {
switch ($path) {
case 'search#noresults':
return t('<ul>
<li>Check if your spelling is correct.</li>
<li>Remove quotes around phrases to match each word individually: <em>"blue smurf"</em> will match less than <em>blue smurf</em>.</li>
</ul>');
}
}
function google_appliance_sgml_id_name($string) {
$string = strtolower(preg_replace('/[^a-zA-Z0-9_:.-]+/', '-', $string));
if (!ctype_lower($string[0])) {
$string = 'id-' . $string;
}
return $string;
}
function google_appliance_simpletest() {
$dir = drupal_get_path('module', 'google_appliance') . '/tests';
$tests = file_scan_directory($dir, '\\.test$');
return array_keys($tests);
}
function google_appliance_mime_types() {
return array(
'application/pdf' => 'PDF',
'application/msword' => 'Word',
'application/vnd.ms-word.document.macroEnabled.12' => 'Word',
'application/vnd.ms-word.template.macroEnabled.12' => 'Word',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'Word',
'application/vnd.openxmlformats-officedocument.wordprocessingml.template' => 'Word',
'application/vnd.ms-excel' => 'Excel',
'application/vnd.ms-excel.addin.macroEnabled.12' => 'Excel',
'application/vnd.ms-excel.sheet.binary.macroEnabled.12' => 'Excel',
'application/vnd.ms-excel.sheet.macroEnabled.12' => 'Excel',
'application/vnd.ms-excel.template.macroEnabled.12' => 'Excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'Excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.template' => 'Excel',
'application/vnd.ms-powerpoint' => 'Powerpoint',
'application/vnd.ms-powerpoint.addin.macroEnabled.12' => 'Powerpoint',
'application/vnd.ms-powerpoint.presentation.macroEnabled.12' => 'Powerpoint',
'application/vnd.ms-powerpoint.slideshow.macroEnabled.12' => 'Powerpoint',
'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'Powerpoint',
'application/vnd.openxmlformats-officedocument.presentationml.slideshow' => 'Powerpoint',
'application/vnd.openxmlformats-officedocument.presentationml.template' => 'Powerpoint',
);
}