public function GoogleCSEServices::responseResults in Google Custom Search Engine 8.2
Same name and namespace in other branches
- 8.3 src/GoogleCSEServices.php \Drupal\google_cse\GoogleCSEServices::responseResults()
Function to fetch the results xml from Google.
@TODO $condition is an used variable verify and remove it.
Parameters
string $response: The XML response from Google.
string $keys: The search keys.
string $conditions: Search conditions.
Return value
string Results.
File
- src/
GoogleCSEServices.php, line 261
Class
- GoogleCSEServices
- Additional functions as services for Google CSE.
Namespace
Drupal\google_cseCode
public function responseResults($response, $keys, $conditions) {
$xml = simplexml_load_string($response);
$results = array();
// Number of results.
$total = 0;
if (isset($xml->RES->R)) {
// Cap the result total if necessary.
// Google will not return more than 1000 results, but RES->M may
// be higher than this, which messes up our paging. Retain a copy
// of the original so that themers can still display it.
// Also, any result beyond pages 8x and 99 tends to repeat themselves, so
// they are not relevant. Limited then to 150 pages (1500)
// @TODO Confirm input in UI
$max_results = $this->CSEconfig
->get('configuration')['google_cse_adv_maximum_results'];
$total = (int) $xml->RES->M;
$xml->RES->M_ORIGINAL = $total;
// Is the result accurate?
if (!$this
->isAccurateResult($response)) {
$total = $this
->getAccurateResultsCount($keys, $total);
}
if ($total > $max_results) {
$xml->RES->M = $total = $max_results;
}
foreach ($xml->RES->R as $result) {
// Clean the text and remove tags.
$title = $this
->cleanString((string) $result->T);
if ($result->PageMap) {
$att = $result->PageMap->DataObject
->attributes();
switch ($att['type']) {
case "cse_image":
$image_att = $result->PageMap->DataObject->Attribute
->attributes();
// Clean the text.
$text_snippet = $this
->cleanString((string) $result->S);
// Add a search result image.
$snippet = $this
->thumbnail($title, $image_att) . $text_snippet;
// Clean the text.
$extra = $this
->cleanString((string) $result->U);
$extra = parse_url($extra);
$extra = $extra['host'];
break;
case "metatags":
// Clean the string.
$snippet = $this
->cleanString((string) $result->S);
// Clean the string.
$extra = $this
->cleanString(Html::escape((string) $result->U));
$extra = parse_url($extra);
$extra = $extra['host'] . " | Document";
break;
}
}
else {
if ($result->SL_RESULTS) {
$snippet = strip_tags((string) $result->SL_RESULTS->SL_MAIN->BODY_LINE->BLOCK->T);
}
else {
$snippet = (string) $result->S;
}
// Clean the text.
$snippet = $this
->cleanString($snippet);
// Clean the text.
$extra = $this
->cleanString(Html::escape((string) $result->U));
$extra = parse_url($extra);
$extra = $extra['host'];
}
// Results in a Drupal themed way for search.
$results[] = array(
'link' => (string) $result->U,
'title' => $title,
'snippet' => $snippet,
'keys' => Html::escape($keys),
'extra' => array(
$extra,
),
'date' => NULL,
);
}
// No pager query was executed - we have to set the pager manually.
// @TODO Confirm input in UI.
$limit = $this->CSEconfig
->get('configuration')['google_cse_adv_results_per_page'];
pager_default_initialize($total, $limit);
}
// Allow other modules to alter the number of results.
$this->moduleHandler
->alter('google_cse_num_results', $total);
return $results;
}