xref: /dokuwiki/inc/search.php (revision 2d85e84158bfbe6ff83458824a787ddcc12db9c8)
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
9*2d85e841SAndreas Gohruse dokuwiki\Utf8\Sort;
10*2d85e841SAndreas Gohr
11f3f0262cSandi/**
12ce4301e3SGerrit Uitslag * Recurse directory
1315fae107Sandi *
14f3f0262cSandi * This function recurses into a given base directory
15f3f0262cSandi * and calls the supplied function for each file and directory
1615fae107Sandi *
1724998b31SGerrit Uitslag * @param   array    &$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)
2024998b31SGerrit Uitslag * @param   array     $opts option array will be given to the Callback
2124baa045SAndreas Gohr * @param   string    $dir  Current directory beyond $base
2224baa045SAndreas Gohr * @param   int       $lvl  Recursion Level
2364159a61SAndreas Gohr * @param   mixed     $sort 'natural' to use natural order sorting (default);
2464159a61SAndreas Gohr *                          'date' to sort by filemtime; leave empty to skip sorting.
2515fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
26f3f0262cSandi */
27155e63c9SChristopher Smithfunction search(&$data,$base,$func,$opts,$dir='',$lvl=1,$sort='natural'){
28f3f0262cSandi    $dirs   = array();
29f3f0262cSandi    $files  = array();
30abc306f4SKate Arzamastseva    $filepaths = array();
31f3f0262cSandi
32e0b6aadeSAndreas Gohr    // safeguard against runaways #1452
33e0b6aadeSAndreas Gohr    if($base == '' || $base == '/') {
34e0b6aadeSAndreas Gohr        throw new RuntimeException('No valid $base passed to search() - possible misconfiguration or bug');
35e0b6aadeSAndreas Gohr    }
36e0b6aadeSAndreas Gohr
37f3f0262cSandi    //read in directories and files
38f3f0262cSandi    $dh = @opendir($base.'/'.$dir);
39f3f0262cSandi    if(!$dh) return;
40f3f0262cSandi    while(($file = readdir($dh)) !== false){
41de3dfc91Sandi        if(preg_match('/^[\._]/',$file)) continue; //skip hidden files and upper dirs
42f3f0262cSandi        if(is_dir($base.'/'.$dir.'/'.$file)){
43f3f0262cSandi            $dirs[] = $dir.'/'.$file;
44f3f0262cSandi            continue;
45f3f0262cSandi        }
46f3f0262cSandi        $files[] = $dir.'/'.$file;
47abc306f4SKate Arzamastseva        $filepaths[] = $base.'/'.$dir.'/'.$file;
48f3f0262cSandi    }
49f3f0262cSandi    closedir($dh);
50ec24a2dfSPhilipp A. Hartmann    if (!empty($sort)) {
51abc306f4SKate Arzamastseva        if ($sort == 'date') {
52d971ea8bSKate Arzamastseva            @array_multisort(array_map('filemtime', $filepaths), SORT_NUMERIC, SORT_DESC, $files);
531dc5d48bSChristopher Smith        } else /* natural */ {
54*2d85e841SAndreas Gohr            Sort::asortFN($files);
55abc306f4SKate Arzamastseva        }
56*2d85e841SAndreas Gohr        Sort::asortFN($dirs);
57ec24a2dfSPhilipp A. Hartmann    }
58f3f0262cSandi
59f3f0262cSandi    //give directories to userfunction then recurse
60f3f0262cSandi    foreach($dirs as $dir){
61d8126df2SGina Haeussge        if (call_user_func_array($func, array(&$data,$base,$dir,'d',$lvl,$opts))){
625514a5a7SChristopher Smith            search($data,$base,$func,$opts,$dir,$lvl+1,$sort);
63f3f0262cSandi        }
64f3f0262cSandi    }
65f3f0262cSandi    //now handle the files
66f3f0262cSandi    foreach($files as $file){
67d8126df2SGina Haeussge        call_user_func_array($func, array(&$data,$base,$file,'f',$lvl,$opts));
68f3f0262cSandi    }
69f3f0262cSandi}
70f3f0262cSandi
71f3f0262cSandi/**
72f3f0262cSandi * The following functions are userfunctions to use with the search
73f3f0262cSandi * function above. This function is called for every found file or
74f3f0262cSandi * directory. When a directory is given to the function it has to
75f3f0262cSandi * decide if this directory should be traversed (true) or not (false)
76f3f0262cSandi * The function has to accept the following parameters:
77f3f0262cSandi *
78ce4301e3SGerrit Uitslag * array &$data  - Reference to the result data structure
79ce4301e3SGerrit Uitslag * string $base  - Base usually $conf['datadir']
80ce4301e3SGerrit Uitslag * string $file  - current file or directory relative to $base
81ce4301e3SGerrit Uitslag * string $type  - Type either 'd' for directory or 'f' for file
82ce4301e3SGerrit Uitslag * int    $lvl   - Current recursion depht
83ce4301e3SGerrit Uitslag * array  $opts  - option array as given to search()
84f3f0262cSandi *
85f3f0262cSandi * return values for files are ignored
86f3f0262cSandi *
87f3f0262cSandi * All functions should check the ACL for document READ rights
88783d2e49SAdrian Lang * namespaces (directories) are NOT checked (when sneaky_index is 0) as this
89783d2e49SAdrian Lang * would break the recursion (You can have an nonreadable dir over a readable
900e1a261eSMichael Klier * one deeper nested) also make sure to check the file type (for example
910e1a261eSMichael Klier * in case of lockfiles).
92f3f0262cSandi */
93f3f0262cSandi
94f3f0262cSandi/**
9563f2400bSandi * Searches for pages beginning with the given query
9663f2400bSandi *
9763f2400bSandi * @author Andreas Gohr <andi@splitbrain.org>
98f50a239bSTakamura *
99f50a239bSTakamura * @param array $data
100f50a239bSTakamura * @param string $base
101f50a239bSTakamura * @param string $file
102f50a239bSTakamura * @param string $type
103f50a239bSTakamura * @param integer $lvl
104f50a239bSTakamura * @param array $opts
105f50a239bSTakamura *
106f50a239bSTakamura * @return bool
10763f2400bSandi */
10863f2400bSandifunction search_qsearch(&$data,$base,$file,$type,$lvl,$opts){
1098705cc81SAndreas Gohr    $opts = array(
1108705cc81SAndreas Gohr            'idmatch'   => '(^|:)'.preg_quote($opts['query'],'/').'/',
1118705cc81SAndreas Gohr            'listfiles' => true,
1128705cc81SAndreas Gohr            'pagesonly' => true,
1138705cc81SAndreas Gohr            );
1148705cc81SAndreas Gohr    return search_universal($data,$base,$file,$type,$lvl,$opts);
11563f2400bSandi}
11663f2400bSandi
11763f2400bSandi/**
11815fae107Sandi * Build the browsable index of pages
119f3f0262cSandi *
120783d2e49SAdrian Lang * $opts['ns'] is the currently viewed namespace
12115fae107Sandi *
12215fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
123f50a239bSTakamura *
124f50a239bSTakamura * @param array $data
125f50a239bSTakamura * @param string $base
126f50a239bSTakamura * @param string $file
127f50a239bSTakamura * @param string $type
128f50a239bSTakamura * @param integer $lvl
129f50a239bSTakamura * @param array $opts
130f50a239bSTakamura *
131f50a239bSTakamura * @return bool
132f3f0262cSandi */
133f3f0262cSandifunction search_index(&$data,$base,$file,$type,$lvl,$opts){
134d1c7b6ecSAndreas Gohr    global $conf;
135783d2e49SAdrian Lang    $opts = array(
136783d2e49SAdrian Lang        'pagesonly' => true,
137783d2e49SAdrian Lang        'listdirs' => true,
138443e135dSChristopher Smith        'listfiles' => empty($opts['nofiles']),
139783d2e49SAdrian Lang        'sneakyacl' => $conf['sneaky_index'],
140783d2e49SAdrian Lang        // Hacky, should rather use recmatch
1411c6c1c6cSMichael Hamann        'depth' => preg_match('#^'.preg_quote($file, '#').'(/|$)#','/'.$opts['ns']) ? 0 : -1
142783d2e49SAdrian Lang    );
143f3f0262cSandi
144783d2e49SAdrian Lang    return search_universal($data, $base, $file, $type, $lvl, $opts);
145f3f0262cSandi}
146f3f0262cSandi
147f3f0262cSandi/**
14815fae107Sandi * List all namespaces
14915fae107Sandi *
15015fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
151f50a239bSTakamura *
152f50a239bSTakamura * @param array $data
153f50a239bSTakamura * @param string $base
154f50a239bSTakamura * @param string $file
155f50a239bSTakamura * @param string $type
156f50a239bSTakamura * @param integer $lvl
157f50a239bSTakamura * @param array $opts
158f50a239bSTakamura *
159f50a239bSTakamura * @return bool
160f3f0262cSandi */
161f3f0262cSandifunction search_namespaces(&$data,$base,$file,$type,$lvl,$opts){
1628705cc81SAndreas Gohr    $opts = array(
1638705cc81SAndreas Gohr            'listdirs' => true,
1648705cc81SAndreas Gohr            );
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
197f3f0262cSandi    $info         = array();
198156a608cSandi    $info['id']   = pathID($file,true);
19964807c84SAndreas Gohr    if($info['id'] != cleanID($info['id'])){
20064807c84SAndreas Gohr        if($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
2168cbc5ee8SAndreas Gohr    $info['file']     = \dokuwiki\Utf8\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/**
236f3f0262cSandi * This function just lists documents (for RSS namespace export)
23715fae107Sandi *
23815fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
239f50a239bSTakamura *
240f50a239bSTakamura * @param array $data
241f50a239bSTakamura * @param string $base
242f50a239bSTakamura * @param string $file
243f50a239bSTakamura * @param string $type
244f50a239bSTakamura * @param integer $lvl
245f50a239bSTakamura * @param array $opts
246f50a239bSTakamura *
247f50a239bSTakamura * @return bool
248f3f0262cSandi */
249f3f0262cSandifunction search_list(&$data,$base,$file,$type,$lvl,$opts){
250f3f0262cSandi    //we do nothing with directories
251f3f0262cSandi    if($type == 'd') return false;
2520e1a261eSMichael Klier    //only search txt files
2530e1a261eSMichael Klier    if(substr($file,-4) == '.txt'){
254f3f0262cSandi        //check ACL
255f3f0262cSandi        $id = pathID($file);
256f3f0262cSandi        if(auth_quickaclcheck($id) < AUTH_READ){
257f3f0262cSandi            return false;
258f3f0262cSandi        }
2590e1a261eSMichael Klier        $data[]['id'] = $id;
260f3f0262cSandi    }
261f3f0262cSandi    return false;
262f3f0262cSandi}
263f3f0262cSandi
264f3f0262cSandi/**
265f3f0262cSandi * Quicksearch for searching matching pagenames
266f3f0262cSandi *
267f3f0262cSandi * $opts['query'] is the search query
26815fae107Sandi *
26915fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
270f50a239bSTakamura *
271f50a239bSTakamura * @param array $data
272f50a239bSTakamura * @param string $base
273f50a239bSTakamura * @param string $file
274f50a239bSTakamura * @param string $type
275f50a239bSTakamura * @param integer $lvl
276f50a239bSTakamura * @param array $opts
277f50a239bSTakamura *
278f50a239bSTakamura * @return bool
279f3f0262cSandi */
280f3f0262cSandifunction search_pagename(&$data,$base,$file,$type,$lvl,$opts){
281f3f0262cSandi    //we do nothing with directories
282f3f0262cSandi    if($type == 'd') return true;
283f3f0262cSandi    //only search txt files
2840e1a261eSMichael Klier    if(substr($file,-4) != '.txt') return true;
285f3f0262cSandi
286f3f0262cSandi    //simple stringmatching
287396b7edbSmatthiasgrimm    if (!empty($opts['query'])){
288f3f0262cSandi        if(strpos($file,$opts['query']) !== false){
289f3f0262cSandi            //check ACL
290f3f0262cSandi            $id = pathID($file);
291f3f0262cSandi            if(auth_quickaclcheck($id) < AUTH_READ){
292f3f0262cSandi                return false;
293f3f0262cSandi            }
294f3f0262cSandi            $data[]['id'] = $id;
295f3f0262cSandi        }
296396b7edbSmatthiasgrimm    }
297f3f0262cSandi    return true;
298f3f0262cSandi}
299f3f0262cSandi
300f3f0262cSandi/**
30158b6f612SAndreas Gohr * Just lists all documents
30258b6f612SAndreas Gohr *
3031fcfad4dSAndreas Gohr * $opts['depth']   recursion level, 0 for all
3041fcfad4dSAndreas Gohr * $opts['hash']    do md5 sum of content?
305224122cfSAndreas Gohr * $opts['skipacl'] list everything regardless of ACL
3061fcfad4dSAndreas Gohr *
30758b6f612SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
308f50a239bSTakamura *
309f50a239bSTakamura * @param array $data
310f50a239bSTakamura * @param string $base
311f50a239bSTakamura * @param string $file
312f50a239bSTakamura * @param string $type
313f50a239bSTakamura * @param integer $lvl
314f50a239bSTakamura * @param array $opts
315f50a239bSTakamura *
316f50a239bSTakamura * @return bool
31758b6f612SAndreas Gohr */
31858b6f612SAndreas Gohrfunction search_allpages(&$data,$base,$file,$type,$lvl,$opts){
3198451f4adSGuillaume Turri    if(isset($opts['depth']) && $opts['depth']){
320c647387eSGuillaume Turri        $parts = explode('/',ltrim($file,'/'));
3215737a81eSMichael Hamann        if(($type == 'd' && count($parts) >= $opts['depth'])
3225737a81eSMichael Hamann          || ($type != 'd' && count($parts) > $opts['depth'])){
323c647387eSGuillaume Turri            return false; // depth reached
324c647387eSGuillaume Turri        }
325c647387eSGuillaume Turri    }
326c647387eSGuillaume Turri
32758b6f612SAndreas Gohr    //we do nothing with directories
3281fcfad4dSAndreas Gohr    if($type == 'd'){
3291fcfad4dSAndreas Gohr        return true;
3301fcfad4dSAndreas Gohr    }
3311fcfad4dSAndreas Gohr
33258b6f612SAndreas Gohr    //only search txt files
3330e1a261eSMichael Klier    if(substr($file,-4) != '.txt') return true;
33458b6f612SAndreas Gohr
33559bc3b48SGerrit Uitslag    $item = array();
3361fcfad4dSAndreas Gohr    $item['id']   = pathID($file);
33777244e70SMichael Hamann    if(empty($opts['skipacl']) && auth_quickaclcheck($item['id']) < AUTH_READ){
3381fcfad4dSAndreas Gohr        return false;
3391fcfad4dSAndreas Gohr    }
3401fcfad4dSAndreas Gohr
3411fcfad4dSAndreas Gohr    $item['rev']   = filemtime($base.'/'.$file);
342224122cfSAndreas Gohr    $item['mtime'] = $item['rev'];
3431fcfad4dSAndreas Gohr    $item['size']  = filesize($base.'/'.$file);
3448f34cf3dSMichael Große    if(!empty($opts['hash'])){
3451fcfad4dSAndreas Gohr        $item['hash'] = md5(trim(rawWiki($item['id'])));
3461fcfad4dSAndreas Gohr    }
3471fcfad4dSAndreas Gohr
3481fcfad4dSAndreas Gohr    $data[] = $item;
34958b6f612SAndreas Gohr    return true;
35058b6f612SAndreas Gohr}
35158b6f612SAndreas Gohr
352b59a406bSmatthiasgrimm/* ------------- helper functions below -------------- */
353b59a406bSmatthiasgrimm
354b59a406bSmatthiasgrimm/**
35515fae107Sandi * fulltext sort
35615fae107Sandi *
357f3f0262cSandi * Callback sort function for use with usort to sort the data
358f3f0262cSandi * structure created by search_fulltext. Sorts descending by count
35915fae107Sandi *
36015fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
361f50a239bSTakamura *
362f50a239bSTakamura * @param array $a
363f50a239bSTakamura * @param array $b
364f50a239bSTakamura *
365f50a239bSTakamura * @return int
366f3f0262cSandi */
367f3f0262cSandifunction sort_search_fulltext($a,$b){
368f3f0262cSandi    if($a['count'] > $b['count']){
369f3f0262cSandi        return -1;
370f3f0262cSandi    }elseif($a['count'] < $b['count']){
371f3f0262cSandi        return 1;
372f3f0262cSandi    }else{
373*2d85e841SAndreas Gohr        return Sort::strcmp($a['id'],$b['id']);
374f3f0262cSandi    }
375f3f0262cSandi}
376f3f0262cSandi
377f3f0262cSandi/**
378f3f0262cSandi * translates a document path to an ID
37915fae107Sandi *
38015fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
38137e34a5eSandi * @todo    move to pageutils
382f50a239bSTakamura *
383f50a239bSTakamura * @param string $path
384f50a239bSTakamura * @param bool $keeptxt
385f50a239bSTakamura *
386f50a239bSTakamura * @return mixed|string
387f3f0262cSandi */
388156a608cSandifunction pathID($path,$keeptxt=false){
38949c713a3Sandi    $id = utf8_decodeFN($path);
39049c713a3Sandi    $id = str_replace('/',':',$id);
391156a608cSandi    if(!$keeptxt) $id = preg_replace('#\.txt$#','',$id);
392709b1063SAdrian Lang    $id = trim($id, ':');
393f3f0262cSandi    return $id;
394f3f0262cSandi}
395f3f0262cSandi
396340756e4Sandi
3973abeade3SAndreas Gohr/**
3983abeade3SAndreas Gohr * This is a very universal callback for the search() function, replacing
3993abeade3SAndreas Gohr * many of the former individual functions at the cost of a more complex
4003abeade3SAndreas Gohr * setup.
4013abeade3SAndreas Gohr *
4023abeade3SAndreas Gohr * How the function behaves, depends on the options passed in the $opts
4033abeade3SAndreas Gohr * array, where the following settings can be used.
4043abeade3SAndreas Gohr *
405e14fe973SGerrit Uitslag * depth      int     recursion depth. 0 for unlimited                       (default: 0)
406e14fe973SGerrit Uitslag * keeptxt    bool    keep .txt extension for IDs                            (default: false)
407e14fe973SGerrit Uitslag * listfiles  bool    include files in listing                               (default: false)
408e14fe973SGerrit Uitslag * listdirs   bool    include namespaces in listing                          (default: false)
409e14fe973SGerrit Uitslag * pagesonly  bool    restrict files to pages                                (default: false)
410e14fe973SGerrit Uitslag * skipacl    bool    do not check for READ permission                       (default: false)
411e14fe973SGerrit Uitslag * sneakyacl  bool    don't recurse into nonreadable dirs                    (default: false)
412e14fe973SGerrit Uitslag * hash       bool    create MD5 hash for files                              (default: false)
413e14fe973SGerrit Uitslag * meta       bool    return file metadata                                   (default: false)
414e14fe973SGerrit Uitslag * filematch  string  match files against this regexp                        (default: '', so accept everything)
415e14fe973SGerrit Uitslag * idmatch    string  match full ID against this regexp                      (default: '', so accept everything)
416e14fe973SGerrit Uitslag * dirmatch   string  match directory against this regexp when adding        (default: '', so accept everything)
417e14fe973SGerrit Uitslag * nsmatch    string  match namespace against this regexp when adding        (default: '', so accept everything)
418e14fe973SGerrit Uitslag * recmatch   string  match directory against this regexp when recursing     (default: '', so accept everything)
419e14fe973SGerrit Uitslag * showmsg    bool    warn about non-ID files                                (default: false)
420e14fe973SGerrit Uitslag * showhidden bool    show hidden files(e.g. by hidepages config) too        (default: false)
421e14fe973SGerrit Uitslag * firsthead  bool    return first heading for pages                         (default: false)
4223abeade3SAndreas Gohr *
423ce4301e3SGerrit Uitslag * @param array &$data  - Reference to the result data structure
424ce4301e3SGerrit Uitslag * @param string $base  - Base usually $conf['datadir']
425ce4301e3SGerrit Uitslag * @param string $file  - current file or directory relative to $base
426ce4301e3SGerrit Uitslag * @param string $type  - Type either 'd' for directory or 'f' for file
427ce4301e3SGerrit Uitslag * @param int    $lvl   - Current recursion depht
428ce4301e3SGerrit Uitslag * @param array  $opts  - option array as given to search()
429ce4301e3SGerrit Uitslag * @return bool if this directory should be traversed (true) or not (false)
430ce4301e3SGerrit Uitslag *              return value is ignored for files
431ce4301e3SGerrit Uitslag *
4323abeade3SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
4333abeade3SAndreas Gohr */
4343abeade3SAndreas Gohrfunction search_universal(&$data,$base,$file,$type,$lvl,$opts){
4353abeade3SAndreas Gohr    $item   = array();
4363abeade3SAndreas Gohr    $return = true;
4373abeade3SAndreas Gohr
4383abeade3SAndreas Gohr    // get ID and check if it is a valid one
439b7a3421aSChristopher Smith    $item['id'] = pathID($file,($type == 'd' || !empty($opts['keeptxt'])));
4408537abd1SAdrian Lang    if($item['id'] != cleanID($item['id'])){
44149f299d6SChristopher Smith        if(!empty($opts['showmsg'])){
4428537abd1SAdrian Lang            msg(hsc($item['id']).' is not a valid file name for DokuWiki - skipped',-1);
443b7a3421aSChristopher Smith        }
4443abeade3SAndreas Gohr        return false; // skip non-valid files
4453abeade3SAndreas Gohr    }
4468705cc81SAndreas Gohr    $item['ns']  = getNS($item['id']);
4473abeade3SAndreas Gohr
4483abeade3SAndreas Gohr    if($type == 'd') {
4493abeade3SAndreas Gohr        // decide if to recursion into this directory is wanted
4500e80bb5eSChristopher Smith        if(empty($opts['depth'])){
4513abeade3SAndreas Gohr            $return = true; // recurse forever
4523abeade3SAndreas Gohr        }else{
4533abeade3SAndreas Gohr            $depth = substr_count($file,'/');
4543abeade3SAndreas Gohr            if($depth >= $opts['depth']){
4553abeade3SAndreas Gohr                $return = false; // depth reached
4563abeade3SAndreas Gohr            }else{
4573abeade3SAndreas Gohr                $return = true;
4583abeade3SAndreas Gohr            }
4593abeade3SAndreas Gohr        }
4609b4337c6SChristopher Smith
4619b4337c6SChristopher Smith        if ($return) {
4629b4337c6SChristopher Smith            $match = empty($opts['recmatch']) || preg_match('/'.$opts['recmatch'].'/',$file);
4639b4337c6SChristopher Smith            if (!$match) {
4649b4337c6SChristopher Smith                return false; // doesn't match
4659b4337c6SChristopher Smith            }
4663abeade3SAndreas Gohr        }
4673abeade3SAndreas Gohr    }
4683abeade3SAndreas Gohr
4693abeade3SAndreas Gohr    // check ACL
470443e135dSChristopher Smith    if(empty($opts['skipacl'])){
4713abeade3SAndreas Gohr        if($type == 'd'){
4723abeade3SAndreas Gohr            $item['perm'] = auth_quickaclcheck($item['id'].':*');
4733abeade3SAndreas Gohr        }else{
4743abeade3SAndreas Gohr            $item['perm'] = auth_quickaclcheck($item['id']); //FIXME check namespace for media files
4753abeade3SAndreas Gohr        }
4763abeade3SAndreas Gohr    }else{
4773abeade3SAndreas Gohr        $item['perm'] = AUTH_DELETE;
4783abeade3SAndreas Gohr    }
4793abeade3SAndreas Gohr
4803abeade3SAndreas Gohr    // are we done here maybe?
4813abeade3SAndreas Gohr    if($type == 'd'){
482443e135dSChristopher Smith        if(empty($opts['listdirs'])) return $return;
48364159a61SAndreas Gohr        //neither list nor recurse forbidden items:
48464159a61SAndreas Gohr        if(empty($opts['skipacl']) && !empty($opts['sneakyacl']) && $item['perm'] < AUTH_READ) return false;
485443e135dSChristopher Smith        if(!empty($opts['dirmatch']) && !preg_match('/'.$opts['dirmatch'].'/',$file)) return $return;
486443e135dSChristopher Smith        if(!empty($opts['nsmatch']) && !preg_match('/'.$opts['nsmatch'].'/',$item['ns'])) return $return;
4873abeade3SAndreas Gohr    }else{
488443e135dSChristopher Smith        if(empty($opts['listfiles'])) return $return;
489443e135dSChristopher Smith        if(empty($opts['skipacl']) && $item['perm'] < AUTH_READ) return $return;
490443e135dSChristopher Smith        if(!empty($opts['pagesonly']) && (substr($file,-4) != '.txt')) return $return;
491443e135dSChristopher Smith        if(empty($opts['showhidden']) && isHiddenPage($item['id'])) return $return;
492443e135dSChristopher Smith        if(!empty($opts['filematch']) && !preg_match('/'.$opts['filematch'].'/',$file)) return $return;
493443e135dSChristopher Smith        if(!empty($opts['idmatch']) && !preg_match('/'.$opts['idmatch'].'/',$item['id'])) return $return;
4943abeade3SAndreas Gohr    }
4953abeade3SAndreas Gohr
4963abeade3SAndreas Gohr    // still here? prepare the item
4973abeade3SAndreas Gohr    $item['type']  = $type;
49832d6093dSAndreas Gohr    $item['level'] = $lvl;
4993abeade3SAndreas Gohr    $item['open']  = $return;
5003abeade3SAndreas Gohr
5010e80bb5eSChristopher Smith    if(!empty($opts['meta'])){
5028cbc5ee8SAndreas Gohr        $item['file']       = \dokuwiki\Utf8\PhpString::basename($file);
5033abeade3SAndreas Gohr        $item['size']       = filesize($base.'/'.$file);
5043abeade3SAndreas Gohr        $item['mtime']      = filemtime($base.'/'.$file);
5053abeade3SAndreas Gohr        $item['rev']        = $item['mtime'];
5063abeade3SAndreas Gohr        $item['writable']   = is_writable($base.'/'.$file);
5073abeade3SAndreas Gohr        $item['executable'] = is_executable($base.'/'.$file);
5083abeade3SAndreas Gohr    }
5093abeade3SAndreas Gohr
5103abeade3SAndreas Gohr    if($type == 'f'){
5110e80bb5eSChristopher Smith        if(!empty($opts['hash'])) $item['hash'] = md5(io_readFile($base.'/'.$file,false));
5120e80bb5eSChristopher Smith        if(!empty($opts['firsthead'])) $item['title'] = p_get_first_heading($item['id'],METADATA_DONT_RENDER);
5133abeade3SAndreas Gohr    }
5143abeade3SAndreas Gohr
5153abeade3SAndreas Gohr    // finally add the item
5163abeade3SAndreas Gohr    $data[] = $item;
5173abeade3SAndreas Gohr    return $return;
5183abeade3SAndreas Gohr}
5193abeade3SAndreas Gohr
520e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
521