]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/Google.php
New experimental feature: use the Google API directly
[SourceForge/phpwiki.git] / lib / Google.php
1 <?
2 /**
3  * Google API
4  *
5  * @author: Chris Petersen, Reini Urban
6  */
7 /*
8  Copyright (c) 2002 Intercept Vector
9  Copyright (c) 2004 Reini Urban
10
11  This library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU Lesser General Public
13  License as published by the Free Software Foundation; either
14  version 2.1 of the License, or (at your option) any later version.
15
16  This library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  Lesser General Public License for more details.
20
21  You should have received a copy of the GNU Lesser General Public
22  License along with this library; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  
25  If you have any questions or comments, please email:
26
27  Chris Petersen
28  admin@interceptvector.com
29  Intercept Vector
30  http://www.interceptvector.com
31 */
32
33 /*
34  * Objectified, simplified, documented and added the two other queries 
35  * by Reini Urban
36  */
37
38 /**
39  * GoogleSearchResults, list of GoogleSearch Result Elements
40  *
41  * Each time you issue a search request to the Google service, a
42  * response is returned back to you. This section describes the
43  * meanings of the values returned to you.
44  *
45  * <documentFiltering> - A Boolean value indicating whether filtering
46  * was performed on the search results. This will be "true" only if
47  * (a) you requested filtering and (b) filtering actually occurred.
48  *
49  * <searchComments> - A text string intended for display to an end
50  * user. One of the most common messages found here is a note that
51  * "stop words" were removed from the search automatically. (This
52  * happens for very common words such as "and" and "as.")
53  *
54  * <estimatedTotalResultsCount> - The estimated total number of
55  * results that exist for the query.  Note: The estimated number may
56  * be either higher or lower than the actual number of results that
57  * exist.
58  *
59  * <estimateIsExact> - A Boolean value indicating that the estimate is
60  * actually the exact value.
61  *
62  * <resultElements> - An array of <resultElement> items. This
63  * corresponds to the actual list of search results.
64  *
65  * <searchQuery> - This is the value of <q> for the search request.
66  *
67  * <startIndex> - Indicates the index (1-based) of the first search
68  * result in <resultElements>.
69  *
70  * <endIndex> - Indicates the index (1-based) of the last search
71  * result in <resultElements>.
72  *
73  * <searchTips> - A text string intended for display to the end
74  * user. It provides instructive suggestions on how to use Google.
75  *
76  * <directoryCategories> - An array of <directoryCategory> items. This
77  * corresponds to the ODP directory matches for this search.
78  *
79  * <searchTime> - Text, floating-point number indicating the total
80  * server time to return the search results, measured in seconds.
81  */
82
83 class GoogleSearchResults {
84     var $_fields = "documentFiltering,searchComments,estimatedTotalResultsCount,estimateIsExact,searchQuery,startIndex,endIndex,searchTips,directoryCategories,searchTime,resultElements";
85     var $resultElements, $results;
86
87     function GoogleSearchResults ($result) {
88         $this->fields = explode(',',$this->_fields);
89         foreach ($this->fields as $f) {
90             $this->{$f} = $result[$f];
91         }
92         $i = 0; $this->results = array();
93         //$this->resultElements = $result['resultElements'];
94         foreach ($this->resultElements as $r) {
95             $this->results[] = new GoogleSearchResult($r);
96         }
97         return $this;
98     }
99 }
100
101 /**
102  *   Google Search Result Element:
103  *
104  *   <summary> - If the search result has a listing in the ODP
105  *   directory, the ODP summary appears here as a text string.
106  *
107  *   <URL> - The URL of the search result, returned as text, with an
108  *   absolute URL path.
109  *
110  *   <snippet> - A snippet which shows the query in context on the URL
111  *   where it appears. This is formatted HTML and usually includes <B>
112  *   tags within it. Note that the query term does not always appear
113  *   in the snippet. Note: Query terms will be in highlighted in bold
114  *   in the results, and line breaks will be included for proper text
115  *   wrapping.
116  *
117  *   <title> - The title of the search result, returned as HTML.
118  *
119  *   <cachedSize> - Text (Integer + "k"). Indicates that a cached
120  *   version of the <URL> is available; size is indicated in
121  *   kilobytes.
122  *
123  *   <relatedInformationPresent> - Boolean indicating that the
124  *   "related:" query term is supported for this URL.
125  *
126  *   <hostName> - When filtering occurs, a maximum of two results from
127  *   any given host is returned. When this occurs, the second
128  *   resultElement that comes from that host contains the host name in
129  *   this parameter.
130  *
131  *   <directoryCategory> - array with "fullViewableName" and 
132  *   "specialEncoding" keys.
133  *
134  *   <directoryTitle> - If the URL for this resultElement is contained
135  *   in the ODP directory, the title that appears in the directory
136  *   appears here as a text string. Note that the directoryTitle may
137  *   be different from the URL's <title>.
138  */
139 class GoogleSearchResult {
140     var $_fields = "summary,URL,snippet,title,cachedSize,relatedInformationPresent,hostName,directoryCategory,directoryTitle";
141     function GoogleSearchResult ($result) {
142         $this->fields = explode(',',$this->_fields);
143         foreach ($this->fields as $f) {
144             $this->{$f} = $result[$f];
145         }
146         return $this;
147     }
148 }
149
150 class Google {
151
152     function Google($maxResults=10,$license_key=false,$proxy=false) {
153         if ($license_key)
154             $this->license_key = $license_key;
155         elseif (!defined('GOOGLE_LICENSE_KEY'))
156             return HTML::div(array('class' => 'errors'),
157                              fmt("You must first obtain a license key at %s to be able to use the Google API.",
158                                  WikiLink("http://www.google.com/apis/")),
159                              fmt("It's free however."));
160         else
161           $this->license_key = GOOGLE_LICENSE_KEY;
162         require_once("lib/nusoap/nusoap.php");
163
164         $this->soapclient = new soapclient(DATA_PATH."/"."GoogleSearch.wsdl", "wsdl");
165         $this->proxy = $this->soapclient->getProxy();
166         if ($maxResults > 10) $maxResults = 10;
167         if ($maxResults < 1) $maxResults = 1;
168         $this->maxResults = $maxResults;
169     }
170
171     /** 
172      * doGoogleSearch
173      *
174      * See http://www.google.com/help/features.html for examples of
175      * advanced features.  Anything that works at the Google web site
176      * will work as a query string in this method.
177      * 
178      * You can use the start and maxResults parameters to page through
179      * multiple pages of results. Note that 'maxResults' is currently
180      * limited by Google to 10.  See the API reference for more
181      * advanced examples and a full list of country codes and topics
182      * for use in the restrict parameter, along with legal values for
183      * the language, inputencoding, and outputencoding parameters.
184      *
185      * <license key> Provided by Google, this is required for you to access the
186      * Google service. Google uses the key for authentication and
187      * logging.
188      * <q> (See the API docs for details on query syntax.)
189      * <start> Zero-based index of the first desired result.
190      * <maxResults> Number of results desired per query. The maximum
191      * value per query is 10.  Note: If you do a query that doesn't
192      * have many matches, the actual number of results you get may be
193      * smaller than what you request.
194      * <filter> Activates or deactivates automatic results filtering,
195      * which hides very similar results and results that all come from
196      * the same Web host. Filtering tends to improve the end user
197      * experience on Google, but for your application you may prefer
198      * to turn it off. (See the API docs for more
199      * details.)
200      * <restrict> Restricts the search to a subset of the Google Web
201      * index, such as a country like "Ukraine" or a topic like
202      * "Linux." (See the API docs for more details.)
203      * <safeSearch> A Boolean value which enables filtering of adult
204      * content in the search results. See SafeSearch for more details.
205      * <lr> Language Restrict - Restricts the search to documents
206      * within one or more languages.
207      * <ie> Input Encoding - this parameter has been deprecated and is
208      * ignored. All requests to the APIs should be made with UTF-8
209      * encoding. (See the API docs for details.)
210      * <oe> Output Encoding - this parameter has been deprecated and is
211      * ignored. All requests to the APIs should be made with UTF-8
212      * encoding.
213      */
214     function doGoogleSearch($query, $startIndex=1, $maxResults=10, $filter = "false",
215                             $restrict='', $safeSearch='false', $lr='',
216                             $inputencoding='UTF-8', $outputencoding='UTF-8') {
217         // doGoogleSearch() gets created automatically!! (some eval'ed code from the soap request)
218         $result = $this->doGoogleSearch(GOOGLE_LICENSE_KEY, // "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
219                                         $query,
220                                         $startIndex,
221                                         $maxResults,
222                                         $filter,
223                                         $restrict,
224                                         $safeSearch,
225                                         $lr,
226                                         $inputencoding, // ignored by server, everything is UTF-8 now
227                                         $outputencoding);
228         return new GoogleSearchResults($result);
229     }
230
231     /**
232      * Retrieve a page from the Google cache.
233      *
234      * Cache requests submit a URL to the Google Web APIs service and
235      * receive in return the contents of the URL when Google's
236      * crawlers last visited the page (if available).
237      *
238      * Please note that Google is not affiliated with the authors of
239      * cached pages nor responsible for their content.
240      *
241      * The return type for cached pages is base64 encoded text.
242      *
243      *  @params string url - full URL to the page to retrieve
244      *  @return string full text of the cached page
245      */
246     function doGetCachedPage($url) {
247         // This class gets created automatically!! (some eval'ed code from the soap request)
248         $result = $this->proxy->doGetCachedPage(GOOGLE_LICENSE_KEY,
249                                                 $url);
250         if (!empty($result)) return base64_decode($result);
251     }
252
253     /**
254      * Get spelling suggestions from Google
255      *
256      * @param  string phrase   word or phrase to spell-check 
257      * @return string          text of any suggested replacement, or None
258      */
259     function doSpellingSuggestion($phrase) {
260         // This class gets created automatically!! (some eval'ed code from the soap request)
261         return $this->proxy->doSpellingSuggestion(GOOGLE_LICENSE_KEY,
262                                                   $phrase);
263     }
264 }
265
266 // Local Variables:
267 // mode: php
268 // tab-width: 8
269 // c-basic-offset: 4
270 // c-hanging-comment-ender-p: nil
271 // indent-tabs-mode: nil
272 // End:
273 ?>