xref: /dokuwiki/inc/search.php (revision 0dc92c6f78995331021c3b8c6a889913cf3f7de3)
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 *
1815fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
19f3f0262cSandi */
20f3f0262cSandifunction search(&$data,$base,$func,$opts,$dir='',$lvl=1){
21f3f0262cSandi  $dirs   = array();
22f3f0262cSandi  $files  = array();
23f3f0262cSandi
24f3f0262cSandi  //read in directories and files
25f3f0262cSandi  $dh = @opendir($base.'/'.$dir);
26f3f0262cSandi  if(!$dh) return;
27f3f0262cSandi  while(($file = readdir($dh)) !== false){
28de3dfc91Sandi    if(preg_match('/^[\._]/',$file)) continue; //skip hidden files and upper dirs
29f3f0262cSandi    if(is_dir($base.'/'.$dir.'/'.$file)){
30f3f0262cSandi      $dirs[] = $dir.'/'.$file;
31f3f0262cSandi      continue;
3264f50cdbSandi    }elseif(substr($file,-5) == '.lock'){
3364f50cdbSandi      //skip lockfiles
3464f50cdbSandi      continue;
35f3f0262cSandi    }
36f3f0262cSandi    $files[] = $dir.'/'.$file;
37f3f0262cSandi  }
38f3f0262cSandi  closedir($dh);
39f3f0262cSandi  sort($files);
40f3f0262cSandi  sort($dirs);
41f3f0262cSandi
42f3f0262cSandi  //give directories to userfunction then recurse
43f3f0262cSandi  foreach($dirs as $dir){
44f3f0262cSandi    if ($func($data,$base,$dir,'d',$lvl,$opts)){
45f3f0262cSandi      search($data,$base,$func,$opts,$dir,$lvl+1);
46f3f0262cSandi    }
47f3f0262cSandi  }
48f3f0262cSandi  //now handle the files
49f3f0262cSandi  foreach($files as $file){
50f3f0262cSandi    $func($data,$base,$file,'f',$lvl,$opts);
51f3f0262cSandi  }
52f3f0262cSandi}
53f3f0262cSandi
54f3f0262cSandi/**
55f3f0262cSandi * The following functions are userfunctions to use with the search
56f3f0262cSandi * function above. This function is called for every found file or
57f3f0262cSandi * directory. When a directory is given to the function it has to
58f3f0262cSandi * decide if this directory should be traversed (true) or not (false)
59f3f0262cSandi * The function has to accept the following parameters:
60f3f0262cSandi *
61f3f0262cSandi * &$data - Reference to the result data structure
62f3f0262cSandi * $base  - Base usually $conf['datadir']
63f3f0262cSandi * $file  - current file or directory relative to $base
64f3f0262cSandi * $type  - Type either 'd' for directory or 'f' for file
65f3f0262cSandi * $lvl   - Current recursion depht
66f3f0262cSandi * $opts  - option array as given to search()
67f3f0262cSandi *
68f3f0262cSandi * return values for files are ignored
69f3f0262cSandi *
70f3f0262cSandi * All functions should check the ACL for document READ rights
71f3f0262cSandi * namespaces (directories) are NOT checked as this would break
72f3f0262cSandi * the recursion (You can have an nonreadable dir over a readable
73f3f0262cSandi * one deeper nested)
74f3f0262cSandi */
75f3f0262cSandi
76f3f0262cSandi/**
7763f2400bSandi * Searches for pages beginning with the given query
7863f2400bSandi *
7963f2400bSandi * @author Andreas Gohr <andi@splitbrain.org>
8063f2400bSandi */
8163f2400bSandifunction search_qsearch(&$data,$base,$file,$type,$lvl,$opts){
8263f2400bSandi  $item = array();
8363f2400bSandi
8463f2400bSandi  if($type == 'd'){
8563f2400bSandi    return false; //no handling yet
8663f2400bSandi  }
8763f2400bSandi
8863f2400bSandi  //get id
8963f2400bSandi  $id = pathID($file);
9063f2400bSandi
9163f2400bSandi  //check if it matches the query
9263f2400bSandi  if(!preg_match('/^'.preg_quote($opts['query'],'/').'/u',$id)){
9363f2400bSandi    return false;
9463f2400bSandi  }
9563f2400bSandi
9663f2400bSandi  //check ACL
9763f2400bSandi  if(auth_quickaclcheck($id) < AUTH_READ){
9863f2400bSandi    return false;
9963f2400bSandi  }
10063f2400bSandi
10163f2400bSandi  $data[]=array( 'id'    => $id,
10263f2400bSandi                 'type'  => $type,
10363f2400bSandi                 'level' => 1,
10463f2400bSandi                 'open'  => true);
10563f2400bSandi  return true;
10663f2400bSandi}
10763f2400bSandi
10863f2400bSandi/**
10915fae107Sandi * Build the browsable index of pages
110f3f0262cSandi *
111f3f0262cSandi * $opts['ns'] is the current namespace
11215fae107Sandi *
11315fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
114f3f0262cSandi */
115f3f0262cSandifunction search_index(&$data,$base,$file,$type,$lvl,$opts){
116f3f0262cSandi  $return = true;
117f3f0262cSandi
118cb70c441Sandi  $item = array();
119cb70c441Sandi
120f3f0262cSandi  if($type == 'd' && !preg_match('#^'.$file.'(/|$)#','/'.$opts['ns'])){
121f3f0262cSandi    //add but don't recurse
122f3f0262cSandi    $return = false;
123f3f0262cSandi  }elseif($type == 'f' && !preg_match('#\.txt$#',$file)){
124f3f0262cSandi    //don't add
125f3f0262cSandi    return false;
126f3f0262cSandi  }
127f3f0262cSandi
128f3f0262cSandi  $id = pathID($file);
129*0dc92c6fSAndreas Gohr
130*0dc92c6fSAndreas Gohr  //check hidden
131*0dc92c6fSAndreas Gohr  if($type=='f' && isHiddenPage($id)){
132*0dc92c6fSAndreas Gohr    return false;
133*0dc92c6fSAndreas Gohr  }
134*0dc92c6fSAndreas Gohr
135*0dc92c6fSAndreas Gohr  //check ACL
136f3f0262cSandi  if($type=='f' && auth_quickaclcheck($id) < AUTH_READ){
137f3f0262cSandi    return false;
138f3f0262cSandi  }
139f3f0262cSandi
140f3f0262cSandi  $data[]=array( 'id'    => $id,
141f3f0262cSandi                 'type'  => $type,
142cb70c441Sandi                 'level' => $lvl,
143cb70c441Sandi                 'open'  => $return );
144f3f0262cSandi  return $return;
145f3f0262cSandi}
146f3f0262cSandi
147f3f0262cSandi/**
14815fae107Sandi * List all namespaces
14915fae107Sandi *
15015fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
151f3f0262cSandi */
152f3f0262cSandifunction search_namespaces(&$data,$base,$file,$type,$lvl,$opts){
153f3f0262cSandi  if($type == 'f') return true; //nothing to do on files
154f3f0262cSandi
155f3f0262cSandi  $id = pathID($file);
156f3f0262cSandi  $data[]=array( 'id'    => $id,
157f3f0262cSandi                 'type'  => $type,
158f3f0262cSandi                 'level' => $lvl );
159f3f0262cSandi  return true;
160f3f0262cSandi}
161f3f0262cSandi
162f3f0262cSandi/**
16315fae107Sandi * List all mediafiles in a namespace
16415fae107Sandi *
16515fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
166f3f0262cSandi */
167f3f0262cSandifunction search_media(&$data,$base,$file,$type,$lvl,$opts){
168f3f0262cSandi  //we do nothing with directories
169f3f0262cSandi  if($type == 'd') return false;
170f3f0262cSandi
171f3f0262cSandi  $info         = array();
172156a608cSandi  $info['id']   = pathID($file,true);
173f3f0262cSandi
174f3f0262cSandi  //check ACL for namespace (we have no ACL for mediafiles)
175f3f0262cSandi  if(auth_quickaclcheck(getNS($info['id']).':*') < AUTH_READ){
176f3f0262cSandi    return false;
177f3f0262cSandi  }
178f3f0262cSandi
179f3f0262cSandi  $info['file'] = basename($file);
180f3f0262cSandi  $info['size'] = filesize($base.'/'.$file);
181f3f0262cSandi  if(preg_match("/\.(jpe?g|gif|png)$/",$file)){
182f3f0262cSandi    $info['isimg'] = true;
18323a34783SAndreas Gohr    require_once(DOKU_INC.'inc/JpegMeta.php');
18423a34783SAndreas Gohr    $info['meta']  = new JpegMeta($base.'/'.$file);
185f3f0262cSandi  }else{
186f3f0262cSandi    $info['isimg'] = false;
187f3f0262cSandi  }
188f3f0262cSandi  $data[] = $info;
189f3f0262cSandi
190f3f0262cSandi  return false;
191f3f0262cSandi}
192f3f0262cSandi
193f3f0262cSandi/**
194f3f0262cSandi * This function just lists documents (for RSS namespace export)
19515fae107Sandi *
19615fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
197f3f0262cSandi */
198f3f0262cSandifunction search_list(&$data,$base,$file,$type,$lvl,$opts){
199f3f0262cSandi  //we do nothing with directories
200f3f0262cSandi  if($type == 'd') return false;
201f3f0262cSandi  if(preg_match('#\.txt$#',$file)){
202f3f0262cSandi    //check ACL
203f3f0262cSandi    $id = pathID($file);
204f3f0262cSandi    if(auth_quickaclcheck($id) < AUTH_READ){
205f3f0262cSandi      return false;
206f3f0262cSandi    }
207f3f0262cSandi    $data[]['id'] = $id;;
208f3f0262cSandi  }
209f3f0262cSandi  return false;
210f3f0262cSandi}
211f3f0262cSandi
212f3f0262cSandi/**
213f3f0262cSandi * Quicksearch for searching matching pagenames
214f3f0262cSandi *
215f3f0262cSandi * $opts['query'] is the search query
21615fae107Sandi *
21715fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
218f3f0262cSandi */
219f3f0262cSandifunction search_pagename(&$data,$base,$file,$type,$lvl,$opts){
220f3f0262cSandi  //we do nothing with directories
221f3f0262cSandi  if($type == 'd') return true;
222f3f0262cSandi  //only search txt files
223f3f0262cSandi  if(!preg_match('#\.txt$#',$file)) return true;
224f3f0262cSandi
225f3f0262cSandi  //simple stringmatching
226396b7edbSmatthiasgrimm  if (!empty($opts['query'])){
227f3f0262cSandi    if(strpos($file,$opts['query']) !== false){
228f3f0262cSandi      //check ACL
229f3f0262cSandi      $id = pathID($file);
230f3f0262cSandi      if(auth_quickaclcheck($id) < AUTH_READ){
231f3f0262cSandi        return false;
232f3f0262cSandi      }
233f3f0262cSandi      $data[]['id'] = $id;
234f3f0262cSandi    }
235396b7edbSmatthiasgrimm  }
236f3f0262cSandi  return true;
237f3f0262cSandi}
238f3f0262cSandi
239f3f0262cSandi/**
24058b6f612SAndreas Gohr * Just lists all documents
24158b6f612SAndreas Gohr *
24258b6f612SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
24358b6f612SAndreas Gohr */
24458b6f612SAndreas Gohrfunction search_allpages(&$data,$base,$file,$type,$lvl,$opts){
24558b6f612SAndreas Gohr  //we do nothing with directories
24658b6f612SAndreas Gohr  if($type == 'd') return true;
24758b6f612SAndreas Gohr  //only search txt files
24858b6f612SAndreas Gohr  if(!preg_match('#\.txt$#',$file)) return true;
24958b6f612SAndreas Gohr
25058b6f612SAndreas Gohr  $data[]['id'] = pathID($file);
25158b6f612SAndreas Gohr  return true;
25258b6f612SAndreas Gohr}
25358b6f612SAndreas Gohr
25458b6f612SAndreas Gohr/**
255f3f0262cSandi * Search for backlinks to a given page
256f3f0262cSandi *
257f3f0262cSandi * $opts['ns']    namespace of the page
258f3f0262cSandi * $opts['name']  name of the page without namespace
25915fae107Sandi *
26015fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
26154f4c056SAndreas Gohr * @deprecated Replaced by ft_backlinks()
262f3f0262cSandi */
263f3f0262cSandifunction search_backlinks(&$data,$base,$file,$type,$lvl,$opts){
264f3f0262cSandi  //we do nothing with directories
265f3f0262cSandi  if($type == 'd') return true;;
266f3f0262cSandi  //only search txt files
267f3f0262cSandi  if(!preg_match('#\.txt$#',$file)) return true;;
268f3f0262cSandi
269f3f0262cSandi  //absolute search id
270f3f0262cSandi  $sid = cleanID($opts['ns'].':'.$opts['name']);
271f3f0262cSandi
27237e34a5eSandi  //current id and namespace
273f3f0262cSandi  $cid = pathID($file);
274f3f0262cSandi  $cns = getNS($cid);
275f3f0262cSandi
276f3f0262cSandi  //check ACL
277f3f0262cSandi  if(auth_quickaclcheck($cid) < AUTH_READ){
278f3f0262cSandi    return false;
279f3f0262cSandi  }
280f3f0262cSandi
28137e34a5eSandi  //fetch instructions
28237e34a5eSandi  require_once(DOKU_INC.'inc/parserutils.php');
28337e34a5eSandi  $instructions = p_cached_instructions($base.$file,true);
28437e34a5eSandi  if(is_null($instructions)) return false;
285f3f0262cSandi
28637e34a5eSandi  //check all links for match
28737e34a5eSandi  foreach($instructions as $ins){
28837e34a5eSandi    if($ins[0] == 'internallink' || ($conf['camelcase'] && $ins[0] == 'camelcaselink') ){
28937e34a5eSandi      $mid = $ins[1][0];
29037e34a5eSandi      resolve_pageid($cns,$mid,$exists); //exists is not used
291f3f0262cSandi      if($mid == $sid){
29237e34a5eSandi        //we have a match - finish
293f3f0262cSandi        $data[]['id'] = $cid;
294f3f0262cSandi        break;
295f3f0262cSandi      }
296f3f0262cSandi    }
297f3f0262cSandi  }
298f3f0262cSandi
29937e34a5eSandi  return false;
30037e34a5eSandi}
30137e34a5eSandi
302f3f0262cSandi/**
303f3f0262cSandi * Fulltextsearch
304f3f0262cSandi *
305f3f0262cSandi * $opts['query'] is the search query
30615fae107Sandi *
30715fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
308506fa893SAndreas Gohr * @deprecated - fulltext indexer is used instead
309f3f0262cSandi */
310f3f0262cSandifunction search_fulltext(&$data,$base,$file,$type,$lvl,$opts){
311f3f0262cSandi  //we do nothing with directories
312f3f0262cSandi  if($type == 'd') return true;;
313f3f0262cSandi  //only search txt files
314f3f0262cSandi  if(!preg_match('#\.txt$#',$file)) return true;;
315f3f0262cSandi
316f3f0262cSandi  //check ACL
317f3f0262cSandi  $id = pathID($file);
318f3f0262cSandi  if(auth_quickaclcheck($id) < AUTH_READ){
319f3f0262cSandi    return false;
320f3f0262cSandi  }
321f3f0262cSandi
322f3f0262cSandi  //create regexp from queries
3235ef370d2Smatthiasgrimm  $poswords = array();
3245ef370d2Smatthiasgrimm  $negwords = array();
3255ef370d2Smatthiasgrimm  $qpreg = preg_split('/\s+/',$opts['query']);
3265ef370d2Smatthiasgrimm
3275ef370d2Smatthiasgrimm  foreach($qpreg as $word){
3285ef370d2Smatthiasgrimm    switch(substr($word,0,1)){
3295ef370d2Smatthiasgrimm      case '-':
330396b7edbSmatthiasgrimm        if(strlen($word) > 1){  // catch single '-'
3315ef370d2Smatthiasgrimm          array_push($negwords,preg_quote(substr($word,1),'#'));
332396b7edbSmatthiasgrimm        }
3335ef370d2Smatthiasgrimm        break;
3345ef370d2Smatthiasgrimm      case '+':
335396b7edbSmatthiasgrimm        if(strlen($word) > 1){  // catch single '+'
3365ef370d2Smatthiasgrimm          array_push($poswords,preg_quote(substr($word,1),'#'));
337396b7edbSmatthiasgrimm        }
3385ef370d2Smatthiasgrimm        break;
3395ef370d2Smatthiasgrimm      default:
3405ef370d2Smatthiasgrimm        array_push($poswords,preg_quote($word,'#'));
3415ef370d2Smatthiasgrimm        break;
3425ef370d2Smatthiasgrimm    }
3435ef370d2Smatthiasgrimm  }
344248a7321Smatthiasgrimm
345248a7321Smatthiasgrimm  // a search without any posword is useless
346248a7321Smatthiasgrimm  if (!count($poswords)) return true;
3475ef370d2Smatthiasgrimm
3485a5d942dSmatthiasgrimm  $reg  = '^(?=.*?'.join(')(?=.*?',$poswords).')';
3495ef370d2Smatthiasgrimm  $reg .= count($negwords) ? '((?!'.join('|',$negwords).').)*$' : '.*$';
350b59a406bSmatthiasgrimm  search_regex($data,$base,$file,$reg,$poswords);
351b59a406bSmatthiasgrimm  return true;
352b59a406bSmatthiasgrimm}
353b59a406bSmatthiasgrimm
354b59a406bSmatthiasgrimm/**
355b59a406bSmatthiasgrimm * Reference search
356b59a406bSmatthiasgrimm * This fuction searches for existing references to a given media file
357b59a406bSmatthiasgrimm * and returns an array with the found pages. It doesn't pay any
358b59a406bSmatthiasgrimm * attention to ACL permissions to find every reference. The caller
359b59a406bSmatthiasgrimm * must check if the user has the appropriate rights to see the found
360b59a406bSmatthiasgrimm * page and eventually have to prevent the result from displaying.
361b59a406bSmatthiasgrimm *
362b59a406bSmatthiasgrimm * @param array  $data Reference to the result data structure
363b59a406bSmatthiasgrimm * @param string $base Base usually $conf['datadir']
364b59a406bSmatthiasgrimm * @param string $file current file or directory relative to $base
365b59a406bSmatthiasgrimm * @param char   $type Type either 'd' for directory or 'f' for file
366b59a406bSmatthiasgrimm * @param int    $lvl  Current recursion depht
367b59a406bSmatthiasgrimm * @param mixed  $opts option array as given to search()
368b59a406bSmatthiasgrimm *
369b59a406bSmatthiasgrimm * $opts['query'] is the demanded media file name
370b59a406bSmatthiasgrimm *
371b59a406bSmatthiasgrimm * @author  Andreas Gohr <andi@splitbrain.org>
372b59a406bSmatthiasgrimm * @author  Matthias Grimm <matthiasgrimm@users.sourceforge.net>
373b59a406bSmatthiasgrimm */
374b59a406bSmatthiasgrimmfunction search_reference(&$data,$base,$file,$type,$lvl,$opts){
375b59a406bSmatthiasgrimm  global $conf;
376b59a406bSmatthiasgrimm
377b59a406bSmatthiasgrimm  //we do nothing with directories
378b59a406bSmatthiasgrimm  if($type == 'd') return true;
379b59a406bSmatthiasgrimm
380b59a406bSmatthiasgrimm  //only search txt files
381b59a406bSmatthiasgrimm  if(!preg_match('#\.txt$#',$file)) return true;
382b59a406bSmatthiasgrimm
383e28299ccSmatthiasgrimm  //we finish after 'cnt' references found. The return value
384b59a406bSmatthiasgrimm  //'false' will skip subdirectories to speed search up.
385e28299ccSmatthiasgrimm  $cnt = $conf['refshow'] > 0 ? $conf['refshow'] : 1;
386e28299ccSmatthiasgrimm  if(count($data) >= $cnt) return false;
387b59a406bSmatthiasgrimm
388d67ca2c0Smatthiasgrimm  $reg = '\{\{ *\:?'.$opts['query'].' *(\|.*)?\}\}';
389b59a406bSmatthiasgrimm  search_regex($data,$base,$file,$reg,array($opts['query']));
390b59a406bSmatthiasgrimm  return true;
391b59a406bSmatthiasgrimm}
392b59a406bSmatthiasgrimm
393b59a406bSmatthiasgrimm/* ------------- helper functions below -------------- */
394b59a406bSmatthiasgrimm
395b59a406bSmatthiasgrimm/**
396b59a406bSmatthiasgrimm * fulltext search helper
397b59a406bSmatthiasgrimm * searches a text file with a given regular expression
398b59a406bSmatthiasgrimm * no ACL checks are performed. This have to be done by
399b59a406bSmatthiasgrimm * the caller if necessary.
400b59a406bSmatthiasgrimm *
401b59a406bSmatthiasgrimm * @param array  $data  reference to array for results
402b59a406bSmatthiasgrimm * @param string $base  base directory
403b59a406bSmatthiasgrimm * @param string $file  file name to search in
404b59a406bSmatthiasgrimm * @param string $reg   regular expression to search for
405b59a406bSmatthiasgrimm * @param array  $words words that should be marked in the results
406b59a406bSmatthiasgrimm *
407b59a406bSmatthiasgrimm * @author  Andreas Gohr <andi@splitbrain.org>
408b59a406bSmatthiasgrimm * @author  Matthias Grimm <matthiasgrimm@users.sourceforge.net>
409506fa893SAndreas Gohr *
410506fa893SAndreas Gohr * @deprecated - fulltext indexer is used instead
411b59a406bSmatthiasgrimm */
412b59a406bSmatthiasgrimmfunction search_regex(&$data,$base,$file,$reg,$words){
413b59a406bSmatthiasgrimm
414b59a406bSmatthiasgrimm  //get text
415b59a406bSmatthiasgrimm  $text = io_readfile($base.'/'.$file);
416b59a406bSmatthiasgrimm  //lowercase text (u modifier does not help with case)
417b59a406bSmatthiasgrimm  $lctext = utf8_strtolower($text);
418f3f0262cSandi
419f3f0262cSandi  //do the fulltext search
420f3f0262cSandi  $matches = array();
4215ef370d2Smatthiasgrimm  if($cnt = preg_match_all('#'.$reg.'#usi',$lctext,$matches)){
422f3f0262cSandi    //this is not the best way for snippet generation but the fastest I could find
423b59a406bSmatthiasgrimm    $q = $words[0];  //use first word for snippet creation
424d5a2a500Sandi    $p = utf8_strpos($lctext,$q);
425f3f0262cSandi    $f = $p - 100;
426d5a2a500Sandi    $l = utf8_strlen($q) + 200;
427f3f0262cSandi    if($f < 0) $f = 0;
428f3f0262cSandi    $snippet = '<span class="search_sep"> ... </span>'.
429d5a2a500Sandi               htmlspecialchars(utf8_substr($text,$f,$l)).
430f3f0262cSandi               '<span class="search_sep"> ... </span>';
431b59a406bSmatthiasgrimm    $mark    = '('.join('|', $words).')';
4325ef370d2Smatthiasgrimm    $snippet = preg_replace('#'.$mark.'#si','<span class="search_hit">\\1</span>',$snippet);
433f3f0262cSandi
434f3f0262cSandi    $data[] = array(
435b59a406bSmatthiasgrimm      'id'       => pathID($file),
4365ef370d2Smatthiasgrimm      'count'    => preg_match_all('#'.$mark.'#usi',$lctext,$matches),
437b59a406bSmatthiasgrimm      'poswords' => join(' ',$words),
438f3f0262cSandi      'snippet'  => $snippet,
439f3f0262cSandi    );
440f3f0262cSandi  }
441f3f0262cSandi
442f3f0262cSandi  return true;
443f3f0262cSandi}
444f3f0262cSandi
445b59a406bSmatthiasgrimm
446f3f0262cSandi/**
44715fae107Sandi * fulltext sort
44815fae107Sandi *
449f3f0262cSandi * Callback sort function for use with usort to sort the data
450f3f0262cSandi * structure created by search_fulltext. Sorts descending by count
45115fae107Sandi *
45215fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
453f3f0262cSandi */
454f3f0262cSandifunction sort_search_fulltext($a,$b){
455f3f0262cSandi  if($a['count'] > $b['count']){
456f3f0262cSandi    return -1;
457f3f0262cSandi  }elseif($a['count'] < $b['count']){
458f3f0262cSandi    return 1;
459f3f0262cSandi  }else{
460f3f0262cSandi    return strcmp($a['id'],$b['id']);
461f3f0262cSandi  }
462f3f0262cSandi}
463f3f0262cSandi
464f3f0262cSandi/**
465f3f0262cSandi * translates a document path to an ID
46615fae107Sandi *
46715fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
46837e34a5eSandi * @todo    move to pageutils
469f3f0262cSandi */
470156a608cSandifunction pathID($path,$keeptxt=false){
47149c713a3Sandi  $id = utf8_decodeFN($path);
47249c713a3Sandi  $id = str_replace('/',':',$id);
473156a608cSandi  if(!$keeptxt) $id = preg_replace('#\.txt$#','',$id);
474f3f0262cSandi  $id = preg_replace('#^:+#','',$id);
475f3f0262cSandi  $id = preg_replace('#:+$#','',$id);
476f3f0262cSandi  return $id;
477f3f0262cSandi}
478f3f0262cSandi
479340756e4Sandi
480340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
481