xref: /plugin/sphinxsearch-was/action.php (revision 8:5afb3def6f44)
1<?php
2/**
3 * Script to search in uploaded pdf documents
4 *
5 * @author Dominik Eckelmann <eckelmann@cosmocode.de>
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('ACTION_ACT_PREPROCESS',
37                                   'BEFORE',
38                                   $this,
39                                   'handle_act_preprocess',
40                                   array());
41
42        $controller->register_hook('TPL_ACT_UNKNOWN',
43                                   'BEFORE',
44                                   $this,
45                                   'handle_act_unknown',
46                                   array());
47    }
48
49    /**
50     * Checks if 'googlesearch' was given as action, if so we
51     * do handle the event our self and no further checking takes place
52     */
53    function handle_act_preprocess(&$event, $param){
54        if($event->data != 'sphinxsearch') return; // nothing to do for us
55
56        $event->stopPropagation(); // this is our very own action, no need to check other plugins
57        $event->preventDefault();  // we handle it our self, thanks
58    }
59
60    /**
61     * If our own 'googlesearch' action was given we produce our content here
62     */
63    function handle_act_unknown(&$event, $param){
64        if($event->data != 'sphinxsearch') return; // nothing to do for us
65
66        // we can handle it -> prevent others
67        $event->stopPropagation();
68        $event->preventDefault();
69
70        global $QUERY;
71        $this->_search($QUERY,$_REQUEST['start']);
72    }
73
74    /**
75     * do the search and displays the result
76     */
77    function _search($query, $start) {
78
79        $start = (int) $start;
80        if($start < 0) $start = 0;
81
82	// backup the config array
83	$cp = $conf;
84
85        $search = new SphinxSearch($this->getConf('host'), $this->getConf('port'), $this->getConf('index'));
86        $pagesList = $search->search($query, $start, $this->getConf('maxresults'));
87
88        $totalFound = $search->getTotalFound();
89        echo '<style type="text/css">
90            div.dokuwiki .search_snippet{
91                color:#000000;
92            }
93            div.dokuwiki .search_cnt{
94                color:#CCCCCC;
95                font-size: 10px;
96            }
97            </style>
98            ';
99
100        echo '<h2>Found '.$totalFound .' documents for query "' . hsc($query).'"</h2>';
101	echo '<div class="search_result">';
102        // printout the results
103	foreach ($pagesList as $crc => $row) {
104            $id = $row['page'];
105            $excerpt = $row['excerpt'];
106            $hid = $row['hid'];
107            $metaData = p_get_metadata($id);
108            if (!empty($row['title'])){
109                $hidTitle = $row['title'];
110            } else {
111                $hidTitle = $metaData['title'];
112            }
113
114            if (!empty($hidTitle)){
115                $title = hsc($hidTitle);
116            } else {
117                $title = hsc($id);
118            }
119            $namespaces = getNsLinks($id);
120            $href = !empty($hid) ? (wl($id).'#'.$hid) : wl($id);
121
122            echo '<a href="'.$href.'" title="" class="wikilink1">'.$title.'</a><br/>';
123            echo '<div class="search_snippet">';
124            echo strip_tags($excerpt, '<b>,<strong>');
125            echo '</div>';
126            $sep=' &raquo; ';
127            $i = 0;
128            foreach ($namespaces as $link => $pageTitle){
129                tpl_link($link, $pageTitle);
130                if ($i++ < count($namespaces)-1){
131                    echo $sep;
132                }
133            }
134            echo '<span class="search_cnt"> - Last modified '.date("Y-m-d H:i",$metaData['date']['modified']).'</span> ';
135            echo '<span class="search_cnt">by '.$metaData['last_change']['user'].'</span> ';
136            echo '<br />';echo '<br />';
137	}
138        echo '</div>';
139        echo '<div class="sphinxsearch_nav">';
140        if ($start > 1){
141            $prev = $start - $this->getConf('maxresults');
142            if($prev < 0) $prev = 0;
143
144            echo $this->external_link(wl('',array('do'=>'sphinxsearch','id'=>$query,'start'=>$prev),'false','&'),
145                                      'prev','wikilink1 gs_prev',$conf['target']['interwiki']);
146        }
147        echo ' ';
148        if($start + $this->getConf('maxresults') < $totalFound){
149            $next = $start + $this->getConf('maxresults');
150
151            echo $this->external_link(wl('',array('do'=>'sphinxsearch','id'=>$query,'start'=>$next),'false','&'),
152                                      'next','wikilink1 gs_next',$conf['target']['interwiki']);
153        }
154        echo '</div>';
155
156    }
157
158     function searchform(){
159          global $lang;
160          global $ACT;
161          global $QUERY;
162
163          // don't print the search form if search action has been disabled
164          if (!actionOk('sphinxsearch')) return false;
165
166          print '<form action="'.wl().'" accept-charset="utf-8" class="search" id="dw__search"><div class="no">';
167          print '<input type="hidden" name="do" value="sphinxsearch" />';
168          print '<input type="text" ';
169          if($ACT == 'sphinxsearch') print 'value="'.htmlspecialchars($QUERY).'" ';
170          print 'id="qsearch__in" accesskey="f" name="id" class="edit" title="[ALT+F]" />';
171          print '<input type="submit" value="'.$lang['btn_search'].'" class="button" title="'.$lang['btn_search'].'" />';
172          print '</div></form>';
173          return true;
174    }
175}
176
177?>
178