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