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