]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/GooglePlugin.php
New experimental feature: use the Google API directly
[SourceForge/phpwiki.git] / lib / plugin / GooglePlugin.php
1 <?php // -*-php-*-
2 rcs_id('$Id: GooglePlugin.php,v 1.1 2004-02-29 01:37:59 rurban Exp $');
3 /**
4  Copyright 2004 $ThePhpWikiProgrammingTeam
5
6  This file is part of PhpWiki.
7
8  PhpWiki is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  PhpWiki is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 require_once("lib/Google.php");
24
25 /**
26  * This module is a wrapper for the Google Web APIs. It allows you to do Google searches, 
27  * retrieve pages from the Google cache, and ask Google for spelling suggestions.
28  *
29  * Note: You must first obtain a license key at http://www.google.com/apis/
30  * Max 1000 queries per day.
31  *
32  * Other possible sample usages:
33  *   Auto-monitor the web for new information on a subject
34  *   Glean market research insights and trends over time
35  *   Invent a catchy online game
36  *   Create a novel UI for searching
37  *   Add Google's spell-checking to an application
38  */
39 class WikiPlugin_GooglePlugin
40 extends WikiPlugin
41 {
42     function getName () {
43         return _("GooglePlugin");
44     }
45
46     function getDescription () {
47         return _("Make use of the Google API");
48     }
49
50     function getVersion() {
51         return preg_replace("/[Revision: $]/", '',
52                             "\$Revision: 1.1 $");
53     }
54
55     function getDefaultArguments() {
56         return array('q'          => '',
57                      'mode'       => 'search', // or 'cache' or 'spell'
58                      'startIndex' => 1,
59                      'maxResults' => 10, // fixed to 10 for now by google
60                      'formsize'   => 30,
61                      // 'language' => `??
62                      //'license_key'  => false,
63                      );
64     }
65
66     function run($dbi, $argstr, &$request, $basepage) {
67         $args = $this->getArgs($argstr, $request);
68         //        if (empty($args['s']))
69         //    return '';
70         $html = HTML();
71         extract($args);
72         if ($request->isPost()) {
73             require_once("lib/Google.php");
74             $google = new Google();
75             switch ($mode) {
76                 case 'search': $result = $google->doGoogleSearch($q); break;
77                 case 'cache':  $result = $google->doGetCachedPage($q); break;
78                 case 'spell':  $result = $google->doSpellingSuggestion($q); break;
79                 default:
80                         trigger_error("Invalid mode");
81             }
82             if (isa($result,'HTML'))
83                 $html->pushContent($result);
84             if (isa($result,'GoogleSearchResults')) {
85                 //todo: result template
86                 foreach ($this->resultElements as $result) {
87                     $html->pushContent(WikiLink($result->URL));
88                     $html->pushContent(HTML::br());
89                 }
90             }
91             if (is_string($result)) {
92                 // cache content also?
93                 $html->pushContent(HTML::blockquote(HTML::raw($result)));
94             }
95         }
96         if ($formsize < 1)  $formsize = 30;
97         // todo: template
98         $form = HTML::form(array('action' => $request->getPostURL(),
99                                  'method' => 'post',
100                                  //'class'  => 'class', //fixme
101                                  'accept-charset' => CHARSET),
102                            HiddenInputs(array('pagename' => $basepage,
103                                               'mode' => $mode)));
104         $form->pushContent(HTML::input(array('type' => 'text',
105                                              'value' => $q,
106                                              'name'  => 'q',
107                                              'size'  => $formsize)));
108         $form->pushContent(HTML::input(array('type' => 'submit',
109                                              'class' => 'button',
110                                              'value' => $mode
111                                              )));
112         return HTML($html,$form);
113     }
114 };
115
116 // $Log: not supported by cvs2svn $
117 // Revision 1.7  2004/02/22 23:20:33  rurban
118 // fixed DumpHtmlToDir,
119 // enhanced sortby handling in PageList
120 //   new button_heading th style (enabled),
121 // added sortby and limit support to the db backends and plugins
122 //   for paging support (<<prev, next>> links on long lists)
123 //
124 // Revision 1.6  2004/02/19 22:06:53  rurban
125 // use new class, to be able to get rid of lib/interwiki.php
126 //
127 // Revision 1.5  2003/02/26 01:56:52  dairiki
128 // Tuning/fixing of POST action URLs and hidden inputs.
129 //
130 // Revision 1.4  2003/01/30 02:46:46  carstenklapp
131 // Bugfix: Plugin was redirecting to nonexistant local wiki page named
132 // "ExternalSearch" instead of the invoked url. Reported by Arthur Chereau.
133 //
134 // Revision 1.3  2003/01/18 21:41:01  carstenklapp
135 // Code cleanup:
136 // Reformatting & tabs to spaces;
137 // Added copyleft, getVersion, getDescription, rcs_id.
138 //
139
140 // Local Variables:
141 // mode: php
142 // tab-width: 8
143 // c-basic-offset: 4
144 // c-hanging-comment-ender-p: nil
145 // indent-tabs-mode: nil
146 // End:
147 ?>