Highway to PSR2
[openemr.git] / portal / patient / fwk / libs / verysimple / Search / Google.php
blobf564edad7869be9f153214d4a82f3709eaf4845f
1 <?php
2 /** @package verysimple::Search */
4 /**
5 * include required files
6 */
7 require_once("SearchEngine.php");
8 require_once('SOAP/Client.php'); // PEAR::SOAP::Client
10 /**
11 * This is an implmentation of a SearchRank/SearchEngine for Google.
12 * It provides
13 * a convenient way to execute search queries, get results, ranking and inbound
14 * links from google
16 * example
17 * $google = new Google($API_KEY); // Google API Key must be obtained from Google
19 * $rank = $google->GetRank($url,$searchterm,$MAX_RESULTS);
20 * $links = $google->GetLinks($url);
22 * @package verysimple::Search
23 * @author VerySimple Inc.
24 * @copyright 1997-2007 VerySimple, Inc.
25 * @license http://www.gnu.org/licenses/lgpl.html LGPL
26 * @version 1.0
28 class Google extends SearchEngine
31 /**
32 * Returns the inbound links for the given url
34 * @param string $url
35 * the url for which you want to find the rank
36 * @param string $max
37 * the maximum number to return
38 * @return SearchResult
40 function GetLinks($url, $max = 10)
42 return $this->DoSearch("link:" . $url, 0, $max);
45 /**
46 * Returns the estimated number of inbound links for the given url
48 * @param string $url
49 * the url for which you want to find the rank
50 * @return int
52 function GetLinkCount($url)
54 return $this->GetLinks($url, 1)->estimatedTotalResultsCount;
57 /**
58 * Given a url and result returned by DoSearch, returns the rank that url appears on the page
60 * @param string $url
61 * the url for which you want to find the rank
62 * @param string $query
63 * the search query
64 * @param int $maxresults
65 * (default 30) the max number of results to search for.
66 * @return SearchRank
68 function GetRank($url, $query, $maxresults = 30)
70 $current_page = 0;
71 $result_counter = 0;
72 $page_size = 10;
74 $rank = new SearchRank();
76 $rank->Query = $query;
78 while ($rank->Position == 0 && $result_counter <= $maxresults) {
79 $current_page ++;
81 $result = $this->DoSearch($query, $result_counter, $page_size);
83 // only loop through as many results as we have
84 if ($result->estimatedTotalResultsCount < $maxresults) {
85 $maxresults = $result->estimatedTotalResultsCount;
88 if ($current_page == 1 && $result->estimatedTotalResultsCount > 0) {
89 $rank->EstimatedResults = $result->estimatedTotalResultsCount;
90 $rank->TopRankedUrl = $result->resultElements [0]->URL;
91 $rank->TopRankedSnippet = $result->resultElements [0]->snippet;
92 $rank->TopRankedTitle = $result->resultElements [0]->title;
95 $cp_rank = $this->GetPositionOnPage($url, $result);
97 if ($cp_rank->Position) {
98 // we found a match
99 $rank->Page = $current_page;
100 $rank->Position = $cp_rank->Position * $current_page;
101 $rank->Title = $cp_rank->Title;
102 $rank->Snippet = $cp_rank->Snippet;
103 $rank->Url = $cp_rank->Url;
106 $result_counter += $page_size;
107 $result = null;
110 return $rank;
114 * Given a url and result returned by DoSearch, returns the rank that url appears on the page
116 * @param string $url
117 * @param Result $result
118 * @return SearchRank
120 function GetPositionOnPage($url, &$result)
122 $rank = new SearchRank();
123 $counter = 0;
125 foreach ($result->resultElements as $element) {
126 $counter ++;
127 // print "<div>$url :: $counter = ".$element->URL."</div>";
128 $normalizedurl = str_replace("/", "\\/", $url);
130 if ($rank->Position == 0 && preg_match("/$normalizedurl/i", $element->URL)) {
131 $rank->Position = ( int ) $counter;
132 $rank->Title = $element->title;
133 $rank->Snippet = $element->snippet;
134 $rank->Url = $element->URL;
138 return $rank;
142 * Queries google for the specified search query
144 * @param string $query
145 * @param int $start
146 * @param int $max
147 * @param bool $filter
148 * @param bool $restrict
149 * @param bool $safe
150 * @param string $lr
151 * @param string $ie
152 * @param string $oe
153 * @throws Exception
155 function DoSearch($query, $start = 0, $max = 10, $filter = false, $restrict = "", $safe = false, $lr = "", $ie = "", $oe = "")
157 $result = null;
158 $continue = true;
159 $counter = 0;
161 while ($continue) {
162 $counter ++;
163 try {
164 $result = $this->_googleSoap($query, $start, $max, $filter, $restrict, $safe, $lr, $ie, $oe);
165 $continue = false;
166 } catch (exception $ex) {
167 $this->FailedRequests ++;
169 if (preg_match("/Daily limit/i", $ex->getMessage()) || preg_match("/Invalid/i", $ex->getMessage())) {
170 throw $ex;
173 if ($counter > 2) {
174 throw new Exception("Tried to contact Google API $counter times without success:" . $ex->getMessage());
179 return $result;
183 * Makes SOAP request to google
185 private function _googleSoap($query, $start = 0, $max = 10, $filter = false, $restrict = "", $safe = false, $lr = "", $ie = "", $oe = "")
187 $wsdl = new SOAP_WSDL('http://api.google.com/GoogleSearch.wsdl');
188 $soapclient = $wsdl->getProxy();
190 if (get_class($soapclient) != "SOAP_Fault") {
191 $result = $soapclient->doGoogleSearch($this->Key, $query, $start, $max, $filter, $restrict, $safe, $lr, $ie, $oe);
192 } else {
193 throw new Exception("SOAP_Fault: " . $soapclient->message);
196 if (PEAR::isError($result)) {
197 throw new Exception("PEAR Exception: " . $result->message);
198 } else {
199 return $result;