xref: /dokuwiki/inc/Ui/Search.php (revision 8788dbbd585b42284320d64cc932f3c875eab6b2)
121fcef82SMichael Große<?php
221fcef82SMichael Große
321fcef82SMichael Großenamespace dokuwiki\Ui;
421fcef82SMichael Große
5cbb44eabSAndreas Gohruse dokuwiki\Extension\Event;
664159a61SAndreas Gohruse dokuwiki\Form\Form;
70cba610bSSatoshi Saharause dokuwiki\Search\FulltextSearch;
80b1bbbbbSAndreas Gohruse dokuwiki\Search\Query\QueryParser;
979a2d784SGerrit Uitslaguse dokuwiki\Utf8\PhpString;
102d85e841SAndreas Gohruse dokuwiki\Utf8\Sort;
110cba610bSSatoshi Sahara
1221fcef82SMichael Großeclass Search extends Ui
1321fcef82SMichael Große{
1421fcef82SMichael Große    protected $query;
154c924eb8SMichael Große    protected $parsedQuery;
1618856c5dSMichael Große    protected $searchState;
17e2d055f5SAndreas Gohr    protected $pageLookupResults = [];
18e2d055f5SAndreas Gohr    protected $fullTextResults = [];
19e2d055f5SAndreas Gohr    protected $highlight = [];
2021fcef82SMichael Große
2121fcef82SMichael Große    /**
2221fcef82SMichael Große     * Search constructor.
236639a152SMichael Große     *
24fc46ed58SMichael Große     * @param array $pageLookupResults pagename lookup results in the form [pagename => pagetitle]
25fc46ed58SMichael Große     * @param array $fullTextResults fulltext search results in the form [pagename => #hits]
26fc46ed58SMichael Große     * @param array $highlight array of strings to be highlighted
2721fcef82SMichael Große     */
286639a152SMichael Große    public function __construct(array $pageLookupResults, array $fullTextResults, $highlight)
2921fcef82SMichael Große    {
30d09b5b64SMichael Große        global $QUERY;
31d09b5b64SMichael Große
32d09b5b64SMichael Große        $this->query = $QUERY;
33*8788dbbdSsplitbrain        $this->parsedQuery = (new QueryParser())->convert($QUERY ?? '');
3418856c5dSMichael Große        $this->searchState = new SearchState($this->parsedQuery);
3521fcef82SMichael Große
366639a152SMichael Große        $this->pageLookupResults = $pageLookupResults;
376639a152SMichael Große        $this->fullTextResults = $fullTextResults;
3821fcef82SMichael Große        $this->highlight = $highlight;
39b3cfe85aSMichael Große    }
40bbc1da2eSMichael Große
41b3cfe85aSMichael Große    /**
4221fcef82SMichael Große     * display the search result
4321fcef82SMichael Große     *
4421fcef82SMichael Große     * @return void
4521fcef82SMichael Große     */
4621fcef82SMichael Große    public function show()
4721fcef82SMichael Große    {
4879a2d784SGerrit Uitslag        $searchHTML = $this->getSearchIntroHTML($this->query);
4921fcef82SMichael Große
502ce8affcSMichael Große        $searchHTML .= $this->getSearchFormHTML($this->query);
512ce8affcSMichael Große
5221fcef82SMichael Große        $searchHTML .= $this->getPageLookupHTML($this->pageLookupResults);
5321fcef82SMichael Große
5421fcef82SMichael Große        $searchHTML .= $this->getFulltextResultsHTML($this->fullTextResults, $this->highlight);
5521fcef82SMichael Große
5621fcef82SMichael Große        echo $searchHTML;
5721fcef82SMichael Große    }
5821fcef82SMichael Große
5921fcef82SMichael Große    /**
60427ed988SMichael Große     * Get a form which can be used to adjust/refine the search
61427ed988SMichael Große     *
62427ed988SMichael Große     * @param string $query
63427ed988SMichael Große     *
64427ed988SMichael Große     * @return string
65427ed988SMichael Große     */
66427ed988SMichael Große    protected function getSearchFormHTML($query)
67427ed988SMichael Große    {
68bbc1da2eSMichael Große        global $lang, $ID, $INPUT;
69427ed988SMichael Große
707fa270bcSMichael Große        $searchForm = (new Form(['method' => 'get'], true))->addClass('search-results-form');
71bb8ef867SMichael Große        $searchForm->setHiddenField('do', 'search');
72d22b78c8SMichael Große        $searchForm->setHiddenField('id', $ID);
731265b193SMichael Große        $searchForm->setHiddenField('sf', '1');
74422bbbc6SMichael Große        if ($INPUT->has('min')) {
75422bbbc6SMichael Große            $searchForm->setHiddenField('min', $INPUT->str('min'));
76bbc1da2eSMichael Große        }
77422bbbc6SMichael Große        if ($INPUT->has('max')) {
78422bbbc6SMichael Große            $searchForm->setHiddenField('max', $INPUT->str('max'));
79bbc1da2eSMichael Große        }
801265b193SMichael Große        if ($INPUT->has('srt')) {
811265b193SMichael Große            $searchForm->setHiddenField('srt', $INPUT->str('srt'));
828d0e286aSMichael Große        }
834bdf82b5SAndreas Gohr        $searchForm->addFieldsetOpen()->addClass('search-form');
84d22b78c8SMichael Große        $searchForm->addTextInput('q')->val($query)->useInput(false);
85427ed988SMichael Große        $searchForm->addButton('', $lang['btn_search'])->attr('type', 'submit');
86bb8ef867SMichael Große
8718856c5dSMichael Große        $this->addSearchAssistanceElements($searchForm);
88bb8ef867SMichael Große
89427ed988SMichael Große        $searchForm->addFieldsetClose();
90427ed988SMichael Große
91c6977b3aSSatoshi Sahara        return $searchForm->toHTML('Search');
92427ed988SMichael Große    }
93427ed988SMichael Große
94be76738bSMichael Große    /**
95be76738bSMichael Große     * Add elements to adjust how the results are sorted
96be76738bSMichael Große     *
97be76738bSMichael Große     * @param Form $searchForm
98be76738bSMichael Große     */
99b005809cSMichael Große    protected function addSortTool(Form $searchForm)
100b005809cSMichael Große    {
101b005809cSMichael Große        global $INPUT, $lang;
102b005809cSMichael Große
103b005809cSMichael Große        $options = [
104b005809cSMichael Große            'hits' => [
105b005809cSMichael Große                'label' => $lang['search_sort_by_hits'],
106b005809cSMichael Große                'sort' => '',
107b005809cSMichael Große            ],
108b005809cSMichael Große            'mtime' => [
109b005809cSMichael Große                'label' => $lang['search_sort_by_mtime'],
110b005809cSMichael Große                'sort' => 'mtime',
111b005809cSMichael Große            ],
112b005809cSMichael Große        ];
113b005809cSMichael Große        $activeOption = 'hits';
114b005809cSMichael Große
1151265b193SMichael Große        if ($INPUT->str('srt') === 'mtime') {
116b005809cSMichael Große            $activeOption = 'mtime';
117b005809cSMichael Große        }
118b005809cSMichael Große
1192171f9cbSAndreas Gohr        $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true');
120b005809cSMichael Große        // render current
1214bdf82b5SAndreas Gohr        $currentWrapper = $searchForm->addTagOpen('div')->addClass('current');
122b005809cSMichael Große        if ($activeOption !== 'hits') {
1234bdf82b5SAndreas Gohr            $currentWrapper->addClass('changed');
124b005809cSMichael Große        }
125b005809cSMichael Große        $searchForm->addHTML($options[$activeOption]['label']);
126b005809cSMichael Große        $searchForm->addTagClose('div');
127b005809cSMichael Große
128b005809cSMichael Große        // render options list
1292171f9cbSAndreas Gohr        $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false');
130b005809cSMichael Große
131b005809cSMichael Große        foreach ($options as $key => $option) {
1324bdf82b5SAndreas Gohr            $listItem = $searchForm->addTagOpen('li');
133b005809cSMichael Große
134b005809cSMichael Große            if ($key === $activeOption) {
1354bdf82b5SAndreas Gohr                $listItem->addClass('active');
136b005809cSMichael Große                $searchForm->addHTML($option['label']);
137b005809cSMichael Große            } else {
13852d4cd42SMichael Große                $link = $this->searchState->withSorting($option['sort'])->getSearchLink($option['label']);
13952d4cd42SMichael Große                $searchForm->addHTML($link);
140b005809cSMichael Große            }
141b005809cSMichael Große            $searchForm->addTagClose('li');
142b005809cSMichael Große        }
143b005809cSMichael Große        $searchForm->addTagClose('ul');
144b005809cSMichael Große
145b005809cSMichael Große        $searchForm->addTagClose('div');
146b005809cSMichael Große    }
147b005809cSMichael Große
148be76738bSMichael Große    /**
149be76738bSMichael Große     * Check if the query is simple enough to modify its namespace limitations without breaking the rest of the query
150be76738bSMichael Große     *
151be76738bSMichael Große     * @param array $parsedQuery
152be76738bSMichael Große     *
153be76738bSMichael Große     * @return bool
154be76738bSMichael Große     */
155e2d055f5SAndreas Gohr    protected function isNamespaceAssistanceAvailable(array $parsedQuery)
156e2d055f5SAndreas Gohr    {
157df977249SMichael Große        if (preg_match('/[\(\)\|]/', $parsedQuery['query']) === 1) {
158bb8ef867SMichael Große            return false;
159bb8ef867SMichael Große        }
160df977249SMichael Große
161df977249SMichael Große        return true;
162df977249SMichael Große    }
163df977249SMichael Große
164be76738bSMichael Große    /**
165be76738bSMichael Große     * Check if the query is simple enough to modify the fragment search behavior without breaking the rest of the query
166be76738bSMichael Große     *
167be76738bSMichael Große     * @param array $parsedQuery
168be76738bSMichael Große     *
169be76738bSMichael Große     * @return bool
170be76738bSMichael Große     */
171e2d055f5SAndreas Gohr    protected function isFragmentAssistanceAvailable(array $parsedQuery)
172e2d055f5SAndreas Gohr    {
173df977249SMichael Große        if (preg_match('/[\(\)\|]/', $parsedQuery['query']) === 1) {
174bb8ef867SMichael Große            return false;
175bb8ef867SMichael Große        }
176bb8ef867SMichael Große
177bb8ef867SMichael Große        if (!empty($parsedQuery['phrases'])) {
178bb8ef867SMichael Große            return false;
179bb8ef867SMichael Große        }
180bb8ef867SMichael Große
181bb8ef867SMichael Große        return true;
182bb8ef867SMichael Große    }
183bb8ef867SMichael Große
184bb8ef867SMichael Große    /**
185bb8ef867SMichael Große     * Add the elements to be used for search assistance
186bb8ef867SMichael Große     *
187bb8ef867SMichael Große     * @param Form $searchForm
188bb8ef867SMichael Große     */
18918856c5dSMichael Große    protected function addSearchAssistanceElements(Form $searchForm)
190bb8ef867SMichael Große    {
191bb8ef867SMichael Große        $searchForm->addTagOpen('div')
1924bdf82b5SAndreas Gohr            ->addClass('advancedOptions')
1932171f9cbSAndreas Gohr            ->attr('style', 'display: none;')
1942171f9cbSAndreas Gohr            ->attr('aria-hidden', 'true');
195bb8ef867SMichael Große
19618856c5dSMichael Große        $this->addFragmentBehaviorLinks($searchForm);
19718856c5dSMichael Große        $this->addNamespaceSelector($searchForm);
19818856c5dSMichael Große        $this->addDateSelector($searchForm);
199b005809cSMichael Große        $this->addSortTool($searchForm);
200bb8ef867SMichael Große
201bb8ef867SMichael Große        $searchForm->addTagClose('div');
202bb8ef867SMichael Große    }
203bb8ef867SMichael Große
204be76738bSMichael Große    /**
205be76738bSMichael Große     *  Add the elements to adjust the fragment search behavior
206be76738bSMichael Große     *
207be76738bSMichael Große     * @param Form $searchForm
208be76738bSMichael Große     */
20918856c5dSMichael Große    protected function addFragmentBehaviorLinks(Form $searchForm)
2104d0cb6e1SMichael Große    {
211df977249SMichael Große        if (!$this->isFragmentAssistanceAvailable($this->parsedQuery)) {
212df977249SMichael Große            return;
213df977249SMichael Große        }
214b005809cSMichael Große        global $lang;
2154d0cb6e1SMichael Große
216b005809cSMichael Große        $options = [
217b005809cSMichael Große            'exact' => [
218b005809cSMichael Große                'label' => $lang['search_exact_match'],
219e2d055f5SAndreas Gohr                'and' => array_map(static fn($term) => trim($term, '*'), $this->parsedQuery['and']),
220e2d055f5SAndreas Gohr                'not' => array_map(static fn($term) => trim($term, '*'), $this->parsedQuery['not']),
221b005809cSMichael Große            ],
222b005809cSMichael Große            'starts' => [
223b005809cSMichael Große                'label' => $lang['search_starts_with'],
224e2d055f5SAndreas Gohr                'and' => array_map(static fn($term) => trim($term, '*') . '*', $this->parsedQuery['and']),
225e2d055f5SAndreas Gohr                'not' => array_map(static fn($term) => trim($term, '*') . '*', $this->parsedQuery['not']),
226b005809cSMichael Große            ],
227b005809cSMichael Große            'ends' => [
228b005809cSMichael Große                'label' => $lang['search_ends_with'],
229e2d055f5SAndreas Gohr                'and' => array_map(static fn($term) => '*' . trim($term, '*'), $this->parsedQuery['and']),
230e2d055f5SAndreas Gohr                'not' => array_map(static fn($term) => '*' . trim($term, '*'), $this->parsedQuery['not']),
231b005809cSMichael Große            ],
232b005809cSMichael Große            'contains' => [
233b005809cSMichael Große                'label' => $lang['search_contains'],
234e2d055f5SAndreas Gohr                'and' => array_map(static fn($term) => '*' . trim($term, '*') . '*', $this->parsedQuery['and']),
235e2d055f5SAndreas Gohr                'not' => array_map(static fn($term) => '*' . trim($term, '*') . '*', $this->parsedQuery['not']),
236b005809cSMichael Große            ]
237b005809cSMichael Große        ];
238b005809cSMichael Große
239b005809cSMichael Große        // detect current
240c6b5b74aSMichael Große        $activeOption = 'custom';
241b005809cSMichael Große        foreach ($options as $key => $option) {
242b005809cSMichael Große            if ($this->parsedQuery['and'] === $option['and']) {
243b005809cSMichael Große                $activeOption = $key;
244b005809cSMichael Große            }
245b005809cSMichael Große        }
246c6b5b74aSMichael Große        if ($activeOption === 'custom') {
247c6b5b74aSMichael Große            $options = array_merge(['custom' => [
248c6b5b74aSMichael Große                'label' => $lang['search_custom_match'],
249c6b5b74aSMichael Große            ]], $options);
250c6b5b74aSMichael Große        }
251b005809cSMichael Große
2522171f9cbSAndreas Gohr        $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true');
253b005809cSMichael Große        // render current
2544bdf82b5SAndreas Gohr        $currentWrapper = $searchForm->addTagOpen('div')->addClass('current');
255b005809cSMichael Große        if ($activeOption !== 'exact') {
2564bdf82b5SAndreas Gohr            $currentWrapper->addClass('changed');
257b005809cSMichael Große        }
258b005809cSMichael Große        $searchForm->addHTML($options[$activeOption]['label']);
259b005809cSMichael Große        $searchForm->addTagClose('div');
260b005809cSMichael Große
261b005809cSMichael Große        // render options list
2622171f9cbSAndreas Gohr        $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false');
263b005809cSMichael Große
264b005809cSMichael Große        foreach ($options as $key => $option) {
2654bdf82b5SAndreas Gohr            $listItem = $searchForm->addTagOpen('li');
266b005809cSMichael Große
267b005809cSMichael Große            if ($key === $activeOption) {
2684bdf82b5SAndreas Gohr                $listItem->addClass('active');
269b005809cSMichael Große                $searchForm->addHTML($option['label']);
270b005809cSMichael Große            } else {
27152d4cd42SMichael Große                $link = $this->searchState
27252d4cd42SMichael Große                    ->withFragments($option['and'], $option['not'])
273e2d055f5SAndreas Gohr                    ->getSearchLink($option['label']);
27452d4cd42SMichael Große                $searchForm->addHTML($link);
275b005809cSMichael Große            }
276b005809cSMichael Große            $searchForm->addTagClose('li');
277b005809cSMichael Große        }
278b005809cSMichael Große        $searchForm->addTagClose('ul');
2794d0cb6e1SMichael Große
2804d0cb6e1SMichael Große        $searchForm->addTagClose('div');
281b005809cSMichael Große
282b005809cSMichael Große        // render options list
2834d0cb6e1SMichael Große    }
2844d0cb6e1SMichael Große
285bb8ef867SMichael Große    /**
286bb8ef867SMichael Große     * Add the elements for the namespace selector
287bb8ef867SMichael Große     *
288bb8ef867SMichael Große     * @param Form $searchForm
289bb8ef867SMichael Große     */
29018856c5dSMichael Große    protected function addNamespaceSelector(Form $searchForm)
291bb8ef867SMichael Große    {
292df977249SMichael Große        if (!$this->isNamespaceAssistanceAvailable($this->parsedQuery)) {
293df977249SMichael Große            return;
294df977249SMichael Große        }
295df977249SMichael Große
296b005809cSMichael Große        global $lang;
297b005809cSMichael Große
29818856c5dSMichael Große        $baseNS = empty($this->parsedQuery['ns']) ? '' : $this->parsedQuery['ns'][0];
299bbc1da2eSMichael Große        $extraNS = $this->getAdditionalNamespacesFromResults($baseNS);
3004d0cb6e1SMichael Große
3012171f9cbSAndreas Gohr        $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true');
302b005809cSMichael Große        // render current
3034bdf82b5SAndreas Gohr        $currentWrapper = $searchForm->addTagOpen('div')->addClass('current');
304bbc1da2eSMichael Große        if ($baseNS) {
3054bdf82b5SAndreas Gohr            $currentWrapper->addClass('changed');
30603fdedf7SAndreas Gohr            $searchForm->addHTML('@' . hsc($baseNS));
307b005809cSMichael Große        } else {
308b005809cSMichael Große            $searchForm->addHTML($lang['search_any_ns']);
309b005809cSMichael Große        }
310b005809cSMichael Große        $searchForm->addTagClose('div');
311b005809cSMichael Große
312b005809cSMichael Große        // render options list
3132171f9cbSAndreas Gohr        $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false');
314b005809cSMichael Große
3154bdf82b5SAndreas Gohr        $listItem = $searchForm->addTagOpen('li');
316b005809cSMichael Große        if ($baseNS) {
3174bdf82b5SAndreas Gohr            $listItem->addClass('active');
31852d4cd42SMichael Große            $link = $this->searchState->withNamespace('')->getSearchLink($lang['search_any_ns']);
31952d4cd42SMichael Große            $searchForm->addHTML($link);
320b005809cSMichael Große        } else {
321b005809cSMichael Große            $searchForm->addHTML($lang['search_any_ns']);
322bb8ef867SMichael Große        }
323b005809cSMichael Große        $searchForm->addTagClose('li');
324bb8ef867SMichael Große
32518856c5dSMichael Große        foreach ($extraNS as $ns => $count) {
3264bdf82b5SAndreas Gohr            $listItem = $searchForm->addTagOpen('li');
32703fdedf7SAndreas Gohr            $label = hsc($ns) . ($count ? " <bdi>($count)</bdi>" : '');
3284d0cb6e1SMichael Große
329b005809cSMichael Große            if ($ns === $baseNS) {
3304bdf82b5SAndreas Gohr                $listItem->addClass('active');
331b005809cSMichael Große                $searchForm->addHTML($label);
332b005809cSMichael Große            } else {
33352d4cd42SMichael Große                $link = $this->searchState->withNamespace($ns)->getSearchLink($label);
33452d4cd42SMichael Große                $searchForm->addHTML($link);
335bb8ef867SMichael Große            }
336b005809cSMichael Große            $searchForm->addTagClose('li');
337bb8ef867SMichael Große        }
338b005809cSMichael Große        $searchForm->addTagClose('ul');
339bb8ef867SMichael Große
340bb8ef867SMichael Große        $searchForm->addTagClose('div');
341bb8ef867SMichael Große    }
342bb8ef867SMichael Große
343bb8ef867SMichael Große    /**
344bb8ef867SMichael Große     * Parse the full text results for their top namespaces below the given base namespace
345bb8ef867SMichael Große     *
346bb8ef867SMichael Große     * @param string $baseNS the namespace within which was searched, empty string for root namespace
347bb8ef867SMichael Große     *
348bb8ef867SMichael Große     * @return array an associative array with namespace => #number of found pages, sorted descending
349bb8ef867SMichael Große     */
350bb8ef867SMichael Große    protected function getAdditionalNamespacesFromResults($baseNS)
351bb8ef867SMichael Große    {
352bb8ef867SMichael Große        $namespaces = [];
353bb8ef867SMichael Große        $baseNSLength = strlen($baseNS);
354bb8ef867SMichael Große        foreach ($this->fullTextResults as $page => $numberOfHits) {
355bb8ef867SMichael Große            $namespace = getNS($page);
356bb8ef867SMichael Große            if (!$namespace) {
357bb8ef867SMichael Große                continue;
358bb8ef867SMichael Große            }
359bb8ef867SMichael Große            if ($namespace === $baseNS) {
360bb8ef867SMichael Große                continue;
361bb8ef867SMichael Große            }
362bb8ef867SMichael Große            $firstColon = strpos((string)$namespace, ':', $baseNSLength + 1) ?: strlen($namespace);
363bb8ef867SMichael Große            $subtopNS = substr($namespace, 0, $firstColon);
364bb8ef867SMichael Große            if (empty($namespaces[$subtopNS])) {
365bb8ef867SMichael Große                $namespaces[$subtopNS] = 0;
366bb8ef867SMichael Große            }
367e2d055f5SAndreas Gohr            ++$namespaces[$subtopNS];
368bb8ef867SMichael Große        }
3692d85e841SAndreas Gohr        Sort::ksort($namespaces);
370bb8ef867SMichael Große        arsort($namespaces);
371bb8ef867SMichael Große        return $namespaces;
372bb8ef867SMichael Große    }
373bb8ef867SMichael Große
374bb8ef867SMichael Große    /**
375bbc1da2eSMichael Große     * @ToDo: custom date input
376bbc1da2eSMichael Große     *
377bbc1da2eSMichael Große     * @param Form $searchForm
378bbc1da2eSMichael Große     */
379b005809cSMichael Große    protected function addDateSelector(Form $searchForm)
380b005809cSMichael Große    {
381b005809cSMichael Große        global $INPUT, $lang;
382bbc1da2eSMichael Große
383b005809cSMichael Große        $options = [
384b005809cSMichael Große            'any' => [
385b005809cSMichael Große                'before' => false,
386b005809cSMichael Große                'after' => false,
387b005809cSMichael Große                'label' => $lang['search_any_time'],
388b005809cSMichael Große            ],
389b005809cSMichael Große            'week' => [
390b005809cSMichael Große                'before' => false,
391b005809cSMichael Große                'after' => '1 week ago',
392b005809cSMichael Große                'label' => $lang['search_past_7_days'],
393b005809cSMichael Große            ],
394b005809cSMichael Große            'month' => [
395b005809cSMichael Große                'before' => false,
396b005809cSMichael Große                'after' => '1 month ago',
397b005809cSMichael Große                'label' => $lang['search_past_month'],
398b005809cSMichael Große            ],
399b005809cSMichael Große            'year' => [
400b005809cSMichael Große                'before' => false,
401b005809cSMichael Große                'after' => '1 year ago',
402b005809cSMichael Große                'label' => $lang['search_past_year'],
403b005809cSMichael Große            ],
404b005809cSMichael Große        ];
405b005809cSMichael Große        $activeOption = 'any';
406b005809cSMichael Große        foreach ($options as $key => $option) {
407422bbbc6SMichael Große            if ($INPUT->str('min') === $option['after']) {
408b005809cSMichael Große                $activeOption = $key;
409b005809cSMichael Große                break;
410b005809cSMichael Große            }
411b005809cSMichael Große        }
412b005809cSMichael Große
4132171f9cbSAndreas Gohr        $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true');
414b005809cSMichael Große        // render current
4154bdf82b5SAndreas Gohr        $currentWrapper = $searchForm->addTagOpen('div')->addClass('current');
416422bbbc6SMichael Große        if ($INPUT->has('max') || $INPUT->has('min')) {
4174bdf82b5SAndreas Gohr            $currentWrapper->addClass('changed');
418bbc1da2eSMichael Große        }
419b005809cSMichael Große        $searchForm->addHTML($options[$activeOption]['label']);
420b005809cSMichael Große        $searchForm->addTagClose('div');
421bbc1da2eSMichael Große
422b005809cSMichael Große        // render options list
4232171f9cbSAndreas Gohr        $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false');
424b005809cSMichael Große
425b005809cSMichael Große        foreach ($options as $key => $option) {
4264bdf82b5SAndreas Gohr            $listItem = $searchForm->addTagOpen('li');
427b005809cSMichael Große
428b005809cSMichael Große            if ($key === $activeOption) {
4294bdf82b5SAndreas Gohr                $listItem->addClass('active');
430b005809cSMichael Große                $searchForm->addHTML($option['label']);
431bbc1da2eSMichael Große            } else {
43252d4cd42SMichael Große                $link = $this->searchState
43352d4cd42SMichael Große                    ->withTimeLimitations($option['after'], $option['before'])
434e2d055f5SAndreas Gohr                    ->getSearchLink($option['label']);
43552d4cd42SMichael Große                $searchForm->addHTML($link);
436bbc1da2eSMichael Große            }
437b005809cSMichael Große            $searchForm->addTagClose('li');
438bbc1da2eSMichael Große        }
439b005809cSMichael Große        $searchForm->addTagClose('ul');
440bbc1da2eSMichael Große
441bbc1da2eSMichael Große        $searchForm->addTagClose('div');
442bbc1da2eSMichael Große    }
443bbc1da2eSMichael Große
444bbc1da2eSMichael Große
445bbc1da2eSMichael Große    /**
44621fcef82SMichael Große     * Build the intro text for the search page
44721fcef82SMichael Große     *
44821fcef82SMichael Große     * @param string $query the search query
44921fcef82SMichael Große     *
45021fcef82SMichael Große     * @return string
45121fcef82SMichael Große     */
45221fcef82SMichael Große    protected function getSearchIntroHTML($query)
45321fcef82SMichael Große    {
4542ce8affcSMichael Große        global $lang;
45521fcef82SMichael Große
45621fcef82SMichael Große        $intro = p_locale_xhtml('searchpage');
4572ce8affcSMichael Große
4582ce8affcSMichael Große        $queryPagename = $this->createPagenameFromQuery($this->parsedQuery);
4592ce8affcSMichael Große        $createQueryPageLink = html_wikilink($queryPagename . '?do=edit', $queryPagename);
4602ce8affcSMichael Große
4612ce8affcSMichael Große        $pagecreateinfo = '';
4622ce8affcSMichael Große        if (auth_quickaclcheck($queryPagename) >= AUTH_CREATE) {
4632ce8affcSMichael Große            $pagecreateinfo = sprintf($lang['searchcreatepage'], $createQueryPageLink);
4642ce8affcSMichael Große        }
46579a2d784SGerrit Uitslag        return str_replace(
466e2d055f5SAndreas Gohr            ['@QUERY@', '@SEARCH@', '@CREATEPAGEINFO@'],
467e2d055f5SAndreas Gohr            [hsc(rawurlencode($query)), hsc($query), $pagecreateinfo],
46821fcef82SMichael Große            $intro
46921fcef82SMichael Große        );
47021fcef82SMichael Große    }
47121fcef82SMichael Große
47221fcef82SMichael Große    /**
4732ce8affcSMichael Große     * Create a pagename based the parsed search query
4742ce8affcSMichael Große     *
4752ce8affcSMichael Große     * @param array $parsedQuery
4762ce8affcSMichael Große     *
4772ce8affcSMichael Große     * @return string pagename constructed from the parsed query
4782ce8affcSMichael Große     */
47942690e4dSMichael Große    public function createPagenameFromQuery($parsedQuery)
4802ce8affcSMichael Große    {
481e180e453SPhy        $cleanedQuery = cleanID($parsedQuery['query']); // already strtolowered
48279a2d784SGerrit Uitslag        if ($cleanedQuery === PhpString::strtolower($parsedQuery['query'])) {
48342690e4dSMichael Große            return ':' . $cleanedQuery;
48442690e4dSMichael Große        }
4852ce8affcSMichael Große        $pagename = '';
4862ce8affcSMichael Große        if (!empty($parsedQuery['ns'])) {
48742690e4dSMichael Große            $pagename .= ':' . cleanID($parsedQuery['ns'][0]);
4882ce8affcSMichael Große        }
4892ce8affcSMichael Große        $pagename .= ':' . cleanID(implode(' ', $parsedQuery['highlight']));
4902ce8affcSMichael Große        return $pagename;
4912ce8affcSMichael Große    }
4922ce8affcSMichael Große
4932ce8affcSMichael Große    /**
49421fcef82SMichael Große     * Build HTML for a list of pages with matching pagenames
49521fcef82SMichael Große     *
49621fcef82SMichael Große     * @param array $data search results
49721fcef82SMichael Große     *
49821fcef82SMichael Große     * @return string
49921fcef82SMichael Große     */
50021fcef82SMichael Große    protected function getPageLookupHTML($data)
50121fcef82SMichael Große    {
50221fcef82SMichael Große        if (empty($data)) {
50321fcef82SMichael Große            return '';
50421fcef82SMichael Große        }
50521fcef82SMichael Große
50621fcef82SMichael Große        global $lang;
50721fcef82SMichael Große
50821fcef82SMichael Große        $html = '<div class="search_quickresult">';
5096d55fda7SMichael Große        $html .= '<h2>' . $lang['quickhits'] . ':</h2>';
51021fcef82SMichael Große        $html .= '<ul class="search_quickhits">';
511e2d055f5SAndreas Gohr        foreach (array_keys($data) as $id) {
5125d87aa31SMichael Große            $name = null;
5135d87aa31SMichael Große            if (!useHeading('navigation') && $ns = getNS($id)) {
5145d87aa31SMichael Große                $name = shorten(noNS($id), ' (' . $ns . ')', 30);
5155d87aa31SMichael Große            }
5165d87aa31SMichael Große            $link = html_wikilink(':' . $id, $name);
5174eab6f7cSMichael Große            $eventData = [
5184eab6f7cSMichael Große                'listItemContent' => [$link],
5194eab6f7cSMichael Große                'page' => $id,
5204eab6f7cSMichael Große            ];
521cbb44eabSAndreas Gohr            Event::createAndTrigger('SEARCH_RESULT_PAGELOOKUP', $eventData);
5224eab6f7cSMichael Große            $html .= '<li>' . implode('', $eventData['listItemContent']) . '</li>';
52321fcef82SMichael Große        }
52421fcef82SMichael Große        $html .= '</ul> ';
52521fcef82SMichael Große        //clear float (see http://www.complexspiral.com/publications/containing-floats/)
52621fcef82SMichael Große        $html .= '<div class="clearer"></div>';
52721fcef82SMichael Große        $html .= '</div>';
52821fcef82SMichael Große
52921fcef82SMichael Große        return $html;
53021fcef82SMichael Große    }
53121fcef82SMichael Große
53221fcef82SMichael Große    /**
53321fcef82SMichael Große     * Build HTML for fulltext search results or "no results" message
53421fcef82SMichael Große     *
53521fcef82SMichael Große     * @param array $data the results of the fulltext search
53621fcef82SMichael Große     * @param array $highlight the terms to be highlighted in the results
53721fcef82SMichael Große     *
53821fcef82SMichael Große     * @return string
53921fcef82SMichael Große     */
54021fcef82SMichael Große    protected function getFulltextResultsHTML($data, $highlight)
54121fcef82SMichael Große    {
54221fcef82SMichael Große        global $lang;
54321fcef82SMichael Große
54421fcef82SMichael Große        if (empty($data)) {
54521fcef82SMichael Große            return '<div class="nothing">' . $lang['nothingfound'] . '</div>';
54621fcef82SMichael Große        }
54721fcef82SMichael Große
5482ce8affcSMichael Große        $html = '<div class="search_fulltextresult">';
5496d55fda7SMichael Große        $html .= '<h2>' . $lang['search_fullresults'] . ':</h2>';
5502ce8affcSMichael Große
55121fcef82SMichael Große        $html .= '<dl class="search_results">';
5528225e1abSMichael Große        $num = 0;
5538225e1abSMichael Große        $position = 0;
554cc3a3cdeSSatoshi Sahara        $FulltextSearch = new FulltextSearch();
5554c924eb8SMichael Große
55621fcef82SMichael Große        foreach ($data as $id => $cnt) {
557e2d055f5SAndreas Gohr            ++$position;
5584eab6f7cSMichael Große            $resultLink = html_wikilink(':' . $id, null, $highlight);
5594c924eb8SMichael Große
5604c924eb8SMichael Große            $resultHeader = [$resultLink];
5614c924eb8SMichael Große
5624eab6f7cSMichael Große
5634c924eb8SMichael Große            $restrictQueryToNSLink = $this->restrictQueryToNSLink(getNS($id));
5644c924eb8SMichael Große            if ($restrictQueryToNSLink) {
5654c924eb8SMichael Große                $resultHeader[] = $restrictQueryToNSLink;
5664c924eb8SMichael Große            }
5674c924eb8SMichael Große
5685d06a1e4SMichael Große            $resultBody = [];
5699a75abfbSMichael Große            $mtime = filemtime(wikiFN($id));
5705d06a1e4SMichael Große            $lastMod = '<span class="lastmod">' . $lang['lastmod'] . '</span> ';
57164159a61SAndreas Gohr            $lastMod .= '<time datetime="' . date_iso8601($mtime) . '" title="' . dformat($mtime) . '">' .
57264159a61SAndreas Gohr                dformat($mtime, '%f') .
57364159a61SAndreas Gohr                '</time>';
574f0861d1fSMichael Große            $resultBody['meta'] = $lastMod;
5755d06a1e4SMichael Große            if ($cnt !== 0) {
5768225e1abSMichael Große                $num++;
577b12bcb77SAnika Henke                $hits = '<span class="hits">' . $cnt . ' ' . $lang['hits'] . '</span>, ';
578f0861d1fSMichael Große                $resultBody['meta'] = $hits . $resultBody['meta'];
5790b1bbbbbSAndreas Gohr                if ($num <= $FulltextSearch->getMaxSnippets()) {
5809329b002SSatoshi Sahara                    $resultBody['snippet'] = $FulltextSearch->snippet($id, $highlight);
5819a75abfbSMichael Große                }
5829a75abfbSMichael Große            }
5839a75abfbSMichael Große
5844eab6f7cSMichael Große            $eventData = [
5854c924eb8SMichael Große                'resultHeader' => $resultHeader,
5865d06a1e4SMichael Große                'resultBody' => $resultBody,
5874eab6f7cSMichael Große                'page' => $id,
58878d786c9SMichael Große                'position' => $position,
5894eab6f7cSMichael Große            ];
590cbb44eabSAndreas Gohr            Event::createAndTrigger('SEARCH_RESULT_FULLPAGE', $eventData);
5915d06a1e4SMichael Große            $html .= '<div class="search_fullpage_result">';
5924eab6f7cSMichael Große            $html .= '<dt>' . implode(' ', $eventData['resultHeader']) . '</dt>';
5935d06a1e4SMichael Große            foreach ($eventData['resultBody'] as $class => $htmlContent) {
5945d06a1e4SMichael Große                $html .= "<dd class=\"$class\">$htmlContent</dd>";
5955d06a1e4SMichael Große            }
5965d06a1e4SMichael Große            $html .= '</div>';
59721fcef82SMichael Große        }
59821fcef82SMichael Große        $html .= '</dl>';
59921fcef82SMichael Große
6002ce8affcSMichael Große        $html .= '</div>';
6012ce8affcSMichael Große
60221fcef82SMichael Große        return $html;
60321fcef82SMichael Große    }
6044c924eb8SMichael Große
6054c924eb8SMichael Große    /**
6064c924eb8SMichael Große     * create a link to restrict the current query to a namespace
6074c924eb8SMichael Große     *
608ec27794fSMichael Große     * @param false|string $ns the namespace to which to restrict the query
6094c924eb8SMichael Große     *
610ec27794fSMichael Große     * @return false|string
6114c924eb8SMichael Große     */
6124c924eb8SMichael Große    protected function restrictQueryToNSLink($ns)
6134c924eb8SMichael Große    {
6144c924eb8SMichael Große        if (!$ns) {
6154c924eb8SMichael Große            return false;
6164c924eb8SMichael Große        }
617df977249SMichael Große        if (!$this->isNamespaceAssistanceAvailable($this->parsedQuery)) {
6184c924eb8SMichael Große            return false;
6194c924eb8SMichael Große        }
6204c924eb8SMichael Große        if (!empty($this->parsedQuery['ns']) && $this->parsedQuery['ns'][0] === $ns) {
6214c924eb8SMichael Große            return false;
6224c924eb8SMichael Große        }
62352d4cd42SMichael Große
6244c924eb8SMichael Große        $name = '@' . $ns;
62552d4cd42SMichael Große        return $this->searchState->withNamespace($ns)->getSearchLink($name);
6264c924eb8SMichael Große    }
62721fcef82SMichael Große}
628