# XXX fucking php includes.
include_once('../bluebones.php');
include_once("SOAP/Client.php");
$soapclient = new SOAP_Client('http://api.google.com/search/beta2');
$soapoptions = array('namespace' => 'urn:GoogleSearch', 'trace' => 0);
################################################################################
#
# main
#
# Purpose:
#
# Runs the action.
#
# Revisions:
#
# [bakert@gmail.com 2004-12-24] Code written.
# [bakert@gmail.com 2004-12-26] Comments added.
#
################################################################################
function main() {
$APIKEY = 'Xec0m+da23jiVXkYASXbZuQeYolQazFp';
$q = (isset($_GET["q"]) ? $_GET["q"] : "");
$start = (isset($_GET["start"]) ? intval($_GET["start"]) : 0);
$title = "bluebones.net Search";
if ($q != '') {
$title .= " - $q";
}
write_header($title, $title, "Search Results, bluebones.net, $q");
$search_form = get_search_form($q);
print $search_form;
print "
\n";
print "Search
\n";
if ($q != "") {
# was doing print search_to_html(get_search_results($REQUEST["q"]));
$page_text = search($q, $APIKEY, $start, $num);
if ($num == 0) {
print "No results for $q.
\n";
} else {
print get_results_line($q, $start, $num);
}
print $page_text;
}
print "
\n";
print $search_form;
write_footer();
}
################################################################################
#
# get_search_form
#
# Purpose:
#
# Gets the HTML of the search form.
#
# Parameters:
#
# $query - Text to prepopulate search box with.
#
# Returns:
#
# [bakert@gmail.com 2004-12-24] Code written.
# [bakert@gmail.com 2004-12-26] Comments added.
#
################################################################################
function get_search_form($query) {
$s = "\n";
return $s;
}
################################################################################
#
# do_search
#
# Purpose:
#
# Calls the Google API.
#
# Parameters:
#
# $q - Search query.
# $key - Google API key.
# $start - 0-indexed start point.
# $num - OUTPUT. Estimated number of search results for the query.
# $html_output - OUTPUT. HTML to display to the user (either search results or
# error message).
#
# Returns:
#
# Boolean. true if query is successful, false if not.
#
# Revisions:
#
# [bakert@gmail.com 2004-12-24] Code written.
# [bakert@gmail.com 2004-12-26] Comments added.
#
################################################################################
function do_search($q, $key, $start, &$num, &$html_output) {
global $soapclient;
global $soapoptions;
// Note that we pass in an array of parameters into the Google search.
// The parameters array has to be passed by reference.
// The parameters are well documented in the developer's kit on the
// Google site http://www.google.com/apis
$s = "";
// remove the slashes that are automatically added by PHP before each
// quotation mark
$query = stripslashes("site:bluebones.net -viewsource " . $q);
$params = array(
'key' => $key, // the Developer's key
'q' => $query, // the search query
'start' => $start, // the point in the search results should
// Google start
'maxResults' => 10, // the number of search results (max 10)
'filter' => true, // should the results be filtered? - stops too many
// very similar results
'restrict' => '',
'safeSearch' => false,
'lr' => '',
'ie' => '',
'oe' => ''
);
// Here's where we actually call Google using SOAP.
// doGoogleSearch is the name of the remote procedure call.
$result = $soapclient->call('doGoogleSearch', $params, $soapoptions);
if (PEAR::isError($result))
{
$s .= "
An error #" . $result->getCode() . " occurred!
";
$s .= " Error: " . $result->getMessage() . "
\n";
$html_output = $s;
return false;
}
$num = $result->estimatedTotalResultsCount;
$s .= get_results_snippets($result, $num);
if ($num > 0) {
$s .= get_results_pages_links($q, $start, $num);
}
$html_output = $s;
return true;
}
################################################################################
#
# get_results_snippets
#
# Purpose:
#
# Turns Google API search result into HTML output.
#
# Parameters:
#
# $result - Google API search result.
# $num - Estimated number of query results.
#
# Returns:
#
# HTML for display to the user.
#
# Revisions:
#
# [bakert@gmail.com 2004-12-24] Code written.
# [bakert@gmail.com 2004-12-26] Comments added.
#
################################################################################
function get_results_snippets($result, $num) {
$s = "";
if ($num > 0) {
$elements = $result->resultElements;
foreach ($elements as $item) {
$size = $item->cachedSize;
$title = $item->title;
$url = $item->URL;
$snippet = $item->snippet;
$desc = "$title
$snippet
";
$desc .= "$url";
if ($size) {
$desc .= " - $size - Cached ";
}
# Similar pages never has any results so not including it.
#$desc .= " - " . "Similar Pages";
$desc .= "
\n";
$s .= $desc;
}
}
return $s;
}
################################################################################
#
# get_results_pages_links
#
# Purpose:
#
# Gets String of links to results pages 1 2 3 etc.
#
# Parameters:
#
# $q - Search query.
# $start - 0-indexed start point.
# $num - Estimated number of search results.
#
# Returns:
#
# HTML String of links to pages 1 2 3 etc. and Next.
#
# Revisions:
#
# [bakert@gmail.com 2004-12-24] Code written.
# [bakert@gmail.com 2004-12-26] Comments added.
#
################################################################################
function get_results_pages_links($q, $start, $num) {
$s = "Result Page: ";
for ($i = 0; $i < $num; $i += 10) {
$link = "/search/index.php?q=$q&start=$i";
$displaynum = ($i / 10) + 1;
if ($start == $i) {
$shownum = " " . $displaynum . "";
} else {
$shownum = " " . $displaynum . "";
}
$s .= $shownum;
}
$next = $start + 10;
if ($num > ($start + 10)) {
$s .= " Next
\n";
}
return $s;
}
################################################################################
#
# get_results_line
#
# Purpose:
#
# Gets an HTML String of how many results there are and which are being
# displayed.
#
# Parameters:
#
# $q - Search query.
# $start - 0-indexed start results.
# $num - Estimated number of search results.
#
# Returns:
#
# HTML String.
#
# Revisions:
#
# [bakert@gmail.com 2004-12-24] Code written.
# [bakert@gmail.com 2004-12-26] Comments added.
#
################################################################################
function get_results_line($q, $start, $num) {
$begin = $start + 1;
$end = min(($start + 10), $num);
# XXX this is including "supplemental results" - detect and remove?
$s = "Results $begin to "
. "$end of about $num for "
. "$q.
\n";
return $s;
}
################################################################################
#
# search
#
# Purpose:
#
# Does Google search with retry. Retry is useful because sometimes the
# connection will fail for some reason but will succeed when retried.
#
# Parameters:
#
# $query - Search query.
# $key - Google API key.
# $start - 0-indexed start point.
# $num - OUTPUT. Estimated number of search results.
#
# Returns:
#
# String of HTML for display to user. Either search results or an error
# message.
#
# Revisions:
#
# [bakert@gmail.com 2004-12-24] Code written.
# [bakert@gmail.com 2004-12-26] Comments added.
#
################################################################################
function search($query, $key, $start, &$num) {
$result = false;
$max_retries = 5;
$retry_count = 0;
while (! $result && $retry_count < $max_retries) {
$result = do_search($query, $key, $start, $num, $html_output);
$retry_count++;
}
if (! $result) {
$s = "Sorry, connection to Google failed after retrying "
. "$retry_count times.
";
} else {
$s = $html_output;
}
return $s;
}
main();
?>