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