xref: /dokuwiki/inc/search.php (revision 7e687fd85a40bd8453b39b64bae8e989ab32fd36)
1ed7b5f09Sandi<?php
2d4f83172SAndreas Gohr
315fae107Sandi/**
415fae107Sandi * DokuWiki search functions
515fae107Sandi *
615fae107Sandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
715fae107Sandi * @author     Andreas Gohr <andi@splitbrain.org>
815fae107Sandi */
9f3f0262cSandi
1024870174SAndreas Gohruse dokuwiki\Utf8\PhpString;
1124870174SAndreas Gohruse dokuwiki\File\MediaFile;
122d85e841SAndreas Gohruse dokuwiki\Utf8\Sort;
132d85e841SAndreas Gohr
14f3f0262cSandi/**
15ce4301e3SGerrit Uitslag * Recurse directory
1615fae107Sandi *
17f3f0262cSandi * This function recurses into a given base directory
18f3f0262cSandi * and calls the supplied function for each file and directory
1915fae107Sandi *
2024998b31SGerrit Uitslag * @param   array    &$data The results of the search are stored here
2124baa045SAndreas Gohr * @param   string    $base Where to start the search
22fe82d751SChristopher Smith * @param   callback  $func Callback (function name or array with object,method)
2324998b31SGerrit Uitslag * @param   array     $opts option array will be given to the Callback
2424baa045SAndreas Gohr * @param   string    $dir  Current directory beyond $base
2524baa045SAndreas Gohr * @param   int       $lvl  Recursion Level
2664159a61SAndreas Gohr * @param   mixed     $sort 'natural' to use natural order sorting (default);
2764159a61SAndreas Gohr *                          'date' to sort by filemtime; leave empty to skip sorting.
2815fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
29f3f0262cSandi */
3083198f92SSatoshi Saharafunction search(&$data, $base, $func, $opts, $dir = '', $lvl = 1, $sort = 'natural')
3183198f92SSatoshi Sahara{
3224870174SAndreas Gohr    $dirs   = [];
3324870174SAndreas Gohr    $files  = [];
3424870174SAndreas Gohr    $filepaths = [];
35f3f0262cSandi
36e0b6aadeSAndreas Gohr    // safeguard against runaways #1452
37e0b6aadeSAndreas Gohr    if ($base == '' || $base == '/') {
38e0b6aadeSAndreas Gohr        throw new RuntimeException('No valid $base passed to search() - possible misconfiguration or bug');
39e0b6aadeSAndreas Gohr    }
40e0b6aadeSAndreas Gohr
41f3f0262cSandi    //read in directories and files
42f3f0262cSandi    $dh = @opendir($base . '/' . $dir);
43f3f0262cSandi    if (!$dh) return;
44f3f0262cSandi    while (($file = readdir($dh)) !== false) {
45de3dfc91Sandi        if (preg_match('/^[\._]/', $file)) continue; //skip hidden files and upper dirs
46f3f0262cSandi        if (is_dir($base . '/' . $dir . '/' . $file)) {
47f3f0262cSandi            $dirs[] = $dir . '/' . $file;
48f3f0262cSandi            continue;
49f3f0262cSandi        }
50f3f0262cSandi        $files[] = $dir . '/' . $file;
51abc306f4SKate Arzamastseva        $filepaths[] = $base . '/' . $dir . '/' . $file;
52f3f0262cSandi    }
53f3f0262cSandi    closedir($dh);
54ec24a2dfSPhilipp A. Hartmann    if (!empty($sort)) {
55abc306f4SKate Arzamastseva        if ($sort == 'date') {
56093fe67eSAndreas Gohr            @array_multisort(array_map(filemtime(...), $filepaths), SORT_NUMERIC, SORT_DESC, $files);
571dc5d48bSChristopher Smith        } else /* natural */ {
582d85e841SAndreas Gohr            Sort::asortFN($files);
59abc306f4SKate Arzamastseva        }
602d85e841SAndreas Gohr        Sort::asortFN($dirs);
61ec24a2dfSPhilipp A. Hartmann    }
62f3f0262cSandi
63f3f0262cSandi    //give directories to userfunction then recurse
64f3f0262cSandi    foreach ($dirs as $dir) {
6524870174SAndreas Gohr        if (call_user_func_array($func, [&$data, $base, $dir, 'd', $lvl, $opts])) {
665514a5a7SChristopher Smith            search($data, $base, $func, $opts, $dir, $lvl + 1, $sort);
67f3f0262cSandi        }
68f3f0262cSandi    }
69f3f0262cSandi    //now handle the files
70f3f0262cSandi    foreach ($files as $file) {
7124870174SAndreas Gohr        call_user_func_array($func, [&$data, $base, $file, 'f', $lvl, $opts]);
72f3f0262cSandi    }
73f3f0262cSandi}
74f3f0262cSandi
75f3f0262cSandi/**
76f3f0262cSandi * The following functions are userfunctions to use with the search
77f3f0262cSandi * function above. This function is called for every found file or
78f3f0262cSandi * directory. When a directory is given to the function it has to
79f3f0262cSandi * decide if this directory should be traversed (true) or not (false)
80f3f0262cSandi * The function has to accept the following parameters:
81f3f0262cSandi *
82ce4301e3SGerrit Uitslag * array &$data  - Reference to the result data structure
83ce4301e3SGerrit Uitslag * string $base  - Base usually $conf['datadir']
84ce4301e3SGerrit Uitslag * string $file  - current file or directory relative to $base
85ce4301e3SGerrit Uitslag * string $type  - Type either 'd' for directory or 'f' for file
86ce4301e3SGerrit Uitslag * int    $lvl   - Current recursion depht
87ce4301e3SGerrit Uitslag * array  $opts  - option array as given to search()
88f3f0262cSandi *
89f3f0262cSandi * return values for files are ignored
90f3f0262cSandi *
91f3f0262cSandi * All functions should check the ACL for document READ rights
92783d2e49SAdrian Lang * namespaces (directories) are NOT checked (when sneaky_index is 0) as this
93783d2e49SAdrian Lang * would break the recursion (You can have an nonreadable dir over a readable
940e1a261eSMichael Klier * one deeper nested) also make sure to check the file type (for example
950e1a261eSMichael Klier * in case of lockfiles).
96f3f0262cSandi */
97f3f0262cSandi
98f3f0262cSandi/**
9963f2400bSandi * Searches for pages beginning with the given query
10063f2400bSandi *
10163f2400bSandi * @author Andreas Gohr <andi@splitbrain.org>
102f50a239bSTakamura *
103f50a239bSTakamura * @param array $data
104f50a239bSTakamura * @param string $base
105f50a239bSTakamura * @param string $file
106f50a239bSTakamura * @param string $type
107f50a239bSTakamura * @param integer $lvl
108f50a239bSTakamura * @param array $opts
109f50a239bSTakamura *
110f50a239bSTakamura * @return bool
11163f2400bSandi */
11283198f92SSatoshi Saharafunction search_qsearch(&$data, $base, $file, $type, $lvl, $opts)
11383198f92SSatoshi Sahara{
11424870174SAndreas Gohr    $opts = [
1158705cc81SAndreas Gohr        'idmatch'   => '(^|:)' . preg_quote($opts['query'], '/') . '/',
1168705cc81SAndreas Gohr        'listfiles' => true,
11724870174SAndreas Gohr        'pagesonly' => true
11824870174SAndreas Gohr    ];
1198705cc81SAndreas Gohr    return search_universal($data, $base, $file, $type, $lvl, $opts);
12063f2400bSandi}
12163f2400bSandi
12263f2400bSandi/**
12315fae107Sandi * Build the browsable index of pages
124f3f0262cSandi *
125783d2e49SAdrian Lang * $opts['ns'] is the currently viewed namespace
12615fae107Sandi *
12715fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
128f50a239bSTakamura *
129f50a239bSTakamura * @param array $data
130f50a239bSTakamura * @param string $base
131f50a239bSTakamura * @param string $file
132f50a239bSTakamura * @param string $type
133f50a239bSTakamura * @param integer $lvl
134f50a239bSTakamura * @param array $opts
135f50a239bSTakamura *
136f50a239bSTakamura * @return bool
137f3f0262cSandi */
13883198f92SSatoshi Saharafunction search_index(&$data, $base, $file, $type, $lvl, $opts)
13983198f92SSatoshi Sahara{
140d1c7b6ecSAndreas Gohr    global $conf;
14124870174SAndreas Gohr    $ns = $opts['ns'] ?? '';
14224870174SAndreas Gohr    $opts = [
143783d2e49SAdrian Lang        'pagesonly' => true,
144783d2e49SAdrian Lang        'listdirs' => true,
145443e135dSChristopher Smith        'listfiles' => empty($opts['nofiles']),
146783d2e49SAdrian Lang        'sneakyacl' => $conf['sneaky_index'],
147783d2e49SAdrian Lang        // Hacky, should rather use recmatch
14824870174SAndreas Gohr        'depth' => preg_match('#^' . preg_quote($file, '#') . '(/|$)#', '/' . $ns) ? 0 : -1,
14924870174SAndreas Gohr    ];
150f3f0262cSandi
151783d2e49SAdrian Lang    return search_universal($data, $base, $file, $type, $lvl, $opts);
152f3f0262cSandi}
153f3f0262cSandi
154f3f0262cSandi/**
15515fae107Sandi * List all namespaces
15615fae107Sandi *
15715fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
158f50a239bSTakamura *
159f50a239bSTakamura * @param array $data
160f50a239bSTakamura * @param string $base
161f50a239bSTakamura * @param string $file
162f50a239bSTakamura * @param string $type
163f50a239bSTakamura * @param integer $lvl
164f50a239bSTakamura * @param array $opts
165f50a239bSTakamura *
166f50a239bSTakamura * @return bool
167f3f0262cSandi */
16883198f92SSatoshi Saharafunction search_namespaces(&$data, $base, $file, $type, $lvl, $opts)
16983198f92SSatoshi Sahara{
17024870174SAndreas Gohr    $opts = ['listdirs' => true];
1718705cc81SAndreas Gohr    return search_universal($data, $base, $file, $type, $lvl, $opts);
172f3f0262cSandi}
173f3f0262cSandi
174f3f0262cSandi/**
17515fae107Sandi * List all mediafiles in a namespace
17642ea7f44SGerrit Uitslag *   $opts['depth']     recursion level, 0 for all
17742ea7f44SGerrit Uitslag *   $opts['showmsg']   shows message if invalid media id is used
17842ea7f44SGerrit Uitslag *   $opts['skipacl']   skip acl checking
17942ea7f44SGerrit Uitslag *   $opts['pattern']   check given pattern
18042ea7f44SGerrit Uitslag *   $opts['hash']      add hashes to result list
18115fae107Sandi *
18215fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
183f50a239bSTakamura *
184f50a239bSTakamura * @param array $data
185f50a239bSTakamura * @param string $base
186f50a239bSTakamura * @param string $file
187f50a239bSTakamura * @param string $type
188f50a239bSTakamura * @param integer $lvl
189f50a239bSTakamura * @param array $opts
190f50a239bSTakamura *
191f50a239bSTakamura * @return bool
192f3f0262cSandi */
19383198f92SSatoshi Saharafunction search_media(&$data, $base, $file, $type, $lvl, $opts)
19483198f92SSatoshi Sahara{
195f3f0262cSandi    //we do nothing with directories
1961a49ac65SGina Haeussge    if ($type == 'd') {
1970e80bb5eSChristopher Smith        if (empty($opts['depth'])) return true; // recurse forever
19878315408SAndreas Gohr        $depth = substr_count($file, '/');
199b8219d2dSAndreas Gohr        if ($depth >= $opts['depth']) return false; // depth reached
200224122cfSAndreas Gohr        return true;
2011a49ac65SGina Haeussge    }
202f3f0262cSandi
20324870174SAndreas Gohr    $info         = [];
204156a608cSandi    $info['id']   = pathID($file, true);
205c347b097Ssplitbrain    if ($info['id'] !== cleanID($info['id'])) {
2062b9be456SAndreas Gohr        if (!empty($opts['showmsg']))
20764807c84SAndreas Gohr            msg(hsc($info['id']) . ' is not a valid file name for DokuWiki - skipped', -1);
20864807c84SAndreas Gohr        return false; // skip non-valid files
20964807c84SAndreas Gohr    }
210f3f0262cSandi
211f3f0262cSandi    //check ACL for namespace (we have no ACL for mediafiles)
212*7e687fd8SAndreas Gohr    $info['perm'] = auth_quickaclcheck(mediaAclPath($info['id']));
2130e80bb5eSChristopher Smith    if (empty($opts['skipacl']) && $info['perm'] < AUTH_READ) {
214224122cfSAndreas Gohr        return false;
215224122cfSAndreas Gohr    }
216224122cfSAndreas Gohr
217224122cfSAndreas Gohr    //check pattern filter
2180e80bb5eSChristopher Smith    if (!empty($opts['pattern']) && !@preg_match($opts['pattern'], $info['id'])) {
219f3f0262cSandi        return false;
220f3f0262cSandi    }
221f3f0262cSandi
22224870174SAndreas Gohr    $info['file']     = PhpString::basename($file);
223f3f0262cSandi    $info['size']     = filesize($base . '/' . $file);
2245e7fa82eSAndreas Gohr    $info['mtime']    = filemtime($base . '/' . $file);
2253df72098SAndreas Gohr    $info['writable'] = is_writable($base . '/' . $file);
226f3f0262cSandi    if (preg_match("/\.(jpe?g|gif|png)$/", $file)) {
227f3f0262cSandi        $info['isimg'] = true;
22823a34783SAndreas Gohr        $info['meta']  = new JpegMeta($base . '/' . $file);
229f3f0262cSandi    } else {
230f3f0262cSandi        $info['isimg'] = false;
231f3f0262cSandi    }
2320e80bb5eSChristopher Smith    if (!empty($opts['hash'])) {
233dfd343c4SAndreas Gohr        $info['hash'] = md5(io_readFile(mediaFN($info['id']), false));
234224122cfSAndreas Gohr    }
235224122cfSAndreas Gohr
236f3f0262cSandi    $data[] = $info;
237f3f0262cSandi
238f3f0262cSandi    return false;
239f3f0262cSandi}
240f3f0262cSandi
241f3f0262cSandi/**
2424f33babfSAndreas Gohr * List all mediafiles in a namespace
2434f33babfSAndreas Gohr *   $opts['depth']     recursion level, 0 for all
2444f33babfSAndreas Gohr *   $opts['showmsg']   shows message if invalid media id is used
2454f33babfSAndreas Gohr *   $opts['skipacl']   skip acl checking
2464f33babfSAndreas Gohr *   $opts['pattern']   check given pattern
2474f33babfSAndreas Gohr *   $opts['hash']      add hashes to result list
2484f33babfSAndreas Gohr *
2494f33babfSAndreas Gohr * @todo This is a temporary copy of search_media returning a list of MediaFile intances
2504f33babfSAndreas Gohr *
2514f33babfSAndreas Gohr * @param array $data
2524f33babfSAndreas Gohr * @param string $base
2534f33babfSAndreas Gohr * @param string $file
2544f33babfSAndreas Gohr * @param string $type
2554f33babfSAndreas Gohr * @param integer $lvl
2564f33babfSAndreas Gohr * @param array $opts
2574f33babfSAndreas Gohr *
2584f33babfSAndreas Gohr * @return bool
2594f33babfSAndreas Gohr */
260d868eb89SAndreas Gohrfunction search_mediafiles(&$data, $base, $file, $type, $lvl, $opts)
261d868eb89SAndreas Gohr{
2624f33babfSAndreas Gohr
2634f33babfSAndreas Gohr    //we do nothing with directories
2644f33babfSAndreas Gohr    if ($type == 'd') {
2654f33babfSAndreas Gohr        if (empty($opts['depth'])) return true; // recurse forever
2664f33babfSAndreas Gohr        $depth = substr_count($file, '/');
2674f33babfSAndreas Gohr        if ($depth >= $opts['depth']) return false; // depth reached
2684f33babfSAndreas Gohr        return true;
2694f33babfSAndreas Gohr    }
2704f33babfSAndreas Gohr
2714f33babfSAndreas Gohr    $id   = pathID($file, true);
2724f33babfSAndreas Gohr    if ($id != cleanID($id)) {
2734f33babfSAndreas Gohr        if ($opts['showmsg'])
2744f33babfSAndreas Gohr            msg(hsc($id) . ' is not a valid file name for DokuWiki - skipped', -1);
2754f33babfSAndreas Gohr        return false; // skip non-valid files
2764f33babfSAndreas Gohr    }
2774f33babfSAndreas Gohr
2784f33babfSAndreas Gohr    //check ACL for namespace (we have no ACL for mediafiles)
279*7e687fd8SAndreas Gohr    $info['perm'] = auth_quickaclcheck(mediaAclPath($id));
2804f33babfSAndreas Gohr    if (empty($opts['skipacl']) && $info['perm'] < AUTH_READ) {
2814f33babfSAndreas Gohr        return false;
2824f33babfSAndreas Gohr    }
2834f33babfSAndreas Gohr
2844f33babfSAndreas Gohr    //check pattern filter
2854f33babfSAndreas Gohr    if (!empty($opts['pattern']) && !@preg_match($opts['pattern'], $id)) {
2864f33babfSAndreas Gohr        return false;
2874f33babfSAndreas Gohr    }
2884f33babfSAndreas Gohr
28924870174SAndreas Gohr    $data[] = new MediaFile($id);
2904f33babfSAndreas Gohr    return false;
2914f33babfSAndreas Gohr}
2924f33babfSAndreas Gohr
2934f33babfSAndreas Gohr
2944f33babfSAndreas Gohr/**
295f3f0262cSandi * This function just lists documents (for RSS namespace export)
29615fae107Sandi *
29715fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
298f50a239bSTakamura *
299f50a239bSTakamura * @param array $data
300f50a239bSTakamura * @param string $base
301f50a239bSTakamura * @param string $file
302f50a239bSTakamura * @param string $type
303f50a239bSTakamura * @param integer $lvl
304f50a239bSTakamura * @param array $opts
305f50a239bSTakamura *
306f50a239bSTakamura * @return bool
307f3f0262cSandi */
30883198f92SSatoshi Saharafunction search_list(&$data, $base, $file, $type, $lvl, $opts)
30983198f92SSatoshi Sahara{
310f3f0262cSandi    //we do nothing with directories
311f3f0262cSandi    if ($type == 'd') return false;
3120e1a261eSMichael Klier    //only search txt files
3136c16a3a9Sfiwswe    if (str_ends_with($file, '.txt')) {
314f3f0262cSandi        //check ACL
315f3f0262cSandi        $id = pathID($file);
316f3f0262cSandi        if (auth_quickaclcheck($id) < AUTH_READ) {
317f3f0262cSandi            return false;
318f3f0262cSandi        }
3190e1a261eSMichael Klier        $data[]['id'] = $id;
320f3f0262cSandi    }
321f3f0262cSandi    return false;
322f3f0262cSandi}
323f3f0262cSandi
324f3f0262cSandi/**
325f3f0262cSandi * Quicksearch for searching matching pagenames
326f3f0262cSandi *
327f3f0262cSandi * $opts['query'] is the search query
32815fae107Sandi *
32915fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
330f50a239bSTakamura *
331f50a239bSTakamura * @param array $data
332f50a239bSTakamura * @param string $base
333f50a239bSTakamura * @param string $file
334f50a239bSTakamura * @param string $type
335f50a239bSTakamura * @param integer $lvl
336f50a239bSTakamura * @param array $opts
337f50a239bSTakamura *
338f50a239bSTakamura * @return bool
339f3f0262cSandi */
34083198f92SSatoshi Saharafunction search_pagename(&$data, $base, $file, $type, $lvl, $opts)
34183198f92SSatoshi Sahara{
342f3f0262cSandi    //we do nothing with directories
343f3f0262cSandi    if ($type == 'd') return true;
344f3f0262cSandi    //only search txt files
3456c16a3a9Sfiwswe    if (!str_ends_with($file, '.txt')) return true;
346f3f0262cSandi
347f3f0262cSandi    //simple stringmatching
348396b7edbSmatthiasgrimm    if (!empty($opts['query'])) {
349093fe67eSAndreas Gohr        if (str_contains($file, (string) $opts['query'])) {
350f3f0262cSandi            //check ACL
351f3f0262cSandi            $id = pathID($file);
352f3f0262cSandi            if (auth_quickaclcheck($id) < AUTH_READ) {
353f3f0262cSandi                return false;
354f3f0262cSandi            }
355f3f0262cSandi            $data[]['id'] = $id;
356f3f0262cSandi        }
357396b7edbSmatthiasgrimm    }
358f3f0262cSandi    return true;
359f3f0262cSandi}
360f3f0262cSandi
361f3f0262cSandi/**
36258b6f612SAndreas Gohr * Just lists all documents
36358b6f612SAndreas Gohr *
3641fcfad4dSAndreas Gohr * $opts['depth']   recursion level, 0 for all
3651fcfad4dSAndreas Gohr * $opts['hash']    do md5 sum of content?
366224122cfSAndreas Gohr * $opts['skipacl'] list everything regardless of ACL
3671fcfad4dSAndreas Gohr *
36858b6f612SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
369f50a239bSTakamura *
370f50a239bSTakamura * @param array $data
371f50a239bSTakamura * @param string $base
372f50a239bSTakamura * @param string $file
373f50a239bSTakamura * @param string $type
374f50a239bSTakamura * @param integer $lvl
375f50a239bSTakamura * @param array $opts
376f50a239bSTakamura *
377f50a239bSTakamura * @return bool
37858b6f612SAndreas Gohr */
37983198f92SSatoshi Saharafunction search_allpages(&$data, $base, $file, $type, $lvl, $opts)
38083198f92SSatoshi Sahara{
38191560755SAndreas Gohr    if (($opts['depth'] ?? 0) > 0) {
382c647387eSGuillaume Turri        $parts = explode('/', ltrim($file, '/'));
3837d34963bSAndreas Gohr        if (
3847d34963bSAndreas Gohr            ($type == 'd' && count($parts) >= $opts['depth'])
38583198f92SSatoshi Sahara            || ($type != 'd' && count($parts) > $opts['depth'])
38683198f92SSatoshi Sahara        ) {
387c647387eSGuillaume Turri            return false; // depth reached
388c647387eSGuillaume Turri        }
389c647387eSGuillaume Turri    }
390c647387eSGuillaume Turri
39158b6f612SAndreas Gohr    //we do nothing with directories
3921fcfad4dSAndreas Gohr    if ($type == 'd') {
3931fcfad4dSAndreas Gohr        return true;
3941fcfad4dSAndreas Gohr    }
3951fcfad4dSAndreas Gohr
39658b6f612SAndreas Gohr    //only search txt files
3976c16a3a9Sfiwswe    if (!str_ends_with($file, '.txt')) return true;
39858b6f612SAndreas Gohr
39924870174SAndreas Gohr    $item = [];
4001fcfad4dSAndreas Gohr    $item['id']   = pathID($file);
40177244e70SMichael Hamann    if (empty($opts['skipacl']) && auth_quickaclcheck($item['id']) < AUTH_READ) {
4021fcfad4dSAndreas Gohr        return false;
4031fcfad4dSAndreas Gohr    }
4041fcfad4dSAndreas Gohr
4051fcfad4dSAndreas Gohr    $item['rev']   = filemtime($base . '/' . $file);
406224122cfSAndreas Gohr    $item['mtime'] = $item['rev'];
4071fcfad4dSAndreas Gohr    $item['size']  = filesize($base . '/' . $file);
4088f34cf3dSMichael Große    if (!empty($opts['hash'])) {
4091fcfad4dSAndreas Gohr        $item['hash'] = md5(trim(rawWiki($item['id'])));
4101fcfad4dSAndreas Gohr    }
4111fcfad4dSAndreas Gohr
4121fcfad4dSAndreas Gohr    $data[] = $item;
41358b6f612SAndreas Gohr    return true;
41458b6f612SAndreas Gohr}
41558b6f612SAndreas Gohr
416b59a406bSmatthiasgrimm/* ------------- helper functions below -------------- */
417b59a406bSmatthiasgrimm
418b59a406bSmatthiasgrimm/**
41915fae107Sandi * fulltext sort
42015fae107Sandi *
421f3f0262cSandi * Callback sort function for use with usort to sort the data
422f3f0262cSandi * structure created by search_fulltext. Sorts descending by count
42315fae107Sandi *
42415fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
425f50a239bSTakamura *
426f50a239bSTakamura * @param array $a
427f50a239bSTakamura * @param array $b
428f50a239bSTakamura *
429f50a239bSTakamura * @return int
430f3f0262cSandi */
43183198f92SSatoshi Saharafunction sort_search_fulltext($a, $b)
43283198f92SSatoshi Sahara{
433f3f0262cSandi    if ($a['count'] > $b['count']) {
434f3f0262cSandi        return -1;
435f3f0262cSandi    } elseif ($a['count'] < $b['count']) {
436f3f0262cSandi        return 1;
437f3f0262cSandi    } else {
4382d85e841SAndreas Gohr        return Sort::strcmp($a['id'], $b['id']);
439f3f0262cSandi    }
440f3f0262cSandi}
441f3f0262cSandi
442f3f0262cSandi/**
443f3f0262cSandi * translates a document path to an ID
44415fae107Sandi *
44515fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
44637e34a5eSandi * @todo    move to pageutils
447f50a239bSTakamura *
448f50a239bSTakamura * @param string $path
449f50a239bSTakamura * @param bool $keeptxt
450f50a239bSTakamura *
4514dc42f7fSGerrit Uitslag * @return string
452f3f0262cSandi */
45383198f92SSatoshi Saharafunction pathID($path, $keeptxt = false)
45483198f92SSatoshi Sahara{
45549c713a3Sandi    $id = utf8_decodeFN($path);
45649c713a3Sandi    $id = str_replace('/', ':', $id);
457156a608cSandi    if (!$keeptxt) $id = preg_replace('#\.txt$#', '', $id);
458709b1063SAdrian Lang    $id = trim($id, ':');
459f3f0262cSandi    return $id;
460f3f0262cSandi}
461f3f0262cSandi
462340756e4Sandi
4633abeade3SAndreas Gohr/**
4643abeade3SAndreas Gohr * This is a very universal callback for the search() function, replacing
4653abeade3SAndreas Gohr * many of the former individual functions at the cost of a more complex
4663abeade3SAndreas Gohr * setup.
4673abeade3SAndreas Gohr *
4683abeade3SAndreas Gohr * How the function behaves, depends on the options passed in the $opts
4693abeade3SAndreas Gohr * array, where the following settings can be used.
4703abeade3SAndreas Gohr *
471e14fe973SGerrit Uitslag * depth      int     recursion depth. 0 for unlimited                       (default: 0)
472e14fe973SGerrit Uitslag * keeptxt    bool    keep .txt extension for IDs                            (default: false)
473e14fe973SGerrit Uitslag * listfiles  bool    include files in listing                               (default: false)
474e14fe973SGerrit Uitslag * listdirs   bool    include namespaces in listing                          (default: false)
475e14fe973SGerrit Uitslag * pagesonly  bool    restrict files to pages                                (default: false)
476e14fe973SGerrit Uitslag * skipacl    bool    do not check for READ permission                       (default: false)
477e14fe973SGerrit Uitslag * sneakyacl  bool    don't recurse into nonreadable dirs                    (default: false)
478e14fe973SGerrit Uitslag * hash       bool    create MD5 hash for files                              (default: false)
479e14fe973SGerrit Uitslag * meta       bool    return file metadata                                   (default: false)
480e14fe973SGerrit Uitslag * filematch  string  match files against this regexp                        (default: '', so accept everything)
481e14fe973SGerrit Uitslag * idmatch    string  match full ID against this regexp                      (default: '', so accept everything)
482e14fe973SGerrit Uitslag * dirmatch   string  match directory against this regexp when adding        (default: '', so accept everything)
483e14fe973SGerrit Uitslag * nsmatch    string  match namespace against this regexp when adding        (default: '', so accept everything)
484e14fe973SGerrit Uitslag * recmatch   string  match directory against this regexp when recursing     (default: '', so accept everything)
485e14fe973SGerrit Uitslag * showmsg    bool    warn about non-ID files                                (default: false)
486e14fe973SGerrit Uitslag * showhidden bool    show hidden files(e.g. by hidepages config) too        (default: false)
487e14fe973SGerrit Uitslag * firsthead  bool    return first heading for pages                         (default: false)
4883abeade3SAndreas Gohr *
489ce4301e3SGerrit Uitslag * @param array &$data  - Reference to the result data structure
490ce4301e3SGerrit Uitslag * @param string $base  - Base usually $conf['datadir']
491ce4301e3SGerrit Uitslag * @param string $file  - current file or directory relative to $base
492ce4301e3SGerrit Uitslag * @param string $type  - Type either 'd' for directory or 'f' for file
493ce4301e3SGerrit Uitslag * @param int    $lvl   - Current recursion depht
494ce4301e3SGerrit Uitslag * @param array  $opts  - option array as given to search()
495ce4301e3SGerrit Uitslag * @return bool if this directory should be traversed (true) or not (false)
496ce4301e3SGerrit Uitslag *              return value is ignored for files
497ce4301e3SGerrit Uitslag *
4983abeade3SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
4993abeade3SAndreas Gohr */
50083198f92SSatoshi Saharafunction search_universal(&$data, $base, $file, $type, $lvl, $opts)
50183198f92SSatoshi Sahara{
50224870174SAndreas Gohr    $item   = [];
5033abeade3SAndreas Gohr    $return = true;
5043abeade3SAndreas Gohr
5053abeade3SAndreas Gohr    // get ID and check if it is a valid one
506b7a3421aSChristopher Smith    $item['id'] = pathID($file, ($type == 'd' || !empty($opts['keeptxt'])));
507c347b097Ssplitbrain    if ($item['id'] !== cleanID($item['id'])) {
50849f299d6SChristopher Smith        if (!empty($opts['showmsg'])) {
5098537abd1SAdrian Lang            msg(hsc($item['id']) . ' is not a valid file name for DokuWiki - skipped', -1);
510b7a3421aSChristopher Smith        }
5113abeade3SAndreas Gohr        return false; // skip non-valid files
5123abeade3SAndreas Gohr    }
5138705cc81SAndreas Gohr    $item['ns']  = getNS($item['id']);
5143abeade3SAndreas Gohr
5153abeade3SAndreas Gohr    if ($type == 'd') {
5163abeade3SAndreas Gohr        // decide if to recursion into this directory is wanted
5170e80bb5eSChristopher Smith        if (empty($opts['depth'])) {
5183abeade3SAndreas Gohr            $return = true; // recurse forever
5193abeade3SAndreas Gohr        } else {
5203abeade3SAndreas Gohr            $depth = substr_count($file, '/');
5213abeade3SAndreas Gohr            if ($depth >= $opts['depth']) {
5223abeade3SAndreas Gohr                $return = false; // depth reached
5233abeade3SAndreas Gohr            } else {
5243abeade3SAndreas Gohr                $return = true;
5253abeade3SAndreas Gohr            }
5263abeade3SAndreas Gohr        }
5279b4337c6SChristopher Smith
5289b4337c6SChristopher Smith        if ($return) {
5299b4337c6SChristopher Smith            $match = empty($opts['recmatch']) || preg_match('/' . $opts['recmatch'] . '/', $file);
5309b4337c6SChristopher Smith            if (!$match) {
5319b4337c6SChristopher Smith                return false; // doesn't match
5329b4337c6SChristopher Smith            }
5333abeade3SAndreas Gohr        }
5343abeade3SAndreas Gohr    }
5353abeade3SAndreas Gohr
5363abeade3SAndreas Gohr    // check ACL
537443e135dSChristopher Smith    if (empty($opts['skipacl'])) {
5383abeade3SAndreas Gohr        if ($type == 'd') {
5393abeade3SAndreas Gohr            $item['perm'] = auth_quickaclcheck($item['id'] . ':*');
5403abeade3SAndreas Gohr        } else {
5413abeade3SAndreas Gohr            $item['perm'] = auth_quickaclcheck($item['id']); //FIXME check namespace for media files
5423abeade3SAndreas Gohr        }
5433abeade3SAndreas Gohr    } else {
5443abeade3SAndreas Gohr        $item['perm'] = AUTH_DELETE;
5453abeade3SAndreas Gohr    }
5463abeade3SAndreas Gohr
5473abeade3SAndreas Gohr    // are we done here maybe?
5483abeade3SAndreas Gohr    if ($type == 'd') {
549443e135dSChristopher Smith        if (empty($opts['listdirs'])) return $return;
55064159a61SAndreas Gohr        //neither list nor recurse forbidden items:
55164159a61SAndreas Gohr        if (empty($opts['skipacl']) && !empty($opts['sneakyacl']) && $item['perm'] < AUTH_READ) return false;
552443e135dSChristopher Smith        if (!empty($opts['dirmatch']) && !preg_match('/' . $opts['dirmatch'] . '/', $file)) return $return;
553443e135dSChristopher Smith        if (!empty($opts['nsmatch']) && !preg_match('/' . $opts['nsmatch'] . '/', $item['ns'])) return $return;
5543abeade3SAndreas Gohr    } else {
555443e135dSChristopher Smith        if (empty($opts['listfiles'])) return $return;
556443e135dSChristopher Smith        if (empty($opts['skipacl']) && $item['perm'] < AUTH_READ) return $return;
5576c16a3a9Sfiwswe        if (!empty($opts['pagesonly']) && !str_ends_with($file, '.txt')) return $return;
558443e135dSChristopher Smith        if (empty($opts['showhidden']) && isHiddenPage($item['id'])) return $return;
559443e135dSChristopher Smith        if (!empty($opts['filematch']) && !preg_match('/' . $opts['filematch'] . '/', $file)) return $return;
560443e135dSChristopher Smith        if (!empty($opts['idmatch']) && !preg_match('/' . $opts['idmatch'] . '/', $item['id'])) return $return;
5613abeade3SAndreas Gohr    }
5623abeade3SAndreas Gohr
5633abeade3SAndreas Gohr    // still here? prepare the item
5643abeade3SAndreas Gohr    $item['type']  = $type;
56532d6093dSAndreas Gohr    $item['level'] = $lvl;
5663abeade3SAndreas Gohr    $item['open']  = $return;
5673abeade3SAndreas Gohr
5680e80bb5eSChristopher Smith    if (!empty($opts['meta'])) {
56924870174SAndreas Gohr        $item['file']       = PhpString::basename($file);
5703abeade3SAndreas Gohr        $item['size']       = filesize($base . '/' . $file);
5713abeade3SAndreas Gohr        $item['mtime']      = filemtime($base . '/' . $file);
5723abeade3SAndreas Gohr        $item['rev']        = $item['mtime'];
5733abeade3SAndreas Gohr        $item['writable']   = is_writable($base . '/' . $file);
5743abeade3SAndreas Gohr        $item['executable'] = is_executable($base . '/' . $file);
5753abeade3SAndreas Gohr    }
5763abeade3SAndreas Gohr
5773abeade3SAndreas Gohr    if ($type == 'f') {
5780e80bb5eSChristopher Smith        if (!empty($opts['hash'])) $item['hash'] = md5(io_readFile($base . '/' . $file, false));
57983198f92SSatoshi Sahara        if (!empty($opts['firsthead'])) {
58083198f92SSatoshi Sahara            $item['title'] = p_get_first_heading($item['id'], METADATA_DONT_RENDER);
58183198f92SSatoshi Sahara        }
5823abeade3SAndreas Gohr    }
5833abeade3SAndreas Gohr
5843abeade3SAndreas Gohr    // finally add the item
5853abeade3SAndreas Gohr    $data[] = $item;
5863abeade3SAndreas Gohr    return $return;
5873abeade3SAndreas Gohr}
5883abeade3SAndreas Gohr
589e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
590