xref: /dokuwiki/inc/Ui/Search.php (revision 2171f9cb4ef0f6e16e4bfe0548d2383544c0198f)
121fcef82SMichael Große<?php
221fcef82SMichael Große
321fcef82SMichael Großenamespace dokuwiki\Ui;
421fcef82SMichael Große
5427ed988SMichael Großeuse \dokuwiki\Form\Form;
6427ed988SMichael Große
721fcef82SMichael Großeclass Search extends Ui
821fcef82SMichael Große{
921fcef82SMichael Große    protected $query;
104c924eb8SMichael Große    protected $parsedQuery;
1118856c5dSMichael Große    protected $searchState;
1221fcef82SMichael Große    protected $pageLookupResults = array();
1321fcef82SMichael Große    protected $fullTextResults = array();
1421fcef82SMichael Große    protected $highlight = array();
1521fcef82SMichael Große
1621fcef82SMichael Große    /**
1721fcef82SMichael Große     * Search constructor.
186639a152SMichael Große     *
196639a152SMichael Große     * @param array  $pageLookupResults
206639a152SMichael Große     * @param array  $fullTextResults
216639a152SMichael Große     * @param string $highlight
2221fcef82SMichael Große     */
236639a152SMichael Große    public function __construct(array $pageLookupResults, array $fullTextResults, $highlight)
2421fcef82SMichael Große    {
25d09b5b64SMichael Große        global $QUERY;
264c924eb8SMichael Große        $Indexer = idx_get_indexer();
27d09b5b64SMichael Große
28d09b5b64SMichael Große        $this->query = $QUERY;
29bbc1da2eSMichael Große        $this->parsedQuery = ft_queryParser($Indexer, $QUERY);
3018856c5dSMichael Große        $this->searchState = new SearchState($this->parsedQuery);
3121fcef82SMichael Große
326639a152SMichael Große        $this->pageLookupResults = $pageLookupResults;
336639a152SMichael Große        $this->fullTextResults = $fullTextResults;
3421fcef82SMichael Große        $this->highlight = $highlight;
35b3cfe85aSMichael Große    }
36bbc1da2eSMichael Große
37b3cfe85aSMichael Große    /**
3821fcef82SMichael Große     * display the search result
3921fcef82SMichael Große     *
4021fcef82SMichael Große     * @return void
4121fcef82SMichael Große     */
4221fcef82SMichael Große    public function show()
4321fcef82SMichael Große    {
4421fcef82SMichael Große        $searchHTML = '';
4521fcef82SMichael Große
4621fcef82SMichael Große        $searchHTML .= $this->getSearchIntroHTML($this->query);
4721fcef82SMichael Große
482ce8affcSMichael Große        $searchHTML .= $this->getSearchFormHTML($this->query);
492ce8affcSMichael Große
5021fcef82SMichael Große        $searchHTML .= $this->getPageLookupHTML($this->pageLookupResults);
5121fcef82SMichael Große
5221fcef82SMichael Große        $searchHTML .= $this->getFulltextResultsHTML($this->fullTextResults, $this->highlight);
5321fcef82SMichael Große
5421fcef82SMichael Große        echo $searchHTML;
5521fcef82SMichael Große    }
5621fcef82SMichael Große
5721fcef82SMichael Große    /**
58427ed988SMichael Große     * Get a form which can be used to adjust/refine the search
59427ed988SMichael Große     *
60427ed988SMichael Große     * @param string $query
61427ed988SMichael Große     *
62427ed988SMichael Große     * @return string
63427ed988SMichael Große     */
64427ed988SMichael Große    protected function getSearchFormHTML($query)
65427ed988SMichael Große    {
66bbc1da2eSMichael Große        global $lang, $ID, $INPUT;
67427ed988SMichael Große
687fa270bcSMichael Große        $searchForm = (new Form(['method' => 'get'], true))->addClass('search-results-form');
69bb8ef867SMichael Große        $searchForm->setHiddenField('do', 'search');
70d22b78c8SMichael Große        $searchForm->setHiddenField('id', $ID);
711265b193SMichael Große        $searchForm->setHiddenField('sf', '1');
721265b193SMichael Große        if ($INPUT->has('dta')) {
731265b193SMichael Große            $searchForm->setHiddenField('dta', $INPUT->str('dta'));
74bbc1da2eSMichael Große        }
751265b193SMichael Große        if ($INPUT->has('dtb')) {
761265b193SMichael Große            $searchForm->setHiddenField('dtb', $INPUT->str('dtb'));
77bbc1da2eSMichael Große        }
781265b193SMichael Große        if ($INPUT->has('srt')) {
791265b193SMichael Große            $searchForm->setHiddenField('srt', $INPUT->str('srt'));
808d0e286aSMichael Große        }
814bdf82b5SAndreas Gohr        $searchForm->addFieldsetOpen()->addClass('search-form');
82d22b78c8SMichael Große        $searchForm->addTextInput('q')->val($query)->useInput(false);
83427ed988SMichael Große        $searchForm->addButton('', $lang['btn_search'])->attr('type', 'submit');
84bb8ef867SMichael Große
8518856c5dSMichael Große        $this->addSearchAssistanceElements($searchForm);
86bb8ef867SMichael Große
87427ed988SMichael Große        $searchForm->addFieldsetClose();
88427ed988SMichael Große
8916ece95cSMichael Große        trigger_event('FORM_SEARCH_OUTPUT', $searchForm);
9081a0edd9SMichael Große
91427ed988SMichael Große        return $searchForm->toHTML();
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
119*2171f9cbSAndreas 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
129*2171f9cbSAndreas 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    }
148b005809cSMichael Große
149be76738bSMichael Große    /**
150be76738bSMichael Große     * Check if the query is simple enough to modify its namespace limitations without breaking the rest of the query
151be76738bSMichael Große     *
152be76738bSMichael Große     * @param array $parsedQuery
153be76738bSMichael Große     *
154be76738bSMichael Große     * @return bool
155be76738bSMichael Große     */
156df977249SMichael Große    protected function isNamespaceAssistanceAvailable(array $parsedQuery) {
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     */
171df977249SMichael Große    protected function isFragmentAssistanceAvailable(array $parsedQuery) {
172df977249SMichael Große        if (preg_match('/[\(\)\|]/', $parsedQuery['query']) === 1) {
173bb8ef867SMichael Große            return false;
174bb8ef867SMichael Große        }
175bb8ef867SMichael Große
176bb8ef867SMichael Große        if (!empty($parsedQuery['phrases'])) {
177bb8ef867SMichael Große            return false;
178bb8ef867SMichael Große        }
179bb8ef867SMichael Große
180bb8ef867SMichael Große        return true;
181bb8ef867SMichael Große    }
182bb8ef867SMichael Große
183bb8ef867SMichael Große    /**
184bb8ef867SMichael Große     * Add the elements to be used for search assistance
185bb8ef867SMichael Große     *
186bb8ef867SMichael Große     * @param Form $searchForm
187bb8ef867SMichael Große     */
18818856c5dSMichael Große    protected function addSearchAssistanceElements(Form $searchForm)
189bb8ef867SMichael Große    {
190c5bd5721SMichael Große        global $lang;
191c5bd5721SMichael Große        $searchForm->addButton('toggleAssistant', $lang['search_toggle_tools'])
192bb8ef867SMichael Große            ->attr('type', 'button')
1934bdf82b5SAndreas Gohr            ->addClass('toggleAssistant');
194bb8ef867SMichael Große
195bb8ef867SMichael Große        $searchForm->addTagOpen('div')
1964bdf82b5SAndreas Gohr            ->addClass('advancedOptions')
197*2171f9cbSAndreas Gohr            ->attr('style', 'display: none;')
198*2171f9cbSAndreas Gohr            ->attr('aria-hidden', 'true');
199bb8ef867SMichael Große
20018856c5dSMichael Große        $this->addFragmentBehaviorLinks($searchForm);
20118856c5dSMichael Große        $this->addNamespaceSelector($searchForm);
20218856c5dSMichael Große        $this->addDateSelector($searchForm);
203b005809cSMichael Große        $this->addSortTool($searchForm);
204bb8ef867SMichael Große
205bb8ef867SMichael Große        $searchForm->addTagClose('div');
206bb8ef867SMichael Große    }
207bb8ef867SMichael Große
208be76738bSMichael Große    /**
209be76738bSMichael Große     *  Add the elements to adjust the fragment search behavior
210be76738bSMichael Große     *
211be76738bSMichael Große     * @param Form $searchForm
212be76738bSMichael Große     */
21318856c5dSMichael Große    protected function addFragmentBehaviorLinks(Form $searchForm)
2144d0cb6e1SMichael Große    {
215df977249SMichael Große        if (!$this->isFragmentAssistanceAvailable($this->parsedQuery)) {
216df977249SMichael Große            return;
217df977249SMichael Große        }
218b005809cSMichael Große        global $lang;
2194d0cb6e1SMichael Große
220b005809cSMichael Große        $options = [
221b005809cSMichael Große            'exact' => [
222b005809cSMichael Große                'label' => $lang['search_exact_match'],
223b005809cSMichael Große                'and' => array_map(function ($term) {
224b005809cSMichael Große                    return trim($term, '*');
225b005809cSMichael Große                }, $this->parsedQuery['and']),
226df977249SMichael Große                'not' => array_map(function ($term) {
227df977249SMichael Große                    return trim($term, '*');
228df977249SMichael Große                }, $this->parsedQuery['not']),
229b005809cSMichael Große            ],
230b005809cSMichael Große            'starts' => [
231b005809cSMichael Große                'label' => $lang['search_starts_with'],
232b005809cSMichael Große                'and' => array_map(function ($term) {
233b005809cSMichael Große                    return trim($term, '*') . '*';
234df977249SMichael Große                }, $this->parsedQuery['and']),
235df977249SMichael Große                'not' => array_map(function ($term) {
236df977249SMichael Große                    return trim($term, '*') . '*';
237df977249SMichael Große                }, $this->parsedQuery['not']),
238b005809cSMichael Große            ],
239b005809cSMichael Große            'ends' => [
240b005809cSMichael Große                'label' => $lang['search_ends_with'],
241b005809cSMichael Große                'and' => array_map(function ($term) {
242b005809cSMichael Große                    return '*' . trim($term, '*');
243df977249SMichael Große                }, $this->parsedQuery['and']),
244df977249SMichael Große                'not' => array_map(function ($term) {
245df977249SMichael Große                    return '*' . trim($term, '*');
246df977249SMichael Große                }, $this->parsedQuery['not']),
247b005809cSMichael Große            ],
248b005809cSMichael Große            'contains' => [
249b005809cSMichael Große                'label' => $lang['search_contains'],
250b005809cSMichael Große                'and' => array_map(function ($term) {
251b005809cSMichael Große                    return '*' . trim($term, '*') . '*';
252df977249SMichael Große                }, $this->parsedQuery['and']),
253df977249SMichael Große                'not' => array_map(function ($term) {
254df977249SMichael Große                    return '*' . trim($term, '*') . '*';
255df977249SMichael Große                }, $this->parsedQuery['not']),
256b005809cSMichael Große            ]
257b005809cSMichael Große        ];
258b005809cSMichael Große
259b005809cSMichael Große        // detect current
260c6b5b74aSMichael Große        $activeOption = 'custom';
261b005809cSMichael Große        foreach ($options as $key => $option) {
262b005809cSMichael Große            if ($this->parsedQuery['and'] === $option['and']) {
263b005809cSMichael Große                $activeOption = $key;
264b005809cSMichael Große            }
265b005809cSMichael Große        }
266c6b5b74aSMichael Große        if ($activeOption === 'custom') {
267c6b5b74aSMichael Große            $options = array_merge(['custom' => [
268c6b5b74aSMichael Große                'label' => $lang['search_custom_match'],
269c6b5b74aSMichael Große            ]], $options);
270c6b5b74aSMichael Große        }
271b005809cSMichael Große
272*2171f9cbSAndreas Gohr        $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true');
273b005809cSMichael Große        // render current
2744bdf82b5SAndreas Gohr        $currentWrapper = $searchForm->addTagOpen('div')->addClass('current');
275b005809cSMichael Große        if ($activeOption !== 'exact') {
2764bdf82b5SAndreas Gohr            $currentWrapper->addClass('changed');
277b005809cSMichael Große        }
278b005809cSMichael Große        $searchForm->addHTML($options[$activeOption]['label']);
279b005809cSMichael Große        $searchForm->addTagClose('div');
280b005809cSMichael Große
281b005809cSMichael Große        // render options list
282*2171f9cbSAndreas Gohr        $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false');
283b005809cSMichael Große
284b005809cSMichael Große        foreach ($options as $key => $option) {
2854bdf82b5SAndreas Gohr            $listItem = $searchForm->addTagOpen('li');
286b005809cSMichael Große
287b005809cSMichael Große            if ($key === $activeOption) {
2884bdf82b5SAndreas Gohr                $listItem->addClass('active');
289b005809cSMichael Große                $searchForm->addHTML($option['label']);
290b005809cSMichael Große            } else {
29152d4cd42SMichael Große                $link = $this->searchState
29252d4cd42SMichael Große                    ->withFragments($option['and'], $option['not'])
29352d4cd42SMichael Große                    ->getSearchLink($option['label'])
29452d4cd42SMichael Große                ;
29552d4cd42SMichael Große                $searchForm->addHTML($link);
296b005809cSMichael Große            }
297b005809cSMichael Große            $searchForm->addTagClose('li');
298b005809cSMichael Große        }
299b005809cSMichael Große        $searchForm->addTagClose('ul');
3004d0cb6e1SMichael Große
3014d0cb6e1SMichael Große        $searchForm->addTagClose('div');
302b005809cSMichael Große
303b005809cSMichael Große        // render options list
3044d0cb6e1SMichael Große    }
3054d0cb6e1SMichael Große
306bb8ef867SMichael Große    /**
307bb8ef867SMichael Große     * Add the elements for the namespace selector
308bb8ef867SMichael Große     *
309bb8ef867SMichael Große     * @param Form $searchForm
310bb8ef867SMichael Große     */
31118856c5dSMichael Große    protected function addNamespaceSelector(Form $searchForm)
312bb8ef867SMichael Große    {
313df977249SMichael Große        if (!$this->isNamespaceAssistanceAvailable($this->parsedQuery)) {
314df977249SMichael Große            return;
315df977249SMichael Große        }
316df977249SMichael Große
317b005809cSMichael Große        global $lang;
318b005809cSMichael Große
31918856c5dSMichael Große        $baseNS = empty($this->parsedQuery['ns']) ? '' : $this->parsedQuery['ns'][0];
320bbc1da2eSMichael Große        $extraNS = $this->getAdditionalNamespacesFromResults($baseNS);
3214d0cb6e1SMichael Große
322*2171f9cbSAndreas Gohr        $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true');
323b005809cSMichael Große        // render current
3244bdf82b5SAndreas Gohr        $currentWrapper = $searchForm->addTagOpen('div')->addClass('current');
325bbc1da2eSMichael Große        if ($baseNS) {
3264bdf82b5SAndreas Gohr            $currentWrapper->addClass('changed');
327b005809cSMichael Große            $searchForm->addHTML('@' . $baseNS);
328b005809cSMichael Große        } else {
329b005809cSMichael Große            $searchForm->addHTML($lang['search_any_ns']);
330b005809cSMichael Große        }
331b005809cSMichael Große        $searchForm->addTagClose('div');
332b005809cSMichael Große
333b005809cSMichael Große        // render options list
334*2171f9cbSAndreas Gohr        $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false');
335b005809cSMichael Große
3364bdf82b5SAndreas Gohr        $listItem = $searchForm->addTagOpen('li');
337b005809cSMichael Große        if ($baseNS) {
3384bdf82b5SAndreas Gohr            $listItem->addClass('active');
33952d4cd42SMichael Große            $link = $this->searchState->withNamespace('')->getSearchLink($lang['search_any_ns']);
34052d4cd42SMichael Große            $searchForm->addHTML($link);
341b005809cSMichael Große        } else {
342b005809cSMichael Große            $searchForm->addHTML($lang['search_any_ns']);
343bb8ef867SMichael Große        }
344b005809cSMichael Große        $searchForm->addTagClose('li');
345bb8ef867SMichael Große
34618856c5dSMichael Große        foreach ($extraNS as $ns => $count) {
3474bdf82b5SAndreas Gohr            $listItem = $searchForm->addTagOpen('li');
3481d918893SAndreas Gohr            $label = $ns . ($count ? " <bdi>($count)</bdi>" : '');
3494d0cb6e1SMichael Große
350b005809cSMichael Große            if ($ns === $baseNS) {
3514bdf82b5SAndreas Gohr                $listItem->addClass('active');
352b005809cSMichael Große                $searchForm->addHTML($label);
353b005809cSMichael Große            } else {
35452d4cd42SMichael Große                $link = $this->searchState->withNamespace($ns)->getSearchLink($label);
35552d4cd42SMichael Große                $searchForm->addHTML($link);
356bb8ef867SMichael Große            }
357b005809cSMichael Große            $searchForm->addTagClose('li');
358bb8ef867SMichael Große        }
359b005809cSMichael Große        $searchForm->addTagClose('ul');
360bb8ef867SMichael Große
361bb8ef867SMichael Große        $searchForm->addTagClose('div');
362b005809cSMichael Große
363bb8ef867SMichael Große    }
364bb8ef867SMichael Große
365bb8ef867SMichael Große    /**
366bb8ef867SMichael Große     * Parse the full text results for their top namespaces below the given base namespace
367bb8ef867SMichael Große     *
368bb8ef867SMichael Große     * @param string $baseNS the namespace within which was searched, empty string for root namespace
369bb8ef867SMichael Große     *
370bb8ef867SMichael Große     * @return array an associative array with namespace => #number of found pages, sorted descending
371bb8ef867SMichael Große     */
372bb8ef867SMichael Große    protected function getAdditionalNamespacesFromResults($baseNS)
373bb8ef867SMichael Große    {
374bb8ef867SMichael Große        $namespaces = [];
375bb8ef867SMichael Große        $baseNSLength = strlen($baseNS);
376bb8ef867SMichael Große        foreach ($this->fullTextResults as $page => $numberOfHits) {
377bb8ef867SMichael Große            $namespace = getNS($page);
378bb8ef867SMichael Große            if (!$namespace) {
379bb8ef867SMichael Große                continue;
380bb8ef867SMichael Große            }
381bb8ef867SMichael Große            if ($namespace === $baseNS) {
382bb8ef867SMichael Große                continue;
383bb8ef867SMichael Große            }
384bb8ef867SMichael Große            $firstColon = strpos((string)$namespace, ':', $baseNSLength + 1) ?: strlen($namespace);
385bb8ef867SMichael Große            $subtopNS = substr($namespace, 0, $firstColon);
386bb8ef867SMichael Große            if (empty($namespaces[$subtopNS])) {
387bb8ef867SMichael Große                $namespaces[$subtopNS] = 0;
388bb8ef867SMichael Große            }
389bb8ef867SMichael Große            $namespaces[$subtopNS] += 1;
390bb8ef867SMichael Große        }
39155dc8783SMichael Große        ksort($namespaces);
392bb8ef867SMichael Große        arsort($namespaces);
393bb8ef867SMichael Große        return $namespaces;
394bb8ef867SMichael Große    }
395bb8ef867SMichael Große
396bb8ef867SMichael Große    /**
397bbc1da2eSMichael Große     * @ToDo: custom date input
398bbc1da2eSMichael Große     *
399bbc1da2eSMichael Große     * @param Form $searchForm
400bbc1da2eSMichael Große     */
401b005809cSMichael Große    protected function addDateSelector(Form $searchForm)
402b005809cSMichael Große    {
403b005809cSMichael Große        global $INPUT, $lang;
404bbc1da2eSMichael Große
405b005809cSMichael Große        $options = [
406b005809cSMichael Große            'any' => [
407b005809cSMichael Große                'before' => false,
408b005809cSMichael Große                'after' => false,
409b005809cSMichael Große                'label' => $lang['search_any_time'],
410b005809cSMichael Große            ],
411b005809cSMichael Große            'week' => [
412b005809cSMichael Große                'before' => false,
413b005809cSMichael Große                'after' => '1 week ago',
414b005809cSMichael Große                'label' => $lang['search_past_7_days'],
415b005809cSMichael Große            ],
416b005809cSMichael Große            'month' => [
417b005809cSMichael Große                'before' => false,
418b005809cSMichael Große                'after' => '1 month ago',
419b005809cSMichael Große                'label' => $lang['search_past_month'],
420b005809cSMichael Große            ],
421b005809cSMichael Große            'year' => [
422b005809cSMichael Große                'before' => false,
423b005809cSMichael Große                'after' => '1 year ago',
424b005809cSMichael Große                'label' => $lang['search_past_year'],
425b005809cSMichael Große            ],
426b005809cSMichael Große        ];
427b005809cSMichael Große        $activeOption = 'any';
428b005809cSMichael Große        foreach ($options as $key => $option) {
4291265b193SMichael Große            if ($INPUT->str('dta') === $option['after']) {
430b005809cSMichael Große                $activeOption = $key;
431b005809cSMichael Große                break;
432b005809cSMichael Große            }
433b005809cSMichael Große        }
434b005809cSMichael Große
435*2171f9cbSAndreas Gohr        $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true');
436b005809cSMichael Große        // render current
4374bdf82b5SAndreas Gohr        $currentWrapper = $searchForm->addTagOpen('div')->addClass('current');
4381265b193SMichael Große        if ($INPUT->has('dtb') || $INPUT->has('dta')) {
4394bdf82b5SAndreas Gohr            $currentWrapper->addClass('changed');
440bbc1da2eSMichael Große        }
441b005809cSMichael Große        $searchForm->addHTML($options[$activeOption]['label']);
442b005809cSMichael Große        $searchForm->addTagClose('div');
443bbc1da2eSMichael Große
444b005809cSMichael Große        // render options list
445*2171f9cbSAndreas Gohr        $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false');
446b005809cSMichael Große
447b005809cSMichael Große        foreach ($options as $key => $option) {
4484bdf82b5SAndreas Gohr            $listItem = $searchForm->addTagOpen('li');
449b005809cSMichael Große
450b005809cSMichael Große            if ($key === $activeOption) {
4514bdf82b5SAndreas Gohr                $listItem->addClass('active');
452b005809cSMichael Große                $searchForm->addHTML($option['label']);
453bbc1da2eSMichael Große            } else {
45452d4cd42SMichael Große                $link = $this->searchState
45552d4cd42SMichael Große                    ->withTimeLimitations($option['after'], $option['before'])
45652d4cd42SMichael Große                    ->getSearchLink($option['label'])
45752d4cd42SMichael Große                ;
45852d4cd42SMichael Große                $searchForm->addHTML($link);
459bbc1da2eSMichael Große            }
460b005809cSMichael Große            $searchForm->addTagClose('li');
461bbc1da2eSMichael Große        }
462b005809cSMichael Große        $searchForm->addTagClose('ul');
463bbc1da2eSMichael Große
464bbc1da2eSMichael Große        $searchForm->addTagClose('div');
465bbc1da2eSMichael Große    }
466bbc1da2eSMichael Große
467bbc1da2eSMichael Große
468bbc1da2eSMichael Große    /**
46921fcef82SMichael Große     * Build the intro text for the search page
47021fcef82SMichael Große     *
47121fcef82SMichael Große     * @param string $query the search query
47221fcef82SMichael Große     *
47321fcef82SMichael Große     * @return string
47421fcef82SMichael Große     */
47521fcef82SMichael Große    protected function getSearchIntroHTML($query)
47621fcef82SMichael Große    {
4772ce8affcSMichael Große        global $lang;
47821fcef82SMichael Große
47921fcef82SMichael Große        $intro = p_locale_xhtml('searchpage');
4802ce8affcSMichael Große
4812ce8affcSMichael Große        $queryPagename = $this->createPagenameFromQuery($this->parsedQuery);
4822ce8affcSMichael Große        $createQueryPageLink = html_wikilink($queryPagename . '?do=edit', $queryPagename);
4832ce8affcSMichael Große
4842ce8affcSMichael Große        $pagecreateinfo = '';
4852ce8affcSMichael Große        if (auth_quickaclcheck($queryPagename) >= AUTH_CREATE) {
4862ce8affcSMichael Große            $pagecreateinfo = sprintf($lang['searchcreatepage'], $createQueryPageLink);
4872ce8affcSMichael Große        }
48821fcef82SMichael Große        $intro = str_replace(
48921fcef82SMichael Große            array('@QUERY@', '@SEARCH@', '@CREATEPAGEINFO@'),
49021fcef82SMichael Große            array(hsc(rawurlencode($query)), hsc($query), $pagecreateinfo),
49121fcef82SMichael Große            $intro
49221fcef82SMichael Große        );
4932ce8affcSMichael Große
49421fcef82SMichael Große        return $intro;
49521fcef82SMichael Große    }
49621fcef82SMichael Große
49721fcef82SMichael Große    /**
4982ce8affcSMichael Große     * Create a pagename based the parsed search query
4992ce8affcSMichael Große     *
5002ce8affcSMichael Große     * @param array $parsedQuery
5012ce8affcSMichael Große     *
5022ce8affcSMichael Große     * @return string pagename constructed from the parsed query
5032ce8affcSMichael Große     */
5042ce8affcSMichael Große    protected function createPagenameFromQuery($parsedQuery)
5052ce8affcSMichael Große    {
5062ce8affcSMichael Große        $pagename = '';
5072ce8affcSMichael Große        if (!empty($parsedQuery['ns'])) {
5082ce8affcSMichael Große            $pagename .= cleanID($parsedQuery['ns'][0]);
5092ce8affcSMichael Große        }
5102ce8affcSMichael Große        $pagename .= ':' . cleanID(implode(' ' , $parsedQuery['highlight']));
5112ce8affcSMichael Große        return $pagename;
5122ce8affcSMichael Große    }
5132ce8affcSMichael Große
5142ce8affcSMichael Große    /**
51521fcef82SMichael Große     * Build HTML for a list of pages with matching pagenames
51621fcef82SMichael Große     *
51721fcef82SMichael Große     * @param array $data search results
51821fcef82SMichael Große     *
51921fcef82SMichael Große     * @return string
52021fcef82SMichael Große     */
52121fcef82SMichael Große    protected function getPageLookupHTML($data)
52221fcef82SMichael Große    {
52321fcef82SMichael Große        if (empty($data)) {
52421fcef82SMichael Große            return '';
52521fcef82SMichael Große        }
52621fcef82SMichael Große
52721fcef82SMichael Große        global $lang;
52821fcef82SMichael Große
52921fcef82SMichael Große        $html = '<div class="search_quickresult">';
53021fcef82SMichael Große        $html .= '<h3>' . $lang['quickhits'] . ':</h3>';
53121fcef82SMichael Große        $html .= '<ul class="search_quickhits">';
53221fcef82SMichael Große        foreach ($data as $id => $title) {
5334eab6f7cSMichael Große            $link = html_wikilink(':' . $id);
5344eab6f7cSMichael Große            $eventData = [
5354eab6f7cSMichael Große                'listItemContent' => [$link],
5364eab6f7cSMichael Große                'page' => $id,
5374eab6f7cSMichael Große            ];
5384eab6f7cSMichael Große            trigger_event('SEARCH_RESULT_PAGELOOKUP', $eventData);
5394eab6f7cSMichael Große            $html .= '<li>' . implode('', $eventData['listItemContent']) . '</li>';
54021fcef82SMichael Große        }
54121fcef82SMichael Große        $html .= '</ul> ';
54221fcef82SMichael Große        //clear float (see http://www.complexspiral.com/publications/containing-floats/)
54321fcef82SMichael Große        $html .= '<div class="clearer"></div>';
54421fcef82SMichael Große        $html .= '</div>';
54521fcef82SMichael Große
54621fcef82SMichael Große        return $html;
54721fcef82SMichael Große    }
54821fcef82SMichael Große
54921fcef82SMichael Große    /**
55021fcef82SMichael Große     * Build HTML for fulltext search results or "no results" message
55121fcef82SMichael Große     *
55221fcef82SMichael Große     * @param array $data      the results of the fulltext search
55321fcef82SMichael Große     * @param array $highlight the terms to be highlighted in the results
55421fcef82SMichael Große     *
55521fcef82SMichael Große     * @return string
55621fcef82SMichael Große     */
55721fcef82SMichael Große    protected function getFulltextResultsHTML($data, $highlight)
55821fcef82SMichael Große    {
55921fcef82SMichael Große        global $lang;
56021fcef82SMichael Große
56121fcef82SMichael Große        if (empty($data)) {
56221fcef82SMichael Große            return '<div class="nothing">' . $lang['nothingfound'] . '</div>';
56321fcef82SMichael Große        }
56421fcef82SMichael Große
5652ce8affcSMichael Große        $html = '<div class="search_fulltextresult">';
5662ce8affcSMichael Große        $html .= '<h3>' . $lang['search_fullresults'] . ':</h3>';
5672ce8affcSMichael Große
56821fcef82SMichael Große        $html .= '<dl class="search_results">';
56921fcef82SMichael Große        $num = 1;
5704c924eb8SMichael Große
57121fcef82SMichael Große        foreach ($data as $id => $cnt) {
5724eab6f7cSMichael Große            $resultLink = html_wikilink(':' . $id, null, $highlight);
5734c924eb8SMichael Große
5744c924eb8SMichael Große            $resultHeader = [$resultLink];
5754c924eb8SMichael Große
5764eab6f7cSMichael Große
5774c924eb8SMichael Große            $restrictQueryToNSLink = $this->restrictQueryToNSLink(getNS($id));
5784c924eb8SMichael Große            if ($restrictQueryToNSLink) {
5794c924eb8SMichael Große                $resultHeader[] = $restrictQueryToNSLink;
5804c924eb8SMichael Große            }
5814c924eb8SMichael Große
5829a75abfbSMichael Große            $snippet = '';
5839a75abfbSMichael Große            $lastMod = '';
5849a75abfbSMichael Große            $mtime = filemtime(wikiFN($id));
5859a75abfbSMichael Große            if ($cnt !== 0) {
5869a75abfbSMichael Große                $resultHeader[] = $cnt . ' ' . $lang['hits'];
5879a75abfbSMichael Große                if ($num < FT_SNIPPET_NUMBER) { // create snippets for the first number of matches only
5889a75abfbSMichael Große                    $snippet = '<dd>' . ft_snippet($id, $highlight) . '</dd>';
5899a75abfbSMichael Große                    $lastMod = '<span class="search_results__lastmod">' . $lang['lastmod'] . ' ';
5900fc38806SAndreas Gohr                    $lastMod .= '<time datetime="' . date_iso8601($mtime) . '" title="'.dformat($mtime).'">' . dformat($mtime, '%f') . '</time>';
5919a75abfbSMichael Große                    $lastMod .= '</span>';
5929a75abfbSMichael Große                }
5939a75abfbSMichael Große                $num++;
5949a75abfbSMichael Große            }
5959a75abfbSMichael Große
5969a75abfbSMichael Große            $metaLine = '<div class="search_results__metaLine">';
5979a75abfbSMichael Große            $metaLine .= $lastMod;
5989a75abfbSMichael Große            $metaLine .= '</div>';
5999a75abfbSMichael Große
6009a75abfbSMichael Große
6014eab6f7cSMichael Große            $eventData = [
6024c924eb8SMichael Große                'resultHeader' => $resultHeader,
6039a75abfbSMichael Große                'resultBody' => [$metaLine, $snippet],
6044eab6f7cSMichael Große                'page' => $id,
6054eab6f7cSMichael Große            ];
6064eab6f7cSMichael Große            trigger_event('SEARCH_RESULT_FULLPAGE', $eventData);
6074eab6f7cSMichael Große            $html .= '<div class="search_fullpage_result">';
6084eab6f7cSMichael Große            $html .= '<dt>' . implode(' ', $eventData['resultHeader']) . '</dt>';
6094eab6f7cSMichael Große            $html .= implode('', $eventData['resultBody']);
6104eab6f7cSMichael Große            $html .= '</div>';
61121fcef82SMichael Große        }
61221fcef82SMichael Große        $html .= '</dl>';
61321fcef82SMichael Große
6142ce8affcSMichael Große        $html .= '</div>';
6152ce8affcSMichael Große
61621fcef82SMichael Große        return $html;
61721fcef82SMichael Große    }
6184c924eb8SMichael Große
6194c924eb8SMichael Große    /**
6204c924eb8SMichael Große     * create a link to restrict the current query to a namespace
6214c924eb8SMichael Große     *
6224c924eb8SMichael Große     * @param bool|string $ns the namespace to which to restrict the query
6234c924eb8SMichael Große     *
6244c924eb8SMichael Große     * @return bool|string
6254c924eb8SMichael Große     */
6264c924eb8SMichael Große    protected function restrictQueryToNSLink($ns)
6274c924eb8SMichael Große    {
6284c924eb8SMichael Große        if (!$ns) {
6294c924eb8SMichael Große            return false;
6304c924eb8SMichael Große        }
631df977249SMichael Große        if (!$this->isNamespaceAssistanceAvailable($this->parsedQuery)) {
6324c924eb8SMichael Große            return false;
6334c924eb8SMichael Große        }
6344c924eb8SMichael Große        if (!empty($this->parsedQuery['ns']) && $this->parsedQuery['ns'][0] === $ns) {
6354c924eb8SMichael Große            return false;
6364c924eb8SMichael Große        }
63752d4cd42SMichael Große
6384c924eb8SMichael Große        $name = '@' . $ns;
63952d4cd42SMichael Große        return $this->searchState->withNamespace($ns)->getSearchLink($name);
6404c924eb8SMichael Große    }
64121fcef82SMichael Große}
642