xref: /plugin/struct/helper/config.php (revision d6d97f6064c3b0f90310be8341edc9585520ee54)
129877279SMichael Große<?php
2*d6d97f60SAnna Dabrowska
329877279SMichael Große/**
429877279SMichael Große * DokuWiki Plugin struct (Helper Component)
529877279SMichael Große *
629877279SMichael Große * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
729877279SMichael Große * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
829877279SMichael Große */
929877279SMichael Große
1029877279SMichael Große// must be run within Dokuwiki
1129877279SMichael Großeif (!defined('DOKU_INC')) die();
1229877279SMichael Große
13*d6d97f60SAnna Dabrowskaclass helper_plugin_struct_config extends DokuWiki_Plugin
14*d6d97f60SAnna Dabrowska{
1529877279SMichael Große
1629877279SMichael Große    /**
1729877279SMichael Große     * @param string $val
1829877279SMichael Große     *
1929877279SMichael Große     * @return array
2029877279SMichael Große     */
21*d6d97f60SAnna Dabrowska    public function parseSort($val)
22*d6d97f60SAnna Dabrowska    {
2329877279SMichael Große        if (substr($val, 0, 1) == '^') {
24aa124708SAndreas Gohr            return array(substr($val, 1), false);
2529877279SMichael Große        }
26aa124708SAndreas Gohr        return array($val, true);
2729877279SMichael Große    }
2829877279SMichael Große
2929877279SMichael Große    /**
3029877279SMichael Große     * @param $logic
3129877279SMichael Große     * @param $val
3229877279SMichael Große     *
3329877279SMichael Große     * @return array|bool
3429877279SMichael Große     */
35*d6d97f60SAnna Dabrowska    public function parseFilterLine($logic, $val)
36*d6d97f60SAnna Dabrowska    {
3729877279SMichael Große        $flt = $this->parseFilter($val);
3829877279SMichael Große        if ($flt) {
3929877279SMichael Große            $flt[] = $logic;
4029877279SMichael Große            return $flt;
4129877279SMichael Große        }
4229877279SMichael Große        return false;
4329877279SMichael Große    }
4429877279SMichael Große
4529877279SMichael Große    /**
4629877279SMichael Große     * Parse a filter
4729877279SMichael Große     *
4829877279SMichael Große     * @param string $val
4929877279SMichael Große     *
5029877279SMichael Große     * @return array ($col, $comp, $value)
51ba766201SAndreas Gohr     * @throws dokuwiki\plugin\struct\meta\StructException
5229877279SMichael Große     */
53*d6d97f60SAnna Dabrowska    protected function parseFilter($val)
54*d6d97f60SAnna Dabrowska    {
5529877279SMichael Große
56ba766201SAndreas Gohr        $comps = dokuwiki\plugin\struct\meta\Search::$COMPARATORS;
5701a8eccdSMichael Große        $comps[] = '*~';
5832c6849eSMichael Große        array_unshift($comps, '<>');
5929877279SMichael Große        $comps = array_map('preg_quote_cb', $comps);
6029877279SMichael Große        $comps = join('|', $comps);
6129877279SMichael Große
6229877279SMichael Große        if (!preg_match('/^(.*?)(' . $comps . ')(.*)$/', $val, $match)) {
63ba766201SAndreas Gohr            throw new dokuwiki\plugin\struct\meta\StructException('Invalid search filter %s', hsc($val));
6429877279SMichael Große        }
6529877279SMichael Große        array_shift($match); // we don't need the zeroth match
6629877279SMichael Große        $match[0] = trim($match[0]);
6729877279SMichael Große        $match[2] = trim($match[2]);
6829877279SMichael Große        return $match;
6929877279SMichael Große    }
7029877279SMichael Große}
7129877279SMichael Große
7229877279SMichael Große// vim:ts=4:sw=4:et:
73