xref: /dokuwiki/inc/search.php (revision b54f94e00cd0f1a818dd79fb2ce3bca7117e243e)
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
9ed7b5f09Sandi  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(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;
3764f50cdbSandi    }elseif(substr($file,-5) == '.lock'){
3864f50cdbSandi      //skip lockfiles
3964f50cdbSandi      continue;
40f3f0262cSandi    }
41f3f0262cSandi    $files[] = $dir.'/'.$file;
42f3f0262cSandi  }
43f3f0262cSandi  closedir($dh);
44f3f0262cSandi  sort($files);
45f3f0262cSandi  sort($dirs);
46f3f0262cSandi
47f3f0262cSandi  //give directories to userfunction then recurse
48f3f0262cSandi  foreach($dirs as $dir){
4924baa045SAndreas Gohr    if (search_callback($func,$data,$base,$dir,'d',$lvl,$opts)){
50f3f0262cSandi      search($data,$base,$func,$opts,$dir,$lvl+1);
51f3f0262cSandi    }
52f3f0262cSandi  }
53f3f0262cSandi  //now handle the files
54f3f0262cSandi  foreach($files as $file){
5524baa045SAndreas Gohr    search_callback($func,$data,$base,$file,'f',$lvl,$opts);
56f3f0262cSandi  }
57f3f0262cSandi}
58f3f0262cSandi
59f3f0262cSandi/**
6024baa045SAndreas Gohr * Used to run the a user callback
6124baa045SAndreas Gohr *
6224baa045SAndreas Gohr * Makes sure the $data array is passed by reference (unlike when using
6324baa045SAndreas Gohr * call_user_func())
6424baa045SAndreas Gohr *
6524baa045SAndreas Gohr * @todo If this can be generalized it may be useful elsewhere in the code
6624baa045SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
6724baa045SAndreas Gohr */
6824baa045SAndreas Gohrfunction search_callback($func,&$data,$base,$file,$type,$lvl,$opts){
6924baa045SAndreas Gohr  if(is_array($func)){
7024baa045SAndreas Gohr    if(is_object($func[0])){
7124baa045SAndreas Gohr      // instanciated object
7224baa045SAndreas Gohr      return $func[0]->$func[1]($data,$base,$file,$type,$lvl,$opts);
7324baa045SAndreas Gohr    }else{
7424baa045SAndreas Gohr      // static call
7524baa045SAndreas Gohr      $f = $func[0].'::'.$func[1];
7624baa045SAndreas Gohr      return $f($data,$base,$file,$type,$lvl,$opts);
7724baa045SAndreas Gohr    }
7824baa045SAndreas Gohr  }
7924baa045SAndreas Gohr  // simple function call
8024baa045SAndreas Gohr  return $func($data,$base,$file,$type,$lvl,$opts);
8124baa045SAndreas Gohr}
8224baa045SAndreas Gohr
8324baa045SAndreas Gohr/**
84f3f0262cSandi * The following functions are userfunctions to use with the search
85f3f0262cSandi * function above. This function is called for every found file or
86f3f0262cSandi * directory. When a directory is given to the function it has to
87f3f0262cSandi * decide if this directory should be traversed (true) or not (false)
88f3f0262cSandi * The function has to accept the following parameters:
89f3f0262cSandi *
90f3f0262cSandi * &$data - Reference to the result data structure
91f3f0262cSandi * $base  - Base usually $conf['datadir']
92f3f0262cSandi * $file  - current file or directory relative to $base
93f3f0262cSandi * $type  - Type either 'd' for directory or 'f' for file
94f3f0262cSandi * $lvl   - Current recursion depht
95f3f0262cSandi * $opts  - option array as given to search()
96f3f0262cSandi *
97f3f0262cSandi * return values for files are ignored
98f3f0262cSandi *
99f3f0262cSandi * All functions should check the ACL for document READ rights
100f3f0262cSandi * namespaces (directories) are NOT checked as this would break
101f3f0262cSandi * the recursion (You can have an nonreadable dir over a readable
102f3f0262cSandi * one deeper nested)
103f3f0262cSandi */
104f3f0262cSandi
105f3f0262cSandi/**
10663f2400bSandi * Searches for pages beginning with the given query
10763f2400bSandi *
10863f2400bSandi * @author Andreas Gohr <andi@splitbrain.org>
10963f2400bSandi */
11063f2400bSandifunction search_qsearch(&$data,$base,$file,$type,$lvl,$opts){
11163f2400bSandi  $item = array();
11263f2400bSandi
11363f2400bSandi  if($type == 'd'){
11463f2400bSandi    return false; //no handling yet
11563f2400bSandi  }
11663f2400bSandi
11763f2400bSandi  //get id
11863f2400bSandi  $id = pathID($file);
11963f2400bSandi
12063f2400bSandi  //check if it matches the query
12163f2400bSandi  if(!preg_match('/^'.preg_quote($opts['query'],'/').'/u',$id)){
12263f2400bSandi    return false;
12363f2400bSandi  }
12463f2400bSandi
12563f2400bSandi  //check ACL
12663f2400bSandi  if(auth_quickaclcheck($id) < AUTH_READ){
12763f2400bSandi    return false;
12863f2400bSandi  }
12963f2400bSandi
13063f2400bSandi  $data[]=array( 'id'    => $id,
13163f2400bSandi                 'type'  => $type,
13263f2400bSandi                 'level' => 1,
13363f2400bSandi                 'open'  => true);
13463f2400bSandi  return true;
13563f2400bSandi}
13663f2400bSandi
13763f2400bSandi/**
13815fae107Sandi * Build the browsable index of pages
139f3f0262cSandi *
140f3f0262cSandi * $opts['ns'] is the current namespace
14115fae107Sandi *
14215fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
143f3f0262cSandi */
144f3f0262cSandifunction search_index(&$data,$base,$file,$type,$lvl,$opts){
145f3f0262cSandi  $return = true;
146f3f0262cSandi
147cb70c441Sandi  $item = array();
148cb70c441Sandi
149f3f0262cSandi  if($type == 'd' && !preg_match('#^'.$file.'(/|$)#','/'.$opts['ns'])){
150f3f0262cSandi    //add but don't recurse
151f3f0262cSandi    $return = false;
152ee7b5a62SAndreas Gohr  }elseif($type == 'f' && ($opts['nofiles'] || !preg_match('#\.txt$#',$file))){
153f3f0262cSandi    //don't add
154f3f0262cSandi    return false;
155f3f0262cSandi  }
156f3f0262cSandi
157f3f0262cSandi  $id = pathID($file);
1580dc92c6fSAndreas Gohr
159*b54f94e0SAndreas Gohr  if($type=='d' && auth_quickaclcheck($id.':') < AUTH_READ){
160*b54f94e0SAndreas Gohr	  return false;
161*b54f94e0SAndreas Gohr  }
162*b54f94e0SAndreas Gohr
1630dc92c6fSAndreas Gohr  //check hidden
1641211a7a9SMartin Tschofen  if(isHiddenPage($id)){
1650dc92c6fSAndreas Gohr    return false;
1660dc92c6fSAndreas Gohr  }
1670dc92c6fSAndreas Gohr
1680dc92c6fSAndreas Gohr  //check ACL
169f3f0262cSandi  if($type=='f' && auth_quickaclcheck($id) < AUTH_READ){
170f3f0262cSandi    return false;
171f3f0262cSandi  }
172f3f0262cSandi
173f3f0262cSandi  $data[]=array( 'id'    => $id,
174f3f0262cSandi                 'type'  => $type,
175cb70c441Sandi                 'level' => $lvl,
176cb70c441Sandi                 'open'  => $return );
177f3f0262cSandi  return $return;
178f3f0262cSandi}
179f3f0262cSandi
180f3f0262cSandi/**
18115fae107Sandi * List all namespaces
18215fae107Sandi *
18315fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
184f3f0262cSandi */
185f3f0262cSandifunction search_namespaces(&$data,$base,$file,$type,$lvl,$opts){
186f3f0262cSandi  if($type == 'f') return true; //nothing to do on files
187f3f0262cSandi
188f3f0262cSandi  $id = pathID($file);
189f3f0262cSandi  $data[]=array( 'id'    => $id,
190f3f0262cSandi                 'type'  => $type,
191f3f0262cSandi                 'level' => $lvl );
192f3f0262cSandi  return true;
193f3f0262cSandi}
194f3f0262cSandi
195f3f0262cSandi/**
19615fae107Sandi * List all mediafiles in a namespace
19715fae107Sandi *
19815fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
199f3f0262cSandi */
200f3f0262cSandifunction search_media(&$data,$base,$file,$type,$lvl,$opts){
201f3f0262cSandi  //we do nothing with directories
202f3f0262cSandi  if($type == 'd') return false;
203f3f0262cSandi
204f3f0262cSandi  $info         = array();
205156a608cSandi  $info['id']   = pathID($file,true);
206f3f0262cSandi
207f3f0262cSandi  //check ACL for namespace (we have no ACL for mediafiles)
208f3f0262cSandi  if(auth_quickaclcheck(getNS($info['id']).':*') < AUTH_READ){
209f3f0262cSandi    return false;
210f3f0262cSandi  }
211f3f0262cSandi
212f3f0262cSandi  $info['file'] = basename($file);
213f3f0262cSandi  $info['size'] = filesize($base.'/'.$file);
2145e7fa82eSAndreas Gohr  $info['mtime'] = filemtime($base.'/'.$file);
2153df72098SAndreas Gohr  $info['writable'] = is_writable($base.'/'.$file);
216f3f0262cSandi  if(preg_match("/\.(jpe?g|gif|png)$/",$file)){
217f3f0262cSandi    $info['isimg'] = true;
21823a34783SAndreas Gohr    require_once(DOKU_INC.'inc/JpegMeta.php');
21923a34783SAndreas Gohr    $info['meta']  = new JpegMeta($base.'/'.$file);
220f3f0262cSandi  }else{
221f3f0262cSandi    $info['isimg'] = false;
222f3f0262cSandi  }
223f3f0262cSandi  $data[] = $info;
224f3f0262cSandi
225f3f0262cSandi  return false;
226f3f0262cSandi}
227f3f0262cSandi
228f3f0262cSandi/**
229f3f0262cSandi * This function just lists documents (for RSS namespace export)
23015fae107Sandi *
23115fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
232f3f0262cSandi */
233f3f0262cSandifunction search_list(&$data,$base,$file,$type,$lvl,$opts){
234f3f0262cSandi  //we do nothing with directories
235f3f0262cSandi  if($type == 'd') return false;
236f3f0262cSandi  if(preg_match('#\.txt$#',$file)){
237f3f0262cSandi    //check ACL
238f3f0262cSandi    $id = pathID($file);
239f3f0262cSandi    if(auth_quickaclcheck($id) < AUTH_READ){
240f3f0262cSandi      return false;
241f3f0262cSandi    }
242f3f0262cSandi    $data[]['id'] = $id;;
243f3f0262cSandi  }
244f3f0262cSandi  return false;
245f3f0262cSandi}
246f3f0262cSandi
247f3f0262cSandi/**
248f3f0262cSandi * Quicksearch for searching matching pagenames
249f3f0262cSandi *
250f3f0262cSandi * $opts['query'] is the search query
25115fae107Sandi *
25215fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
253f3f0262cSandi */
254f3f0262cSandifunction search_pagename(&$data,$base,$file,$type,$lvl,$opts){
255f3f0262cSandi  //we do nothing with directories
256f3f0262cSandi  if($type == 'd') return true;
257f3f0262cSandi  //only search txt files
258f3f0262cSandi  if(!preg_match('#\.txt$#',$file)) return true;
259f3f0262cSandi
260f3f0262cSandi  //simple stringmatching
261396b7edbSmatthiasgrimm  if (!empty($opts['query'])){
262f3f0262cSandi    if(strpos($file,$opts['query']) !== false){
263f3f0262cSandi      //check ACL
264f3f0262cSandi      $id = pathID($file);
265f3f0262cSandi      if(auth_quickaclcheck($id) < AUTH_READ){
266f3f0262cSandi        return false;
267f3f0262cSandi      }
268f3f0262cSandi      $data[]['id'] = $id;
269f3f0262cSandi    }
270396b7edbSmatthiasgrimm  }
271f3f0262cSandi  return true;
272f3f0262cSandi}
273f3f0262cSandi
274f3f0262cSandi/**
27558b6f612SAndreas Gohr * Just lists all documents
27658b6f612SAndreas Gohr *
27758b6f612SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
27858b6f612SAndreas Gohr */
27958b6f612SAndreas Gohrfunction search_allpages(&$data,$base,$file,$type,$lvl,$opts){
28058b6f612SAndreas Gohr  //we do nothing with directories
28158b6f612SAndreas Gohr  if($type == 'd') return true;
28258b6f612SAndreas Gohr  //only search txt files
28358b6f612SAndreas Gohr  if(!preg_match('#\.txt$#',$file)) return true;
28458b6f612SAndreas Gohr
28558b6f612SAndreas Gohr  $data[]['id'] = pathID($file);
28658b6f612SAndreas Gohr  return true;
28758b6f612SAndreas Gohr}
28858b6f612SAndreas Gohr
28958b6f612SAndreas Gohr/**
290f3f0262cSandi * Search for backlinks to a given page
291f3f0262cSandi *
292f3f0262cSandi * $opts['ns']    namespace of the page
293f3f0262cSandi * $opts['name']  name of the page without namespace
29415fae107Sandi *
29515fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
29654f4c056SAndreas Gohr * @deprecated Replaced by ft_backlinks()
297f3f0262cSandi */
298f3f0262cSandifunction search_backlinks(&$data,$base,$file,$type,$lvl,$opts){
299f3f0262cSandi  //we do nothing with directories
300f3f0262cSandi  if($type == 'd') return true;;
301f3f0262cSandi  //only search txt files
302f3f0262cSandi  if(!preg_match('#\.txt$#',$file)) return true;;
303f3f0262cSandi
304f3f0262cSandi  //absolute search id
305f3f0262cSandi  $sid = cleanID($opts['ns'].':'.$opts['name']);
306f3f0262cSandi
30737e34a5eSandi  //current id and namespace
308f3f0262cSandi  $cid = pathID($file);
309f3f0262cSandi  $cns = getNS($cid);
310f3f0262cSandi
311f3f0262cSandi  //check ACL
312f3f0262cSandi  if(auth_quickaclcheck($cid) < AUTH_READ){
313f3f0262cSandi    return false;
314f3f0262cSandi  }
315f3f0262cSandi
31637e34a5eSandi  //fetch instructions
31737e34a5eSandi  require_once(DOKU_INC.'inc/parserutils.php');
31837e34a5eSandi  $instructions = p_cached_instructions($base.$file,true);
31937e34a5eSandi  if(is_null($instructions)) return false;
320f3f0262cSandi
32137e34a5eSandi  //check all links for match
32237e34a5eSandi  foreach($instructions as $ins){
32337e34a5eSandi    if($ins[0] == 'internallink' || ($conf['camelcase'] && $ins[0] == 'camelcaselink') ){
32437e34a5eSandi      $mid = $ins[1][0];
32537e34a5eSandi      resolve_pageid($cns,$mid,$exists); //exists is not used
326f3f0262cSandi      if($mid == $sid){
32737e34a5eSandi        //we have a match - finish
328f3f0262cSandi        $data[]['id'] = $cid;
329f3f0262cSandi        break;
330f3f0262cSandi      }
331f3f0262cSandi    }
332f3f0262cSandi  }
333f3f0262cSandi
33437e34a5eSandi  return false;
33537e34a5eSandi}
33637e34a5eSandi
337f3f0262cSandi/**
338f3f0262cSandi * Fulltextsearch
339f3f0262cSandi *
340f3f0262cSandi * $opts['query'] is the search query
34115fae107Sandi *
34215fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
343506fa893SAndreas Gohr * @deprecated - fulltext indexer is used instead
344f3f0262cSandi */
345f3f0262cSandifunction search_fulltext(&$data,$base,$file,$type,$lvl,$opts){
346f3f0262cSandi  //we do nothing with directories
347f3f0262cSandi  if($type == 'd') return true;;
348f3f0262cSandi  //only search txt files
349f3f0262cSandi  if(!preg_match('#\.txt$#',$file)) return true;;
350f3f0262cSandi
351f3f0262cSandi  //check ACL
352f3f0262cSandi  $id = pathID($file);
353f3f0262cSandi  if(auth_quickaclcheck($id) < AUTH_READ){
354f3f0262cSandi    return false;
355f3f0262cSandi  }
356f3f0262cSandi
357f3f0262cSandi  //create regexp from queries
3585ef370d2Smatthiasgrimm  $poswords = array();
3595ef370d2Smatthiasgrimm  $negwords = array();
3605ef370d2Smatthiasgrimm  $qpreg = preg_split('/\s+/',$opts['query']);
3615ef370d2Smatthiasgrimm
3625ef370d2Smatthiasgrimm  foreach($qpreg as $word){
3635ef370d2Smatthiasgrimm    switch(substr($word,0,1)){
3645ef370d2Smatthiasgrimm      case '-':
365396b7edbSmatthiasgrimm        if(strlen($word) > 1){  // catch single '-'
3665ef370d2Smatthiasgrimm          array_push($negwords,preg_quote(substr($word,1),'#'));
367396b7edbSmatthiasgrimm        }
3685ef370d2Smatthiasgrimm        break;
3695ef370d2Smatthiasgrimm      case '+':
370396b7edbSmatthiasgrimm        if(strlen($word) > 1){  // catch single '+'
3715ef370d2Smatthiasgrimm          array_push($poswords,preg_quote(substr($word,1),'#'));
372396b7edbSmatthiasgrimm        }
3735ef370d2Smatthiasgrimm        break;
3745ef370d2Smatthiasgrimm      default:
3755ef370d2Smatthiasgrimm        array_push($poswords,preg_quote($word,'#'));
3765ef370d2Smatthiasgrimm        break;
3775ef370d2Smatthiasgrimm    }
3785ef370d2Smatthiasgrimm  }
379248a7321Smatthiasgrimm
380248a7321Smatthiasgrimm  // a search without any posword is useless
381248a7321Smatthiasgrimm  if (!count($poswords)) return true;
3825ef370d2Smatthiasgrimm
3835a5d942dSmatthiasgrimm  $reg  = '^(?=.*?'.join(')(?=.*?',$poswords).')';
3845ef370d2Smatthiasgrimm  $reg .= count($negwords) ? '((?!'.join('|',$negwords).').)*$' : '.*$';
385b59a406bSmatthiasgrimm  search_regex($data,$base,$file,$reg,$poswords);
386b59a406bSmatthiasgrimm  return true;
387b59a406bSmatthiasgrimm}
388b59a406bSmatthiasgrimm
389b59a406bSmatthiasgrimm/**
390b59a406bSmatthiasgrimm * Reference search
391b59a406bSmatthiasgrimm * This fuction searches for existing references to a given media file
392b59a406bSmatthiasgrimm * and returns an array with the found pages. It doesn't pay any
393b59a406bSmatthiasgrimm * attention to ACL permissions to find every reference. The caller
394b59a406bSmatthiasgrimm * must check if the user has the appropriate rights to see the found
395b59a406bSmatthiasgrimm * page and eventually have to prevent the result from displaying.
396b59a406bSmatthiasgrimm *
397b59a406bSmatthiasgrimm * @param array  $data Reference to the result data structure
398b59a406bSmatthiasgrimm * @param string $base Base usually $conf['datadir']
399b59a406bSmatthiasgrimm * @param string $file current file or directory relative to $base
400b59a406bSmatthiasgrimm * @param char   $type Type either 'd' for directory or 'f' for file
401b59a406bSmatthiasgrimm * @param int    $lvl  Current recursion depht
402b59a406bSmatthiasgrimm * @param mixed  $opts option array as given to search()
403b59a406bSmatthiasgrimm *
404b59a406bSmatthiasgrimm * $opts['query'] is the demanded media file name
405b59a406bSmatthiasgrimm *
406b59a406bSmatthiasgrimm * @author  Andreas Gohr <andi@splitbrain.org>
407b59a406bSmatthiasgrimm * @author  Matthias Grimm <matthiasgrimm@users.sourceforge.net>
408b59a406bSmatthiasgrimm */
409b59a406bSmatthiasgrimmfunction search_reference(&$data,$base,$file,$type,$lvl,$opts){
410b59a406bSmatthiasgrimm  global $conf;
411b59a406bSmatthiasgrimm
412b59a406bSmatthiasgrimm  //we do nothing with directories
413b59a406bSmatthiasgrimm  if($type == 'd') return true;
414b59a406bSmatthiasgrimm
415b59a406bSmatthiasgrimm  //only search txt files
416b59a406bSmatthiasgrimm  if(!preg_match('#\.txt$#',$file)) return true;
417b59a406bSmatthiasgrimm
418e28299ccSmatthiasgrimm  //we finish after 'cnt' references found. The return value
419b59a406bSmatthiasgrimm  //'false' will skip subdirectories to speed search up.
420e28299ccSmatthiasgrimm  $cnt = $conf['refshow'] > 0 ? $conf['refshow'] : 1;
421e28299ccSmatthiasgrimm  if(count($data) >= $cnt) return false;
422b59a406bSmatthiasgrimm
423d67ca2c0Smatthiasgrimm  $reg = '\{\{ *\:?'.$opts['query'].' *(\|.*)?\}\}';
424b59a406bSmatthiasgrimm  search_regex($data,$base,$file,$reg,array($opts['query']));
425b59a406bSmatthiasgrimm  return true;
426b59a406bSmatthiasgrimm}
427b59a406bSmatthiasgrimm
428b59a406bSmatthiasgrimm/* ------------- helper functions below -------------- */
429b59a406bSmatthiasgrimm
430b59a406bSmatthiasgrimm/**
431b59a406bSmatthiasgrimm * fulltext search helper
432b59a406bSmatthiasgrimm * searches a text file with a given regular expression
433b59a406bSmatthiasgrimm * no ACL checks are performed. This have to be done by
434b59a406bSmatthiasgrimm * the caller if necessary.
435b59a406bSmatthiasgrimm *
436b59a406bSmatthiasgrimm * @param array  $data  reference to array for results
437b59a406bSmatthiasgrimm * @param string $base  base directory
438b59a406bSmatthiasgrimm * @param string $file  file name to search in
439b59a406bSmatthiasgrimm * @param string $reg   regular expression to search for
440b59a406bSmatthiasgrimm * @param array  $words words that should be marked in the results
441b59a406bSmatthiasgrimm *
442b59a406bSmatthiasgrimm * @author  Andreas Gohr <andi@splitbrain.org>
443b59a406bSmatthiasgrimm * @author  Matthias Grimm <matthiasgrimm@users.sourceforge.net>
444506fa893SAndreas Gohr *
445506fa893SAndreas Gohr * @deprecated - fulltext indexer is used instead
446b59a406bSmatthiasgrimm */
447b59a406bSmatthiasgrimmfunction search_regex(&$data,$base,$file,$reg,$words){
448b59a406bSmatthiasgrimm
449b59a406bSmatthiasgrimm  //get text
450b59a406bSmatthiasgrimm  $text = io_readfile($base.'/'.$file);
451b59a406bSmatthiasgrimm  //lowercase text (u modifier does not help with case)
452b59a406bSmatthiasgrimm  $lctext = utf8_strtolower($text);
453f3f0262cSandi
454f3f0262cSandi  //do the fulltext search
455f3f0262cSandi  $matches = array();
4565ef370d2Smatthiasgrimm  if($cnt = preg_match_all('#'.$reg.'#usi',$lctext,$matches)){
457f3f0262cSandi    //this is not the best way for snippet generation but the fastest I could find
458b59a406bSmatthiasgrimm    $q = $words[0];  //use first word for snippet creation
459d5a2a500Sandi    $p = utf8_strpos($lctext,$q);
460f3f0262cSandi    $f = $p - 100;
461d5a2a500Sandi    $l = utf8_strlen($q) + 200;
462f3f0262cSandi    if($f < 0) $f = 0;
463f3f0262cSandi    $snippet = '<span class="search_sep"> ... </span>'.
464d5a2a500Sandi               htmlspecialchars(utf8_substr($text,$f,$l)).
465f3f0262cSandi               '<span class="search_sep"> ... </span>';
466b59a406bSmatthiasgrimm    $mark    = '('.join('|', $words).')';
4675ef370d2Smatthiasgrimm    $snippet = preg_replace('#'.$mark.'#si','<span class="search_hit">\\1</span>',$snippet);
468f3f0262cSandi
469f3f0262cSandi    $data[] = array(
470b59a406bSmatthiasgrimm      'id'       => pathID($file),
4715ef370d2Smatthiasgrimm      'count'    => preg_match_all('#'.$mark.'#usi',$lctext,$matches),
472b59a406bSmatthiasgrimm      'poswords' => join(' ',$words),
473f3f0262cSandi      'snippet'  => $snippet,
474f3f0262cSandi    );
475f3f0262cSandi  }
476f3f0262cSandi
477f3f0262cSandi  return true;
478f3f0262cSandi}
479f3f0262cSandi
480b59a406bSmatthiasgrimm
481f3f0262cSandi/**
48215fae107Sandi * fulltext sort
48315fae107Sandi *
484f3f0262cSandi * Callback sort function for use with usort to sort the data
485f3f0262cSandi * structure created by search_fulltext. Sorts descending by count
48615fae107Sandi *
48715fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
488f3f0262cSandi */
489f3f0262cSandifunction sort_search_fulltext($a,$b){
490f3f0262cSandi  if($a['count'] > $b['count']){
491f3f0262cSandi    return -1;
492f3f0262cSandi  }elseif($a['count'] < $b['count']){
493f3f0262cSandi    return 1;
494f3f0262cSandi  }else{
495f3f0262cSandi    return strcmp($a['id'],$b['id']);
496f3f0262cSandi  }
497f3f0262cSandi}
498f3f0262cSandi
499f3f0262cSandi/**
500f3f0262cSandi * translates a document path to an ID
50115fae107Sandi *
50215fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
50337e34a5eSandi * @todo    move to pageutils
504f3f0262cSandi */
505156a608cSandifunction pathID($path,$keeptxt=false){
50649c713a3Sandi  $id = utf8_decodeFN($path);
50749c713a3Sandi  $id = str_replace('/',':',$id);
508156a608cSandi  if(!$keeptxt) $id = preg_replace('#\.txt$#','',$id);
509f3f0262cSandi  $id = preg_replace('#^:+#','',$id);
510f3f0262cSandi  $id = preg_replace('#:+$#','',$id);
511f3f0262cSandi  return $id;
512f3f0262cSandi}
513f3f0262cSandi
514340756e4Sandi
515340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
516