View source
<?php
namespace Drupal\google_appliance\Service;
use Drupal;
use Drupal\Component\Utility\Html;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\google_appliance\SearchResults\ResultSet;
use Drupal\google_appliance\SearchResults\SearchQuery;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
class Search implements SearchInterface {
protected $configFactory;
protected $moduleHandler;
protected $httpClient;
protected $parser;
public function __construct(ConfigFactoryInterface $configFactory, ModuleHandlerInterface $moduleHandler, ClientInterface $httpClient, ParserInterface $parser) {
$this->configFactory = $configFactory;
$this->moduleHandler = $moduleHandler;
$this->httpClient = $httpClient;
$this->parser = $parser;
}
public function search(SearchQuery $query) {
$config = $this->configFactory
->get('google_appliance.settings');
$resultsPerPage = (int) $config
->get('display_settings.results_per_page');
$params = [
'site' => Html::escape($config
->get('connection_info.collection')),
'oe' => 'utf8',
'ie' => 'utf8',
'getfields' => '*',
'client' => Html::escape($config
->get('connection_info.frontend')),
'start' => $query
->getPage() * $resultsPerPage,
'num' => Html::escape($config
->get('display_settings.results_per_page')),
'filter' => Html::escape($config
->get('query_param.autofilter')),
'q' => $query
->getSearchQuery(),
'output' => 'xml_no_dtd',
'sort' => $query
->getSort(),
'access' => 'p',
];
$this->moduleHandler
->alter('google_appliance_query', $params);
try {
$response = $this->httpClient
->request('GET', $config
->get('connection_info.hostname') . '/search', [
'query' => $params,
]);
$return = $this->parser
->parseResponse((string) $response
->getBody());
} catch (GuzzleException $e) {
$return = (new ResultSet())
->addError($e
->getMessage(), ResultSet::ERROR_HTTP);
}
$this->moduleHandler
->alter('google_appliance_response', $return);
return $return
->setSearchTitle($config
->get('display_settings.search_title'))
->setQuery($query)
->setResultsPerPage($resultsPerPage);
}
}
function google_appliance_get_clusters() {
$settings = _google_appliance_get_settings();
$query_pos = substr_count($settings['drupal_path'], '/') + 1;
$search_query = urldecode(arg($query_pos));
$cluster_content = NULL;
$clusterQueryURL = Html::escape($settings['hostname'] . '/cluster');
$clusterQueryParams = [
'q' => Html::escape($search_query),
'btnG' => 'Google+Search',
'access' => 'p',
'entqr' => '0',
'ud' => '1',
'sort' => 'date:D:L:d1',
'output' => 'xml_no_dtd',
'oe' => 'utf8',
'ie' => 'utf8',
'site' => Html::escape($settings['collection']),
'client' => Html::escape($settings['frontend']),
];
if (Drupal::moduleHandler()
->moduleExists('locale') && $settings['language_filter_toggle']) {
$clusterQueryParams['lr'] = _google_appliance_get_lr($settings['language_filter_options']);
}
$gsa_clusters_json = _curl_post($clusterQueryURL, $clusterQueryParams);
if (!$gsa_clusters_json['is_error']) {
$clusters = json_decode($gsa_clusters_json['response'], TRUE);
if (isset($clusters['clusters'][0])) {
$cluster_list_items = [];
foreach ($clusters['clusters'][0]['clusters'] as $cluster) {
}
$cluster_list = [
'#theme' => 'item_list',
'#items' => $cluster_list_items,
'#title' => NULL,
'#type' => 'ul',
'#attributes' => [],
];
Drupal::moduleHandler()
->alter('google_appliance_cluster_list', $cluster_list, $cluster_results);
$cluster_content = Drupal::service('renderer')
->render($cluster_list);
}
}
return $cluster_content;
}
function _google_appliance_log_search_error($search_keys = NULL, $error_string = NULL) {
$settings = _google_appliance_get_settings();
$type = 'google_appliance';
$message = 'Search for %keys produced error: %error_string';
$vars = [
'%keys' => isset($search_keys) ? $search_keys : 'nothing (empty search submit)',
'%error_string' => isset($error_string) ? $error_string : 'undefinded error',
];
Drupal::logger($type)
->notice($message, []);
}
function _google_appliance_get_googleonoff() {
return [
'index' => [
'prefix' => '<!--googleoff: index-->',
'suffix' => '<!--googleon: index-->',
],
'anchor' => [
'prefix' => '<!--googleoff: anchor-->',
'suffix' => '<!--googleon: anchor-->',
],
'snippet' => [
'prefix' => '<!--googleoff: snippet-->',
'suffix' => '<!--googleon: snippet-->',
],
'all' => [
'prefix' => '<!--googleoff: all-->',
'suffix' => '<!--googleon: all-->',
],
];
}
function _google_appliance_get_lr($options) {
$langcodes = [];
$options = array_filter($options);
foreach ($options as $option) {
switch ($option) {
case '***CURRENT_LANGUAGE***':
$language = Drupal::languageManager()
->getCurrentLanguage();
$langcode = $language->language;
break;
case '***DEFAULT_LANGUAGE***':
$langcode = language_default('language');
break;
default:
$langcode = $option;
}
$langcodes[$langcode] = "lang_{$langcode}";
}
return implode('|', $langcodes);
}