1<?php 2/** 3 * DokuWiki search functions 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/common.php'); 11 12/** 13 * recurse direcory 14 * 15 * This function recurses into a given base directory 16 * and calls the supplied function for each file and directory 17 * 18 * @author Andreas Gohr <andi@splitbrain.org> 19 */ 20function search(&$data,$base,$func,$opts,$dir='',$lvl=1){ 21 $dirs = array(); 22 $files = array(); 23 24 //read in directories and files 25 $dh = @opendir($base.'/'.$dir); 26 if(!$dh) return; 27 while(($file = readdir($dh)) !== false){ 28 if(preg_match('/^[\._]/',$file)) continue; //skip hidden files and upper dirs 29 if(is_dir($base.'/'.$dir.'/'.$file)){ 30 $dirs[] = $dir.'/'.$file; 31 continue; 32 }elseif(substr($file,-5) == '.lock'){ 33 //skip lockfiles 34 continue; 35 } 36 $files[] = $dir.'/'.$file; 37 } 38 closedir($dh); 39 sort($files); 40 sort($dirs); 41 42 //give directories to userfunction then recurse 43 foreach($dirs as $dir){ 44 if ($func($data,$base,$dir,'d',$lvl,$opts)){ 45 search($data,$base,$func,$opts,$dir,$lvl+1); 46 } 47 } 48 //now handle the files 49 foreach($files as $file){ 50 $func($data,$base,$file,'f',$lvl,$opts); 51 } 52} 53 54/** 55 * The following functions are userfunctions to use with the search 56 * function above. This function is called for every found file or 57 * directory. When a directory is given to the function it has to 58 * decide if this directory should be traversed (true) or not (false) 59 * The function has to accept the following parameters: 60 * 61 * &$data - Reference to the result data structure 62 * $base - Base usually $conf['datadir'] 63 * $file - current file or directory relative to $base 64 * $type - Type either 'd' for directory or 'f' for file 65 * $lvl - Current recursion depht 66 * $opts - option array as given to search() 67 * 68 * return values for files are ignored 69 * 70 * All functions should check the ACL for document READ rights 71 * namespaces (directories) are NOT checked as this would break 72 * the recursion (You can have an nonreadable dir over a readable 73 * one deeper nested) 74 */ 75 76/** 77 * Searches for pages beginning with the given query 78 * 79 * @author Andreas Gohr <andi@splitbrain.org> 80 */ 81function search_qsearch(&$data,$base,$file,$type,$lvl,$opts){ 82 $item = array(); 83 84 if($type == 'd'){ 85 return false; //no handling yet 86 } 87 88 //get id 89 $id = pathID($file); 90 91 //check if it matches the query 92 if(!preg_match('/^'.preg_quote($opts['query'],'/').'/u',$id)){ 93 return false; 94 } 95 96 //check ACL 97 if(auth_quickaclcheck($id) < AUTH_READ){ 98 return false; 99 } 100 101 $data[]=array( 'id' => $id, 102 'type' => $type, 103 'level' => 1, 104 'open' => true); 105 return true; 106} 107 108/** 109 * Build the browsable index of pages 110 * 111 * $opts['ns'] is the current namespace 112 * 113 * @author Andreas Gohr <andi@splitbrain.org> 114 */ 115function search_index(&$data,$base,$file,$type,$lvl,$opts){ 116 $return = true; 117 118 $item = array(); 119 120 if($type == 'd' && !preg_match('#^'.$file.'(/|$)#','/'.$opts['ns'])){ 121 //add but don't recurse 122 $return = false; 123 }elseif($type == 'f' && !preg_match('#\.txt$#',$file)){ 124 //don't add 125 return false; 126 } 127 128 //check ACL 129 $id = pathID($file); 130 if($type=='f' && auth_quickaclcheck($id) < AUTH_READ){ 131 return false; 132 } 133 134 $data[]=array( 'id' => $id, 135 'type' => $type, 136 'level' => $lvl, 137 'open' => $return ); 138 return $return; 139} 140 141/** 142 * List all namespaces 143 * 144 * @author Andreas Gohr <andi@splitbrain.org> 145 */ 146function search_namespaces(&$data,$base,$file,$type,$lvl,$opts){ 147 if($type == 'f') return true; //nothing to do on files 148 149 $id = pathID($file); 150 $data[]=array( 'id' => $id, 151 'type' => $type, 152 'level' => $lvl ); 153 return true; 154} 155 156/** 157 * List all mediafiles in a namespace 158 * 159 * @author Andreas Gohr <andi@splitbrain.org> 160 */ 161function search_media(&$data,$base,$file,$type,$lvl,$opts){ 162 //we do nothing with directories 163 if($type == 'd') return false; 164 165 $info = array(); 166 $info['id'] = pathID($file,true); 167 168 //check ACL for namespace (we have no ACL for mediafiles) 169 if(auth_quickaclcheck(getNS($info['id']).':*') < AUTH_READ){ 170 return false; 171 } 172 173 $info['file'] = basename($file); 174 $info['size'] = filesize($base.'/'.$file); 175 if(preg_match("/\.(jpe?g|gif|png)$/",$file)){ 176 $info['isimg'] = true; 177 $info['info'] = getimagesize($base.'/'.$file); 178 }else{ 179 $info['isimg'] = false; 180 } 181 $data[] = $info; 182 183 return false; 184} 185 186/** 187 * This function just lists documents (for RSS namespace export) 188 * 189 * @author Andreas Gohr <andi@splitbrain.org> 190 */ 191function search_list(&$data,$base,$file,$type,$lvl,$opts){ 192 //we do nothing with directories 193 if($type == 'd') return false; 194 if(preg_match('#\.txt$#',$file)){ 195 //check ACL 196 $id = pathID($file); 197 if(auth_quickaclcheck($id) < AUTH_READ){ 198 return false; 199 } 200 $data[]['id'] = $id;; 201 } 202 return false; 203} 204 205/** 206 * Quicksearch for searching matching pagenames 207 * 208 * $opts['query'] is the search query 209 * 210 * @author Andreas Gohr <andi@splitbrain.org> 211 */ 212function search_pagename(&$data,$base,$file,$type,$lvl,$opts){ 213 //we do nothing with directories 214 if($type == 'd') return true; 215 //only search txt files 216 if(!preg_match('#\.txt$#',$file)) return true; 217 218 //simple stringmatching 219 if (!empty($opts['query'])){ 220 if(strpos($file,$opts['query']) !== false){ 221 //check ACL 222 $id = pathID($file); 223 if(auth_quickaclcheck($id) < AUTH_READ){ 224 return false; 225 } 226 $data[]['id'] = $id; 227 } 228 } 229 return true; 230} 231 232/** 233 * Search for backlinks to a given page 234 * 235 * $opts['ns'] namespace of the page 236 * $opts['name'] name of the page without namespace 237 * 238 * @author Andreas Gohr <andi@splitbrain.org> 239 */ 240function search_backlinks(&$data,$base,$file,$type,$lvl,$opts){ 241 //we do nothing with directories 242 if($type == 'd') return true;; 243 //only search txt files 244 if(!preg_match('#\.txt$#',$file)) return true;; 245 246 //absolute search id 247 $sid = cleanID($opts['ns'].':'.$opts['name']); 248 249 //current id and namespace 250 $cid = pathID($file); 251 $cns = getNS($cid); 252 253 //check ACL 254 if(auth_quickaclcheck($cid) < AUTH_READ){ 255 return false; 256 } 257 258 //fetch instructions 259 require_once(DOKU_INC.'inc/parserutils.php'); 260 $instructions = p_cached_instructions($base.$file,true); 261 if(is_null($instructions)) return false; 262 263 //check all links for match 264 foreach($instructions as $ins){ 265 if($ins[0] == 'internallink' || ($conf['camelcase'] && $ins[0] == 'camelcaselink') ){ 266 $mid = $ins[1][0]; 267 resolve_pageid($cns,$mid,$exists); //exists is not used 268 if($mid == $sid){ 269 //we have a match - finish 270 $data[]['id'] = $cid; 271 break; 272 } 273 } 274 } 275 276 return false; 277} 278 279/** 280 * Fulltextsearch 281 * 282 * $opts['query'] is the search query 283 * 284 * @author Andreas Gohr <andi@splitbrain.org> 285 */ 286function search_fulltext(&$data,$base,$file,$type,$lvl,$opts){ 287 //we do nothing with directories 288 if($type == 'd') return true;; 289 //only search txt files 290 if(!preg_match('#\.txt$#',$file)) return true;; 291 292 //check ACL 293 $id = pathID($file); 294 if(auth_quickaclcheck($id) < AUTH_READ){ 295 return false; 296 } 297 298 //create regexp from queries 299 $poswords = array(); 300 $negwords = array(); 301 $qpreg = preg_split('/\s+/',$opts['query']); 302 303 foreach($qpreg as $word){ 304 switch(substr($word,0,1)){ 305 case '-': 306 if(strlen($word) > 1){ // catch single '-' 307 array_push($negwords,preg_quote(substr($word,1),'#')); 308 } 309 break; 310 case '+': 311 if(strlen($word) > 1){ // catch single '+' 312 array_push($poswords,preg_quote(substr($word,1),'#')); 313 } 314 break; 315 default: 316 array_push($poswords,preg_quote($word,'#')); 317 break; 318 } 319 } 320 321 // a search without any posword is useless 322 if (!count($poswords)) return true; 323 324 $reg = '^(?=.*?'.join(')(?=.*?',$poswords).')'; 325 $reg .= count($negwords) ? '((?!'.join('|',$negwords).').)*$' : '.*$'; 326 search_regex($data,$base,$file,$reg,$poswords); 327 return true; 328} 329 330/** 331 * Reference search 332 * This fuction searches for existing references to a given media file 333 * and returns an array with the found pages. It doesn't pay any 334 * attention to ACL permissions to find every reference. The caller 335 * must check if the user has the appropriate rights to see the found 336 * page and eventually have to prevent the result from displaying. 337 * 338 * @param array $data Reference to the result data structure 339 * @param string $base Base usually $conf['datadir'] 340 * @param string $file current file or directory relative to $base 341 * @param char $type Type either 'd' for directory or 'f' for file 342 * @param int $lvl Current recursion depht 343 * @param mixed $opts option array as given to search() 344 * 345 * $opts['query'] is the demanded media file name 346 * 347 * @author Andreas Gohr <andi@splitbrain.org> 348 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net> 349 */ 350function search_reference(&$data,$base,$file,$type,$lvl,$opts){ 351 global $conf; 352 353 //we do nothing with directories 354 if($type == 'd') return true; 355 356 //only search txt files 357 if(!preg_match('#\.txt$#',$file)) return true; 358 359 //we finish after 'cnt' references found. The return value 360 //'false' will skip subdirectories to speed search up. 361 $cnt = $conf['refshow'] > 0 ? $conf['refshow'] : 1; 362 if(count($data) >= $cnt) return false; 363 364 $reg = '\{\{ *\:?'.$opts['query'].' *(\|.*)?\}\}'; 365 search_regex($data,$base,$file,$reg,array($opts['query'])); 366 return true; 367} 368 369/* ------------- helper functions below -------------- */ 370 371/** 372 * fulltext search helper 373 * searches a text file with a given regular expression 374 * no ACL checks are performed. This have to be done by 375 * the caller if necessary. 376 * 377 * @param array $data reference to array for results 378 * @param string $base base directory 379 * @param string $file file name to search in 380 * @param string $reg regular expression to search for 381 * @param array $words words that should be marked in the results 382 * 383 * @author Andreas Gohr <andi@splitbrain.org> 384 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net> 385 */ 386function search_regex(&$data,$base,$file,$reg,$words){ 387 388 //get text 389 $text = io_readfile($base.'/'.$file); 390 //lowercase text (u modifier does not help with case) 391 $lctext = utf8_strtolower($text); 392 393 //do the fulltext search 394 $matches = array(); 395 if($cnt = preg_match_all('#'.$reg.'#usi',$lctext,$matches)){ 396 //this is not the best way for snippet generation but the fastest I could find 397 $q = $words[0]; //use first word for snippet creation 398 $p = utf8_strpos($lctext,$q); 399 $f = $p - 100; 400 $l = utf8_strlen($q) + 200; 401 if($f < 0) $f = 0; 402 $snippet = '<span class="search_sep"> ... </span>'. 403 htmlspecialchars(utf8_substr($text,$f,$l)). 404 '<span class="search_sep"> ... </span>'; 405 $mark = '('.join('|', $words).')'; 406 $snippet = preg_replace('#'.$mark.'#si','<span class="search_hit">\\1</span>',$snippet); 407 408 $data[] = array( 409 'id' => pathID($file), 410 'count' => preg_match_all('#'.$mark.'#usi',$lctext,$matches), 411 'poswords' => join(' ',$words), 412 'snippet' => $snippet, 413 ); 414 } 415 416 return true; 417} 418 419 420/** 421 * fulltext sort 422 * 423 * Callback sort function for use with usort to sort the data 424 * structure created by search_fulltext. Sorts descending by count 425 * 426 * @author Andreas Gohr <andi@splitbrain.org> 427 */ 428function sort_search_fulltext($a,$b){ 429 if($a['count'] > $b['count']){ 430 return -1; 431 }elseif($a['count'] < $b['count']){ 432 return 1; 433 }else{ 434 return strcmp($a['id'],$b['id']); 435 } 436} 437 438/** 439 * translates a document path to an ID 440 * 441 * @author Andreas Gohr <andi@splitbrain.org> 442 * @todo move to pageutils 443 */ 444function pathID($path,$keeptxt=false){ 445 $id = utf8_decodeFN($path); 446 $id = str_replace('/',':',$id); 447 if(!$keeptxt) $id = preg_replace('#\.txt$#','',$id); 448 $id = preg_replace('#^:+#','',$id); 449 $id = preg_replace('#:+$#','',$id); 450 return $id; 451} 452 453 454//Setup VIM: ex: et ts=2 enc=utf-8 : 455