xref: /plugin/sphinxsearch-was/action.php (revision 31:fcd9815f71a0)
1<?php
2/**
3 * Script to search in dokuwiki documents
4 *
5 * @author Yaroslav Vorozhko <yaroslav@ivinco.com>
6 */
7
8if(!defined('DOKU_INC')) die();
9if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
10
11require_once(DOKU_INC.'inc/parser/parser.php');
12
13require_once(DOKU_PLUGIN . 'action.php');
14require_once(DOKU_PLUGIN . 'sphinxsearch/sphinxapi.php');
15require_once(DOKU_PLUGIN . 'sphinxsearch/PageMapper.php');
16require_once(DOKU_PLUGIN . 'sphinxsearch/SphinxSearch.php');
17require_once(DOKU_PLUGIN . 'sphinxsearch/functions.php');
18
19
20class action_plugin_sphinxsearch extends DokuWiki_Action_Plugin {
21
22    /**
23	* return some info
24	*/
25    function getInfo() {
26        return confToHash(dirname(__FILE__).'/plugin.info.txt');
27	}
28
29    /**
30	* Register to the content display event to place the results under it.
31	*/
32    /**
33     * register the eventhandlers
34     */
35    function register(&$controller){
36        $controller->register_hook('TPL_CONTENT_DISPLAY',   'BEFORE', $this, 'handle_act_unknown', array());
37    }
38
39    /**
40     * If our own 'googlesearch' action was given we produce our content here
41     */
42    function handle_act_unknown(&$event, $param){
43        global $ACT;
44        global $QUERY;
45        if($ACT != 'search') return; // nothing to do for us
46
47        // we can handle it -> prevent others
48        $event->stopPropagation();
49        $event->preventDefault();
50
51
52        $this->_search($QUERY,$_REQUEST['start']);
53    }
54
55    /**
56     * do the search and displays the result
57     */
58    function _search($query, $start) {
59        global $conf;
60
61        $start = (int) $start;
62        if($start < 0) $start = 0;
63
64        $categories = $this->_getCategories($query);
65        $keywords = $this->_getKeywords($query);
66
67        $search = new SphinxSearch($this->getConf('host'), $this->getConf('port'), $this->getConf('index'));
68        $search->setSnippetSize($this->getConf('snippetsize'));
69        $search->setArroundWordsCount($this->getConf('aroundwords'));
70        $search->setTitlePriority($this->getConf('title_priority'));
71        $search->setBodyPriority($this->getConf('body_priority'));
72        $search->setCategoriesPriority($this->getConf('categories_priority'));
73
74        $pagesList = $search->search($keywords, $categories, $start, $this->getConf('maxresults'));
75
76        if ($search->getError()){
77            echo '<b>' . $search->getError() . '</b>!';
78            return;
79        }
80
81        $totalFound = $search->getTotalFound();
82        if(empty($pagesList)){
83            echo '<b>Nothing was found by ' . $query . '</b>!';
84            return;
85        } else {
86            echo '<style type="text/css">
87                div.dokuwiki .search_snippet{
88                    color:#000000;
89                    margin-left:0px;
90                }
91                div.dokuwiki .search_cnt{
92                    color:#CCCCCC;
93                    font-size: 10px;
94                }
95                div.dokuwiki .search_nmsp{
96                    font-size: 10px;
97                }
98                </style>
99                ';
100
101            echo '<h2>Found '.$totalFound . ($totalFound == 1  ? ' document ' : ' documents ') . ' for query "' . hsc($query).'"</h2>';
102            echo '<div class="search_result">';
103            // printout the results
104            foreach ($pagesList as $crc => $row) {
105                $page = $row['page'];
106                $bodyExcerpt = $row['bodyExcerpt'];
107                $titleTextExcerpt = $row['titleTextExcerpt'];
108                $hid = $row['hid'];
109
110                $metaData = p_get_metadata($page);
111
112                if (!empty($titleTextExcerpt)){
113                    $titleText = $titleTextExcerpt;
114                } elseif(!empty($row['title_text'])){
115                    $titleText = $row['title_text'];
116                } elseif(!empty($metaData['title'])){
117                    $titleText = hsc($metaData['title']);
118                } else {
119                    $titleText = hsc($page);
120                }
121
122                $namespaces = getNsLinks($page, $keywords, $search);
123                $href = !empty($hid) ? (wl($page).'#'.$hid) : wl($page);
124
125                echo '<a href="'.$href.'" title="" class="wikilink1">'.$titleText.'</a><br/>';
126                echo '<div class="search_snippet">';
127                echo strip_tags($bodyExcerpt, '<b>,<strong>');
128                echo '</div>';
129                $sep=':';
130                $i = 0;
131                echo '<span class="search_nmsp">';
132                foreach ($namespaces as $name){
133                    $link = $name['link'];
134                    $pageTitle = $name['title'];
135                    tpl_link($link, $pageTitle);
136                    if ($i++ < count($namespaces)-1){
137                        echo $sep;
138                    }
139                }
140                if (!empty($hid)){
141                    echo '#'.$hid;
142                }
143                echo '</span>';
144                echo '<span class="search_cnt"> - Last modified '.date("Y-m-d H:i",$metaData['date']['modified']).'</span> ';
145                echo '<span class="search_cnt">by '.$metaData['last_change']['user'].'</span> ';
146                echo '<br />';echo '<br />';
147            }
148            echo '</div>';
149            echo '<div class="sphinxsearch_nav">';
150            if ($start > 1){
151                $prev = $start - $this->getConf('maxresults');
152                if($prev < 0) $prev = 0;
153
154                echo $this->external_link(wl('',array('do'=>'search','id'=>$query,'start'=>$prev),'false','&'),
155                                          'prev','wikilink1 gs_prev',$conf['target']['interwiki']);
156            }
157            echo ' ';
158            if($start + $this->getConf('maxresults') < $totalFound){
159                $next = $start + $this->getConf('maxresults');
160
161                echo $this->external_link(wl('',array('do'=>'search','id'=>$query,'start'=>$next),'false','&'),
162                                          'next','wikilink1 gs_next',$conf['target']['interwiki']);
163            }
164            echo '</div>';
165        }
166
167    }
168
169     function searchform(){
170          global $lang;
171          global $ACT;
172          global $QUERY;
173
174          // don't print the search form if search action has been disabled
175          if (!actionOk('search')) return false;
176
177          print '<form action="'.wl().'" accept-charset="utf-8" class="search" id="dw__search"><div class="no">';
178          print '<input type="hidden" name="do" value="search" />';
179          print '<input type="text" ';
180          if($ACT == 'search') print 'value="'.htmlspecialchars($QUERY).'" ';
181          print 'id="qsearch__in" accesskey="f" name="id" class="edit" title="[ALT+F]" />';
182          print '<input type="submit" value="'.$lang['btn_search'].'" class="button" title="'.$lang['btn_search'].'" />';
183          print '</div></form>';
184          return true;
185    }
186
187    function _getCategories($query)
188    {
189        $categories = '';
190        $query = urldecode($query);
191        if (false !== ($pos = strpos($query, "@categories"))){;
192            $categories = substr($query, $pos + strlen("@categories"));
193        }
194        return trim($categories);
195    }
196
197    function _getKeywords($query)
198    {
199        $keywords = $query;
200        $query = urldecode($query);
201        if (false !== ($pos = strpos($query, "@categories"))){;
202            $keywords = substr($keywords, 0, $pos);
203        }
204        return trim($keywords);
205    }
206}
207
208?>
209