xref: /dokuwiki/inc/search.php (revision 24870174d2ee45460ba6bcfe5f5a0ae94715efd7)
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 */
8*24870174SAndreas Gohruse dokuwiki\Utf8\PhpString;
9*24870174SAndreas Gohruse dokuwiki\File\MediaFile;
102d85e841SAndreas Gohruse dokuwiki\Utf8\Sort;
112d85e841SAndreas Gohr
12f3f0262cSandi/**
13ce4301e3SGerrit Uitslag * Recurse directory
1415fae107Sandi *
15f3f0262cSandi * This function recurses into a given base directory
16f3f0262cSandi * and calls the supplied function for each file and directory
1715fae107Sandi *
1824998b31SGerrit Uitslag * @param   array    &$data The results of the search are stored here
1924baa045SAndreas Gohr * @param   string    $base Where to start the search
20fe82d751SChristopher Smith * @param   callback  $func Callback (function name or array with object,method)
2124998b31SGerrit Uitslag * @param   array     $opts option array will be given to the Callback
2224baa045SAndreas Gohr * @param   string    $dir  Current directory beyond $base
2324baa045SAndreas Gohr * @param   int       $lvl  Recursion Level
2464159a61SAndreas Gohr * @param   mixed     $sort 'natural' to use natural order sorting (default);
2564159a61SAndreas Gohr *                          'date' to sort by filemtime; leave empty to skip sorting.
2615fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
27f3f0262cSandi */
28155e63c9SChristopher Smithfunction search(&$data,$base,$func,$opts,$dir='',$lvl=1,$sort='natural'){
29*24870174SAndreas Gohr    $dirs   = [];
30*24870174SAndreas Gohr    $files  = [];
31*24870174SAndreas Gohr    $filepaths = [];
32f3f0262cSandi
33e0b6aadeSAndreas Gohr    // safeguard against runaways #1452
34e0b6aadeSAndreas Gohr    if($base == '' || $base == '/') {
35e0b6aadeSAndreas Gohr        throw new RuntimeException('No valid $base passed to search() - possible misconfiguration or bug');
36e0b6aadeSAndreas Gohr    }
37e0b6aadeSAndreas Gohr
38f3f0262cSandi    //read in directories and files
39f3f0262cSandi    $dh = @opendir($base.'/'.$dir);
40f3f0262cSandi    if(!$dh) return;
41f3f0262cSandi    while(($file = readdir($dh)) !== false){
42de3dfc91Sandi        if(preg_match('/^[\._]/',$file)) continue; //skip hidden files and upper dirs
43f3f0262cSandi        if(is_dir($base.'/'.$dir.'/'.$file)){
44f3f0262cSandi            $dirs[] = $dir.'/'.$file;
45f3f0262cSandi            continue;
46f3f0262cSandi        }
47f3f0262cSandi        $files[] = $dir.'/'.$file;
48abc306f4SKate Arzamastseva        $filepaths[] = $base.'/'.$dir.'/'.$file;
49f3f0262cSandi    }
50f3f0262cSandi    closedir($dh);
51ec24a2dfSPhilipp A. Hartmann    if (!empty($sort)) {
52abc306f4SKate Arzamastseva        if ($sort == 'date') {
53d971ea8bSKate Arzamastseva            @array_multisort(array_map('filemtime', $filepaths), SORT_NUMERIC, SORT_DESC, $files);
541dc5d48bSChristopher Smith        } else /* natural */ {
552d85e841SAndreas Gohr            Sort::asortFN($files);
56abc306f4SKate Arzamastseva        }
572d85e841SAndreas Gohr        Sort::asortFN($dirs);
58ec24a2dfSPhilipp A. Hartmann    }
59f3f0262cSandi
60f3f0262cSandi    //give directories to userfunction then recurse
61f3f0262cSandi    foreach($dirs as $dir){
62*24870174SAndreas Gohr        if (call_user_func_array($func, [&$data, $base, $dir, 'd', $lvl, $opts])){
635514a5a7SChristopher Smith            search($data,$base,$func,$opts,$dir,$lvl+1,$sort);
64f3f0262cSandi        }
65f3f0262cSandi    }
66f3f0262cSandi    //now handle the files
67f3f0262cSandi    foreach($files as $file){
68*24870174SAndreas Gohr        call_user_func_array($func, [&$data, $base, $file, 'f', $lvl, $opts]);
69f3f0262cSandi    }
70f3f0262cSandi}
71f3f0262cSandi
72f3f0262cSandi/**
73f3f0262cSandi * The following functions are userfunctions to use with the search
74f3f0262cSandi * function above. This function is called for every found file or
75f3f0262cSandi * directory. When a directory is given to the function it has to
76f3f0262cSandi * decide if this directory should be traversed (true) or not (false)
77f3f0262cSandi * The function has to accept the following parameters:
78f3f0262cSandi *
79ce4301e3SGerrit Uitslag * array &$data  - Reference to the result data structure
80ce4301e3SGerrit Uitslag * string $base  - Base usually $conf['datadir']
81ce4301e3SGerrit Uitslag * string $file  - current file or directory relative to $base
82ce4301e3SGerrit Uitslag * string $type  - Type either 'd' for directory or 'f' for file
83ce4301e3SGerrit Uitslag * int    $lvl   - Current recursion depht
84ce4301e3SGerrit Uitslag * array  $opts  - option array as given to search()
85f3f0262cSandi *
86f3f0262cSandi * return values for files are ignored
87f3f0262cSandi *
88f3f0262cSandi * All functions should check the ACL for document READ rights
89783d2e49SAdrian Lang * namespaces (directories) are NOT checked (when sneaky_index is 0) as this
90783d2e49SAdrian Lang * would break the recursion (You can have an nonreadable dir over a readable
910e1a261eSMichael Klier * one deeper nested) also make sure to check the file type (for example
920e1a261eSMichael Klier * in case of lockfiles).
93f3f0262cSandi */
94f3f0262cSandi
95f3f0262cSandi/**
9663f2400bSandi * Searches for pages beginning with the given query
9763f2400bSandi *
9863f2400bSandi * @author Andreas Gohr <andi@splitbrain.org>
99f50a239bSTakamura *
100f50a239bSTakamura * @param array $data
101f50a239bSTakamura * @param string $base
102f50a239bSTakamura * @param string $file
103f50a239bSTakamura * @param string $type
104f50a239bSTakamura * @param integer $lvl
105f50a239bSTakamura * @param array $opts
106f50a239bSTakamura *
107f50a239bSTakamura * @return bool
10863f2400bSandi */
10963f2400bSandifunction search_qsearch(&$data,$base,$file,$type,$lvl,$opts){
110*24870174SAndreas Gohr    $opts = [
1118705cc81SAndreas Gohr        'idmatch'   => '(^|:)'.preg_quote($opts['query'],'/').'/',
1128705cc81SAndreas Gohr        'listfiles' => true,
113*24870174SAndreas Gohr        'pagesonly' => true
114*24870174SAndreas Gohr    ];
1158705cc81SAndreas Gohr    return search_universal($data,$base,$file,$type,$lvl,$opts);
11663f2400bSandi}
11763f2400bSandi
11863f2400bSandi/**
11915fae107Sandi * Build the browsable index of pages
120f3f0262cSandi *
121783d2e49SAdrian Lang * $opts['ns'] is the currently viewed namespace
12215fae107Sandi *
12315fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
124f50a239bSTakamura *
125f50a239bSTakamura * @param array $data
126f50a239bSTakamura * @param string $base
127f50a239bSTakamura * @param string $file
128f50a239bSTakamura * @param string $type
129f50a239bSTakamura * @param integer $lvl
130f50a239bSTakamura * @param array $opts
131f50a239bSTakamura *
132f50a239bSTakamura * @return bool
133f3f0262cSandi */
134f3f0262cSandifunction search_index(&$data,$base,$file,$type,$lvl,$opts){
135d1c7b6ecSAndreas Gohr    global $conf;
136*24870174SAndreas Gohr    $ns = $opts['ns'] ?? '';
137*24870174SAndreas Gohr    $opts = [
138783d2e49SAdrian Lang        'pagesonly' => true,
139783d2e49SAdrian Lang        'listdirs' => true,
140443e135dSChristopher Smith        'listfiles' => empty($opts['nofiles']),
141783d2e49SAdrian Lang        'sneakyacl' => $conf['sneaky_index'],
142783d2e49SAdrian Lang        // Hacky, should rather use recmatch
143*24870174SAndreas Gohr        'depth' => preg_match('#^'.preg_quote($file, '#').'(/|$)#','/'.$ns) ? 0 : -1,
144*24870174SAndreas Gohr    ];
145f3f0262cSandi
146783d2e49SAdrian Lang    return search_universal($data, $base, $file, $type, $lvl, $opts);
147f3f0262cSandi}
148f3f0262cSandi
149f3f0262cSandi/**
15015fae107Sandi * List all namespaces
15115fae107Sandi *
15215fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
153f50a239bSTakamura *
154f50a239bSTakamura * @param array $data
155f50a239bSTakamura * @param string $base
156f50a239bSTakamura * @param string $file
157f50a239bSTakamura * @param string $type
158f50a239bSTakamura * @param integer $lvl
159f50a239bSTakamura * @param array $opts
160f50a239bSTakamura *
161f50a239bSTakamura * @return bool
162f3f0262cSandi */
163f3f0262cSandifunction search_namespaces(&$data,$base,$file,$type,$lvl,$opts){
164*24870174SAndreas Gohr    $opts = ['listdirs' => true];
1658705cc81SAndreas Gohr    return search_universal($data,$base,$file,$type,$lvl,$opts);
166f3f0262cSandi}
167f3f0262cSandi
168f3f0262cSandi/**
16915fae107Sandi * List all mediafiles in a namespace
17042ea7f44SGerrit Uitslag *   $opts['depth']     recursion level, 0 for all
17142ea7f44SGerrit Uitslag *   $opts['showmsg']   shows message if invalid media id is used
17242ea7f44SGerrit Uitslag *   $opts['skipacl']   skip acl checking
17342ea7f44SGerrit Uitslag *   $opts['pattern']   check given pattern
17442ea7f44SGerrit Uitslag *   $opts['hash']      add hashes to result list
17515fae107Sandi *
17615fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
177f50a239bSTakamura *
178f50a239bSTakamura * @param array $data
179f50a239bSTakamura * @param string $base
180f50a239bSTakamura * @param string $file
181f50a239bSTakamura * @param string $type
182f50a239bSTakamura * @param integer $lvl
183f50a239bSTakamura * @param array $opts
184f50a239bSTakamura *
185f50a239bSTakamura * @return bool
186f3f0262cSandi */
187f3f0262cSandifunction search_media(&$data,$base,$file,$type,$lvl,$opts){
188b8219d2dSAndreas Gohr
189f3f0262cSandi    //we do nothing with directories
1901a49ac65SGina Haeussge    if($type == 'd') {
1910e80bb5eSChristopher Smith        if(empty($opts['depth'])) return true; // recurse forever
19278315408SAndreas Gohr        $depth = substr_count($file,'/');
193b8219d2dSAndreas Gohr        if($depth >= $opts['depth']) return false; // depth reached
194224122cfSAndreas Gohr        return true;
1951a49ac65SGina Haeussge    }
196f3f0262cSandi
197*24870174SAndreas Gohr    $info         = [];
198156a608cSandi    $info['id']   = pathID($file,true);
19964807c84SAndreas Gohr    if($info['id'] != cleanID($info['id'])){
2002b9be456SAndreas Gohr        if(!empty($opts['showmsg']))
20164807c84SAndreas Gohr            msg(hsc($info['id']).' is not a valid file name for DokuWiki - skipped',-1);
20264807c84SAndreas Gohr        return false; // skip non-valid files
20364807c84SAndreas Gohr    }
204f3f0262cSandi
205f3f0262cSandi    //check ACL for namespace (we have no ACL for mediafiles)
206224122cfSAndreas Gohr    $info['perm'] = auth_quickaclcheck(getNS($info['id']).':*');
2070e80bb5eSChristopher Smith    if(empty($opts['skipacl']) && $info['perm'] < AUTH_READ){
208224122cfSAndreas Gohr        return false;
209224122cfSAndreas Gohr    }
210224122cfSAndreas Gohr
211224122cfSAndreas Gohr    //check pattern filter
2120e80bb5eSChristopher Smith    if(!empty($opts['pattern']) && !@preg_match($opts['pattern'], $info['id'])){
213f3f0262cSandi        return false;
214f3f0262cSandi    }
215f3f0262cSandi
216*24870174SAndreas Gohr    $info['file']     = PhpString::basename($file);
217f3f0262cSandi    $info['size']     = filesize($base.'/'.$file);
2185e7fa82eSAndreas Gohr    $info['mtime']    = filemtime($base.'/'.$file);
2193df72098SAndreas Gohr    $info['writable'] = is_writable($base.'/'.$file);
220f3f0262cSandi    if(preg_match("/\.(jpe?g|gif|png)$/",$file)){
221f3f0262cSandi        $info['isimg'] = true;
22223a34783SAndreas Gohr        $info['meta']  = new JpegMeta($base.'/'.$file);
223f3f0262cSandi    }else{
224f3f0262cSandi        $info['isimg'] = false;
225f3f0262cSandi    }
2260e80bb5eSChristopher Smith    if(!empty($opts['hash'])){
227dfd343c4SAndreas Gohr        $info['hash'] = md5(io_readFile(mediaFN($info['id']),false));
228224122cfSAndreas Gohr    }
229224122cfSAndreas Gohr
230f3f0262cSandi    $data[] = $info;
231f3f0262cSandi
232f3f0262cSandi    return false;
233f3f0262cSandi}
234f3f0262cSandi
235f3f0262cSandi/**
2364f33babfSAndreas Gohr * List all mediafiles in a namespace
2374f33babfSAndreas Gohr *   $opts['depth']     recursion level, 0 for all
2384f33babfSAndreas Gohr *   $opts['showmsg']   shows message if invalid media id is used
2394f33babfSAndreas Gohr *   $opts['skipacl']   skip acl checking
2404f33babfSAndreas Gohr *   $opts['pattern']   check given pattern
2414f33babfSAndreas Gohr *   $opts['hash']      add hashes to result list
2424f33babfSAndreas Gohr *
2434f33babfSAndreas Gohr * @todo This is a temporary copy of search_media returning a list of MediaFile intances
2444f33babfSAndreas Gohr *
2454f33babfSAndreas Gohr * @param array $data
2464f33babfSAndreas Gohr * @param string $base
2474f33babfSAndreas Gohr * @param string $file
2484f33babfSAndreas Gohr * @param string $type
2494f33babfSAndreas Gohr * @param integer $lvl
2504f33babfSAndreas Gohr * @param array $opts
2514f33babfSAndreas Gohr *
2524f33babfSAndreas Gohr * @return bool
2534f33babfSAndreas Gohr */
2544f33babfSAndreas Gohrfunction search_mediafiles(&$data,$base,$file,$type,$lvl,$opts){
2554f33babfSAndreas Gohr
2564f33babfSAndreas Gohr    //we do nothing with directories
2574f33babfSAndreas Gohr    if($type == 'd') {
2584f33babfSAndreas Gohr        if(empty($opts['depth'])) return true; // recurse forever
2594f33babfSAndreas Gohr        $depth = substr_count($file,'/');
2604f33babfSAndreas Gohr        if($depth >= $opts['depth']) return false; // depth reached
2614f33babfSAndreas Gohr        return true;
2624f33babfSAndreas Gohr    }
2634f33babfSAndreas Gohr
2644f33babfSAndreas Gohr    $id   = pathID($file,true);
2654f33babfSAndreas Gohr    if($id != cleanID($id)){
2664f33babfSAndreas Gohr        if($opts['showmsg'])
2674f33babfSAndreas Gohr            msg(hsc($id).' is not a valid file name for DokuWiki - skipped',-1);
2684f33babfSAndreas Gohr        return false; // skip non-valid files
2694f33babfSAndreas Gohr    }
2704f33babfSAndreas Gohr
2714f33babfSAndreas Gohr    //check ACL for namespace (we have no ACL for mediafiles)
2724f33babfSAndreas Gohr    $info['perm'] = auth_quickaclcheck(getNS($id).':*');
2734f33babfSAndreas Gohr    if(empty($opts['skipacl']) && $info['perm'] < AUTH_READ){
2744f33babfSAndreas Gohr        return false;
2754f33babfSAndreas Gohr    }
2764f33babfSAndreas Gohr
2774f33babfSAndreas Gohr    //check pattern filter
2784f33babfSAndreas Gohr    if(!empty($opts['pattern']) && !@preg_match($opts['pattern'], $id)){
2794f33babfSAndreas Gohr        return false;
2804f33babfSAndreas Gohr    }
2814f33babfSAndreas Gohr
282*24870174SAndreas Gohr    $data[] = new MediaFile($id);
2834f33babfSAndreas Gohr    return false;
2844f33babfSAndreas Gohr}
2854f33babfSAndreas Gohr
2864f33babfSAndreas Gohr
2874f33babfSAndreas Gohr/**
288f3f0262cSandi * This function just lists documents (for RSS namespace export)
28915fae107Sandi *
29015fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
291f50a239bSTakamura *
292f50a239bSTakamura * @param array $data
293f50a239bSTakamura * @param string $base
294f50a239bSTakamura * @param string $file
295f50a239bSTakamura * @param string $type
296f50a239bSTakamura * @param integer $lvl
297f50a239bSTakamura * @param array $opts
298f50a239bSTakamura *
299f50a239bSTakamura * @return bool
300f3f0262cSandi */
301f3f0262cSandifunction search_list(&$data,$base,$file,$type,$lvl,$opts){
302f3f0262cSandi    //we do nothing with directories
303f3f0262cSandi    if($type == 'd') return false;
3040e1a261eSMichael Klier    //only search txt files
3050e1a261eSMichael Klier    if(substr($file,-4) == '.txt'){
306f3f0262cSandi        //check ACL
307f3f0262cSandi        $id = pathID($file);
308f3f0262cSandi        if(auth_quickaclcheck($id) < AUTH_READ){
309f3f0262cSandi            return false;
310f3f0262cSandi        }
3110e1a261eSMichael Klier        $data[]['id'] = $id;
312f3f0262cSandi    }
313f3f0262cSandi    return false;
314f3f0262cSandi}
315f3f0262cSandi
316f3f0262cSandi/**
317f3f0262cSandi * Quicksearch for searching matching pagenames
318f3f0262cSandi *
319f3f0262cSandi * $opts['query'] is the search query
32015fae107Sandi *
32115fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
322f50a239bSTakamura *
323f50a239bSTakamura * @param array $data
324f50a239bSTakamura * @param string $base
325f50a239bSTakamura * @param string $file
326f50a239bSTakamura * @param string $type
327f50a239bSTakamura * @param integer $lvl
328f50a239bSTakamura * @param array $opts
329f50a239bSTakamura *
330f50a239bSTakamura * @return bool
331f3f0262cSandi */
332f3f0262cSandifunction search_pagename(&$data,$base,$file,$type,$lvl,$opts){
333f3f0262cSandi    //we do nothing with directories
334f3f0262cSandi    if($type == 'd') return true;
335f3f0262cSandi    //only search txt files
3360e1a261eSMichael Klier    if(substr($file,-4) != '.txt') return true;
337f3f0262cSandi
338f3f0262cSandi    //simple stringmatching
339396b7edbSmatthiasgrimm    if (!empty($opts['query'])){
340*24870174SAndreas Gohr        if(strpos($file,(string) $opts['query']) !== false){
341f3f0262cSandi            //check ACL
342f3f0262cSandi            $id = pathID($file);
343f3f0262cSandi            if(auth_quickaclcheck($id) < AUTH_READ){
344f3f0262cSandi                return false;
345f3f0262cSandi            }
346f3f0262cSandi            $data[]['id'] = $id;
347f3f0262cSandi        }
348396b7edbSmatthiasgrimm    }
349f3f0262cSandi    return true;
350f3f0262cSandi}
351f3f0262cSandi
352f3f0262cSandi/**
35358b6f612SAndreas Gohr * Just lists all documents
35458b6f612SAndreas Gohr *
3551fcfad4dSAndreas Gohr * $opts['depth']   recursion level, 0 for all
3561fcfad4dSAndreas Gohr * $opts['hash']    do md5 sum of content?
357224122cfSAndreas Gohr * $opts['skipacl'] list everything regardless of ACL
3581fcfad4dSAndreas Gohr *
35958b6f612SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
360f50a239bSTakamura *
361f50a239bSTakamura * @param array $data
362f50a239bSTakamura * @param string $base
363f50a239bSTakamura * @param string $file
364f50a239bSTakamura * @param string $type
365f50a239bSTakamura * @param integer $lvl
366f50a239bSTakamura * @param array $opts
367f50a239bSTakamura *
368f50a239bSTakamura * @return bool
36958b6f612SAndreas Gohr */
37058b6f612SAndreas Gohrfunction search_allpages(&$data,$base,$file,$type,$lvl,$opts){
3718451f4adSGuillaume Turri    if(isset($opts['depth']) && $opts['depth']){
372c647387eSGuillaume Turri        $parts = explode('/',ltrim($file,'/'));
3735737a81eSMichael Hamann        if(($type == 'd' && count($parts) >= $opts['depth'])
3745737a81eSMichael Hamann          || ($type != 'd' && count($parts) > $opts['depth'])){
375c647387eSGuillaume Turri            return false; // depth reached
376c647387eSGuillaume Turri        }
377c647387eSGuillaume Turri    }
378c647387eSGuillaume Turri
37958b6f612SAndreas Gohr    //we do nothing with directories
3801fcfad4dSAndreas Gohr    if($type == 'd'){
3811fcfad4dSAndreas Gohr        return true;
3821fcfad4dSAndreas Gohr    }
3831fcfad4dSAndreas Gohr
38458b6f612SAndreas Gohr    //only search txt files
3850e1a261eSMichael Klier    if(substr($file,-4) != '.txt') return true;
38658b6f612SAndreas Gohr
387*24870174SAndreas Gohr    $item = [];
3881fcfad4dSAndreas Gohr    $item['id']   = pathID($file);
38977244e70SMichael Hamann    if(empty($opts['skipacl']) && auth_quickaclcheck($item['id']) < AUTH_READ){
3901fcfad4dSAndreas Gohr        return false;
3911fcfad4dSAndreas Gohr    }
3921fcfad4dSAndreas Gohr
3931fcfad4dSAndreas Gohr    $item['rev']   = filemtime($base.'/'.$file);
394224122cfSAndreas Gohr    $item['mtime'] = $item['rev'];
3951fcfad4dSAndreas Gohr    $item['size']  = filesize($base.'/'.$file);
3968f34cf3dSMichael Große    if(!empty($opts['hash'])){
3971fcfad4dSAndreas Gohr        $item['hash'] = md5(trim(rawWiki($item['id'])));
3981fcfad4dSAndreas Gohr    }
3991fcfad4dSAndreas Gohr
4001fcfad4dSAndreas Gohr    $data[] = $item;
40158b6f612SAndreas Gohr    return true;
40258b6f612SAndreas Gohr}
40358b6f612SAndreas Gohr
404b59a406bSmatthiasgrimm/* ------------- helper functions below -------------- */
405b59a406bSmatthiasgrimm
406b59a406bSmatthiasgrimm/**
40715fae107Sandi * fulltext sort
40815fae107Sandi *
409f3f0262cSandi * Callback sort function for use with usort to sort the data
410f3f0262cSandi * structure created by search_fulltext. Sorts descending by count
41115fae107Sandi *
41215fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
413f50a239bSTakamura *
414f50a239bSTakamura * @param array $a
415f50a239bSTakamura * @param array $b
416f50a239bSTakamura *
417f50a239bSTakamura * @return int
418f3f0262cSandi */
419f3f0262cSandifunction sort_search_fulltext($a,$b){
420f3f0262cSandi    if($a['count'] > $b['count']){
421f3f0262cSandi        return -1;
422f3f0262cSandi    }elseif($a['count'] < $b['count']){
423f3f0262cSandi        return 1;
424f3f0262cSandi    }else{
4252d85e841SAndreas Gohr        return Sort::strcmp($a['id'],$b['id']);
426f3f0262cSandi    }
427f3f0262cSandi}
428f3f0262cSandi
429f3f0262cSandi/**
430f3f0262cSandi * translates a document path to an ID
43115fae107Sandi *
43215fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
43337e34a5eSandi * @todo    move to pageutils
434f50a239bSTakamura *
435f50a239bSTakamura * @param string $path
436f50a239bSTakamura * @param bool $keeptxt
437f50a239bSTakamura *
438f50a239bSTakamura * @return mixed|string
439f3f0262cSandi */
440156a608cSandifunction pathID($path,$keeptxt=false){
44149c713a3Sandi    $id = utf8_decodeFN($path);
44249c713a3Sandi    $id = str_replace('/',':',$id);
443156a608cSandi    if(!$keeptxt) $id = preg_replace('#\.txt$#','',$id);
444709b1063SAdrian Lang    $id = trim($id, ':');
445f3f0262cSandi    return $id;
446f3f0262cSandi}
447f3f0262cSandi
448340756e4Sandi
4493abeade3SAndreas Gohr/**
4503abeade3SAndreas Gohr * This is a very universal callback for the search() function, replacing
4513abeade3SAndreas Gohr * many of the former individual functions at the cost of a more complex
4523abeade3SAndreas Gohr * setup.
4533abeade3SAndreas Gohr *
4543abeade3SAndreas Gohr * How the function behaves, depends on the options passed in the $opts
4553abeade3SAndreas Gohr * array, where the following settings can be used.
4563abeade3SAndreas Gohr *
457e14fe973SGerrit Uitslag * depth      int     recursion depth. 0 for unlimited                       (default: 0)
458e14fe973SGerrit Uitslag * keeptxt    bool    keep .txt extension for IDs                            (default: false)
459e14fe973SGerrit Uitslag * listfiles  bool    include files in listing                               (default: false)
460e14fe973SGerrit Uitslag * listdirs   bool    include namespaces in listing                          (default: false)
461e14fe973SGerrit Uitslag * pagesonly  bool    restrict files to pages                                (default: false)
462e14fe973SGerrit Uitslag * skipacl    bool    do not check for READ permission                       (default: false)
463e14fe973SGerrit Uitslag * sneakyacl  bool    don't recurse into nonreadable dirs                    (default: false)
464e14fe973SGerrit Uitslag * hash       bool    create MD5 hash for files                              (default: false)
465e14fe973SGerrit Uitslag * meta       bool    return file metadata                                   (default: false)
466e14fe973SGerrit Uitslag * filematch  string  match files against this regexp                        (default: '', so accept everything)
467e14fe973SGerrit Uitslag * idmatch    string  match full ID against this regexp                      (default: '', so accept everything)
468e14fe973SGerrit Uitslag * dirmatch   string  match directory against this regexp when adding        (default: '', so accept everything)
469e14fe973SGerrit Uitslag * nsmatch    string  match namespace against this regexp when adding        (default: '', so accept everything)
470e14fe973SGerrit Uitslag * recmatch   string  match directory against this regexp when recursing     (default: '', so accept everything)
471e14fe973SGerrit Uitslag * showmsg    bool    warn about non-ID files                                (default: false)
472e14fe973SGerrit Uitslag * showhidden bool    show hidden files(e.g. by hidepages config) too        (default: false)
473e14fe973SGerrit Uitslag * firsthead  bool    return first heading for pages                         (default: false)
4743abeade3SAndreas Gohr *
475ce4301e3SGerrit Uitslag * @param array &$data  - Reference to the result data structure
476ce4301e3SGerrit Uitslag * @param string $base  - Base usually $conf['datadir']
477ce4301e3SGerrit Uitslag * @param string $file  - current file or directory relative to $base
478ce4301e3SGerrit Uitslag * @param string $type  - Type either 'd' for directory or 'f' for file
479ce4301e3SGerrit Uitslag * @param int    $lvl   - Current recursion depht
480ce4301e3SGerrit Uitslag * @param array  $opts  - option array as given to search()
481ce4301e3SGerrit Uitslag * @return bool if this directory should be traversed (true) or not (false)
482ce4301e3SGerrit Uitslag *              return value is ignored for files
483ce4301e3SGerrit Uitslag *
4843abeade3SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
4853abeade3SAndreas Gohr */
4863abeade3SAndreas Gohrfunction search_universal(&$data,$base,$file,$type,$lvl,$opts){
487*24870174SAndreas Gohr    $item   = [];
4883abeade3SAndreas Gohr    $return = true;
4893abeade3SAndreas Gohr
4903abeade3SAndreas Gohr    // get ID and check if it is a valid one
491b7a3421aSChristopher Smith    $item['id'] = pathID($file,($type == 'd' || !empty($opts['keeptxt'])));
4928537abd1SAdrian Lang    if($item['id'] != cleanID($item['id'])){
49349f299d6SChristopher Smith        if(!empty($opts['showmsg'])){
4948537abd1SAdrian Lang            msg(hsc($item['id']).' is not a valid file name for DokuWiki - skipped',-1);
495b7a3421aSChristopher Smith        }
4963abeade3SAndreas Gohr        return false; // skip non-valid files
4973abeade3SAndreas Gohr    }
4988705cc81SAndreas Gohr    $item['ns']  = getNS($item['id']);
4993abeade3SAndreas Gohr
5003abeade3SAndreas Gohr    if($type == 'd') {
5013abeade3SAndreas Gohr        // decide if to recursion into this directory is wanted
5020e80bb5eSChristopher Smith        if(empty($opts['depth'])){
5033abeade3SAndreas Gohr            $return = true; // recurse forever
5043abeade3SAndreas Gohr        }else{
5053abeade3SAndreas Gohr            $depth = substr_count($file,'/');
5063abeade3SAndreas Gohr            if($depth >= $opts['depth']){
5073abeade3SAndreas Gohr                $return = false; // depth reached
5083abeade3SAndreas Gohr            }else{
5093abeade3SAndreas Gohr                $return = true;
5103abeade3SAndreas Gohr            }
5113abeade3SAndreas Gohr        }
5129b4337c6SChristopher Smith
5139b4337c6SChristopher Smith        if ($return) {
5149b4337c6SChristopher Smith            $match = empty($opts['recmatch']) || preg_match('/'.$opts['recmatch'].'/',$file);
5159b4337c6SChristopher Smith            if (!$match) {
5169b4337c6SChristopher Smith                return false; // doesn't match
5179b4337c6SChristopher Smith            }
5183abeade3SAndreas Gohr        }
5193abeade3SAndreas Gohr    }
5203abeade3SAndreas Gohr
5213abeade3SAndreas Gohr    // check ACL
522443e135dSChristopher Smith    if(empty($opts['skipacl'])){
5233abeade3SAndreas Gohr        if($type == 'd'){
5243abeade3SAndreas Gohr            $item['perm'] = auth_quickaclcheck($item['id'].':*');
5253abeade3SAndreas Gohr        }else{
5263abeade3SAndreas Gohr            $item['perm'] = auth_quickaclcheck($item['id']); //FIXME check namespace for media files
5273abeade3SAndreas Gohr        }
5283abeade3SAndreas Gohr    }else{
5293abeade3SAndreas Gohr        $item['perm'] = AUTH_DELETE;
5303abeade3SAndreas Gohr    }
5313abeade3SAndreas Gohr
5323abeade3SAndreas Gohr    // are we done here maybe?
5333abeade3SAndreas Gohr    if($type == 'd'){
534443e135dSChristopher Smith        if(empty($opts['listdirs'])) return $return;
53564159a61SAndreas Gohr        //neither list nor recurse forbidden items:
53664159a61SAndreas Gohr        if(empty($opts['skipacl']) && !empty($opts['sneakyacl']) && $item['perm'] < AUTH_READ) return false;
537443e135dSChristopher Smith        if(!empty($opts['dirmatch']) && !preg_match('/'.$opts['dirmatch'].'/',$file)) return $return;
538443e135dSChristopher Smith        if(!empty($opts['nsmatch']) && !preg_match('/'.$opts['nsmatch'].'/',$item['ns'])) return $return;
5393abeade3SAndreas Gohr    }else{
540443e135dSChristopher Smith        if(empty($opts['listfiles'])) return $return;
541443e135dSChristopher Smith        if(empty($opts['skipacl']) && $item['perm'] < AUTH_READ) return $return;
542443e135dSChristopher Smith        if(!empty($opts['pagesonly']) && (substr($file,-4) != '.txt')) return $return;
543443e135dSChristopher Smith        if(empty($opts['showhidden']) && isHiddenPage($item['id'])) return $return;
544443e135dSChristopher Smith        if(!empty($opts['filematch']) && !preg_match('/'.$opts['filematch'].'/',$file)) return $return;
545443e135dSChristopher Smith        if(!empty($opts['idmatch']) && !preg_match('/'.$opts['idmatch'].'/',$item['id'])) return $return;
5463abeade3SAndreas Gohr    }
5473abeade3SAndreas Gohr
5483abeade3SAndreas Gohr    // still here? prepare the item
5493abeade3SAndreas Gohr    $item['type']  = $type;
55032d6093dSAndreas Gohr    $item['level'] = $lvl;
5513abeade3SAndreas Gohr    $item['open']  = $return;
5523abeade3SAndreas Gohr
5530e80bb5eSChristopher Smith    if(!empty($opts['meta'])){
554*24870174SAndreas Gohr        $item['file']       = PhpString::basename($file);
5553abeade3SAndreas Gohr        $item['size']       = filesize($base.'/'.$file);
5563abeade3SAndreas Gohr        $item['mtime']      = filemtime($base.'/'.$file);
5573abeade3SAndreas Gohr        $item['rev']        = $item['mtime'];
5583abeade3SAndreas Gohr        $item['writable']   = is_writable($base.'/'.$file);
5593abeade3SAndreas Gohr        $item['executable'] = is_executable($base.'/'.$file);
5603abeade3SAndreas Gohr    }
5613abeade3SAndreas Gohr
5623abeade3SAndreas Gohr    if($type == 'f'){
5630e80bb5eSChristopher Smith        if(!empty($opts['hash'])) $item['hash'] = md5(io_readFile($base.'/'.$file,false));
5640e80bb5eSChristopher Smith        if(!empty($opts['firsthead'])) $item['title'] = p_get_first_heading($item['id'],METADATA_DONT_RENDER);
5653abeade3SAndreas Gohr    }
5663abeade3SAndreas Gohr
5673abeade3SAndreas Gohr    // finally add the item
5683abeade3SAndreas Gohr    $data[] = $item;
5693abeade3SAndreas Gohr    return $return;
5703abeade3SAndreas Gohr}
5713abeade3SAndreas Gohr
572e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
573