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 $totalFound = $search->getTotalFound(); 85 86 echo '<h2>Found '.$totalFound .' documents for query "' . hsc($query).'"</h2>'; 87 echo '<div class="search_result">'; 88 // printout the results 89 foreach ($pagesList as $id => $excerpt) { 90 $metaData = p_get_metadata($id); 91 if (!empty($metaData['title'])){ 92 $title = hsc($id).' ('.hsc($metaData['title']).')'; 93 } else { 94 $title = hsc($id); 95 } 96 echo '<a href="'.wl($id).'" title="" class="wikilink1">'.$title.'</a><br/>'; 97 echo '<span class="search_cnt">Last modified:'.date("Y-m-d H:i",$metaData['date']['modified']).'</span>'; 98 echo '<div class="search_snippet">'; 99 echo $excerpt; 100 echo '</div>'; 101 echo '<br />'; 102 } 103 echo '</div>'; 104 echo '<div class="sphinxsearch_nav">'; 105 if ($start > 1){ 106 $prev = $start - $this->getConf('maxresults'); 107 if($prev < 0) $prev = 0; 108 109 echo $this->external_link(wl('',array('do'=>'sphinxsearch','id'=>$query,'start'=>$prev),'false','&'), 110 'prev','wikilink1 gs_prev',$conf['target']['interwiki']); 111 } 112 echo ' '; 113 if($start + $this->getConf('maxresults') < $totalFound){ 114 $next = $start + $this->getConf('maxresults'); 115 116 echo $this->external_link(wl('',array('do'=>'sphinxsearch','id'=>$query,'start'=>$next),'false','&'), 117 'next','wikilink1 gs_next',$conf['target']['interwiki']); 118 } 119 echo '</div>'; 120 121 } 122 123 function searchform(){ 124 global $lang; 125 global $ACT; 126 global $QUERY; 127 128 // don't print the search form if search action has been disabled 129 if (!actionOk('sphinxsearch')) return false; 130 131 print '<form action="'.wl().'" accept-charset="utf-8" class="search" id="dw__search"><div class="no">'; 132 print '<input type="hidden" name="do" value="sphinxsearch" />'; 133 print '<input type="text" '; 134 if($ACT == 'sphinxsearch') print 'value="'.htmlspecialchars($QUERY).'" '; 135 print 'id="qsearch__in" accesskey="f" name="id" class="edit" title="[ALT+F]" />'; 136 print '<input type="submit" value="'.$lang['btn_search'].'" class="button" title="'.$lang['btn_search'].'" />'; 137 print '</div></form>'; 138 return true; 139 } 140} 141 142?> 143