xref: /dokuwiki/inc/search.php (revision 83198f922207b439a43f2533a6dc7ef855f802ba)
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
9f3f0262cSandi/**
10ce4301e3SGerrit Uitslag * Recurse directory
1115fae107Sandi *
12f3f0262cSandi * This function recurses into a given base directory
13f3f0262cSandi * and calls the supplied function for each file and directory
1415fae107Sandi *
1524998b31SGerrit Uitslag * @param   array    &$data The results of the search are stored here
1624baa045SAndreas Gohr * @param   string    $base Where to start the search
17fe82d751SChristopher Smith * @param   callback  $func Callback (function name or array with object,method)
1824998b31SGerrit Uitslag * @param   array     $opts option array will be given to the Callback
1924baa045SAndreas Gohr * @param   string    $dir  Current directory beyond $base
2024baa045SAndreas Gohr * @param   int       $lvl  Recursion Level
2164159a61SAndreas Gohr * @param   mixed     $sort 'natural' to use natural order sorting (default);
2264159a61SAndreas Gohr *                          'date' to sort by filemtime; leave empty to skip sorting.
2315fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
24f3f0262cSandi */
25*83198f92SSatoshi Saharafunction search(&$data, $base, $func, $opts, $dir='', $lvl=1, $sort='natural')
26*83198f92SSatoshi Sahara{
27f3f0262cSandi    $dirs   = array();
28f3f0262cSandi    $files  = array();
29abc306f4SKate Arzamastseva    $filepaths = array();
30f3f0262cSandi
31e0b6aadeSAndreas Gohr    // safeguard against runaways #1452
32e0b6aadeSAndreas Gohr    if ($base == '' || $base == '/') {
33e0b6aadeSAndreas Gohr        throw new RuntimeException('No valid $base passed to search() - possible misconfiguration or bug');
34e0b6aadeSAndreas Gohr    }
35e0b6aadeSAndreas Gohr
36f3f0262cSandi    //read in directories and files
37f3f0262cSandi    $dh = @opendir($base.'/'.$dir);
38f3f0262cSandi    if (!$dh) return;
39f3f0262cSandi    while (($file = readdir($dh)) !== false) {
40de3dfc91Sandi        if (preg_match('/^[\._]/', $file)) continue; //skip hidden files and upper dirs
41f3f0262cSandi        if (is_dir($base.'/'.$dir.'/'.$file)) {
42f3f0262cSandi            $dirs[] = $dir.'/'.$file;
43f3f0262cSandi            continue;
44f3f0262cSandi        }
45f3f0262cSandi        $files[] = $dir.'/'.$file;
46abc306f4SKate Arzamastseva        $filepaths[] = $base.'/'.$dir.'/'.$file;
47f3f0262cSandi    }
48f3f0262cSandi    closedir($dh);
49ec24a2dfSPhilipp A. Hartmann    if (!empty($sort)) {
50abc306f4SKate Arzamastseva        if ($sort == 'date') {
51d971ea8bSKate Arzamastseva            @array_multisort(array_map('filemtime', $filepaths), SORT_NUMERIC, SORT_DESC, $files);
521dc5d48bSChristopher Smith        } else /* natural */ {
531dc5d48bSChristopher Smith            natsort($files);
54abc306f4SKate Arzamastseva        }
551dc5d48bSChristopher Smith        natsort($dirs);
56ec24a2dfSPhilipp A. Hartmann    }
57f3f0262cSandi
58f3f0262cSandi    //give directories to userfunction then recurse
59f3f0262cSandi    foreach ($dirs as $dir) {
60d8126df2SGina Haeussge        if (call_user_func_array($func, array(&$data, $base, $dir, 'd', $lvl,$opts))) {
615514a5a7SChristopher Smith            search($data, $base, $func, $opts, $dir, $lvl+1, $sort);
62f3f0262cSandi        }
63f3f0262cSandi    }
64f3f0262cSandi    //now handle the files
65f3f0262cSandi    foreach ($files as $file) {
66d8126df2SGina Haeussge        call_user_func_array($func, array(&$data, $base, $file, 'f', $lvl, $opts));
67f3f0262cSandi    }
68f3f0262cSandi}
69f3f0262cSandi
70f3f0262cSandi/**
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 *
77ce4301e3SGerrit Uitslag * array &$data  - Reference to the result data structure
78ce4301e3SGerrit Uitslag * string $base  - Base usually $conf['datadir']
79ce4301e3SGerrit Uitslag * string $file  - current file or directory relative to $base
80ce4301e3SGerrit Uitslag * string $type  - Type either 'd' for directory or 'f' for file
81ce4301e3SGerrit Uitslag * int    $lvl   - Current recursion depht
82ce4301e3SGerrit Uitslag * array  $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
87783d2e49SAdrian Lang * namespaces (directories) are NOT checked (when sneaky_index is 0) as this
88783d2e49SAdrian Lang * would break 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>
97f50a239bSTakamura *
98f50a239bSTakamura * @param array $data
99f50a239bSTakamura * @param string $base
100f50a239bSTakamura * @param string $file
101f50a239bSTakamura * @param string $type
102f50a239bSTakamura * @param integer $lvl
103f50a239bSTakamura * @param array $opts
104f50a239bSTakamura *
105f50a239bSTakamura * @return bool
10663f2400bSandi */
107*83198f92SSatoshi Saharafunction search_qsearch(&$data, $base, $file, $type, $lvl, $opts)
108*83198f92SSatoshi Sahara{
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 */
133*83198f92SSatoshi Saharafunction search_index(&$data, $base, $file, $type, $lvl, $opts)
134*83198f92SSatoshi Sahara{
135d1c7b6ecSAndreas Gohr    global $conf;
136783d2e49SAdrian Lang    $opts = array(
137783d2e49SAdrian Lang        'pagesonly' => true,
138783d2e49SAdrian Lang        'listdirs' => true,
139443e135dSChristopher Smith        'listfiles' => empty($opts['nofiles']),
140783d2e49SAdrian Lang        'sneakyacl' => $conf['sneaky_index'],
141783d2e49SAdrian Lang        // Hacky, should rather use recmatch
1421c6c1c6cSMichael Hamann        'depth' => preg_match('#^'.preg_quote($file, '#').'(/|$)#','/'.$opts['ns']) ? 0 : -1
143783d2e49SAdrian Lang    );
144f3f0262cSandi
145783d2e49SAdrian Lang    return search_universal($data, $base, $file, $type, $lvl, $opts);
146f3f0262cSandi}
147f3f0262cSandi
148f3f0262cSandi/**
14915fae107Sandi * List all namespaces
15015fae107Sandi *
15115fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
152f50a239bSTakamura *
153f50a239bSTakamura * @param array $data
154f50a239bSTakamura * @param string $base
155f50a239bSTakamura * @param string $file
156f50a239bSTakamura * @param string $type
157f50a239bSTakamura * @param integer $lvl
158f50a239bSTakamura * @param array $opts
159f50a239bSTakamura *
160f50a239bSTakamura * @return bool
161f3f0262cSandi */
162*83198f92SSatoshi Saharafunction search_namespaces(&$data, $base, $file, $type, $lvl, $opts)
163*83198f92SSatoshi Sahara{
1648705cc81SAndreas Gohr    $opts = array(
1658705cc81SAndreas Gohr            'listdirs' => true,
1668705cc81SAndreas Gohr            );
1678705cc81SAndreas Gohr    return search_universal($data, $base, $file, $type, $lvl, $opts);
168f3f0262cSandi}
169f3f0262cSandi
170f3f0262cSandi/**
17115fae107Sandi * List all mediafiles in a namespace
17242ea7f44SGerrit Uitslag *   $opts['depth']     recursion level, 0 for all
17342ea7f44SGerrit Uitslag *   $opts['showmsg']   shows message if invalid media id is used
17442ea7f44SGerrit Uitslag *   $opts['skipacl']   skip acl checking
17542ea7f44SGerrit Uitslag *   $opts['pattern']   check given pattern
17642ea7f44SGerrit Uitslag *   $opts['hash']      add hashes to result list
17715fae107Sandi *
17815fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
179f50a239bSTakamura *
180f50a239bSTakamura * @param array $data
181f50a239bSTakamura * @param string $base
182f50a239bSTakamura * @param string $file
183f50a239bSTakamura * @param string $type
184f50a239bSTakamura * @param integer $lvl
185f50a239bSTakamura * @param array $opts
186f50a239bSTakamura *
187f50a239bSTakamura * @return bool
188f3f0262cSandi */
189*83198f92SSatoshi Saharafunction search_media(&$data, $base, $file, $type, $lvl, $opts)
190*83198f92SSatoshi Sahara{
191f3f0262cSandi    //we do nothing with directories
1921a49ac65SGina Haeussge    if ($type == 'd') {
1930e80bb5eSChristopher Smith        if (empty($opts['depth'])) return true; // recurse forever
19478315408SAndreas Gohr        $depth = substr_count($file,'/');
195b8219d2dSAndreas Gohr        if ($depth >= $opts['depth']) return false; // depth reached
196224122cfSAndreas Gohr        return true;
1971a49ac65SGina Haeussge    }
198f3f0262cSandi
199f3f0262cSandi    $info         = array();
200156a608cSandi    $info['id']   = pathID($file, true);
20164807c84SAndreas Gohr    if ($info['id'] != cleanID($info['id'])) {
20264807c84SAndreas Gohr        if ($opts['showmsg'])
20364807c84SAndreas Gohr            msg(hsc($info['id']).' is not a valid file name for DokuWiki - skipped',-1);
20464807c84SAndreas Gohr        return false; // skip non-valid files
20564807c84SAndreas Gohr    }
206f3f0262cSandi
207f3f0262cSandi    //check ACL for namespace (we have no ACL for mediafiles)
208224122cfSAndreas Gohr    $info['perm'] = auth_quickaclcheck(getNS($info['id']).':*');
2090e80bb5eSChristopher Smith    if (empty($opts['skipacl']) && $info['perm'] < AUTH_READ) {
210224122cfSAndreas Gohr        return false;
211224122cfSAndreas Gohr    }
212224122cfSAndreas Gohr
213224122cfSAndreas Gohr    //check pattern filter
2140e80bb5eSChristopher Smith    if (!empty($opts['pattern']) && !@preg_match($opts['pattern'], $info['id'])) {
215f3f0262cSandi        return false;
216f3f0262cSandi    }
217f3f0262cSandi
2188cbc5ee8SAndreas Gohr    $info['file']     = \dokuwiki\Utf8\PhpString::basename($file);
219f3f0262cSandi    $info['size']     = filesize($base.'/'.$file);
2205e7fa82eSAndreas Gohr    $info['mtime']    = filemtime($base.'/'.$file);
2213df72098SAndreas Gohr    $info['writable'] = is_writable($base.'/'.$file);
222f3f0262cSandi    if (preg_match("/\.(jpe?g|gif|png)$/", $file)) {
223f3f0262cSandi        $info['isimg'] = true;
22423a34783SAndreas Gohr        $info['meta']  = new JpegMeta($base.'/'.$file);
225f3f0262cSandi    } else {
226f3f0262cSandi        $info['isimg'] = false;
227f3f0262cSandi    }
2280e80bb5eSChristopher Smith    if (!empty($opts['hash'])) {
229dfd343c4SAndreas Gohr        $info['hash'] = md5(io_readFile(mediaFN($info['id']), false));
230224122cfSAndreas Gohr    }
231224122cfSAndreas Gohr
232f3f0262cSandi    $data[] = $info;
233f3f0262cSandi
234f3f0262cSandi    return false;
235f3f0262cSandi}
236f3f0262cSandi
237f3f0262cSandi/**
238f3f0262cSandi * This function just lists documents (for RSS namespace export)
23915fae107Sandi *
24015fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
241f50a239bSTakamura *
242f50a239bSTakamura * @param array $data
243f50a239bSTakamura * @param string $base
244f50a239bSTakamura * @param string $file
245f50a239bSTakamura * @param string $type
246f50a239bSTakamura * @param integer $lvl
247f50a239bSTakamura * @param array $opts
248f50a239bSTakamura *
249f50a239bSTakamura * @return bool
250f3f0262cSandi */
251*83198f92SSatoshi Saharafunction search_list(&$data, $base, $file, $type, $lvl, $opts)
252*83198f92SSatoshi Sahara{
253f3f0262cSandi    //we do nothing with directories
254f3f0262cSandi    if ($type == 'd') return false;
2550e1a261eSMichael Klier    //only search txt files
2560e1a261eSMichael Klier    if (substr($file, -4) == '.txt') {
257f3f0262cSandi        //check ACL
258f3f0262cSandi        $id = pathID($file);
259f3f0262cSandi        if (auth_quickaclcheck($id) < AUTH_READ) {
260f3f0262cSandi            return false;
261f3f0262cSandi        }
2620e1a261eSMichael Klier        $data[]['id'] = $id;
263f3f0262cSandi    }
264f3f0262cSandi    return false;
265f3f0262cSandi}
266f3f0262cSandi
267f3f0262cSandi/**
268f3f0262cSandi * Quicksearch for searching matching pagenames
269f3f0262cSandi *
270f3f0262cSandi * $opts['query'] is the search query
27115fae107Sandi *
27215fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
273f50a239bSTakamura *
274f50a239bSTakamura * @param array $data
275f50a239bSTakamura * @param string $base
276f50a239bSTakamura * @param string $file
277f50a239bSTakamura * @param string $type
278f50a239bSTakamura * @param integer $lvl
279f50a239bSTakamura * @param array $opts
280f50a239bSTakamura *
281f50a239bSTakamura * @return bool
282f3f0262cSandi */
283*83198f92SSatoshi Saharafunction search_pagename(&$data, $base, $file, $type, $lvl, $opts)
284*83198f92SSatoshi Sahara{
285f3f0262cSandi    //we do nothing with directories
286f3f0262cSandi    if ($type == 'd') return true;
287f3f0262cSandi    //only search txt files
2880e1a261eSMichael Klier    if (substr($file, -4) != '.txt') return true;
289f3f0262cSandi
290f3f0262cSandi    //simple stringmatching
291396b7edbSmatthiasgrimm    if (!empty($opts['query'])) {
292f3f0262cSandi        if (strpos($file, $opts['query']) !== false) {
293f3f0262cSandi            //check ACL
294f3f0262cSandi            $id = pathID($file);
295f3f0262cSandi            if (auth_quickaclcheck($id) < AUTH_READ) {
296f3f0262cSandi                return false;
297f3f0262cSandi            }
298f3f0262cSandi            $data[]['id'] = $id;
299f3f0262cSandi        }
300396b7edbSmatthiasgrimm    }
301f3f0262cSandi    return true;
302f3f0262cSandi}
303f3f0262cSandi
304f3f0262cSandi/**
30558b6f612SAndreas Gohr * Just lists all documents
30658b6f612SAndreas Gohr *
3071fcfad4dSAndreas Gohr * $opts['depth']   recursion level, 0 for all
3081fcfad4dSAndreas Gohr * $opts['hash']    do md5 sum of content?
309224122cfSAndreas Gohr * $opts['skipacl'] list everything regardless of ACL
3101fcfad4dSAndreas Gohr *
31158b6f612SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
312f50a239bSTakamura *
313f50a239bSTakamura * @param array $data
314f50a239bSTakamura * @param string $base
315f50a239bSTakamura * @param string $file
316f50a239bSTakamura * @param string $type
317f50a239bSTakamura * @param integer $lvl
318f50a239bSTakamura * @param array $opts
319f50a239bSTakamura *
320f50a239bSTakamura * @return bool
32158b6f612SAndreas Gohr */
322*83198f92SSatoshi Saharafunction search_allpages(&$data, $base, $file, $type, $lvl, $opts)
323*83198f92SSatoshi Sahara{
3248451f4adSGuillaume Turri    if (isset($opts['depth']) && $opts['depth']) {
325c647387eSGuillaume Turri        $parts = explode('/',ltrim($file, '/'));
3265737a81eSMichael Hamann        if (($type == 'd' && count($parts) >= $opts['depth'])
327*83198f92SSatoshi Sahara            || ($type != 'd' && count($parts) > $opts['depth'])
328*83198f92SSatoshi Sahara        ){
329c647387eSGuillaume Turri            return false; // depth reached
330c647387eSGuillaume Turri        }
331c647387eSGuillaume Turri    }
332c647387eSGuillaume Turri
33358b6f612SAndreas Gohr    //we do nothing with directories
3341fcfad4dSAndreas Gohr    if ($type == 'd') {
3351fcfad4dSAndreas Gohr        return true;
3361fcfad4dSAndreas Gohr    }
3371fcfad4dSAndreas Gohr
33858b6f612SAndreas Gohr    //only search txt files
3390e1a261eSMichael Klier    if (substr($file, -4) != '.txt') return true;
34058b6f612SAndreas Gohr
34159bc3b48SGerrit Uitslag    $item = array();
3421fcfad4dSAndreas Gohr    $item['id']   = pathID($file);
34377244e70SMichael Hamann    if (empty($opts['skipacl']) && auth_quickaclcheck($item['id']) < AUTH_READ) {
3441fcfad4dSAndreas Gohr        return false;
3451fcfad4dSAndreas Gohr    }
3461fcfad4dSAndreas Gohr
3471fcfad4dSAndreas Gohr    $item['rev']   = filemtime($base.'/'.$file);
348224122cfSAndreas Gohr    $item['mtime'] = $item['rev'];
3491fcfad4dSAndreas Gohr    $item['size']  = filesize($base.'/'.$file);
3508f34cf3dSMichael Große    if (!empty($opts['hash'])) {
3511fcfad4dSAndreas Gohr        $item['hash'] = md5(trim(rawWiki($item['id'])));
3521fcfad4dSAndreas Gohr    }
3531fcfad4dSAndreas Gohr
3541fcfad4dSAndreas Gohr    $data[] = $item;
35558b6f612SAndreas Gohr    return true;
35658b6f612SAndreas Gohr}
35758b6f612SAndreas Gohr
358b59a406bSmatthiasgrimm/* ------------- helper functions below -------------- */
359b59a406bSmatthiasgrimm
360b59a406bSmatthiasgrimm/**
36115fae107Sandi * fulltext sort
36215fae107Sandi *
363f3f0262cSandi * Callback sort function for use with usort to sort the data
364f3f0262cSandi * structure created by search_fulltext. Sorts descending by count
36515fae107Sandi *
36615fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
367f50a239bSTakamura *
368f50a239bSTakamura * @param array $a
369f50a239bSTakamura * @param array $b
370f50a239bSTakamura *
371f50a239bSTakamura * @return int
372f3f0262cSandi */
373*83198f92SSatoshi Saharafunction sort_search_fulltext($a, $b)
374*83198f92SSatoshi Sahara{
375f3f0262cSandi    if ($a['count'] > $b['count']) {
376f3f0262cSandi        return -1;
377f3f0262cSandi    } elseif ($a['count'] < $b['count']) {
378f3f0262cSandi        return 1;
379f3f0262cSandi    } else {
380f3f0262cSandi        return strcmp($a['id'], $b['id']);
381f3f0262cSandi    }
382f3f0262cSandi}
383f3f0262cSandi
384f3f0262cSandi/**
385f3f0262cSandi * translates a document path to an ID
38615fae107Sandi *
38715fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
38837e34a5eSandi * @todo    move to pageutils
389f50a239bSTakamura *
390f50a239bSTakamura * @param string $path
391f50a239bSTakamura * @param bool $keeptxt
392f50a239bSTakamura *
393f50a239bSTakamura * @return mixed|string
394f3f0262cSandi */
395*83198f92SSatoshi Saharafunction pathID($path, $keeptxt=false)
396*83198f92SSatoshi Sahara{
39749c713a3Sandi    $id = utf8_decodeFN($path);
39849c713a3Sandi    $id = str_replace('/', ':', $id);
399156a608cSandi    if (!$keeptxt) $id = preg_replace('#\.txt$#', '', $id);
400709b1063SAdrian Lang    $id = trim($id, ':');
401f3f0262cSandi    return $id;
402f3f0262cSandi}
403f3f0262cSandi
404340756e4Sandi
4053abeade3SAndreas Gohr/**
4063abeade3SAndreas Gohr * This is a very universal callback for the search() function, replacing
4073abeade3SAndreas Gohr * many of the former individual functions at the cost of a more complex
4083abeade3SAndreas Gohr * setup.
4093abeade3SAndreas Gohr *
4103abeade3SAndreas Gohr * How the function behaves, depends on the options passed in the $opts
4113abeade3SAndreas Gohr * array, where the following settings can be used.
4123abeade3SAndreas Gohr *
413e14fe973SGerrit Uitslag * depth      int     recursion depth. 0 for unlimited                       (default: 0)
414e14fe973SGerrit Uitslag * keeptxt    bool    keep .txt extension for IDs                            (default: false)
415e14fe973SGerrit Uitslag * listfiles  bool    include files in listing                               (default: false)
416e14fe973SGerrit Uitslag * listdirs   bool    include namespaces in listing                          (default: false)
417e14fe973SGerrit Uitslag * pagesonly  bool    restrict files to pages                                (default: false)
418e14fe973SGerrit Uitslag * skipacl    bool    do not check for READ permission                       (default: false)
419e14fe973SGerrit Uitslag * sneakyacl  bool    don't recurse into nonreadable dirs                    (default: false)
420e14fe973SGerrit Uitslag * hash       bool    create MD5 hash for files                              (default: false)
421e14fe973SGerrit Uitslag * meta       bool    return file metadata                                   (default: false)
422e14fe973SGerrit Uitslag * filematch  string  match files against this regexp                        (default: '', so accept everything)
423e14fe973SGerrit Uitslag * idmatch    string  match full ID against this regexp                      (default: '', so accept everything)
424e14fe973SGerrit Uitslag * dirmatch   string  match directory against this regexp when adding        (default: '', so accept everything)
425e14fe973SGerrit Uitslag * nsmatch    string  match namespace against this regexp when adding        (default: '', so accept everything)
426e14fe973SGerrit Uitslag * recmatch   string  match directory against this regexp when recursing     (default: '', so accept everything)
427e14fe973SGerrit Uitslag * showmsg    bool    warn about non-ID files                                (default: false)
428e14fe973SGerrit Uitslag * showhidden bool    show hidden files(e.g. by hidepages config) too        (default: false)
429e14fe973SGerrit Uitslag * firsthead  bool    return first heading for pages                         (default: false)
4303abeade3SAndreas Gohr *
431ce4301e3SGerrit Uitslag * @param array &$data  - Reference to the result data structure
432ce4301e3SGerrit Uitslag * @param string $base  - Base usually $conf['datadir']
433ce4301e3SGerrit Uitslag * @param string $file  - current file or directory relative to $base
434ce4301e3SGerrit Uitslag * @param string $type  - Type either 'd' for directory or 'f' for file
435ce4301e3SGerrit Uitslag * @param int    $lvl   - Current recursion depht
436ce4301e3SGerrit Uitslag * @param array  $opts  - option array as given to search()
437ce4301e3SGerrit Uitslag * @return bool if this directory should be traversed (true) or not (false)
438ce4301e3SGerrit Uitslag *              return value is ignored for files
439ce4301e3SGerrit Uitslag *
4403abeade3SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
4413abeade3SAndreas Gohr */
442*83198f92SSatoshi Saharafunction search_universal(&$data, $base, $file, $type, $lvl, $opts)
443*83198f92SSatoshi Sahara{
4443abeade3SAndreas Gohr    $item   = array();
4453abeade3SAndreas Gohr    $return = true;
4463abeade3SAndreas Gohr
4473abeade3SAndreas Gohr    // get ID and check if it is a valid one
448b7a3421aSChristopher Smith    $item['id'] = pathID($file, ($type == 'd' || !empty($opts['keeptxt'])));
4498537abd1SAdrian Lang    if ($item['id'] != cleanID($item['id'])){
45049f299d6SChristopher Smith        if (!empty($opts['showmsg'])) {
4518537abd1SAdrian Lang            msg(hsc($item['id']).' is not a valid file name for DokuWiki - skipped',-1);
452b7a3421aSChristopher Smith        }
4533abeade3SAndreas Gohr        return false; // skip non-valid files
4543abeade3SAndreas Gohr    }
4558705cc81SAndreas Gohr    $item['ns']  = getNS($item['id']);
4563abeade3SAndreas Gohr
4573abeade3SAndreas Gohr    if ($type == 'd') {
4583abeade3SAndreas Gohr        // decide if to recursion into this directory is wanted
4590e80bb5eSChristopher Smith        if (empty($opts['depth'])) {
4603abeade3SAndreas Gohr            $return = true; // recurse forever
4613abeade3SAndreas Gohr        } else {
4623abeade3SAndreas Gohr            $depth = substr_count($file,'/');
4633abeade3SAndreas Gohr            if ($depth >= $opts['depth']) {
4643abeade3SAndreas Gohr                $return = false; // depth reached
4653abeade3SAndreas Gohr            } else {
4663abeade3SAndreas Gohr                $return = true;
4673abeade3SAndreas Gohr            }
4683abeade3SAndreas Gohr        }
4699b4337c6SChristopher Smith
4709b4337c6SChristopher Smith        if ($return) {
4719b4337c6SChristopher Smith            $match = empty($opts['recmatch']) || preg_match('/'.$opts['recmatch'].'/', $file);
4729b4337c6SChristopher Smith            if (!$match) {
4739b4337c6SChristopher Smith                return false; // doesn't match
4749b4337c6SChristopher Smith            }
4753abeade3SAndreas Gohr        }
4763abeade3SAndreas Gohr    }
4773abeade3SAndreas Gohr
4783abeade3SAndreas Gohr    // check ACL
479443e135dSChristopher Smith    if (empty($opts['skipacl'])) {
4803abeade3SAndreas Gohr        if ($type == 'd') {
4813abeade3SAndreas Gohr            $item['perm'] = auth_quickaclcheck($item['id'].':*');
4823abeade3SAndreas Gohr        } else {
4833abeade3SAndreas Gohr            $item['perm'] = auth_quickaclcheck($item['id']); //FIXME check namespace for media files
4843abeade3SAndreas Gohr        }
4853abeade3SAndreas Gohr    } else {
4863abeade3SAndreas Gohr        $item['perm'] = AUTH_DELETE;
4873abeade3SAndreas Gohr    }
4883abeade3SAndreas Gohr
4893abeade3SAndreas Gohr    // are we done here maybe?
4903abeade3SAndreas Gohr    if ($type == 'd') {
491443e135dSChristopher Smith        if (empty($opts['listdirs'])) return $return;
49264159a61SAndreas Gohr        //neither list nor recurse forbidden items:
49364159a61SAndreas Gohr        if (empty($opts['skipacl']) && !empty($opts['sneakyacl']) && $item['perm'] < AUTH_READ) return false;
494443e135dSChristopher Smith        if (!empty($opts['dirmatch']) && !preg_match('/'.$opts['dirmatch'].'/', $file)) return $return;
495443e135dSChristopher Smith        if (!empty($opts['nsmatch']) && !preg_match('/'.$opts['nsmatch'].'/', $item['ns'])) return $return;
4963abeade3SAndreas Gohr    } else {
497443e135dSChristopher Smith        if (empty($opts['listfiles'])) return $return;
498443e135dSChristopher Smith        if (empty($opts['skipacl']) && $item['perm'] < AUTH_READ) return $return;
499443e135dSChristopher Smith        if (!empty($opts['pagesonly']) && (substr($file, -4) != '.txt')) return $return;
500443e135dSChristopher Smith        if (empty($opts['showhidden']) && isHiddenPage($item['id'])) return $return;
501443e135dSChristopher Smith        if (!empty($opts['filematch']) && !preg_match('/'.$opts['filematch'].'/', $file)) return $return;
502443e135dSChristopher Smith        if (!empty($opts['idmatch']) && !preg_match('/'.$opts['idmatch'].'/', $item['id'])) return $return;
5033abeade3SAndreas Gohr    }
5043abeade3SAndreas Gohr
5053abeade3SAndreas Gohr    // still here? prepare the item
5063abeade3SAndreas Gohr    $item['type']  = $type;
50732d6093dSAndreas Gohr    $item['level'] = $lvl;
5083abeade3SAndreas Gohr    $item['open']  = $return;
5093abeade3SAndreas Gohr
5100e80bb5eSChristopher Smith    if (!empty($opts['meta'])) {
5118cbc5ee8SAndreas Gohr        $item['file']       = \dokuwiki\Utf8\PhpString::basename($file);
5123abeade3SAndreas Gohr        $item['size']       = filesize($base.'/'.$file);
5133abeade3SAndreas Gohr        $item['mtime']      = filemtime($base.'/'.$file);
5143abeade3SAndreas Gohr        $item['rev']        = $item['mtime'];
5153abeade3SAndreas Gohr        $item['writable']   = is_writable($base.'/'.$file);
5163abeade3SAndreas Gohr        $item['executable'] = is_executable($base.'/'.$file);
5173abeade3SAndreas Gohr    }
5183abeade3SAndreas Gohr
5193abeade3SAndreas Gohr    if ($type == 'f') {
5200e80bb5eSChristopher Smith        if (!empty($opts['hash'])) $item['hash'] = md5(io_readFile($base.'/'.$file, false));
521*83198f92SSatoshi Sahara        if (!empty($opts['firsthead'])) {
522*83198f92SSatoshi Sahara            $item['title'] = p_get_first_heading($item['id'], METADATA_DONT_RENDER);
523*83198f92SSatoshi Sahara        }
5243abeade3SAndreas Gohr    }
5253abeade3SAndreas Gohr
5263abeade3SAndreas Gohr    // finally add the item
5273abeade3SAndreas Gohr    $data[] = $item;
5283abeade3SAndreas Gohr    return $return;
5293abeade3SAndreas Gohr}
5303abeade3SAndreas Gohr
531e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
532