1<?php 2/** 3 * DokuWiki fulltextsearch functions using the index 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Andreas Gohr <andi@splitbrain.org> 7 */ 8 9 if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/'); 10 require_once(DOKU_INC.'inc/indexer.php'); 11 12 13/** 14 * The fulltext search 15 * 16 * Returns a list of matching documents for the given query 17 * 18 */ 19function ft_pageSearch($query,&$poswords){ 20 $q = ft_queryParser($query); 21 22 // use this for higlighting later: 23 $poswords = join(' ',$q['and']); 24 25 // lookup all words found in the query 26 $words = array_merge($q['and'],$q['not']); 27 if(!count($words)) return array(); 28 $result = idx_lookup($words); 29 30 // merge search results with query 31 foreach($q['and'] as $pos => $w){ 32 $q['and'][$pos] = $result[$w]; 33 } 34 // create a list of unwanted docs 35 $not = array(); 36 foreach($q['not'] as $pos => $w){ 37 $not = array_merge($not,array_keys($result[$w])); 38 } 39 40 // combine and-words 41 if(count($q['and']) > 1){ 42 $docs = ft_resultCombine($q['and']); 43 }else{ 44 $docs = $q['and'][0]; 45 } 46 if(!count($docs)) return array(); 47 48 // remove negative matches 49 foreach($not as $n){ 50 unset($docs[$n]); 51 } 52 53 if(!count($docs)) return array(); 54 55 // handle phrases 56 if(count($q['phrases'])){ 57 //build a regexp 58 $q['phrases'] = array_map('utf8_strtolower',$q['phrases']); 59 $q['phrases'] = array_map('preg_quote',$q['phrases']); 60 $regex = '('.join('|',$q['phrases']).')'; 61 62 // check the source of all documents for the exact phrases 63 foreach(array_keys($docs) as $id){ 64 $text = utf8_strtolower(rawWiki($id)); 65 if(!preg_match('/'.$regex.'/usi',$text)){ 66 unset($docs[$id]); // no hit - remove 67 } 68 } 69 } 70 71 if(!count($docs)) return array(); 72 73 // check ACL permissions 74 foreach(array_keys($docs) as $doc){ 75 if(auth_quickaclcheck($doc) < AUTH_READ){ 76 unset($docs[$doc]); 77 } 78 } 79 80 if(!count($docs)) return array(); 81 82 // if there are any hits left, sort them by count 83 arsort($docs); 84 85 return $docs; 86} 87 88/** 89 * Returns the backlinks for a given page 90 * 91 * Does a quick lookup with the fulltext index, then 92 * evaluates the instructions of the found pages 93 */ 94function ft_backlinks($id){ 95 global $conf; 96 $result = array(); 97 98 // quick lookup of the pagename 99 $page = noNS($id); 100 $matches = idx_lookup(array($page)); 101 102 if(!count($matches)) return $result; 103 require_once(DOKU_INC.'inc/parserutils.php'); 104 105 106 // check instructions for matching links 107 foreach(array_keys($matches[$page]) as $match){ 108 $instructions = p_cached_instructions(wikiFN($match),true); 109 if(is_null($instructions)) continue; 110 111 $match_ns = getNS($match); 112 113 foreach($instructions as $ins){ 114 if($ins[0] == 'internallink' || ($conf['camelcase'] && $ins[0] == 'camelcaselink') ){ 115 $link = $ins[1][0]; 116 resolve_pageid($match_ns,$link,$exists); //exists is not used 117 if($link == $id){ 118 //we have a match - finish 119 $result[] = $match; 120 break; 121 } 122 } 123 } 124 } 125 126 if(!count($result)) return $result; 127 128 // check ACL permissions 129 foreach(array_keys($result) as $idx){ 130 if(auth_quickaclcheck($result[$idx]) < AUTH_READ){ 131 unset($result[$idx]); 132 } 133 } 134 135 sort($result); 136 return $result; 137} 138 139/** 140 * Quicksearch for pagenames 141 * 142 * By default it only matches the pagename and ignores the 143 * namespace. This can be changed with the second parameter 144 * 145 * @author Andreas Gohr <andi@splitbrain.org> 146 */ 147function ft_pageLookup($id,$pageonly=true){ 148 global $conf; 149 $id = preg_quote($id,'/'); 150 $pages = file($conf['cachedir'].'/page.idx'); 151 $pages = array_values(preg_grep('/'.$id.'/',$pages)); 152 153 $cnt = count($pages); 154 for($i=0; $i<$cnt; $i++){ 155 if($pageonly){ 156 if(!preg_match('/'.$id.'/',noNS($pages[$i]))){ 157 unset($pages[$i]); 158 continue; 159 } 160 } 161 if(!@file_exists(wikiFN($pages[$i]))){ 162 unset($pages[$i]); 163 continue; 164 } 165 } 166 167 if(!count($pages)) return array(); 168 169 // check ACL permissions 170 foreach(array_keys($pages) as $idx){ 171 if(auth_quickaclcheck($pages[$idx]) < AUTH_READ){ 172 unset($pages[$idx]); 173 } 174 } 175 176 sort($pages); 177 return $pages; 178} 179 180/** 181 * Creates a snippet extract 182 * 183 * @author Andreas Gohr <andi@splitbrain.org> 184 */ 185function ft_snippet($id,$poswords){ 186 $poswords = preg_quote($poswords,'#'); 187 $re = '('.str_replace(' ','|',$poswords).')'; 188 $text = rawWiki($id); 189 //FIXME caseinsensitive matching doesn't work with UTF-8!? 190 preg_match_all('#(.{0,50})'.$re.'(.{0,50})#iu',$text,$matches,PREG_SET_ORDER); 191 192 $cnt = 0; 193 $snippet = ''; 194 foreach($matches as $match){ 195 $snippet .= '...'.htmlspecialchars($match[1]); 196 $snippet .= '<span class="search_hit">'; 197 $snippet .= htmlspecialchars($match[2]); 198 $snippet .= '</span>'; 199 $snippet .= htmlspecialchars($match[3]).'... '; 200 if($cnt++ == 2) break; 201 } 202 203 return $snippet; 204} 205 206/** 207 * Combine found documents and sum up their scores 208 * 209 * This function is used to combine searched words with a logical 210 * AND. Only documents available in all arrays are returned. 211 * 212 * based upon PEAR's PHP_Compat function for array_intersect_key() 213 * 214 * @param array $args An array of page arrays 215 */ 216function ft_resultCombine($args){ 217 $array_count = count($args); 218 $result = array(); 219 foreach ($args[0] as $key1 => $value1) { 220 for ($i = 1; $i !== $array_count; $i++) { 221 foreach ($args[$i] as $key2 => $value2) { 222 if ((string) $key1 === (string) $key2) { 223 if(!isset($result[$key1])) $result[$key1] = $value1; 224 $result[$key1] += $value2; 225 } 226 } 227 } 228 } 229 return $result; 230} 231 232/** 233 * Builds an array of search words from a query 234 * 235 * @todo support OR and parenthesises? 236 */ 237function ft_queryParser($query){ 238 global $conf; 239 $swfile = DOKU_INC.'inc/lang/'.$conf['lang'].'/stopwords.txt'; 240 if(@file_exists($swfile)){ 241 $stopwords = file($swfile); 242 }else{ 243 $stopwords = array(); 244 } 245 246 $q = array(); 247 $q['query'] = $query; 248 $q['phrases'] = array(); 249 $q['and'] = array(); 250 $q['not'] = array(); 251 252 // handle phrase searches 253 while(preg_match('/"(.*?)"/',$query,$match)){ 254 $q['phrases'][] = $match[0]; 255 $q['and'] = array_merge(idx_tokenizer($match[0],$stopwords)); 256 $query = preg_replace('/"(.*?)"/','',$query,1); 257 } 258 259 $words = explode(' ',$query); 260 foreach($words as $w){ 261 if($w{0} == '-'){ 262 $token = idx_tokenizer($w,$stopwords); 263 if(count($token)) $q['not'] = array_merge($q['not'],$token); 264 }else{ 265 $token = idx_tokenizer($w,$stopwords); 266 if(count($token)) $q['and'] = array_merge($q['and'],$token); 267 } 268 } 269 270 return $q; 271} 272 273//Setup VIM: ex: et ts=4 enc=utf-8 : 274