xref: /plugin/struct/helper/config.php (revision 61356325e2c5dbdcb8405fa2eb4c34732d79b65f)
1<?php
2
3/**
4 * DokuWiki Plugin struct (Helper Component)
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
8 */
9
10// must be run within Dokuwiki
11if (!defined('DOKU_INC')) die();
12
13class helper_plugin_struct_config extends DokuWiki_Plugin
14{
15
16    /**
17     * @param string $val
18     *
19     * @return array
20     */
21    public function parseSort($val)
22    {
23        if (substr($val, 0, 1) == '^') {
24            return array(substr($val, 1), false);
25        }
26        return array($val, true);
27    }
28
29    /**
30     * @param $logic
31     * @param $val
32     *
33     * @return array|bool
34     */
35    public function parseFilterLine($logic, $val)
36    {
37        $flt = $this->parseFilter($val);
38        if ($flt) {
39            $flt[] = $logic;
40            return $flt;
41        }
42        return false;
43    }
44
45    /**
46     * Parse a filter
47     *
48     * @param string $val
49     *
50     * @return array ($col, $comp, $value)
51     * @throws dokuwiki\plugin\struct\meta\StructException
52     */
53    protected function parseFilter($val)
54    {
55
56        $comps = dokuwiki\plugin\struct\meta\Search::$COMPARATORS;
57        $comps[] = '*~';
58        array_unshift($comps, '<>');
59        $comps = array_map('preg_quote_cb', $comps);
60        $comps = join('|', $comps);
61
62        if (!preg_match('/^(.*?)(' . $comps . ')(.*)$/', $val, $match)) {
63            throw new dokuwiki\plugin\struct\meta\StructException('Invalid search filter %s', hsc($val));
64        }
65        array_shift($match); // we don't need the zeroth match
66        $match[0] = trim($match[0]);
67        $match[2] = trim($match[2]);
68        return $match;
69    }
70}
71
72// vim:ts=4:sw=4:et:
73