Merge pull request #3024 from splitbrain/cookieupdate
[dokuwiki.git] / inc / search.php
blob27efc65fe3bf9cbfcf4e437c5d2216df913aaa7f
1 <?php
2 /**
3 * DokuWiki search functions
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Gohr <andi@splitbrain.org>
7 */
9 /**
10 * Recurse directory
12 * This function recurses into a given base directory
13 * and calls the supplied function for each file and directory
15 * @param array &$data The results of the search are stored here
16 * @param string $base Where to start the search
17 * @param callback $func Callback (function name or array with object,method)
18 * @param array $opts option array will be given to the Callback
19 * @param string $dir Current directory beyond $base
20 * @param int $lvl Recursion Level
21 * @param mixed $sort 'natural' to use natural order sorting (default);
22 * 'date' to sort by filemtime; leave empty to skip sorting.
23 * @author Andreas Gohr <andi@splitbrain.org>
25 function search(&$data,$base,$func,$opts,$dir='',$lvl=1,$sort='natural'){
26 $dirs = array();
27 $files = array();
28 $filepaths = array();
30 // safeguard against runaways #1452
31 if($base == '' || $base == '/') {
32 throw new RuntimeException('No valid $base passed to search() - possible misconfiguration or bug');
35 //read in directories and files
36 $dh = @opendir($base.'/'.$dir);
37 if(!$dh) return;
38 while(($file = readdir($dh)) !== false){
39 if(preg_match('/^[\._]/',$file)) continue; //skip hidden files and upper dirs
40 if(is_dir($base.'/'.$dir.'/'.$file)){
41 $dirs[] = $dir.'/'.$file;
42 continue;
44 $files[] = $dir.'/'.$file;
45 $filepaths[] = $base.'/'.$dir.'/'.$file;
47 closedir($dh);
48 if (!empty($sort)) {
49 if ($sort == 'date') {
50 @array_multisort(array_map('filemtime', $filepaths), SORT_NUMERIC, SORT_DESC, $files);
51 } else /* natural */ {
52 natsort($files);
54 natsort($dirs);
57 //give directories to userfunction then recurse
58 foreach($dirs as $dir){
59 if (call_user_func_array($func, array(&$data,$base,$dir,'d',$lvl,$opts))){
60 search($data,$base,$func,$opts,$dir,$lvl+1,$sort);
63 //now handle the files
64 foreach($files as $file){
65 call_user_func_array($func, array(&$data,$base,$file,'f',$lvl,$opts));
69 /**
70 * The following functions are userfunctions to use with the search
71 * function above. This function is called for every found file or
72 * directory. When a directory is given to the function it has to
73 * decide if this directory should be traversed (true) or not (false)
74 * The function has to accept the following parameters:
76 * array &$data - Reference to the result data structure
77 * string $base - Base usually $conf['datadir']
78 * string $file - current file or directory relative to $base
79 * string $type - Type either 'd' for directory or 'f' for file
80 * int $lvl - Current recursion depht
81 * array $opts - option array as given to search()
83 * return values for files are ignored
85 * All functions should check the ACL for document READ rights
86 * namespaces (directories) are NOT checked (when sneaky_index is 0) as this
87 * would break the recursion (You can have an nonreadable dir over a readable
88 * one deeper nested) also make sure to check the file type (for example
89 * in case of lockfiles).
92 /**
93 * Searches for pages beginning with the given query
95 * @author Andreas Gohr <andi@splitbrain.org>
97 * @param array $data
98 * @param string $base
99 * @param string $file
100 * @param string $type
101 * @param integer $lvl
102 * @param array $opts
104 * @return bool
106 function search_qsearch(&$data,$base,$file,$type,$lvl,$opts){
107 $opts = array(
108 'idmatch' => '(^|:)'.preg_quote($opts['query'],'/').'/',
109 'listfiles' => true,
110 'pagesonly' => true,
112 return search_universal($data,$base,$file,$type,$lvl,$opts);
116 * Build the browsable index of pages
118 * $opts['ns'] is the currently viewed namespace
120 * @author Andreas Gohr <andi@splitbrain.org>
122 * @param array $data
123 * @param string $base
124 * @param string $file
125 * @param string $type
126 * @param integer $lvl
127 * @param array $opts
129 * @return bool
131 function search_index(&$data,$base,$file,$type,$lvl,$opts){
132 global $conf;
133 $opts = array(
134 'pagesonly' => true,
135 'listdirs' => true,
136 'listfiles' => empty($opts['nofiles']),
137 'sneakyacl' => $conf['sneaky_index'],
138 // Hacky, should rather use recmatch
139 'depth' => preg_match('#^'.preg_quote($file, '#').'(/|$)#','/'.$opts['ns']) ? 0 : -1
142 return search_universal($data, $base, $file, $type, $lvl, $opts);
146 * List all namespaces
148 * @author Andreas Gohr <andi@splitbrain.org>
150 * @param array $data
151 * @param string $base
152 * @param string $file
153 * @param string $type
154 * @param integer $lvl
155 * @param array $opts
157 * @return bool
159 function search_namespaces(&$data,$base,$file,$type,$lvl,$opts){
160 $opts = array(
161 'listdirs' => true,
163 return search_universal($data,$base,$file,$type,$lvl,$opts);
167 * List all mediafiles in a namespace
168 * $opts['depth'] recursion level, 0 for all
169 * $opts['showmsg'] shows message if invalid media id is used
170 * $opts['skipacl'] skip acl checking
171 * $opts['pattern'] check given pattern
172 * $opts['hash'] add hashes to result list
174 * @author Andreas Gohr <andi@splitbrain.org>
176 * @param array $data
177 * @param string $base
178 * @param string $file
179 * @param string $type
180 * @param integer $lvl
181 * @param array $opts
183 * @return bool
185 function search_media(&$data,$base,$file,$type,$lvl,$opts){
187 //we do nothing with directories
188 if($type == 'd') {
189 if(empty($opts['depth'])) return true; // recurse forever
190 $depth = substr_count($file,'/');
191 if($depth >= $opts['depth']) return false; // depth reached
192 return true;
195 $info = array();
196 $info['id'] = pathID($file,true);
197 if($info['id'] != cleanID($info['id'])){
198 if($opts['showmsg'])
199 msg(hsc($info['id']).' is not a valid file name for DokuWiki - skipped',-1);
200 return false; // skip non-valid files
203 //check ACL for namespace (we have no ACL for mediafiles)
204 $info['perm'] = auth_quickaclcheck(getNS($info['id']).':*');
205 if(empty($opts['skipacl']) && $info['perm'] < AUTH_READ){
206 return false;
209 //check pattern filter
210 if(!empty($opts['pattern']) && !@preg_match($opts['pattern'], $info['id'])){
211 return false;
214 $info['file'] = \dokuwiki\Utf8\PhpString::basename($file);
215 $info['size'] = filesize($base.'/'.$file);
216 $info['mtime'] = filemtime($base.'/'.$file);
217 $info['writable'] = is_writable($base.'/'.$file);
218 if(preg_match("/\.(jpe?g|gif|png)$/",$file)){
219 $info['isimg'] = true;
220 $info['meta'] = new JpegMeta($base.'/'.$file);
221 }else{
222 $info['isimg'] = false;
224 if(!empty($opts['hash'])){
225 $info['hash'] = md5(io_readFile(mediaFN($info['id']),false));
228 $data[] = $info;
230 return false;
234 * This function just lists documents (for RSS namespace export)
236 * @author Andreas Gohr <andi@splitbrain.org>
238 * @param array $data
239 * @param string $base
240 * @param string $file
241 * @param string $type
242 * @param integer $lvl
243 * @param array $opts
245 * @return bool
247 function search_list(&$data,$base,$file,$type,$lvl,$opts){
248 //we do nothing with directories
249 if($type == 'd') return false;
250 //only search txt files
251 if(substr($file,-4) == '.txt'){
252 //check ACL
253 $id = pathID($file);
254 if(auth_quickaclcheck($id) < AUTH_READ){
255 return false;
257 $data[]['id'] = $id;
259 return false;
263 * Quicksearch for searching matching pagenames
265 * $opts['query'] is the search query
267 * @author Andreas Gohr <andi@splitbrain.org>
269 * @param array $data
270 * @param string $base
271 * @param string $file
272 * @param string $type
273 * @param integer $lvl
274 * @param array $opts
276 * @return bool
278 function search_pagename(&$data,$base,$file,$type,$lvl,$opts){
279 //we do nothing with directories
280 if($type == 'd') return true;
281 //only search txt files
282 if(substr($file,-4) != '.txt') return true;
284 //simple stringmatching
285 if (!empty($opts['query'])){
286 if(strpos($file,$opts['query']) !== false){
287 //check ACL
288 $id = pathID($file);
289 if(auth_quickaclcheck($id) < AUTH_READ){
290 return false;
292 $data[]['id'] = $id;
295 return true;
299 * Just lists all documents
301 * $opts['depth'] recursion level, 0 for all
302 * $opts['hash'] do md5 sum of content?
303 * $opts['skipacl'] list everything regardless of ACL
305 * @author Andreas Gohr <andi@splitbrain.org>
307 * @param array $data
308 * @param string $base
309 * @param string $file
310 * @param string $type
311 * @param integer $lvl
312 * @param array $opts
314 * @return bool
316 function search_allpages(&$data,$base,$file,$type,$lvl,$opts){
317 if(isset($opts['depth']) && $opts['depth']){
318 $parts = explode('/',ltrim($file,'/'));
319 if(($type == 'd' && count($parts) >= $opts['depth'])
320 || ($type != 'd' && count($parts) > $opts['depth'])){
321 return false; // depth reached
325 //we do nothing with directories
326 if($type == 'd'){
327 return true;
330 //only search txt files
331 if(substr($file,-4) != '.txt') return true;
333 $item = array();
334 $item['id'] = pathID($file);
335 if(empty($opts['skipacl']) && auth_quickaclcheck($item['id']) < AUTH_READ){
336 return false;
339 $item['rev'] = filemtime($base.'/'.$file);
340 $item['mtime'] = $item['rev'];
341 $item['size'] = filesize($base.'/'.$file);
342 if(!empty($opts['hash'])){
343 $item['hash'] = md5(trim(rawWiki($item['id'])));
346 $data[] = $item;
347 return true;
350 /* ------------- helper functions below -------------- */
353 * fulltext sort
355 * Callback sort function for use with usort to sort the data
356 * structure created by search_fulltext. Sorts descending by count
358 * @author Andreas Gohr <andi@splitbrain.org>
360 * @param array $a
361 * @param array $b
363 * @return int
365 function sort_search_fulltext($a,$b){
366 if($a['count'] > $b['count']){
367 return -1;
368 }elseif($a['count'] < $b['count']){
369 return 1;
370 }else{
371 return strcmp($a['id'],$b['id']);
376 * translates a document path to an ID
378 * @author Andreas Gohr <andi@splitbrain.org>
379 * @todo move to pageutils
381 * @param string $path
382 * @param bool $keeptxt
384 * @return mixed|string
386 function pathID($path,$keeptxt=false){
387 $id = utf8_decodeFN($path);
388 $id = str_replace('/',':',$id);
389 if(!$keeptxt) $id = preg_replace('#\.txt$#','',$id);
390 $id = trim($id, ':');
391 return $id;
396 * This is a very universal callback for the search() function, replacing
397 * many of the former individual functions at the cost of a more complex
398 * setup.
400 * How the function behaves, depends on the options passed in the $opts
401 * array, where the following settings can be used.
403 * depth int recursion depth. 0 for unlimited (default: 0)
404 * keeptxt bool keep .txt extension for IDs (default: false)
405 * listfiles bool include files in listing (default: false)
406 * listdirs bool include namespaces in listing (default: false)
407 * pagesonly bool restrict files to pages (default: false)
408 * skipacl bool do not check for READ permission (default: false)
409 * sneakyacl bool don't recurse into nonreadable dirs (default: false)
410 * hash bool create MD5 hash for files (default: false)
411 * meta bool return file metadata (default: false)
412 * filematch string match files against this regexp (default: '', so accept everything)
413 * idmatch string match full ID against this regexp (default: '', so accept everything)
414 * dirmatch string match directory against this regexp when adding (default: '', so accept everything)
415 * nsmatch string match namespace against this regexp when adding (default: '', so accept everything)
416 * recmatch string match directory against this regexp when recursing (default: '', so accept everything)
417 * showmsg bool warn about non-ID files (default: false)
418 * showhidden bool show hidden files(e.g. by hidepages config) too (default: false)
419 * firsthead bool return first heading for pages (default: false)
421 * @param array &$data - Reference to the result data structure
422 * @param string $base - Base usually $conf['datadir']
423 * @param string $file - current file or directory relative to $base
424 * @param string $type - Type either 'd' for directory or 'f' for file
425 * @param int $lvl - Current recursion depht
426 * @param array $opts - option array as given to search()
427 * @return bool if this directory should be traversed (true) or not (false)
428 * return value is ignored for files
430 * @author Andreas Gohr <gohr@cosmocode.de>
432 function search_universal(&$data,$base,$file,$type,$lvl,$opts){
433 $item = array();
434 $return = true;
436 // get ID and check if it is a valid one
437 $item['id'] = pathID($file,($type == 'd' || !empty($opts['keeptxt'])));
438 if($item['id'] != cleanID($item['id'])){
439 if(!empty($opts['showmsg'])){
440 msg(hsc($item['id']).' is not a valid file name for DokuWiki - skipped',-1);
442 return false; // skip non-valid files
444 $item['ns'] = getNS($item['id']);
446 if($type == 'd') {
447 // decide if to recursion into this directory is wanted
448 if(empty($opts['depth'])){
449 $return = true; // recurse forever
450 }else{
451 $depth = substr_count($file,'/');
452 if($depth >= $opts['depth']){
453 $return = false; // depth reached
454 }else{
455 $return = true;
459 if ($return) {
460 $match = empty($opts['recmatch']) || preg_match('/'.$opts['recmatch'].'/',$file);
461 if (!$match) {
462 return false; // doesn't match
467 // check ACL
468 if(empty($opts['skipacl'])){
469 if($type == 'd'){
470 $item['perm'] = auth_quickaclcheck($item['id'].':*');
471 }else{
472 $item['perm'] = auth_quickaclcheck($item['id']); //FIXME check namespace for media files
474 }else{
475 $item['perm'] = AUTH_DELETE;
478 // are we done here maybe?
479 if($type == 'd'){
480 if(empty($opts['listdirs'])) return $return;
481 //neither list nor recurse forbidden items:
482 if(empty($opts['skipacl']) && !empty($opts['sneakyacl']) && $item['perm'] < AUTH_READ) return false;
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;
485 }else{
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'] = \dokuwiki\Utf8\PhpString::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);
508 if($type == 'f'){
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
514 $data[] = $item;
515 return $return;
518 //Setup VIM: ex: et ts=4 :