xref: /dokuwiki/inc/search.php (revision 64807c842ccad35656e9abb857246a511d56d29a)
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
900976812SAndreas Gohr  if(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/');
10ed7b5f09Sandi  require_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){
4624baa045SAndreas Gohr    if (search_callback($func,$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){
5224baa045SAndreas Gohr    search_callback($func,$data,$base,$file,'f',$lvl,$opts);
53f3f0262cSandi  }
54f3f0262cSandi}
55f3f0262cSandi
56f3f0262cSandi/**
570e1a261eSMichael Klier * Used to run a user callback
5824baa045SAndreas Gohr *
5924baa045SAndreas Gohr * Makes sure the $data array is passed by reference (unlike when using
6024baa045SAndreas Gohr * call_user_func())
6124baa045SAndreas Gohr *
6224baa045SAndreas Gohr * @todo If this can be generalized it may be useful elsewhere in the code
6324baa045SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
6424baa045SAndreas Gohr */
6524baa045SAndreas Gohrfunction search_callback($func,&$data,$base,$file,$type,$lvl,$opts){
6624baa045SAndreas Gohr  if(is_array($func)){
6724baa045SAndreas Gohr    if(is_object($func[0])){
6824baa045SAndreas Gohr      // instanciated object
6924baa045SAndreas Gohr      return $func[0]->$func[1]($data,$base,$file,$type,$lvl,$opts);
7024baa045SAndreas Gohr    }else{
7124baa045SAndreas Gohr      // static call
7224baa045SAndreas Gohr      $f = $func[0].'::'.$func[1];
7324baa045SAndreas Gohr      return $f($data,$base,$file,$type,$lvl,$opts);
7424baa045SAndreas Gohr    }
7524baa045SAndreas Gohr  }
7624baa045SAndreas Gohr  // simple function call
7724baa045SAndreas Gohr  return $func($data,$base,$file,$type,$lvl,$opts);
7824baa045SAndreas Gohr}
7924baa045SAndreas Gohr
8024baa045SAndreas Gohr/**
81f3f0262cSandi * The following functions are userfunctions to use with the search
82f3f0262cSandi * function above. This function is called for every found file or
83f3f0262cSandi * directory. When a directory is given to the function it has to
84f3f0262cSandi * decide if this directory should be traversed (true) or not (false)
85f3f0262cSandi * The function has to accept the following parameters:
86f3f0262cSandi *
87f3f0262cSandi * &$data - Reference to the result data structure
88f3f0262cSandi * $base  - Base usually $conf['datadir']
89f3f0262cSandi * $file  - current file or directory relative to $base
90f3f0262cSandi * $type  - Type either 'd' for directory or 'f' for file
91f3f0262cSandi * $lvl   - Current recursion depht
92f3f0262cSandi * $opts  - option array as given to search()
93f3f0262cSandi *
94f3f0262cSandi * return values for files are ignored
95f3f0262cSandi *
96f3f0262cSandi * All functions should check the ACL for document READ rights
97f3f0262cSandi * namespaces (directories) are NOT checked as this would break
98f3f0262cSandi * the recursion (You can have an nonreadable dir over a readable
990e1a261eSMichael Klier * one deeper nested) also make sure to check the file type (for example
1000e1a261eSMichael Klier * in case of lockfiles).
101f3f0262cSandi */
102f3f0262cSandi
103f3f0262cSandi/**
10463f2400bSandi * Searches for pages beginning with the given query
10563f2400bSandi *
10663f2400bSandi * @author Andreas Gohr <andi@splitbrain.org>
10763f2400bSandi */
10863f2400bSandifunction search_qsearch(&$data,$base,$file,$type,$lvl,$opts){
10963f2400bSandi  $item = array();
11063f2400bSandi
11163f2400bSandi  if($type == 'd'){
11263f2400bSandi    return false; //no handling yet
11363f2400bSandi  }
11463f2400bSandi
1150e1a261eSMichael Klier  //only search txt files
1160e1a261eSMichael Klier  if(substr($file,-4) != '.txt') return false;
1170e1a261eSMichael Klier
11863f2400bSandi  //get id
11963f2400bSandi  $id = pathID($file);
12063f2400bSandi
12163f2400bSandi  //check if it matches the query
12263f2400bSandi  if(!preg_match('/^'.preg_quote($opts['query'],'/').'/u',$id)){
12363f2400bSandi    return false;
12463f2400bSandi  }
12563f2400bSandi
12663f2400bSandi  //check ACL
12763f2400bSandi  if(auth_quickaclcheck($id) < AUTH_READ){
12863f2400bSandi    return false;
12963f2400bSandi  }
13063f2400bSandi
13163f2400bSandi  $data[]=array( 'id'    => $id,
13263f2400bSandi                 'type'  => $type,
13363f2400bSandi                 'level' => 1,
13463f2400bSandi                 'open'  => true);
13563f2400bSandi  return true;
13663f2400bSandi}
13763f2400bSandi
13863f2400bSandi/**
13915fae107Sandi * Build the browsable index of pages
140f3f0262cSandi *
141f3f0262cSandi * $opts['ns'] is the current namespace
14215fae107Sandi *
14315fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
144f3f0262cSandi */
145f3f0262cSandifunction search_index(&$data,$base,$file,$type,$lvl,$opts){
146d1c7b6ecSAndreas Gohr  global $conf;
147f3f0262cSandi  $return = true;
148f3f0262cSandi
149cb70c441Sandi  $item = array();
150cb70c441Sandi
151f3f0262cSandi  if($type == 'd' && !preg_match('#^'.$file.'(/|$)#','/'.$opts['ns'])){
152f3f0262cSandi    //add but don't recurse
153f3f0262cSandi    $return = false;
1540e1a261eSMichael Klier  }elseif($type == 'f' && ($opts['nofiles'] || substr($file,-4) != '.txt')){
155f3f0262cSandi    //don't add
156f3f0262cSandi    return false;
157f3f0262cSandi  }
158f3f0262cSandi
159f3f0262cSandi  $id = pathID($file);
1600dc92c6fSAndreas Gohr
161d1c7b6ecSAndreas Gohr  if($type=='d' && $conf['sneaky_index'] && auth_quickaclcheck($id.':') < AUTH_READ){
162b54f94e0SAndreas Gohr    return false;
163b54f94e0SAndreas Gohr  }
164b54f94e0SAndreas Gohr
1650dc92c6fSAndreas Gohr  //check hidden
1661211a7a9SMartin Tschofen  if(isHiddenPage($id)){
1670dc92c6fSAndreas Gohr    return false;
1680dc92c6fSAndreas Gohr  }
1690dc92c6fSAndreas Gohr
1700dc92c6fSAndreas Gohr  //check ACL
171f3f0262cSandi  if($type=='f' && auth_quickaclcheck($id) < AUTH_READ){
172f3f0262cSandi    return false;
173f3f0262cSandi  }
174f3f0262cSandi
175f3f0262cSandi  $data[]=array( 'id'    => $id,
176f3f0262cSandi                 'type'  => $type,
177cb70c441Sandi                 'level' => $lvl,
178cb70c441Sandi                 'open'  => $return );
179f3f0262cSandi  return $return;
180f3f0262cSandi}
181f3f0262cSandi
182f3f0262cSandi/**
18315fae107Sandi * List all namespaces
18415fae107Sandi *
18515fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
186f3f0262cSandi */
187f3f0262cSandifunction search_namespaces(&$data,$base,$file,$type,$lvl,$opts){
188f3f0262cSandi  if($type == 'f') return true; //nothing to do on files
189f3f0262cSandi
190f3f0262cSandi  $id = pathID($file);
191f3f0262cSandi  $data[]=array( 'id'    => $id,
192f3f0262cSandi                 'type'  => $type,
193f3f0262cSandi                 'level' => $lvl );
194f3f0262cSandi  return true;
195f3f0262cSandi}
196f3f0262cSandi
197f3f0262cSandi/**
19815fae107Sandi * List all mediafiles in a namespace
19915fae107Sandi *
20015fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
201f3f0262cSandi */
202f3f0262cSandifunction search_media(&$data,$base,$file,$type,$lvl,$opts){
203f3f0262cSandi  //we do nothing with directories
204f3f0262cSandi  if($type == 'd') return false;
205f3f0262cSandi
206f3f0262cSandi  $info         = array();
207156a608cSandi  $info['id']   = pathID($file,true);
208*64807c84SAndreas Gohr  if($info['id'] != cleanID($info['id'])){
209*64807c84SAndreas Gohr    if($opts['showmsg'])
210*64807c84SAndreas Gohr      msg(hsc($info['id']).' is not a valid file name for DokuWiki - skipped',-1);
211*64807c84SAndreas Gohr    return false; // skip non-valid files
212*64807c84SAndreas Gohr  }
213f3f0262cSandi
214f3f0262cSandi  //check ACL for namespace (we have no ACL for mediafiles)
215f3f0262cSandi  if(auth_quickaclcheck(getNS($info['id']).':*') < AUTH_READ){
216f3f0262cSandi    return false;
217f3f0262cSandi  }
218f3f0262cSandi
219f3f0262cSandi  $info['file'] = basename($file);
220f3f0262cSandi  $info['size'] = filesize($base.'/'.$file);
2215e7fa82eSAndreas Gohr  $info['mtime'] = filemtime($base.'/'.$file);
2223df72098SAndreas Gohr  $info['writable'] = is_writable($base.'/'.$file);
223f3f0262cSandi  if(preg_match("/\.(jpe?g|gif|png)$/",$file)){
224f3f0262cSandi    $info['isimg'] = true;
22523a34783SAndreas Gohr    require_once(DOKU_INC.'inc/JpegMeta.php');
22623a34783SAndreas Gohr    $info['meta']  = new JpegMeta($base.'/'.$file);
227f3f0262cSandi  }else{
228f3f0262cSandi    $info['isimg'] = false;
229f3f0262cSandi  }
230f3f0262cSandi  $data[] = $info;
231f3f0262cSandi
232f3f0262cSandi  return false;
233f3f0262cSandi}
234f3f0262cSandi
235f3f0262cSandi/**
236f3f0262cSandi * This function just lists documents (for RSS namespace export)
23715fae107Sandi *
23815fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
239f3f0262cSandi */
240f3f0262cSandifunction search_list(&$data,$base,$file,$type,$lvl,$opts){
241f3f0262cSandi  //we do nothing with directories
242f3f0262cSandi  if($type == 'd') return false;
2430e1a261eSMichael Klier  //only search txt files
2440e1a261eSMichael Klier  if(substr($file,-4) == '.txt'){
245f3f0262cSandi    //check ACL
246f3f0262cSandi    $id = pathID($file);
247f3f0262cSandi    if(auth_quickaclcheck($id) < AUTH_READ){
248f3f0262cSandi      return false;
249f3f0262cSandi    }
2500e1a261eSMichael Klier    $data[]['id'] = $id;
251f3f0262cSandi  }
252f3f0262cSandi  return false;
253f3f0262cSandi}
254f3f0262cSandi
255f3f0262cSandi/**
256f3f0262cSandi * Quicksearch for searching matching pagenames
257f3f0262cSandi *
258f3f0262cSandi * $opts['query'] is the search query
25915fae107Sandi *
26015fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
261f3f0262cSandi */
262f3f0262cSandifunction search_pagename(&$data,$base,$file,$type,$lvl,$opts){
263f3f0262cSandi  //we do nothing with directories
264f3f0262cSandi  if($type == 'd') return true;
265f3f0262cSandi  //only search txt files
2660e1a261eSMichael Klier  if(substr($file,-4) != '.txt') return true;
267f3f0262cSandi
268f3f0262cSandi  //simple stringmatching
269396b7edbSmatthiasgrimm  if (!empty($opts['query'])){
270f3f0262cSandi    if(strpos($file,$opts['query']) !== false){
271f3f0262cSandi      //check ACL
272f3f0262cSandi      $id = pathID($file);
273f3f0262cSandi      if(auth_quickaclcheck($id) < AUTH_READ){
274f3f0262cSandi        return false;
275f3f0262cSandi      }
276f3f0262cSandi      $data[]['id'] = $id;
277f3f0262cSandi    }
278396b7edbSmatthiasgrimm  }
279f3f0262cSandi  return true;
280f3f0262cSandi}
281f3f0262cSandi
282f3f0262cSandi/**
28358b6f612SAndreas Gohr * Just lists all documents
28458b6f612SAndreas Gohr *
28558b6f612SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
28658b6f612SAndreas Gohr */
28758b6f612SAndreas Gohrfunction search_allpages(&$data,$base,$file,$type,$lvl,$opts){
28858b6f612SAndreas Gohr  //we do nothing with directories
28958b6f612SAndreas Gohr  if($type == 'd') return true;
29058b6f612SAndreas Gohr  //only search txt files
2910e1a261eSMichael Klier  if(substr($file,-4) != '.txt') return true;
29258b6f612SAndreas Gohr
29358b6f612SAndreas Gohr  $data[]['id'] = pathID($file);
29458b6f612SAndreas Gohr  return true;
29558b6f612SAndreas Gohr}
29658b6f612SAndreas Gohr
29758b6f612SAndreas Gohr/**
298f3f0262cSandi * Search for backlinks to a given page
299f3f0262cSandi *
300f3f0262cSandi * $opts['ns']    namespace of the page
301f3f0262cSandi * $opts['name']  name of the page without namespace
30215fae107Sandi *
30315fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
30454f4c056SAndreas Gohr * @deprecated Replaced by ft_backlinks()
305f3f0262cSandi */
306f3f0262cSandifunction search_backlinks(&$data,$base,$file,$type,$lvl,$opts){
307f3f0262cSandi  //we do nothing with directories
3080e1a261eSMichael Klier  if($type == 'd') return true;
309f3f0262cSandi  //only search txt files
3100e1a261eSMichael Klier  if(substr($file,-4) != '.txt') return true;
311f3f0262cSandi
312f3f0262cSandi  //absolute search id
313f3f0262cSandi  $sid = cleanID($opts['ns'].':'.$opts['name']);
314f3f0262cSandi
31537e34a5eSandi  //current id and namespace
316f3f0262cSandi  $cid = pathID($file);
317f3f0262cSandi  $cns = getNS($cid);
318f3f0262cSandi
319f3f0262cSandi  //check ACL
320f3f0262cSandi  if(auth_quickaclcheck($cid) < AUTH_READ){
321f3f0262cSandi    return false;
322f3f0262cSandi  }
323f3f0262cSandi
32437e34a5eSandi  //fetch instructions
32537e34a5eSandi  require_once(DOKU_INC.'inc/parserutils.php');
32637e34a5eSandi  $instructions = p_cached_instructions($base.$file,true);
32737e34a5eSandi  if(is_null($instructions)) return false;
328f3f0262cSandi
32937e34a5eSandi  //check all links for match
33037e34a5eSandi  foreach($instructions as $ins){
33137e34a5eSandi    if($ins[0] == 'internallink' || ($conf['camelcase'] && $ins[0] == 'camelcaselink') ){
33237e34a5eSandi      $mid = $ins[1][0];
33337e34a5eSandi      resolve_pageid($cns,$mid,$exists); //exists is not used
334f3f0262cSandi      if($mid == $sid){
33537e34a5eSandi        //we have a match - finish
336f3f0262cSandi        $data[]['id'] = $cid;
337f3f0262cSandi        break;
338f3f0262cSandi      }
339f3f0262cSandi    }
340f3f0262cSandi  }
341f3f0262cSandi
34237e34a5eSandi  return false;
34337e34a5eSandi}
34437e34a5eSandi
345f3f0262cSandi/**
346f3f0262cSandi * Fulltextsearch
347f3f0262cSandi *
348f3f0262cSandi * $opts['query'] is the search query
34915fae107Sandi *
35015fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
351506fa893SAndreas Gohr * @deprecated - fulltext indexer is used instead
352f3f0262cSandi */
353f3f0262cSandifunction search_fulltext(&$data,$base,$file,$type,$lvl,$opts){
354f3f0262cSandi  //we do nothing with directories
3550e1a261eSMichael Klier  if($type == 'd') return true;
356f3f0262cSandi  //only search txt files
3570e1a261eSMichael Klier  if(substr($file,-4) != '.txt') return true;
358f3f0262cSandi
359f3f0262cSandi  //check ACL
360f3f0262cSandi  $id = pathID($file);
361f3f0262cSandi  if(auth_quickaclcheck($id) < AUTH_READ){
362f3f0262cSandi    return false;
363f3f0262cSandi  }
364f3f0262cSandi
365f3f0262cSandi  //create regexp from queries
3665ef370d2Smatthiasgrimm  $poswords = array();
3675ef370d2Smatthiasgrimm  $negwords = array();
3685ef370d2Smatthiasgrimm  $qpreg = preg_split('/\s+/',$opts['query']);
3695ef370d2Smatthiasgrimm
3705ef370d2Smatthiasgrimm  foreach($qpreg as $word){
3715ef370d2Smatthiasgrimm    switch(substr($word,0,1)){
3725ef370d2Smatthiasgrimm      case '-':
373396b7edbSmatthiasgrimm        if(strlen($word) > 1){  // catch single '-'
3745ef370d2Smatthiasgrimm          array_push($negwords,preg_quote(substr($word,1),'#'));
375396b7edbSmatthiasgrimm        }
3765ef370d2Smatthiasgrimm        break;
3775ef370d2Smatthiasgrimm      case '+':
378396b7edbSmatthiasgrimm        if(strlen($word) > 1){  // catch single '+'
3795ef370d2Smatthiasgrimm          array_push($poswords,preg_quote(substr($word,1),'#'));
380396b7edbSmatthiasgrimm        }
3815ef370d2Smatthiasgrimm        break;
3825ef370d2Smatthiasgrimm      default:
3835ef370d2Smatthiasgrimm        array_push($poswords,preg_quote($word,'#'));
3845ef370d2Smatthiasgrimm        break;
3855ef370d2Smatthiasgrimm    }
3865ef370d2Smatthiasgrimm  }
387248a7321Smatthiasgrimm
388248a7321Smatthiasgrimm  // a search without any posword is useless
389248a7321Smatthiasgrimm  if (!count($poswords)) return true;
3905ef370d2Smatthiasgrimm
3915a5d942dSmatthiasgrimm  $reg  = '^(?=.*?'.join(')(?=.*?',$poswords).')';
3925ef370d2Smatthiasgrimm  $reg .= count($negwords) ? '((?!'.join('|',$negwords).').)*$' : '.*$';
393b59a406bSmatthiasgrimm  search_regex($data,$base,$file,$reg,$poswords);
394b59a406bSmatthiasgrimm  return true;
395b59a406bSmatthiasgrimm}
396b59a406bSmatthiasgrimm
397b59a406bSmatthiasgrimm/**
398b59a406bSmatthiasgrimm * Reference search
399b59a406bSmatthiasgrimm * This fuction searches for existing references to a given media file
400b59a406bSmatthiasgrimm * and returns an array with the found pages. It doesn't pay any
401b59a406bSmatthiasgrimm * attention to ACL permissions to find every reference. The caller
402b59a406bSmatthiasgrimm * must check if the user has the appropriate rights to see the found
403b59a406bSmatthiasgrimm * page and eventually have to prevent the result from displaying.
404b59a406bSmatthiasgrimm *
405b59a406bSmatthiasgrimm * @param array  $data Reference to the result data structure
406b59a406bSmatthiasgrimm * @param string $base Base usually $conf['datadir']
407b59a406bSmatthiasgrimm * @param string $file current file or directory relative to $base
408b59a406bSmatthiasgrimm * @param char   $type Type either 'd' for directory or 'f' for file
409b59a406bSmatthiasgrimm * @param int    $lvl  Current recursion depht
410b59a406bSmatthiasgrimm * @param mixed  $opts option array as given to search()
411b59a406bSmatthiasgrimm *
412b59a406bSmatthiasgrimm * $opts['query'] is the demanded media file name
413b59a406bSmatthiasgrimm *
414b59a406bSmatthiasgrimm * @author  Andreas Gohr <andi@splitbrain.org>
415b59a406bSmatthiasgrimm * @author  Matthias Grimm <matthiasgrimm@users.sourceforge.net>
416b59a406bSmatthiasgrimm */
417b59a406bSmatthiasgrimmfunction search_reference(&$data,$base,$file,$type,$lvl,$opts){
418b59a406bSmatthiasgrimm  global $conf;
419b59a406bSmatthiasgrimm
420b59a406bSmatthiasgrimm  //we do nothing with directories
421b59a406bSmatthiasgrimm  if($type == 'd') return true;
422b59a406bSmatthiasgrimm
423b59a406bSmatthiasgrimm  //only search txt files
4240e1a261eSMichael Klier  if(substr($file,-4) != '.txt') return true;
425b59a406bSmatthiasgrimm
426e28299ccSmatthiasgrimm  //we finish after 'cnt' references found. The return value
427b59a406bSmatthiasgrimm  //'false' will skip subdirectories to speed search up.
428e28299ccSmatthiasgrimm  $cnt = $conf['refshow'] > 0 ? $conf['refshow'] : 1;
429e28299ccSmatthiasgrimm  if(count($data) >= $cnt) return false;
430b59a406bSmatthiasgrimm
431d67ca2c0Smatthiasgrimm  $reg = '\{\{ *\:?'.$opts['query'].' *(\|.*)?\}\}';
432b59a406bSmatthiasgrimm  search_regex($data,$base,$file,$reg,array($opts['query']));
433b59a406bSmatthiasgrimm  return true;
434b59a406bSmatthiasgrimm}
435b59a406bSmatthiasgrimm
436b59a406bSmatthiasgrimm/* ------------- helper functions below -------------- */
437b59a406bSmatthiasgrimm
438b59a406bSmatthiasgrimm/**
439b59a406bSmatthiasgrimm * fulltext search helper
440b59a406bSmatthiasgrimm * searches a text file with a given regular expression
441b59a406bSmatthiasgrimm * no ACL checks are performed. This have to be done by
442b59a406bSmatthiasgrimm * the caller if necessary.
443b59a406bSmatthiasgrimm *
444b59a406bSmatthiasgrimm * @param array  $data  reference to array for results
445b59a406bSmatthiasgrimm * @param string $base  base directory
446b59a406bSmatthiasgrimm * @param string $file  file name to search in
447b59a406bSmatthiasgrimm * @param string $reg   regular expression to search for
448b59a406bSmatthiasgrimm * @param array  $words words that should be marked in the results
449b59a406bSmatthiasgrimm *
450b59a406bSmatthiasgrimm * @author  Andreas Gohr <andi@splitbrain.org>
451b59a406bSmatthiasgrimm * @author  Matthias Grimm <matthiasgrimm@users.sourceforge.net>
452506fa893SAndreas Gohr *
453506fa893SAndreas Gohr * @deprecated - fulltext indexer is used instead
454b59a406bSmatthiasgrimm */
455b59a406bSmatthiasgrimmfunction search_regex(&$data,$base,$file,$reg,$words){
456b59a406bSmatthiasgrimm
457b59a406bSmatthiasgrimm  //get text
458b59a406bSmatthiasgrimm  $text = io_readfile($base.'/'.$file);
459b59a406bSmatthiasgrimm  //lowercase text (u modifier does not help with case)
460b59a406bSmatthiasgrimm  $lctext = utf8_strtolower($text);
461f3f0262cSandi
462f3f0262cSandi  //do the fulltext search
463f3f0262cSandi  $matches = array();
4645ef370d2Smatthiasgrimm  if($cnt = preg_match_all('#'.$reg.'#usi',$lctext,$matches)){
465f3f0262cSandi    //this is not the best way for snippet generation but the fastest I could find
466b59a406bSmatthiasgrimm    $q = $words[0];  //use first word for snippet creation
467d5a2a500Sandi    $p = utf8_strpos($lctext,$q);
468f3f0262cSandi    $f = $p - 100;
469d5a2a500Sandi    $l = utf8_strlen($q) + 200;
470f3f0262cSandi    if($f < 0) $f = 0;
471f3f0262cSandi    $snippet = '<span class="search_sep"> ... </span>'.
472d5a2a500Sandi               htmlspecialchars(utf8_substr($text,$f,$l)).
473f3f0262cSandi               '<span class="search_sep"> ... </span>';
474b59a406bSmatthiasgrimm    $mark    = '('.join('|', $words).')';
475ed7ecb79SAnika Henke    $snippet = preg_replace('#'.$mark.'#si','<strong class="search_hit">\\1</strong>',$snippet);
476f3f0262cSandi
477f3f0262cSandi    $data[] = array(
478b59a406bSmatthiasgrimm      'id'       => pathID($file),
4795ef370d2Smatthiasgrimm      'count'    => preg_match_all('#'.$mark.'#usi',$lctext,$matches),
480b59a406bSmatthiasgrimm      'poswords' => join(' ',$words),
481f3f0262cSandi      'snippet'  => $snippet,
482f3f0262cSandi    );
483f3f0262cSandi  }
484f3f0262cSandi
485f3f0262cSandi  return true;
486f3f0262cSandi}
487f3f0262cSandi
488b59a406bSmatthiasgrimm
489f3f0262cSandi/**
49015fae107Sandi * fulltext sort
49115fae107Sandi *
492f3f0262cSandi * Callback sort function for use with usort to sort the data
493f3f0262cSandi * structure created by search_fulltext. Sorts descending by count
49415fae107Sandi *
49515fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
496f3f0262cSandi */
497f3f0262cSandifunction sort_search_fulltext($a,$b){
498f3f0262cSandi  if($a['count'] > $b['count']){
499f3f0262cSandi    return -1;
500f3f0262cSandi  }elseif($a['count'] < $b['count']){
501f3f0262cSandi    return 1;
502f3f0262cSandi  }else{
503f3f0262cSandi    return strcmp($a['id'],$b['id']);
504f3f0262cSandi  }
505f3f0262cSandi}
506f3f0262cSandi
507f3f0262cSandi/**
508f3f0262cSandi * translates a document path to an ID
50915fae107Sandi *
51015fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
51137e34a5eSandi * @todo    move to pageutils
512f3f0262cSandi */
513156a608cSandifunction pathID($path,$keeptxt=false){
51449c713a3Sandi  $id = utf8_decodeFN($path);
51549c713a3Sandi  $id = str_replace('/',':',$id);
516156a608cSandi  if(!$keeptxt) $id = preg_replace('#\.txt$#','',$id);
517f3f0262cSandi  $id = preg_replace('#^:+#','',$id);
518f3f0262cSandi  $id = preg_replace('#:+$#','',$id);
519f3f0262cSandi  return $id;
520f3f0262cSandi}
521f3f0262cSandi
522340756e4Sandi
523340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
524