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