xref: /dokuwiki/inc/search.php (revision b8219d2d5e3003683926bd4f9d7bd37ca93b95ec)
1ed7b5f09Sandi<?php
215fae107Sandi/**
315fae107Sandi * DokuWiki search functions
415fae107Sandi *
515fae107Sandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
615fae107Sandi * @author     Andreas Gohr <andi@splitbrain.org>
715fae107Sandi */
8f3f0262cSandi
9fa8adffeSAndreas Gohrif(!defined('DOKU_INC')) die('meh.');
10ed7b5f09Sandirequire_once(DOKU_INC.'inc/common.php');
11f3f0262cSandi
12f3f0262cSandi/**
1315fae107Sandi * recurse direcory
1415fae107Sandi *
15f3f0262cSandi * This function recurses into a given base directory
16f3f0262cSandi * and calls the supplied function for each file and directory
1715fae107Sandi *
1824baa045SAndreas Gohr * @param   array ref $data The results of the search are stored here
1924baa045SAndreas Gohr * @param   string    $base Where to start the search
2024baa045SAndreas Gohr * @param   callback  $func Callback (function name or arayy with object,method)
2124baa045SAndreas Gohr * @param   string    $dir  Current directory beyond $base
2224baa045SAndreas Gohr * @param   int       $lvl  Recursion Level
2315fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
24f3f0262cSandi */
25f3f0262cSandifunction search(&$data,$base,$func,$opts,$dir='',$lvl=1){
26f3f0262cSandi  $dirs   = array();
27f3f0262cSandi  $files  = array();
28f3f0262cSandi
29f3f0262cSandi  //read in directories and files
30f3f0262cSandi  $dh = @opendir($base.'/'.$dir);
31f3f0262cSandi  if(!$dh) return;
32f3f0262cSandi  while(($file = readdir($dh)) !== false){
33de3dfc91Sandi    if(preg_match('/^[\._]/',$file)) continue; //skip hidden files and upper dirs
34f3f0262cSandi    if(is_dir($base.'/'.$dir.'/'.$file)){
35f3f0262cSandi      $dirs[] = $dir.'/'.$file;
36f3f0262cSandi      continue;
37f3f0262cSandi    }
38f3f0262cSandi    $files[] = $dir.'/'.$file;
39f3f0262cSandi  }
40f3f0262cSandi  closedir($dh);
41f3f0262cSandi  sort($files);
42f3f0262cSandi  sort($dirs);
43f3f0262cSandi
44f3f0262cSandi  //give directories to userfunction then recurse
45f3f0262cSandi  foreach($dirs as $dir){
46d8126df2SGina Haeussge    if (call_user_func_array($func, array(&$data,$base,$dir,'d',$lvl,$opts))){
47f3f0262cSandi      search($data,$base,$func,$opts,$dir,$lvl+1);
48f3f0262cSandi    }
49f3f0262cSandi  }
50f3f0262cSandi  //now handle the files
51f3f0262cSandi  foreach($files as $file){
52d8126df2SGina Haeussge    call_user_func_array($func, array(&$data,$base,$file,'f',$lvl,$opts));
53f3f0262cSandi  }
54f3f0262cSandi}
55f3f0262cSandi
56f3f0262cSandi/**
57d8126df2SGina Haeussge * Wrapper around call_user_func_array.
5824baa045SAndreas Gohr *
59d8126df2SGina Haeussge * @deprecated
6024baa045SAndreas Gohr */
6124baa045SAndreas Gohrfunction search_callback($func,&$data,$base,$file,$type,$lvl,$opts){
62d8126df2SGina Haeussge  return call_user_func_array($func, array(&$data,$base,$file,$type,$lvl,$opts));
6324baa045SAndreas Gohr}
6424baa045SAndreas Gohr
6524baa045SAndreas Gohr/**
66f3f0262cSandi * The following functions are userfunctions to use with the search
67f3f0262cSandi * function above. This function is called for every found file or
68f3f0262cSandi * directory. When a directory is given to the function it has to
69f3f0262cSandi * decide if this directory should be traversed (true) or not (false)
70f3f0262cSandi * The function has to accept the following parameters:
71f3f0262cSandi *
72f3f0262cSandi * &$data - Reference to the result data structure
73f3f0262cSandi * $base  - Base usually $conf['datadir']
74f3f0262cSandi * $file  - current file or directory relative to $base
75f3f0262cSandi * $type  - Type either 'd' for directory or 'f' for file
76f3f0262cSandi * $lvl   - Current recursion depht
77f3f0262cSandi * $opts  - option array as given to search()
78f3f0262cSandi *
79f3f0262cSandi * return values for files are ignored
80f3f0262cSandi *
81f3f0262cSandi * All functions should check the ACL for document READ rights
82f3f0262cSandi * namespaces (directories) are NOT checked as this would break
83f3f0262cSandi * the recursion (You can have an nonreadable dir over a readable
840e1a261eSMichael Klier * one deeper nested) also make sure to check the file type (for example
850e1a261eSMichael Klier * in case of lockfiles).
86f3f0262cSandi */
87f3f0262cSandi
88f3f0262cSandi/**
8963f2400bSandi * Searches for pages beginning with the given query
9063f2400bSandi *
9163f2400bSandi * @author Andreas Gohr <andi@splitbrain.org>
9263f2400bSandi */
9363f2400bSandifunction search_qsearch(&$data,$base,$file,$type,$lvl,$opts){
9463f2400bSandi  $item = array();
9563f2400bSandi
9663f2400bSandi  if($type == 'd'){
9763f2400bSandi    return false; //no handling yet
9863f2400bSandi  }
9963f2400bSandi
1000e1a261eSMichael Klier  //only search txt files
1010e1a261eSMichael Klier  if(substr($file,-4) != '.txt') return false;
1020e1a261eSMichael Klier
10363f2400bSandi  //get id
10463f2400bSandi  $id = pathID($file);
10563f2400bSandi
10663f2400bSandi  //check if it matches the query
10763f2400bSandi  if(!preg_match('/^'.preg_quote($opts['query'],'/').'/u',$id)){
10863f2400bSandi    return false;
10963f2400bSandi  }
11063f2400bSandi
11163f2400bSandi  //check ACL
11263f2400bSandi  if(auth_quickaclcheck($id) < AUTH_READ){
11363f2400bSandi    return false;
11463f2400bSandi  }
11563f2400bSandi
11663f2400bSandi  $data[]=array( 'id'    => $id,
11763f2400bSandi                 'type'  => $type,
11863f2400bSandi                 'level' => 1,
11963f2400bSandi                 'open'  => true);
12063f2400bSandi  return true;
12163f2400bSandi}
12263f2400bSandi
12363f2400bSandi/**
12415fae107Sandi * Build the browsable index of pages
125f3f0262cSandi *
126f3f0262cSandi * $opts['ns'] is the current namespace
12715fae107Sandi *
12815fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
129f3f0262cSandi */
130f3f0262cSandifunction search_index(&$data,$base,$file,$type,$lvl,$opts){
131d1c7b6ecSAndreas Gohr  global $conf;
132f3f0262cSandi  $return = true;
133f3f0262cSandi
134cb70c441Sandi  $item = array();
135cb70c441Sandi
136f3f0262cSandi  if($type == 'd' && !preg_match('#^'.$file.'(/|$)#','/'.$opts['ns'])){
137f3f0262cSandi    //add but don't recurse
138f3f0262cSandi    $return = false;
1390e1a261eSMichael Klier  }elseif($type == 'f' && ($opts['nofiles'] || substr($file,-4) != '.txt')){
140f3f0262cSandi    //don't add
141f3f0262cSandi    return false;
142f3f0262cSandi  }
143f3f0262cSandi
144f3f0262cSandi  $id = pathID($file);
1450dc92c6fSAndreas Gohr
146d1c7b6ecSAndreas Gohr  if($type=='d' && $conf['sneaky_index'] && auth_quickaclcheck($id.':') < AUTH_READ){
147b54f94e0SAndreas Gohr    return false;
148b54f94e0SAndreas Gohr  }
149b54f94e0SAndreas Gohr
1500dc92c6fSAndreas Gohr  //check hidden
1511211a7a9SMartin Tschofen  if(isHiddenPage($id)){
1520dc92c6fSAndreas Gohr    return false;
1530dc92c6fSAndreas Gohr  }
1540dc92c6fSAndreas Gohr
1550dc92c6fSAndreas Gohr  //check ACL
156f3f0262cSandi  if($type=='f' && auth_quickaclcheck($id) < AUTH_READ){
157f3f0262cSandi    return false;
158f3f0262cSandi  }
159f3f0262cSandi
160f3f0262cSandi  $data[]=array( 'id'    => $id,
161f3f0262cSandi                 'type'  => $type,
162cb70c441Sandi                 'level' => $lvl,
163cb70c441Sandi                 'open'  => $return );
164f3f0262cSandi  return $return;
165f3f0262cSandi}
166f3f0262cSandi
167f3f0262cSandi/**
16815fae107Sandi * List all namespaces
16915fae107Sandi *
17015fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
171f3f0262cSandi */
172f3f0262cSandifunction search_namespaces(&$data,$base,$file,$type,$lvl,$opts){
173f3f0262cSandi  if($type == 'f') return true; //nothing to do on files
174f3f0262cSandi
175f3f0262cSandi  $id = pathID($file);
176f3f0262cSandi  $data[]=array( 'id'    => $id,
177f3f0262cSandi                 'type'  => $type,
178f3f0262cSandi                 'level' => $lvl );
179f3f0262cSandi  return true;
180f3f0262cSandi}
181f3f0262cSandi
182f3f0262cSandi/**
18315fae107Sandi * List all mediafiles in a namespace
18415fae107Sandi *
18515fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
186f3f0262cSandi */
187f3f0262cSandifunction search_media(&$data,$base,$file,$type,$lvl,$opts){
188*b8219d2dSAndreas Gohr
189f3f0262cSandi    //we do nothing with directories
1901a49ac65SGina Haeussge    if($type == 'd') {
191224122cfSAndreas Gohr        if(!$opts['depth']) return true; // recurse forever
192*b8219d2dSAndreas Gohr        $depth = substr_count(ltrim($file,'/'),'/');
193*b8219d2dSAndreas Gohr        if($depth >= $opts['depth']) return false; // depth reached
194224122cfSAndreas Gohr        return true;
1951a49ac65SGina Haeussge    }
196f3f0262cSandi
197f3f0262cSandi    $info         = array();
198156a608cSandi    $info['id']   = pathID($file,true);
19964807c84SAndreas Gohr    if($info['id'] != cleanID($info['id'])){
20064807c84SAndreas Gohr        if($opts['showmsg'])
20164807c84SAndreas Gohr            msg(hsc($info['id']).' is not a valid file name for DokuWiki - skipped',-1);
20264807c84SAndreas Gohr        return false; // skip non-valid files
20364807c84SAndreas Gohr    }
204f3f0262cSandi
205f3f0262cSandi    //check ACL for namespace (we have no ACL for mediafiles)
206224122cfSAndreas Gohr    $info['perm'] = auth_quickaclcheck(getNS($info['id']).':*');
207224122cfSAndreas Gohr    if(!$opts['skipacl'] && $info['perm'] < AUTH_READ){
208224122cfSAndreas Gohr        return false;
209224122cfSAndreas Gohr    }
210224122cfSAndreas Gohr
211224122cfSAndreas Gohr    //check pattern filter
212224122cfSAndreas Gohr    if($opts['pattern'] && !@preg_match($opts['pattern'], $info['id'])){
213f3f0262cSandi        return false;
214f3f0262cSandi    }
215f3f0262cSandi
216f3f0262cSandi    $info['file']     = basename($file);
217f3f0262cSandi    $info['size']     = filesize($base.'/'.$file);
2185e7fa82eSAndreas Gohr    $info['mtime']    = filemtime($base.'/'.$file);
2193df72098SAndreas Gohr    $info['writable'] = is_writable($base.'/'.$file);
220f3f0262cSandi    if(preg_match("/\.(jpe?g|gif|png)$/",$file)){
221f3f0262cSandi        $info['isimg'] = true;
22223a34783SAndreas Gohr        require_once(DOKU_INC.'inc/JpegMeta.php');
22323a34783SAndreas Gohr        $info['meta']  = new JpegMeta($base.'/'.$file);
224f3f0262cSandi    }else{
225f3f0262cSandi        $info['isimg'] = false;
226f3f0262cSandi    }
227224122cfSAndreas Gohr    if($opts['hash']){
228224122cfSAndreas Gohr        $info['hash'] = md5(io_readFile(wikiFN($info['id']),false));
229224122cfSAndreas Gohr    }
230224122cfSAndreas Gohr
231f3f0262cSandi    $data[] = $info;
232f3f0262cSandi
233f3f0262cSandi    return false;
234f3f0262cSandi}
235f3f0262cSandi
236f3f0262cSandi/**
237f3f0262cSandi * This function just lists documents (for RSS namespace export)
23815fae107Sandi *
23915fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
240f3f0262cSandi */
241f3f0262cSandifunction search_list(&$data,$base,$file,$type,$lvl,$opts){
242f3f0262cSandi  //we do nothing with directories
243f3f0262cSandi  if($type == 'd') return false;
2440e1a261eSMichael Klier  //only search txt files
2450e1a261eSMichael Klier  if(substr($file,-4) == '.txt'){
246f3f0262cSandi    //check ACL
247f3f0262cSandi    $id = pathID($file);
248f3f0262cSandi    if(auth_quickaclcheck($id) < AUTH_READ){
249f3f0262cSandi      return false;
250f3f0262cSandi    }
2510e1a261eSMichael Klier    $data[]['id'] = $id;
252f3f0262cSandi  }
253f3f0262cSandi  return false;
254f3f0262cSandi}
255f3f0262cSandi
256f3f0262cSandi/**
257f3f0262cSandi * Quicksearch for searching matching pagenames
258f3f0262cSandi *
259f3f0262cSandi * $opts['query'] is the search query
26015fae107Sandi *
26115fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
262f3f0262cSandi */
263f3f0262cSandifunction search_pagename(&$data,$base,$file,$type,$lvl,$opts){
264f3f0262cSandi  //we do nothing with directories
265f3f0262cSandi  if($type == 'd') return true;
266f3f0262cSandi  //only search txt files
2670e1a261eSMichael Klier  if(substr($file,-4) != '.txt') return true;
268f3f0262cSandi
269f3f0262cSandi  //simple stringmatching
270396b7edbSmatthiasgrimm  if (!empty($opts['query'])){
271f3f0262cSandi    if(strpos($file,$opts['query']) !== false){
272f3f0262cSandi      //check ACL
273f3f0262cSandi      $id = pathID($file);
274f3f0262cSandi      if(auth_quickaclcheck($id) < AUTH_READ){
275f3f0262cSandi        return false;
276f3f0262cSandi      }
277f3f0262cSandi      $data[]['id'] = $id;
278f3f0262cSandi    }
279396b7edbSmatthiasgrimm  }
280f3f0262cSandi  return true;
281f3f0262cSandi}
282f3f0262cSandi
283f3f0262cSandi/**
28458b6f612SAndreas Gohr * Just lists all documents
28558b6f612SAndreas Gohr *
2861fcfad4dSAndreas Gohr * $opts['depth']   recursion level, 0 for all
2871fcfad4dSAndreas Gohr * $opts['hash']    do md5 sum of content?
288224122cfSAndreas Gohr * $opts['skipacl'] list everything regardless of ACL
2891fcfad4dSAndreas Gohr *
29058b6f612SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
29158b6f612SAndreas Gohr */
29258b6f612SAndreas Gohrfunction search_allpages(&$data,$base,$file,$type,$lvl,$opts){
29358b6f612SAndreas Gohr    //we do nothing with directories
2941fcfad4dSAndreas Gohr    if($type == 'd'){
2951fcfad4dSAndreas Gohr        if(!$opts['depth']) return true; // recurse forever
29617ff9c04SAndreas Gohr        $parts = explode('/',ltrim($file,'/'));
2971fcfad4dSAndreas Gohr        if(count($parts) == $opts['depth']) return false; // depth reached
2981fcfad4dSAndreas Gohr        return true;
2991fcfad4dSAndreas Gohr    }
3001fcfad4dSAndreas Gohr
30158b6f612SAndreas Gohr    //only search txt files
3020e1a261eSMichael Klier    if(substr($file,-4) != '.txt') return true;
30358b6f612SAndreas Gohr
3041fcfad4dSAndreas Gohr    $item['id']   = pathID($file);
305061df79cSAndreas Gohr    if(!$opts['skipacl'] && auth_quickaclcheck($item['id']) < AUTH_READ){
3061fcfad4dSAndreas Gohr      return false;
3071fcfad4dSAndreas Gohr    }
3081fcfad4dSAndreas Gohr
3091fcfad4dSAndreas Gohr    $item['rev']   = filemtime($base.'/'.$file);
310224122cfSAndreas Gohr    $item['mtime'] = $item['rev'];
3111fcfad4dSAndreas Gohr    $item['size']  = filesize($base.'/'.$file);
3121fcfad4dSAndreas Gohr    if($opts['hash']){
3131fcfad4dSAndreas Gohr        $item['hash'] = md5(trim(rawWiki($item['id'])));
3141fcfad4dSAndreas Gohr    }
3151fcfad4dSAndreas Gohr
3161fcfad4dSAndreas Gohr    $data[] = $item;
31758b6f612SAndreas Gohr    return true;
31858b6f612SAndreas Gohr}
31958b6f612SAndreas Gohr
32058b6f612SAndreas Gohr/**
321f3f0262cSandi * Search for backlinks to a given page
322f3f0262cSandi *
323f3f0262cSandi * $opts['ns']    namespace of the page
324f3f0262cSandi * $opts['name']  name of the page without namespace
32515fae107Sandi *
32615fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
32754f4c056SAndreas Gohr * @deprecated Replaced by ft_backlinks()
328f3f0262cSandi */
329f3f0262cSandifunction search_backlinks(&$data,$base,$file,$type,$lvl,$opts){
330f3f0262cSandi  //we do nothing with directories
3310e1a261eSMichael Klier  if($type == 'd') return true;
332f3f0262cSandi  //only search txt files
3330e1a261eSMichael Klier  if(substr($file,-4) != '.txt') return true;
334f3f0262cSandi
335f3f0262cSandi  //absolute search id
336f3f0262cSandi  $sid = cleanID($opts['ns'].':'.$opts['name']);
337f3f0262cSandi
33837e34a5eSandi  //current id and namespace
339f3f0262cSandi  $cid = pathID($file);
340f3f0262cSandi  $cns = getNS($cid);
341f3f0262cSandi
342f3f0262cSandi  //check ACL
343f3f0262cSandi  if(auth_quickaclcheck($cid) < AUTH_READ){
344f3f0262cSandi    return false;
345f3f0262cSandi  }
346f3f0262cSandi
34737e34a5eSandi  //fetch instructions
34837e34a5eSandi  require_once(DOKU_INC.'inc/parserutils.php');
34937e34a5eSandi  $instructions = p_cached_instructions($base.$file,true);
35037e34a5eSandi  if(is_null($instructions)) return false;
351f3f0262cSandi
35237e34a5eSandi  //check all links for match
35337e34a5eSandi  foreach($instructions as $ins){
35437e34a5eSandi    if($ins[0] == 'internallink' || ($conf['camelcase'] && $ins[0] == 'camelcaselink') ){
35537e34a5eSandi      $mid = $ins[1][0];
35637e34a5eSandi      resolve_pageid($cns,$mid,$exists); //exists is not used
357f3f0262cSandi      if($mid == $sid){
35837e34a5eSandi        //we have a match - finish
359f3f0262cSandi        $data[]['id'] = $cid;
360f3f0262cSandi        break;
361f3f0262cSandi      }
362f3f0262cSandi    }
363f3f0262cSandi  }
364f3f0262cSandi
36537e34a5eSandi  return false;
36637e34a5eSandi}
36737e34a5eSandi
368f3f0262cSandi/**
369f3f0262cSandi * Fulltextsearch
370f3f0262cSandi *
371f3f0262cSandi * $opts['query'] is the search query
37215fae107Sandi *
37315fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
374506fa893SAndreas Gohr * @deprecated - fulltext indexer is used instead
375f3f0262cSandi */
376f3f0262cSandifunction search_fulltext(&$data,$base,$file,$type,$lvl,$opts){
377f3f0262cSandi  //we do nothing with directories
3780e1a261eSMichael Klier  if($type == 'd') return true;
379f3f0262cSandi  //only search txt files
3800e1a261eSMichael Klier  if(substr($file,-4) != '.txt') return true;
381f3f0262cSandi
382f3f0262cSandi  //check ACL
383f3f0262cSandi  $id = pathID($file);
384f3f0262cSandi  if(auth_quickaclcheck($id) < AUTH_READ){
385f3f0262cSandi    return false;
386f3f0262cSandi  }
387f3f0262cSandi
388f3f0262cSandi  //create regexp from queries
3895ef370d2Smatthiasgrimm  $poswords = array();
3905ef370d2Smatthiasgrimm  $negwords = array();
3915ef370d2Smatthiasgrimm  $qpreg = preg_split('/\s+/',$opts['query']);
3925ef370d2Smatthiasgrimm
3935ef370d2Smatthiasgrimm  foreach($qpreg as $word){
3945ef370d2Smatthiasgrimm    switch(substr($word,0,1)){
3955ef370d2Smatthiasgrimm      case '-':
396396b7edbSmatthiasgrimm        if(strlen($word) > 1){  // catch single '-'
3975ef370d2Smatthiasgrimm          array_push($negwords,preg_quote(substr($word,1),'#'));
398396b7edbSmatthiasgrimm        }
3995ef370d2Smatthiasgrimm        break;
4005ef370d2Smatthiasgrimm      case '+':
401396b7edbSmatthiasgrimm        if(strlen($word) > 1){  // catch single '+'
4025ef370d2Smatthiasgrimm          array_push($poswords,preg_quote(substr($word,1),'#'));
403396b7edbSmatthiasgrimm        }
4045ef370d2Smatthiasgrimm        break;
4055ef370d2Smatthiasgrimm      default:
4065ef370d2Smatthiasgrimm        array_push($poswords,preg_quote($word,'#'));
4075ef370d2Smatthiasgrimm        break;
4085ef370d2Smatthiasgrimm    }
4095ef370d2Smatthiasgrimm  }
410248a7321Smatthiasgrimm
411248a7321Smatthiasgrimm  // a search without any posword is useless
412248a7321Smatthiasgrimm  if (!count($poswords)) return true;
4135ef370d2Smatthiasgrimm
4145a5d942dSmatthiasgrimm  $reg  = '^(?=.*?'.join(')(?=.*?',$poswords).')';
4155ef370d2Smatthiasgrimm  $reg .= count($negwords) ? '((?!'.join('|',$negwords).').)*$' : '.*$';
416b59a406bSmatthiasgrimm  search_regex($data,$base,$file,$reg,$poswords);
417b59a406bSmatthiasgrimm  return true;
418b59a406bSmatthiasgrimm}
419b59a406bSmatthiasgrimm
420b59a406bSmatthiasgrimm/**
421b59a406bSmatthiasgrimm * Reference search
422b59a406bSmatthiasgrimm * This fuction searches for existing references to a given media file
423b59a406bSmatthiasgrimm * and returns an array with the found pages. It doesn't pay any
424b59a406bSmatthiasgrimm * attention to ACL permissions to find every reference. The caller
425b59a406bSmatthiasgrimm * must check if the user has the appropriate rights to see the found
426b59a406bSmatthiasgrimm * page and eventually have to prevent the result from displaying.
427b59a406bSmatthiasgrimm *
428b59a406bSmatthiasgrimm * @param array  $data Reference to the result data structure
429b59a406bSmatthiasgrimm * @param string $base Base usually $conf['datadir']
430b59a406bSmatthiasgrimm * @param string $file current file or directory relative to $base
431b59a406bSmatthiasgrimm * @param char   $type Type either 'd' for directory or 'f' for file
432b59a406bSmatthiasgrimm * @param int    $lvl  Current recursion depht
433b59a406bSmatthiasgrimm * @param mixed  $opts option array as given to search()
434b59a406bSmatthiasgrimm *
435b59a406bSmatthiasgrimm * $opts['query'] is the demanded media file name
436b59a406bSmatthiasgrimm *
437b59a406bSmatthiasgrimm * @author  Andreas Gohr <andi@splitbrain.org>
438b59a406bSmatthiasgrimm * @author  Matthias Grimm <matthiasgrimm@users.sourceforge.net>
439b59a406bSmatthiasgrimm */
440b59a406bSmatthiasgrimmfunction search_reference(&$data,$base,$file,$type,$lvl,$opts){
441b59a406bSmatthiasgrimm  global $conf;
442b59a406bSmatthiasgrimm
443b59a406bSmatthiasgrimm  //we do nothing with directories
444b59a406bSmatthiasgrimm  if($type == 'd') return true;
445b59a406bSmatthiasgrimm
446b59a406bSmatthiasgrimm  //only search txt files
4470e1a261eSMichael Klier  if(substr($file,-4) != '.txt') return true;
448b59a406bSmatthiasgrimm
449e28299ccSmatthiasgrimm  //we finish after 'cnt' references found. The return value
450b59a406bSmatthiasgrimm  //'false' will skip subdirectories to speed search up.
451e28299ccSmatthiasgrimm  $cnt = $conf['refshow'] > 0 ? $conf['refshow'] : 1;
452e28299ccSmatthiasgrimm  if(count($data) >= $cnt) return false;
453b59a406bSmatthiasgrimm
454d67ca2c0Smatthiasgrimm  $reg = '\{\{ *\:?'.$opts['query'].' *(\|.*)?\}\}';
455b59a406bSmatthiasgrimm  search_regex($data,$base,$file,$reg,array($opts['query']));
456b59a406bSmatthiasgrimm  return true;
457b59a406bSmatthiasgrimm}
458b59a406bSmatthiasgrimm
459b59a406bSmatthiasgrimm/* ------------- helper functions below -------------- */
460b59a406bSmatthiasgrimm
461b59a406bSmatthiasgrimm/**
462b59a406bSmatthiasgrimm * fulltext search helper
463b59a406bSmatthiasgrimm * searches a text file with a given regular expression
464b59a406bSmatthiasgrimm * no ACL checks are performed. This have to be done by
465b59a406bSmatthiasgrimm * the caller if necessary.
466b59a406bSmatthiasgrimm *
467b59a406bSmatthiasgrimm * @param array  $data  reference to array for results
468b59a406bSmatthiasgrimm * @param string $base  base directory
469b59a406bSmatthiasgrimm * @param string $file  file name to search in
470b59a406bSmatthiasgrimm * @param string $reg   regular expression to search for
471b59a406bSmatthiasgrimm * @param array  $words words that should be marked in the results
472b59a406bSmatthiasgrimm *
473b59a406bSmatthiasgrimm * @author  Andreas Gohr <andi@splitbrain.org>
474b59a406bSmatthiasgrimm * @author  Matthias Grimm <matthiasgrimm@users.sourceforge.net>
475506fa893SAndreas Gohr *
476506fa893SAndreas Gohr * @deprecated - fulltext indexer is used instead
477b59a406bSmatthiasgrimm */
478b59a406bSmatthiasgrimmfunction search_regex(&$data,$base,$file,$reg,$words){
479b59a406bSmatthiasgrimm
480b59a406bSmatthiasgrimm  //get text
481b59a406bSmatthiasgrimm  $text = io_readfile($base.'/'.$file);
482b59a406bSmatthiasgrimm  //lowercase text (u modifier does not help with case)
483b59a406bSmatthiasgrimm  $lctext = utf8_strtolower($text);
484f3f0262cSandi
485f3f0262cSandi  //do the fulltext search
486f3f0262cSandi  $matches = array();
4875ef370d2Smatthiasgrimm  if($cnt = preg_match_all('#'.$reg.'#usi',$lctext,$matches)){
488f3f0262cSandi    //this is not the best way for snippet generation but the fastest I could find
489b59a406bSmatthiasgrimm    $q = $words[0];  //use first word for snippet creation
490d5a2a500Sandi    $p = utf8_strpos($lctext,$q);
491f3f0262cSandi    $f = $p - 100;
492d5a2a500Sandi    $l = utf8_strlen($q) + 200;
493f3f0262cSandi    if($f < 0) $f = 0;
494f3f0262cSandi    $snippet = '<span class="search_sep"> ... </span>'.
495d5a2a500Sandi               htmlspecialchars(utf8_substr($text,$f,$l)).
496f3f0262cSandi               '<span class="search_sep"> ... </span>';
497b59a406bSmatthiasgrimm    $mark    = '('.join('|', $words).')';
498ed7ecb79SAnika Henke    $snippet = preg_replace('#'.$mark.'#si','<strong class="search_hit">\\1</strong>',$snippet);
499f3f0262cSandi
500f3f0262cSandi    $data[] = array(
501b59a406bSmatthiasgrimm      'id'       => pathID($file),
5025ef370d2Smatthiasgrimm      'count'    => preg_match_all('#'.$mark.'#usi',$lctext,$matches),
503b59a406bSmatthiasgrimm      'poswords' => join(' ',$words),
504f3f0262cSandi      'snippet'  => $snippet,
505f3f0262cSandi    );
506f3f0262cSandi  }
507f3f0262cSandi
508f3f0262cSandi  return true;
509f3f0262cSandi}
510f3f0262cSandi
511b59a406bSmatthiasgrimm
512f3f0262cSandi/**
51315fae107Sandi * fulltext sort
51415fae107Sandi *
515f3f0262cSandi * Callback sort function for use with usort to sort the data
516f3f0262cSandi * structure created by search_fulltext. Sorts descending by count
51715fae107Sandi *
51815fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
519f3f0262cSandi */
520f3f0262cSandifunction sort_search_fulltext($a,$b){
521f3f0262cSandi  if($a['count'] > $b['count']){
522f3f0262cSandi    return -1;
523f3f0262cSandi  }elseif($a['count'] < $b['count']){
524f3f0262cSandi    return 1;
525f3f0262cSandi  }else{
526f3f0262cSandi    return strcmp($a['id'],$b['id']);
527f3f0262cSandi  }
528f3f0262cSandi}
529f3f0262cSandi
530f3f0262cSandi/**
531f3f0262cSandi * translates a document path to an ID
53215fae107Sandi *
53315fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
53437e34a5eSandi * @todo    move to pageutils
535f3f0262cSandi */
536156a608cSandifunction pathID($path,$keeptxt=false){
53749c713a3Sandi  $id = utf8_decodeFN($path);
53849c713a3Sandi  $id = str_replace('/',':',$id);
539156a608cSandi  if(!$keeptxt) $id = preg_replace('#\.txt$#','',$id);
540f3f0262cSandi  $id = preg_replace('#^:+#','',$id);
541f3f0262cSandi  $id = preg_replace('#:+$#','',$id);
542f3f0262cSandi  return $id;
543f3f0262cSandi}
544f3f0262cSandi
545340756e4Sandi
546340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
547