]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/GooglePlugin.php
New FSF address
[SourceForge/phpwiki.git] / lib / plugin / GooglePlugin.php
1 <?php // -*-php-*-
2 // $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 along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 getDefaultArguments() {
51         return array('q'          => '',
52                      'mode'       => 'search', // or 'cache' or 'spell'
53                      'startIndex' => 1,
54                      'maxResults' => 10, // fixed to 10 for now by google
55                      'formsize'   => 30,
56                      // 'language' => `??
57                      //'license_key'  => false,
58                      );
59     }
60
61     function run($dbi, $argstr, &$request, $basepage) {
62         $args = $this->getArgs($argstr, $request);
63         //        if (empty($args['s']))
64         //    return '';
65         $html = HTML();
66         extract($args);
67         // prevent from dump
68         if ($q and $request->isPost()) {
69             require_once("lib/Google.php");
70             $google = new Google();
71             if (!$google) return '';
72             switch ($mode) {
73                 case 'search': $result = $google->doGoogleSearch($q); break;
74                 case 'cache':  $result = $google->doGetCachedPage($q); break;
75                 case 'spell':  $result = $google->doSpellingSuggestion($q); break;
76                 default:
77                         trigger_error("Invalid mode");
78             }
79             if (isa($result,'HTML'))
80                 $html->pushContent($result);
81             if (isa($result,'GoogleSearchResults')) {
82                 //TODO: result template
83                 if (!empty($result->resultElements)) {
84                     $list = HTML::ol();
85                     foreach ($result->resultElements as $res) {
86                             $li = HTML::li(LinkURL($res['URL'],$res['directoryTitle']),HTML::br(),
87                                            $res['directoryTitle'] ? HTML(HTML::raw('&nbsp;&nbsp;'),HTML::em($res['summary']),' -- ',LinkURL($res['URL'])) : '');
88                         $list->pushContent($li);
89                     }
90                     $html->pushContent($list);
91                 }
92                 else
93                     return _("Nothing found");
94             }
95             if (is_string($result)) {
96                 // cache content also?
97                 $html->pushContent(HTML::blockquote(HTML::raw($result)));
98             }
99         }
100         if ($formsize < 1)  $formsize = 30;
101         // todo: template
102         $form = HTML::form(array('action' => $request->getPostURL(),
103                                  'method' => 'post',
104                                  //'class'  => 'class', //fixme
105                                  'accept-charset' => $GLOBALS['charset']),
106                            HiddenInputs(array('pagename' => $basepage,
107                                               'mode' => $mode)));
108         $form->pushContent(HTML::input(array('type' => 'text',
109                                              'value' => $q,
110                                              'name'  => 'q',
111                                              'size'  => $formsize)));
112         $form->pushContent(HTML::input(array('type' => 'submit',
113                                              'class' => 'button',
114                                              'value' => gettext($mode)
115                                              )));
116         return HTML($html,$form);
117     }
118 };
119
120 // Local Variables:
121 // mode: php
122 // tab-width: 8
123 // c-basic-offset: 4
124 // c-hanging-comment-ender-p: nil
125 // indent-tabs-mode: nil
126 // End:
127 ?>