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