3 * DokuWiki search functions
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Gohr <andi@splitbrain.org>
9 if(!defined('DOKU_INC')) die('meh.');
14 * This function recurses into a given base directory
15 * and calls the supplied function for each file and directory
17 * @param array &$data The results of the search are stored here
18 * @param string $base Where to start the search
19 * @param callback $func Callback (function name or array with object,method)
20 * @param array $opts option array will be given to the Callback
21 * @param string $dir Current directory beyond $base
22 * @param int $lvl Recursion Level
23 * @param mixed $sort 'natural' to use natural order sorting (default); 'date' to sort by filemtime; leave empty to skip sorting.
24 * @author Andreas Gohr <andi@splitbrain.org>
26 function search(&$data,$base,$func,$opts,$dir='',$lvl=1,$sort='natural'){
31 // safeguard against runaways #1452
32 if($base == '' ||
$base == '/') {
33 throw new RuntimeException('No valid $base passed to search() - possible misconfiguration or bug');
36 //read in directories and files
37 $dh = @opendir
($base.'/'.$dir);
39 while(($file = readdir($dh)) !== false){
40 if(preg_match('/^[\._]/',$file)) continue; //skip hidden files and upper dirs
41 if(is_dir($base.'/'.$dir.'/'.$file)){
42 $dirs[] = $dir.'/'.$file;
45 $files[] = $dir.'/'.$file;
46 $filepaths[] = $base.'/'.$dir.'/'.$file;
50 if ($sort == 'date') {
51 @array_multisort
(array_map('filemtime', $filepaths), SORT_NUMERIC
, SORT_DESC
, $files);
52 } else /* natural */ {
58 //give directories to userfunction then recurse
59 foreach($dirs as $dir){
60 if (call_user_func_array($func, array(&$data,$base,$dir,'d',$lvl,$opts))){
61 search($data,$base,$func,$opts,$dir,$lvl+
1,$sort);
64 //now handle the files
65 foreach($files as $file){
66 call_user_func_array($func, array(&$data,$base,$file,'f',$lvl,$opts));
71 * The following functions are userfunctions to use with the search
72 * function above. This function is called for every found file or
73 * directory. When a directory is given to the function it has to
74 * decide if this directory should be traversed (true) or not (false)
75 * The function has to accept the following parameters:
77 * array &$data - Reference to the result data structure
78 * string $base - Base usually $conf['datadir']
79 * string $file - current file or directory relative to $base
80 * string $type - Type either 'd' for directory or 'f' for file
81 * int $lvl - Current recursion depht
82 * array $opts - option array as given to search()
84 * return values for files are ignored
86 * All functions should check the ACL for document READ rights
87 * namespaces (directories) are NOT checked (when sneaky_index is 0) as this
88 * would break the recursion (You can have an nonreadable dir over a readable
89 * one deeper nested) also make sure to check the file type (for example
90 * in case of lockfiles).
94 * Searches for pages beginning with the given query
96 * @author Andreas Gohr <andi@splitbrain.org>
100 * @param string $file
101 * @param string $type
102 * @param integer $lvl
107 function search_qsearch(&$data,$base,$file,$type,$lvl,$opts){
109 'idmatch' => '(^|:)'.preg_quote($opts['query'],'/').'/',
113 return search_universal($data,$base,$file,$type,$lvl,$opts);
117 * Build the browsable index of pages
119 * $opts['ns'] is the currently viewed namespace
121 * @author Andreas Gohr <andi@splitbrain.org>
124 * @param string $base
125 * @param string $file
126 * @param string $type
127 * @param integer $lvl
132 function search_index(&$data,$base,$file,$type,$lvl,$opts){
137 'listfiles' => empty($opts['nofiles']),
138 'sneakyacl' => $conf['sneaky_index'],
139 // Hacky, should rather use recmatch
140 'depth' => preg_match('#^'.preg_quote($file, '#').'(/|$)#','/'.$opts['ns']) ?
0 : -1
143 return search_universal($data, $base, $file, $type, $lvl, $opts);
147 * List all namespaces
149 * @author Andreas Gohr <andi@splitbrain.org>
152 * @param string $base
153 * @param string $file
154 * @param string $type
155 * @param integer $lvl
160 function search_namespaces(&$data,$base,$file,$type,$lvl,$opts){
164 return search_universal($data,$base,$file,$type,$lvl,$opts);
168 * List all mediafiles in a namespace
169 * $opts['depth'] recursion level, 0 for all
170 * $opts['showmsg'] shows message if invalid media id is used
171 * $opts['skipacl'] skip acl checking
172 * $opts['pattern'] check given pattern
173 * $opts['hash'] add hashes to result list
175 * @author Andreas Gohr <andi@splitbrain.org>
178 * @param string $base
179 * @param string $file
180 * @param string $type
181 * @param integer $lvl
186 function search_media(&$data,$base,$file,$type,$lvl,$opts){
188 //we do nothing with directories
190 if(empty($opts['depth'])) return true; // recurse forever
191 $depth = substr_count($file,'/');
192 if($depth >= $opts['depth']) return false; // depth reached
197 $info['id'] = pathID($file,true);
198 if($info['id'] != cleanID($info['id'])){
200 msg(hsc($info['id']).' is not a valid file name for DokuWiki - skipped',-1);
201 return false; // skip non-valid files
204 //check ACL for namespace (we have no ACL for mediafiles)
205 $info['perm'] = auth_quickaclcheck(getNS($info['id']).':*');
206 if(empty($opts['skipacl']) && $info['perm'] < AUTH_READ
){
210 //check pattern filter
211 if(!empty($opts['pattern']) && !@preg_match
($opts['pattern'], $info['id'])){
215 $info['file'] = utf8_basename($file);
216 $info['size'] = filesize($base.'/'.$file);
217 $info['mtime'] = filemtime($base.'/'.$file);
218 $info['writable'] = is_writable($base.'/'.$file);
219 if(preg_match("/\.(jpe?g|gif|png)$/",$file)){
220 $info['isimg'] = true;
221 $info['meta'] = new JpegMeta($base.'/'.$file);
223 $info['isimg'] = false;
225 if(!empty($opts['hash'])){
226 $info['hash'] = md5(io_readFile(mediaFN($info['id']),false));
235 * This function just lists documents (for RSS namespace export)
237 * @author Andreas Gohr <andi@splitbrain.org>
240 * @param string $base
241 * @param string $file
242 * @param string $type
243 * @param integer $lvl
248 function search_list(&$data,$base,$file,$type,$lvl,$opts){
249 //we do nothing with directories
250 if($type == 'd') return false;
251 //only search txt files
252 if(substr($file,-4) == '.txt'){
255 if(auth_quickaclcheck($id) < AUTH_READ
){
264 * Quicksearch for searching matching pagenames
266 * $opts['query'] is the search query
268 * @author Andreas Gohr <andi@splitbrain.org>
271 * @param string $base
272 * @param string $file
273 * @param string $type
274 * @param integer $lvl
279 function search_pagename(&$data,$base,$file,$type,$lvl,$opts){
280 //we do nothing with directories
281 if($type == 'd') return true;
282 //only search txt files
283 if(substr($file,-4) != '.txt') return true;
285 //simple stringmatching
286 if (!empty($opts['query'])){
287 if(strpos($file,$opts['query']) !== false){
290 if(auth_quickaclcheck($id) < AUTH_READ
){
300 * Just lists all documents
302 * $opts['depth'] recursion level, 0 for all
303 * $opts['hash'] do md5 sum of content?
304 * $opts['skipacl'] list everything regardless of ACL
306 * @author Andreas Gohr <andi@splitbrain.org>
309 * @param string $base
310 * @param string $file
311 * @param string $type
312 * @param integer $lvl
317 function search_allpages(&$data,$base,$file,$type,$lvl,$opts){
318 if(isset($opts['depth']) && $opts['depth']){
319 $parts = explode('/',ltrim($file,'/'));
320 if(($type == 'd' && count($parts) >= $opts['depth'])
321 ||
($type != 'd' && count($parts) > $opts['depth'])){
322 return false; // depth reached
326 //we do nothing with directories
331 //only search txt files
332 if(substr($file,-4) != '.txt') return true;
335 $item['id'] = pathID($file);
336 if(isset($opts['skipacl']) && !$opts['skipacl'] && auth_quickaclcheck($item['id']) < AUTH_READ
){
340 $item['rev'] = filemtime($base.'/'.$file);
341 $item['mtime'] = $item['rev'];
342 $item['size'] = filesize($base.'/'.$file);
343 if(!empty($opts['hash'])){
344 $item['hash'] = md5(trim(rawWiki($item['id'])));
351 /* ------------- helper functions below -------------- */
356 * Callback sort function for use with usort to sort the data
357 * structure created by search_fulltext. Sorts descending by count
359 * @author Andreas Gohr <andi@splitbrain.org>
366 function sort_search_fulltext($a,$b){
367 if($a['count'] > $b['count']){
369 }elseif($a['count'] < $b['count']){
372 return strcmp($a['id'],$b['id']);
377 * translates a document path to an ID
379 * @author Andreas Gohr <andi@splitbrain.org>
380 * @todo move to pageutils
382 * @param string $path
383 * @param bool $keeptxt
385 * @return mixed|string
387 function pathID($path,$keeptxt=false){
388 $id = utf8_decodeFN($path);
389 $id = str_replace('/',':',$id);
390 if(!$keeptxt) $id = preg_replace('#\.txt$#','',$id);
391 $id = trim($id, ':');
397 * This is a very universal callback for the search() function, replacing
398 * many of the former individual functions at the cost of a more complex
401 * How the function behaves, depends on the options passed in the $opts
402 * array, where the following settings can be used.
404 * depth int recursion depth. 0 for unlimited (default: 0)
405 * keeptxt bool keep .txt extension for IDs (default: false)
406 * listfiles bool include files in listing (default: false)
407 * listdirs bool include namespaces in listing (default: false)
408 * pagesonly bool restrict files to pages (default: false)
409 * skipacl bool do not check for READ permission (default: false)
410 * sneakyacl bool don't recurse into nonreadable dirs (default: false)
411 * hash bool create MD5 hash for files (default: false)
412 * meta bool return file metadata (default: false)
413 * filematch string match files against this regexp (default: '', so accept everything)
414 * idmatch string match full ID against this regexp (default: '', so accept everything)
415 * dirmatch string match directory against this regexp when adding (default: '', so accept everything)
416 * nsmatch string match namespace against this regexp when adding (default: '', so accept everything)
417 * recmatch string match directory against this regexp when recursing (default: '', so accept everything)
418 * showmsg bool warn about non-ID files (default: false)
419 * showhidden bool show hidden files(e.g. by hidepages config) too (default: false)
420 * firsthead bool return first heading for pages (default: false)
422 * @param array &$data - Reference to the result data structure
423 * @param string $base - Base usually $conf['datadir']
424 * @param string $file - current file or directory relative to $base
425 * @param string $type - Type either 'd' for directory or 'f' for file
426 * @param int $lvl - Current recursion depht
427 * @param array $opts - option array as given to search()
428 * @return bool if this directory should be traversed (true) or not (false)
429 * return value is ignored for files
431 * @author Andreas Gohr <gohr@cosmocode.de>
433 function search_universal(&$data,$base,$file,$type,$lvl,$opts){
437 // get ID and check if it is a valid one
438 $item['id'] = pathID($file,($type == 'd' ||
!empty($opts['keeptxt'])));
439 if($item['id'] != cleanID($item['id'])){
440 if(!empty($opts['showmsg'])){
441 msg(hsc($item['id']).' is not a valid file name for DokuWiki - skipped',-1);
443 return false; // skip non-valid files
445 $item['ns'] = getNS($item['id']);
448 // decide if to recursion into this directory is wanted
449 if(empty($opts['depth'])){
450 $return = true; // recurse forever
452 $depth = substr_count($file,'/');
453 if($depth >= $opts['depth']){
454 $return = false; // depth reached
461 $match = empty($opts['recmatch']) ||
preg_match('/'.$opts['recmatch'].'/',$file);
463 return false; // doesn't match
469 if(empty($opts['skipacl'])){
471 $item['perm'] = auth_quickaclcheck($item['id'].':*');
473 $item['perm'] = auth_quickaclcheck($item['id']); //FIXME check namespace for media files
476 $item['perm'] = AUTH_DELETE
;
479 // are we done here maybe?
481 if(empty($opts['listdirs'])) return $return;
482 if(empty($opts['skipacl']) && !empty($opts['sneakyacl']) && $item['perm'] < AUTH_READ
) return false; //neither list nor recurse
483 if(!empty($opts['dirmatch']) && !preg_match('/'.$opts['dirmatch'].'/',$file)) return $return;
484 if(!empty($opts['nsmatch']) && !preg_match('/'.$opts['nsmatch'].'/',$item['ns'])) return $return;
486 if(empty($opts['listfiles'])) return $return;
487 if(empty($opts['skipacl']) && $item['perm'] < AUTH_READ
) return $return;
488 if(!empty($opts['pagesonly']) && (substr($file,-4) != '.txt')) return $return;
489 if(empty($opts['showhidden']) && isHiddenPage($item['id'])) return $return;
490 if(!empty($opts['filematch']) && !preg_match('/'.$opts['filematch'].'/',$file)) return $return;
491 if(!empty($opts['idmatch']) && !preg_match('/'.$opts['idmatch'].'/',$item['id'])) return $return;
494 // still here? prepare the item
495 $item['type'] = $type;
496 $item['level'] = $lvl;
497 $item['open'] = $return;
499 if(!empty($opts['meta'])){
500 $item['file'] = utf8_basename($file);
501 $item['size'] = filesize($base.'/'.$file);
502 $item['mtime'] = filemtime($base.'/'.$file);
503 $item['rev'] = $item['mtime'];
504 $item['writable'] = is_writable($base.'/'.$file);
505 $item['executable'] = is_executable($base.'/'.$file);
509 if(!empty($opts['hash'])) $item['hash'] = md5(io_readFile($base.'/'.$file,false));
510 if(!empty($opts['firsthead'])) $item['title'] = p_get_first_heading($item['id'],METADATA_DONT_RENDER
);
513 // finally add the item
518 //Setup VIM: ex: et ts=4 :