xref: /dokuwiki/inc/search.php (revision d059ba9bb09b872361eee77c46cbd4aba0042e83)
1<?php
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
9if(!defined('DOKU_INC')) die('meh.');
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 * @param   array ref $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 arayy with object,method)
20 * @param   string    $dir  Current directory beyond $base
21 * @param   int       $lvl  Recursion Level
22 * @author  Andreas Gohr <andi@splitbrain.org>
23 */
24function search(&$data,$base,$func,$opts,$dir='',$lvl=1){
25    $dirs   = array();
26    $files  = array();
27
28    //read in directories and files
29    $dh = @opendir($base.'/'.$dir);
30    if(!$dh) return;
31    while(($file = readdir($dh)) !== false){
32        if(preg_match('/^[\._]/',$file)) continue; //skip hidden files and upper dirs
33        if(is_dir($base.'/'.$dir.'/'.$file)){
34            $dirs[] = $dir.'/'.$file;
35            continue;
36        }
37        $files[] = $dir.'/'.$file;
38    }
39    closedir($dh);
40    sort($files);
41    sort($dirs);
42
43    //give directories to userfunction then recurse
44    foreach($dirs as $dir){
45        if (call_user_func_array($func, array(&$data,$base,$dir,'d',$lvl,$opts))){
46            search($data,$base,$func,$opts,$dir,$lvl+1);
47        }
48    }
49    //now handle the files
50    foreach($files as $file){
51        call_user_func_array($func, array(&$data,$base,$file,'f',$lvl,$opts));
52    }
53}
54
55/**
56 * Wrapper around call_user_func_array.
57 *
58 * @deprecated
59 */
60function search_callback($func,&$data,$base,$file,$type,$lvl,$opts){
61    return call_user_func_array($func, array(&$data,$base,$file,$type,$lvl,$opts));
62}
63
64/**
65 * The following functions are userfunctions to use with the search
66 * function above. This function is called for every found file or
67 * directory. When a directory is given to the function it has to
68 * decide if this directory should be traversed (true) or not (false)
69 * The function has to accept the following parameters:
70 *
71 * &$data - Reference to the result data structure
72 * $base  - Base usually $conf['datadir']
73 * $file  - current file or directory relative to $base
74 * $type  - Type either 'd' for directory or 'f' for file
75 * $lvl   - Current recursion depht
76 * $opts  - option array as given to search()
77 *
78 * return values for files are ignored
79 *
80 * All functions should check the ACL for document READ rights
81 * namespaces (directories) are NOT checked as this would break
82 * the recursion (You can have an nonreadable dir over a readable
83 * one deeper nested) also make sure to check the file type (for example
84 * in case of lockfiles).
85 */
86
87/**
88 * Searches for pages beginning with the given query
89 *
90 * @author Andreas Gohr <andi@splitbrain.org>
91 */
92function search_qsearch(&$data,$base,$file,$type,$lvl,$opts){
93    $opts = array(
94            'idmatch'   => '(^|:)'.preg_quote($opts['query'],'/').'/',
95            'listfiles' => true,
96            'pagesonly' => true,
97            );
98    return search_universal($data,$base,$file,$type,$lvl,$opts);
99}
100
101/**
102 * Build the browsable index of pages
103 *
104 * $opts['ns'] is the current namespace
105 *
106 * @author  Andreas Gohr <andi@splitbrain.org>
107 */
108function search_index(&$data,$base,$file,$type,$lvl,$opts){
109    global $conf;
110    $return = true;
111
112    $item = array();
113
114    if($type == 'd' && !preg_match('#^'.$file.'(/|$)#','/'.$opts['ns'])){
115        //add but don't recurse
116        $return = false;
117    }elseif($type == 'f' && ($opts['nofiles'] || substr($file,-4) != '.txt')){
118        //don't add
119        return false;
120    }
121
122    $id = pathID($file);
123
124    if($type=='d' && $conf['sneaky_index'] && auth_quickaclcheck($id.':') < AUTH_READ){
125        return false;
126    }
127
128    //check hidden
129    if(isHiddenPage($id)){
130        return false;
131    }
132
133    //check ACL
134    if($type=='f' && auth_quickaclcheck($id) < AUTH_READ){
135        return false;
136    }
137
138    $data[]=array( 'id'    => $id,
139            'type'  => $type,
140            'level' => $lvl,
141            'open'  => $return );
142    return $return;
143}
144
145/**
146 * List all namespaces
147 *
148 * @author  Andreas Gohr <andi@splitbrain.org>
149 */
150function search_namespaces(&$data,$base,$file,$type,$lvl,$opts){
151    $opts = array(
152            'listdirs' => true,
153            );
154    return search_universal($data,$base,$file,$type,$lvl,$opts);
155}
156
157/**
158 * List all mediafiles in a namespace
159 *
160 * @author  Andreas Gohr <andi@splitbrain.org>
161 */
162function search_media(&$data,$base,$file,$type,$lvl,$opts){
163
164    //we do nothing with directories
165    if($type == 'd') {
166        if(!$opts['depth']) return true; // recurse forever
167        $depth = substr_count($file,'/');
168        if($depth >= $opts['depth']) return false; // depth reached
169        return true;
170    }
171
172    $info         = array();
173    $info['id']   = pathID($file,true);
174    if($info['id'] != cleanID($info['id'])){
175        if($opts['showmsg'])
176            msg(hsc($info['id']).' is not a valid file name for DokuWiki - skipped',-1);
177        return false; // skip non-valid files
178    }
179
180    //check ACL for namespace (we have no ACL for mediafiles)
181    $info['perm'] = auth_quickaclcheck(getNS($info['id']).':*');
182    if(!$opts['skipacl'] && $info['perm'] < AUTH_READ){
183        return false;
184    }
185
186    //check pattern filter
187    if($opts['pattern'] && !@preg_match($opts['pattern'], $info['id'])){
188        return false;
189    }
190
191    $info['file']     = basename($file);
192    $info['size']     = filesize($base.'/'.$file);
193    $info['mtime']    = filemtime($base.'/'.$file);
194    $info['writable'] = is_writable($base.'/'.$file);
195    if(preg_match("/\.(jpe?g|gif|png)$/",$file)){
196        $info['isimg'] = true;
197        $info['meta']  = new JpegMeta($base.'/'.$file);
198    }else{
199        $info['isimg'] = false;
200    }
201    if($opts['hash']){
202        $info['hash'] = md5(io_readFile(mediaFN($info['id']),false));
203    }
204
205    $data[] = $info;
206
207    return false;
208}
209
210/**
211 * This function just lists documents (for RSS namespace export)
212 *
213 * @author  Andreas Gohr <andi@splitbrain.org>
214 */
215function search_list(&$data,$base,$file,$type,$lvl,$opts){
216    //we do nothing with directories
217    if($type == 'd') return false;
218    //only search txt files
219    if(substr($file,-4) == '.txt'){
220        //check ACL
221        $id = pathID($file);
222        if(auth_quickaclcheck($id) < AUTH_READ){
223            return false;
224        }
225        $data[]['id'] = $id;
226    }
227    return false;
228}
229
230/**
231 * Quicksearch for searching matching pagenames
232 *
233 * $opts['query'] is the search query
234 *
235 * @author  Andreas Gohr <andi@splitbrain.org>
236 */
237function search_pagename(&$data,$base,$file,$type,$lvl,$opts){
238    //we do nothing with directories
239    if($type == 'd') return true;
240    //only search txt files
241    if(substr($file,-4) != '.txt') return true;
242
243    //simple stringmatching
244    if (!empty($opts['query'])){
245        if(strpos($file,$opts['query']) !== false){
246            //check ACL
247            $id = pathID($file);
248            if(auth_quickaclcheck($id) < AUTH_READ){
249                return false;
250            }
251            $data[]['id'] = $id;
252        }
253    }
254    return true;
255}
256
257/**
258 * Just lists all documents
259 *
260 * $opts['depth']   recursion level, 0 for all
261 * $opts['hash']    do md5 sum of content?
262 * $opts['skipacl'] list everything regardless of ACL
263 *
264 * @author  Andreas Gohr <andi@splitbrain.org>
265 */
266function search_allpages(&$data,$base,$file,$type,$lvl,$opts){
267    //we do nothing with directories
268    if($type == 'd'){
269        if(!$opts['depth']) return true; // recurse forever
270        $parts = explode('/',ltrim($file,'/'));
271        if(count($parts) == $opts['depth']) return false; // depth reached
272        return true;
273    }
274
275    //only search txt files
276    if(substr($file,-4) != '.txt') return true;
277
278    $item['id']   = pathID($file);
279    if(!$opts['skipacl'] && auth_quickaclcheck($item['id']) < AUTH_READ){
280        return false;
281    }
282
283    $item['rev']   = filemtime($base.'/'.$file);
284    $item['mtime'] = $item['rev'];
285    $item['size']  = filesize($base.'/'.$file);
286    if($opts['hash']){
287        $item['hash'] = md5(trim(rawWiki($item['id'])));
288    }
289
290    $data[] = $item;
291    return true;
292}
293
294/**
295 * Search for backlinks to a given page
296 *
297 * $opts['ns']    namespace of the page
298 * $opts['name']  name of the page without namespace
299 *
300 * @author  Andreas Gohr <andi@splitbrain.org>
301 * @deprecated Replaced by ft_backlinks()
302 */
303function search_backlinks(&$data,$base,$file,$type,$lvl,$opts){
304    //we do nothing with directories
305    if($type == 'd') return true;
306    //only search txt files
307    if(substr($file,-4) != '.txt') return true;
308
309    //absolute search id
310    $sid = cleanID($opts['ns'].':'.$opts['name']);
311
312    //current id and namespace
313    $cid = pathID($file);
314    $cns = getNS($cid);
315
316    //check ACL
317    if(auth_quickaclcheck($cid) < AUTH_READ){
318        return false;
319    }
320
321    //fetch instructions
322    $instructions = p_cached_instructions($base.$file,true);
323    if(is_null($instructions)) return false;
324
325    //check all links for match
326    foreach($instructions as $ins){
327        if($ins[0] == 'internallink' || ($conf['camelcase'] && $ins[0] == 'camelcaselink') ){
328            $mid = $ins[1][0];
329            resolve_pageid($cns,$mid,$exists); //exists is not used
330            if($mid == $sid){
331                //we have a match - finish
332                $data[]['id'] = $cid;
333                break;
334            }
335        }
336    }
337
338    return false;
339}
340
341/**
342 * Fulltextsearch
343 *
344 * $opts['query'] is the search query
345 *
346 * @author  Andreas Gohr <andi@splitbrain.org>
347 * @deprecated - fulltext indexer is used instead
348 */
349function search_fulltext(&$data,$base,$file,$type,$lvl,$opts){
350    //we do nothing with directories
351    if($type == 'd') return true;
352    //only search txt files
353    if(substr($file,-4) != '.txt') return true;
354
355    //check ACL
356    $id = pathID($file);
357    if(auth_quickaclcheck($id) < AUTH_READ){
358        return false;
359    }
360
361    //create regexp from queries
362    $poswords = array();
363    $negwords = array();
364    $qpreg = preg_split('/\s+/',$opts['query']);
365
366    foreach($qpreg as $word){
367        switch(substr($word,0,1)){
368            case '-':
369                if(strlen($word) > 1){  // catch single '-'
370                    array_push($negwords,preg_quote(substr($word,1),'#'));
371                }
372                break;
373            case '+':
374                if(strlen($word) > 1){  // catch single '+'
375                    array_push($poswords,preg_quote(substr($word,1),'#'));
376                }
377                break;
378            default:
379                array_push($poswords,preg_quote($word,'#'));
380                break;
381        }
382    }
383
384    // a search without any posword is useless
385    if (!count($poswords)) return true;
386
387    $reg  = '^(?=.*?'.join(')(?=.*?',$poswords).')';
388            $reg .= count($negwords) ? '((?!'.join('|',$negwords).').)*$' : '.*$';
389            search_regex($data,$base,$file,$reg,$poswords);
390            return true;
391            }
392
393            /**
394             * Reference search
395             * This fuction searches for existing references to a given media file
396             * and returns an array with the found pages. It doesn't pay any
397             * attention to ACL permissions to find every reference. The caller
398             * must check if the user has the appropriate rights to see the found
399             * page and eventually have to prevent the result from displaying.
400             *
401             * @param array  $data Reference to the result data structure
402             * @param string $base Base usually $conf['datadir']
403             * @param string $file current file or directory relative to $base
404             * @param char   $type Type either 'd' for directory or 'f' for file
405             * @param int    $lvl  Current recursion depht
406             * @param mixed  $opts option array as given to search()
407             *
408             * $opts['query'] is the demanded media file name
409             *
410             * @author  Andreas Gohr <andi@splitbrain.org>
411             * @author  Matthias Grimm <matthiasgrimm@users.sourceforge.net>
412             */
413function search_reference(&$data,$base,$file,$type,$lvl,$opts){
414    global $conf;
415
416    //we do nothing with directories
417    if($type == 'd') return true;
418
419    //only search txt files
420    if(substr($file,-4) != '.txt') return true;
421
422    //we finish after 'cnt' references found. The return value
423    //'false' will skip subdirectories to speed search up.
424    $cnt = $conf['refshow'] > 0 ? $conf['refshow'] : 1;
425    if(count($data) >= $cnt) return false;
426
427    $reg = '\{\{ *\:?'.$opts['query'].' *(\|.*)?\}\}';
428    search_regex($data,$base,$file,$reg,array($opts['query']));
429    return true;
430}
431
432/* ------------- helper functions below -------------- */
433
434/**
435 * fulltext search helper
436 * searches a text file with a given regular expression
437 * no ACL checks are performed. This have to be done by
438 * the caller if necessary.
439 *
440 * @param array  $data  reference to array for results
441 * @param string $base  base directory
442 * @param string $file  file name to search in
443 * @param string $reg   regular expression to search for
444 * @param array  $words words that should be marked in the results
445 *
446 * @author  Andreas Gohr <andi@splitbrain.org>
447 * @author  Matthias Grimm <matthiasgrimm@users.sourceforge.net>
448 *
449 * @deprecated - fulltext indexer is used instead
450 */
451function search_regex(&$data,$base,$file,$reg,$words){
452
453    //get text
454    $text = io_readfile($base.'/'.$file);
455    //lowercase text (u modifier does not help with case)
456    $lctext = utf8_strtolower($text);
457
458    //do the fulltext search
459    $matches = array();
460    if($cnt = preg_match_all('#'.$reg.'#usi',$lctext,$matches)){
461        //this is not the best way for snippet generation but the fastest I could find
462        $q = $words[0];  //use first word for snippet creation
463        $p = utf8_strpos($lctext,$q);
464        $f = $p - 100;
465        $l = utf8_strlen($q) + 200;
466        if($f < 0) $f = 0;
467        $snippet = '<span class="search_sep"> ... </span>'.
468            htmlspecialchars(utf8_substr($text,$f,$l)).
469            '<span class="search_sep"> ... </span>';
470        $mark    = '('.join('|', $words).')';
471        $snippet = preg_replace('#'.$mark.'#si','<strong class="search_hit">\\1</strong>',$snippet);
472
473        $data[] = array(
474                'id'       => pathID($file),
475                'count'    => preg_match_all('#'.$mark.'#usi',$lctext,$matches),
476                'poswords' => join(' ',$words),
477                'snippet'  => $snippet,
478                );
479    }
480
481    return true;
482}
483
484
485/**
486 * fulltext sort
487 *
488 * Callback sort function for use with usort to sort the data
489 * structure created by search_fulltext. Sorts descending by count
490 *
491 * @author  Andreas Gohr <andi@splitbrain.org>
492 */
493function sort_search_fulltext($a,$b){
494    if($a['count'] > $b['count']){
495        return -1;
496    }elseif($a['count'] < $b['count']){
497        return 1;
498    }else{
499        return strcmp($a['id'],$b['id']);
500    }
501}
502
503/**
504 * translates a document path to an ID
505 *
506 * @author  Andreas Gohr <andi@splitbrain.org>
507 * @todo    move to pageutils
508 */
509function pathID($path,$keeptxt=false){
510    $id = utf8_decodeFN($path);
511    $id = str_replace('/',':',$id);
512    if(!$keeptxt) $id = preg_replace('#\.txt$#','',$id);
513    $id = preg_replace('#^:+#','',$id);
514    $id = preg_replace('#:+$#','',$id);
515    return $id;
516}
517
518
519/**
520 * This is a very universal callback for the search() function, replacing
521 * many of the former individual functions at the cost of a more complex
522 * setup.
523 *
524 * How the function behaves, depends on the options passed in the $opts
525 * array, where the following settings can be used.
526 *
527 * depth      int     recursion depth. 0 for unlimited
528 * keeptxt    bool    keep .txt extension for IDs
529 * listfiles  bool    include files in listing
530 * listdirs   bool    include namespaces in listing
531 * pagesonly  bool    restrict files to pages
532 * skipacl    bool    do not check for READ permission
533 * sneakyacl  bool    don't recurse into nonreadable dirs
534 * hash       bool    create MD5 hash for files
535 * meta       bool    return file metadata
536 * filematch  string  match files against this regexp
537 * idmatch    string  match full ID against this regexp
538 * dirmatch   string  match directory against this regexp when adding
539 * nsmatch    string  match namespace against this regexp when adding
540 * recmatch   string  match directory against this regexp when recursing
541 * showmsg    bool    warn about non-ID files
542 * showhidden bool    show hidden files too
543 * firsthead  bool    return first heading for pages
544 *
545 * @author Andreas Gohr <gohr@cosmocode.de>
546 */
547function search_universal(&$data,$base,$file,$type,$lvl,$opts){
548    $item   = array();
549    $return = true;
550
551    // get ID and check if it is a valid one
552    $item['id'] = pathID($file);
553    if($item['id'] != cleanID($item['id'])){
554        if($opts['showmsg'])
555            msg(hsc($item['id']).' is not a valid file name for DokuWiki - skipped',-1);
556        return false; // skip non-valid files
557    }
558    $item['ns']  = getNS($item['id']);
559
560    if($type == 'd') {
561        // decide if to recursion into this directory is wanted
562        if(!$opts['depth']){
563            $return = true; // recurse forever
564        }else{
565            $depth = substr_count($file,'/');
566            if($depth >= $opts['depth']){
567                $return = false; // depth reached
568            }else{
569                $return = true;
570            }
571        }
572        if($return && !preg_match('/'.$opts['recmatch'].'/',$file)){
573            $return = false; // doesn't match
574        }
575    }
576
577    // check ACL
578    if(!$opts['skipacl']){
579        if($type == 'd'){
580            $item['perm'] = auth_quickaclcheck($item['id'].':*');
581        }else{
582            $item['perm'] = auth_quickaclcheck($item['id']); //FIXME check namespace for media files
583        }
584    }else{
585        $item['perm'] = AUTH_DELETE;
586    }
587
588    // are we done here maybe?
589    if($type == 'd'){
590        if(!$opts['listdirs']) return $return;
591        if(!$opts['skipacl'] && $opts['sneakyacl'] && $item['perm'] < AUTH_READ) return false; //neither list nor recurse
592        if($opts['dirmatch'] && !preg_match('/'.$opts['dirmatch'].'/',$file)) return $return;
593        if($opts['nsmatch'] && !preg_match('/'.$opts['nsmatch'].'/',$item['ns'])) return $return;
594    }else{
595        if(!$opts['listfiles']) return $return;
596        if(!$opts['skipacl'] && $item['perm'] < AUTH_READ) return $return;
597        if($opts['pagesonly'] && (substr($file,-4) != '.txt')) return $return;
598        if(!$conf['showhidden'] && isHiddenPage($id)) return $return;
599        if($opts['filematch'] && !preg_match('/'.$opts['filematch'].'/',$file)) return $return;
600        if($opts['idmatch'] && !preg_match('/'.$opts['idmatch'].'/',$item['id'])) return $return;
601    }
602
603    // still here? prepare the item
604    $item['type']  = $type;
605    $item['level'] = $lvl;
606    $item['open']  = $return;
607
608    if($opts['meta']){
609        $item['file']       = basename($file);
610        $item['size']       = filesize($base.'/'.$file);
611        $item['mtime']      = filemtime($base.'/'.$file);
612        $item['rev']        = $item['mtime'];
613        $item['writable']   = is_writable($base.'/'.$file);
614        $item['executable'] = is_executable($base.'/'.$file);
615    }
616
617    if($type == 'f'){
618        if($opts['hash']) $item['hash'] = md5(io_readFile($base.'/'.$file,false));
619        if($opts['firsthead']) $item['title'] = p_get_first_heading($item['id'],false);
620    }
621
622    // finally add the item
623    $data[] = $item;
624    return $return;
625}
626
627//Setup VIM: ex: et ts=4 enc=utf-8 :
628