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 9fa8adffeSAndreas Gohrif(!defined('DOKU_INC')) die('meh.'); 10f3f0262cSandi 11f3f0262cSandi/** 1215fae107Sandi * recurse direcory 1315fae107Sandi * 14f3f0262cSandi * This function recurses into a given base directory 15f3f0262cSandi * and calls the supplied function for each file and directory 1615fae107Sandi * 1724baa045SAndreas Gohr * @param array ref $data The results of the search are stored here 1824baa045SAndreas Gohr * @param string $base Where to start the search 1924baa045SAndreas Gohr * @param callback $func Callback (function name or arayy with object,method) 2024baa045SAndreas Gohr * @param string $dir Current directory beyond $base 2124baa045SAndreas Gohr * @param int $lvl Recursion Level 2215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 23f3f0262cSandi */ 24f3f0262cSandifunction search(&$data,$base,$func,$opts,$dir='',$lvl=1){ 25f3f0262cSandi $dirs = array(); 26f3f0262cSandi $files = array(); 27f3f0262cSandi 28f3f0262cSandi //read in directories and files 29f3f0262cSandi $dh = @opendir($base.'/'.$dir); 30f3f0262cSandi if(!$dh) return; 31f3f0262cSandi while(($file = readdir($dh)) !== false){ 32de3dfc91Sandi if(preg_match('/^[\._]/',$file)) continue; //skip hidden files and upper dirs 33f3f0262cSandi if(is_dir($base.'/'.$dir.'/'.$file)){ 34f3f0262cSandi $dirs[] = $dir.'/'.$file; 35f3f0262cSandi continue; 36f3f0262cSandi } 37f3f0262cSandi $files[] = $dir.'/'.$file; 38f3f0262cSandi } 39f3f0262cSandi closedir($dh); 40f3f0262cSandi sort($files); 41f3f0262cSandi sort($dirs); 42f3f0262cSandi 43f3f0262cSandi //give directories to userfunction then recurse 44f3f0262cSandi foreach($dirs as $dir){ 45d8126df2SGina Haeussge if (call_user_func_array($func, array(&$data,$base,$dir,'d',$lvl,$opts))){ 46f3f0262cSandi search($data,$base,$func,$opts,$dir,$lvl+1); 47f3f0262cSandi } 48f3f0262cSandi } 49f3f0262cSandi //now handle the files 50f3f0262cSandi foreach($files as $file){ 51d8126df2SGina Haeussge call_user_func_array($func, array(&$data,$base,$file,'f',$lvl,$opts)); 52f3f0262cSandi } 53f3f0262cSandi} 54f3f0262cSandi 55f3f0262cSandi/** 56d8126df2SGina Haeussge * Wrapper around call_user_func_array. 5724baa045SAndreas Gohr * 58d8126df2SGina Haeussge * @deprecated 5924baa045SAndreas Gohr */ 6024baa045SAndreas Gohrfunction search_callback($func,&$data,$base,$file,$type,$lvl,$opts){ 61d8126df2SGina Haeussge return call_user_func_array($func, array(&$data,$base,$file,$type,$lvl,$opts)); 6224baa045SAndreas Gohr} 6324baa045SAndreas Gohr 6424baa045SAndreas Gohr/** 65f3f0262cSandi * The following functions are userfunctions to use with the search 66f3f0262cSandi * function above. This function is called for every found file or 67f3f0262cSandi * directory. When a directory is given to the function it has to 68f3f0262cSandi * decide if this directory should be traversed (true) or not (false) 69f3f0262cSandi * The function has to accept the following parameters: 70f3f0262cSandi * 71f3f0262cSandi * &$data - Reference to the result data structure 72f3f0262cSandi * $base - Base usually $conf['datadir'] 73f3f0262cSandi * $file - current file or directory relative to $base 74f3f0262cSandi * $type - Type either 'd' for directory or 'f' for file 75f3f0262cSandi * $lvl - Current recursion depht 76f3f0262cSandi * $opts - option array as given to search() 77f3f0262cSandi * 78f3f0262cSandi * return values for files are ignored 79f3f0262cSandi * 80f3f0262cSandi * All functions should check the ACL for document READ rights 81f3f0262cSandi * namespaces (directories) are NOT checked as this would break 82f3f0262cSandi * the recursion (You can have an nonreadable dir over a readable 830e1a261eSMichael Klier * one deeper nested) also make sure to check the file type (for example 840e1a261eSMichael Klier * in case of lockfiles). 85f3f0262cSandi */ 86f3f0262cSandi 87f3f0262cSandi/** 8863f2400bSandi * Searches for pages beginning with the given query 8963f2400bSandi * 9063f2400bSandi * @author Andreas Gohr <andi@splitbrain.org> 9163f2400bSandi */ 9263f2400bSandifunction search_qsearch(&$data,$base,$file,$type,$lvl,$opts){ 938705cc81SAndreas Gohr $opts = array( 948705cc81SAndreas Gohr 'idmatch' => '(^|:)'.preg_quote($opts['query'],'/').'/', 958705cc81SAndreas Gohr 'listfiles' => true, 968705cc81SAndreas Gohr 'pagesonly' => true, 978705cc81SAndreas Gohr ); 988705cc81SAndreas Gohr return search_universal($data,$base,$file,$type,$lvl,$opts); 9963f2400bSandi} 10063f2400bSandi 10163f2400bSandi/** 10215fae107Sandi * Build the browsable index of pages 103f3f0262cSandi * 104f3f0262cSandi * $opts['ns'] is the current namespace 10515fae107Sandi * 10615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 107f3f0262cSandi */ 108f3f0262cSandifunction search_index(&$data,$base,$file,$type,$lvl,$opts){ 109d1c7b6ecSAndreas Gohr global $conf; 110f3f0262cSandi $return = true; 111f3f0262cSandi 112cb70c441Sandi $item = array(); 113cb70c441Sandi 114f3f0262cSandi if($type == 'd' && !preg_match('#^'.$file.'(/|$)#','/'.$opts['ns'])){ 115f3f0262cSandi //add but don't recurse 116f3f0262cSandi $return = false; 1170e1a261eSMichael Klier }elseif($type == 'f' && ($opts['nofiles'] || substr($file,-4) != '.txt')){ 118f3f0262cSandi //don't add 119f3f0262cSandi return false; 120f3f0262cSandi } 121f3f0262cSandi 122*e63d421bSAndreas Gohr $id = pathID($file,($type == 'd')); 1230dc92c6fSAndreas Gohr 124d1c7b6ecSAndreas Gohr if($type=='d' && $conf['sneaky_index'] && auth_quickaclcheck($id.':') < AUTH_READ){ 125b54f94e0SAndreas Gohr return false; 126b54f94e0SAndreas Gohr } 127b54f94e0SAndreas Gohr 1280dc92c6fSAndreas Gohr //check hidden 1291211a7a9SMartin Tschofen if(isHiddenPage($id)){ 1300dc92c6fSAndreas Gohr return false; 1310dc92c6fSAndreas Gohr } 1320dc92c6fSAndreas Gohr 1330dc92c6fSAndreas Gohr //check ACL 134f3f0262cSandi if($type=='f' && auth_quickaclcheck($id) < AUTH_READ){ 135f3f0262cSandi return false; 136f3f0262cSandi } 137f3f0262cSandi 138f3f0262cSandi $data[]=array( 'id' => $id, 139f3f0262cSandi 'type' => $type, 140cb70c441Sandi 'level' => $lvl, 141cb70c441Sandi 'open' => $return ); 142f3f0262cSandi return $return; 143f3f0262cSandi} 144f3f0262cSandi 145f3f0262cSandi/** 14615fae107Sandi * List all namespaces 14715fae107Sandi * 14815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 149f3f0262cSandi */ 150f3f0262cSandifunction search_namespaces(&$data,$base,$file,$type,$lvl,$opts){ 1518705cc81SAndreas Gohr $opts = array( 1528705cc81SAndreas Gohr 'listdirs' => true, 1538705cc81SAndreas Gohr ); 1548705cc81SAndreas Gohr return search_universal($data,$base,$file,$type,$lvl,$opts); 155f3f0262cSandi} 156f3f0262cSandi 157f3f0262cSandi/** 15815fae107Sandi * List all mediafiles in a namespace 15915fae107Sandi * 16015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 161f3f0262cSandi */ 162f3f0262cSandifunction search_media(&$data,$base,$file,$type,$lvl,$opts){ 163b8219d2dSAndreas Gohr 164f3f0262cSandi //we do nothing with directories 1651a49ac65SGina Haeussge if($type == 'd') { 166224122cfSAndreas Gohr if(!$opts['depth']) return true; // recurse forever 16778315408SAndreas Gohr $depth = substr_count($file,'/'); 168b8219d2dSAndreas Gohr if($depth >= $opts['depth']) return false; // depth reached 169224122cfSAndreas Gohr return true; 1701a49ac65SGina Haeussge } 171f3f0262cSandi 172f3f0262cSandi $info = array(); 173156a608cSandi $info['id'] = pathID($file,true); 17464807c84SAndreas Gohr if($info['id'] != cleanID($info['id'])){ 17564807c84SAndreas Gohr if($opts['showmsg']) 17664807c84SAndreas Gohr msg(hsc($info['id']).' is not a valid file name for DokuWiki - skipped',-1); 17764807c84SAndreas Gohr return false; // skip non-valid files 17864807c84SAndreas Gohr } 179f3f0262cSandi 180f3f0262cSandi //check ACL for namespace (we have no ACL for mediafiles) 181224122cfSAndreas Gohr $info['perm'] = auth_quickaclcheck(getNS($info['id']).':*'); 182224122cfSAndreas Gohr if(!$opts['skipacl'] && $info['perm'] < AUTH_READ){ 183224122cfSAndreas Gohr return false; 184224122cfSAndreas Gohr } 185224122cfSAndreas Gohr 186224122cfSAndreas Gohr //check pattern filter 187224122cfSAndreas Gohr if($opts['pattern'] && !@preg_match($opts['pattern'], $info['id'])){ 188f3f0262cSandi return false; 189f3f0262cSandi } 190f3f0262cSandi 191f3f0262cSandi $info['file'] = basename($file); 192f3f0262cSandi $info['size'] = filesize($base.'/'.$file); 1935e7fa82eSAndreas Gohr $info['mtime'] = filemtime($base.'/'.$file); 1943df72098SAndreas Gohr $info['writable'] = is_writable($base.'/'.$file); 195f3f0262cSandi if(preg_match("/\.(jpe?g|gif|png)$/",$file)){ 196f3f0262cSandi $info['isimg'] = true; 19723a34783SAndreas Gohr $info['meta'] = new JpegMeta($base.'/'.$file); 198f3f0262cSandi }else{ 199f3f0262cSandi $info['isimg'] = false; 200f3f0262cSandi } 201224122cfSAndreas Gohr if($opts['hash']){ 202dfd343c4SAndreas Gohr $info['hash'] = md5(io_readFile(mediaFN($info['id']),false)); 203224122cfSAndreas Gohr } 204224122cfSAndreas Gohr 205f3f0262cSandi $data[] = $info; 206f3f0262cSandi 207f3f0262cSandi return false; 208f3f0262cSandi} 209f3f0262cSandi 210f3f0262cSandi/** 211f3f0262cSandi * This function just lists documents (for RSS namespace export) 21215fae107Sandi * 21315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 214f3f0262cSandi */ 215f3f0262cSandifunction search_list(&$data,$base,$file,$type,$lvl,$opts){ 216f3f0262cSandi //we do nothing with directories 217f3f0262cSandi if($type == 'd') return false; 2180e1a261eSMichael Klier //only search txt files 2190e1a261eSMichael Klier if(substr($file,-4) == '.txt'){ 220f3f0262cSandi //check ACL 221f3f0262cSandi $id = pathID($file); 222f3f0262cSandi if(auth_quickaclcheck($id) < AUTH_READ){ 223f3f0262cSandi return false; 224f3f0262cSandi } 2250e1a261eSMichael Klier $data[]['id'] = $id; 226f3f0262cSandi } 227f3f0262cSandi return false; 228f3f0262cSandi} 229f3f0262cSandi 230f3f0262cSandi/** 231f3f0262cSandi * Quicksearch for searching matching pagenames 232f3f0262cSandi * 233f3f0262cSandi * $opts['query'] is the search query 23415fae107Sandi * 23515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 236f3f0262cSandi */ 237f3f0262cSandifunction search_pagename(&$data,$base,$file,$type,$lvl,$opts){ 238f3f0262cSandi //we do nothing with directories 239f3f0262cSandi if($type == 'd') return true; 240f3f0262cSandi //only search txt files 2410e1a261eSMichael Klier if(substr($file,-4) != '.txt') return true; 242f3f0262cSandi 243f3f0262cSandi //simple stringmatching 244396b7edbSmatthiasgrimm if (!empty($opts['query'])){ 245f3f0262cSandi if(strpos($file,$opts['query']) !== false){ 246f3f0262cSandi //check ACL 247f3f0262cSandi $id = pathID($file); 248f3f0262cSandi if(auth_quickaclcheck($id) < AUTH_READ){ 249f3f0262cSandi return false; 250f3f0262cSandi } 251f3f0262cSandi $data[]['id'] = $id; 252f3f0262cSandi } 253396b7edbSmatthiasgrimm } 254f3f0262cSandi return true; 255f3f0262cSandi} 256f3f0262cSandi 257f3f0262cSandi/** 25858b6f612SAndreas Gohr * Just lists all documents 25958b6f612SAndreas Gohr * 2601fcfad4dSAndreas Gohr * $opts['depth'] recursion level, 0 for all 2611fcfad4dSAndreas Gohr * $opts['hash'] do md5 sum of content? 262224122cfSAndreas Gohr * $opts['skipacl'] list everything regardless of ACL 2631fcfad4dSAndreas Gohr * 26458b6f612SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 26558b6f612SAndreas Gohr */ 26658b6f612SAndreas Gohrfunction search_allpages(&$data,$base,$file,$type,$lvl,$opts){ 26758b6f612SAndreas Gohr //we do nothing with directories 2681fcfad4dSAndreas Gohr if($type == 'd'){ 2691fcfad4dSAndreas Gohr if(!$opts['depth']) return true; // recurse forever 27017ff9c04SAndreas Gohr $parts = explode('/',ltrim($file,'/')); 2711fcfad4dSAndreas Gohr if(count($parts) == $opts['depth']) return false; // depth reached 2721fcfad4dSAndreas Gohr return true; 2731fcfad4dSAndreas Gohr } 2741fcfad4dSAndreas Gohr 27558b6f612SAndreas Gohr //only search txt files 2760e1a261eSMichael Klier if(substr($file,-4) != '.txt') return true; 27758b6f612SAndreas Gohr 2781fcfad4dSAndreas Gohr $item['id'] = pathID($file); 279061df79cSAndreas Gohr if(!$opts['skipacl'] && auth_quickaclcheck($item['id']) < AUTH_READ){ 2801fcfad4dSAndreas Gohr return false; 2811fcfad4dSAndreas Gohr } 2821fcfad4dSAndreas Gohr 2831fcfad4dSAndreas Gohr $item['rev'] = filemtime($base.'/'.$file); 284224122cfSAndreas Gohr $item['mtime'] = $item['rev']; 2851fcfad4dSAndreas Gohr $item['size'] = filesize($base.'/'.$file); 2861fcfad4dSAndreas Gohr if($opts['hash']){ 2871fcfad4dSAndreas Gohr $item['hash'] = md5(trim(rawWiki($item['id']))); 2881fcfad4dSAndreas Gohr } 2891fcfad4dSAndreas Gohr 2901fcfad4dSAndreas Gohr $data[] = $item; 29158b6f612SAndreas Gohr return true; 29258b6f612SAndreas Gohr} 29358b6f612SAndreas Gohr 29458b6f612SAndreas Gohr/** 295f3f0262cSandi * Search for backlinks to a given page 296f3f0262cSandi * 297f3f0262cSandi * $opts['ns'] namespace of the page 298f3f0262cSandi * $opts['name'] name of the page without namespace 29915fae107Sandi * 30015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 30154f4c056SAndreas Gohr * @deprecated Replaced by ft_backlinks() 302f3f0262cSandi */ 303f3f0262cSandifunction search_backlinks(&$data,$base,$file,$type,$lvl,$opts){ 304f3f0262cSandi //we do nothing with directories 3050e1a261eSMichael Klier if($type == 'd') return true; 306f3f0262cSandi //only search txt files 3070e1a261eSMichael Klier if(substr($file,-4) != '.txt') return true; 308f3f0262cSandi 309f3f0262cSandi //absolute search id 310f3f0262cSandi $sid = cleanID($opts['ns'].':'.$opts['name']); 311f3f0262cSandi 31237e34a5eSandi //current id and namespace 313f3f0262cSandi $cid = pathID($file); 314f3f0262cSandi $cns = getNS($cid); 315f3f0262cSandi 316f3f0262cSandi //check ACL 317f3f0262cSandi if(auth_quickaclcheck($cid) < AUTH_READ){ 318f3f0262cSandi return false; 319f3f0262cSandi } 320f3f0262cSandi 32137e34a5eSandi //fetch instructions 32237e34a5eSandi $instructions = p_cached_instructions($base.$file,true); 32337e34a5eSandi if(is_null($instructions)) return false; 324f3f0262cSandi 325de3eb1d7SAdrian Lang global $conf; 32637e34a5eSandi //check all links for match 32737e34a5eSandi foreach($instructions as $ins){ 32837e34a5eSandi if($ins[0] == 'internallink' || ($conf['camelcase'] && $ins[0] == 'camelcaselink') ){ 32937e34a5eSandi $mid = $ins[1][0]; 33037e34a5eSandi resolve_pageid($cns,$mid,$exists); //exists is not used 331f3f0262cSandi if($mid == $sid){ 33237e34a5eSandi //we have a match - finish 333f3f0262cSandi $data[]['id'] = $cid; 334f3f0262cSandi break; 335f3f0262cSandi } 336f3f0262cSandi } 337f3f0262cSandi } 338f3f0262cSandi 33937e34a5eSandi return false; 34037e34a5eSandi} 34137e34a5eSandi 342f3f0262cSandi/** 343f3f0262cSandi * Fulltextsearch 344f3f0262cSandi * 345f3f0262cSandi * $opts['query'] is the search query 34615fae107Sandi * 34715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 348506fa893SAndreas Gohr * @deprecated - fulltext indexer is used instead 349f3f0262cSandi */ 350f3f0262cSandifunction search_fulltext(&$data,$base,$file,$type,$lvl,$opts){ 351f3f0262cSandi //we do nothing with directories 3520e1a261eSMichael Klier if($type == 'd') return true; 353f3f0262cSandi //only search txt files 3540e1a261eSMichael Klier if(substr($file,-4) != '.txt') return true; 355f3f0262cSandi 356f3f0262cSandi //check ACL 357f3f0262cSandi $id = pathID($file); 358f3f0262cSandi if(auth_quickaclcheck($id) < AUTH_READ){ 359f3f0262cSandi return false; 360f3f0262cSandi } 361f3f0262cSandi 362f3f0262cSandi //create regexp from queries 3635ef370d2Smatthiasgrimm $poswords = array(); 3645ef370d2Smatthiasgrimm $negwords = array(); 3655ef370d2Smatthiasgrimm $qpreg = preg_split('/\s+/',$opts['query']); 3665ef370d2Smatthiasgrimm 3675ef370d2Smatthiasgrimm foreach($qpreg as $word){ 3685ef370d2Smatthiasgrimm switch(substr($word,0,1)){ 3695ef370d2Smatthiasgrimm case '-': 370396b7edbSmatthiasgrimm if(strlen($word) > 1){ // catch single '-' 3715ef370d2Smatthiasgrimm array_push($negwords,preg_quote(substr($word,1),'#')); 372396b7edbSmatthiasgrimm } 3735ef370d2Smatthiasgrimm break; 3745ef370d2Smatthiasgrimm case '+': 375396b7edbSmatthiasgrimm if(strlen($word) > 1){ // catch single '+' 3765ef370d2Smatthiasgrimm array_push($poswords,preg_quote(substr($word,1),'#')); 377396b7edbSmatthiasgrimm } 3785ef370d2Smatthiasgrimm break; 3795ef370d2Smatthiasgrimm default: 3805ef370d2Smatthiasgrimm array_push($poswords,preg_quote($word,'#')); 3815ef370d2Smatthiasgrimm break; 3825ef370d2Smatthiasgrimm } 3835ef370d2Smatthiasgrimm } 384248a7321Smatthiasgrimm 385248a7321Smatthiasgrimm // a search without any posword is useless 386248a7321Smatthiasgrimm if (!count($poswords)) return true; 3875ef370d2Smatthiasgrimm 3885a5d942dSmatthiasgrimm $reg = '^(?=.*?'.join(')(?=.*?',$poswords).')'; 3895ef370d2Smatthiasgrimm $reg .= count($negwords) ? '((?!'.join('|',$negwords).').)*$' : '.*$'; 390b59a406bSmatthiasgrimm search_regex($data,$base,$file,$reg,$poswords); 391b59a406bSmatthiasgrimm return true; 392b59a406bSmatthiasgrimm } 393b59a406bSmatthiasgrimm 394b59a406bSmatthiasgrimm /** 395b59a406bSmatthiasgrimm * Reference search 396b59a406bSmatthiasgrimm * This fuction searches for existing references to a given media file 397b59a406bSmatthiasgrimm * and returns an array with the found pages. It doesn't pay any 398b59a406bSmatthiasgrimm * attention to ACL permissions to find every reference. The caller 399b59a406bSmatthiasgrimm * must check if the user has the appropriate rights to see the found 400b59a406bSmatthiasgrimm * page and eventually have to prevent the result from displaying. 401b59a406bSmatthiasgrimm * 402b59a406bSmatthiasgrimm * @param array $data Reference to the result data structure 403b59a406bSmatthiasgrimm * @param string $base Base usually $conf['datadir'] 404b59a406bSmatthiasgrimm * @param string $file current file or directory relative to $base 405b59a406bSmatthiasgrimm * @param char $type Type either 'd' for directory or 'f' for file 406b59a406bSmatthiasgrimm * @param int $lvl Current recursion depht 407b59a406bSmatthiasgrimm * @param mixed $opts option array as given to search() 408b59a406bSmatthiasgrimm * 409b59a406bSmatthiasgrimm * $opts['query'] is the demanded media file name 410b59a406bSmatthiasgrimm * 411b59a406bSmatthiasgrimm * @author Andreas Gohr <andi@splitbrain.org> 412b59a406bSmatthiasgrimm * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net> 413b59a406bSmatthiasgrimm */ 414b59a406bSmatthiasgrimmfunction search_reference(&$data,$base,$file,$type,$lvl,$opts){ 415b59a406bSmatthiasgrimm global $conf; 416b59a406bSmatthiasgrimm 417b59a406bSmatthiasgrimm //we do nothing with directories 418b59a406bSmatthiasgrimm if($type == 'd') return true; 419b59a406bSmatthiasgrimm 420b59a406bSmatthiasgrimm //only search txt files 4210e1a261eSMichael Klier if(substr($file,-4) != '.txt') return true; 422b59a406bSmatthiasgrimm 423e28299ccSmatthiasgrimm //we finish after 'cnt' references found. The return value 424b59a406bSmatthiasgrimm //'false' will skip subdirectories to speed search up. 425e28299ccSmatthiasgrimm $cnt = $conf['refshow'] > 0 ? $conf['refshow'] : 1; 426e28299ccSmatthiasgrimm if(count($data) >= $cnt) return false; 427b59a406bSmatthiasgrimm 428d67ca2c0Smatthiasgrimm $reg = '\{\{ *\:?'.$opts['query'].' *(\|.*)?\}\}'; 429b59a406bSmatthiasgrimm search_regex($data,$base,$file,$reg,array($opts['query'])); 430b59a406bSmatthiasgrimm return true; 431b59a406bSmatthiasgrimm} 432b59a406bSmatthiasgrimm 433b59a406bSmatthiasgrimm/* ------------- helper functions below -------------- */ 434b59a406bSmatthiasgrimm 435b59a406bSmatthiasgrimm/** 436b59a406bSmatthiasgrimm * fulltext search helper 437b59a406bSmatthiasgrimm * searches a text file with a given regular expression 438b59a406bSmatthiasgrimm * no ACL checks are performed. This have to be done by 439b59a406bSmatthiasgrimm * the caller if necessary. 440b59a406bSmatthiasgrimm * 441b59a406bSmatthiasgrimm * @param array $data reference to array for results 442b59a406bSmatthiasgrimm * @param string $base base directory 443b59a406bSmatthiasgrimm * @param string $file file name to search in 444b59a406bSmatthiasgrimm * @param string $reg regular expression to search for 445b59a406bSmatthiasgrimm * @param array $words words that should be marked in the results 446b59a406bSmatthiasgrimm * 447b59a406bSmatthiasgrimm * @author Andreas Gohr <andi@splitbrain.org> 448b59a406bSmatthiasgrimm * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net> 449506fa893SAndreas Gohr * 450506fa893SAndreas Gohr * @deprecated - fulltext indexer is used instead 451b59a406bSmatthiasgrimm */ 452b59a406bSmatthiasgrimmfunction search_regex(&$data,$base,$file,$reg,$words){ 453b59a406bSmatthiasgrimm 454b59a406bSmatthiasgrimm //get text 455b59a406bSmatthiasgrimm $text = io_readfile($base.'/'.$file); 456b59a406bSmatthiasgrimm //lowercase text (u modifier does not help with case) 457b59a406bSmatthiasgrimm $lctext = utf8_strtolower($text); 458f3f0262cSandi 459f3f0262cSandi //do the fulltext search 460f3f0262cSandi $matches = array(); 4615ef370d2Smatthiasgrimm if($cnt = preg_match_all('#'.$reg.'#usi',$lctext,$matches)){ 462f3f0262cSandi //this is not the best way for snippet generation but the fastest I could find 463b59a406bSmatthiasgrimm $q = $words[0]; //use first word for snippet creation 464d5a2a500Sandi $p = utf8_strpos($lctext,$q); 465f3f0262cSandi $f = $p - 100; 466d5a2a500Sandi $l = utf8_strlen($q) + 200; 467f3f0262cSandi if($f < 0) $f = 0; 468f3f0262cSandi $snippet = '<span class="search_sep"> ... </span>'. 469d5a2a500Sandi htmlspecialchars(utf8_substr($text,$f,$l)). 470f3f0262cSandi '<span class="search_sep"> ... </span>'; 471b59a406bSmatthiasgrimm $mark = '('.join('|', $words).')'; 472ed7ecb79SAnika Henke $snippet = preg_replace('#'.$mark.'#si','<strong class="search_hit">\\1</strong>',$snippet); 473f3f0262cSandi 474f3f0262cSandi $data[] = array( 475b59a406bSmatthiasgrimm 'id' => pathID($file), 4765ef370d2Smatthiasgrimm 'count' => preg_match_all('#'.$mark.'#usi',$lctext,$matches), 477b59a406bSmatthiasgrimm 'poswords' => join(' ',$words), 478f3f0262cSandi 'snippet' => $snippet, 479f3f0262cSandi ); 480f3f0262cSandi } 481f3f0262cSandi 482f3f0262cSandi return true; 483f3f0262cSandi} 484f3f0262cSandi 485b59a406bSmatthiasgrimm 486f3f0262cSandi/** 48715fae107Sandi * fulltext sort 48815fae107Sandi * 489f3f0262cSandi * Callback sort function for use with usort to sort the data 490f3f0262cSandi * structure created by search_fulltext. Sorts descending by count 49115fae107Sandi * 49215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 493f3f0262cSandi */ 494f3f0262cSandifunction sort_search_fulltext($a,$b){ 495f3f0262cSandi if($a['count'] > $b['count']){ 496f3f0262cSandi return -1; 497f3f0262cSandi }elseif($a['count'] < $b['count']){ 498f3f0262cSandi return 1; 499f3f0262cSandi }else{ 500f3f0262cSandi return strcmp($a['id'],$b['id']); 501f3f0262cSandi } 502f3f0262cSandi} 503f3f0262cSandi 504f3f0262cSandi/** 505f3f0262cSandi * translates a document path to an ID 50615fae107Sandi * 50715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 50837e34a5eSandi * @todo move to pageutils 509f3f0262cSandi */ 510156a608cSandifunction pathID($path,$keeptxt=false){ 51149c713a3Sandi $id = utf8_decodeFN($path); 51249c713a3Sandi $id = str_replace('/',':',$id); 513156a608cSandi if(!$keeptxt) $id = preg_replace('#\.txt$#','',$id); 514f3f0262cSandi $id = preg_replace('#^:+#','',$id); 515f3f0262cSandi $id = preg_replace('#:+$#','',$id); 516f3f0262cSandi return $id; 517f3f0262cSandi} 518f3f0262cSandi 519340756e4Sandi 5203abeade3SAndreas Gohr/** 5213abeade3SAndreas Gohr * This is a very universal callback for the search() function, replacing 5223abeade3SAndreas Gohr * many of the former individual functions at the cost of a more complex 5233abeade3SAndreas Gohr * setup. 5243abeade3SAndreas Gohr * 5253abeade3SAndreas Gohr * How the function behaves, depends on the options passed in the $opts 5263abeade3SAndreas Gohr * array, where the following settings can be used. 5273abeade3SAndreas Gohr * 5283abeade3SAndreas Gohr * depth int recursion depth. 0 for unlimited 5293abeade3SAndreas Gohr * keeptxt bool keep .txt extension for IDs 5303abeade3SAndreas Gohr * listfiles bool include files in listing 5313abeade3SAndreas Gohr * listdirs bool include namespaces in listing 5323abeade3SAndreas Gohr * pagesonly bool restrict files to pages 5333abeade3SAndreas Gohr * skipacl bool do not check for READ permission 5343abeade3SAndreas Gohr * sneakyacl bool don't recurse into nonreadable dirs 5353abeade3SAndreas Gohr * hash bool create MD5 hash for files 5363abeade3SAndreas Gohr * meta bool return file metadata 5373abeade3SAndreas Gohr * filematch string match files against this regexp 5388705cc81SAndreas Gohr * idmatch string match full ID against this regexp 5398705cc81SAndreas Gohr * dirmatch string match directory against this regexp when adding 5408705cc81SAndreas Gohr * nsmatch string match namespace against this regexp when adding 5418705cc81SAndreas Gohr * recmatch string match directory against this regexp when recursing 5423abeade3SAndreas Gohr * showmsg bool warn about non-ID files 5433abeade3SAndreas Gohr * showhidden bool show hidden files too 5443abeade3SAndreas Gohr * firsthead bool return first heading for pages 5453abeade3SAndreas Gohr * 5463abeade3SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 5473abeade3SAndreas Gohr */ 5483abeade3SAndreas Gohrfunction search_universal(&$data,$base,$file,$type,$lvl,$opts){ 5493abeade3SAndreas Gohr $item = array(); 5503abeade3SAndreas Gohr $return = true; 5513abeade3SAndreas Gohr 5523abeade3SAndreas Gohr // get ID and check if it is a valid one 553*e63d421bSAndreas Gohr $item['id'] = pathID($file,($type == 'd' || $opts['keeptxt'])); 5548537abd1SAdrian Lang if($item['id'] != cleanID($item['id'])){ 5553abeade3SAndreas Gohr if($opts['showmsg']) 5568537abd1SAdrian Lang msg(hsc($item['id']).' is not a valid file name for DokuWiki - skipped',-1); 5573abeade3SAndreas Gohr return false; // skip non-valid files 5583abeade3SAndreas Gohr } 5598705cc81SAndreas Gohr $item['ns'] = getNS($item['id']); 5603abeade3SAndreas Gohr 5613abeade3SAndreas Gohr if($type == 'd') { 5623abeade3SAndreas Gohr // decide if to recursion into this directory is wanted 5633abeade3SAndreas Gohr if(!$opts['depth']){ 5643abeade3SAndreas Gohr $return = true; // recurse forever 5653abeade3SAndreas Gohr }else{ 5663abeade3SAndreas Gohr $depth = substr_count($file,'/'); 5673abeade3SAndreas Gohr if($depth >= $opts['depth']){ 5683abeade3SAndreas Gohr $return = false; // depth reached 5693abeade3SAndreas Gohr }else{ 5703abeade3SAndreas Gohr $return = true; 5713abeade3SAndreas Gohr } 5723abeade3SAndreas Gohr } 5733abeade3SAndreas Gohr if($return && !preg_match('/'.$opts['recmatch'].'/',$file)){ 5743abeade3SAndreas Gohr $return = false; // doesn't match 5753abeade3SAndreas Gohr } 5763abeade3SAndreas Gohr } 5773abeade3SAndreas Gohr 5783abeade3SAndreas Gohr // check ACL 5793abeade3SAndreas Gohr if(!$opts['skipacl']){ 5803abeade3SAndreas Gohr if($type == 'd'){ 5813abeade3SAndreas Gohr $item['perm'] = auth_quickaclcheck($item['id'].':*'); 5823abeade3SAndreas Gohr }else{ 5833abeade3SAndreas Gohr $item['perm'] = auth_quickaclcheck($item['id']); //FIXME check namespace for media files 5843abeade3SAndreas Gohr } 5853abeade3SAndreas Gohr }else{ 5863abeade3SAndreas Gohr $item['perm'] = AUTH_DELETE; 5873abeade3SAndreas Gohr } 5883abeade3SAndreas Gohr 5893abeade3SAndreas Gohr // are we done here maybe? 5903abeade3SAndreas Gohr if($type == 'd'){ 5913abeade3SAndreas Gohr if(!$opts['listdirs']) return $return; 5923abeade3SAndreas Gohr if(!$opts['skipacl'] && $opts['sneakyacl'] && $item['perm'] < AUTH_READ) return false; //neither list nor recurse 5933abeade3SAndreas Gohr if($opts['dirmatch'] && !preg_match('/'.$opts['dirmatch'].'/',$file)) return $return; 5948705cc81SAndreas Gohr if($opts['nsmatch'] && !preg_match('/'.$opts['nsmatch'].'/',$item['ns'])) return $return; 5953abeade3SAndreas Gohr }else{ 5963abeade3SAndreas Gohr if(!$opts['listfiles']) return $return; 5973abeade3SAndreas Gohr if(!$opts['skipacl'] && $item['perm'] < AUTH_READ) return $return; 5983abeade3SAndreas Gohr if($opts['pagesonly'] && (substr($file,-4) != '.txt')) return $return; 599de3eb1d7SAdrian Lang if(!$opts['showhidden'] && isHiddenPage($item['id'])) return $return; 6003abeade3SAndreas Gohr if($opts['filematch'] && !preg_match('/'.$opts['filematch'].'/',$file)) return $return; 6018705cc81SAndreas Gohr if($opts['idmatch'] && !preg_match('/'.$opts['idmatch'].'/',$item['id'])) return $return; 6023abeade3SAndreas Gohr } 6033abeade3SAndreas Gohr 6043abeade3SAndreas Gohr // still here? prepare the item 6053abeade3SAndreas Gohr $item['type'] = $type; 60632d6093dSAndreas Gohr $item['level'] = $lvl; 6073abeade3SAndreas Gohr $item['open'] = $return; 6083abeade3SAndreas Gohr 6093abeade3SAndreas Gohr if($opts['meta']){ 6103abeade3SAndreas Gohr $item['file'] = basename($file); 6113abeade3SAndreas Gohr $item['size'] = filesize($base.'/'.$file); 6123abeade3SAndreas Gohr $item['mtime'] = filemtime($base.'/'.$file); 6133abeade3SAndreas Gohr $item['rev'] = $item['mtime']; 6143abeade3SAndreas Gohr $item['writable'] = is_writable($base.'/'.$file); 6153abeade3SAndreas Gohr $item['executable'] = is_executable($base.'/'.$file); 6163abeade3SAndreas Gohr } 6173abeade3SAndreas Gohr 6183abeade3SAndreas Gohr if($type == 'f'){ 6193abeade3SAndreas Gohr if($opts['hash']) $item['hash'] = md5(io_readFile($base.'/'.$file,false)); 6203abeade3SAndreas Gohr if($opts['firsthead']) $item['title'] = p_get_first_heading($item['id'],false); 6213abeade3SAndreas Gohr } 6223abeade3SAndreas Gohr 6233abeade3SAndreas Gohr // finally add the item 6243abeade3SAndreas Gohr $data[] = $item; 6253abeade3SAndreas Gohr return $return; 6263abeade3SAndreas Gohr} 6273abeade3SAndreas Gohr 6283abeade3SAndreas Gohr//Setup VIM: ex: et ts=4 enc=utf-8 : 629