1<?php 2/** 3 * 4 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 5 * @author Andreas Gohr <gohr@cosmocode.de> 6 */ 7 8// must be run within Dokuwiki 9if(!defined('DOKU_INC')) die(); 10 11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 12require_once(DOKU_PLUGIN.'action.php'); 13 14class action_plugin_googlesearch extends DokuWiki_Action_Plugin { 15 16 /** 17 * return some info 18 */ 19 function getInfo(){ 20 return array( 21 'author' => 'Andreas Gohr', 22 'email' => 'gohr@cosmocode.de', 23 'date' => '2006-07-17', 24 'name' => 'Google Search Plugin', 25 'desc' => 'Search the wiki using the Google API', 26 'url' => 'http://wiki:splitbrain.org/plugin:googlesearch', 27 ); 28 } 29 30 /** 31 * register the eventhandlers 32 */ 33 function register(&$controller){ 34 $controller->register_hook('ACTION_ACT_PREPROCESS', 35 'BEFORE', 36 $this, 37 'handle_act_preprocess', 38 array()); 39 40 $controller->register_hook('TPL_ACT_UNKNOWN', 41 'BEFORE', 42 $this, 43 'handle_act_unknown', 44 array()); 45 } 46 47 /** 48 * Checks if 'googlesearch' was given as action, if so we 49 * do handle the event our self and no further checking takes place 50 */ 51 function handle_act_preprocess(&$event, $param){ 52 if($event->data != 'googlesearch') return; // nothing to do for us 53 54 $event->stopPropagation(); // this is our very own action, no need to check other plugins 55 $event->preventDefault(); // we handle it our self, thanks 56 } 57 58 /** 59 * If our own 'googlesearch' action was given we produce our content here 60 */ 61 function handle_act_unknown(&$event, $param){ 62 if($event->data != 'googlesearch') return; // nothing to do for us 63 64 // we can handle it -> prevent others 65 $event->stopPropagation(); 66 $event->preventDefault(); 67 68 global $QUERY; 69 $this->_search($QUERY,$_REQUEST['start']); 70 } 71 72 /** 73 * Do the google search and display the results 74 */ 75 function _search($q,$start=0){ 76 global $conf; 77 require_once(dirname(__FILE__).'/nusoap.php'); 78 require_once(dirname(__FILE__).'/GoogleAPI.php'); 79 80 $start = (int) $start; 81 if($start < 0) $start = 0; 82 83 //prepare site URL 84 $urlinfo = parse_url(DOKU_URL); 85 $url = $urlinfo['host']; 86 87 // do the search 88 $API = new GoogleAPI($this->getConf('apikey'),$url); 89 $ret = $API->do_search($q,$start,$this->getConf('maxresults')); 90 91 // handle errors 92 if($ret === false){ 93 echo $this->locale_xhtml('error'); 94 echo '<p>'; 95 echo $this->external_link('http://www.google.com/search?hl=en&safe=off&q='. 96 urlencode("site:$url $q"),'Search for '.hsc($q), 97 'urlextern',$conf['target']['extern']); 98 echo '</p>'; 99 100 echo '<p><b>Error:</b> <em>'.hsc($API->error).'</em></p>'; 101 return; 102 } 103 104 // no results 105 if(!count($ret['resultElements'])){ 106 echo $this->locale_xhtml('noresult'); 107 return; 108 } 109 110 // give info 111 if($ret['estimateIsExact']){ 112 $info = $this->getLang('resultinfo'); 113 }else{ 114 $info = $this->getLang('estimateinfo'); 115 } 116 $info = str_replace('%from',$ret['startIndex'],$info); 117 $info = str_replace('%to',$ret['endIndex'],$info); 118 $info = str_replace('%num',$ret['estimatedTotalResultsCount'],$info); 119 $info = str_replace('%q',hsc($q),$info); 120 121 echo $this->locale_xhtml('result'); 122 echo "<p>$info</p>"; 123 124 // output results 125 foreach($ret['resultElements'] as $hit){ 126 echo '<div class="search_result">'; 127 echo $this->external_link($hit['URL'],$hit['title'], 128 'wikilink1',$conf['target']['intern']); 129 echo '<div class="search_snippet">'; 130 echo $hit['snippet']; 131 echo '</div>'; 132 133 echo '</div>'; 134 } 135 136 echo '<div class="googlesearch_nav">'; 137 138 // give paging buttons 139 if($ret['startIndex'] > 1){ 140 $prev = $ret['startIndex'] - $this->getConf('maxresults') - 1; 141 if($prev < 0) $prev = 0; 142 143 echo $this->external_link(wl('',array('do'=>'googlesearch','id'=>$q,'start'=>$prev),'false','&'), 144 $this->getLang('prev'),'wikilink1 gs_prev',$conf['target']['intern']); 145 } 146 147 148 if($ret['endIndex'] < $ret['estimatedTotalResultsCount'] - $this->getConf('maxresults')){ 149 $next = $ret['endIndex'] + $this->getConf('maxresults'); 150 151 echo $this->external_link(wl('',array('do'=>'googlesearch','id'=>$q,'start'=>$next),'false','&'), 152 $this->getLang('next'),'wikilink1 gs_next',$conf['target']['intern']); 153 } 154 155 echo '</div>'; 156 //dbg($ret); 157 } 158 159 /** 160 * Outputs the search form 161 * 162 * Static function to call from your template like this: 163 * action_plugin_googlesearch::searchform() 164 */ 165 function searchform(){ 166 global $lang; 167 global $ACT; 168 global $QUERY; 169 170 print '<form action="'.wl().'" accept-charset="utf-8" class="search" id="dw__search"><div class="no">'; 171 print '<input type="hidden" name="do" value="googlesearch" />'; 172 print '<input type="text" '; 173 if($ACT == 'googlesearch') print 'value="'.htmlspecialchars($QUERY).'" '; 174 print 'id="qsearch__in" accesskey="f" name="id" class="edit" title="[ALT+F]" />'; 175 print '<input type="submit" value="'.$lang['btn_search'].'" class="button" />'; 176 print '</div></form>'; 177 } 178} 179 180//Setup VIM: ex: et ts=4 enc=utf-8 : 181