xref: /dokuwiki/inc/Action/Search.php (revision d09b5b6441fa2464fba24f84eff22348b879676c)
164ab5140SAndreas Gohr<?php
264ab5140SAndreas Gohr
364ab5140SAndreas Gohrnamespace dokuwiki\Action;
464ab5140SAndreas Gohr
564ab5140SAndreas Gohruse dokuwiki\Action\Exception\ActionAbort;
664ab5140SAndreas Gohr
7ab583a1bSAndreas Gohr/**
8ab583a1bSAndreas Gohr * Class Search
9ab583a1bSAndreas Gohr *
10ab583a1bSAndreas Gohr * Search for pages and content
11ab583a1bSAndreas Gohr *
12ab583a1bSAndreas Gohr * @package dokuwiki\Action
13ab583a1bSAndreas Gohr */
1464ab5140SAndreas Gohrclass Search extends AbstractAction {
1564ab5140SAndreas Gohr
1664ab5140SAndreas Gohr    /** @inheritdoc */
17ec701221SAndreas Gohr    public function minimumPermission() {
1864ab5140SAndreas Gohr        return AUTH_NONE;
1964ab5140SAndreas Gohr    }
2064ab5140SAndreas Gohr
2164ab5140SAndreas Gohr    /**
2264ab5140SAndreas Gohr     * we only search if a search word was given
2364ab5140SAndreas Gohr     *
2464ab5140SAndreas Gohr     * @inheritdoc
2564ab5140SAndreas Gohr     */
2664ab5140SAndreas Gohr    public function checkPermissions() {
2764ab5140SAndreas Gohr        parent::checkPermissions();
2864ab5140SAndreas Gohr        global $QUERY;
2964ab5140SAndreas Gohr        $s = cleanID($QUERY);
3064ab5140SAndreas Gohr        if($s === '') throw new ActionAbort();
3164ab5140SAndreas Gohr    }
3264ab5140SAndreas Gohr
33*d09b5b64SMichael Große    public function preProcess()
34*d09b5b64SMichael Große    {
35*d09b5b64SMichael Große        $this->adjustGlobalQuery();
36*d09b5b64SMichael Große    }
37*d09b5b64SMichael Große
3864ab5140SAndreas Gohr    /** @inheritdoc */
39*d09b5b64SMichael Große    public function tplContent()
40*d09b5b64SMichael Große    {
41*d09b5b64SMichael Große        $search = new \dokuwiki\Ui\Search();
4221fcef82SMichael Große        $search->execute();
4321fcef82SMichael Große        $search->show();
4464ab5140SAndreas Gohr    }
45*d09b5b64SMichael Große
46*d09b5b64SMichael Große    /**
47*d09b5b64SMichael Große     * Adjust the global query accordingly to the config search_limit_to_first_ns and search_default_fragment_behaviour
48*d09b5b64SMichael Große     *
49*d09b5b64SMichael Große     * This will only do something if the search didn't originate from the form on the searchpage itself
50*d09b5b64SMichael Große     */
51*d09b5b64SMichael Große    protected function adjustGlobalQuery()
52*d09b5b64SMichael Große    {
53*d09b5b64SMichael Große        global $conf, $INPUT, $QUERY;
54*d09b5b64SMichael Große
55*d09b5b64SMichael Große        if ($INPUT->bool('searchPageForm')) {
56*d09b5b64SMichael Große            return;
57*d09b5b64SMichael Große        }
58*d09b5b64SMichael Große
59*d09b5b64SMichael Große        $Indexer = idx_get_indexer();
60*d09b5b64SMichael Große        $parsedQuery = ft_queryParser($Indexer, $QUERY);
61*d09b5b64SMichael Große
62*d09b5b64SMichael Große        if (empty($parsedQuery['ns']) && empty($parsedQuery['notns'])) {
63*d09b5b64SMichael Große            if ($conf['search_limit_to_first_ns'] > 0) {
64*d09b5b64SMichael Große                $searchOriginPage = $INPUT->str('from');
65*d09b5b64SMichael Große                if (getNS($searchOriginPage) !== false) {
66*d09b5b64SMichael Große                    $nsParts = explode(':', getNS($searchOriginPage));
67*d09b5b64SMichael Große                    $ns = implode(':', array_slice($nsParts, 0, $conf['search_limit_to_first_ns']));
68*d09b5b64SMichael Große                    $QUERY .= " @$ns";
69*d09b5b64SMichael Große                }
70*d09b5b64SMichael Große            }
71*d09b5b64SMichael Große        }
72*d09b5b64SMichael Große
73*d09b5b64SMichael Große        if ($conf['search_default_fragment_behaviour'] !== 'exact') {
74*d09b5b64SMichael Große            if (empty(array_diff($parsedQuery['words'], $parsedQuery['and']))) {
75*d09b5b64SMichael Große                if (strpos($QUERY, '*') === false) {
76*d09b5b64SMichael Große                    $queryParts = explode(' ', $QUERY);
77*d09b5b64SMichael Große                    $queryParts = array_map(function ($part) {
78*d09b5b64SMichael Große                        if (strpos($part, '@') === 0) {
79*d09b5b64SMichael Große                            return $part;
80*d09b5b64SMichael Große                        }
81*d09b5b64SMichael Große                        if (strpos($part, 'ns:') === 0) {
82*d09b5b64SMichael Große                            return $part;
83*d09b5b64SMichael Große                        }
84*d09b5b64SMichael Große                        if (strpos($part, '^') === 0) {
85*d09b5b64SMichael Große                            return $part;
86*d09b5b64SMichael Große                        }
87*d09b5b64SMichael Große                        if (strpos($part, '-ns:') === 0) {
88*d09b5b64SMichael Große                            return $part;
89*d09b5b64SMichael Große                        }
90*d09b5b64SMichael Große
91*d09b5b64SMichael Große                        global $conf;
92*d09b5b64SMichael Große
93*d09b5b64SMichael Große                        if ($conf['search_default_fragment_behaviour'] === 'starts_with') {
94*d09b5b64SMichael Große                            return $part . '*';
95*d09b5b64SMichael Große                        }
96*d09b5b64SMichael Große                        if ($conf['search_default_fragment_behaviour'] === 'ends_with') {
97*d09b5b64SMichael Große                            return '*' . $part;
98*d09b5b64SMichael Große                        }
99*d09b5b64SMichael Große
100*d09b5b64SMichael Große                        return '*' . $part . '*';
101*d09b5b64SMichael Große
102*d09b5b64SMichael Große                    }, $queryParts);
103*d09b5b64SMichael Große                    $QUERY = implode(' ', $queryParts);
104*d09b5b64SMichael Große                }
105*d09b5b64SMichael Große            }
106*d09b5b64SMichael Große        }
107*d09b5b64SMichael Große    }
10864ab5140SAndreas Gohr}
109