]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/GooglePlugin.php
Replace tabs by spaces; remove EOL spaces
[SourceForge/phpwiki.git] / lib / plugin / GooglePlugin.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
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$");
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         // prevent from dump
73         if ($q and $request->isPost()) {
74             require_once("lib/Google.php");
75             $google = new Google();
76             if (!$google) return '';
77             switch ($mode) {
78                 case 'search': $result = $google->doGoogleSearch($q); break;
79                 case 'cache':  $result = $google->doGetCachedPage($q); break;
80                 case 'spell':  $result = $google->doSpellingSuggestion($q); break;
81                 default:
82                         trigger_error("Invalid mode");
83             }
84             if (isa($result,'HTML'))
85                 $html->pushContent($result);
86             if (isa($result,'GoogleSearchResults')) {
87                 //TODO: result template
88                 if (!empty($result->resultElements)) {
89                     $list = HTML::ol();
90                     foreach ($result->resultElements as $res) {
91                             $li = HTML::li(LinkURL($res['URL'],$res['directoryTitle']),HTML::br(),
92                                            $res['directoryTitle'] ? HTML(HTML::raw('&nbsp;&nbsp;'),HTML::em($res['summary']),' -- ',LinkURL($res['URL'])) : '');
93                         $list->pushContent($li);
94                     }
95                     $html->pushContent($list);
96                 }
97                 else
98                     return _("Nothing found");
99             }
100             if (is_string($result)) {
101                 // cache content also?
102                 $html->pushContent(HTML::blockquote(HTML::raw($result)));
103             }
104         }
105         if ($formsize < 1)  $formsize = 30;
106         // todo: template
107         $form = HTML::form(array('action' => $request->getPostURL(),
108                                  'method' => 'post',
109                                  //'class'  => 'class', //fixme
110                                  'accept-charset' => $GLOBALS['charset']),
111                            HiddenInputs(array('pagename' => $basepage,
112                                               'mode' => $mode)));
113         $form->pushContent(HTML::input(array('type' => 'text',
114                                              'value' => $q,
115                                              'name'  => 'q',
116                                              'size'  => $formsize)));
117         $form->pushContent(HTML::input(array('type' => 'submit',
118                                              'class' => 'button',
119                                              'value' => gettext($mode)
120                                              )));
121         return HTML($html,$form);
122     }
123 };
124
125 // Local Variables:
126 // mode: php
127 // tab-width: 8
128 // c-basic-offset: 4
129 // c-hanging-comment-ender-p: nil
130 // indent-tabs-mode: nil
131 // End:
132 ?>