xref: /dokuwiki/inc/search.php (revision 7d34963b3e75ea04c63ec066a6b7a692e123cb53)
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 */
824870174SAndreas Gohruse dokuwiki\Utf8\PhpString;
924870174SAndreas 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 */
28d868eb89SAndreas Gohrfunction search(&$data, $base, $func, $opts, $dir = '', $lvl = 1, $sort = 'natural')
29d868eb89SAndreas Gohr{
3024870174SAndreas Gohr    $dirs   = [];
3124870174SAndreas Gohr    $files  = [];
3224870174SAndreas Gohr    $filepaths = [];
33f3f0262cSandi
34e0b6aadeSAndreas Gohr    // safeguard against runaways #1452
35e0b6aadeSAndreas Gohr    if ($base == '' || $base == '/') {
36e0b6aadeSAndreas Gohr        throw new RuntimeException('No valid $base passed to search() - possible misconfiguration or bug');
37e0b6aadeSAndreas Gohr    }
38e0b6aadeSAndreas Gohr
39f3f0262cSandi    //read in directories and files
40f3f0262cSandi    $dh = @opendir($base.'/'.$dir);
41f3f0262cSandi    if (!$dh) return;
42f3f0262cSandi    while (($file = readdir($dh)) !== false) {
43de3dfc91Sandi        if (preg_match('/^[\._]/', $file)) continue; //skip hidden files and upper dirs
44f3f0262cSandi        if (is_dir($base.'/'.$dir.'/'.$file)) {
45f3f0262cSandi            $dirs[] = $dir.'/'.$file;
46f3f0262cSandi            continue;
47f3f0262cSandi        }
48f3f0262cSandi        $files[] = $dir.'/'.$file;
49abc306f4SKate Arzamastseva        $filepaths[] = $base.'/'.$dir.'/'.$file;
50f3f0262cSandi    }
51f3f0262cSandi    closedir($dh);
52ec24a2dfSPhilipp A. Hartmann    if (!empty($sort)) {
53abc306f4SKate Arzamastseva        if ($sort == 'date') {
54d971ea8bSKate Arzamastseva            @array_multisort(array_map('filemtime', $filepaths), SORT_NUMERIC, SORT_DESC, $files);
551dc5d48bSChristopher Smith        } else /* natural */ {
562d85e841SAndreas Gohr            Sort::asortFN($files);
57abc306f4SKate Arzamastseva        }
582d85e841SAndreas Gohr        Sort::asortFN($dirs);
59ec24a2dfSPhilipp A. Hartmann    }
60f3f0262cSandi
61f3f0262cSandi    //give directories to userfunction then recurse
62f3f0262cSandi    foreach ($dirs as $dir) {
6324870174SAndreas Gohr        if (call_user_func_array($func, [&$data, $base, $dir, 'd', $lvl, $opts])) {
645514a5a7SChristopher Smith            search($data, $base, $func, $opts, $dir, $lvl+1, $sort);
65f3f0262cSandi        }
66f3f0262cSandi    }
67f3f0262cSandi    //now handle the files
68f3f0262cSandi    foreach ($files as $file) {
6924870174SAndreas Gohr        call_user_func_array($func, [&$data, $base, $file, 'f', $lvl, $opts]);
70f3f0262cSandi    }
71f3f0262cSandi}
72f3f0262cSandi
73f3f0262cSandi/**
74f3f0262cSandi * The following functions are userfunctions to use with the search
75f3f0262cSandi * function above. This function is called for every found file or
76f3f0262cSandi * directory. When a directory is given to the function it has to
77f3f0262cSandi * decide if this directory should be traversed (true) or not (false)
78f3f0262cSandi * The function has to accept the following parameters:
79f3f0262cSandi *
80ce4301e3SGerrit Uitslag * array &$data  - Reference to the result data structure
81ce4301e3SGerrit Uitslag * string $base  - Base usually $conf['datadir']
82ce4301e3SGerrit Uitslag * string $file  - current file or directory relative to $base
83ce4301e3SGerrit Uitslag * string $type  - Type either 'd' for directory or 'f' for file
84ce4301e3SGerrit Uitslag * int    $lvl   - Current recursion depht
85ce4301e3SGerrit Uitslag * array  $opts  - option array as given to search()
86f3f0262cSandi *
87f3f0262cSandi * return values for files are ignored
88f3f0262cSandi *
89f3f0262cSandi * All functions should check the ACL for document READ rights
90783d2e49SAdrian Lang * namespaces (directories) are NOT checked (when sneaky_index is 0) as this
91783d2e49SAdrian Lang * would break the recursion (You can have an nonreadable dir over a readable
920e1a261eSMichael Klier * one deeper nested) also make sure to check the file type (for example
930e1a261eSMichael Klier * in case of lockfiles).
94f3f0262cSandi */
95f3f0262cSandi
96f3f0262cSandi/**
9763f2400bSandi * Searches for pages beginning with the given query
9863f2400bSandi *
9963f2400bSandi * @author Andreas Gohr <andi@splitbrain.org>
100f50a239bSTakamura *
101f50a239bSTakamura * @param array $data
102f50a239bSTakamura * @param string $base
103f50a239bSTakamura * @param string $file
104f50a239bSTakamura * @param string $type
105f50a239bSTakamura * @param integer $lvl
106f50a239bSTakamura * @param array $opts
107f50a239bSTakamura *
108f50a239bSTakamura * @return bool
10963f2400bSandi */
110d868eb89SAndreas Gohrfunction search_qsearch(&$data, $base, $file, $type, $lvl, $opts)
111d868eb89SAndreas Gohr{
11224870174SAndreas Gohr    $opts = [
1138705cc81SAndreas Gohr        'idmatch'   => '(^|:)'.preg_quote($opts['query'], '/').'/',
1148705cc81SAndreas Gohr        'listfiles' => true,
11524870174SAndreas Gohr        'pagesonly' => true
11624870174SAndreas Gohr    ];
1178705cc81SAndreas Gohr    return search_universal($data, $base, $file, $type, $lvl, $opts);
11863f2400bSandi}
11963f2400bSandi
12063f2400bSandi/**
12115fae107Sandi * Build the browsable index of pages
122f3f0262cSandi *
123783d2e49SAdrian Lang * $opts['ns'] is the currently viewed namespace
12415fae107Sandi *
12515fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
126f50a239bSTakamura *
127f50a239bSTakamura * @param array $data
128f50a239bSTakamura * @param string $base
129f50a239bSTakamura * @param string $file
130f50a239bSTakamura * @param string $type
131f50a239bSTakamura * @param integer $lvl
132f50a239bSTakamura * @param array $opts
133f50a239bSTakamura *
134f50a239bSTakamura * @return bool
135f3f0262cSandi */
136d868eb89SAndreas Gohrfunction search_index(&$data, $base, $file, $type, $lvl, $opts)
137d868eb89SAndreas Gohr{
138d1c7b6ecSAndreas Gohr    global $conf;
13924870174SAndreas Gohr    $ns = $opts['ns'] ?? '';
14024870174SAndreas Gohr    $opts = [
141783d2e49SAdrian Lang        'pagesonly' => true,
142783d2e49SAdrian Lang        'listdirs' => true,
143443e135dSChristopher Smith        'listfiles' => empty($opts['nofiles']),
144783d2e49SAdrian Lang        'sneakyacl' => $conf['sneaky_index'],
145783d2e49SAdrian Lang        // Hacky, should rather use recmatch
14624870174SAndreas Gohr        'depth' => preg_match('#^'.preg_quote($file, '#').'(/|$)#', '/'.$ns) ? 0 : -1,
14724870174SAndreas Gohr    ];
148f3f0262cSandi
149783d2e49SAdrian Lang    return search_universal($data, $base, $file, $type, $lvl, $opts);
150f3f0262cSandi}
151f3f0262cSandi
152f3f0262cSandi/**
15315fae107Sandi * List all namespaces
15415fae107Sandi *
15515fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
156f50a239bSTakamura *
157f50a239bSTakamura * @param array $data
158f50a239bSTakamura * @param string $base
159f50a239bSTakamura * @param string $file
160f50a239bSTakamura * @param string $type
161f50a239bSTakamura * @param integer $lvl
162f50a239bSTakamura * @param array $opts
163f50a239bSTakamura *
164f50a239bSTakamura * @return bool
165f3f0262cSandi */
166d868eb89SAndreas Gohrfunction search_namespaces(&$data, $base, $file, $type, $lvl, $opts)
167d868eb89SAndreas Gohr{
16824870174SAndreas Gohr    $opts = ['listdirs' => true];
1698705cc81SAndreas Gohr    return search_universal($data, $base, $file, $type, $lvl, $opts);
170f3f0262cSandi}
171f3f0262cSandi
172f3f0262cSandi/**
17315fae107Sandi * List all mediafiles in a namespace
17442ea7f44SGerrit Uitslag *   $opts['depth']     recursion level, 0 for all
17542ea7f44SGerrit Uitslag *   $opts['showmsg']   shows message if invalid media id is used
17642ea7f44SGerrit Uitslag *   $opts['skipacl']   skip acl checking
17742ea7f44SGerrit Uitslag *   $opts['pattern']   check given pattern
17842ea7f44SGerrit Uitslag *   $opts['hash']      add hashes to result list
17915fae107Sandi *
18015fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
181f50a239bSTakamura *
182f50a239bSTakamura * @param array $data
183f50a239bSTakamura * @param string $base
184f50a239bSTakamura * @param string $file
185f50a239bSTakamura * @param string $type
186f50a239bSTakamura * @param integer $lvl
187f50a239bSTakamura * @param array $opts
188f50a239bSTakamura *
189f50a239bSTakamura * @return bool
190f3f0262cSandi */
191d868eb89SAndreas Gohrfunction search_media(&$data, $base, $file, $type, $lvl, $opts)
192d868eb89SAndreas Gohr{
193b8219d2dSAndreas Gohr
194f3f0262cSandi    //we do nothing with directories
1951a49ac65SGina Haeussge    if ($type == 'd') {
1960e80bb5eSChristopher Smith        if (empty($opts['depth'])) return true; // recurse forever
19778315408SAndreas Gohr        $depth = substr_count($file, '/');
198b8219d2dSAndreas Gohr        if ($depth >= $opts['depth']) return false; // depth reached
199224122cfSAndreas Gohr        return true;
2001a49ac65SGina Haeussge    }
201f3f0262cSandi
20224870174SAndreas Gohr    $info         = [];
203156a608cSandi    $info['id']   = pathID($file, true);
20464807c84SAndreas Gohr    if ($info['id'] != cleanID($info['id'])) {
2052b9be456SAndreas Gohr        if (!empty($opts['showmsg']))
20664807c84SAndreas Gohr            msg(hsc($info['id']).' is not a valid file name for DokuWiki - skipped', -1);
20764807c84SAndreas Gohr        return false; // skip non-valid files
20864807c84SAndreas Gohr    }
209f3f0262cSandi
210f3f0262cSandi    //check ACL for namespace (we have no ACL for mediafiles)
211224122cfSAndreas Gohr    $info['perm'] = auth_quickaclcheck(getNS($info['id']).':*');
2120e80bb5eSChristopher Smith    if (empty($opts['skipacl']) && $info['perm'] < AUTH_READ) {
213224122cfSAndreas Gohr        return false;
214224122cfSAndreas Gohr    }
215224122cfSAndreas Gohr
216224122cfSAndreas Gohr    //check pattern filter
2170e80bb5eSChristopher Smith    if (!empty($opts['pattern']) && !@preg_match($opts['pattern'], $info['id'])) {
218f3f0262cSandi        return false;
219f3f0262cSandi    }
220f3f0262cSandi
22124870174SAndreas Gohr    $info['file']     = PhpString::basename($file);
222f3f0262cSandi    $info['size']     = filesize($base.'/'.$file);
2235e7fa82eSAndreas Gohr    $info['mtime']    = filemtime($base.'/'.$file);
2243df72098SAndreas Gohr    $info['writable'] = is_writable($base.'/'.$file);
225f3f0262cSandi    if (preg_match("/\.(jpe?g|gif|png)$/", $file)) {
226f3f0262cSandi        $info['isimg'] = true;
22723a34783SAndreas Gohr        $info['meta']  = new JpegMeta($base.'/'.$file);
228f3f0262cSandi    } else {
229f3f0262cSandi        $info['isimg'] = false;
230f3f0262cSandi    }
2310e80bb5eSChristopher Smith    if (!empty($opts['hash'])) {
232dfd343c4SAndreas Gohr        $info['hash'] = md5(io_readFile(mediaFN($info['id']), false));
233224122cfSAndreas Gohr    }
234224122cfSAndreas Gohr
235f3f0262cSandi    $data[] = $info;
236f3f0262cSandi
237f3f0262cSandi    return false;
238f3f0262cSandi}
239f3f0262cSandi
240f3f0262cSandi/**
2414f33babfSAndreas Gohr * List all mediafiles in a namespace
2424f33babfSAndreas Gohr *   $opts['depth']     recursion level, 0 for all
2434f33babfSAndreas Gohr *   $opts['showmsg']   shows message if invalid media id is used
2444f33babfSAndreas Gohr *   $opts['skipacl']   skip acl checking
2454f33babfSAndreas Gohr *   $opts['pattern']   check given pattern
2464f33babfSAndreas Gohr *   $opts['hash']      add hashes to result list
2474f33babfSAndreas Gohr *
2484f33babfSAndreas Gohr * @todo This is a temporary copy of search_media returning a list of MediaFile intances
2494f33babfSAndreas Gohr *
2504f33babfSAndreas Gohr * @param array $data
2514f33babfSAndreas Gohr * @param string $base
2524f33babfSAndreas Gohr * @param string $file
2534f33babfSAndreas Gohr * @param string $type
2544f33babfSAndreas Gohr * @param integer $lvl
2554f33babfSAndreas Gohr * @param array $opts
2564f33babfSAndreas Gohr *
2574f33babfSAndreas Gohr * @return bool
2584f33babfSAndreas Gohr */
259d868eb89SAndreas Gohrfunction search_mediafiles(&$data, $base, $file, $type, $lvl, $opts)
260d868eb89SAndreas Gohr{
2614f33babfSAndreas Gohr
2624f33babfSAndreas Gohr    //we do nothing with directories
2634f33babfSAndreas Gohr    if ($type == 'd') {
2644f33babfSAndreas Gohr        if (empty($opts['depth'])) return true; // recurse forever
2654f33babfSAndreas Gohr        $depth = substr_count($file, '/');
2664f33babfSAndreas Gohr        if ($depth >= $opts['depth']) return false; // depth reached
2674f33babfSAndreas Gohr        return true;
2684f33babfSAndreas Gohr    }
2694f33babfSAndreas Gohr
2704f33babfSAndreas Gohr    $id   = pathID($file, true);
2714f33babfSAndreas Gohr    if ($id != cleanID($id)) {
2724f33babfSAndreas Gohr        if ($opts['showmsg'])
2734f33babfSAndreas Gohr            msg(hsc($id).' is not a valid file name for DokuWiki - skipped', -1);
2744f33babfSAndreas Gohr        return false; // skip non-valid files
2754f33babfSAndreas Gohr    }
2764f33babfSAndreas Gohr
2774f33babfSAndreas Gohr    //check ACL for namespace (we have no ACL for mediafiles)
2784f33babfSAndreas Gohr    $info['perm'] = auth_quickaclcheck(getNS($id).':*');
2794f33babfSAndreas Gohr    if (empty($opts['skipacl']) && $info['perm'] < AUTH_READ) {
2804f33babfSAndreas Gohr        return false;
2814f33babfSAndreas Gohr    }
2824f33babfSAndreas Gohr
2834f33babfSAndreas Gohr    //check pattern filter
2844f33babfSAndreas Gohr    if (!empty($opts['pattern']) && !@preg_match($opts['pattern'], $id)) {
2854f33babfSAndreas Gohr        return false;
2864f33babfSAndreas Gohr    }
2874f33babfSAndreas Gohr
28824870174SAndreas Gohr    $data[] = new MediaFile($id);
2894f33babfSAndreas Gohr    return false;
2904f33babfSAndreas Gohr}
2914f33babfSAndreas Gohr
2924f33babfSAndreas Gohr
2934f33babfSAndreas Gohr/**
294f3f0262cSandi * This function just lists documents (for RSS namespace export)
29515fae107Sandi *
29615fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
297f50a239bSTakamura *
298f50a239bSTakamura * @param array $data
299f50a239bSTakamura * @param string $base
300f50a239bSTakamura * @param string $file
301f50a239bSTakamura * @param string $type
302f50a239bSTakamura * @param integer $lvl
303f50a239bSTakamura * @param array $opts
304f50a239bSTakamura *
305f50a239bSTakamura * @return bool
306f3f0262cSandi */
307d868eb89SAndreas Gohrfunction search_list(&$data, $base, $file, $type, $lvl, $opts)
308d868eb89SAndreas Gohr{
309f3f0262cSandi    //we do nothing with directories
310f3f0262cSandi    if ($type == 'd') return false;
3110e1a261eSMichael Klier    //only search txt files
3120e1a261eSMichael Klier    if (substr($file, -4) == '.txt') {
313f3f0262cSandi        //check ACL
314f3f0262cSandi        $id = pathID($file);
315f3f0262cSandi        if (auth_quickaclcheck($id) < AUTH_READ) {
316f3f0262cSandi            return false;
317f3f0262cSandi        }
3180e1a261eSMichael Klier        $data[]['id'] = $id;
319f3f0262cSandi    }
320f3f0262cSandi    return false;
321f3f0262cSandi}
322f3f0262cSandi
323f3f0262cSandi/**
324f3f0262cSandi * Quicksearch for searching matching pagenames
325f3f0262cSandi *
326f3f0262cSandi * $opts['query'] is the search query
32715fae107Sandi *
32815fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
329f50a239bSTakamura *
330f50a239bSTakamura * @param array $data
331f50a239bSTakamura * @param string $base
332f50a239bSTakamura * @param string $file
333f50a239bSTakamura * @param string $type
334f50a239bSTakamura * @param integer $lvl
335f50a239bSTakamura * @param array $opts
336f50a239bSTakamura *
337f50a239bSTakamura * @return bool
338f3f0262cSandi */
339d868eb89SAndreas Gohrfunction search_pagename(&$data, $base, $file, $type, $lvl, $opts)
340d868eb89SAndreas Gohr{
341f3f0262cSandi    //we do nothing with directories
342f3f0262cSandi    if ($type == 'd') return true;
343f3f0262cSandi    //only search txt files
3440e1a261eSMichael Klier    if (substr($file, -4) != '.txt') return true;
345f3f0262cSandi
346f3f0262cSandi    //simple stringmatching
347396b7edbSmatthiasgrimm    if (!empty($opts['query'])) {
34824870174SAndreas Gohr        if (strpos($file, (string) $opts['query']) !== false) {
349f3f0262cSandi            //check ACL
350f3f0262cSandi            $id = pathID($file);
351f3f0262cSandi            if (auth_quickaclcheck($id) < AUTH_READ) {
352f3f0262cSandi                return false;
353f3f0262cSandi            }
354f3f0262cSandi            $data[]['id'] = $id;
355f3f0262cSandi        }
356396b7edbSmatthiasgrimm    }
357f3f0262cSandi    return true;
358f3f0262cSandi}
359f3f0262cSandi
360f3f0262cSandi/**
36158b6f612SAndreas Gohr * Just lists all documents
36258b6f612SAndreas Gohr *
3631fcfad4dSAndreas Gohr * $opts['depth']   recursion level, 0 for all
3641fcfad4dSAndreas Gohr * $opts['hash']    do md5 sum of content?
365224122cfSAndreas Gohr * $opts['skipacl'] list everything regardless of ACL
3661fcfad4dSAndreas Gohr *
36758b6f612SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
368f50a239bSTakamura *
369f50a239bSTakamura * @param array $data
370f50a239bSTakamura * @param string $base
371f50a239bSTakamura * @param string $file
372f50a239bSTakamura * @param string $type
373f50a239bSTakamura * @param integer $lvl
374f50a239bSTakamura * @param array $opts
375f50a239bSTakamura *
376f50a239bSTakamura * @return bool
37758b6f612SAndreas Gohr */
378d868eb89SAndreas Gohrfunction search_allpages(&$data, $base, $file, $type, $lvl, $opts)
379d868eb89SAndreas Gohr{
3808451f4adSGuillaume Turri    if (isset($opts['depth']) && $opts['depth']) {
381c647387eSGuillaume Turri        $parts = explode('/', ltrim($file, '/'));
382*7d34963bSAndreas Gohr        if (
383*7d34963bSAndreas Gohr            ($type == 'd' && count($parts) >= $opts['depth'])
384*7d34963bSAndreas Gohr            || ($type != 'd' && count($parts) > $opts['depth'])
385*7d34963bSAndreas Gohr        ) {
386c647387eSGuillaume Turri            return false; // depth reached
387c647387eSGuillaume Turri        }
388c647387eSGuillaume Turri    }
389c647387eSGuillaume Turri
39058b6f612SAndreas Gohr    //we do nothing with directories
3911fcfad4dSAndreas Gohr    if ($type == 'd') {
3921fcfad4dSAndreas Gohr        return true;
3931fcfad4dSAndreas Gohr    }
3941fcfad4dSAndreas Gohr
39558b6f612SAndreas Gohr    //only search txt files
3960e1a261eSMichael Klier    if (substr($file, -4) != '.txt') return true;
39758b6f612SAndreas Gohr
39824870174SAndreas Gohr    $item = [];
3991fcfad4dSAndreas Gohr    $item['id']   = pathID($file);
40077244e70SMichael Hamann    if (empty($opts['skipacl']) && auth_quickaclcheck($item['id']) < AUTH_READ) {
4011fcfad4dSAndreas Gohr        return false;
4021fcfad4dSAndreas Gohr    }
4031fcfad4dSAndreas Gohr
4041fcfad4dSAndreas Gohr    $item['rev']   = filemtime($base.'/'.$file);
405224122cfSAndreas Gohr    $item['mtime'] = $item['rev'];
4061fcfad4dSAndreas Gohr    $item['size']  = filesize($base.'/'.$file);
4078f34cf3dSMichael Große    if (!empty($opts['hash'])) {
4081fcfad4dSAndreas Gohr        $item['hash'] = md5(trim(rawWiki($item['id'])));
4091fcfad4dSAndreas Gohr    }
4101fcfad4dSAndreas Gohr
4111fcfad4dSAndreas Gohr    $data[] = $item;
41258b6f612SAndreas Gohr    return true;
41358b6f612SAndreas Gohr}
41458b6f612SAndreas Gohr
415b59a406bSmatthiasgrimm/* ------------- helper functions below -------------- */
416b59a406bSmatthiasgrimm
417b59a406bSmatthiasgrimm/**
41815fae107Sandi * fulltext sort
41915fae107Sandi *
420f3f0262cSandi * Callback sort function for use with usort to sort the data
421f3f0262cSandi * structure created by search_fulltext. Sorts descending by count
42215fae107Sandi *
42315fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
424f50a239bSTakamura *
425f50a239bSTakamura * @param array $a
426f50a239bSTakamura * @param array $b
427f50a239bSTakamura *
428f50a239bSTakamura * @return int
429f3f0262cSandi */
430d868eb89SAndreas Gohrfunction sort_search_fulltext($a, $b)
431d868eb89SAndreas Gohr{
432f3f0262cSandi    if ($a['count'] > $b['count']) {
433f3f0262cSandi        return -1;
434f3f0262cSandi    } elseif ($a['count'] < $b['count']) {
435f3f0262cSandi        return 1;
436f3f0262cSandi    } else {
4372d85e841SAndreas Gohr        return Sort::strcmp($a['id'], $b['id']);
438f3f0262cSandi    }
439f3f0262cSandi}
440f3f0262cSandi
441f3f0262cSandi/**
442f3f0262cSandi * translates a document path to an ID
44315fae107Sandi *
44415fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
44537e34a5eSandi * @todo    move to pageutils
446f50a239bSTakamura *
447f50a239bSTakamura * @param string $path
448f50a239bSTakamura * @param bool $keeptxt
449f50a239bSTakamura *
450f50a239bSTakamura * @return mixed|string
451f3f0262cSandi */
452d868eb89SAndreas Gohrfunction pathID($path, $keeptxt = false)
453d868eb89SAndreas Gohr{
45449c713a3Sandi    $id = utf8_decodeFN($path);
45549c713a3Sandi    $id = str_replace('/', ':', $id);
456156a608cSandi    if (!$keeptxt) $id = preg_replace('#\.txt$#', '', $id);
457709b1063SAdrian Lang    $id = trim($id, ':');
458f3f0262cSandi    return $id;
459f3f0262cSandi}
460f3f0262cSandi
461340756e4Sandi
4623abeade3SAndreas Gohr/**
4633abeade3SAndreas Gohr * This is a very universal callback for the search() function, replacing
4643abeade3SAndreas Gohr * many of the former individual functions at the cost of a more complex
4653abeade3SAndreas Gohr * setup.
4663abeade3SAndreas Gohr *
4673abeade3SAndreas Gohr * How the function behaves, depends on the options passed in the $opts
4683abeade3SAndreas Gohr * array, where the following settings can be used.
4693abeade3SAndreas Gohr *
470e14fe973SGerrit Uitslag * depth      int     recursion depth. 0 for unlimited                       (default: 0)
471e14fe973SGerrit Uitslag * keeptxt    bool    keep .txt extension for IDs                            (default: false)
472e14fe973SGerrit Uitslag * listfiles  bool    include files in listing                               (default: false)
473e14fe973SGerrit Uitslag * listdirs   bool    include namespaces in listing                          (default: false)
474e14fe973SGerrit Uitslag * pagesonly  bool    restrict files to pages                                (default: false)
475e14fe973SGerrit Uitslag * skipacl    bool    do not check for READ permission                       (default: false)
476e14fe973SGerrit Uitslag * sneakyacl  bool    don't recurse into nonreadable dirs                    (default: false)
477e14fe973SGerrit Uitslag * hash       bool    create MD5 hash for files                              (default: false)
478e14fe973SGerrit Uitslag * meta       bool    return file metadata                                   (default: false)
479e14fe973SGerrit Uitslag * filematch  string  match files against this regexp                        (default: '', so accept everything)
480e14fe973SGerrit Uitslag * idmatch    string  match full ID against this regexp                      (default: '', so accept everything)
481e14fe973SGerrit Uitslag * dirmatch   string  match directory against this regexp when adding        (default: '', so accept everything)
482e14fe973SGerrit Uitslag * nsmatch    string  match namespace against this regexp when adding        (default: '', so accept everything)
483e14fe973SGerrit Uitslag * recmatch   string  match directory against this regexp when recursing     (default: '', so accept everything)
484e14fe973SGerrit Uitslag * showmsg    bool    warn about non-ID files                                (default: false)
485e14fe973SGerrit Uitslag * showhidden bool    show hidden files(e.g. by hidepages config) too        (default: false)
486e14fe973SGerrit Uitslag * firsthead  bool    return first heading for pages                         (default: false)
4873abeade3SAndreas Gohr *
488ce4301e3SGerrit Uitslag * @param array &$data  - Reference to the result data structure
489ce4301e3SGerrit Uitslag * @param string $base  - Base usually $conf['datadir']
490ce4301e3SGerrit Uitslag * @param string $file  - current file or directory relative to $base
491ce4301e3SGerrit Uitslag * @param string $type  - Type either 'd' for directory or 'f' for file
492ce4301e3SGerrit Uitslag * @param int    $lvl   - Current recursion depht
493ce4301e3SGerrit Uitslag * @param array  $opts  - option array as given to search()
494ce4301e3SGerrit Uitslag * @return bool if this directory should be traversed (true) or not (false)
495ce4301e3SGerrit Uitslag *              return value is ignored for files
496ce4301e3SGerrit Uitslag *
4973abeade3SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
4983abeade3SAndreas Gohr */
499d868eb89SAndreas Gohrfunction search_universal(&$data, $base, $file, $type, $lvl, $opts)
500d868eb89SAndreas Gohr{
50124870174SAndreas Gohr    $item   = [];
5023abeade3SAndreas Gohr    $return = true;
5033abeade3SAndreas Gohr
5043abeade3SAndreas Gohr    // get ID and check if it is a valid one
505b7a3421aSChristopher Smith    $item['id'] = pathID($file, ($type == 'd' || !empty($opts['keeptxt'])));
5068537abd1SAdrian Lang    if ($item['id'] != cleanID($item['id'])) {
50749f299d6SChristopher Smith        if (!empty($opts['showmsg'])) {
5088537abd1SAdrian Lang            msg(hsc($item['id']).' is not a valid file name for DokuWiki - skipped', -1);
509b7a3421aSChristopher Smith        }
5103abeade3SAndreas Gohr        return false; // skip non-valid files
5113abeade3SAndreas Gohr    }
5128705cc81SAndreas Gohr    $item['ns']  = getNS($item['id']);
5133abeade3SAndreas Gohr
5143abeade3SAndreas Gohr    if ($type == 'd') {
5153abeade3SAndreas Gohr        // decide if to recursion into this directory is wanted
5160e80bb5eSChristopher Smith        if (empty($opts['depth'])) {
5173abeade3SAndreas Gohr            $return = true; // recurse forever
5183abeade3SAndreas Gohr        } else {
5193abeade3SAndreas Gohr            $depth = substr_count($file, '/');
5203abeade3SAndreas Gohr            if ($depth >= $opts['depth']) {
5213abeade3SAndreas Gohr                $return = false; // depth reached
5223abeade3SAndreas Gohr            } else {
5233abeade3SAndreas Gohr                $return = true;
5243abeade3SAndreas Gohr            }
5253abeade3SAndreas Gohr        }
5269b4337c6SChristopher Smith
5279b4337c6SChristopher Smith        if ($return) {
5289b4337c6SChristopher Smith            $match = empty($opts['recmatch']) || preg_match('/'.$opts['recmatch'].'/', $file);
5299b4337c6SChristopher Smith            if (!$match) {
5309b4337c6SChristopher Smith                return false; // doesn't match
5319b4337c6SChristopher Smith            }
5323abeade3SAndreas Gohr        }
5333abeade3SAndreas Gohr    }
5343abeade3SAndreas Gohr
5353abeade3SAndreas Gohr    // check ACL
536443e135dSChristopher Smith    if (empty($opts['skipacl'])) {
5373abeade3SAndreas Gohr        if ($type == 'd') {
5383abeade3SAndreas Gohr            $item['perm'] = auth_quickaclcheck($item['id'].':*');
5393abeade3SAndreas Gohr        } else {
5403abeade3SAndreas Gohr            $item['perm'] = auth_quickaclcheck($item['id']); //FIXME check namespace for media files
5413abeade3SAndreas Gohr        }
5423abeade3SAndreas Gohr    } else {
5433abeade3SAndreas Gohr        $item['perm'] = AUTH_DELETE;
5443abeade3SAndreas Gohr    }
5453abeade3SAndreas Gohr
5463abeade3SAndreas Gohr    // are we done here maybe?
5473abeade3SAndreas Gohr    if ($type == 'd') {
548443e135dSChristopher Smith        if (empty($opts['listdirs'])) return $return;
54964159a61SAndreas Gohr        //neither list nor recurse forbidden items:
55064159a61SAndreas Gohr        if (empty($opts['skipacl']) && !empty($opts['sneakyacl']) && $item['perm'] < AUTH_READ) return false;
551443e135dSChristopher Smith        if (!empty($opts['dirmatch']) && !preg_match('/'.$opts['dirmatch'].'/', $file)) return $return;
552443e135dSChristopher Smith        if (!empty($opts['nsmatch']) && !preg_match('/'.$opts['nsmatch'].'/', $item['ns'])) return $return;
5533abeade3SAndreas Gohr    } else {
554443e135dSChristopher Smith        if (empty($opts['listfiles'])) return $return;
555443e135dSChristopher Smith        if (empty($opts['skipacl']) && $item['perm'] < AUTH_READ) return $return;
556443e135dSChristopher Smith        if (!empty($opts['pagesonly']) && (substr($file, -4) != '.txt')) return $return;
557443e135dSChristopher Smith        if (empty($opts['showhidden']) && isHiddenPage($item['id'])) return $return;
558443e135dSChristopher Smith        if (!empty($opts['filematch']) && !preg_match('/'.$opts['filematch'].'/', $file)) return $return;
559443e135dSChristopher Smith        if (!empty($opts['idmatch']) && !preg_match('/'.$opts['idmatch'].'/', $item['id'])) return $return;
5603abeade3SAndreas Gohr    }
5613abeade3SAndreas Gohr
5623abeade3SAndreas Gohr    // still here? prepare the item
5633abeade3SAndreas Gohr    $item['type']  = $type;
56432d6093dSAndreas Gohr    $item['level'] = $lvl;
5653abeade3SAndreas Gohr    $item['open']  = $return;
5663abeade3SAndreas Gohr
5670e80bb5eSChristopher Smith    if (!empty($opts['meta'])) {
56824870174SAndreas Gohr        $item['file']       = PhpString::basename($file);
5693abeade3SAndreas Gohr        $item['size']       = filesize($base.'/'.$file);
5703abeade3SAndreas Gohr        $item['mtime']      = filemtime($base.'/'.$file);
5713abeade3SAndreas Gohr        $item['rev']        = $item['mtime'];
5723abeade3SAndreas Gohr        $item['writable']   = is_writable($base.'/'.$file);
5733abeade3SAndreas Gohr        $item['executable'] = is_executable($base.'/'.$file);
5743abeade3SAndreas Gohr    }
5753abeade3SAndreas Gohr
5763abeade3SAndreas Gohr    if ($type == 'f') {
5770e80bb5eSChristopher Smith        if (!empty($opts['hash'])) $item['hash'] = md5(io_readFile($base.'/'.$file, false));
5780e80bb5eSChristopher Smith        if (!empty($opts['firsthead'])) $item['title'] = p_get_first_heading($item['id'], METADATA_DONT_RENDER);
5793abeade3SAndreas Gohr    }
5803abeade3SAndreas Gohr
5813abeade3SAndreas Gohr    // finally add the item
5823abeade3SAndreas Gohr    $data[] = $item;
5833abeade3SAndreas Gohr    return $return;
5843abeade3SAndreas Gohr}
5853abeade3SAndreas Gohr
586e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
587