View source
<?php
define('GSS_BASE_URL', 'http://www.google.com/search');
class GoogleSiteSearch {
private $key = NULL;
private $language = NULL;
private $query = NULL;
private $extraParams = NULL;
private $pageSize = 20;
private $pagerSize = 19;
private $currentPage = NULL;
private $totalResults = 0;
private $searchResultsXml = NULL;
private $allowedTags = array(
'a',
'em',
'strong',
'cite',
'blockquote',
'code',
'ul',
'ol',
'li',
'dl',
'dt',
'dd',
'b',
);
public function __construct($query, $key = NULL, $pageSize = 20, $extraParams = NULL) {
$this->query = $query;
$this->key = $key;
$this->pageSize = $pageSize;
$this->extraParams = $extraParams;
}
public function SetKey($key) {
$this->key = $key;
}
public function GetKey() {
return $this->key;
}
public function SetLanguage($language) {
$this->language = $language;
}
public function SetPageSize($pageSize) {
$this->pageSize = $pageSize;
}
public function SetPagerSize($pagerSize) {
$this->pagerSize = $pagerSize;
}
public function GetPageSize() {
return $this->pageSize;
}
public function GetPagerSize() {
return $this->pagerSize;
}
public function GetCurrentPage() {
return $this->currentPage;
}
public function GetTotalResults() {
return $this->totalResults;
}
public function GetTotalPages() {
return $this->totalResults / $this->pageSize + 1;
}
public function GetAllowedTags() {
return $this->allowedTags;
}
public function GetQuery() {
return $this->query;
}
public function GetExtraParameters() {
return $this->extraParams;
}
public function GetSearchResults($page = 1) {
$this->currentPage = $page;
$startPos = ($this->currentPage - 1) * $this->pageSize;
if ($this->language) {
$language = "&lr=lang_{$this->language}";
}
else {
$language = NULL;
}
if (count($this->extraParams) > 0) {
$extraParamsQuery = '&' . http_build_query($this->extraParams);
}
else {
$extraParamsQuery = NULL;
}
$url_response = drupal_http_request(url(GSS_BASE_URL, array(
'query' => drupal_query_string_encode(array(
'start' => $startPos,
'num' => $this->pageSize,
$language,
'client' => 'google-csbe',
'output' => 'xml_no_dtd',
'cx' => $this->key,
'q' => $this->query,
$extraParamsQuery,
)),
)));
if ($url_response->error) {
return NULL;
}
$results = simplexml_load_string($url_response->data);
if ($results !== FALSE) {
if (!isset($results->RES->M)) {
return NULL;
}
else {
$this->totalResults = intval(check_plain((string) $results->RES->M));
$categories = array();
foreach ($results->Context->Facet as $facet) {
$categories[] = array(
'label' => check_plain((string) $facet->FacetItem->label),
'anchor_text' => check_plain((string) $facet->FacetItem->anchor_text),
);
}
$this->categories = $categories;
$arr = array();
foreach ($results->RES->R as $result) {
$item = array();
$item['title'] = filter_xss((string) $result->T, $this
->GetAllowedTags());
$item['url'] = check_url((string) $result->UE);
$item['description'] = filter_xss((string) $result->S, $this
->GetAllowedTags());
if (isset($result->PageMap->DataObject)) {
foreach ($result->PageMap->DataObject as $do) {
switch ((string) $do['type']) {
case 'cse_thumbnail':
foreach ($do->Attribute as $att) {
switch ((string) $att['name']) {
case 'src':
$item['thumbnail_url'] = check_url((string) $att['value']);
break;
}
}
break;
}
}
}
$arr[] = $item;
}
return $arr;
}
}
else {
throw new Exception('Could not load search results from Google.');
}
}
public function GetPager($link = '?page=') {
$totalPages = $this
->GetTotalPages();
$currentPage = $this
->GetCurrentPage();
$pagerSize = $this
->GetPagerSize();
$halfPagerSize = $pagerSize / 2;
if ($totalPages < 2) {
return;
}
$html = "<div class=\"item-list google-search-pager\"><ul class=\"pager\">\n";
$totalPages = $totalPages > 50 ? 50 : $totalPages;
if ($currentPage > 1) {
$link_page = $link . ($currentPage - 1);
$html .= "\t<li class=\"pager-previous\"><a href=\"{$link_page}\">" . t('‹ previous') . "</a></li>\n";
}
$pagerStart = $currentPage - (round($pagerSize / 2) - 1);
$pagerFinish = $currentPage + (round($pagerSize / 2) - 1);
if ($pagerStart < 1) {
$pagerStart = 1;
$pagerFinish = $pagerSize > $totalPages ? $totalPages : $pagerSize;
}
$pagerFinish = $pagerFinish > $totalPages ? $totalPages : $pagerFinish;
if ($pagerStart > 1) {
$html .= "\t<li class=\"pager-ellipsis\">...</li>\n";
}
for ($i = $pagerStart; $i <= $pagerFinish; $i++) {
if ($i != $currentPage) {
$link_page = $link . $i;
$html .= "\t<li class=\"pager-item\"><a href=\"{$link_page}\">{$i}</a></li>\n";
}
else {
$html .= "\t<li class=\"pager-current\">{$i}</li>\n";
}
}
if ($pagerFinish < $totalPages) {
$html .= "\t<li class=\"pager-ellipsis\">...</li>\n";
}
if ($currentPage < intval($totalPages)) {
$link_page = $link . ($currentPage + 1);
$html .= "\t<li class=\"pager-next\"><a href=\"{$link_page}\">" . t('next ›') . "</a></li>\n";
}
$html .= "</ul></div>\n";
return $html;
}
public function GetSearchHead() {
$totalResults = $this
->GetTotalResults();
$pageSize = $this
->GetPageSize();
$currentPage = $this
->GetCurrentPage();
$show_start = $pageSize * $currentPage - $pageSize + 1;
$show_end = $pageSize * $currentPage;
if ($show_end > $totalResults) {
$show_end = $totalResults;
}
$html = '<div class="searchhead">';
if (variable_get('gss_labels', TRUE) == 1) {
$html .= '<span>' . t("Show only results of type:") . '</span><ul>';
$active_html_first_element = "";
$active_html_others = "";
if (strpos($this->query, "more:") === FALSE) {
$active_html_first_element = 'class="active"';
$term_from_query = "";
}
else {
$term_from_query = end(explode(":", $this->query));
}
$this->query = current(explode(" more:", $this->query));
$html .= '<li>' . '<a ' . $active_html_first_element . ' href=' . urlencode($this->query) . '>' . t('All results') . '</a>' . '</li>';
if ($totalResults > 0) {
foreach ($this->categories as $categ) {
$active_html_others = "";
if ($categ['label'] == $term_from_query) {
$active_html_others = 'class="active"';
}
$new_query = urlencode($this->query) . '+more%3A' . $categ['label'];
$html .= '<li>' . '<a ' . $active_html_others . ' href=' . $new_query . '>' . $categ['anchor_text'] . '</a>' . '</li>';
}
}
$html .= '</ul>';
}
if ($totalResults !== 0) {
$html .= t('Shows @show_start to @show_end of approximately @totalResults hits', array(
'@show_start' => $show_start,
'@show_end' => $show_end,
'@totalResults' => $totalResults,
));
}
$html .= '</div>';
return $html;
}
}