xref: /dokuwiki/inc/search.php (revision abc306f45f2ace038967bf7c51abd6ea53f56170)
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
1924baa045SAndreas Gohr * @param   callback  $func Callback (function name or arayy with object,method)
2024baa045SAndreas Gohr * @param   string    $dir  Current directory beyond $base
2124baa045SAndreas Gohr * @param   int       $lvl  Recursion Level
2215fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
23f3f0262cSandi */
24*abc306f4SKate Arzamastsevafunction search(&$data,$base,$func,$opts,$dir='',$lvl=1,$sort=false){
25f3f0262cSandi    $dirs   = array();
26f3f0262cSandi    $files  = array();
27*abc306f4SKate Arzamastseva    $filepaths = array();
28f3f0262cSandi
29f3f0262cSandi    //read in directories and files
30f3f0262cSandi    $dh = @opendir($base.'/'.$dir);
31f3f0262cSandi    if(!$dh) return;
32f3f0262cSandi    while(($file = readdir($dh)) !== false){
33de3dfc91Sandi        if(preg_match('/^[\._]/',$file)) continue; //skip hidden files and upper dirs
34f3f0262cSandi        if(is_dir($base.'/'.$dir.'/'.$file)){
35f3f0262cSandi            $dirs[] = $dir.'/'.$file;
36f3f0262cSandi            continue;
37f3f0262cSandi        }
38f3f0262cSandi        $files[] = $dir.'/'.$file;
39*abc306f4SKate Arzamastseva        $filepaths[] = $base.'/'.$dir.'/'.$file;
40f3f0262cSandi    }
41f3f0262cSandi    closedir($dh);
42*abc306f4SKate Arzamastseva    if ($sort == 'date') {
43*abc306f4SKate Arzamastseva        @array_multisort(array_map('filemtime', $filepaths), SORT_NUMERIC, SORT_ASC, $files);
44*abc306f4SKate Arzamastseva    } else {
45f3f0262cSandi        sort($files);
46*abc306f4SKate Arzamastseva    }
47f3f0262cSandi    sort($dirs);
48f3f0262cSandi
49f3f0262cSandi    //give directories to userfunction then recurse
50f3f0262cSandi    foreach($dirs as $dir){
51d8126df2SGina Haeussge        if (call_user_func_array($func, array(&$data,$base,$dir,'d',$lvl,$opts))){
52f3f0262cSandi            search($data,$base,$func,$opts,$dir,$lvl+1);
53f3f0262cSandi        }
54f3f0262cSandi    }
55f3f0262cSandi    //now handle the files
56f3f0262cSandi    foreach($files as $file){
57d8126df2SGina Haeussge        call_user_func_array($func, array(&$data,$base,$file,'f',$lvl,$opts));
58f3f0262cSandi    }
59f3f0262cSandi}
60f3f0262cSandi
61f3f0262cSandi/**
62d8126df2SGina Haeussge * Wrapper around call_user_func_array.
6324baa045SAndreas Gohr *
64d8126df2SGina Haeussge * @deprecated
6524baa045SAndreas Gohr */
6624baa045SAndreas Gohrfunction search_callback($func,&$data,$base,$file,$type,$lvl,$opts){
67d8126df2SGina Haeussge    return call_user_func_array($func, array(&$data,$base,$file,$type,$lvl,$opts));
6824baa045SAndreas Gohr}
6924baa045SAndreas Gohr
7024baa045SAndreas Gohr/**
71f3f0262cSandi * The following functions are userfunctions to use with the search
72f3f0262cSandi * function above. This function is called for every found file or
73f3f0262cSandi * directory. When a directory is given to the function it has to
74f3f0262cSandi * decide if this directory should be traversed (true) or not (false)
75f3f0262cSandi * The function has to accept the following parameters:
76f3f0262cSandi *
77f3f0262cSandi * &$data - Reference to the result data structure
78f3f0262cSandi * $base  - Base usually $conf['datadir']
79f3f0262cSandi * $file  - current file or directory relative to $base
80f3f0262cSandi * $type  - Type either 'd' for directory or 'f' for file
81f3f0262cSandi * $lvl   - Current recursion depht
82f3f0262cSandi * $opts  - option array as given to search()
83f3f0262cSandi *
84f3f0262cSandi * return values for files are ignored
85f3f0262cSandi *
86f3f0262cSandi * All functions should check the ACL for document READ rights
87f3f0262cSandi * namespaces (directories) are NOT checked as this would break
88f3f0262cSandi * the recursion (You can have an nonreadable dir over a readable
890e1a261eSMichael Klier * one deeper nested) also make sure to check the file type (for example
900e1a261eSMichael Klier * in case of lockfiles).
91f3f0262cSandi */
92f3f0262cSandi
93f3f0262cSandi/**
9463f2400bSandi * Searches for pages beginning with the given query
9563f2400bSandi *
9663f2400bSandi * @author Andreas Gohr <andi@splitbrain.org>
9763f2400bSandi */
9863f2400bSandifunction search_qsearch(&$data,$base,$file,$type,$lvl,$opts){
998705cc81SAndreas Gohr    $opts = array(
1008705cc81SAndreas Gohr            'idmatch'   => '(^|:)'.preg_quote($opts['query'],'/').'/',
1018705cc81SAndreas Gohr            'listfiles' => true,
1028705cc81SAndreas Gohr            'pagesonly' => true,
1038705cc81SAndreas Gohr            );
1048705cc81SAndreas Gohr    return search_universal($data,$base,$file,$type,$lvl,$opts);
10563f2400bSandi}
10663f2400bSandi
10763f2400bSandi/**
10815fae107Sandi * Build the browsable index of pages
109f3f0262cSandi *
110f3f0262cSandi * $opts['ns'] is the current namespace
11115fae107Sandi *
11215fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
113f3f0262cSandi */
114f3f0262cSandifunction search_index(&$data,$base,$file,$type,$lvl,$opts){
115d1c7b6ecSAndreas Gohr    global $conf;
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;
1230e1a261eSMichael Klier    }elseif($type == 'f' && ($opts['nofiles'] || substr($file,-4) != '.txt')){
124f3f0262cSandi        //don't add
125f3f0262cSandi        return false;
126f3f0262cSandi    }
127f3f0262cSandi
128e63d421bSAndreas Gohr    $id = pathID($file,($type == 'd'));
1290dc92c6fSAndreas Gohr
130d1c7b6ecSAndreas Gohr    if($type=='d' && $conf['sneaky_index'] && auth_quickaclcheck($id.':') < AUTH_READ){
131b54f94e0SAndreas Gohr        return false;
132b54f94e0SAndreas Gohr    }
133b54f94e0SAndreas Gohr
1340dc92c6fSAndreas Gohr    //check hidden
1351211a7a9SMartin Tschofen    if(isHiddenPage($id)){
1360dc92c6fSAndreas Gohr        return false;
1370dc92c6fSAndreas Gohr    }
1380dc92c6fSAndreas Gohr
1390dc92c6fSAndreas Gohr    //check ACL
140f3f0262cSandi    if($type=='f' && auth_quickaclcheck($id) < AUTH_READ){
141f3f0262cSandi        return false;
142f3f0262cSandi    }
143f3f0262cSandi
144f3f0262cSandi    $data[]=array( 'id'    => $id,
145f3f0262cSandi            'type'  => $type,
146cb70c441Sandi            'level' => $lvl,
147cb70c441Sandi            'open'  => $return );
148f3f0262cSandi    return $return;
149f3f0262cSandi}
150f3f0262cSandi
151f3f0262cSandi/**
15215fae107Sandi * List all namespaces
15315fae107Sandi *
15415fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
155f3f0262cSandi */
156f3f0262cSandifunction search_namespaces(&$data,$base,$file,$type,$lvl,$opts){
1578705cc81SAndreas Gohr    $opts = array(
1588705cc81SAndreas Gohr            'listdirs' => true,
1598705cc81SAndreas Gohr            );
1608705cc81SAndreas Gohr    return search_universal($data,$base,$file,$type,$lvl,$opts);
161f3f0262cSandi}
162f3f0262cSandi
163f3f0262cSandi/**
16415fae107Sandi * List all mediafiles in a namespace
16515fae107Sandi *
16615fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
167f3f0262cSandi */
168f3f0262cSandifunction search_media(&$data,$base,$file,$type,$lvl,$opts){
169b8219d2dSAndreas Gohr
170f3f0262cSandi    //we do nothing with directories
1711a49ac65SGina Haeussge    if($type == 'd') {
172224122cfSAndreas Gohr        if(!$opts['depth']) return true; // recurse forever
17378315408SAndreas Gohr        $depth = substr_count($file,'/');
174b8219d2dSAndreas Gohr        if($depth >= $opts['depth']) return false; // depth reached
175224122cfSAndreas Gohr        return true;
1761a49ac65SGina Haeussge    }
177f3f0262cSandi
178f3f0262cSandi    $info         = array();
179156a608cSandi    $info['id']   = pathID($file,true);
18064807c84SAndreas Gohr    if($info['id'] != cleanID($info['id'])){
18164807c84SAndreas Gohr        if($opts['showmsg'])
18264807c84SAndreas Gohr            msg(hsc($info['id']).' is not a valid file name for DokuWiki - skipped',-1);
18364807c84SAndreas Gohr        return false; // skip non-valid files
18464807c84SAndreas Gohr    }
185f3f0262cSandi
186f3f0262cSandi    //check ACL for namespace (we have no ACL for mediafiles)
187224122cfSAndreas Gohr    $info['perm'] = auth_quickaclcheck(getNS($info['id']).':*');
188224122cfSAndreas Gohr    if(!$opts['skipacl'] && $info['perm'] < AUTH_READ){
189224122cfSAndreas Gohr        return false;
190224122cfSAndreas Gohr    }
191224122cfSAndreas Gohr
192224122cfSAndreas Gohr    //check pattern filter
193224122cfSAndreas Gohr    if($opts['pattern'] && !@preg_match($opts['pattern'], $info['id'])){
194f3f0262cSandi        return false;
195f3f0262cSandi    }
196f3f0262cSandi
197f3f0262cSandi    $info['file']     = basename($file);
198f3f0262cSandi    $info['size']     = filesize($base.'/'.$file);
1995e7fa82eSAndreas Gohr    $info['mtime']    = filemtime($base.'/'.$file);
2003df72098SAndreas Gohr    $info['writable'] = is_writable($base.'/'.$file);
201f3f0262cSandi    if(preg_match("/\.(jpe?g|gif|png)$/",$file)){
202f3f0262cSandi        $info['isimg'] = true;
20323a34783SAndreas Gohr        $info['meta']  = new JpegMeta($base.'/'.$file);
204f3f0262cSandi    }else{
205f3f0262cSandi        $info['isimg'] = false;
206f3f0262cSandi    }
207224122cfSAndreas Gohr    if($opts['hash']){
208dfd343c4SAndreas Gohr        $info['hash'] = md5(io_readFile(mediaFN($info['id']),false));
209224122cfSAndreas Gohr    }
210224122cfSAndreas Gohr
211f3f0262cSandi    $data[] = $info;
212f3f0262cSandi
213f3f0262cSandi    return false;
214f3f0262cSandi}
215f3f0262cSandi
216f3f0262cSandi/**
217f3f0262cSandi * This function just lists documents (for RSS namespace export)
21815fae107Sandi *
21915fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
220f3f0262cSandi */
221f3f0262cSandifunction search_list(&$data,$base,$file,$type,$lvl,$opts){
222f3f0262cSandi    //we do nothing with directories
223f3f0262cSandi    if($type == 'd') return false;
2240e1a261eSMichael Klier    //only search txt files
2250e1a261eSMichael Klier    if(substr($file,-4) == '.txt'){
226f3f0262cSandi        //check ACL
227f3f0262cSandi        $id = pathID($file);
228f3f0262cSandi        if(auth_quickaclcheck($id) < AUTH_READ){
229f3f0262cSandi            return false;
230f3f0262cSandi        }
2310e1a261eSMichael Klier        $data[]['id'] = $id;
232f3f0262cSandi    }
233f3f0262cSandi    return false;
234f3f0262cSandi}
235f3f0262cSandi
236f3f0262cSandi/**
237f3f0262cSandi * Quicksearch for searching matching pagenames
238f3f0262cSandi *
239f3f0262cSandi * $opts['query'] is the search query
24015fae107Sandi *
24115fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
242f3f0262cSandi */
243f3f0262cSandifunction search_pagename(&$data,$base,$file,$type,$lvl,$opts){
244f3f0262cSandi    //we do nothing with directories
245f3f0262cSandi    if($type == 'd') return true;
246f3f0262cSandi    //only search txt files
2470e1a261eSMichael Klier    if(substr($file,-4) != '.txt') return true;
248f3f0262cSandi
249f3f0262cSandi    //simple stringmatching
250396b7edbSmatthiasgrimm    if (!empty($opts['query'])){
251f3f0262cSandi        if(strpos($file,$opts['query']) !== false){
252f3f0262cSandi            //check ACL
253f3f0262cSandi            $id = pathID($file);
254f3f0262cSandi            if(auth_quickaclcheck($id) < AUTH_READ){
255f3f0262cSandi                return false;
256f3f0262cSandi            }
257f3f0262cSandi            $data[]['id'] = $id;
258f3f0262cSandi        }
259396b7edbSmatthiasgrimm    }
260f3f0262cSandi    return true;
261f3f0262cSandi}
262f3f0262cSandi
263f3f0262cSandi/**
26458b6f612SAndreas Gohr * Just lists all documents
26558b6f612SAndreas Gohr *
2661fcfad4dSAndreas Gohr * $opts['depth']   recursion level, 0 for all
2671fcfad4dSAndreas Gohr * $opts['hash']    do md5 sum of content?
268224122cfSAndreas Gohr * $opts['skipacl'] list everything regardless of ACL
2691fcfad4dSAndreas Gohr *
27058b6f612SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
27158b6f612SAndreas Gohr */
27258b6f612SAndreas Gohrfunction search_allpages(&$data,$base,$file,$type,$lvl,$opts){
27358b6f612SAndreas Gohr    //we do nothing with directories
2741fcfad4dSAndreas Gohr    if($type == 'd'){
2751fcfad4dSAndreas Gohr        if(!$opts['depth']) return true; // recurse forever
27617ff9c04SAndreas Gohr        $parts = explode('/',ltrim($file,'/'));
2771fcfad4dSAndreas Gohr        if(count($parts) == $opts['depth']) return false; // depth reached
2781fcfad4dSAndreas Gohr        return true;
2791fcfad4dSAndreas Gohr    }
2801fcfad4dSAndreas Gohr
28158b6f612SAndreas Gohr    //only search txt files
2820e1a261eSMichael Klier    if(substr($file,-4) != '.txt') return true;
28358b6f612SAndreas Gohr
2841fcfad4dSAndreas Gohr    $item['id']   = pathID($file);
285061df79cSAndreas Gohr    if(!$opts['skipacl'] && auth_quickaclcheck($item['id']) < AUTH_READ){
2861fcfad4dSAndreas Gohr        return false;
2871fcfad4dSAndreas Gohr    }
2881fcfad4dSAndreas Gohr
2891fcfad4dSAndreas Gohr    $item['rev']   = filemtime($base.'/'.$file);
290224122cfSAndreas Gohr    $item['mtime'] = $item['rev'];
2911fcfad4dSAndreas Gohr    $item['size']  = filesize($base.'/'.$file);
2921fcfad4dSAndreas Gohr    if($opts['hash']){
2931fcfad4dSAndreas Gohr        $item['hash'] = md5(trim(rawWiki($item['id'])));
2941fcfad4dSAndreas Gohr    }
2951fcfad4dSAndreas Gohr
2961fcfad4dSAndreas Gohr    $data[] = $item;
29758b6f612SAndreas Gohr    return true;
29858b6f612SAndreas Gohr}
29958b6f612SAndreas Gohr
30058b6f612SAndreas Gohr/**
301f3f0262cSandi * Search for backlinks to a given page
302f3f0262cSandi *
303f3f0262cSandi * $opts['ns']    namespace of the page
304f3f0262cSandi * $opts['name']  name of the page without namespace
30515fae107Sandi *
30615fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
30754f4c056SAndreas Gohr * @deprecated Replaced by ft_backlinks()
308f3f0262cSandi */
309f3f0262cSandifunction search_backlinks(&$data,$base,$file,$type,$lvl,$opts){
310f3f0262cSandi    //we do nothing with directories
3110e1a261eSMichael Klier    if($type == 'd') return true;
312f3f0262cSandi    //only search txt files
3130e1a261eSMichael Klier    if(substr($file,-4) != '.txt') return true;
314f3f0262cSandi
315f3f0262cSandi    //absolute search id
316f3f0262cSandi    $sid = cleanID($opts['ns'].':'.$opts['name']);
317f3f0262cSandi
31837e34a5eSandi    //current id and namespace
319f3f0262cSandi    $cid = pathID($file);
320f3f0262cSandi    $cns = getNS($cid);
321f3f0262cSandi
322f3f0262cSandi    //check ACL
323f3f0262cSandi    if(auth_quickaclcheck($cid) < AUTH_READ){
324f3f0262cSandi        return false;
325f3f0262cSandi    }
326f3f0262cSandi
32737e34a5eSandi    //fetch instructions
32837e34a5eSandi    $instructions = p_cached_instructions($base.$file,true);
32937e34a5eSandi    if(is_null($instructions)) return false;
330f3f0262cSandi
331de3eb1d7SAdrian Lang    global $conf;
33237e34a5eSandi    //check all links for match
33337e34a5eSandi    foreach($instructions as $ins){
33437e34a5eSandi        if($ins[0] == 'internallink' || ($conf['camelcase'] && $ins[0] == 'camelcaselink') ){
33537e34a5eSandi            $mid = $ins[1][0];
33637e34a5eSandi            resolve_pageid($cns,$mid,$exists); //exists is not used
337f3f0262cSandi            if($mid == $sid){
33837e34a5eSandi                //we have a match - finish
339f3f0262cSandi                $data[]['id'] = $cid;
340f3f0262cSandi                break;
341f3f0262cSandi            }
342f3f0262cSandi        }
343f3f0262cSandi    }
344f3f0262cSandi
34537e34a5eSandi    return false;
34637e34a5eSandi}
34737e34a5eSandi
348f3f0262cSandi/**
349f3f0262cSandi * Fulltextsearch
350f3f0262cSandi *
351f3f0262cSandi * $opts['query'] is the search query
35215fae107Sandi *
35315fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
354506fa893SAndreas Gohr * @deprecated - fulltext indexer is used instead
355f3f0262cSandi */
356f3f0262cSandifunction search_fulltext(&$data,$base,$file,$type,$lvl,$opts){
357f3f0262cSandi    //we do nothing with directories
3580e1a261eSMichael Klier    if($type == 'd') return true;
359f3f0262cSandi    //only search txt files
3600e1a261eSMichael Klier    if(substr($file,-4) != '.txt') return true;
361f3f0262cSandi
362f3f0262cSandi    //check ACL
363f3f0262cSandi    $id = pathID($file);
364f3f0262cSandi    if(auth_quickaclcheck($id) < AUTH_READ){
365f3f0262cSandi        return false;
366f3f0262cSandi    }
367f3f0262cSandi
368f3f0262cSandi    //create regexp from queries
3695ef370d2Smatthiasgrimm    $poswords = array();
3705ef370d2Smatthiasgrimm    $negwords = array();
3715ef370d2Smatthiasgrimm    $qpreg = preg_split('/\s+/',$opts['query']);
3725ef370d2Smatthiasgrimm
3735ef370d2Smatthiasgrimm    foreach($qpreg as $word){
3745ef370d2Smatthiasgrimm        switch(substr($word,0,1)){
3755ef370d2Smatthiasgrimm            case '-':
376396b7edbSmatthiasgrimm                if(strlen($word) > 1){  // catch single '-'
3775ef370d2Smatthiasgrimm                    array_push($negwords,preg_quote(substr($word,1),'#'));
378396b7edbSmatthiasgrimm                }
3795ef370d2Smatthiasgrimm                break;
3805ef370d2Smatthiasgrimm            case '+':
381396b7edbSmatthiasgrimm                if(strlen($word) > 1){  // catch single '+'
3825ef370d2Smatthiasgrimm                    array_push($poswords,preg_quote(substr($word,1),'#'));
383396b7edbSmatthiasgrimm                }
3845ef370d2Smatthiasgrimm                break;
3855ef370d2Smatthiasgrimm            default:
3865ef370d2Smatthiasgrimm                array_push($poswords,preg_quote($word,'#'));
3875ef370d2Smatthiasgrimm                break;
3885ef370d2Smatthiasgrimm        }
3895ef370d2Smatthiasgrimm    }
390248a7321Smatthiasgrimm
391248a7321Smatthiasgrimm    // a search without any posword is useless
392248a7321Smatthiasgrimm    if (!count($poswords)) return true;
3935ef370d2Smatthiasgrimm
3945a5d942dSmatthiasgrimm    $reg  = '^(?=.*?'.join(')(?=.*?',$poswords).')';
3955ef370d2Smatthiasgrimm            $reg .= count($negwords) ? '((?!'.join('|',$negwords).').)*$' : '.*$';
396b59a406bSmatthiasgrimm            search_regex($data,$base,$file,$reg,$poswords);
397b59a406bSmatthiasgrimm            return true;
398b59a406bSmatthiasgrimm            }
399b59a406bSmatthiasgrimm
400b59a406bSmatthiasgrimm            /**
401b59a406bSmatthiasgrimm             * Reference search
402b59a406bSmatthiasgrimm             * This fuction searches for existing references to a given media file
403b59a406bSmatthiasgrimm             * and returns an array with the found pages. It doesn't pay any
404b59a406bSmatthiasgrimm             * attention to ACL permissions to find every reference. The caller
405b59a406bSmatthiasgrimm             * must check if the user has the appropriate rights to see the found
406b59a406bSmatthiasgrimm             * page and eventually have to prevent the result from displaying.
407b59a406bSmatthiasgrimm             *
408b59a406bSmatthiasgrimm             * @param array  $data Reference to the result data structure
409b59a406bSmatthiasgrimm             * @param string $base Base usually $conf['datadir']
410b59a406bSmatthiasgrimm             * @param string $file current file or directory relative to $base
411b59a406bSmatthiasgrimm             * @param char   $type Type either 'd' for directory or 'f' for file
412b59a406bSmatthiasgrimm             * @param int    $lvl  Current recursion depht
413b59a406bSmatthiasgrimm             * @param mixed  $opts option array as given to search()
414b59a406bSmatthiasgrimm             *
415b59a406bSmatthiasgrimm             * $opts['query'] is the demanded media file name
416b59a406bSmatthiasgrimm             *
417b59a406bSmatthiasgrimm             * @author  Andreas Gohr <andi@splitbrain.org>
418b59a406bSmatthiasgrimm             * @author  Matthias Grimm <matthiasgrimm@users.sourceforge.net>
419b59a406bSmatthiasgrimm             */
420b59a406bSmatthiasgrimmfunction search_reference(&$data,$base,$file,$type,$lvl,$opts){
421b59a406bSmatthiasgrimm    global $conf;
422b59a406bSmatthiasgrimm
423b59a406bSmatthiasgrimm    //we do nothing with directories
424b59a406bSmatthiasgrimm    if($type == 'd') return true;
425b59a406bSmatthiasgrimm
426b59a406bSmatthiasgrimm    //only search txt files
4270e1a261eSMichael Klier    if(substr($file,-4) != '.txt') return true;
428b59a406bSmatthiasgrimm
429e28299ccSmatthiasgrimm    //we finish after 'cnt' references found. The return value
430b59a406bSmatthiasgrimm    //'false' will skip subdirectories to speed search up.
431e28299ccSmatthiasgrimm    $cnt = $conf['refshow'] > 0 ? $conf['refshow'] : 1;
432e28299ccSmatthiasgrimm    if(count($data) >= $cnt) return false;
433b59a406bSmatthiasgrimm
434d67ca2c0Smatthiasgrimm    $reg = '\{\{ *\:?'.$opts['query'].' *(\|.*)?\}\}';
435b59a406bSmatthiasgrimm    search_regex($data,$base,$file,$reg,array($opts['query']));
436b59a406bSmatthiasgrimm    return true;
437b59a406bSmatthiasgrimm}
438b59a406bSmatthiasgrimm
439b59a406bSmatthiasgrimm/* ------------- helper functions below -------------- */
440b59a406bSmatthiasgrimm
441b59a406bSmatthiasgrimm/**
442b59a406bSmatthiasgrimm * fulltext search helper
443b59a406bSmatthiasgrimm * searches a text file with a given regular expression
444b59a406bSmatthiasgrimm * no ACL checks are performed. This have to be done by
445b59a406bSmatthiasgrimm * the caller if necessary.
446b59a406bSmatthiasgrimm *
447b59a406bSmatthiasgrimm * @param array  $data  reference to array for results
448b59a406bSmatthiasgrimm * @param string $base  base directory
449b59a406bSmatthiasgrimm * @param string $file  file name to search in
450b59a406bSmatthiasgrimm * @param string $reg   regular expression to search for
451b59a406bSmatthiasgrimm * @param array  $words words that should be marked in the results
452b59a406bSmatthiasgrimm *
453b59a406bSmatthiasgrimm * @author  Andreas Gohr <andi@splitbrain.org>
454b59a406bSmatthiasgrimm * @author  Matthias Grimm <matthiasgrimm@users.sourceforge.net>
455506fa893SAndreas Gohr *
456506fa893SAndreas Gohr * @deprecated - fulltext indexer is used instead
457b59a406bSmatthiasgrimm */
458b59a406bSmatthiasgrimmfunction search_regex(&$data,$base,$file,$reg,$words){
459b59a406bSmatthiasgrimm
460b59a406bSmatthiasgrimm    //get text
461b59a406bSmatthiasgrimm    $text = io_readfile($base.'/'.$file);
462b59a406bSmatthiasgrimm    //lowercase text (u modifier does not help with case)
463b59a406bSmatthiasgrimm    $lctext = utf8_strtolower($text);
464f3f0262cSandi
465f3f0262cSandi    //do the fulltext search
466f3f0262cSandi    $matches = array();
4675ef370d2Smatthiasgrimm    if($cnt = preg_match_all('#'.$reg.'#usi',$lctext,$matches)){
468f3f0262cSandi        //this is not the best way for snippet generation but the fastest I could find
469b59a406bSmatthiasgrimm        $q = $words[0];  //use first word for snippet creation
470d5a2a500Sandi        $p = utf8_strpos($lctext,$q);
471f3f0262cSandi        $f = $p - 100;
472d5a2a500Sandi        $l = utf8_strlen($q) + 200;
473f3f0262cSandi        if($f < 0) $f = 0;
474f3f0262cSandi        $snippet = '<span class="search_sep"> ... </span>'.
475d5a2a500Sandi            htmlspecialchars(utf8_substr($text,$f,$l)).
476f3f0262cSandi            '<span class="search_sep"> ... </span>';
477b59a406bSmatthiasgrimm        $mark    = '('.join('|', $words).')';
478ed7ecb79SAnika Henke        $snippet = preg_replace('#'.$mark.'#si','<strong class="search_hit">\\1</strong>',$snippet);
479f3f0262cSandi
480f3f0262cSandi        $data[] = array(
481b59a406bSmatthiasgrimm                'id'       => pathID($file),
4825ef370d2Smatthiasgrimm                'count'    => preg_match_all('#'.$mark.'#usi',$lctext,$matches),
483b59a406bSmatthiasgrimm                'poswords' => join(' ',$words),
484f3f0262cSandi                'snippet'  => $snippet,
485f3f0262cSandi                );
486f3f0262cSandi    }
487f3f0262cSandi
488f3f0262cSandi    return true;
489f3f0262cSandi}
490f3f0262cSandi
491b59a406bSmatthiasgrimm
492f3f0262cSandi/**
49315fae107Sandi * fulltext sort
49415fae107Sandi *
495f3f0262cSandi * Callback sort function for use with usort to sort the data
496f3f0262cSandi * structure created by search_fulltext. Sorts descending by count
49715fae107Sandi *
49815fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
499f3f0262cSandi */
500f3f0262cSandifunction sort_search_fulltext($a,$b){
501f3f0262cSandi    if($a['count'] > $b['count']){
502f3f0262cSandi        return -1;
503f3f0262cSandi    }elseif($a['count'] < $b['count']){
504f3f0262cSandi        return 1;
505f3f0262cSandi    }else{
506f3f0262cSandi        return strcmp($a['id'],$b['id']);
507f3f0262cSandi    }
508f3f0262cSandi}
509f3f0262cSandi
510f3f0262cSandi/**
511f3f0262cSandi * translates a document path to an ID
51215fae107Sandi *
51315fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
51437e34a5eSandi * @todo    move to pageutils
515f3f0262cSandi */
516156a608cSandifunction pathID($path,$keeptxt=false){
51749c713a3Sandi    $id = utf8_decodeFN($path);
51849c713a3Sandi    $id = str_replace('/',':',$id);
519156a608cSandi    if(!$keeptxt) $id = preg_replace('#\.txt$#','',$id);
520709b1063SAdrian Lang    $id = trim($id, ':');
521f3f0262cSandi    return $id;
522f3f0262cSandi}
523f3f0262cSandi
524340756e4Sandi
5253abeade3SAndreas Gohr/**
5263abeade3SAndreas Gohr * This is a very universal callback for the search() function, replacing
5273abeade3SAndreas Gohr * many of the former individual functions at the cost of a more complex
5283abeade3SAndreas Gohr * setup.
5293abeade3SAndreas Gohr *
5303abeade3SAndreas Gohr * How the function behaves, depends on the options passed in the $opts
5313abeade3SAndreas Gohr * array, where the following settings can be used.
5323abeade3SAndreas Gohr *
5333abeade3SAndreas Gohr * depth      int     recursion depth. 0 for unlimited
5343abeade3SAndreas Gohr * keeptxt    bool    keep .txt extension for IDs
5353abeade3SAndreas Gohr * listfiles  bool    include files in listing
5363abeade3SAndreas Gohr * listdirs   bool    include namespaces in listing
5373abeade3SAndreas Gohr * pagesonly  bool    restrict files to pages
5383abeade3SAndreas Gohr * skipacl    bool    do not check for READ permission
5393abeade3SAndreas Gohr * sneakyacl  bool    don't recurse into nonreadable dirs
5403abeade3SAndreas Gohr * hash       bool    create MD5 hash for files
5413abeade3SAndreas Gohr * meta       bool    return file metadata
5423abeade3SAndreas Gohr * filematch  string  match files against this regexp
5438705cc81SAndreas Gohr * idmatch    string  match full ID against this regexp
5448705cc81SAndreas Gohr * dirmatch   string  match directory against this regexp when adding
5458705cc81SAndreas Gohr * nsmatch    string  match namespace against this regexp when adding
5468705cc81SAndreas Gohr * recmatch   string  match directory against this regexp when recursing
5473abeade3SAndreas Gohr * showmsg    bool    warn about non-ID files
5483abeade3SAndreas Gohr * showhidden bool    show hidden files too
5493abeade3SAndreas Gohr * firsthead  bool    return first heading for pages
5503abeade3SAndreas Gohr *
5513abeade3SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
5523abeade3SAndreas Gohr */
5533abeade3SAndreas Gohrfunction search_universal(&$data,$base,$file,$type,$lvl,$opts){
5543abeade3SAndreas Gohr    $item   = array();
5553abeade3SAndreas Gohr    $return = true;
5563abeade3SAndreas Gohr
5573abeade3SAndreas Gohr    // get ID and check if it is a valid one
558e63d421bSAndreas Gohr    $item['id'] = pathID($file,($type == 'd' || $opts['keeptxt']));
5598537abd1SAdrian Lang    if($item['id'] != cleanID($item['id'])){
5603abeade3SAndreas Gohr        if($opts['showmsg'])
5618537abd1SAdrian Lang            msg(hsc($item['id']).' is not a valid file name for DokuWiki - skipped',-1);
5623abeade3SAndreas Gohr        return false; // skip non-valid files
5633abeade3SAndreas Gohr    }
5648705cc81SAndreas Gohr    $item['ns']  = getNS($item['id']);
5653abeade3SAndreas Gohr
5663abeade3SAndreas Gohr    if($type == 'd') {
5673abeade3SAndreas Gohr        // decide if to recursion into this directory is wanted
5683abeade3SAndreas Gohr        if(!$opts['depth']){
5693abeade3SAndreas Gohr            $return = true; // recurse forever
5703abeade3SAndreas Gohr        }else{
5713abeade3SAndreas Gohr            $depth = substr_count($file,'/');
5723abeade3SAndreas Gohr            if($depth >= $opts['depth']){
5733abeade3SAndreas Gohr                $return = false; // depth reached
5743abeade3SAndreas Gohr            }else{
5753abeade3SAndreas Gohr                $return = true;
5763abeade3SAndreas Gohr            }
5773abeade3SAndreas Gohr        }
5783abeade3SAndreas Gohr        if($return && !preg_match('/'.$opts['recmatch'].'/',$file)){
5793abeade3SAndreas Gohr            $return = false; // doesn't match
5803abeade3SAndreas Gohr        }
5813abeade3SAndreas Gohr    }
5823abeade3SAndreas Gohr
5833abeade3SAndreas Gohr    // check ACL
5843abeade3SAndreas Gohr    if(!$opts['skipacl']){
5853abeade3SAndreas Gohr        if($type == 'd'){
5863abeade3SAndreas Gohr            $item['perm'] = auth_quickaclcheck($item['id'].':*');
5873abeade3SAndreas Gohr        }else{
5883abeade3SAndreas Gohr            $item['perm'] = auth_quickaclcheck($item['id']); //FIXME check namespace for media files
5893abeade3SAndreas Gohr        }
5903abeade3SAndreas Gohr    }else{
5913abeade3SAndreas Gohr        $item['perm'] = AUTH_DELETE;
5923abeade3SAndreas Gohr    }
5933abeade3SAndreas Gohr
5943abeade3SAndreas Gohr    // are we done here maybe?
5953abeade3SAndreas Gohr    if($type == 'd'){
5963abeade3SAndreas Gohr        if(!$opts['listdirs']) return $return;
5973abeade3SAndreas Gohr        if(!$opts['skipacl'] && $opts['sneakyacl'] && $item['perm'] < AUTH_READ) return false; //neither list nor recurse
5983abeade3SAndreas Gohr        if($opts['dirmatch'] && !preg_match('/'.$opts['dirmatch'].'/',$file)) return $return;
5998705cc81SAndreas Gohr        if($opts['nsmatch'] && !preg_match('/'.$opts['nsmatch'].'/',$item['ns'])) return $return;
6003abeade3SAndreas Gohr    }else{
6013abeade3SAndreas Gohr        if(!$opts['listfiles']) return $return;
6023abeade3SAndreas Gohr        if(!$opts['skipacl'] && $item['perm'] < AUTH_READ) return $return;
6033abeade3SAndreas Gohr        if($opts['pagesonly'] && (substr($file,-4) != '.txt')) return $return;
604de3eb1d7SAdrian Lang        if(!$opts['showhidden'] && isHiddenPage($item['id'])) return $return;
6053abeade3SAndreas Gohr        if($opts['filematch'] && !preg_match('/'.$opts['filematch'].'/',$file)) return $return;
6068705cc81SAndreas Gohr        if($opts['idmatch'] && !preg_match('/'.$opts['idmatch'].'/',$item['id'])) return $return;
6073abeade3SAndreas Gohr    }
6083abeade3SAndreas Gohr
6093abeade3SAndreas Gohr    // still here? prepare the item
6103abeade3SAndreas Gohr    $item['type']  = $type;
61132d6093dSAndreas Gohr    $item['level'] = $lvl;
6123abeade3SAndreas Gohr    $item['open']  = $return;
6133abeade3SAndreas Gohr
6143abeade3SAndreas Gohr    if($opts['meta']){
6153abeade3SAndreas Gohr        $item['file']       = basename($file);
6163abeade3SAndreas Gohr        $item['size']       = filesize($base.'/'.$file);
6173abeade3SAndreas Gohr        $item['mtime']      = filemtime($base.'/'.$file);
6183abeade3SAndreas Gohr        $item['rev']        = $item['mtime'];
6193abeade3SAndreas Gohr        $item['writable']   = is_writable($base.'/'.$file);
6203abeade3SAndreas Gohr        $item['executable'] = is_executable($base.'/'.$file);
6213abeade3SAndreas Gohr    }
6223abeade3SAndreas Gohr
6233abeade3SAndreas Gohr    if($type == 'f'){
6243abeade3SAndreas Gohr        if($opts['hash']) $item['hash'] = md5(io_readFile($base.'/'.$file,false));
62567c15eceSMichael Hamann        if($opts['firsthead']) $item['title'] = p_get_first_heading($item['id'],METADATA_DONT_RENDER);
6263abeade3SAndreas Gohr    }
6273abeade3SAndreas Gohr
6283abeade3SAndreas Gohr    // finally add the item
6293abeade3SAndreas Gohr    $data[] = $item;
6303abeade3SAndreas Gohr    return $return;
6313abeade3SAndreas Gohr}
6323abeade3SAndreas Gohr
633e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
634