xref: /plugin/struct/helper/config.php (revision ba76620163eb4cb2b8d6042c6bec7725074f508c)
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) == '^') {
21aa124708SAndreas Gohr            return array(substr($val, 1), false);
2229877279SMichael Große        }
23aa124708SAndreas Gohr        return array($val, true);
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)
47*ba766201SAndreas Gohr     * @throws dokuwiki\plugin\struct\meta\StructException
4829877279SMichael Große     */
4929877279SMichael Große    protected function parseFilter($val) {
5029877279SMichael Große
51*ba766201SAndreas Gohr        $comps = dokuwiki\plugin\struct\meta\Search::$COMPARATORS;
5201a8eccdSMichael Große        $comps[] = '*~';
5332c6849eSMichael Große        array_unshift($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)) {
58*ba766201SAndreas Gohr            throw new dokuwiki\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]);
6329877279SMichael Große        return $match;
6429877279SMichael Große    }
6529877279SMichael Große}
6629877279SMichael Große
6729877279SMichael Große// vim:ts=4:sw=4:et:
68