xref: /dokuwiki/inc/search.php (revision ec24a2df21bc25730b6d807a6930867e09f2aa58)
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.');
10f3f0262cSandi
11f3f0262cSandi/**
1215fae107Sandi * recurse direcory
1315fae107Sandi *
14f3f0262cSandi * This function recurses into a given base directory
15f3f0262cSandi * and calls the supplied function for each file and directory
1615fae107Sandi *
1724baa045SAndreas Gohr * @param   array ref $data The results of the search are stored here
1824baa045SAndreas Gohr * @param   string    $base Where to start the search
19fe82d751SChristopher Smith * @param   callback  $func Callback (function name or array with object,method)
2024baa045SAndreas Gohr * @param   string    $dir  Current directory beyond $base
2124baa045SAndreas Gohr * @param   int       $lvl  Recursion Level
22*ec24a2dfSPhilipp A. Hartmann * @param   mixed     $sort 'natural' to use natural order sorting (default); 'date' to sort by filemtime; leave empty to skip sorting.
2315fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
24f3f0262cSandi */
25155e63c9SChristopher Smithfunction search(&$data,$base,$func,$opts,$dir='',$lvl=1,$sort='natural'){
26f3f0262cSandi    $dirs   = array();
27f3f0262cSandi    $files  = array();
28abc306f4SKate Arzamastseva    $filepaths = array();
29f3f0262cSandi
30f3f0262cSandi    //read in directories and files
31f3f0262cSandi    $dh = @opendir($base.'/'.$dir);
32f3f0262cSandi    if(!$dh) return;
33f3f0262cSandi    while(($file = readdir($dh)) !== false){
34de3dfc91Sandi        if(preg_match('/^[\._]/',$file)) continue; //skip hidden files and upper dirs
35f3f0262cSandi        if(is_dir($base.'/'.$dir.'/'.$file)){
36f3f0262cSandi            $dirs[] = $dir.'/'.$file;
37f3f0262cSandi            continue;
38f3f0262cSandi        }
39f3f0262cSandi        $files[] = $dir.'/'.$file;
40abc306f4SKate Arzamastseva        $filepaths[] = $base.'/'.$dir.'/'.$file;
41f3f0262cSandi    }
42f3f0262cSandi    closedir($dh);
43*ec24a2dfSPhilipp A. Hartmann    if (!empty($sort)) {
44abc306f4SKate Arzamastseva        if ($sort == 'date') {
45d971ea8bSKate Arzamastseva            @array_multisort(array_map('filemtime', $filepaths), SORT_NUMERIC, SORT_DESC, $files);
461dc5d48bSChristopher Smith        } else /* natural */ {
471dc5d48bSChristopher Smith            natsort($files);
48abc306f4SKate Arzamastseva        }
491dc5d48bSChristopher Smith        natsort($dirs);
50*ec24a2dfSPhilipp A. Hartmann    }
51f3f0262cSandi
52f3f0262cSandi    //give directories to userfunction then recurse
53f3f0262cSandi    foreach($dirs as $dir){
54d8126df2SGina Haeussge        if (call_user_func_array($func, array(&$data,$base,$dir,'d',$lvl,$opts))){
555514a5a7SChristopher Smith            search($data,$base,$func,$opts,$dir,$lvl+1,$sort);
56f3f0262cSandi        }
57f3f0262cSandi    }
58f3f0262cSandi    //now handle the files
59f3f0262cSandi    foreach($files as $file){
60d8126df2SGina Haeussge        call_user_func_array($func, array(&$data,$base,$file,'f',$lvl,$opts));
61f3f0262cSandi    }
62f3f0262cSandi}
63f3f0262cSandi
64f3f0262cSandi/**
65d8126df2SGina Haeussge * Wrapper around call_user_func_array.
6624baa045SAndreas Gohr *
67d8126df2SGina Haeussge * @deprecated
6824baa045SAndreas Gohr */
6924baa045SAndreas Gohrfunction search_callback($func,&$data,$base,$file,$type,$lvl,$opts){
70d8126df2SGina Haeussge    return call_user_func_array($func, array(&$data,$base,$file,$type,$lvl,$opts));
7124baa045SAndreas Gohr}
7224baa045SAndreas Gohr
7324baa045SAndreas Gohr/**
74f3f0262cSandi * The following functions are userfunctions to use with the search
75f3f0262cSandi * function above. This function is called for every found file or
76f3f0262cSandi * directory. When a directory is given to the function it has to
77f3f0262cSandi * decide if this directory should be traversed (true) or not (false)
78f3f0262cSandi * The function has to accept the following parameters:
79f3f0262cSandi *
80f3f0262cSandi * &$data - Reference to the result data structure
81f3f0262cSandi * $base  - Base usually $conf['datadir']
82f3f0262cSandi * $file  - current file or directory relative to $base
83f3f0262cSandi * $type  - Type either 'd' for directory or 'f' for file
84f3f0262cSandi * $lvl   - Current recursion depht
85f3f0262cSandi * $opts  - option array as given to search()
86f3f0262cSandi *
87f3f0262cSandi * return values for files are ignored
88f3f0262cSandi *
89f3f0262cSandi * All functions should check the ACL for document READ rights
90783d2e49SAdrian Lang * namespaces (directories) are NOT checked (when sneaky_index is 0) as this
91783d2e49SAdrian Lang * would break the recursion (You can have an nonreadable dir over a readable
920e1a261eSMichael Klier * one deeper nested) also make sure to check the file type (for example
930e1a261eSMichael Klier * in case of lockfiles).
94f3f0262cSandi */
95f3f0262cSandi
96f3f0262cSandi/**
9763f2400bSandi * Searches for pages beginning with the given query
9863f2400bSandi *
9963f2400bSandi * @author Andreas Gohr <andi@splitbrain.org>
10063f2400bSandi */
10163f2400bSandifunction search_qsearch(&$data,$base,$file,$type,$lvl,$opts){
1028705cc81SAndreas Gohr    $opts = array(
1038705cc81SAndreas Gohr            'idmatch'   => '(^|:)'.preg_quote($opts['query'],'/').'/',
1048705cc81SAndreas Gohr            'listfiles' => true,
1058705cc81SAndreas Gohr            'pagesonly' => true,
1068705cc81SAndreas Gohr            );
1078705cc81SAndreas Gohr    return search_universal($data,$base,$file,$type,$lvl,$opts);
10863f2400bSandi}
10963f2400bSandi
11063f2400bSandi/**
11115fae107Sandi * Build the browsable index of pages
112f3f0262cSandi *
113783d2e49SAdrian Lang * $opts['ns'] is the currently viewed namespace
11415fae107Sandi *
11515fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
116f3f0262cSandi */
117f3f0262cSandifunction search_index(&$data,$base,$file,$type,$lvl,$opts){
118d1c7b6ecSAndreas Gohr    global $conf;
119783d2e49SAdrian Lang    $opts = array(
120783d2e49SAdrian Lang        'pagesonly' => true,
121783d2e49SAdrian Lang        'listdirs' => true,
122783d2e49SAdrian Lang        'listfiles' => !$opts['nofiles'],
123783d2e49SAdrian Lang        'sneakyacl' => $conf['sneaky_index'],
124783d2e49SAdrian Lang        // Hacky, should rather use recmatch
1251c6c1c6cSMichael Hamann        'depth' => preg_match('#^'.preg_quote($file, '#').'(/|$)#','/'.$opts['ns']) ? 0 : -1
126783d2e49SAdrian Lang    );
127f3f0262cSandi
128783d2e49SAdrian Lang    return search_universal($data, $base, $file, $type, $lvl, $opts);
129f3f0262cSandi}
130f3f0262cSandi
131f3f0262cSandi/**
13215fae107Sandi * List all namespaces
13315fae107Sandi *
13415fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
135f3f0262cSandi */
136f3f0262cSandifunction search_namespaces(&$data,$base,$file,$type,$lvl,$opts){
1378705cc81SAndreas Gohr    $opts = array(
1388705cc81SAndreas Gohr            'listdirs' => true,
1398705cc81SAndreas Gohr            );
1408705cc81SAndreas Gohr    return search_universal($data,$base,$file,$type,$lvl,$opts);
141f3f0262cSandi}
142f3f0262cSandi
143f3f0262cSandi/**
14415fae107Sandi * List all mediafiles in a namespace
14515fae107Sandi *
14615fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
147f3f0262cSandi */
148f3f0262cSandifunction search_media(&$data,$base,$file,$type,$lvl,$opts){
149b8219d2dSAndreas Gohr
150f3f0262cSandi    //we do nothing with directories
1511a49ac65SGina Haeussge    if($type == 'd') {
152224122cfSAndreas Gohr        if(!$opts['depth']) return true; // recurse forever
15378315408SAndreas Gohr        $depth = substr_count($file,'/');
154b8219d2dSAndreas Gohr        if($depth >= $opts['depth']) return false; // depth reached
155224122cfSAndreas Gohr        return true;
1561a49ac65SGina Haeussge    }
157f3f0262cSandi
158f3f0262cSandi    $info         = array();
159156a608cSandi    $info['id']   = pathID($file,true);
16064807c84SAndreas Gohr    if($info['id'] != cleanID($info['id'])){
16164807c84SAndreas Gohr        if($opts['showmsg'])
16264807c84SAndreas Gohr            msg(hsc($info['id']).' is not a valid file name for DokuWiki - skipped',-1);
16364807c84SAndreas Gohr        return false; // skip non-valid files
16464807c84SAndreas Gohr    }
165f3f0262cSandi
166f3f0262cSandi    //check ACL for namespace (we have no ACL for mediafiles)
167224122cfSAndreas Gohr    $info['perm'] = auth_quickaclcheck(getNS($info['id']).':*');
168224122cfSAndreas Gohr    if(!$opts['skipacl'] && $info['perm'] < AUTH_READ){
169224122cfSAndreas Gohr        return false;
170224122cfSAndreas Gohr    }
171224122cfSAndreas Gohr
172224122cfSAndreas Gohr    //check pattern filter
173224122cfSAndreas Gohr    if($opts['pattern'] && !@preg_match($opts['pattern'], $info['id'])){
174f3f0262cSandi        return false;
175f3f0262cSandi    }
176f3f0262cSandi
1773009a773SAndreas Gohr    $info['file']     = utf8_basename($file);
178f3f0262cSandi    $info['size']     = filesize($base.'/'.$file);
1795e7fa82eSAndreas Gohr    $info['mtime']    = filemtime($base.'/'.$file);
1803df72098SAndreas Gohr    $info['writable'] = is_writable($base.'/'.$file);
181f3f0262cSandi    if(preg_match("/\.(jpe?g|gif|png)$/",$file)){
182f3f0262cSandi        $info['isimg'] = true;
18323a34783SAndreas Gohr        $info['meta']  = new JpegMeta($base.'/'.$file);
184f3f0262cSandi    }else{
185f3f0262cSandi        $info['isimg'] = false;
186f3f0262cSandi    }
187224122cfSAndreas Gohr    if($opts['hash']){
188dfd343c4SAndreas Gohr        $info['hash'] = md5(io_readFile(mediaFN($info['id']),false));
189224122cfSAndreas Gohr    }
190224122cfSAndreas Gohr
191f3f0262cSandi    $data[] = $info;
192f3f0262cSandi
193f3f0262cSandi    return false;
194f3f0262cSandi}
195f3f0262cSandi
196f3f0262cSandi/**
197f3f0262cSandi * This function just lists documents (for RSS namespace export)
19815fae107Sandi *
19915fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
200f3f0262cSandi */
201f3f0262cSandifunction search_list(&$data,$base,$file,$type,$lvl,$opts){
202f3f0262cSandi    //we do nothing with directories
203f3f0262cSandi    if($type == 'd') return false;
2040e1a261eSMichael Klier    //only search txt files
2050e1a261eSMichael Klier    if(substr($file,-4) == '.txt'){
206f3f0262cSandi        //check ACL
207f3f0262cSandi        $id = pathID($file);
208f3f0262cSandi        if(auth_quickaclcheck($id) < AUTH_READ){
209f3f0262cSandi            return false;
210f3f0262cSandi        }
2110e1a261eSMichael Klier        $data[]['id'] = $id;
212f3f0262cSandi    }
213f3f0262cSandi    return false;
214f3f0262cSandi}
215f3f0262cSandi
216f3f0262cSandi/**
217f3f0262cSandi * Quicksearch for searching matching pagenames
218f3f0262cSandi *
219f3f0262cSandi * $opts['query'] is the search query
22015fae107Sandi *
22115fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
222f3f0262cSandi */
223f3f0262cSandifunction search_pagename(&$data,$base,$file,$type,$lvl,$opts){
224f3f0262cSandi    //we do nothing with directories
225f3f0262cSandi    if($type == 'd') return true;
226f3f0262cSandi    //only search txt files
2270e1a261eSMichael Klier    if(substr($file,-4) != '.txt') return true;
228f3f0262cSandi
229f3f0262cSandi    //simple stringmatching
230396b7edbSmatthiasgrimm    if (!empty($opts['query'])){
231f3f0262cSandi        if(strpos($file,$opts['query']) !== false){
232f3f0262cSandi            //check ACL
233f3f0262cSandi            $id = pathID($file);
234f3f0262cSandi            if(auth_quickaclcheck($id) < AUTH_READ){
235f3f0262cSandi                return false;
236f3f0262cSandi            }
237f3f0262cSandi            $data[]['id'] = $id;
238f3f0262cSandi        }
239396b7edbSmatthiasgrimm    }
240f3f0262cSandi    return true;
241f3f0262cSandi}
242f3f0262cSandi
243f3f0262cSandi/**
24458b6f612SAndreas Gohr * Just lists all documents
24558b6f612SAndreas Gohr *
2461fcfad4dSAndreas Gohr * $opts['depth']   recursion level, 0 for all
2471fcfad4dSAndreas Gohr * $opts['hash']    do md5 sum of content?
248224122cfSAndreas Gohr * $opts['skipacl'] list everything regardless of ACL
2491fcfad4dSAndreas Gohr *
25058b6f612SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
25158b6f612SAndreas Gohr */
25258b6f612SAndreas Gohrfunction search_allpages(&$data,$base,$file,$type,$lvl,$opts){
2538451f4adSGuillaume Turri    if(isset($opts['depth']) && $opts['depth']){
254c647387eSGuillaume Turri        $parts = explode('/',ltrim($file,'/'));
255c647387eSGuillaume Turri        if(($type == 'd' && count($parts) > $opts['depth'])
256c647387eSGuillaume Turri          || ($type != 'd' && count($parts) > $opts['depth'] + 1)){
257c647387eSGuillaume Turri            return false; // depth reached
258c647387eSGuillaume Turri        }
259c647387eSGuillaume Turri    }
260c647387eSGuillaume Turri
26158b6f612SAndreas Gohr    //we do nothing with directories
2621fcfad4dSAndreas Gohr    if($type == 'd'){
2631fcfad4dSAndreas Gohr        return true;
2641fcfad4dSAndreas Gohr    }
2651fcfad4dSAndreas Gohr
26658b6f612SAndreas Gohr    //only search txt files
2670e1a261eSMichael Klier    if(substr($file,-4) != '.txt') return true;
26858b6f612SAndreas Gohr
2691fcfad4dSAndreas Gohr    $item['id']   = pathID($file);
270061df79cSAndreas Gohr    if(!$opts['skipacl'] && auth_quickaclcheck($item['id']) < AUTH_READ){
2711fcfad4dSAndreas Gohr        return false;
2721fcfad4dSAndreas Gohr    }
2731fcfad4dSAndreas Gohr
2741fcfad4dSAndreas Gohr    $item['rev']   = filemtime($base.'/'.$file);
275224122cfSAndreas Gohr    $item['mtime'] = $item['rev'];
2761fcfad4dSAndreas Gohr    $item['size']  = filesize($base.'/'.$file);
2771fcfad4dSAndreas Gohr    if($opts['hash']){
2781fcfad4dSAndreas Gohr        $item['hash'] = md5(trim(rawWiki($item['id'])));
2791fcfad4dSAndreas Gohr    }
2801fcfad4dSAndreas Gohr
2811fcfad4dSAndreas Gohr    $data[] = $item;
28258b6f612SAndreas Gohr    return true;
28358b6f612SAndreas Gohr}
28458b6f612SAndreas Gohr
28558b6f612SAndreas Gohr/**
286f3f0262cSandi * Search for backlinks to a given page
287f3f0262cSandi *
288f3f0262cSandi * $opts['ns']    namespace of the page
289f3f0262cSandi * $opts['name']  name of the page without namespace
29015fae107Sandi *
29115fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
29254f4c056SAndreas Gohr * @deprecated Replaced by ft_backlinks()
293f3f0262cSandi */
294f3f0262cSandifunction search_backlinks(&$data,$base,$file,$type,$lvl,$opts){
295f3f0262cSandi    //we do nothing with directories
2960e1a261eSMichael Klier    if($type == 'd') return true;
297f3f0262cSandi    //only search txt files
2980e1a261eSMichael Klier    if(substr($file,-4) != '.txt') return true;
299f3f0262cSandi
300f3f0262cSandi    //absolute search id
301f3f0262cSandi    $sid = cleanID($opts['ns'].':'.$opts['name']);
302f3f0262cSandi
30337e34a5eSandi    //current id and namespace
304f3f0262cSandi    $cid = pathID($file);
305f3f0262cSandi    $cns = getNS($cid);
306f3f0262cSandi
307f3f0262cSandi    //check ACL
308f3f0262cSandi    if(auth_quickaclcheck($cid) < AUTH_READ){
309f3f0262cSandi        return false;
310f3f0262cSandi    }
311f3f0262cSandi
31237e34a5eSandi    //fetch instructions
31337e34a5eSandi    $instructions = p_cached_instructions($base.$file,true);
31437e34a5eSandi    if(is_null($instructions)) return false;
315f3f0262cSandi
316de3eb1d7SAdrian Lang    global $conf;
31737e34a5eSandi    //check all links for match
31837e34a5eSandi    foreach($instructions as $ins){
31937e34a5eSandi        if($ins[0] == 'internallink' || ($conf['camelcase'] && $ins[0] == 'camelcaselink') ){
32037e34a5eSandi            $mid = $ins[1][0];
32137e34a5eSandi            resolve_pageid($cns,$mid,$exists); //exists is not used
322f3f0262cSandi            if($mid == $sid){
32337e34a5eSandi                //we have a match - finish
324f3f0262cSandi                $data[]['id'] = $cid;
325f3f0262cSandi                break;
326f3f0262cSandi            }
327f3f0262cSandi        }
328f3f0262cSandi    }
329f3f0262cSandi
33037e34a5eSandi    return false;
33137e34a5eSandi}
33237e34a5eSandi
333f3f0262cSandi/**
334f3f0262cSandi * Fulltextsearch
335f3f0262cSandi *
336f3f0262cSandi * $opts['query'] is the search query
33715fae107Sandi *
33815fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
339506fa893SAndreas Gohr * @deprecated - fulltext indexer is used instead
340f3f0262cSandi */
341f3f0262cSandifunction search_fulltext(&$data,$base,$file,$type,$lvl,$opts){
342f3f0262cSandi    //we do nothing with directories
3430e1a261eSMichael Klier    if($type == 'd') return true;
344f3f0262cSandi    //only search txt files
3450e1a261eSMichael Klier    if(substr($file,-4) != '.txt') return true;
346f3f0262cSandi
347f3f0262cSandi    //check ACL
348f3f0262cSandi    $id = pathID($file);
349f3f0262cSandi    if(auth_quickaclcheck($id) < AUTH_READ){
350f3f0262cSandi        return false;
351f3f0262cSandi    }
352f3f0262cSandi
353f3f0262cSandi    //create regexp from queries
3545ef370d2Smatthiasgrimm    $poswords = array();
3555ef370d2Smatthiasgrimm    $negwords = array();
3565ef370d2Smatthiasgrimm    $qpreg = preg_split('/\s+/',$opts['query']);
3575ef370d2Smatthiasgrimm
3585ef370d2Smatthiasgrimm    foreach($qpreg as $word){
3595ef370d2Smatthiasgrimm        switch(substr($word,0,1)){
3605ef370d2Smatthiasgrimm            case '-':
361396b7edbSmatthiasgrimm                if(strlen($word) > 1){  // catch single '-'
3625ef370d2Smatthiasgrimm                    array_push($negwords,preg_quote(substr($word,1),'#'));
363396b7edbSmatthiasgrimm                }
3645ef370d2Smatthiasgrimm                break;
3655ef370d2Smatthiasgrimm            case '+':
366396b7edbSmatthiasgrimm                if(strlen($word) > 1){  // catch single '+'
3675ef370d2Smatthiasgrimm                    array_push($poswords,preg_quote(substr($word,1),'#'));
368396b7edbSmatthiasgrimm                }
3695ef370d2Smatthiasgrimm                break;
3705ef370d2Smatthiasgrimm            default:
3715ef370d2Smatthiasgrimm                array_push($poswords,preg_quote($word,'#'));
3725ef370d2Smatthiasgrimm                break;
3735ef370d2Smatthiasgrimm        }
3745ef370d2Smatthiasgrimm    }
375248a7321Smatthiasgrimm
376248a7321Smatthiasgrimm    // a search without any posword is useless
377248a7321Smatthiasgrimm    if (!count($poswords)) return true;
3785ef370d2Smatthiasgrimm
3795a5d942dSmatthiasgrimm    $reg  = '^(?=.*?'.join(')(?=.*?',$poswords).')';
3805ef370d2Smatthiasgrimm            $reg .= count($negwords) ? '((?!'.join('|',$negwords).').)*$' : '.*$';
381b59a406bSmatthiasgrimm            search_regex($data,$base,$file,$reg,$poswords);
382b59a406bSmatthiasgrimm            return true;
383b59a406bSmatthiasgrimm            }
384b59a406bSmatthiasgrimm
385b59a406bSmatthiasgrimm            /**
386b59a406bSmatthiasgrimm             * Reference search
387b59a406bSmatthiasgrimm             * This fuction searches for existing references to a given media file
388b59a406bSmatthiasgrimm             * and returns an array with the found pages. It doesn't pay any
389b59a406bSmatthiasgrimm             * attention to ACL permissions to find every reference. The caller
390b59a406bSmatthiasgrimm             * must check if the user has the appropriate rights to see the found
391b59a406bSmatthiasgrimm             * page and eventually have to prevent the result from displaying.
392b59a406bSmatthiasgrimm             *
393b59a406bSmatthiasgrimm             * @param array  $data Reference to the result data structure
394b59a406bSmatthiasgrimm             * @param string $base Base usually $conf['datadir']
395b59a406bSmatthiasgrimm             * @param string $file current file or directory relative to $base
396b59a406bSmatthiasgrimm             * @param char   $type Type either 'd' for directory or 'f' for file
397b59a406bSmatthiasgrimm             * @param int    $lvl  Current recursion depht
398b59a406bSmatthiasgrimm             * @param mixed  $opts option array as given to search()
399b59a406bSmatthiasgrimm             *
400b59a406bSmatthiasgrimm             * $opts['query'] is the demanded media file name
401b59a406bSmatthiasgrimm             *
402b59a406bSmatthiasgrimm             * @author  Andreas Gohr <andi@splitbrain.org>
403b59a406bSmatthiasgrimm             * @author  Matthias Grimm <matthiasgrimm@users.sourceforge.net>
404b59a406bSmatthiasgrimm             */
405b59a406bSmatthiasgrimmfunction search_reference(&$data,$base,$file,$type,$lvl,$opts){
406b59a406bSmatthiasgrimm    global $conf;
407b59a406bSmatthiasgrimm
408b59a406bSmatthiasgrimm    //we do nothing with directories
409b59a406bSmatthiasgrimm    if($type == 'd') return true;
410b59a406bSmatthiasgrimm
411b59a406bSmatthiasgrimm    //only search txt files
4120e1a261eSMichael Klier    if(substr($file,-4) != '.txt') return true;
413b59a406bSmatthiasgrimm
414e28299ccSmatthiasgrimm    //we finish after 'cnt' references found. The return value
415b59a406bSmatthiasgrimm    //'false' will skip subdirectories to speed search up.
416e28299ccSmatthiasgrimm    $cnt = $conf['refshow'] > 0 ? $conf['refshow'] : 1;
417e28299ccSmatthiasgrimm    if(count($data) >= $cnt) return false;
418b59a406bSmatthiasgrimm
419d67ca2c0Smatthiasgrimm    $reg = '\{\{ *\:?'.$opts['query'].' *(\|.*)?\}\}';
420b59a406bSmatthiasgrimm    search_regex($data,$base,$file,$reg,array($opts['query']));
421b59a406bSmatthiasgrimm    return true;
422b59a406bSmatthiasgrimm}
423b59a406bSmatthiasgrimm
424b59a406bSmatthiasgrimm/* ------------- helper functions below -------------- */
425b59a406bSmatthiasgrimm
426b59a406bSmatthiasgrimm/**
427b59a406bSmatthiasgrimm * fulltext search helper
428b59a406bSmatthiasgrimm * searches a text file with a given regular expression
429b59a406bSmatthiasgrimm * no ACL checks are performed. This have to be done by
430b59a406bSmatthiasgrimm * the caller if necessary.
431b59a406bSmatthiasgrimm *
432b59a406bSmatthiasgrimm * @param array  $data  reference to array for results
433b59a406bSmatthiasgrimm * @param string $base  base directory
434b59a406bSmatthiasgrimm * @param string $file  file name to search in
435b59a406bSmatthiasgrimm * @param string $reg   regular expression to search for
436b59a406bSmatthiasgrimm * @param array  $words words that should be marked in the results
437b59a406bSmatthiasgrimm *
438b59a406bSmatthiasgrimm * @author  Andreas Gohr <andi@splitbrain.org>
439b59a406bSmatthiasgrimm * @author  Matthias Grimm <matthiasgrimm@users.sourceforge.net>
440506fa893SAndreas Gohr *
441506fa893SAndreas Gohr * @deprecated - fulltext indexer is used instead
442b59a406bSmatthiasgrimm */
443b59a406bSmatthiasgrimmfunction search_regex(&$data,$base,$file,$reg,$words){
444b59a406bSmatthiasgrimm
445b59a406bSmatthiasgrimm    //get text
446b59a406bSmatthiasgrimm    $text = io_readfile($base.'/'.$file);
447b59a406bSmatthiasgrimm    //lowercase text (u modifier does not help with case)
448b59a406bSmatthiasgrimm    $lctext = utf8_strtolower($text);
449f3f0262cSandi
450f3f0262cSandi    //do the fulltext search
451f3f0262cSandi    $matches = array();
4525ef370d2Smatthiasgrimm    if($cnt = preg_match_all('#'.$reg.'#usi',$lctext,$matches)){
453f3f0262cSandi        //this is not the best way for snippet generation but the fastest I could find
454b59a406bSmatthiasgrimm        $q = $words[0];  //use first word for snippet creation
455d5a2a500Sandi        $p = utf8_strpos($lctext,$q);
456f3f0262cSandi        $f = $p - 100;
457d5a2a500Sandi        $l = utf8_strlen($q) + 200;
458f3f0262cSandi        if($f < 0) $f = 0;
459f3f0262cSandi        $snippet = '<span class="search_sep"> ... </span>'.
460d5a2a500Sandi            htmlspecialchars(utf8_substr($text,$f,$l)).
461f3f0262cSandi            '<span class="search_sep"> ... </span>';
462b59a406bSmatthiasgrimm        $mark    = '('.join('|', $words).')';
463ed7ecb79SAnika Henke        $snippet = preg_replace('#'.$mark.'#si','<strong class="search_hit">\\1</strong>',$snippet);
464f3f0262cSandi
465f3f0262cSandi        $data[] = array(
466b59a406bSmatthiasgrimm                'id'       => pathID($file),
4675ef370d2Smatthiasgrimm                'count'    => preg_match_all('#'.$mark.'#usi',$lctext,$matches),
468b59a406bSmatthiasgrimm                'poswords' => join(' ',$words),
469f3f0262cSandi                'snippet'  => $snippet,
470f3f0262cSandi                );
471f3f0262cSandi    }
472f3f0262cSandi
473f3f0262cSandi    return true;
474f3f0262cSandi}
475f3f0262cSandi
476b59a406bSmatthiasgrimm
477f3f0262cSandi/**
47815fae107Sandi * fulltext sort
47915fae107Sandi *
480f3f0262cSandi * Callback sort function for use with usort to sort the data
481f3f0262cSandi * structure created by search_fulltext. Sorts descending by count
48215fae107Sandi *
48315fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
484f3f0262cSandi */
485f3f0262cSandifunction sort_search_fulltext($a,$b){
486f3f0262cSandi    if($a['count'] > $b['count']){
487f3f0262cSandi        return -1;
488f3f0262cSandi    }elseif($a['count'] < $b['count']){
489f3f0262cSandi        return 1;
490f3f0262cSandi    }else{
491f3f0262cSandi        return strcmp($a['id'],$b['id']);
492f3f0262cSandi    }
493f3f0262cSandi}
494f3f0262cSandi
495f3f0262cSandi/**
496f3f0262cSandi * translates a document path to an ID
49715fae107Sandi *
49815fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
49937e34a5eSandi * @todo    move to pageutils
500f3f0262cSandi */
501156a608cSandifunction pathID($path,$keeptxt=false){
50249c713a3Sandi    $id = utf8_decodeFN($path);
50349c713a3Sandi    $id = str_replace('/',':',$id);
504156a608cSandi    if(!$keeptxt) $id = preg_replace('#\.txt$#','',$id);
505709b1063SAdrian Lang    $id = trim($id, ':');
506f3f0262cSandi    return $id;
507f3f0262cSandi}
508f3f0262cSandi
509340756e4Sandi
5103abeade3SAndreas Gohr/**
5113abeade3SAndreas Gohr * This is a very universal callback for the search() function, replacing
5123abeade3SAndreas Gohr * many of the former individual functions at the cost of a more complex
5133abeade3SAndreas Gohr * setup.
5143abeade3SAndreas Gohr *
5153abeade3SAndreas Gohr * How the function behaves, depends on the options passed in the $opts
5163abeade3SAndreas Gohr * array, where the following settings can be used.
5173abeade3SAndreas Gohr *
5183abeade3SAndreas Gohr * depth      int     recursion depth. 0 for unlimited
5193abeade3SAndreas Gohr * keeptxt    bool    keep .txt extension for IDs
5203abeade3SAndreas Gohr * listfiles  bool    include files in listing
5213abeade3SAndreas Gohr * listdirs   bool    include namespaces in listing
5223abeade3SAndreas Gohr * pagesonly  bool    restrict files to pages
5233abeade3SAndreas Gohr * skipacl    bool    do not check for READ permission
5243abeade3SAndreas Gohr * sneakyacl  bool    don't recurse into nonreadable dirs
5253abeade3SAndreas Gohr * hash       bool    create MD5 hash for files
5263abeade3SAndreas Gohr * meta       bool    return file metadata
5273abeade3SAndreas Gohr * filematch  string  match files against this regexp
5288705cc81SAndreas Gohr * idmatch    string  match full ID against this regexp
5298705cc81SAndreas Gohr * dirmatch   string  match directory against this regexp when adding
5308705cc81SAndreas Gohr * nsmatch    string  match namespace against this regexp when adding
5318705cc81SAndreas Gohr * recmatch   string  match directory against this regexp when recursing
5323abeade3SAndreas Gohr * showmsg    bool    warn about non-ID files
5333abeade3SAndreas Gohr * showhidden bool    show hidden files too
5343abeade3SAndreas Gohr * firsthead  bool    return first heading for pages
5353abeade3SAndreas Gohr *
5363abeade3SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
5373abeade3SAndreas Gohr */
5383abeade3SAndreas Gohrfunction search_universal(&$data,$base,$file,$type,$lvl,$opts){
5393abeade3SAndreas Gohr    $item   = array();
5403abeade3SAndreas Gohr    $return = true;
5413abeade3SAndreas Gohr
5423abeade3SAndreas Gohr    // get ID and check if it is a valid one
543e63d421bSAndreas Gohr    $item['id'] = pathID($file,($type == 'd' || $opts['keeptxt']));
5448537abd1SAdrian Lang    if($item['id'] != cleanID($item['id'])){
5453abeade3SAndreas Gohr        if($opts['showmsg'])
5468537abd1SAdrian Lang            msg(hsc($item['id']).' is not a valid file name for DokuWiki - skipped',-1);
5473abeade3SAndreas Gohr        return false; // skip non-valid files
5483abeade3SAndreas Gohr    }
5498705cc81SAndreas Gohr    $item['ns']  = getNS($item['id']);
5503abeade3SAndreas Gohr
5513abeade3SAndreas Gohr    if($type == 'd') {
5523abeade3SAndreas Gohr        // decide if to recursion into this directory is wanted
5533abeade3SAndreas Gohr        if(!$opts['depth']){
5543abeade3SAndreas Gohr            $return = true; // recurse forever
5553abeade3SAndreas Gohr        }else{
5563abeade3SAndreas Gohr            $depth = substr_count($file,'/');
5573abeade3SAndreas Gohr            if($depth >= $opts['depth']){
5583abeade3SAndreas Gohr                $return = false; // depth reached
5593abeade3SAndreas Gohr            }else{
5603abeade3SAndreas Gohr                $return = true;
5613abeade3SAndreas Gohr            }
5623abeade3SAndreas Gohr        }
5633abeade3SAndreas Gohr        if($return && !preg_match('/'.$opts['recmatch'].'/',$file)){
5643abeade3SAndreas Gohr            $return = false; // doesn't match
5653abeade3SAndreas Gohr        }
5663abeade3SAndreas Gohr    }
5673abeade3SAndreas Gohr
5683abeade3SAndreas Gohr    // check ACL
5693abeade3SAndreas Gohr    if(!$opts['skipacl']){
5703abeade3SAndreas Gohr        if($type == 'd'){
5713abeade3SAndreas Gohr            $item['perm'] = auth_quickaclcheck($item['id'].':*');
5723abeade3SAndreas Gohr        }else{
5733abeade3SAndreas Gohr            $item['perm'] = auth_quickaclcheck($item['id']); //FIXME check namespace for media files
5743abeade3SAndreas Gohr        }
5753abeade3SAndreas Gohr    }else{
5763abeade3SAndreas Gohr        $item['perm'] = AUTH_DELETE;
5773abeade3SAndreas Gohr    }
5783abeade3SAndreas Gohr
5793abeade3SAndreas Gohr    // are we done here maybe?
5803abeade3SAndreas Gohr    if($type == 'd'){
5813abeade3SAndreas Gohr        if(!$opts['listdirs']) return $return;
5823abeade3SAndreas Gohr        if(!$opts['skipacl'] && $opts['sneakyacl'] && $item['perm'] < AUTH_READ) return false; //neither list nor recurse
5833abeade3SAndreas Gohr        if($opts['dirmatch'] && !preg_match('/'.$opts['dirmatch'].'/',$file)) return $return;
5848705cc81SAndreas Gohr        if($opts['nsmatch'] && !preg_match('/'.$opts['nsmatch'].'/',$item['ns'])) return $return;
5853abeade3SAndreas Gohr    }else{
5863abeade3SAndreas Gohr        if(!$opts['listfiles']) return $return;
5873abeade3SAndreas Gohr        if(!$opts['skipacl'] && $item['perm'] < AUTH_READ) return $return;
5883abeade3SAndreas Gohr        if($opts['pagesonly'] && (substr($file,-4) != '.txt')) return $return;
589de3eb1d7SAdrian Lang        if(!$opts['showhidden'] && isHiddenPage($item['id'])) return $return;
5903abeade3SAndreas Gohr        if($opts['filematch'] && !preg_match('/'.$opts['filematch'].'/',$file)) return $return;
5918705cc81SAndreas Gohr        if($opts['idmatch'] && !preg_match('/'.$opts['idmatch'].'/',$item['id'])) return $return;
5923abeade3SAndreas Gohr    }
5933abeade3SAndreas Gohr
5943abeade3SAndreas Gohr    // still here? prepare the item
5953abeade3SAndreas Gohr    $item['type']  = $type;
59632d6093dSAndreas Gohr    $item['level'] = $lvl;
5973abeade3SAndreas Gohr    $item['open']  = $return;
5983abeade3SAndreas Gohr
5993abeade3SAndreas Gohr    if($opts['meta']){
6003009a773SAndreas Gohr        $item['file']       = utf8_basename($file);
6013abeade3SAndreas Gohr        $item['size']       = filesize($base.'/'.$file);
6023abeade3SAndreas Gohr        $item['mtime']      = filemtime($base.'/'.$file);
6033abeade3SAndreas Gohr        $item['rev']        = $item['mtime'];
6043abeade3SAndreas Gohr        $item['writable']   = is_writable($base.'/'.$file);
6053abeade3SAndreas Gohr        $item['executable'] = is_executable($base.'/'.$file);
6063abeade3SAndreas Gohr    }
6073abeade3SAndreas Gohr
6083abeade3SAndreas Gohr    if($type == 'f'){
6093abeade3SAndreas Gohr        if($opts['hash']) $item['hash'] = md5(io_readFile($base.'/'.$file,false));
61067c15eceSMichael Hamann        if($opts['firsthead']) $item['title'] = p_get_first_heading($item['id'],METADATA_DONT_RENDER);
6113abeade3SAndreas Gohr    }
6123abeade3SAndreas Gohr
6133abeade3SAndreas Gohr    // finally add the item
6143abeade3SAndreas Gohr    $data[] = $item;
6153abeade3SAndreas Gohr    return $return;
6163abeade3SAndreas Gohr}
6173abeade3SAndreas Gohr
618e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
619