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 $id = pathID($file); 129 130 //check hidden 131 if($type=='f' && isHiddenPage($id)){ 132 return false; 133 } 134 135 //check ACL 136 if($type=='f' && auth_quickaclcheck($id) < AUTH_READ){ 137 return false; 138 } 139 140 $data[]=array( 'id' => $id, 141 'type' => $type, 142 'level' => $lvl, 143 'open' => $return ); 144 return $return; 145} 146 147/** 148 * List all namespaces 149 * 150 * @author Andreas Gohr <andi@splitbrain.org> 151 */ 152function search_namespaces(&$data,$base,$file,$type,$lvl,$opts){ 153 if($type == 'f') return true; //nothing to do on files 154 155 $id = pathID($file); 156 $data[]=array( 'id' => $id, 157 'type' => $type, 158 'level' => $lvl ); 159 return true; 160} 161 162/** 163 * List all mediafiles in a namespace 164 * 165 * @author Andreas Gohr <andi@splitbrain.org> 166 */ 167function search_media(&$data,$base,$file,$type,$lvl,$opts){ 168 //we do nothing with directories 169 if($type == 'd') return false; 170 171 $info = array(); 172 $info['id'] = pathID($file,true); 173 174 //check ACL for namespace (we have no ACL for mediafiles) 175 if(auth_quickaclcheck(getNS($info['id']).':*') < AUTH_READ){ 176 return false; 177 } 178 179 $info['file'] = basename($file); 180 $info['size'] = filesize($base.'/'.$file); 181 if(preg_match("/\.(jpe?g|gif|png)$/",$file)){ 182 $info['isimg'] = true; 183 require_once(DOKU_INC.'inc/JpegMeta.php'); 184 $info['meta'] = new JpegMeta($base.'/'.$file); 185 }else{ 186 $info['isimg'] = false; 187 } 188 $data[] = $info; 189 190 return false; 191} 192 193/** 194 * This function just lists documents (for RSS namespace export) 195 * 196 * @author Andreas Gohr <andi@splitbrain.org> 197 */ 198function search_list(&$data,$base,$file,$type,$lvl,$opts){ 199 //we do nothing with directories 200 if($type == 'd') return false; 201 if(preg_match('#\.txt$#',$file)){ 202 //check ACL 203 $id = pathID($file); 204 if(auth_quickaclcheck($id) < AUTH_READ){ 205 return false; 206 } 207 $data[]['id'] = $id;; 208 } 209 return false; 210} 211 212/** 213 * Quicksearch for searching matching pagenames 214 * 215 * $opts['query'] is the search query 216 * 217 * @author Andreas Gohr <andi@splitbrain.org> 218 */ 219function search_pagename(&$data,$base,$file,$type,$lvl,$opts){ 220 //we do nothing with directories 221 if($type == 'd') return true; 222 //only search txt files 223 if(!preg_match('#\.txt$#',$file)) return true; 224 225 //simple stringmatching 226 if (!empty($opts['query'])){ 227 if(strpos($file,$opts['query']) !== false){ 228 //check ACL 229 $id = pathID($file); 230 if(auth_quickaclcheck($id) < AUTH_READ){ 231 return false; 232 } 233 $data[]['id'] = $id; 234 } 235 } 236 return true; 237} 238 239/** 240 * Just lists all documents 241 * 242 * @author Andreas Gohr <andi@splitbrain.org> 243 */ 244function search_allpages(&$data,$base,$file,$type,$lvl,$opts){ 245 //we do nothing with directories 246 if($type == 'd') return true; 247 //only search txt files 248 if(!preg_match('#\.txt$#',$file)) return true; 249 250 $data[]['id'] = pathID($file); 251 return true; 252} 253 254/** 255 * Search for backlinks to a given page 256 * 257 * $opts['ns'] namespace of the page 258 * $opts['name'] name of the page without namespace 259 * 260 * @author Andreas Gohr <andi@splitbrain.org> 261 * @deprecated Replaced by ft_backlinks() 262 */ 263function search_backlinks(&$data,$base,$file,$type,$lvl,$opts){ 264 //we do nothing with directories 265 if($type == 'd') return true;; 266 //only search txt files 267 if(!preg_match('#\.txt$#',$file)) return true;; 268 269 //absolute search id 270 $sid = cleanID($opts['ns'].':'.$opts['name']); 271 272 //current id and namespace 273 $cid = pathID($file); 274 $cns = getNS($cid); 275 276 //check ACL 277 if(auth_quickaclcheck($cid) < AUTH_READ){ 278 return false; 279 } 280 281 //fetch instructions 282 require_once(DOKU_INC.'inc/parserutils.php'); 283 $instructions = p_cached_instructions($base.$file,true); 284 if(is_null($instructions)) return false; 285 286 //check all links for match 287 foreach($instructions as $ins){ 288 if($ins[0] == 'internallink' || ($conf['camelcase'] && $ins[0] == 'camelcaselink') ){ 289 $mid = $ins[1][0]; 290 resolve_pageid($cns,$mid,$exists); //exists is not used 291 if($mid == $sid){ 292 //we have a match - finish 293 $data[]['id'] = $cid; 294 break; 295 } 296 } 297 } 298 299 return false; 300} 301 302/** 303 * Fulltextsearch 304 * 305 * $opts['query'] is the search query 306 * 307 * @author Andreas Gohr <andi@splitbrain.org> 308 * @deprecated - fulltext indexer is used instead 309 */ 310function search_fulltext(&$data,$base,$file,$type,$lvl,$opts){ 311 //we do nothing with directories 312 if($type == 'd') return true;; 313 //only search txt files 314 if(!preg_match('#\.txt$#',$file)) return true;; 315 316 //check ACL 317 $id = pathID($file); 318 if(auth_quickaclcheck($id) < AUTH_READ){ 319 return false; 320 } 321 322 //create regexp from queries 323 $poswords = array(); 324 $negwords = array(); 325 $qpreg = preg_split('/\s+/',$opts['query']); 326 327 foreach($qpreg as $word){ 328 switch(substr($word,0,1)){ 329 case '-': 330 if(strlen($word) > 1){ // catch single '-' 331 array_push($negwords,preg_quote(substr($word,1),'#')); 332 } 333 break; 334 case '+': 335 if(strlen($word) > 1){ // catch single '+' 336 array_push($poswords,preg_quote(substr($word,1),'#')); 337 } 338 break; 339 default: 340 array_push($poswords,preg_quote($word,'#')); 341 break; 342 } 343 } 344 345 // a search without any posword is useless 346 if (!count($poswords)) return true; 347 348 $reg = '^(?=.*?'.join(')(?=.*?',$poswords).')'; 349 $reg .= count($negwords) ? '((?!'.join('|',$negwords).').)*$' : '.*$'; 350 search_regex($data,$base,$file,$reg,$poswords); 351 return true; 352} 353 354/** 355 * Reference search 356 * This fuction searches for existing references to a given media file 357 * and returns an array with the found pages. It doesn't pay any 358 * attention to ACL permissions to find every reference. The caller 359 * must check if the user has the appropriate rights to see the found 360 * page and eventually have to prevent the result from displaying. 361 * 362 * @param array $data Reference to the result data structure 363 * @param string $base Base usually $conf['datadir'] 364 * @param string $file current file or directory relative to $base 365 * @param char $type Type either 'd' for directory or 'f' for file 366 * @param int $lvl Current recursion depht 367 * @param mixed $opts option array as given to search() 368 * 369 * $opts['query'] is the demanded media file name 370 * 371 * @author Andreas Gohr <andi@splitbrain.org> 372 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net> 373 */ 374function search_reference(&$data,$base,$file,$type,$lvl,$opts){ 375 global $conf; 376 377 //we do nothing with directories 378 if($type == 'd') return true; 379 380 //only search txt files 381 if(!preg_match('#\.txt$#',$file)) return true; 382 383 //we finish after 'cnt' references found. The return value 384 //'false' will skip subdirectories to speed search up. 385 $cnt = $conf['refshow'] > 0 ? $conf['refshow'] : 1; 386 if(count($data) >= $cnt) return false; 387 388 $reg = '\{\{ *\:?'.$opts['query'].' *(\|.*)?\}\}'; 389 search_regex($data,$base,$file,$reg,array($opts['query'])); 390 return true; 391} 392 393/* ------------- helper functions below -------------- */ 394 395/** 396 * fulltext search helper 397 * searches a text file with a given regular expression 398 * no ACL checks are performed. This have to be done by 399 * the caller if necessary. 400 * 401 * @param array $data reference to array for results 402 * @param string $base base directory 403 * @param string $file file name to search in 404 * @param string $reg regular expression to search for 405 * @param array $words words that should be marked in the results 406 * 407 * @author Andreas Gohr <andi@splitbrain.org> 408 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net> 409 * 410 * @deprecated - fulltext indexer is used instead 411 */ 412function search_regex(&$data,$base,$file,$reg,$words){ 413 414 //get text 415 $text = io_readfile($base.'/'.$file); 416 //lowercase text (u modifier does not help with case) 417 $lctext = utf8_strtolower($text); 418 419 //do the fulltext search 420 $matches = array(); 421 if($cnt = preg_match_all('#'.$reg.'#usi',$lctext,$matches)){ 422 //this is not the best way for snippet generation but the fastest I could find 423 $q = $words[0]; //use first word for snippet creation 424 $p = utf8_strpos($lctext,$q); 425 $f = $p - 100; 426 $l = utf8_strlen($q) + 200; 427 if($f < 0) $f = 0; 428 $snippet = '<span class="search_sep"> ... </span>'. 429 htmlspecialchars(utf8_substr($text,$f,$l)). 430 '<span class="search_sep"> ... </span>'; 431 $mark = '('.join('|', $words).')'; 432 $snippet = preg_replace('#'.$mark.'#si','<span class="search_hit">\\1</span>',$snippet); 433 434 $data[] = array( 435 'id' => pathID($file), 436 'count' => preg_match_all('#'.$mark.'#usi',$lctext,$matches), 437 'poswords' => join(' ',$words), 438 'snippet' => $snippet, 439 ); 440 } 441 442 return true; 443} 444 445 446/** 447 * fulltext sort 448 * 449 * Callback sort function for use with usort to sort the data 450 * structure created by search_fulltext. Sorts descending by count 451 * 452 * @author Andreas Gohr <andi@splitbrain.org> 453 */ 454function sort_search_fulltext($a,$b){ 455 if($a['count'] > $b['count']){ 456 return -1; 457 }elseif($a['count'] < $b['count']){ 458 return 1; 459 }else{ 460 return strcmp($a['id'],$b['id']); 461 } 462} 463 464/** 465 * translates a document path to an ID 466 * 467 * @author Andreas Gohr <andi@splitbrain.org> 468 * @todo move to pageutils 469 */ 470function pathID($path,$keeptxt=false){ 471 $id = utf8_decodeFN($path); 472 $id = str_replace('/',':',$id); 473 if(!$keeptxt) $id = preg_replace('#\.txt$#','',$id); 474 $id = preg_replace('#^:+#','',$id); 475 $id = preg_replace('#:+$#','',$id); 476 return $id; 477} 478 479 480//Setup VIM: ex: et ts=2 enc=utf-8 : 481