1<? 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 require_once("inc/common.php"); 10 11/** 12 * recurse direcory 13 * 14 * This function recurses into a given base directory 15 * and calls the supplied function for each file and directory 16 * 17 * @author Andreas Gohr <andi@splitbrain.org> 18 */ 19function search(&$data,$base,$func,$opts,$dir='',$lvl=1){ 20 $dirs = array(); 21 $files = array(); 22 23 //read in directories and files 24 $dh = @opendir($base.'/'.$dir); 25 if(!$dh) return; 26 while(($file = readdir($dh)) !== false){ 27 if(preg_match('/^\./',$file)) continue; //skip hidden files and upper dirs 28 if(is_dir($base.'/'.$dir.'/'.$file)){ 29 $dirs[] = $dir.'/'.$file; 30 continue; 31 } 32 $files[] = $dir.'/'.$file; 33 } 34 closedir($dh); 35 sort($files); 36 sort($dirs); 37 38 //give directories to userfunction then recurse 39 foreach($dirs as $dir){ 40 if ($func($data,$base,$dir,'d',$lvl,$opts)){ 41 search($data,$base,$func,$opts,$dir,$lvl+1); 42 } 43 } 44 //now handle the files 45 foreach($files as $file){ 46 $func($data,$base,$file,'f',$lvl,$opts); 47 } 48} 49 50/** 51 * The following functions are userfunctions to use with the search 52 * function above. This function is called for every found file or 53 * directory. When a directory is given to the function it has to 54 * decide if this directory should be traversed (true) or not (false) 55 * The function has to accept the following parameters: 56 * 57 * &$data - Reference to the result data structure 58 * $base - Base usually $conf['datadir'] 59 * $file - current file or directory relative to $base 60 * $type - Type either 'd' for directory or 'f' for file 61 * $lvl - Current recursion depht 62 * $opts - option array as given to search() 63 * 64 * return values for files are ignored 65 * 66 * All functions should check the ACL for document READ rights 67 * namespaces (directories) are NOT checked as this would break 68 * the recursion (You can have an nonreadable dir over a readable 69 * one deeper nested) 70 */ 71 72/** 73 * Build the browsable index of pages 74 * 75 * $opts['ns'] is the current namespace 76 * 77 * @author Andreas Gohr <andi@splitbrain.org> 78 */ 79function search_index(&$data,$base,$file,$type,$lvl,$opts){ 80 $return = true; 81 82 if($type == 'd' && !preg_match('#^'.$file.'(/|$)#','/'.$opts['ns'])){ 83 //add but don't recurse 84 $return = false; 85 }elseif($type == 'f' && !preg_match('#\.txt$#',$file)){ 86 //don't add 87 return false; 88 } 89 90 //check ACL 91 $id = pathID($file); 92 if($type=='f' && auth_quickaclcheck($id) < AUTH_READ){ 93 return false; 94 } 95 96 $data[]=array( 'id' => $id, 97 'type' => $type, 98 'level' => $lvl ); 99 return $return; 100} 101 102/** 103 * List all namespaces 104 * 105 * @author Andreas Gohr <andi@splitbrain.org> 106 */ 107function search_namespaces(&$data,$base,$file,$type,$lvl,$opts){ 108 if($type == 'f') return true; //nothing to do on files 109 110 $id = pathID($file); 111 $data[]=array( 'id' => $id, 112 'type' => $type, 113 'level' => $lvl ); 114 return true; 115} 116 117/** 118 * List all mediafiles in a namespace 119 * 120 * @author Andreas Gohr <andi@splitbrain.org> 121 */ 122function search_media(&$data,$base,$file,$type,$lvl,$opts){ 123 //we do nothing with directories 124 if($type == 'd') return false; 125 126 $info = array(); 127 $info['id'] = pathID($file); 128 129 //check ACL for namespace (we have no ACL for mediafiles) 130 if(auth_quickaclcheck(getNS($info['id']).':*') < AUTH_READ){ 131 return false; 132 } 133 134 $info['file'] = basename($file); 135 $info['size'] = filesize($base.'/'.$file); 136 if(preg_match("/\.(jpe?g|gif|png)$/",$file)){ 137 $info['isimg'] = true; 138 $info['info'] = getimagesize($base.'/'.$file); 139 }else{ 140 $info['isimg'] = false; 141 } 142 $data[] = $info; 143 144 return false; 145} 146 147/** 148 * This function just lists documents (for RSS namespace export) 149 * 150 * @author Andreas Gohr <andi@splitbrain.org> 151 */ 152function search_list(&$data,$base,$file,$type,$lvl,$opts){ 153 //we do nothing with directories 154 if($type == 'd') return false; 155 if(preg_match('#\.txt$#',$file)){ 156 //check ACL 157 $id = pathID($file); 158 if(auth_quickaclcheck($id) < AUTH_READ){ 159 return false; 160 } 161 $data[]['id'] = $id;; 162 } 163 return false; 164} 165 166/** 167 * Quicksearch for searching matching pagenames 168 * 169 * $opts['query'] is the search query 170 * 171 * @author Andreas Gohr <andi@splitbrain.org> 172 */ 173function search_pagename(&$data,$base,$file,$type,$lvl,$opts){ 174 //we do nothing with directories 175 if($type == 'd') return true; 176 //only search txt files 177 if(!preg_match('#\.txt$#',$file)) return true; 178 179 //simple stringmatching 180 if(strpos($file,$opts['query']) !== false){ 181 //check ACL 182 $id = pathID($file); 183 if(auth_quickaclcheck($id) < AUTH_READ){ 184 return false; 185 } 186 $data[]['id'] = $id; 187 } 188 189 return true; 190} 191 192/** 193 * Search for backlinks to a given page 194 * 195 * $opts['ns'] namespace of the page 196 * $opts['name'] name of the page without namespace 197 * 198 * @author Andreas Gohr <andi@splitbrain.org> 199 */ 200function search_backlinks(&$data,$base,$file,$type,$lvl,$opts){ 201 //we do nothing with directories 202 if($type == 'd') return true;; 203 //only search txt files 204 if(!preg_match('#\.txt$#',$file)) return true;; 205 206 //get text 207 $text = io_readfile($base.'/'.$file); 208 209 //absolute search id 210 $sid = cleanID($opts['ns'].':'.$opts['name']); 211 212 //construct current namespace 213 $cid = pathID($file); 214 $cns = getNS($cid); 215 216 //check ACL 217 if(auth_quickaclcheck($cid) < AUTH_READ){ 218 return false; 219 } 220 221 //match all links 222 //FIXME may be incorrect because of code blocks 223 // CamelCase isn't supported, too 224 preg_match_all('#\[\[(.+?)\]\]#si',$text,$matches,PREG_SET_ORDER); 225 foreach($matches as $match){ 226 //get ID from link and discard most non wikilinks 227 list($mid) = split('\|',$match[1],2); 228 if(preg_match("#^(https?|telnet|gopher|file|wais|ftp|ed2k|irc)://#",$mid)) continue; 229 if(preg_match("#\w+>#",$mid)) continue; 230 $mns = getNS($mid); 231 //namespace starting with "." - prepend current namespace 232 if(strpos($mns,'.')===0){ 233 $mid = $cns.":".substr($mid,1); 234 } 235 if($mns===false){ 236 //no namespace in link? add current 237 $mid = "$cns:$mid"; 238 } 239 $mid = cleanID($mid); 240 241 if ($mid == $sid){ 242 $data[]['id'] = $cid; 243 break; 244 } 245 } 246} 247 248/** 249 * Fulltextsearch 250 * 251 * $opts['query'] is the search query 252 * 253 * @author Andreas Gohr <andi@splitbrain.org> 254 */ 255function search_fulltext(&$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 //check ACL 262 $id = pathID($file); 263 if(auth_quickaclcheck($id) < AUTH_READ){ 264 return false; 265 } 266 267 //get text 268 $text = io_readfile($base.'/'.$file); 269 270 //create regexp from queries 271 $qpreg = preg_split('/\s+/',preg_quote($opts['query'],'#')); 272 $qpreg = '('.join('|',$qpreg).')'; 273 274 //do the fulltext search 275 $matches = array(); 276 if($cnt = preg_match_all('#'.$qpreg.'#si',$text,$matches)){ 277 //this is not the best way for snippet generation but the fastest I could find 278 //split query and only use the first token 279 $q = preg_split('/\s+/',$opts['query'],2); 280 $q = $q[0]; 281 $p = strpos(strtolower($text),$q); 282 $f = $p - 100; 283 $l = strlen($q) + 200; 284 if($f < 0) $f = 0; 285 $snippet = '<span class="search_sep"> ... </span>'. 286 htmlspecialchars(substr($text,$f,$l)). 287 '<span class="search_sep"> ... </span>'; 288 $snippet = preg_replace('#'.$qpreg.'#si','<span class="search_hit">\\1</span>',$snippet); 289 290 $data[] = array( 291 'id' => $id, 292 'count' => $cnt, 293 'snippet' => $snippet, 294 ); 295 } 296 297 return true; 298} 299 300/** 301 * fulltext sort 302 * 303 * Callback sort function for use with usort to sort the data 304 * structure created by search_fulltext. Sorts descending by count 305 * 306 * @author Andreas Gohr <andi@splitbrain.org> 307 */ 308function sort_search_fulltext($a,$b){ 309 if($a['count'] > $b['count']){ 310 return -1; 311 }elseif($a['count'] < $b['count']){ 312 return 1; 313 }else{ 314 return strcmp($a['id'],$b['id']); 315 } 316} 317 318/** 319 * translates a document path to an ID 320 * 321 * @author Andreas Gohr <andi@splitbrain.org> 322 */ 323function pathID($path){ 324 $id = str_replace('/',':',$path); 325 $id = preg_replace('#\.txt$#','',$id); 326 $id = preg_replace('#^:+#','',$id); 327 $id = preg_replace('#:+$#','',$id); 328 return $id; 329} 330 331?> 332