1ed7b5f09Sandi<?php 215fae107Sandi/** 315fae107Sandi * DokuWiki search functions 415fae107Sandi * 515fae107Sandi * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 715fae107Sandi */ 8f3f0262cSandi 9ed7b5f09Sandi if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/'); 10ed7b5f09Sandi require_once(DOKU_INC.'inc/common.php'); 11f3f0262cSandi 12f3f0262cSandi/** 1315fae107Sandi * recurse direcory 1415fae107Sandi * 15f3f0262cSandi * This function recurses into a given base directory 16f3f0262cSandi * and calls the supplied function for each file and directory 1715fae107Sandi * 1815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 19f3f0262cSandi */ 20f3f0262cSandifunction search(&$data,$base,$func,$opts,$dir='',$lvl=1){ 21f3f0262cSandi $dirs = array(); 22f3f0262cSandi $files = array(); 23f3f0262cSandi 24f3f0262cSandi //read in directories and files 25f3f0262cSandi $dh = @opendir($base.'/'.$dir); 26f3f0262cSandi if(!$dh) return; 27f3f0262cSandi while(($file = readdir($dh)) !== false){ 28de3dfc91Sandi if(preg_match('/^[\._]/',$file)) continue; //skip hidden files and upper dirs 29f3f0262cSandi if(is_dir($base.'/'.$dir.'/'.$file)){ 30f3f0262cSandi $dirs[] = $dir.'/'.$file; 31f3f0262cSandi continue; 3264f50cdbSandi }elseif(substr($file,-5) == '.lock'){ 3364f50cdbSandi //skip lockfiles 3464f50cdbSandi continue; 35f3f0262cSandi } 36f3f0262cSandi $files[] = $dir.'/'.$file; 37f3f0262cSandi } 38f3f0262cSandi closedir($dh); 39f3f0262cSandi sort($files); 40f3f0262cSandi sort($dirs); 41f3f0262cSandi 42f3f0262cSandi //give directories to userfunction then recurse 43f3f0262cSandi foreach($dirs as $dir){ 44f3f0262cSandi if ($func($data,$base,$dir,'d',$lvl,$opts)){ 45f3f0262cSandi search($data,$base,$func,$opts,$dir,$lvl+1); 46f3f0262cSandi } 47f3f0262cSandi } 48f3f0262cSandi //now handle the files 49f3f0262cSandi foreach($files as $file){ 50f3f0262cSandi $func($data,$base,$file,'f',$lvl,$opts); 51f3f0262cSandi } 52f3f0262cSandi} 53f3f0262cSandi 54f3f0262cSandi/** 55f3f0262cSandi * The following functions are userfunctions to use with the search 56f3f0262cSandi * function above. This function is called for every found file or 57f3f0262cSandi * directory. When a directory is given to the function it has to 58f3f0262cSandi * decide if this directory should be traversed (true) or not (false) 59f3f0262cSandi * The function has to accept the following parameters: 60f3f0262cSandi * 61f3f0262cSandi * &$data - Reference to the result data structure 62f3f0262cSandi * $base - Base usually $conf['datadir'] 63f3f0262cSandi * $file - current file or directory relative to $base 64f3f0262cSandi * $type - Type either 'd' for directory or 'f' for file 65f3f0262cSandi * $lvl - Current recursion depht 66f3f0262cSandi * $opts - option array as given to search() 67f3f0262cSandi * 68f3f0262cSandi * return values for files are ignored 69f3f0262cSandi * 70f3f0262cSandi * All functions should check the ACL for document READ rights 71f3f0262cSandi * namespaces (directories) are NOT checked as this would break 72f3f0262cSandi * the recursion (You can have an nonreadable dir over a readable 73f3f0262cSandi * one deeper nested) 74f3f0262cSandi */ 75f3f0262cSandi 76f3f0262cSandi/** 7763f2400bSandi * Searches for pages beginning with the given query 7863f2400bSandi * 7963f2400bSandi * @author Andreas Gohr <andi@splitbrain.org> 8063f2400bSandi */ 8163f2400bSandifunction search_qsearch(&$data,$base,$file,$type,$lvl,$opts){ 8263f2400bSandi $item = array(); 8363f2400bSandi 8463f2400bSandi if($type == 'd'){ 8563f2400bSandi return false; //no handling yet 8663f2400bSandi } 8763f2400bSandi 8863f2400bSandi //get id 8963f2400bSandi $id = pathID($file); 9063f2400bSandi 9163f2400bSandi //check if it matches the query 9263f2400bSandi if(!preg_match('/^'.preg_quote($opts['query'],'/').'/u',$id)){ 9363f2400bSandi return false; 9463f2400bSandi } 9563f2400bSandi 9663f2400bSandi //check ACL 9763f2400bSandi if(auth_quickaclcheck($id) < AUTH_READ){ 9863f2400bSandi return false; 9963f2400bSandi } 10063f2400bSandi 10163f2400bSandi $data[]=array( 'id' => $id, 10263f2400bSandi 'type' => $type, 10363f2400bSandi 'level' => 1, 10463f2400bSandi 'open' => true); 10563f2400bSandi return true; 10663f2400bSandi} 10763f2400bSandi 10863f2400bSandi/** 10915fae107Sandi * Build the browsable index of pages 110f3f0262cSandi * 111f3f0262cSandi * $opts['ns'] is the current namespace 11215fae107Sandi * 11315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 114f3f0262cSandi */ 115f3f0262cSandifunction search_index(&$data,$base,$file,$type,$lvl,$opts){ 116f3f0262cSandi $return = true; 117f3f0262cSandi 118cb70c441Sandi $item = array(); 119cb70c441Sandi 120f3f0262cSandi if($type == 'd' && !preg_match('#^'.$file.'(/|$)#','/'.$opts['ns'])){ 121f3f0262cSandi //add but don't recurse 122f3f0262cSandi $return = false; 123f3f0262cSandi }elseif($type == 'f' && !preg_match('#\.txt$#',$file)){ 124f3f0262cSandi //don't add 125f3f0262cSandi return false; 126f3f0262cSandi } 127f3f0262cSandi 128f3f0262cSandi //check ACL 129f3f0262cSandi $id = pathID($file); 130f3f0262cSandi if($type=='f' && auth_quickaclcheck($id) < AUTH_READ){ 131f3f0262cSandi return false; 132f3f0262cSandi } 133f3f0262cSandi 134f3f0262cSandi $data[]=array( 'id' => $id, 135f3f0262cSandi 'type' => $type, 136cb70c441Sandi 'level' => $lvl, 137cb70c441Sandi 'open' => $return ); 138f3f0262cSandi return $return; 139f3f0262cSandi} 140f3f0262cSandi 141f3f0262cSandi/** 14215fae107Sandi * List all namespaces 14315fae107Sandi * 14415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 145f3f0262cSandi */ 146f3f0262cSandifunction search_namespaces(&$data,$base,$file,$type,$lvl,$opts){ 147f3f0262cSandi if($type == 'f') return true; //nothing to do on files 148f3f0262cSandi 149f3f0262cSandi $id = pathID($file); 150f3f0262cSandi $data[]=array( 'id' => $id, 151f3f0262cSandi 'type' => $type, 152f3f0262cSandi 'level' => $lvl ); 153f3f0262cSandi return true; 154f3f0262cSandi} 155f3f0262cSandi 156f3f0262cSandi/** 15715fae107Sandi * List all mediafiles in a namespace 15815fae107Sandi * 15915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 160f3f0262cSandi */ 161f3f0262cSandifunction search_media(&$data,$base,$file,$type,$lvl,$opts){ 162f3f0262cSandi //we do nothing with directories 163f3f0262cSandi if($type == 'd') return false; 164f3f0262cSandi 165f3f0262cSandi $info = array(); 166156a608cSandi $info['id'] = pathID($file,true); 167f3f0262cSandi 168f3f0262cSandi //check ACL for namespace (we have no ACL for mediafiles) 169f3f0262cSandi if(auth_quickaclcheck(getNS($info['id']).':*') < AUTH_READ){ 170f3f0262cSandi return false; 171f3f0262cSandi } 172f3f0262cSandi 173f3f0262cSandi $info['file'] = basename($file); 174f3f0262cSandi $info['size'] = filesize($base.'/'.$file); 175f3f0262cSandi if(preg_match("/\.(jpe?g|gif|png)$/",$file)){ 176f3f0262cSandi $info['isimg'] = true; 177f3f0262cSandi $info['info'] = getimagesize($base.'/'.$file); 178f3f0262cSandi }else{ 179f3f0262cSandi $info['isimg'] = false; 180f3f0262cSandi } 181f3f0262cSandi $data[] = $info; 182f3f0262cSandi 183f3f0262cSandi return false; 184f3f0262cSandi} 185f3f0262cSandi 186f3f0262cSandi/** 187f3f0262cSandi * This function just lists documents (for RSS namespace export) 18815fae107Sandi * 18915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 190f3f0262cSandi */ 191f3f0262cSandifunction search_list(&$data,$base,$file,$type,$lvl,$opts){ 192f3f0262cSandi //we do nothing with directories 193f3f0262cSandi if($type == 'd') return false; 194f3f0262cSandi if(preg_match('#\.txt$#',$file)){ 195f3f0262cSandi //check ACL 196f3f0262cSandi $id = pathID($file); 197f3f0262cSandi if(auth_quickaclcheck($id) < AUTH_READ){ 198f3f0262cSandi return false; 199f3f0262cSandi } 200f3f0262cSandi $data[]['id'] = $id;; 201f3f0262cSandi } 202f3f0262cSandi return false; 203f3f0262cSandi} 204f3f0262cSandi 205f3f0262cSandi/** 206f3f0262cSandi * Quicksearch for searching matching pagenames 207f3f0262cSandi * 208f3f0262cSandi * $opts['query'] is the search query 20915fae107Sandi * 21015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 211f3f0262cSandi */ 212f3f0262cSandifunction search_pagename(&$data,$base,$file,$type,$lvl,$opts){ 213f3f0262cSandi //we do nothing with directories 214f3f0262cSandi if($type == 'd') return true; 215f3f0262cSandi //only search txt files 216f3f0262cSandi if(!preg_match('#\.txt$#',$file)) return true; 217f3f0262cSandi 218f3f0262cSandi //simple stringmatching 219f3f0262cSandi if(strpos($file,$opts['query']) !== false){ 220f3f0262cSandi //check ACL 221f3f0262cSandi $id = pathID($file); 222f3f0262cSandi if(auth_quickaclcheck($id) < AUTH_READ){ 223f3f0262cSandi return false; 224f3f0262cSandi } 225f3f0262cSandi $data[]['id'] = $id; 226f3f0262cSandi } 227f3f0262cSandi 228f3f0262cSandi return true; 229f3f0262cSandi} 230f3f0262cSandi 231f3f0262cSandi/** 232f3f0262cSandi * Search for backlinks to a given page 233f3f0262cSandi * 234f3f0262cSandi * $opts['ns'] namespace of the page 235f3f0262cSandi * $opts['name'] name of the page without namespace 23615fae107Sandi * 23715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 238f3f0262cSandi */ 239f3f0262cSandifunction search_backlinks(&$data,$base,$file,$type,$lvl,$opts){ 240f3f0262cSandi //we do nothing with directories 241f3f0262cSandi if($type == 'd') return true;; 242f3f0262cSandi //only search txt files 243f3f0262cSandi if(!preg_match('#\.txt$#',$file)) return true;; 244f3f0262cSandi 245f3f0262cSandi //absolute search id 246f3f0262cSandi $sid = cleanID($opts['ns'].':'.$opts['name']); 247f3f0262cSandi 24837e34a5eSandi //current id and namespace 249f3f0262cSandi $cid = pathID($file); 250f3f0262cSandi $cns = getNS($cid); 251f3f0262cSandi 252f3f0262cSandi //check ACL 253f3f0262cSandi if(auth_quickaclcheck($cid) < AUTH_READ){ 254f3f0262cSandi return false; 255f3f0262cSandi } 256f3f0262cSandi 25737e34a5eSandi //fetch instructions 25837e34a5eSandi require_once(DOKU_INC.'inc/parserutils.php'); 25937e34a5eSandi $instructions = p_cached_instructions($base.$file,true); 26037e34a5eSandi if(is_null($instructions)) return false; 261f3f0262cSandi 26237e34a5eSandi //check all links for match 26337e34a5eSandi foreach($instructions as $ins){ 26437e34a5eSandi if($ins[0] == 'internallink' || ($conf['camelcase'] && $ins[0] == 'camelcaselink') ){ 26537e34a5eSandi $mid = $ins[1][0]; 26637e34a5eSandi resolve_pageid($cns,$mid,$exists); //exists is not used 267f3f0262cSandi if($mid == $sid){ 26837e34a5eSandi //we have a match - finish 269f3f0262cSandi $data[]['id'] = $cid; 270f3f0262cSandi break; 271f3f0262cSandi } 272f3f0262cSandi } 273f3f0262cSandi } 274f3f0262cSandi 27537e34a5eSandi return false; 27637e34a5eSandi} 27737e34a5eSandi 278f3f0262cSandi/** 279f3f0262cSandi * Fulltextsearch 280f3f0262cSandi * 281f3f0262cSandi * $opts['query'] is the search query 28215fae107Sandi * 28315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 284f3f0262cSandi */ 285f3f0262cSandifunction search_fulltext(&$data,$base,$file,$type,$lvl,$opts){ 286f3f0262cSandi //we do nothing with directories 287f3f0262cSandi if($type == 'd') return true;; 288f3f0262cSandi //only search txt files 289f3f0262cSandi if(!preg_match('#\.txt$#',$file)) return true;; 290f3f0262cSandi 291f3f0262cSandi //check ACL 292f3f0262cSandi $id = pathID($file); 293f3f0262cSandi if(auth_quickaclcheck($id) < AUTH_READ){ 294f3f0262cSandi return false; 295f3f0262cSandi } 296f3f0262cSandi 297f3f0262cSandi //get text 298f3f0262cSandi $text = io_readfile($base.'/'.$file); 299d5a2a500Sandi //lowercase text (u modifier does not help with case) 300d5a2a500Sandi $lctext = utf8_strtolower($text); 301f3f0262cSandi 302f3f0262cSandi //create regexp from queries 3035ef370d2Smatthiasgrimm $poswords = array(); 3045ef370d2Smatthiasgrimm $negwords = array(); 3055ef370d2Smatthiasgrimm $qpreg = preg_split('/\s+/',$opts['query']); 3065ef370d2Smatthiasgrimm 3075ef370d2Smatthiasgrimm foreach($qpreg as $word){ 3085ef370d2Smatthiasgrimm switch(substr($word,0,1)){ 3095ef370d2Smatthiasgrimm case '-': 3105ef370d2Smatthiasgrimm array_push($negwords,preg_quote(substr($word,1),'#')); 3115ef370d2Smatthiasgrimm break; 3125ef370d2Smatthiasgrimm case '+': 3135ef370d2Smatthiasgrimm array_push($poswords,preg_quote(substr($word,1),'#')); 3145ef370d2Smatthiasgrimm break; 3155ef370d2Smatthiasgrimm default: 3165ef370d2Smatthiasgrimm array_push($poswords,preg_quote($word,'#')); 3175ef370d2Smatthiasgrimm break; 3185ef370d2Smatthiasgrimm } 3195ef370d2Smatthiasgrimm } 320248a7321Smatthiasgrimm 321248a7321Smatthiasgrimm // a search without any posword is useless 322248a7321Smatthiasgrimm if (!count($poswords)) return true; 3235ef370d2Smatthiasgrimm 324*5a5d942dSmatthiasgrimm $reg = '^(?=.*?'.join(')(?=.*?',$poswords).')'; 3255ef370d2Smatthiasgrimm $reg .= count($negwords) ? '((?!'.join('|',$negwords).').)*$' : '.*$'; 326f3f0262cSandi 327f3f0262cSandi //do the fulltext search 328f3f0262cSandi $matches = array(); 3295ef370d2Smatthiasgrimm if($cnt = preg_match_all('#'.$reg.'#usi',$lctext,$matches)){ 330f3f0262cSandi //this is not the best way for snippet generation but the fastest I could find 331*5a5d942dSmatthiasgrimm $q = $poswords[0]; //use first posword for snippet 332d5a2a500Sandi $p = utf8_strpos($lctext,$q); 333f3f0262cSandi $f = $p - 100; 334d5a2a500Sandi $l = utf8_strlen($q) + 200; 335f3f0262cSandi if($f < 0) $f = 0; 336f3f0262cSandi $snippet = '<span class="search_sep"> ... </span>'. 337d5a2a500Sandi htmlspecialchars(utf8_substr($text,$f,$l)). 338f3f0262cSandi '<span class="search_sep"> ... </span>'; 339*5a5d942dSmatthiasgrimm $mark = '('.join('|',$poswords).')'; 3405ef370d2Smatthiasgrimm $snippet = preg_replace('#'.$mark.'#si','<span class="search_hit">\\1</span>',$snippet); 341f3f0262cSandi 342f3f0262cSandi $data[] = array( 343f3f0262cSandi 'id' => $id, 3445ef370d2Smatthiasgrimm 'count' => preg_match_all('#'.$mark.'#usi',$lctext,$matches), 3455ef370d2Smatthiasgrimm 'poswords' => join(' ',$poswords), 346f3f0262cSandi 'snippet' => $snippet, 347f3f0262cSandi ); 348f3f0262cSandi } 349f3f0262cSandi 350f3f0262cSandi return true; 351f3f0262cSandi} 352f3f0262cSandi 353f3f0262cSandi/** 35415fae107Sandi * fulltext sort 35515fae107Sandi * 356f3f0262cSandi * Callback sort function for use with usort to sort the data 357f3f0262cSandi * structure created by search_fulltext. Sorts descending by count 35815fae107Sandi * 35915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 360f3f0262cSandi */ 361f3f0262cSandifunction sort_search_fulltext($a,$b){ 362f3f0262cSandi if($a['count'] > $b['count']){ 363f3f0262cSandi return -1; 364f3f0262cSandi }elseif($a['count'] < $b['count']){ 365f3f0262cSandi return 1; 366f3f0262cSandi }else{ 367f3f0262cSandi return strcmp($a['id'],$b['id']); 368f3f0262cSandi } 369f3f0262cSandi} 370f3f0262cSandi 371f3f0262cSandi/** 372f3f0262cSandi * translates a document path to an ID 37315fae107Sandi * 37415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 37537e34a5eSandi * @todo move to pageutils 376f3f0262cSandi */ 377156a608cSandifunction pathID($path,$keeptxt=false){ 37849c713a3Sandi $id = utf8_decodeFN($path); 37949c713a3Sandi $id = str_replace('/',':',$id); 380156a608cSandi if(!$keeptxt) $id = preg_replace('#\.txt$#','',$id); 381f3f0262cSandi $id = preg_replace('#^:+#','',$id); 382f3f0262cSandi $id = preg_replace('#:+$#','',$id); 383f3f0262cSandi return $id; 384f3f0262cSandi} 385f3f0262cSandi 386340756e4Sandi 387340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 : 388