xref: /dokuwiki/inc/Ui/Search.php (revision 55dc87835992032369c397150874230e66064d95)
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);
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
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
1101265b193SMichael Große        if ($INPUT->str('srt') === 'mtime') {
111b005809cSMichael Große            $activeOption = 'mtime';
112b005809cSMichael Große        }
113b005809cSMichael Große
1144bdf82b5SAndreas Gohr        $searchForm->addTagOpen('div')->addClass('toggle');
115b005809cSMichael Große        // render current
1164bdf82b5SAndreas Gohr        $currentWrapper = $searchForm->addTagOpen('div')->addClass('current');
117b005809cSMichael Große        if ($activeOption !== 'hits') {
1184bdf82b5SAndreas Gohr            $currentWrapper->addClass('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
1244bdf82b5SAndreas Gohr        $searchForm->addTagOpen('ul');
125b005809cSMichael Große
126b005809cSMichael Große        foreach ($options as $key => $option) {
1274bdf82b5SAndreas Gohr            $listItem = $searchForm->addTagOpen('li');
128b005809cSMichael Große
129b005809cSMichael Große            if ($key === $activeOption) {
1304bdf82b5SAndreas Gohr                $listItem->addClass('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
147df977249SMichael Große    protected function isNamespaceAssistanceAvailable(array $parsedQuery) {
148df977249SMichael Große        if (preg_match('/[\(\)\|]/', $parsedQuery['query']) === 1) {
149bb8ef867SMichael Große            return false;
150bb8ef867SMichael Große        }
151df977249SMichael Große
152df977249SMichael Große        return true;
153df977249SMichael Große    }
154df977249SMichael Große
155df977249SMichael Große    protected function isFragmentAssistanceAvailable(array $parsedQuery) {
156df977249SMichael 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    {
1744bdf82b5SAndreas Gohr        // FIXME localize
175bb8ef867SMichael Große        $searchForm->addButton('toggleAssistant', 'toggle search assistant')
176bb8ef867SMichael Große            ->attr('type', 'button')
1774bdf82b5SAndreas Gohr            ->addClass('toggleAssistant');
178bb8ef867SMichael Große
179bb8ef867SMichael Große        $searchForm->addTagOpen('div')
1804bdf82b5SAndreas Gohr            ->addClass('advancedOptions')
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    {
193df977249SMichael Große        if (!$this->isFragmentAssistanceAvailable($this->parsedQuery)) {
194df977249SMichael Große            return;
195df977249SMichael 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']),
204df977249SMichael Große                'not' => array_map(function ($term) {
205df977249SMichael Große                    return trim($term, '*');
206df977249SMichael 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, '*') . '*';
212df977249SMichael Große                }, $this->parsedQuery['and']),
213df977249SMichael Große                'not' => array_map(function ($term) {
214df977249SMichael Große                    return trim($term, '*') . '*';
215df977249SMichael 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, '*');
221df977249SMichael Große                }, $this->parsedQuery['and']),
222df977249SMichael Große                'not' => array_map(function ($term) {
223df977249SMichael Große                    return '*' . trim($term, '*');
224df977249SMichael 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, '*') . '*';
230df977249SMichael Große                }, $this->parsedQuery['and']),
231df977249SMichael Große                'not' => array_map(function ($term) {
232df977249SMichael Große                    return '*' . trim($term, '*') . '*';
233df977249SMichael Große                }, $this->parsedQuery['not']),
234b005809cSMichael Große            ]
235b005809cSMichael Große        ];
236b005809cSMichael Große
237b005809cSMichael Große        // detect current
238c6b5b74aSMichael Große        $activeOption = 'custom';
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        }
244c6b5b74aSMichael Große        if ($activeOption === 'custom') {
245c6b5b74aSMichael Große            $options = array_merge(['custom' => [
246c6b5b74aSMichael Große                'label' => $lang['search_custom_match'],
247c6b5b74aSMichael Große            ]], $options);
248c6b5b74aSMichael Große        }
249b005809cSMichael Große
2504bdf82b5SAndreas Gohr        $searchForm->addTagOpen('div')->addClass('toggle');
251b005809cSMichael Große        // render current
2524bdf82b5SAndreas Gohr        $currentWrapper = $searchForm->addTagOpen('div')->addClass('current');
253b005809cSMichael Große        if ($activeOption !== 'exact') {
2544bdf82b5SAndreas Gohr            $currentWrapper->addClass('changed');
255b005809cSMichael Große        }
256b005809cSMichael Große        $searchForm->addHTML($options[$activeOption]['label']);
257b005809cSMichael Große        $searchForm->addTagClose('div');
258b005809cSMichael Große
259b005809cSMichael Große        // render options list
2604bdf82b5SAndreas Gohr        $searchForm->addTagOpen('ul');
261b005809cSMichael Große
262b005809cSMichael Große        foreach ($options as $key => $option) {
2634bdf82b5SAndreas Gohr            $listItem = $searchForm->addTagOpen('li');
264b005809cSMichael Große
265b005809cSMichael Große            if ($key === $activeOption) {
2664bdf82b5SAndreas Gohr                $listItem->addClass('active');
267b005809cSMichael Große                $searchForm->addHTML($option['label']);
268b005809cSMichael Große            } else {
26918856c5dSMichael Große                $this->searchState->addSearchLinkFragment(
2704d0cb6e1SMichael Große                    $searchForm,
271b005809cSMichael Große                    $option['label'],
272df977249SMichael Große                    $option['and'],
273df977249SMichael Große                    $option['not']
2744d0cb6e1SMichael Große                );
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
3014bdf82b5SAndreas Gohr        $searchForm->addTagOpen('div')->addClass('toggle');
302b005809cSMichael Große        // render current
3034bdf82b5SAndreas Gohr        $currentWrapper = $searchForm->addTagOpen('div')->addClass('current');
304bbc1da2eSMichael Große        if ($baseNS) {
3054bdf82b5SAndreas Gohr            $currentWrapper->addClass('changed');
306b005809cSMichael Große            $searchForm->addHTML('@' . $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
3134bdf82b5SAndreas Gohr        $searchForm->addTagOpen('ul');
314b005809cSMichael Große
3154bdf82b5SAndreas Gohr        $listItem = $searchForm->addTagOpen('li');
316b005809cSMichael Große        if ($baseNS) {
3174bdf82b5SAndreas Gohr            $listItem->addClass('active');
31818856c5dSMichael Große            $this->searchState->addSeachLinkNS(
3194d0cb6e1SMichael Große                $searchForm,
320b005809cSMichael Große                $lang['search_any_ns'],
32118856c5dSMichael Große                ''
3224d0cb6e1SMichael Große            );
323b005809cSMichael Große        } else {
324b005809cSMichael Große            $searchForm->addHTML($lang['search_any_ns']);
325bb8ef867SMichael Große        }
326b005809cSMichael Große        $searchForm->addTagClose('li');
327bb8ef867SMichael Große
32818856c5dSMichael Große        foreach ($extraNS as $ns => $count) {
3294bdf82b5SAndreas Gohr            $listItem = $searchForm->addTagOpen('li');
33018856c5dSMichael Große            $label = $ns . ($count ? " ($count)" : '');
3314d0cb6e1SMichael Große
332b005809cSMichael Große            if ($ns === $baseNS) {
3334bdf82b5SAndreas Gohr                $listItem->addClass('active');
334b005809cSMichael Große                $searchForm->addHTML($label);
335b005809cSMichael Große            } else {
336b005809cSMichael Große                $this->searchState->addSeachLinkNS(
337b005809cSMichael Große                    $searchForm,
338b005809cSMichael Große                    $label,
339b005809cSMichael Große                    $ns
340b005809cSMichael Große                );
341bb8ef867SMichael Große            }
342b005809cSMichael Große            $searchForm->addTagClose('li');
343bb8ef867SMichael Große        }
344b005809cSMichael Große        $searchForm->addTagClose('ul');
345bb8ef867SMichael Große
346bb8ef867SMichael Große        $searchForm->addTagClose('div');
347b005809cSMichael Große
348bb8ef867SMichael Große    }
349bb8ef867SMichael Große
350bb8ef867SMichael Große    /**
351bb8ef867SMichael Große     * Parse the full text results for their top namespaces below the given base namespace
352bb8ef867SMichael Große     *
353bb8ef867SMichael Große     * @param string $baseNS the namespace within which was searched, empty string for root namespace
354bb8ef867SMichael Große     *
355bb8ef867SMichael Große     * @return array an associative array with namespace => #number of found pages, sorted descending
356bb8ef867SMichael Große     */
357bb8ef867SMichael Große    protected function getAdditionalNamespacesFromResults($baseNS)
358bb8ef867SMichael Große    {
359bb8ef867SMichael Große        $namespaces = [];
360bb8ef867SMichael Große        $baseNSLength = strlen($baseNS);
361bb8ef867SMichael Große        foreach ($this->fullTextResults as $page => $numberOfHits) {
362bb8ef867SMichael Große            $namespace = getNS($page);
363bb8ef867SMichael Große            if (!$namespace) {
364bb8ef867SMichael Große                continue;
365bb8ef867SMichael Große            }
366bb8ef867SMichael Große            if ($namespace === $baseNS) {
367bb8ef867SMichael Große                continue;
368bb8ef867SMichael Große            }
369bb8ef867SMichael Große            $firstColon = strpos((string)$namespace, ':', $baseNSLength + 1) ?: strlen($namespace);
370bb8ef867SMichael Große            $subtopNS = substr($namespace, 0, $firstColon);
371bb8ef867SMichael Große            if (empty($namespaces[$subtopNS])) {
372bb8ef867SMichael Große                $namespaces[$subtopNS] = 0;
373bb8ef867SMichael Große            }
374bb8ef867SMichael Große            $namespaces[$subtopNS] += 1;
375bb8ef867SMichael Große        }
376*55dc8783SMichael Große        ksort($namespaces);
377bb8ef867SMichael Große        arsort($namespaces);
378bb8ef867SMichael Große        return $namespaces;
379bb8ef867SMichael Große    }
380bb8ef867SMichael Große
381bb8ef867SMichael Große    /**
382bbc1da2eSMichael Große     * @ToDo: custom date input
383bbc1da2eSMichael Große     *
384bbc1da2eSMichael Große     * @param Form $searchForm
385bbc1da2eSMichael Große     */
386b005809cSMichael Große    protected function addDateSelector(Form $searchForm)
387b005809cSMichael Große    {
388b005809cSMichael Große        global $INPUT, $lang;
389bbc1da2eSMichael Große
390b005809cSMichael Große        $options = [
391b005809cSMichael Große            'any' => [
392b005809cSMichael Große                'before' => false,
393b005809cSMichael Große                'after' => false,
394b005809cSMichael Große                'label' => $lang['search_any_time'],
395b005809cSMichael Große            ],
396b005809cSMichael Große            'week' => [
397b005809cSMichael Große                'before' => false,
398b005809cSMichael Große                'after' => '1 week ago',
399b005809cSMichael Große                'label' => $lang['search_past_7_days'],
400b005809cSMichael Große            ],
401b005809cSMichael Große            'month' => [
402b005809cSMichael Große                'before' => false,
403b005809cSMichael Große                'after' => '1 month ago',
404b005809cSMichael Große                'label' => $lang['search_past_month'],
405b005809cSMichael Große            ],
406b005809cSMichael Große            'year' => [
407b005809cSMichael Große                'before' => false,
408b005809cSMichael Große                'after' => '1 year ago',
409b005809cSMichael Große                'label' => $lang['search_past_year'],
410b005809cSMichael Große            ],
411b005809cSMichael Große        ];
412b005809cSMichael Große        $activeOption = 'any';
413b005809cSMichael Große        foreach ($options as $key => $option) {
4141265b193SMichael Große            if ($INPUT->str('dta') === $option['after']) {
415b005809cSMichael Große                $activeOption = $key;
416b005809cSMichael Große                break;
417b005809cSMichael Große            }
418b005809cSMichael Große        }
419b005809cSMichael Große
4204bdf82b5SAndreas Gohr        $searchForm->addTagOpen('div')->addClass('toggle');
421b005809cSMichael Große        // render current
4224bdf82b5SAndreas Gohr        $currentWrapper = $searchForm->addTagOpen('div')->addClass('current');
4231265b193SMichael Große        if ($INPUT->has('dtb') || $INPUT->has('dta')) {
4244bdf82b5SAndreas Gohr            $currentWrapper->addClass('changed');
425bbc1da2eSMichael Große        }
426b005809cSMichael Große        $searchForm->addHTML($options[$activeOption]['label']);
427b005809cSMichael Große        $searchForm->addTagClose('div');
428bbc1da2eSMichael Große
429b005809cSMichael Große        // render options list
4304bdf82b5SAndreas Gohr        $searchForm->addTagOpen('ul');
431b005809cSMichael Große
432b005809cSMichael Große        foreach ($options as $key => $option) {
4334bdf82b5SAndreas Gohr            $listItem = $searchForm->addTagOpen('li');
434b005809cSMichael Große
435b005809cSMichael Große            if ($key === $activeOption) {
4364bdf82b5SAndreas Gohr                $listItem->addClass('active');
437b005809cSMichael Große                $searchForm->addHTML($option['label']);
438bbc1da2eSMichael Große            } else {
43918856c5dSMichael Große                $this->searchState->addSearchLinkTime(
440bbc1da2eSMichael Große                    $searchForm,
441b005809cSMichael Große                    $option['label'],
442b005809cSMichael Große                    $option['after'],
443b005809cSMichael Große                    $option['before']
444bbc1da2eSMichael Große                );
445bbc1da2eSMichael Große            }
446b005809cSMichael Große            $searchForm->addTagClose('li');
447bbc1da2eSMichael Große        }
448b005809cSMichael Große        $searchForm->addTagClose('ul');
449bbc1da2eSMichael Große
450bbc1da2eSMichael Große        $searchForm->addTagClose('div');
451bbc1da2eSMichael Große    }
452bbc1da2eSMichael Große
453bbc1da2eSMichael Große
454bbc1da2eSMichael Große    /**
45521fcef82SMichael Große     * Build the intro text for the search page
45621fcef82SMichael Große     *
45721fcef82SMichael Große     * @param string $query the search query
45821fcef82SMichael Große     *
45921fcef82SMichael Große     * @return string
46021fcef82SMichael Große     */
46121fcef82SMichael Große    protected function getSearchIntroHTML($query)
46221fcef82SMichael Große    {
46321fcef82SMichael Große        global $ID, $lang;
46421fcef82SMichael Große
46521fcef82SMichael Große        $intro = p_locale_xhtml('searchpage');
46621fcef82SMichael Große        // allow use of placeholder in search intro
46721fcef82SMichael Große        $pagecreateinfo = (auth_quickaclcheck($ID) >= AUTH_CREATE) ? $lang['searchcreatepage'] : '';
46821fcef82SMichael Große        $intro = str_replace(
46921fcef82SMichael Große            array('@QUERY@', '@SEARCH@', '@CREATEPAGEINFO@'),
47021fcef82SMichael Große            array(hsc(rawurlencode($query)), hsc($query), $pagecreateinfo),
47121fcef82SMichael Große            $intro
47221fcef82SMichael Große        );
47321fcef82SMichael Große        return $intro;
47421fcef82SMichael Große    }
47521fcef82SMichael Große
47621fcef82SMichael Große    /**
47721fcef82SMichael Große     * Build HTML for a list of pages with matching pagenames
47821fcef82SMichael Große     *
47921fcef82SMichael Große     * @param array $data search results
48021fcef82SMichael Große     *
48121fcef82SMichael Große     * @return string
48221fcef82SMichael Große     */
48321fcef82SMichael Große    protected function getPageLookupHTML($data)
48421fcef82SMichael Große    {
48521fcef82SMichael Große        if (empty($data)) {
48621fcef82SMichael Große            return '';
48721fcef82SMichael Große        }
48821fcef82SMichael Große
48921fcef82SMichael Große        global $lang;
49021fcef82SMichael Große
49121fcef82SMichael Große        $html = '<div class="search_quickresult">';
49221fcef82SMichael Große        $html .= '<h3>' . $lang['quickhits'] . ':</h3>';
49321fcef82SMichael Große        $html .= '<ul class="search_quickhits">';
49421fcef82SMichael Große        foreach ($data as $id => $title) {
4954eab6f7cSMichael Große            $link = html_wikilink(':' . $id);
4964eab6f7cSMichael Große            $eventData = [
4974eab6f7cSMichael Große                'listItemContent' => [$link],
4984eab6f7cSMichael Große                'page' => $id,
4994eab6f7cSMichael Große            ];
5004eab6f7cSMichael Große            trigger_event('SEARCH_RESULT_PAGELOOKUP', $eventData);
5014eab6f7cSMichael Große            $html .= '<li>' . implode('', $eventData['listItemContent']) . '</li>';
50221fcef82SMichael Große        }
50321fcef82SMichael Große        $html .= '</ul> ';
50421fcef82SMichael Große        //clear float (see http://www.complexspiral.com/publications/containing-floats/)
50521fcef82SMichael Große        $html .= '<div class="clearer"></div>';
50621fcef82SMichael Große        $html .= '</div>';
50721fcef82SMichael Große
50821fcef82SMichael Große        return $html;
50921fcef82SMichael Große    }
51021fcef82SMichael Große
51121fcef82SMichael Große    /**
51221fcef82SMichael Große     * Build HTML for fulltext search results or "no results" message
51321fcef82SMichael Große     *
51421fcef82SMichael Große     * @param array $data      the results of the fulltext search
51521fcef82SMichael Große     * @param array $highlight the terms to be highlighted in the results
51621fcef82SMichael Große     *
51721fcef82SMichael Große     * @return string
51821fcef82SMichael Große     */
51921fcef82SMichael Große    protected function getFulltextResultsHTML($data, $highlight)
52021fcef82SMichael Große    {
52121fcef82SMichael Große        global $lang;
52221fcef82SMichael Große
52321fcef82SMichael Große        if (empty($data)) {
52421fcef82SMichael Große            return '<div class="nothing">' . $lang['nothingfound'] . '</div>';
52521fcef82SMichael Große        }
52621fcef82SMichael Große
52721fcef82SMichael Große        $html = '';
52821fcef82SMichael Große        $html .= '<dl class="search_results">';
52921fcef82SMichael Große        $num = 1;
5304c924eb8SMichael Große
53121fcef82SMichael Große        foreach ($data as $id => $cnt) {
5324eab6f7cSMichael Große            $resultLink = html_wikilink(':' . $id, null, $highlight);
5334c924eb8SMichael Große
5344c924eb8SMichael Große            $resultHeader = [$resultLink];
5354c924eb8SMichael Große
5364eab6f7cSMichael Große
5374c924eb8SMichael Große            $restrictQueryToNSLink = $this->restrictQueryToNSLink(getNS($id));
5384c924eb8SMichael Große            if ($restrictQueryToNSLink) {
5394c924eb8SMichael Große                $resultHeader[] = $restrictQueryToNSLink;
5404c924eb8SMichael Große            }
5414c924eb8SMichael Große
5429a75abfbSMichael Große            $snippet = '';
5439a75abfbSMichael Große            $lastMod = '';
5449a75abfbSMichael Große            $mtime = filemtime(wikiFN($id));
5459a75abfbSMichael Große            if ($cnt !== 0) {
5469a75abfbSMichael Große                $resultHeader[] = $cnt . ' ' . $lang['hits'];
5479a75abfbSMichael Große                if ($num < FT_SNIPPET_NUMBER) { // create snippets for the first number of matches only
5489a75abfbSMichael Große                    $snippet = '<dd>' . ft_snippet($id, $highlight) . '</dd>';
5499a75abfbSMichael Große                    $lastMod = '<span class="search_results__lastmod">' . $lang['lastmod'] . ' ';
5509a75abfbSMichael Große                    $lastMod .= '<time datetime="' . date_iso8601($mtime) . '">' . dformat($mtime) . '</time>';
5519a75abfbSMichael Große                    $lastMod .= '</span>';
5529a75abfbSMichael Große                }
5539a75abfbSMichael Große                $num++;
5549a75abfbSMichael Große            }
5559a75abfbSMichael Große
5569a75abfbSMichael Große            $metaLine = '<div class="search_results__metaLine">';
5579a75abfbSMichael Große            $metaLine .= $lastMod;
5589a75abfbSMichael Große            $metaLine .= '</div>';
5599a75abfbSMichael Große
5609a75abfbSMichael Große
5614eab6f7cSMichael Große            $eventData = [
5624c924eb8SMichael Große                'resultHeader' => $resultHeader,
5639a75abfbSMichael Große                'resultBody' => [$metaLine, $snippet],
5644eab6f7cSMichael Große                'page' => $id,
5654eab6f7cSMichael Große            ];
5664eab6f7cSMichael Große            trigger_event('SEARCH_RESULT_FULLPAGE', $eventData);
5674eab6f7cSMichael Große            $html .= '<div class="search_fullpage_result">';
5684eab6f7cSMichael Große            $html .= '<dt>' . implode(' ', $eventData['resultHeader']) . '</dt>';
5694eab6f7cSMichael Große            $html .= implode('', $eventData['resultBody']);
5704eab6f7cSMichael Große            $html .= '</div>';
57121fcef82SMichael Große        }
57221fcef82SMichael Große        $html .= '</dl>';
57321fcef82SMichael Große
57421fcef82SMichael Große        return $html;
57521fcef82SMichael Große    }
5764c924eb8SMichael Große
5774c924eb8SMichael Große    /**
5784c924eb8SMichael Große     * create a link to restrict the current query to a namespace
5794c924eb8SMichael Große     *
5804c924eb8SMichael Große     * @param bool|string $ns the namespace to which to restrict the query
5814c924eb8SMichael Große     *
5824c924eb8SMichael Große     * @return bool|string
5834c924eb8SMichael Große     */
5844c924eb8SMichael Große    protected function restrictQueryToNSLink($ns)
5854c924eb8SMichael Große    {
5864c924eb8SMichael Große        if (!$ns) {
5874c924eb8SMichael Große            return false;
5884c924eb8SMichael Große        }
589df977249SMichael Große        if (!$this->isNamespaceAssistanceAvailable($this->parsedQuery)) {
5904c924eb8SMichael Große            return false;
5914c924eb8SMichael Große        }
5924c924eb8SMichael Große        if (!empty($this->parsedQuery['ns']) && $this->parsedQuery['ns'][0] === $ns) {
5934c924eb8SMichael Große            return false;
5944c924eb8SMichael Große        }
5954c924eb8SMichael Große        $name = '@' . $ns;
596bbc1da2eSMichael Große        $tmpForm = new Form();
59718856c5dSMichael Große        $this->searchState->addSeachLinkNS($tmpForm, $name, $ns);
598bbc1da2eSMichael Große        return $tmpForm->toHTML();
5994c924eb8SMichael Große    }
60021fcef82SMichael Große}
601