xref: /dokuwiki/inc/search.php (revision a1207a93a037fd8bed3efabaed4b58e7ee6397ca)
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
9  if(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/');
10  require_once(DOKU_INC.'inc/common.php');
11
12/**
13 * recurse direcory
14 *
15 * This function recurses into a given base directory
16 * and calls the supplied function for each file and directory
17 *
18 * @param   array ref $data The results of the search are stored here
19 * @param   string    $base Where to start the search
20 * @param   callback  $func Callback (function name or arayy with object,method)
21 * @param   string    $dir  Current directory beyond $base
22 * @param   int       $lvl  Recursion Level
23 * @author  Andreas Gohr <andi@splitbrain.org>
24 */
25function search(&$data,$base,$func,$opts,$dir='',$lvl=1){
26  $dirs   = array();
27  $files  = array();
28
29  //read in directories and files
30  $dh = @opendir($base.'/'.$dir);
31  if(!$dh) return;
32  while(($file = readdir($dh)) !== false){
33    if(preg_match('/^[\._]/',$file)) continue; //skip hidden files and upper dirs
34    if(is_dir($base.'/'.$dir.'/'.$file)){
35      $dirs[] = $dir.'/'.$file;
36      continue;
37    }
38    $files[] = $dir.'/'.$file;
39  }
40  closedir($dh);
41  sort($files);
42  sort($dirs);
43
44  //give directories to userfunction then recurse
45  foreach($dirs as $dir){
46    if (search_callback($func,$data,$base,$dir,'d',$lvl,$opts)){
47      search($data,$base,$func,$opts,$dir,$lvl+1);
48    }
49  }
50  //now handle the files
51  foreach($files as $file){
52    search_callback($func,$data,$base,$file,'f',$lvl,$opts);
53  }
54}
55
56/**
57 * Used to run a user callback
58 *
59 * Makes sure the $data array is passed by reference (unlike when using
60 * call_user_func())
61 *
62 * @todo If this can be generalized it may be useful elsewhere in the code
63 * @author Andreas Gohr <andi@splitbrain.org>
64 */
65function search_callback($func,&$data,$base,$file,$type,$lvl,$opts){
66  if(is_array($func)){
67    if(is_object($func[0])){
68      // instanciated object
69      return $func[0]->$func[1]($data,$base,$file,$type,$lvl,$opts);
70    }else{
71      // static call
72      $f = $func[0].'::'.$func[1];
73      return $f($data,$base,$file,$type,$lvl,$opts);
74    }
75  }
76  // simple function call
77  return $func($data,$base,$file,$type,$lvl,$opts);
78}
79
80/**
81 * The following functions are userfunctions to use with the search
82 * function above. This function is called for every found file or
83 * directory. When a directory is given to the function it has to
84 * decide if this directory should be traversed (true) or not (false)
85 * The function has to accept the following parameters:
86 *
87 * &$data - Reference to the result data structure
88 * $base  - Base usually $conf['datadir']
89 * $file  - current file or directory relative to $base
90 * $type  - Type either 'd' for directory or 'f' for file
91 * $lvl   - Current recursion depht
92 * $opts  - option array as given to search()
93 *
94 * return values for files are ignored
95 *
96 * All functions should check the ACL for document READ rights
97 * namespaces (directories) are NOT checked as this would break
98 * the recursion (You can have an nonreadable dir over a readable
99 * one deeper nested) also make sure to check the file type (for example
100 * in case of lockfiles).
101 */
102
103/**
104 * Searches for pages beginning with the given query
105 *
106 * @author Andreas Gohr <andi@splitbrain.org>
107 */
108function search_qsearch(&$data,$base,$file,$type,$lvl,$opts){
109  $item = array();
110
111  if($type == 'd'){
112    return false; //no handling yet
113  }
114
115  //only search txt files
116  if(substr($file,-4) != '.txt') return false;
117
118  //get id
119  $id = pathID($file);
120
121  //check if it matches the query
122  if(!preg_match('/^'.preg_quote($opts['query'],'/').'/u',$id)){
123    return false;
124  }
125
126  //check ACL
127  if(auth_quickaclcheck($id) < AUTH_READ){
128    return false;
129  }
130
131  $data[]=array( 'id'    => $id,
132                 'type'  => $type,
133                 'level' => 1,
134                 'open'  => true);
135  return true;
136}
137
138/**
139 * Build the browsable index of pages
140 *
141 * $opts['ns'] is the current namespace
142 *
143 * @author  Andreas Gohr <andi@splitbrain.org>
144 */
145function search_index(&$data,$base,$file,$type,$lvl,$opts){
146  global $conf;
147  $return = true;
148
149  $item = array();
150
151  if($type == 'd' && !preg_match('#^'.$file.'(/|$)#','/'.$opts['ns'])){
152    //add but don't recurse
153    $return = false;
154  }elseif($type == 'f' && ($opts['nofiles'] || substr($file,-4) != '.txt')){
155    //don't add
156    return false;
157  }
158
159  $id = pathID($file);
160
161  if($type=='d' && $conf['sneaky_index'] && auth_quickaclcheck($id.':') < AUTH_READ){
162    return false;
163  }
164
165  //check hidden
166  if(isHiddenPage($id)){
167    return false;
168  }
169
170  //check ACL
171  if($type=='f' && auth_quickaclcheck($id) < AUTH_READ){
172    return false;
173  }
174
175  $data[]=array( 'id'    => $id,
176                 'type'  => $type,
177                 'level' => $lvl,
178                 'open'  => $return );
179  return $return;
180}
181
182/**
183 * List all namespaces
184 *
185 * @author  Andreas Gohr <andi@splitbrain.org>
186 */
187function search_namespaces(&$data,$base,$file,$type,$lvl,$opts){
188  if($type == 'f') return true; //nothing to do on files
189
190  $id = pathID($file);
191  $data[]=array( 'id'    => $id,
192                 'type'  => $type,
193                 'level' => $lvl );
194  return true;
195}
196
197/**
198 * List all mediafiles in a namespace
199 *
200 * @author  Andreas Gohr <andi@splitbrain.org>
201 */
202function search_media(&$data,$base,$file,$type,$lvl,$opts){
203  //we do nothing with directories
204  if($type == 'd') return false;
205
206  $info         = array();
207  $info['id']   = pathID($file,true);
208
209  //check ACL for namespace (we have no ACL for mediafiles)
210  if(auth_quickaclcheck(getNS($info['id']).':*') < AUTH_READ){
211    return false;
212  }
213
214  $info['file'] = 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    require_once(DOKU_INC.'inc/JpegMeta.php');
221    $info['meta']  = new JpegMeta($base.'/'.$file);
222  }else{
223    $info['isimg'] = false;
224  }
225  $data[] = $info;
226
227  return false;
228}
229
230/**
231 * This function just lists documents (for RSS namespace export)
232 *
233 * @author  Andreas Gohr <andi@splitbrain.org>
234 */
235function search_list(&$data,$base,$file,$type,$lvl,$opts){
236  //we do nothing with directories
237  if($type == 'd') return false;
238  //only search txt files
239  if(substr($file,-4) == '.txt'){
240    //check ACL
241    $id = pathID($file);
242    if(auth_quickaclcheck($id) < AUTH_READ){
243      return false;
244    }
245    $data[]['id'] = $id;
246  }
247  return false;
248}
249
250/**
251 * Quicksearch for searching matching pagenames
252 *
253 * $opts['query'] is the search query
254 *
255 * @author  Andreas Gohr <andi@splitbrain.org>
256 */
257function search_pagename(&$data,$base,$file,$type,$lvl,$opts){
258  //we do nothing with directories
259  if($type == 'd') return true;
260  //only search txt files
261  if(substr($file,-4) != '.txt') return true;
262
263  //simple stringmatching
264  if (!empty($opts['query'])){
265    if(strpos($file,$opts['query']) !== false){
266      //check ACL
267      $id = pathID($file);
268      if(auth_quickaclcheck($id) < AUTH_READ){
269        return false;
270      }
271      $data[]['id'] = $id;
272    }
273  }
274  return true;
275}
276
277/**
278 * Just lists all documents
279 *
280 * @author  Andreas Gohr <andi@splitbrain.org>
281 */
282function search_allpages(&$data,$base,$file,$type,$lvl,$opts){
283  //we do nothing with directories
284  if($type == 'd') return true;
285  //only search txt files
286  if(substr($file,-4) != '.txt') return true;
287
288  $data[]['id'] = pathID($file);
289  return true;
290}
291
292/**
293 * Search for backlinks to a given page
294 *
295 * $opts['ns']    namespace of the page
296 * $opts['name']  name of the page without namespace
297 *
298 * @author  Andreas Gohr <andi@splitbrain.org>
299 * @deprecated Replaced by ft_backlinks()
300 */
301function search_backlinks(&$data,$base,$file,$type,$lvl,$opts){
302  //we do nothing with directories
303  if($type == 'd') return true;
304  //only search txt files
305  if(substr($file,-4) != '.txt') return true;
306
307  //absolute search id
308  $sid = cleanID($opts['ns'].':'.$opts['name']);
309
310  //current id and namespace
311  $cid = pathID($file);
312  $cns = getNS($cid);
313
314  //check ACL
315  if(auth_quickaclcheck($cid) < AUTH_READ){
316    return false;
317  }
318
319  //fetch instructions
320  require_once(DOKU_INC.'inc/parserutils.php');
321  $instructions = p_cached_instructions($base.$file,true);
322  if(is_null($instructions)) return false;
323
324  //check all links for match
325  foreach($instructions as $ins){
326    if($ins[0] == 'internallink' || ($conf['camelcase'] && $ins[0] == 'camelcaselink') ){
327      $mid = $ins[1][0];
328      resolve_pageid($cns,$mid,$exists); //exists is not used
329      if($mid == $sid){
330        //we have a match - finish
331        $data[]['id'] = $cid;
332        break;
333      }
334    }
335  }
336
337  return false;
338}
339
340/**
341 * Fulltextsearch
342 *
343 * $opts['query'] is the search query
344 *
345 * @author  Andreas Gohr <andi@splitbrain.org>
346 * @deprecated - fulltext indexer is used instead
347 */
348function search_fulltext(&$data,$base,$file,$type,$lvl,$opts){
349  //we do nothing with directories
350  if($type == 'd') return true;
351  //only search txt files
352  if(substr($file,-4) != '.txt') return true;
353
354  //check ACL
355  $id = pathID($file);
356  if(auth_quickaclcheck($id) < AUTH_READ){
357    return false;
358  }
359
360  //create regexp from queries
361  $poswords = array();
362  $negwords = array();
363  $qpreg = preg_split('/\s+/',$opts['query']);
364
365  foreach($qpreg as $word){
366    switch(substr($word,0,1)){
367      case '-':
368        if(strlen($word) > 1){  // catch single '-'
369          array_push($negwords,preg_quote(substr($word,1),'#'));
370        }
371        break;
372      case '+':
373        if(strlen($word) > 1){  // catch single '+'
374          array_push($poswords,preg_quote(substr($word,1),'#'));
375        }
376        break;
377      default:
378        array_push($poswords,preg_quote($word,'#'));
379        break;
380    }
381  }
382
383  // a search without any posword is useless
384  if (!count($poswords)) return true;
385
386  $reg  = '^(?=.*?'.join(')(?=.*?',$poswords).')';
387  $reg .= count($negwords) ? '((?!'.join('|',$negwords).').)*$' : '.*$';
388  search_regex($data,$base,$file,$reg,$poswords);
389  return true;
390}
391
392/**
393 * Reference search
394 * This fuction searches for existing references to a given media file
395 * and returns an array with the found pages. It doesn't pay any
396 * attention to ACL permissions to find every reference. The caller
397 * must check if the user has the appropriate rights to see the found
398 * page and eventually have to prevent the result from displaying.
399 *
400 * @param array  $data Reference to the result data structure
401 * @param string $base Base usually $conf['datadir']
402 * @param string $file current file or directory relative to $base
403 * @param char   $type Type either 'd' for directory or 'f' for file
404 * @param int    $lvl  Current recursion depht
405 * @param mixed  $opts option array as given to search()
406 *
407 * $opts['query'] is the demanded media file name
408 *
409 * @author  Andreas Gohr <andi@splitbrain.org>
410 * @author  Matthias Grimm <matthiasgrimm@users.sourceforge.net>
411 */
412function search_reference(&$data,$base,$file,$type,$lvl,$opts){
413  global $conf;
414
415  //we do nothing with directories
416  if($type == 'd') return true;
417
418  //only search txt files
419  if(substr($file,-4) != '.txt') return true;
420
421  //we finish after 'cnt' references found. The return value
422  //'false' will skip subdirectories to speed search up.
423  $cnt = $conf['refshow'] > 0 ? $conf['refshow'] : 1;
424  if(count($data) >= $cnt) return false;
425
426  $reg = '\{\{ *\:?'.$opts['query'].' *(\|.*)?\}\}';
427  search_regex($data,$base,$file,$reg,array($opts['query']));
428  return true;
429}
430
431/* ------------- helper functions below -------------- */
432
433/**
434 * fulltext search helper
435 * searches a text file with a given regular expression
436 * no ACL checks are performed. This have to be done by
437 * the caller if necessary.
438 *
439 * @param array  $data  reference to array for results
440 * @param string $base  base directory
441 * @param string $file  file name to search in
442 * @param string $reg   regular expression to search for
443 * @param array  $words words that should be marked in the results
444 *
445 * @author  Andreas Gohr <andi@splitbrain.org>
446 * @author  Matthias Grimm <matthiasgrimm@users.sourceforge.net>
447 *
448 * @deprecated - fulltext indexer is used instead
449 */
450function search_regex(&$data,$base,$file,$reg,$words){
451
452  //get text
453  $text = io_readfile($base.'/'.$file);
454  //lowercase text (u modifier does not help with case)
455  $lctext = utf8_strtolower($text);
456
457  //do the fulltext search
458  $matches = array();
459  if($cnt = preg_match_all('#'.$reg.'#usi',$lctext,$matches)){
460    //this is not the best way for snippet generation but the fastest I could find
461    $q = $words[0];  //use first word for snippet creation
462    $p = utf8_strpos($lctext,$q);
463    $f = $p - 100;
464    $l = utf8_strlen($q) + 200;
465    if($f < 0) $f = 0;
466    $snippet = '<span class="search_sep"> ... </span>'.
467               htmlspecialchars(utf8_substr($text,$f,$l)).
468               '<span class="search_sep"> ... </span>';
469    $mark    = '('.join('|', $words).')';
470    $snippet = preg_replace('#'.$mark.'#si','<strong class="search_hit">\\1</strong>',$snippet);
471
472    $data[] = array(
473      'id'       => pathID($file),
474      'count'    => preg_match_all('#'.$mark.'#usi',$lctext,$matches),
475      'poswords' => join(' ',$words),
476      'snippet'  => $snippet,
477    );
478  }
479
480  return true;
481}
482
483
484/**
485 * fulltext sort
486 *
487 * Callback sort function for use with usort to sort the data
488 * structure created by search_fulltext. Sorts descending by count
489 *
490 * @author  Andreas Gohr <andi@splitbrain.org>
491 */
492function sort_search_fulltext($a,$b){
493  if($a['count'] > $b['count']){
494    return -1;
495  }elseif($a['count'] < $b['count']){
496    return 1;
497  }else{
498    return strcmp($a['id'],$b['id']);
499  }
500}
501
502/**
503 * translates a document path to an ID
504 *
505 * @author  Andreas Gohr <andi@splitbrain.org>
506 * @todo    move to pageutils
507 */
508function pathID($path,$keeptxt=false){
509  $id = utf8_decodeFN($path);
510  $id = str_replace('/',':',$id);
511  if(!$keeptxt) $id = preg_replace('#\.txt$#','',$id);
512  $id = preg_replace('#^:+#','',$id);
513  $id = preg_replace('#:+$#','',$id);
514  return $id;
515}
516
517
518//Setup VIM: ex: et ts=2 enc=utf-8 :
519