xref: /plugin/struct/helper/config.php (revision 01a8eccd70603dd29d644f62afedc0fd6a69bdc6)
129877279SMichael Große<?php
229877279SMichael Große/**
329877279SMichael Große * DokuWiki Plugin struct (Helper Component)
429877279SMichael Große *
529877279SMichael Große * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
629877279SMichael Große * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
729877279SMichael Große */
829877279SMichael Große
929877279SMichael Große// must be run within Dokuwiki
1029877279SMichael Großeif(!defined('DOKU_INC')) die();
1129877279SMichael Große
1229877279SMichael Großeclass helper_plugin_struct_config extends DokuWiki_Plugin {
1329877279SMichael Große
1429877279SMichael Große    /**
1529877279SMichael Große     * @param string $val
1629877279SMichael Große     *
1729877279SMichael Große     * @return array
1829877279SMichael Große     */
1929877279SMichael Große    public function parseSort($val) {
2029877279SMichael Große        if(substr($val, 0, 1) == '^') {
2129877279SMichael Große            return array(substr($val, 1), 'DESC',);
2229877279SMichael Große        }
2329877279SMichael Große        return array($val, 'ASC',);
2429877279SMichael Große    }
2529877279SMichael Große
2629877279SMichael Große    /**
2729877279SMichael Große     * @param $logic
2829877279SMichael Große     * @param $val
2929877279SMichael Große     *
3029877279SMichael Große     * @return array|bool
3129877279SMichael Große     */
3229877279SMichael Große    public function parseFilterLine($logic, $val) {
3329877279SMichael Große        $flt = $this->parseFilter($val);
3429877279SMichael Große        if($flt) {
3529877279SMichael Große            $flt[] = $logic;
3629877279SMichael Große            return $flt;
3729877279SMichael Große        }
3829877279SMichael Große        return false;
3929877279SMichael Große    }
4029877279SMichael Große
4129877279SMichael Große    /**
4229877279SMichael Große     * Parse a filter
4329877279SMichael Große     *
4429877279SMichael Große     * @param string $val
4529877279SMichael Große     *
4629877279SMichael Große     * @return array ($col, $comp, $value)
4729877279SMichael Große     * @throws plugin\struct\meta\StructException
4829877279SMichael Große     */
4929877279SMichael Große    protected function parseFilter($val) {
5029877279SMichael Große
5129877279SMichael Große        $comps = plugin\struct\meta\Search::$COMPARATORS;
52*01a8eccdSMichael Große        $comps[] = '*~';
53*01a8eccdSMichael Große        $comps[] = '<>';
5429877279SMichael Große        $comps = array_map('preg_quote_cb', $comps);
5529877279SMichael Große        $comps = join('|', $comps);
5629877279SMichael Große
5729877279SMichael Große        if(!preg_match('/^(.*?)('.$comps.')(.*)$/', $val, $match)) {
5829877279SMichael Große            throw new plugin\struct\meta\StructException('Invalid search filter %s', hsc($val));
5929877279SMichael Große        }
6029877279SMichael Große        array_shift($match); // we don't need the zeroth match
6129877279SMichael Große        $match[0] = trim($match[0]);
6229877279SMichael Große        $match[2] = trim($match[2]);
63*01a8eccdSMichael Große        if ($match[1] == '*~') {
64*01a8eccdSMichael Große            $match[2] = '*' . $match[2] . '*';
65*01a8eccdSMichael Große            $match[1] = '~';
66*01a8eccdSMichael Große        } elseif ($match[1] == '<>') {
67*01a8eccdSMichael Große            $match[1] = '!=';
68*01a8eccdSMichael Große        }
6929877279SMichael Große        return $match;
7029877279SMichael Große    }
7129877279SMichael Große}
7229877279SMichael Große
7329877279SMichael Große// vim:ts=4:sw=4:et:
74