xref: /dokuwiki/inc/search.php (revision 58b6f612a14a40fc14fffa363d6f272b36cd9f30)
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  //check ACL
129f3f0262cSandi  $id = pathID($file);
130f3f0262cSandi  if($type=='f' && auth_quickaclcheck($id) < AUTH_READ){
131f3f0262cSandi    return false;
132f3f0262cSandi  }
133f3f0262cSandi
134f3f0262cSandi  $data[]=array( 'id'    => $id,
135f3f0262cSandi                 'type'  => $type,
136cb70c441Sandi                 'level' => $lvl,
137cb70c441Sandi                 'open'  => $return );
138f3f0262cSandi  return $return;
139f3f0262cSandi}
140f3f0262cSandi
141f3f0262cSandi/**
14215fae107Sandi * List all namespaces
14315fae107Sandi *
14415fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
145f3f0262cSandi */
146f3f0262cSandifunction search_namespaces(&$data,$base,$file,$type,$lvl,$opts){
147f3f0262cSandi  if($type == 'f') return true; //nothing to do on files
148f3f0262cSandi
149f3f0262cSandi  $id = pathID($file);
150f3f0262cSandi  $data[]=array( 'id'    => $id,
151f3f0262cSandi                 'type'  => $type,
152f3f0262cSandi                 'level' => $lvl );
153f3f0262cSandi  return true;
154f3f0262cSandi}
155f3f0262cSandi
156f3f0262cSandi/**
15715fae107Sandi * List all mediafiles in a namespace
15815fae107Sandi *
15915fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
160f3f0262cSandi */
161f3f0262cSandifunction search_media(&$data,$base,$file,$type,$lvl,$opts){
162f3f0262cSandi  //we do nothing with directories
163f3f0262cSandi  if($type == 'd') return false;
164f3f0262cSandi
165f3f0262cSandi  $info         = array();
166156a608cSandi  $info['id']   = pathID($file,true);
167f3f0262cSandi
168f3f0262cSandi  //check ACL for namespace (we have no ACL for mediafiles)
169f3f0262cSandi  if(auth_quickaclcheck(getNS($info['id']).':*') < AUTH_READ){
170f3f0262cSandi    return false;
171f3f0262cSandi  }
172f3f0262cSandi
173f3f0262cSandi  $info['file'] = basename($file);
174f3f0262cSandi  $info['size'] = filesize($base.'/'.$file);
175f3f0262cSandi  if(preg_match("/\.(jpe?g|gif|png)$/",$file)){
176f3f0262cSandi    $info['isimg'] = true;
17723a34783SAndreas Gohr    require_once(DOKU_INC.'inc/JpegMeta.php');
17823a34783SAndreas Gohr    $info['meta']  = new JpegMeta($base.'/'.$file);
179f3f0262cSandi  }else{
180f3f0262cSandi    $info['isimg'] = false;
181f3f0262cSandi  }
182f3f0262cSandi  $data[] = $info;
183f3f0262cSandi
184f3f0262cSandi  return false;
185f3f0262cSandi}
186f3f0262cSandi
187f3f0262cSandi/**
188f3f0262cSandi * This function just lists documents (for RSS namespace export)
18915fae107Sandi *
19015fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
191f3f0262cSandi */
192f3f0262cSandifunction search_list(&$data,$base,$file,$type,$lvl,$opts){
193f3f0262cSandi  //we do nothing with directories
194f3f0262cSandi  if($type == 'd') return false;
195f3f0262cSandi  if(preg_match('#\.txt$#',$file)){
196f3f0262cSandi    //check ACL
197f3f0262cSandi    $id = pathID($file);
198f3f0262cSandi    if(auth_quickaclcheck($id) < AUTH_READ){
199f3f0262cSandi      return false;
200f3f0262cSandi    }
201f3f0262cSandi    $data[]['id'] = $id;;
202f3f0262cSandi  }
203f3f0262cSandi  return false;
204f3f0262cSandi}
205f3f0262cSandi
206f3f0262cSandi/**
207f3f0262cSandi * Quicksearch for searching matching pagenames
208f3f0262cSandi *
209f3f0262cSandi * $opts['query'] is the search query
21015fae107Sandi *
21115fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
212f3f0262cSandi */
213f3f0262cSandifunction search_pagename(&$data,$base,$file,$type,$lvl,$opts){
214f3f0262cSandi  //we do nothing with directories
215f3f0262cSandi  if($type == 'd') return true;
216f3f0262cSandi  //only search txt files
217f3f0262cSandi  if(!preg_match('#\.txt$#',$file)) return true;
218f3f0262cSandi
219f3f0262cSandi  //simple stringmatching
220396b7edbSmatthiasgrimm  if (!empty($opts['query'])){
221f3f0262cSandi    if(strpos($file,$opts['query']) !== false){
222f3f0262cSandi      //check ACL
223f3f0262cSandi      $id = pathID($file);
224f3f0262cSandi      if(auth_quickaclcheck($id) < AUTH_READ){
225f3f0262cSandi        return false;
226f3f0262cSandi      }
227f3f0262cSandi      $data[]['id'] = $id;
228f3f0262cSandi    }
229396b7edbSmatthiasgrimm  }
230f3f0262cSandi  return true;
231f3f0262cSandi}
232f3f0262cSandi
233f3f0262cSandi/**
234*58b6f612SAndreas Gohr * Just lists all documents
235*58b6f612SAndreas Gohr *
236*58b6f612SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
237*58b6f612SAndreas Gohr */
238*58b6f612SAndreas Gohrfunction search_allpages(&$data,$base,$file,$type,$lvl,$opts){
239*58b6f612SAndreas Gohr  //we do nothing with directories
240*58b6f612SAndreas Gohr  if($type == 'd') return true;
241*58b6f612SAndreas Gohr  //only search txt files
242*58b6f612SAndreas Gohr  if(!preg_match('#\.txt$#',$file)) return true;
243*58b6f612SAndreas Gohr
244*58b6f612SAndreas Gohr  $data[]['id'] = pathID($file);
245*58b6f612SAndreas Gohr  return true;
246*58b6f612SAndreas Gohr}
247*58b6f612SAndreas Gohr
248*58b6f612SAndreas Gohr/**
249f3f0262cSandi * Search for backlinks to a given page
250f3f0262cSandi *
251f3f0262cSandi * $opts['ns']    namespace of the page
252f3f0262cSandi * $opts['name']  name of the page without namespace
25315fae107Sandi *
25415fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
255f3f0262cSandi */
256f3f0262cSandifunction search_backlinks(&$data,$base,$file,$type,$lvl,$opts){
257f3f0262cSandi  //we do nothing with directories
258f3f0262cSandi  if($type == 'd') return true;;
259f3f0262cSandi  //only search txt files
260f3f0262cSandi  if(!preg_match('#\.txt$#',$file)) return true;;
261f3f0262cSandi
262f3f0262cSandi  //absolute search id
263f3f0262cSandi  $sid = cleanID($opts['ns'].':'.$opts['name']);
264f3f0262cSandi
26537e34a5eSandi  //current id and namespace
266f3f0262cSandi  $cid = pathID($file);
267f3f0262cSandi  $cns = getNS($cid);
268f3f0262cSandi
269f3f0262cSandi  //check ACL
270f3f0262cSandi  if(auth_quickaclcheck($cid) < AUTH_READ){
271f3f0262cSandi    return false;
272f3f0262cSandi  }
273f3f0262cSandi
27437e34a5eSandi  //fetch instructions
27537e34a5eSandi  require_once(DOKU_INC.'inc/parserutils.php');
27637e34a5eSandi  $instructions = p_cached_instructions($base.$file,true);
27737e34a5eSandi  if(is_null($instructions)) return false;
278f3f0262cSandi
27937e34a5eSandi  //check all links for match
28037e34a5eSandi  foreach($instructions as $ins){
28137e34a5eSandi    if($ins[0] == 'internallink' || ($conf['camelcase'] && $ins[0] == 'camelcaselink') ){
28237e34a5eSandi      $mid = $ins[1][0];
28337e34a5eSandi      resolve_pageid($cns,$mid,$exists); //exists is not used
284f3f0262cSandi      if($mid == $sid){
28537e34a5eSandi        //we have a match - finish
286f3f0262cSandi        $data[]['id'] = $cid;
287f3f0262cSandi        break;
288f3f0262cSandi      }
289f3f0262cSandi    }
290f3f0262cSandi  }
291f3f0262cSandi
29237e34a5eSandi  return false;
29337e34a5eSandi}
29437e34a5eSandi
295f3f0262cSandi/**
296f3f0262cSandi * Fulltextsearch
297f3f0262cSandi *
298f3f0262cSandi * $opts['query'] is the search query
29915fae107Sandi *
30015fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
301506fa893SAndreas Gohr * @deprecated - fulltext indexer is used instead
302f3f0262cSandi */
303f3f0262cSandifunction search_fulltext(&$data,$base,$file,$type,$lvl,$opts){
304f3f0262cSandi  //we do nothing with directories
305f3f0262cSandi  if($type == 'd') return true;;
306f3f0262cSandi  //only search txt files
307f3f0262cSandi  if(!preg_match('#\.txt$#',$file)) return true;;
308f3f0262cSandi
309f3f0262cSandi  //check ACL
310f3f0262cSandi  $id = pathID($file);
311f3f0262cSandi  if(auth_quickaclcheck($id) < AUTH_READ){
312f3f0262cSandi    return false;
313f3f0262cSandi  }
314f3f0262cSandi
315f3f0262cSandi  //create regexp from queries
3165ef370d2Smatthiasgrimm  $poswords = array();
3175ef370d2Smatthiasgrimm  $negwords = array();
3185ef370d2Smatthiasgrimm  $qpreg = preg_split('/\s+/',$opts['query']);
3195ef370d2Smatthiasgrimm
3205ef370d2Smatthiasgrimm  foreach($qpreg as $word){
3215ef370d2Smatthiasgrimm    switch(substr($word,0,1)){
3225ef370d2Smatthiasgrimm      case '-':
323396b7edbSmatthiasgrimm        if(strlen($word) > 1){  // catch single '-'
3245ef370d2Smatthiasgrimm          array_push($negwords,preg_quote(substr($word,1),'#'));
325396b7edbSmatthiasgrimm        }
3265ef370d2Smatthiasgrimm        break;
3275ef370d2Smatthiasgrimm      case '+':
328396b7edbSmatthiasgrimm        if(strlen($word) > 1){  // catch single '+'
3295ef370d2Smatthiasgrimm          array_push($poswords,preg_quote(substr($word,1),'#'));
330396b7edbSmatthiasgrimm        }
3315ef370d2Smatthiasgrimm        break;
3325ef370d2Smatthiasgrimm      default:
3335ef370d2Smatthiasgrimm        array_push($poswords,preg_quote($word,'#'));
3345ef370d2Smatthiasgrimm        break;
3355ef370d2Smatthiasgrimm    }
3365ef370d2Smatthiasgrimm  }
337248a7321Smatthiasgrimm
338248a7321Smatthiasgrimm  // a search without any posword is useless
339248a7321Smatthiasgrimm  if (!count($poswords)) return true;
3405ef370d2Smatthiasgrimm
3415a5d942dSmatthiasgrimm  $reg  = '^(?=.*?'.join(')(?=.*?',$poswords).')';
3425ef370d2Smatthiasgrimm  $reg .= count($negwords) ? '((?!'.join('|',$negwords).').)*$' : '.*$';
343b59a406bSmatthiasgrimm  search_regex($data,$base,$file,$reg,$poswords);
344b59a406bSmatthiasgrimm  return true;
345b59a406bSmatthiasgrimm}
346b59a406bSmatthiasgrimm
347b59a406bSmatthiasgrimm/**
348b59a406bSmatthiasgrimm * Reference search
349b59a406bSmatthiasgrimm * This fuction searches for existing references to a given media file
350b59a406bSmatthiasgrimm * and returns an array with the found pages. It doesn't pay any
351b59a406bSmatthiasgrimm * attention to ACL permissions to find every reference. The caller
352b59a406bSmatthiasgrimm * must check if the user has the appropriate rights to see the found
353b59a406bSmatthiasgrimm * page and eventually have to prevent the result from displaying.
354b59a406bSmatthiasgrimm *
355b59a406bSmatthiasgrimm * @param array  $data Reference to the result data structure
356b59a406bSmatthiasgrimm * @param string $base Base usually $conf['datadir']
357b59a406bSmatthiasgrimm * @param string $file current file or directory relative to $base
358b59a406bSmatthiasgrimm * @param char   $type Type either 'd' for directory or 'f' for file
359b59a406bSmatthiasgrimm * @param int    $lvl  Current recursion depht
360b59a406bSmatthiasgrimm * @param mixed  $opts option array as given to search()
361b59a406bSmatthiasgrimm *
362b59a406bSmatthiasgrimm * $opts['query'] is the demanded media file name
363b59a406bSmatthiasgrimm *
364b59a406bSmatthiasgrimm * @author  Andreas Gohr <andi@splitbrain.org>
365b59a406bSmatthiasgrimm * @author  Matthias Grimm <matthiasgrimm@users.sourceforge.net>
366b59a406bSmatthiasgrimm */
367b59a406bSmatthiasgrimmfunction search_reference(&$data,$base,$file,$type,$lvl,$opts){
368b59a406bSmatthiasgrimm  global $conf;
369b59a406bSmatthiasgrimm
370b59a406bSmatthiasgrimm  //we do nothing with directories
371b59a406bSmatthiasgrimm  if($type == 'd') return true;
372b59a406bSmatthiasgrimm
373b59a406bSmatthiasgrimm  //only search txt files
374b59a406bSmatthiasgrimm  if(!preg_match('#\.txt$#',$file)) return true;
375b59a406bSmatthiasgrimm
376e28299ccSmatthiasgrimm  //we finish after 'cnt' references found. The return value
377b59a406bSmatthiasgrimm  //'false' will skip subdirectories to speed search up.
378e28299ccSmatthiasgrimm  $cnt = $conf['refshow'] > 0 ? $conf['refshow'] : 1;
379e28299ccSmatthiasgrimm  if(count($data) >= $cnt) return false;
380b59a406bSmatthiasgrimm
381d67ca2c0Smatthiasgrimm  $reg = '\{\{ *\:?'.$opts['query'].' *(\|.*)?\}\}';
382b59a406bSmatthiasgrimm  search_regex($data,$base,$file,$reg,array($opts['query']));
383b59a406bSmatthiasgrimm  return true;
384b59a406bSmatthiasgrimm}
385b59a406bSmatthiasgrimm
386b59a406bSmatthiasgrimm/* ------------- helper functions below -------------- */
387b59a406bSmatthiasgrimm
388b59a406bSmatthiasgrimm/**
389b59a406bSmatthiasgrimm * fulltext search helper
390b59a406bSmatthiasgrimm * searches a text file with a given regular expression
391b59a406bSmatthiasgrimm * no ACL checks are performed. This have to be done by
392b59a406bSmatthiasgrimm * the caller if necessary.
393b59a406bSmatthiasgrimm *
394b59a406bSmatthiasgrimm * @param array  $data  reference to array for results
395b59a406bSmatthiasgrimm * @param string $base  base directory
396b59a406bSmatthiasgrimm * @param string $file  file name to search in
397b59a406bSmatthiasgrimm * @param string $reg   regular expression to search for
398b59a406bSmatthiasgrimm * @param array  $words words that should be marked in the results
399b59a406bSmatthiasgrimm *
400b59a406bSmatthiasgrimm * @author  Andreas Gohr <andi@splitbrain.org>
401b59a406bSmatthiasgrimm * @author  Matthias Grimm <matthiasgrimm@users.sourceforge.net>
402506fa893SAndreas Gohr *
403506fa893SAndreas Gohr * @deprecated - fulltext indexer is used instead
404b59a406bSmatthiasgrimm */
405b59a406bSmatthiasgrimmfunction search_regex(&$data,$base,$file,$reg,$words){
406b59a406bSmatthiasgrimm
407b59a406bSmatthiasgrimm  //get text
408b59a406bSmatthiasgrimm  $text = io_readfile($base.'/'.$file);
409b59a406bSmatthiasgrimm  //lowercase text (u modifier does not help with case)
410b59a406bSmatthiasgrimm  $lctext = utf8_strtolower($text);
411f3f0262cSandi
412f3f0262cSandi  //do the fulltext search
413f3f0262cSandi  $matches = array();
4145ef370d2Smatthiasgrimm  if($cnt = preg_match_all('#'.$reg.'#usi',$lctext,$matches)){
415f3f0262cSandi    //this is not the best way for snippet generation but the fastest I could find
416b59a406bSmatthiasgrimm    $q = $words[0];  //use first word for snippet creation
417d5a2a500Sandi    $p = utf8_strpos($lctext,$q);
418f3f0262cSandi    $f = $p - 100;
419d5a2a500Sandi    $l = utf8_strlen($q) + 200;
420f3f0262cSandi    if($f < 0) $f = 0;
421f3f0262cSandi    $snippet = '<span class="search_sep"> ... </span>'.
422d5a2a500Sandi               htmlspecialchars(utf8_substr($text,$f,$l)).
423f3f0262cSandi               '<span class="search_sep"> ... </span>';
424b59a406bSmatthiasgrimm    $mark    = '('.join('|', $words).')';
4255ef370d2Smatthiasgrimm    $snippet = preg_replace('#'.$mark.'#si','<span class="search_hit">\\1</span>',$snippet);
426f3f0262cSandi
427f3f0262cSandi    $data[] = array(
428b59a406bSmatthiasgrimm      'id'       => pathID($file),
4295ef370d2Smatthiasgrimm      'count'    => preg_match_all('#'.$mark.'#usi',$lctext,$matches),
430b59a406bSmatthiasgrimm      'poswords' => join(' ',$words),
431f3f0262cSandi      'snippet'  => $snippet,
432f3f0262cSandi    );
433f3f0262cSandi  }
434f3f0262cSandi
435f3f0262cSandi  return true;
436f3f0262cSandi}
437f3f0262cSandi
438b59a406bSmatthiasgrimm
439f3f0262cSandi/**
44015fae107Sandi * fulltext sort
44115fae107Sandi *
442f3f0262cSandi * Callback sort function for use with usort to sort the data
443f3f0262cSandi * structure created by search_fulltext. Sorts descending by count
44415fae107Sandi *
44515fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
446f3f0262cSandi */
447f3f0262cSandifunction sort_search_fulltext($a,$b){
448f3f0262cSandi  if($a['count'] > $b['count']){
449f3f0262cSandi    return -1;
450f3f0262cSandi  }elseif($a['count'] < $b['count']){
451f3f0262cSandi    return 1;
452f3f0262cSandi  }else{
453f3f0262cSandi    return strcmp($a['id'],$b['id']);
454f3f0262cSandi  }
455f3f0262cSandi}
456f3f0262cSandi
457f3f0262cSandi/**
458f3f0262cSandi * translates a document path to an ID
45915fae107Sandi *
46015fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
46137e34a5eSandi * @todo    move to pageutils
462f3f0262cSandi */
463156a608cSandifunction pathID($path,$keeptxt=false){
46449c713a3Sandi  $id = utf8_decodeFN($path);
46549c713a3Sandi  $id = str_replace('/',':',$id);
466156a608cSandi  if(!$keeptxt) $id = preg_replace('#\.txt$#','',$id);
467f3f0262cSandi  $id = preg_replace('#^:+#','',$id);
468f3f0262cSandi  $id = preg_replace('#:+$#','',$id);
469f3f0262cSandi  return $id;
470f3f0262cSandi}
471f3f0262cSandi
472340756e4Sandi
473340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
474