xref: /plugin/sphinxsearch-was/action.php (revision 5:3b758b0a1a7b)
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_PLUGIN . 'action.php');
12require_once(DOKU_PLUGIN . 'sphinxsearch/sphinxapi.php');
13require_once(DOKU_PLUGIN . 'sphinxsearch/PageMapper.php');
14require_once(DOKU_PLUGIN . 'sphinxsearch/SphinxSearch.php');
15
16
17class action_plugin_sphinxsearch extends DokuWiki_Action_Plugin {
18
19    /**
20	* return some info
21	*/
22    function getInfo() {
23        return confToHash(dirname(__FILE__).'/plugin.info.txt');
24	}
25
26    /**
27	* Register to the content display event to place the results under it.
28	*/
29    /**
30     * register the eventhandlers
31     */
32    function register(&$controller){
33        $controller->register_hook('ACTION_ACT_PREPROCESS',
34                                   'BEFORE',
35                                   $this,
36                                   'handle_act_preprocess',
37                                   array());
38
39        $controller->register_hook('TPL_ACT_UNKNOWN',
40                                   'BEFORE',
41                                   $this,
42                                   'handle_act_unknown',
43                                   array());
44    }
45
46    /**
47     * Checks if 'googlesearch' was given as action, if so we
48     * do handle the event our self and no further checking takes place
49     */
50    function handle_act_preprocess(&$event, $param){
51        if($event->data != 'sphinxsearch') return; // nothing to do for us
52
53        $event->stopPropagation(); // this is our very own action, no need to check other plugins
54        $event->preventDefault();  // we handle it our self, thanks
55    }
56
57    /**
58     * If our own 'googlesearch' action was given we produce our content here
59     */
60    function handle_act_unknown(&$event, $param){
61        if($event->data != 'sphinxsearch') return; // nothing to do for us
62
63        // we can handle it -> prevent others
64        $event->stopPropagation();
65        $event->preventDefault();
66
67        global $QUERY;
68        $this->_search($QUERY,$_REQUEST['start']);
69    }
70
71    /**
72     * do the search and displays the result
73     */
74    function _search($query, $start) {
75
76        $start = (int) $start;
77        if($start < 0) $start = 0;
78
79	// backup the config array
80	$cp = $conf;
81
82        $search = new SphinxSearch($this->getConf('host'), $this->getConf('port'), $this->getConf('index'));
83        $pagesList = $search->search($query, $start, $this->getConf('maxresults'));
84
85        $totalFound = $search->getTotalFound();
86
87        echo '<h2>Found '.$totalFound .' documents for query "' . hsc($query).'"</h2>';
88	echo '<div class="search_result">';
89        // printout the results
90	foreach ($pagesList as $crc => $row) {
91            $id = $row['page'];
92            $excerpt = $row['excerpt'];
93            $hid = $row['hid'];
94            $metaData = p_get_metadata($id);
95            if (!empty($row['title'])){
96                $hidTitle = $row['title'];
97            } else {
98                $hidTitle = $metaData['title'];
99            }
100
101            if (!empty($hidTitle)){
102                $title = hsc($id).' ('.hsc($hidTitle).')';
103            } else {
104                $title = hsc($id);
105            }
106
107            $href = !empty($hid) ? (wl($id).'#'.$hid) : wl($id);
108
109            echo '<a href="'.$href.'" title="" class="wikilink1">'.$title.'</a><br/>';
110            echo '<span class="search_cnt">Last modified '.date("Y-m-d H:i",$metaData['date']['modified']).'</span> ';
111            echo '<span class="search_cnt">by '.$metaData['last_change']['user'].'</span> ';
112            echo '<div class="search_snippet">';
113            echo $excerpt;
114            echo '</div>';
115            echo '<br />';
116	}
117        echo '</div>';
118        echo '<div class="sphinxsearch_nav">';
119        if ($start > 1){
120            $prev = $start - $this->getConf('maxresults');
121            if($prev < 0) $prev = 0;
122
123            echo $this->external_link(wl('',array('do'=>'sphinxsearch','id'=>$query,'start'=>$prev),'false','&'),
124                                      'prev','wikilink1 gs_prev',$conf['target']['interwiki']);
125        }
126        echo ' ';
127        if($start + $this->getConf('maxresults') < $totalFound){
128            $next = $start + $this->getConf('maxresults');
129
130            echo $this->external_link(wl('',array('do'=>'sphinxsearch','id'=>$query,'start'=>$next),'false','&'),
131                                      'next','wikilink1 gs_next',$conf['target']['interwiki']);
132        }
133        echo '</div>';
134
135    }
136
137     function searchform(){
138          global $lang;
139          global $ACT;
140          global $QUERY;
141
142          // don't print the search form if search action has been disabled
143          if (!actionOk('sphinxsearch')) return false;
144
145          print '<form action="'.wl().'" accept-charset="utf-8" class="search" id="dw__search"><div class="no">';
146          print '<input type="hidden" name="do" value="sphinxsearch" />';
147          print '<input type="text" ';
148          if($ACT == 'sphinxsearch') print 'value="'.htmlspecialchars($QUERY).'" ';
149          print 'id="qsearch__in" accesskey="f" name="id" class="edit" title="[ALT+F]" />';
150          print '<input type="submit" value="'.$lang['btn_search'].'" class="button" title="'.$lang['btn_search'].'" />';
151          print '</div></form>';
152          return true;
153    }
154}
155
156?>
157