xref: /dokuwiki/inc/Ui/Search.php (revision df9772490f769599375e1ea1e769c2f18740eafb)
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
46427ed988SMichael Große        $searchHTML .= $this->getSearchFormHTML($this->query);
47427ed988SMichael Große
4821fcef82SMichael Große        $searchHTML .= $this->getSearchIntroHTML($this->query);
4921fcef82SMichael 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
68bb8ef867SMichael Große        $searchForm = (new Form())->attrs(['method' => 'get'])->addClass('search-results-form');
69bb8ef867SMichael Große        $searchForm->setHiddenField('do', 'search');
70d22b78c8SMichael Große        $searchForm->setHiddenField('id', $ID);
71d09b5b64SMichael Große        $searchForm->setHiddenField('searchPageForm', '1');
72bbc1da2eSMichael Große        if ($INPUT->has('after')) {
73bbc1da2eSMichael Große            $searchForm->setHiddenField('after', $INPUT->str('after'));
74bbc1da2eSMichael Große        }
75bbc1da2eSMichael Große        if ($INPUT->has('before')) {
76bbc1da2eSMichael Große            $searchForm->setHiddenField('before', $INPUT->str('before'));
77bbc1da2eSMichael Große        }
788d0e286aSMichael Große        if ($INPUT->has('sort')) {
798d0e286aSMichael Große            $searchForm->setHiddenField('sort', $INPUT->str('sort'));
808d0e286aSMichael Große        }
81bb8ef867SMichael Große        $searchForm->addFieldsetOpen()->addClass('search-results-form__fieldset');
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
8981a0edd9SMichael Große        trigger_event('SEARCH_FORM_DISPLAY', $searchForm);
9081a0edd9SMichael Große
91427ed988SMichael Große        return $searchForm->toHTML();
92427ed988SMichael Große    }
93427ed988SMichael Große
94b005809cSMichael Große    protected function addSortTool(Form $searchForm)
95b005809cSMichael Große    {
96b005809cSMichael Große        global $INPUT, $lang;
97b005809cSMichael Große
98b005809cSMichael Große        $options = [
99b005809cSMichael Große            'hits' => [
100b005809cSMichael Große                'label' => $lang['search_sort_by_hits'],
101b005809cSMichael Große                'sort' => '',
102b005809cSMichael Große            ],
103b005809cSMichael Große            'mtime' => [
104b005809cSMichael Große                'label' => $lang['search_sort_by_mtime'],
105b005809cSMichael Große                'sort' => 'mtime',
106b005809cSMichael Große            ],
107b005809cSMichael Große        ];
108b005809cSMichael Große        $activeOption = 'hits';
109b005809cSMichael Große
110b005809cSMichael Große        if ($INPUT->str('sort') === 'mtime') {
111b005809cSMichael Große            $activeOption = 'mtime';
112b005809cSMichael Große        }
113b005809cSMichael Große
114b005809cSMichael Große        $searchForm->addTagOpen('div')->addClass('search-tool js-search-tool');
115b005809cSMichael Große        // render current
116b005809cSMichael Große        $currentWrapper = $searchForm->addTagOpen('div')->addClass('search-tool__current js-current');
117b005809cSMichael Große        if ($activeOption !== 'hits') {
118b005809cSMichael Große            $currentWrapper->addClass('search-tool__current--changed');
119b005809cSMichael Große        }
120b005809cSMichael Große        $searchForm->addHTML($options[$activeOption]['label']);
121b005809cSMichael Große        $searchForm->addTagClose('div');
122b005809cSMichael Große
123b005809cSMichael Große        // render options list
124b005809cSMichael Große        $searchForm->addTagOpen('ul')->addClass('search-tool__options-list js-optionsList');
125b005809cSMichael Große
126b005809cSMichael Große        foreach ($options as $key => $option) {
127b005809cSMichael Große            $listItem = $searchForm->addTagOpen('li')->addClass('search-tool__options-list-item');
128b005809cSMichael Große
129b005809cSMichael Große            if ($key === $activeOption) {
130b005809cSMichael Große                $listItem->addClass('search-tool__options-list-item--active');
131b005809cSMichael Große                $searchForm->addHTML($option['label']);
132b005809cSMichael Große            } else {
133b005809cSMichael Große                $this->searchState->addSearchLinkSort(
134b005809cSMichael Große                    $searchForm,
135b005809cSMichael Große                    $option['label'],
136b005809cSMichael Große                    $option['sort']
137b005809cSMichael Große                );
138b005809cSMichael Große            }
139b005809cSMichael Große            $searchForm->addTagClose('li');
140b005809cSMichael Große        }
141b005809cSMichael Große        $searchForm->addTagClose('ul');
142b005809cSMichael Große
143b005809cSMichael Große        $searchForm->addTagClose('div');
144b005809cSMichael Große
145b005809cSMichael Große    }
146b005809cSMichael Große
147*df977249SMichael Große    protected function isNamespaceAssistanceAvailable(array $parsedQuery) {
148*df977249SMichael Große        if (preg_match('/[\(\)\|]/', $parsedQuery['query']) === 1) {
149bb8ef867SMichael Große            return false;
150bb8ef867SMichael Große        }
151*df977249SMichael Große
152*df977249SMichael Große        return true;
153*df977249SMichael Große    }
154*df977249SMichael Große
155*df977249SMichael Große    protected function isFragmentAssistanceAvailable(array $parsedQuery) {
156*df977249SMichael Große        if (preg_match('/[\(\)\|]/', $parsedQuery['query']) === 1) {
157bb8ef867SMichael Große            return false;
158bb8ef867SMichael Große        }
159bb8ef867SMichael Große
160bb8ef867SMichael Große        if (!empty($parsedQuery['phrases'])) {
161bb8ef867SMichael Große            return false;
162bb8ef867SMichael Große        }
163bb8ef867SMichael Große
164bb8ef867SMichael Große        return true;
165bb8ef867SMichael Große    }
166bb8ef867SMichael Große
167bb8ef867SMichael Große    /**
168bb8ef867SMichael Große     * Add the elements to be used for search assistance
169bb8ef867SMichael Große     *
170bb8ef867SMichael Große     * @param Form $searchForm
171bb8ef867SMichael Große     */
17218856c5dSMichael Große    protected function addSearchAssistanceElements(Form $searchForm)
173bb8ef867SMichael Große    {
174bb8ef867SMichael Große        $searchForm->addButton('toggleAssistant', 'toggle search assistant')
175bb8ef867SMichael Große            ->attr('type', 'button')
176bb8ef867SMichael Große            ->id('search-results-form__show-assistance-button')
177bb8ef867SMichael Große            ->addClass('search-results-form__show-assistance-button');
178bb8ef867SMichael Große
179bb8ef867SMichael Große        $searchForm->addTagOpen('div')
180bb8ef867SMichael Große            ->addClass('js-advancedSearchOptions')
181bb8ef867SMichael Große            ->attr('style', 'display: none;');
182bb8ef867SMichael Große
18318856c5dSMichael Große        $this->addFragmentBehaviorLinks($searchForm);
18418856c5dSMichael Große        $this->addNamespaceSelector($searchForm);
18518856c5dSMichael Große        $this->addDateSelector($searchForm);
186b005809cSMichael Große        $this->addSortTool($searchForm);
187bb8ef867SMichael Große
188bb8ef867SMichael Große        $searchForm->addTagClose('div');
189bb8ef867SMichael Große    }
190bb8ef867SMichael Große
19118856c5dSMichael Große    protected function addFragmentBehaviorLinks(Form $searchForm)
1924d0cb6e1SMichael Große    {
193*df977249SMichael Große        if (!$this->isFragmentAssistanceAvailable($this->parsedQuery)) {
194*df977249SMichael Große            return;
195*df977249SMichael Große        }
196b005809cSMichael Große        global $lang;
1974d0cb6e1SMichael Große
198b005809cSMichael Große        $options = [
199b005809cSMichael Große            'exact' => [
200b005809cSMichael Große                'label' => $lang['search_exact_match'],
201b005809cSMichael Große                'and' => array_map(function ($term) {
202b005809cSMichael Große                    return trim($term, '*');
203b005809cSMichael Große                }, $this->parsedQuery['and']),
204*df977249SMichael Große                'not' => array_map(function ($term) {
205*df977249SMichael Große                    return trim($term, '*');
206*df977249SMichael Große                }, $this->parsedQuery['not']),
207b005809cSMichael Große            ],
208b005809cSMichael Große            'starts' => [
209b005809cSMichael Große                'label' => $lang['search_starts_with'],
210b005809cSMichael Große                'and' => array_map(function ($term) {
211b005809cSMichael Große                    return trim($term, '*') . '*';
212*df977249SMichael Große                }, $this->parsedQuery['and']),
213*df977249SMichael Große                'not' => array_map(function ($term) {
214*df977249SMichael Große                    return trim($term, '*') . '*';
215*df977249SMichael Große                }, $this->parsedQuery['not']),
216b005809cSMichael Große            ],
217b005809cSMichael Große            'ends' => [
218b005809cSMichael Große                'label' => $lang['search_ends_with'],
219b005809cSMichael Große                'and' => array_map(function ($term) {
220b005809cSMichael Große                    return '*' . trim($term, '*');
221*df977249SMichael Große                }, $this->parsedQuery['and']),
222*df977249SMichael Große                'not' => array_map(function ($term) {
223*df977249SMichael Große                    return '*' . trim($term, '*');
224*df977249SMichael Große                }, $this->parsedQuery['not']),
225b005809cSMichael Große            ],
226b005809cSMichael Große            'contains' => [
227b005809cSMichael Große                'label' => $lang['search_contains'],
228b005809cSMichael Große                'and' => array_map(function ($term) {
229b005809cSMichael Große                    return '*' . trim($term, '*') . '*';
230*df977249SMichael Große                }, $this->parsedQuery['and']),
231*df977249SMichael Große                'not' => array_map(function ($term) {
232*df977249SMichael Große                    return '*' . trim($term, '*') . '*';
233*df977249SMichael Große                }, $this->parsedQuery['not']),
234b005809cSMichael Große            ]
235b005809cSMichael Große        ];
236b005809cSMichael Große
237b005809cSMichael Große        // detect current
238b005809cSMichael Große        $activeOption = 'exact';
239b005809cSMichael Große        foreach ($options as $key => $option) {
240b005809cSMichael Große            if ($this->parsedQuery['and'] === $option['and']) {
241b005809cSMichael Große                $activeOption = $key;
242b005809cSMichael Große            }
243b005809cSMichael Große        }
244b005809cSMichael Große
245b005809cSMichael Große        $searchForm->addTagOpen('div')->addClass('search-tool js-search-tool');
246b005809cSMichael Große        // render current
247b005809cSMichael Große        $currentWrapper = $searchForm->addTagOpen('div')->addClass('search-tool__current js-current');
248b005809cSMichael Große        if ($activeOption !== 'exact') {
249b005809cSMichael Große            $currentWrapper->addClass('search-tool__current--changed');
250b005809cSMichael Große        }
251b005809cSMichael Große        $searchForm->addHTML($options[$activeOption]['label']);
252b005809cSMichael Große        $searchForm->addTagClose('div');
253b005809cSMichael Große
254b005809cSMichael Große        // render options list
255b005809cSMichael Große        $searchForm->addTagOpen('ul')->addClass('search-tool__options-list js-optionsList');
256b005809cSMichael Große
257b005809cSMichael Große        foreach ($options as $key => $option) {
258b005809cSMichael Große            $listItem = $searchForm->addTagOpen('li')->addClass('search-tool__options-list-item');
259b005809cSMichael Große
260b005809cSMichael Große            if ($key === $activeOption) {
261b005809cSMichael Große                $listItem->addClass('search-tool__options-list-item--active');
262b005809cSMichael Große                $searchForm->addHTML($option['label']);
263b005809cSMichael Große            } else {
26418856c5dSMichael Große                $this->searchState->addSearchLinkFragment(
2654d0cb6e1SMichael Große                    $searchForm,
266b005809cSMichael Große                    $option['label'],
267*df977249SMichael Große                    $option['and'],
268*df977249SMichael Große                    $option['not']
2694d0cb6e1SMichael Große                );
270b005809cSMichael Große            }
271b005809cSMichael Große            $searchForm->addTagClose('li');
272b005809cSMichael Große        }
273b005809cSMichael Große        $searchForm->addTagClose('ul');
2744d0cb6e1SMichael Große
2754d0cb6e1SMichael Große        $searchForm->addTagClose('div');
276b005809cSMichael Große
277b005809cSMichael Große        // render options list
2784d0cb6e1SMichael Große    }
2794d0cb6e1SMichael Große
280bb8ef867SMichael Große    /**
281bb8ef867SMichael Große     * Add the elements for the namespace selector
282bb8ef867SMichael Große     *
283bb8ef867SMichael Große     * @param Form $searchForm
284bb8ef867SMichael Große     */
28518856c5dSMichael Große    protected function addNamespaceSelector(Form $searchForm)
286bb8ef867SMichael Große    {
287*df977249SMichael Große        if (!$this->isNamespaceAssistanceAvailable($this->parsedQuery)) {
288*df977249SMichael Große            return;
289*df977249SMichael Große        }
290*df977249SMichael Große
291b005809cSMichael Große        global $lang;
292b005809cSMichael Große
29318856c5dSMichael Große        $baseNS = empty($this->parsedQuery['ns']) ? '' : $this->parsedQuery['ns'][0];
294bbc1da2eSMichael Große        $extraNS = $this->getAdditionalNamespacesFromResults($baseNS);
2954d0cb6e1SMichael Große
296b005809cSMichael Große        $searchForm->addTagOpen('div')->addClass('search-tool js-search-tool');
297b005809cSMichael Große        // render current
298b005809cSMichael Große        $currentWrapper = $searchForm->addTagOpen('div')->addClass('search-tool__current js-current');
299bbc1da2eSMichael Große        if ($baseNS) {
300b005809cSMichael Große            $currentWrapper->addClass('search-tool__current--changed');
301b005809cSMichael Große            $searchForm->addHTML('@' . $baseNS);
302b005809cSMichael Große        } else {
303b005809cSMichael Große            $searchForm->addHTML($lang['search_any_ns']);
304b005809cSMichael Große        }
305b005809cSMichael Große        $searchForm->addTagClose('div');
306b005809cSMichael Große
307b005809cSMichael Große        // render options list
308b005809cSMichael Große        $searchForm->addTagOpen('ul')->addClass('search-tool__options-list js-optionsList');
309b005809cSMichael Große
310b005809cSMichael Große        $listItem = $searchForm->addTagOpen('li')->addClass('search-tool__options-list-item');
311b005809cSMichael Große        if ($baseNS) {
312b005809cSMichael Große            $listItem->addClass('search-tool__options-list-item--active');
31318856c5dSMichael Große            $this->searchState->addSeachLinkNS(
3144d0cb6e1SMichael Große                $searchForm,
315b005809cSMichael Große                $lang['search_any_ns'],
31618856c5dSMichael Große                ''
3174d0cb6e1SMichael Große            );
318b005809cSMichael Große        } else {
319b005809cSMichael Große            $searchForm->addHTML($lang['search_any_ns']);
320bb8ef867SMichael Große        }
321b005809cSMichael Große        $searchForm->addTagClose('li');
322bb8ef867SMichael Große
32318856c5dSMichael Große        foreach ($extraNS as $ns => $count) {
324b005809cSMichael Große            $listItem = $searchForm->addTagOpen('li')->addClass('search-tool__options-list-item');
32518856c5dSMichael Große            $label = $ns . ($count ? " ($count)" : '');
3264d0cb6e1SMichael Große
327b005809cSMichael Große            if ($ns === $baseNS) {
328b005809cSMichael Große                $listItem->addClass('search-tool__options-list-item--active');
329b005809cSMichael Große                $searchForm->addHTML($label);
330b005809cSMichael Große            } else {
331b005809cSMichael Große                $this->searchState->addSeachLinkNS(
332b005809cSMichael Große                    $searchForm,
333b005809cSMichael Große                    $label,
334b005809cSMichael Große                    $ns
335b005809cSMichael Große                );
336bb8ef867SMichael Große            }
337b005809cSMichael Große            $searchForm->addTagClose('li');
338bb8ef867SMichael Große        }
339b005809cSMichael Große        $searchForm->addTagClose('ul');
340bb8ef867SMichael Große
341bb8ef867SMichael Große        $searchForm->addTagClose('div');
342b005809cSMichael Große
343bb8ef867SMichael Große    }
344bb8ef867SMichael Große
345bb8ef867SMichael Große    /**
346bb8ef867SMichael Große     * Parse the full text results for their top namespaces below the given base namespace
347bb8ef867SMichael Große     *
348bb8ef867SMichael Große     * @param string $baseNS the namespace within which was searched, empty string for root namespace
349bb8ef867SMichael Große     *
350bb8ef867SMichael Große     * @return array an associative array with namespace => #number of found pages, sorted descending
351bb8ef867SMichael Große     */
352bb8ef867SMichael Große    protected function getAdditionalNamespacesFromResults($baseNS)
353bb8ef867SMichael Große    {
354bb8ef867SMichael Große        $namespaces = [];
355bb8ef867SMichael Große        $baseNSLength = strlen($baseNS);
356bb8ef867SMichael Große        foreach ($this->fullTextResults as $page => $numberOfHits) {
357bb8ef867SMichael Große            $namespace = getNS($page);
358bb8ef867SMichael Große            if (!$namespace) {
359bb8ef867SMichael Große                continue;
360bb8ef867SMichael Große            }
361bb8ef867SMichael Große            if ($namespace === $baseNS) {
362bb8ef867SMichael Große                continue;
363bb8ef867SMichael Große            }
364bb8ef867SMichael Große            $firstColon = strpos((string)$namespace, ':', $baseNSLength + 1) ?: strlen($namespace);
365bb8ef867SMichael Große            $subtopNS = substr($namespace, 0, $firstColon);
366bb8ef867SMichael Große            if (empty($namespaces[$subtopNS])) {
367bb8ef867SMichael Große                $namespaces[$subtopNS] = 0;
368bb8ef867SMichael Große            }
369bb8ef867SMichael Große            $namespaces[$subtopNS] += 1;
370bb8ef867SMichael Große        }
371bb8ef867SMichael Große        arsort($namespaces);
372bb8ef867SMichael Große        return $namespaces;
373bb8ef867SMichael Große    }
374bb8ef867SMichael Große
375bb8ef867SMichael Große    /**
376bbc1da2eSMichael Große     * @ToDo: custom date input
377bbc1da2eSMichael Große     *
378bbc1da2eSMichael Große     * @param Form $searchForm
379bbc1da2eSMichael Große     */
380b005809cSMichael Große    protected function addDateSelector(Form $searchForm)
381b005809cSMichael Große    {
382b005809cSMichael Große        global $INPUT, $lang;
383bbc1da2eSMichael Große
384b005809cSMichael Große        $options = [
385b005809cSMichael Große            'any' => [
386b005809cSMichael Große                'before' => false,
387b005809cSMichael Große                'after' => false,
388b005809cSMichael Große                'label' => $lang['search_any_time'],
389b005809cSMichael Große            ],
390b005809cSMichael Große            'week' => [
391b005809cSMichael Große                'before' => false,
392b005809cSMichael Große                'after' => '1 week ago',
393b005809cSMichael Große                'label' => $lang['search_past_7_days'],
394b005809cSMichael Große            ],
395b005809cSMichael Große            'month' => [
396b005809cSMichael Große                'before' => false,
397b005809cSMichael Große                'after' => '1 month ago',
398b005809cSMichael Große                'label' => $lang['search_past_month'],
399b005809cSMichael Große            ],
400b005809cSMichael Große            'year' => [
401b005809cSMichael Große                'before' => false,
402b005809cSMichael Große                'after' => '1 year ago',
403b005809cSMichael Große                'label' => $lang['search_past_year'],
404b005809cSMichael Große            ],
405b005809cSMichael Große        ];
406b005809cSMichael Große        $activeOption = 'any';
407b005809cSMichael Große        foreach ($options as $key => $option) {
408b005809cSMichael Große            if ($INPUT->str('after') === $option['after']) {
409b005809cSMichael Große                $activeOption = $key;
410b005809cSMichael Große                break;
411b005809cSMichael Große            }
412b005809cSMichael Große        }
413b005809cSMichael Große
414b005809cSMichael Große        $searchForm->addTagOpen('div')->addClass('search-tool js-search-tool');
415b005809cSMichael Große        // render current
416b005809cSMichael Große        $currentWrapper = $searchForm->addTagOpen('div')->addClass('search-tool__current js-current');
417bbc1da2eSMichael Große        if ($INPUT->has('before') || $INPUT->has('after')) {
418b005809cSMichael Große            $currentWrapper->addClass('search-tool__current--changed');
419bbc1da2eSMichael Große        }
420b005809cSMichael Große        $searchForm->addHTML($options[$activeOption]['label']);
421b005809cSMichael Große        $searchForm->addTagClose('div');
422bbc1da2eSMichael Große
423b005809cSMichael Große        // render options list
424b005809cSMichael Große        $searchForm->addTagOpen('ul')->addClass('search-tool__options-list js-optionsList');
425b005809cSMichael Große
426b005809cSMichael Große        foreach ($options as $key => $option) {
427b005809cSMichael Große            $listItem = $searchForm->addTagOpen('li')->addClass('search-tool__options-list-item');
428b005809cSMichael Große
429b005809cSMichael Große            if ($key === $activeOption) {
430b005809cSMichael Große                $listItem->addClass('search-tool__options-list-item--active');
431b005809cSMichael Große                $searchForm->addHTML($option['label']);
432bbc1da2eSMichael Große            } else {
43318856c5dSMichael Große                $this->searchState->addSearchLinkTime(
434bbc1da2eSMichael Große                    $searchForm,
435b005809cSMichael Große                    $option['label'],
436b005809cSMichael Große                    $option['after'],
437b005809cSMichael Große                    $option['before']
438bbc1da2eSMichael Große                );
439bbc1da2eSMichael Große            }
440b005809cSMichael Große            $searchForm->addTagClose('li');
441bbc1da2eSMichael Große        }
442b005809cSMichael Große        $searchForm->addTagClose('ul');
443bbc1da2eSMichael Große
444bbc1da2eSMichael Große        $searchForm->addTagClose('div');
445bbc1da2eSMichael Große    }
446bbc1da2eSMichael Große
447bbc1da2eSMichael Große
448bbc1da2eSMichael Große    /**
44921fcef82SMichael Große     * Build the intro text for the search page
45021fcef82SMichael Große     *
45121fcef82SMichael Große     * @param string $query the search query
45221fcef82SMichael Große     *
45321fcef82SMichael Große     * @return string
45421fcef82SMichael Große     */
45521fcef82SMichael Große    protected function getSearchIntroHTML($query)
45621fcef82SMichael Große    {
45721fcef82SMichael Große        global $ID, $lang;
45821fcef82SMichael Große
45921fcef82SMichael Große        $intro = p_locale_xhtml('searchpage');
46021fcef82SMichael Große        // allow use of placeholder in search intro
46121fcef82SMichael Große        $pagecreateinfo = (auth_quickaclcheck($ID) >= AUTH_CREATE) ? $lang['searchcreatepage'] : '';
46221fcef82SMichael Große        $intro = str_replace(
46321fcef82SMichael Große            array('@QUERY@', '@SEARCH@', '@CREATEPAGEINFO@'),
46421fcef82SMichael Große            array(hsc(rawurlencode($query)), hsc($query), $pagecreateinfo),
46521fcef82SMichael Große            $intro
46621fcef82SMichael Große        );
46721fcef82SMichael Große        return $intro;
46821fcef82SMichael Große    }
46921fcef82SMichael Große
47021fcef82SMichael Große    /**
47121fcef82SMichael Große     * Build HTML for a list of pages with matching pagenames
47221fcef82SMichael Große     *
47321fcef82SMichael Große     * @param array $data search results
47421fcef82SMichael Große     *
47521fcef82SMichael Große     * @return string
47621fcef82SMichael Große     */
47721fcef82SMichael Große    protected function getPageLookupHTML($data)
47821fcef82SMichael Große    {
47921fcef82SMichael Große        if (empty($data)) {
48021fcef82SMichael Große            return '';
48121fcef82SMichael Große        }
48221fcef82SMichael Große
48321fcef82SMichael Große        global $lang;
48421fcef82SMichael Große
48521fcef82SMichael Große        $html = '<div class="search_quickresult">';
48621fcef82SMichael Große        $html .= '<h3>' . $lang['quickhits'] . ':</h3>';
48721fcef82SMichael Große        $html .= '<ul class="search_quickhits">';
48821fcef82SMichael Große        foreach ($data as $id => $title) {
4894eab6f7cSMichael Große            $link = html_wikilink(':' . $id);
4904eab6f7cSMichael Große            $eventData = [
4914eab6f7cSMichael Große                'listItemContent' => [$link],
4924eab6f7cSMichael Große                'page' => $id,
4934eab6f7cSMichael Große            ];
4944eab6f7cSMichael Große            trigger_event('SEARCH_RESULT_PAGELOOKUP', $eventData);
4954eab6f7cSMichael Große            $html .= '<li>' . implode('', $eventData['listItemContent']) . '</li>';
49621fcef82SMichael Große        }
49721fcef82SMichael Große        $html .= '</ul> ';
49821fcef82SMichael Große        //clear float (see http://www.complexspiral.com/publications/containing-floats/)
49921fcef82SMichael Große        $html .= '<div class="clearer"></div>';
50021fcef82SMichael Große        $html .= '</div>';
50121fcef82SMichael Große
50221fcef82SMichael Große        return $html;
50321fcef82SMichael Große    }
50421fcef82SMichael Große
50521fcef82SMichael Große    /**
50621fcef82SMichael Große     * Build HTML for fulltext search results or "no results" message
50721fcef82SMichael Große     *
50821fcef82SMichael Große     * @param array $data      the results of the fulltext search
50921fcef82SMichael Große     * @param array $highlight the terms to be highlighted in the results
51021fcef82SMichael Große     *
51121fcef82SMichael Große     * @return string
51221fcef82SMichael Große     */
51321fcef82SMichael Große    protected function getFulltextResultsHTML($data, $highlight)
51421fcef82SMichael Große    {
51521fcef82SMichael Große        global $lang;
51621fcef82SMichael Große
51721fcef82SMichael Große        if (empty($data)) {
51821fcef82SMichael Große            return '<div class="nothing">' . $lang['nothingfound'] . '</div>';
51921fcef82SMichael Große        }
52021fcef82SMichael Große
52121fcef82SMichael Große        $html = '';
52221fcef82SMichael Große        $html .= '<dl class="search_results">';
52321fcef82SMichael Große        $num = 1;
5244c924eb8SMichael Große
52521fcef82SMichael Große        foreach ($data as $id => $cnt) {
5264eab6f7cSMichael Große            $resultLink = html_wikilink(':' . $id, null, $highlight);
5274c924eb8SMichael Große
5284c924eb8SMichael Große            $resultHeader = [$resultLink];
5294c924eb8SMichael Große
5304eab6f7cSMichael Große
5314c924eb8SMichael Große            $restrictQueryToNSLink = $this->restrictQueryToNSLink(getNS($id));
5324c924eb8SMichael Große            if ($restrictQueryToNSLink) {
5334c924eb8SMichael Große                $resultHeader[] = $restrictQueryToNSLink;
5344c924eb8SMichael Große            }
5354c924eb8SMichael Große
5369a75abfbSMichael Große            $snippet = '';
5379a75abfbSMichael Große            $lastMod = '';
5389a75abfbSMichael Große            $mtime = filemtime(wikiFN($id));
5399a75abfbSMichael Große            if ($cnt !== 0) {
5409a75abfbSMichael Große                $resultHeader[] = $cnt . ' ' . $lang['hits'];
5419a75abfbSMichael Große                if ($num < FT_SNIPPET_NUMBER) { // create snippets for the first number of matches only
5429a75abfbSMichael Große                    $snippet = '<dd>' . ft_snippet($id, $highlight) . '</dd>';
5439a75abfbSMichael Große                    $lastMod = '<span class="search_results__lastmod">' . $lang['lastmod'] . ' ';
5449a75abfbSMichael Große                    $lastMod .= '<time datetime="' . date_iso8601($mtime) . '">' . dformat($mtime) . '</time>';
5459a75abfbSMichael Große                    $lastMod .= '</span>';
5469a75abfbSMichael Große                }
5479a75abfbSMichael Große                $num++;
5489a75abfbSMichael Große            }
5499a75abfbSMichael Große
5509a75abfbSMichael Große            $metaLine = '<div class="search_results__metaLine">';
5519a75abfbSMichael Große            $metaLine .= $lastMod;
5529a75abfbSMichael Große            $metaLine .= '</div>';
5539a75abfbSMichael Große
5549a75abfbSMichael Große
5554eab6f7cSMichael Große            $eventData = [
5564c924eb8SMichael Große                'resultHeader' => $resultHeader,
5579a75abfbSMichael Große                'resultBody' => [$metaLine, $snippet],
5584eab6f7cSMichael Große                'page' => $id,
5594eab6f7cSMichael Große            ];
5604eab6f7cSMichael Große            trigger_event('SEARCH_RESULT_FULLPAGE', $eventData);
5614eab6f7cSMichael Große            $html .= '<div class="search_fullpage_result">';
5624eab6f7cSMichael Große            $html .= '<dt>' . implode(' ', $eventData['resultHeader']) . '</dt>';
5634eab6f7cSMichael Große            $html .= implode('', $eventData['resultBody']);
5644eab6f7cSMichael Große            $html .= '</div>';
56521fcef82SMichael Große        }
56621fcef82SMichael Große        $html .= '</dl>';
56721fcef82SMichael Große
56821fcef82SMichael Große        return $html;
56921fcef82SMichael Große    }
5704c924eb8SMichael Große
5714c924eb8SMichael Große    /**
5724c924eb8SMichael Große     * create a link to restrict the current query to a namespace
5734c924eb8SMichael Große     *
5744c924eb8SMichael Große     * @param bool|string $ns the namespace to which to restrict the query
5754c924eb8SMichael Große     *
5764c924eb8SMichael Große     * @return bool|string
5774c924eb8SMichael Große     */
5784c924eb8SMichael Große    protected function restrictQueryToNSLink($ns)
5794c924eb8SMichael Große    {
5804c924eb8SMichael Große        if (!$ns) {
5814c924eb8SMichael Große            return false;
5824c924eb8SMichael Große        }
583*df977249SMichael Große        if (!$this->isNamespaceAssistanceAvailable($this->parsedQuery)) {
5844c924eb8SMichael Große            return false;
5854c924eb8SMichael Große        }
5864c924eb8SMichael Große        if (!empty($this->parsedQuery['ns']) && $this->parsedQuery['ns'][0] === $ns) {
5874c924eb8SMichael Große            return false;
5884c924eb8SMichael Große        }
5894c924eb8SMichael Große        $name = '@' . $ns;
590bbc1da2eSMichael Große        $tmpForm = new Form();
59118856c5dSMichael Große        $this->searchState->addSeachLinkNS($tmpForm, $name, $ns);
592bbc1da2eSMichael Große        return $tmpForm->toHTML();
5934c924eb8SMichael Große    }
59421fcef82SMichael Große}
595