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