xref: /dokuwiki/inc/search.php (revision c347b0979268fdf935e7048330cb4cfea55efb52)
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 */
9d4f83172SAndreas Gohr
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 */
30d868eb89SAndreas Gohrfunction search(&$data, $base, $func, $opts, $dir = '', $lvl = 1, $sort = 'natural')
31d868eb89SAndreas Gohr{
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') {
56d971ea8bSKate Arzamastseva            @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 */
112d868eb89SAndreas Gohrfunction search_qsearch(&$data, $base, $file, $type, $lvl, $opts)
113d868eb89SAndreas Gohr{
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 */
138d868eb89SAndreas Gohrfunction search_index(&$data, $base, $file, $type, $lvl, $opts)
139d868eb89SAndreas Gohr{
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 */
168d868eb89SAndreas Gohrfunction search_namespaces(&$data, $base, $file, $type, $lvl, $opts)
169d868eb89SAndreas Gohr{
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 */
193d868eb89SAndreas Gohrfunction search_media(&$data, $base, $file, $type, $lvl, $opts)
194d868eb89SAndreas Gohr{
195b8219d2dSAndreas Gohr
196f3f0262cSandi    //we do nothing with directories
1971a49ac65SGina Haeussge    if ($type == 'd') {
1980e80bb5eSChristopher Smith        if (empty($opts['depth'])) return true; // recurse forever
19978315408SAndreas Gohr        $depth = substr_count($file, '/');
200b8219d2dSAndreas Gohr        if ($depth >= $opts['depth']) return false; // depth reached
201224122cfSAndreas Gohr        return true;
2021a49ac65SGina Haeussge    }
203f3f0262cSandi
20424870174SAndreas Gohr    $info         = [];
205156a608cSandi    $info['id']   = pathID($file, true);
206*c347b097Ssplitbrain    if ($info['id'] !== cleanID($info['id'])) {
2072b9be456SAndreas Gohr        if (!empty($opts['showmsg']))
20864807c84SAndreas Gohr            msg(hsc($info['id']) . ' is not a valid file name for DokuWiki - skipped', -1);
20964807c84SAndreas Gohr        return false; // skip non-valid files
21064807c84SAndreas Gohr    }
211f3f0262cSandi
212f3f0262cSandi    //check ACL for namespace (we have no ACL for mediafiles)
213224122cfSAndreas Gohr    $info['perm'] = auth_quickaclcheck(getNS($info['id']) . ':*');
2140e80bb5eSChristopher Smith    if (empty($opts['skipacl']) && $info['perm'] < AUTH_READ) {
215224122cfSAndreas Gohr        return false;
216224122cfSAndreas Gohr    }
217224122cfSAndreas Gohr
218224122cfSAndreas Gohr    //check pattern filter
2190e80bb5eSChristopher Smith    if (!empty($opts['pattern']) && !@preg_match($opts['pattern'], $info['id'])) {
220f3f0262cSandi        return false;
221f3f0262cSandi    }
222f3f0262cSandi
22324870174SAndreas Gohr    $info['file']     = PhpString::basename($file);
224f3f0262cSandi    $info['size']     = filesize($base . '/' . $file);
2255e7fa82eSAndreas Gohr    $info['mtime']    = filemtime($base . '/' . $file);
2263df72098SAndreas Gohr    $info['writable'] = is_writable($base . '/' . $file);
227f3f0262cSandi    if (preg_match("/\.(jpe?g|gif|png)$/", $file)) {
228f3f0262cSandi        $info['isimg'] = true;
22923a34783SAndreas Gohr        $info['meta']  = new JpegMeta($base . '/' . $file);
230f3f0262cSandi    } else {
231f3f0262cSandi        $info['isimg'] = false;
232f3f0262cSandi    }
2330e80bb5eSChristopher Smith    if (!empty($opts['hash'])) {
234dfd343c4SAndreas Gohr        $info['hash'] = md5(io_readFile(mediaFN($info['id']), false));
235224122cfSAndreas Gohr    }
236224122cfSAndreas Gohr
237f3f0262cSandi    $data[] = $info;
238f3f0262cSandi
239f3f0262cSandi    return false;
240f3f0262cSandi}
241f3f0262cSandi
242f3f0262cSandi/**
2434f33babfSAndreas Gohr * List all mediafiles in a namespace
2444f33babfSAndreas Gohr *   $opts['depth']     recursion level, 0 for all
2454f33babfSAndreas Gohr *   $opts['showmsg']   shows message if invalid media id is used
2464f33babfSAndreas Gohr *   $opts['skipacl']   skip acl checking
2474f33babfSAndreas Gohr *   $opts['pattern']   check given pattern
2484f33babfSAndreas Gohr *   $opts['hash']      add hashes to result list
2494f33babfSAndreas Gohr *
2504f33babfSAndreas Gohr * @todo This is a temporary copy of search_media returning a list of MediaFile intances
2514f33babfSAndreas Gohr *
2524f33babfSAndreas Gohr * @param array $data
2534f33babfSAndreas Gohr * @param string $base
2544f33babfSAndreas Gohr * @param string $file
2554f33babfSAndreas Gohr * @param string $type
2564f33babfSAndreas Gohr * @param integer $lvl
2574f33babfSAndreas Gohr * @param array $opts
2584f33babfSAndreas Gohr *
2594f33babfSAndreas Gohr * @return bool
2604f33babfSAndreas Gohr */
261d868eb89SAndreas Gohrfunction search_mediafiles(&$data, $base, $file, $type, $lvl, $opts)
262d868eb89SAndreas Gohr{
2634f33babfSAndreas Gohr
2644f33babfSAndreas Gohr    //we do nothing with directories
2654f33babfSAndreas Gohr    if ($type == 'd') {
2664f33babfSAndreas Gohr        if (empty($opts['depth'])) return true; // recurse forever
2674f33babfSAndreas Gohr        $depth = substr_count($file, '/');
2684f33babfSAndreas Gohr        if ($depth >= $opts['depth']) return false; // depth reached
2694f33babfSAndreas Gohr        return true;
2704f33babfSAndreas Gohr    }
2714f33babfSAndreas Gohr
2724f33babfSAndreas Gohr    $id   = pathID($file, true);
2734f33babfSAndreas Gohr    if ($id != cleanID($id)) {
2744f33babfSAndreas Gohr        if ($opts['showmsg'])
2754f33babfSAndreas Gohr            msg(hsc($id) . ' is not a valid file name for DokuWiki - skipped', -1);
2764f33babfSAndreas Gohr        return false; // skip non-valid files
2774f33babfSAndreas Gohr    }
2784f33babfSAndreas Gohr
2794f33babfSAndreas Gohr    //check ACL for namespace (we have no ACL for mediafiles)
2804f33babfSAndreas Gohr    $info['perm'] = auth_quickaclcheck(getNS($id) . ':*');
2814f33babfSAndreas Gohr    if (empty($opts['skipacl']) && $info['perm'] < AUTH_READ) {
2824f33babfSAndreas Gohr        return false;
2834f33babfSAndreas Gohr    }
2844f33babfSAndreas Gohr
2854f33babfSAndreas Gohr    //check pattern filter
2864f33babfSAndreas Gohr    if (!empty($opts['pattern']) && !@preg_match($opts['pattern'], $id)) {
2874f33babfSAndreas Gohr        return false;
2884f33babfSAndreas Gohr    }
2894f33babfSAndreas Gohr
29024870174SAndreas Gohr    $data[] = new MediaFile($id);
2914f33babfSAndreas Gohr    return false;
2924f33babfSAndreas Gohr}
2934f33babfSAndreas Gohr
2944f33babfSAndreas Gohr
2954f33babfSAndreas Gohr/**
296f3f0262cSandi * This function just lists documents (for RSS namespace export)
29715fae107Sandi *
29815fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
299f50a239bSTakamura *
300f50a239bSTakamura * @param array $data
301f50a239bSTakamura * @param string $base
302f50a239bSTakamura * @param string $file
303f50a239bSTakamura * @param string $type
304f50a239bSTakamura * @param integer $lvl
305f50a239bSTakamura * @param array $opts
306f50a239bSTakamura *
307f50a239bSTakamura * @return bool
308f3f0262cSandi */
309d868eb89SAndreas Gohrfunction search_list(&$data, $base, $file, $type, $lvl, $opts)
310d868eb89SAndreas Gohr{
311f3f0262cSandi    //we do nothing with directories
312f3f0262cSandi    if ($type == 'd') return false;
3130e1a261eSMichael Klier    //only search txt files
3140e1a261eSMichael Klier    if (substr($file, -4) == '.txt') {
315f3f0262cSandi        //check ACL
316f3f0262cSandi        $id = pathID($file);
317f3f0262cSandi        if (auth_quickaclcheck($id) < AUTH_READ) {
318f3f0262cSandi            return false;
319f3f0262cSandi        }
3200e1a261eSMichael Klier        $data[]['id'] = $id;
321f3f0262cSandi    }
322f3f0262cSandi    return false;
323f3f0262cSandi}
324f3f0262cSandi
325f3f0262cSandi/**
326f3f0262cSandi * Quicksearch for searching matching pagenames
327f3f0262cSandi *
328f3f0262cSandi * $opts['query'] is the search query
32915fae107Sandi *
33015fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
331f50a239bSTakamura *
332f50a239bSTakamura * @param array $data
333f50a239bSTakamura * @param string $base
334f50a239bSTakamura * @param string $file
335f50a239bSTakamura * @param string $type
336f50a239bSTakamura * @param integer $lvl
337f50a239bSTakamura * @param array $opts
338f50a239bSTakamura *
339f50a239bSTakamura * @return bool
340f3f0262cSandi */
341d868eb89SAndreas Gohrfunction search_pagename(&$data, $base, $file, $type, $lvl, $opts)
342d868eb89SAndreas Gohr{
343f3f0262cSandi    //we do nothing with directories
344f3f0262cSandi    if ($type == 'd') return true;
345f3f0262cSandi    //only search txt files
3460e1a261eSMichael Klier    if (substr($file, -4) != '.txt') return true;
347f3f0262cSandi
348f3f0262cSandi    //simple stringmatching
349396b7edbSmatthiasgrimm    if (!empty($opts['query'])) {
35024870174SAndreas Gohr        if (strpos($file, (string) $opts['query']) !== false) {
351f3f0262cSandi            //check ACL
352f3f0262cSandi            $id = pathID($file);
353f3f0262cSandi            if (auth_quickaclcheck($id) < AUTH_READ) {
354f3f0262cSandi                return false;
355f3f0262cSandi            }
356f3f0262cSandi            $data[]['id'] = $id;
357f3f0262cSandi        }
358396b7edbSmatthiasgrimm    }
359f3f0262cSandi    return true;
360f3f0262cSandi}
361f3f0262cSandi
362f3f0262cSandi/**
36358b6f612SAndreas Gohr * Just lists all documents
36458b6f612SAndreas Gohr *
3651fcfad4dSAndreas Gohr * $opts['depth']   recursion level, 0 for all
3661fcfad4dSAndreas Gohr * $opts['hash']    do md5 sum of content?
367224122cfSAndreas Gohr * $opts['skipacl'] list everything regardless of ACL
3681fcfad4dSAndreas Gohr *
36958b6f612SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
370f50a239bSTakamura *
371f50a239bSTakamura * @param array $data
372f50a239bSTakamura * @param string $base
373f50a239bSTakamura * @param string $file
374f50a239bSTakamura * @param string $type
375f50a239bSTakamura * @param integer $lvl
376f50a239bSTakamura * @param array $opts
377f50a239bSTakamura *
378f50a239bSTakamura * @return bool
37958b6f612SAndreas Gohr */
380d868eb89SAndreas Gohrfunction search_allpages(&$data, $base, $file, $type, $lvl, $opts)
381d868eb89SAndreas Gohr{
38291560755SAndreas Gohr    if (($opts['depth'] ?? 0) > 0) {
383c647387eSGuillaume Turri        $parts = explode('/', ltrim($file, '/'));
3847d34963bSAndreas Gohr        if (
3857d34963bSAndreas Gohr            ($type == 'd' && count($parts) >= $opts['depth'])
3867d34963bSAndreas Gohr            || ($type != 'd' && count($parts) > $opts['depth'])
3877d34963bSAndreas Gohr        ) {
388c647387eSGuillaume Turri            return false; // depth reached
389c647387eSGuillaume Turri        }
390c647387eSGuillaume Turri    }
391c647387eSGuillaume Turri
39258b6f612SAndreas Gohr    //we do nothing with directories
3931fcfad4dSAndreas Gohr    if ($type == 'd') {
3941fcfad4dSAndreas Gohr        return true;
3951fcfad4dSAndreas Gohr    }
3961fcfad4dSAndreas Gohr
39758b6f612SAndreas Gohr    //only search txt files
3980e1a261eSMichael Klier    if (substr($file, -4) != '.txt') return true;
39958b6f612SAndreas Gohr
40024870174SAndreas Gohr    $item = [];
4011fcfad4dSAndreas Gohr    $item['id']   = pathID($file);
40277244e70SMichael Hamann    if (empty($opts['skipacl']) && auth_quickaclcheck($item['id']) < AUTH_READ) {
4031fcfad4dSAndreas Gohr        return false;
4041fcfad4dSAndreas Gohr    }
4051fcfad4dSAndreas Gohr
4061fcfad4dSAndreas Gohr    $item['rev']   = filemtime($base . '/' . $file);
407224122cfSAndreas Gohr    $item['mtime'] = $item['rev'];
4081fcfad4dSAndreas Gohr    $item['size']  = filesize($base . '/' . $file);
4098f34cf3dSMichael Große    if (!empty($opts['hash'])) {
4101fcfad4dSAndreas Gohr        $item['hash'] = md5(trim(rawWiki($item['id'])));
4111fcfad4dSAndreas Gohr    }
4121fcfad4dSAndreas Gohr
4131fcfad4dSAndreas Gohr    $data[] = $item;
41458b6f612SAndreas Gohr    return true;
41558b6f612SAndreas Gohr}
41658b6f612SAndreas Gohr
417b59a406bSmatthiasgrimm/* ------------- helper functions below -------------- */
418b59a406bSmatthiasgrimm
419b59a406bSmatthiasgrimm/**
42015fae107Sandi * fulltext sort
42115fae107Sandi *
422f3f0262cSandi * Callback sort function for use with usort to sort the data
423f3f0262cSandi * structure created by search_fulltext. Sorts descending by count
42415fae107Sandi *
42515fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
426f50a239bSTakamura *
427f50a239bSTakamura * @param array $a
428f50a239bSTakamura * @param array $b
429f50a239bSTakamura *
430f50a239bSTakamura * @return int
431f3f0262cSandi */
432d868eb89SAndreas Gohrfunction sort_search_fulltext($a, $b)
433d868eb89SAndreas Gohr{
434f3f0262cSandi    if ($a['count'] > $b['count']) {
435f3f0262cSandi        return -1;
436f3f0262cSandi    } elseif ($a['count'] < $b['count']) {
437f3f0262cSandi        return 1;
438f3f0262cSandi    } else {
4392d85e841SAndreas Gohr        return Sort::strcmp($a['id'], $b['id']);
440f3f0262cSandi    }
441f3f0262cSandi}
442f3f0262cSandi
443f3f0262cSandi/**
444f3f0262cSandi * translates a document path to an ID
44515fae107Sandi *
44615fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
44737e34a5eSandi * @todo    move to pageutils
448f50a239bSTakamura *
449f50a239bSTakamura * @param string $path
450f50a239bSTakamura * @param bool $keeptxt
451f50a239bSTakamura *
4524dc42f7fSGerrit Uitslag * @return string
453f3f0262cSandi */
454d868eb89SAndreas Gohrfunction pathID($path, $keeptxt = false)
455d868eb89SAndreas Gohr{
45649c713a3Sandi    $id = utf8_decodeFN($path);
45749c713a3Sandi    $id = str_replace('/', ':', $id);
458156a608cSandi    if (!$keeptxt) $id = preg_replace('#\.txt$#', '', $id);
459709b1063SAdrian Lang    $id = trim($id, ':');
460f3f0262cSandi    return $id;
461f3f0262cSandi}
462f3f0262cSandi
463340756e4Sandi
4643abeade3SAndreas Gohr/**
4653abeade3SAndreas Gohr * This is a very universal callback for the search() function, replacing
4663abeade3SAndreas Gohr * many of the former individual functions at the cost of a more complex
4673abeade3SAndreas Gohr * setup.
4683abeade3SAndreas Gohr *
4693abeade3SAndreas Gohr * How the function behaves, depends on the options passed in the $opts
4703abeade3SAndreas Gohr * array, where the following settings can be used.
4713abeade3SAndreas Gohr *
472e14fe973SGerrit Uitslag * depth      int     recursion depth. 0 for unlimited                       (default: 0)
473e14fe973SGerrit Uitslag * keeptxt    bool    keep .txt extension for IDs                            (default: false)
474e14fe973SGerrit Uitslag * listfiles  bool    include files in listing                               (default: false)
475e14fe973SGerrit Uitslag * listdirs   bool    include namespaces in listing                          (default: false)
476e14fe973SGerrit Uitslag * pagesonly  bool    restrict files to pages                                (default: false)
477e14fe973SGerrit Uitslag * skipacl    bool    do not check for READ permission                       (default: false)
478e14fe973SGerrit Uitslag * sneakyacl  bool    don't recurse into nonreadable dirs                    (default: false)
479e14fe973SGerrit Uitslag * hash       bool    create MD5 hash for files                              (default: false)
480e14fe973SGerrit Uitslag * meta       bool    return file metadata                                   (default: false)
481e14fe973SGerrit Uitslag * filematch  string  match files against this regexp                        (default: '', so accept everything)
482e14fe973SGerrit Uitslag * idmatch    string  match full ID against this regexp                      (default: '', so accept everything)
483e14fe973SGerrit Uitslag * dirmatch   string  match directory against this regexp when adding        (default: '', so accept everything)
484e14fe973SGerrit Uitslag * nsmatch    string  match namespace against this regexp when adding        (default: '', so accept everything)
485e14fe973SGerrit Uitslag * recmatch   string  match directory against this regexp when recursing     (default: '', so accept everything)
486e14fe973SGerrit Uitslag * showmsg    bool    warn about non-ID files                                (default: false)
487e14fe973SGerrit Uitslag * showhidden bool    show hidden files(e.g. by hidepages config) too        (default: false)
488e14fe973SGerrit Uitslag * firsthead  bool    return first heading for pages                         (default: false)
4893abeade3SAndreas Gohr *
490ce4301e3SGerrit Uitslag * @param array &$data  - Reference to the result data structure
491ce4301e3SGerrit Uitslag * @param string $base  - Base usually $conf['datadir']
492ce4301e3SGerrit Uitslag * @param string $file  - current file or directory relative to $base
493ce4301e3SGerrit Uitslag * @param string $type  - Type either 'd' for directory or 'f' for file
494ce4301e3SGerrit Uitslag * @param int    $lvl   - Current recursion depht
495ce4301e3SGerrit Uitslag * @param array  $opts  - option array as given to search()
496ce4301e3SGerrit Uitslag * @return bool if this directory should be traversed (true) or not (false)
497ce4301e3SGerrit Uitslag *              return value is ignored for files
498ce4301e3SGerrit Uitslag *
4993abeade3SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
5003abeade3SAndreas Gohr */
501d868eb89SAndreas Gohrfunction search_universal(&$data, $base, $file, $type, $lvl, $opts)
502d868eb89SAndreas Gohr{
50324870174SAndreas Gohr    $item   = [];
5043abeade3SAndreas Gohr    $return = true;
5053abeade3SAndreas Gohr
5063abeade3SAndreas Gohr    // get ID and check if it is a valid one
507b7a3421aSChristopher Smith    $item['id'] = pathID($file, ($type == 'd' || !empty($opts['keeptxt'])));
508*c347b097Ssplitbrain    if ($item['id'] !== cleanID($item['id'])) {
50949f299d6SChristopher Smith        if (!empty($opts['showmsg'])) {
5108537abd1SAdrian Lang            msg(hsc($item['id']) . ' is not a valid file name for DokuWiki - skipped', -1);
511b7a3421aSChristopher Smith        }
5123abeade3SAndreas Gohr        return false; // skip non-valid files
5133abeade3SAndreas Gohr    }
5148705cc81SAndreas Gohr    $item['ns']  = getNS($item['id']);
5153abeade3SAndreas Gohr
5163abeade3SAndreas Gohr    if ($type == 'd') {
5173abeade3SAndreas Gohr        // decide if to recursion into this directory is wanted
5180e80bb5eSChristopher Smith        if (empty($opts['depth'])) {
5193abeade3SAndreas Gohr            $return = true; // recurse forever
5203abeade3SAndreas Gohr        } else {
5213abeade3SAndreas Gohr            $depth = substr_count($file, '/');
5223abeade3SAndreas Gohr            if ($depth >= $opts['depth']) {
5233abeade3SAndreas Gohr                $return = false; // depth reached
5243abeade3SAndreas Gohr            } else {
5253abeade3SAndreas Gohr                $return = true;
5263abeade3SAndreas Gohr            }
5273abeade3SAndreas Gohr        }
5289b4337c6SChristopher Smith
5299b4337c6SChristopher Smith        if ($return) {
5309b4337c6SChristopher Smith            $match = empty($opts['recmatch']) || preg_match('/' . $opts['recmatch'] . '/', $file);
5319b4337c6SChristopher Smith            if (!$match) {
5329b4337c6SChristopher Smith                return false; // doesn't match
5339b4337c6SChristopher Smith            }
5343abeade3SAndreas Gohr        }
5353abeade3SAndreas Gohr    }
5363abeade3SAndreas Gohr
5373abeade3SAndreas Gohr    // check ACL
538443e135dSChristopher Smith    if (empty($opts['skipacl'])) {
5393abeade3SAndreas Gohr        if ($type == 'd') {
5403abeade3SAndreas Gohr            $item['perm'] = auth_quickaclcheck($item['id'] . ':*');
5413abeade3SAndreas Gohr        } else {
5423abeade3SAndreas Gohr            $item['perm'] = auth_quickaclcheck($item['id']); //FIXME check namespace for media files
5433abeade3SAndreas Gohr        }
5443abeade3SAndreas Gohr    } else {
5453abeade3SAndreas Gohr        $item['perm'] = AUTH_DELETE;
5463abeade3SAndreas Gohr    }
5473abeade3SAndreas Gohr
5483abeade3SAndreas Gohr    // are we done here maybe?
5493abeade3SAndreas Gohr    if ($type == 'd') {
550443e135dSChristopher Smith        if (empty($opts['listdirs'])) return $return;
55164159a61SAndreas Gohr        //neither list nor recurse forbidden items:
55264159a61SAndreas Gohr        if (empty($opts['skipacl']) && !empty($opts['sneakyacl']) && $item['perm'] < AUTH_READ) return false;
553443e135dSChristopher Smith        if (!empty($opts['dirmatch']) && !preg_match('/' . $opts['dirmatch'] . '/', $file)) return $return;
554443e135dSChristopher Smith        if (!empty($opts['nsmatch']) && !preg_match('/' . $opts['nsmatch'] . '/', $item['ns'])) return $return;
5553abeade3SAndreas Gohr    } else {
556443e135dSChristopher Smith        if (empty($opts['listfiles'])) return $return;
557443e135dSChristopher Smith        if (empty($opts['skipacl']) && $item['perm'] < AUTH_READ) return $return;
558443e135dSChristopher Smith        if (!empty($opts['pagesonly']) && (substr($file, -4) != '.txt')) return $return;
559443e135dSChristopher Smith        if (empty($opts['showhidden']) && isHiddenPage($item['id'])) return $return;
560443e135dSChristopher Smith        if (!empty($opts['filematch']) && !preg_match('/' . $opts['filematch'] . '/', $file)) return $return;
561443e135dSChristopher Smith        if (!empty($opts['idmatch']) && !preg_match('/' . $opts['idmatch'] . '/', $item['id'])) return $return;
5623abeade3SAndreas Gohr    }
5633abeade3SAndreas Gohr
5643abeade3SAndreas Gohr    // still here? prepare the item
5653abeade3SAndreas Gohr    $item['type']  = $type;
56632d6093dSAndreas Gohr    $item['level'] = $lvl;
5673abeade3SAndreas Gohr    $item['open']  = $return;
5683abeade3SAndreas Gohr
5690e80bb5eSChristopher Smith    if (!empty($opts['meta'])) {
57024870174SAndreas Gohr        $item['file']       = PhpString::basename($file);
5713abeade3SAndreas Gohr        $item['size']       = filesize($base . '/' . $file);
5723abeade3SAndreas Gohr        $item['mtime']      = filemtime($base . '/' . $file);
5733abeade3SAndreas Gohr        $item['rev']        = $item['mtime'];
5743abeade3SAndreas Gohr        $item['writable']   = is_writable($base . '/' . $file);
5753abeade3SAndreas Gohr        $item['executable'] = is_executable($base . '/' . $file);
5763abeade3SAndreas Gohr    }
5773abeade3SAndreas Gohr
5783abeade3SAndreas Gohr    if ($type == 'f') {
5790e80bb5eSChristopher Smith        if (!empty($opts['hash'])) $item['hash'] = md5(io_readFile($base . '/' . $file, false));
5800e80bb5eSChristopher Smith        if (!empty($opts['firsthead'])) $item['title'] = p_get_first_heading($item['id'], METADATA_DONT_RENDER);
5813abeade3SAndreas Gohr    }
5823abeade3SAndreas Gohr
5833abeade3SAndreas Gohr    // finally add the item
5843abeade3SAndreas Gohr    $data[] = $item;
5853abeade3SAndreas Gohr    return $return;
5863abeade3SAndreas Gohr}
5873abeade3SAndreas Gohr
588e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
589