xref: /dokuwiki/inc/Ui/Search.php (revision 4bdf82b5b91895d4ed8395f280e67e09a5a42e71)
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        }
81*4bdf82b5SAndreas 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
114*4bdf82b5SAndreas Gohr        $searchForm->addTagOpen('div')->addClass('toggle');
115b005809cSMichael Große        // render current
116*4bdf82b5SAndreas Gohr        $currentWrapper = $searchForm->addTagOpen('div')->addClass('current');
117b005809cSMichael Große        if ($activeOption !== 'hits') {
118*4bdf82b5SAndreas 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
124*4bdf82b5SAndreas Gohr        $searchForm->addTagOpen('ul');
125b005809cSMichael Große
126b005809cSMichael Große        foreach ($options as $key => $option) {
127*4bdf82b5SAndreas Gohr            $listItem = $searchForm->addTagOpen('li');
128b005809cSMichael Große
129b005809cSMichael Große            if ($key === $activeOption) {
130*4bdf82b5SAndreas 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    {
174*4bdf82b5SAndreas Gohr        // FIXME localize
175bb8ef867SMichael Große        $searchForm->addButton('toggleAssistant', 'toggle search assistant')
176bb8ef867SMichael Große            ->attr('type', 'button')
177*4bdf82b5SAndreas Gohr            ->addClass('toggleAssistant');
178bb8ef867SMichael Große
179bb8ef867SMichael Große        $searchForm->addTagOpen('div')
180*4bdf82b5SAndreas 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
250*4bdf82b5SAndreas Gohr        $searchForm->addTagOpen('div')->addClass('toggle');
251b005809cSMichael Große        // render current
252*4bdf82b5SAndreas Gohr        $currentWrapper = $searchForm->addTagOpen('div')->addClass('current');
253b005809cSMichael Große        if ($activeOption !== 'exact') {
254*4bdf82b5SAndreas 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
260*4bdf82b5SAndreas Gohr        $searchForm->addTagOpen('ul');
261b005809cSMichael Große
262b005809cSMichael Große        foreach ($options as $key => $option) {
263*4bdf82b5SAndreas Gohr            $listItem = $searchForm->addTagOpen('li');
264b005809cSMichael Große
265b005809cSMichael Große            if ($key === $activeOption) {
266*4bdf82b5SAndreas 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
301*4bdf82b5SAndreas Gohr        $searchForm->addTagOpen('div')->addClass('toggle');
302b005809cSMichael Große        // render current
303*4bdf82b5SAndreas Gohr        $currentWrapper = $searchForm->addTagOpen('div')->addClass('current');
304bbc1da2eSMichael Große        if ($baseNS) {
305*4bdf82b5SAndreas 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
313*4bdf82b5SAndreas Gohr        $searchForm->addTagOpen('ul');
314b005809cSMichael Große
315*4bdf82b5SAndreas Gohr        $listItem = $searchForm->addTagOpen('li');
316b005809cSMichael Große        if ($baseNS) {
317*4bdf82b5SAndreas 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) {
329*4bdf82b5SAndreas Gohr            $listItem = $searchForm->addTagOpen('li');
33018856c5dSMichael Große            $label = $ns . ($count ? " ($count)" : '');
3314d0cb6e1SMichael Große
332b005809cSMichael Große            if ($ns === $baseNS) {
333*4bdf82b5SAndreas 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        }
376bb8ef867SMichael Große        arsort($namespaces);
377bb8ef867SMichael Große        return $namespaces;
378bb8ef867SMichael Große    }
379bb8ef867SMichael Große
380bb8ef867SMichael Große    /**
381bbc1da2eSMichael Große     * @ToDo: custom date input
382bbc1da2eSMichael Große     *
383bbc1da2eSMichael Große     * @param Form $searchForm
384bbc1da2eSMichael Große     */
385b005809cSMichael Große    protected function addDateSelector(Form $searchForm)
386b005809cSMichael Große    {
387b005809cSMichael Große        global $INPUT, $lang;
388bbc1da2eSMichael Große
389b005809cSMichael Große        $options = [
390b005809cSMichael Große            'any' => [
391b005809cSMichael Große                'before' => false,
392b005809cSMichael Große                'after' => false,
393b005809cSMichael Große                'label' => $lang['search_any_time'],
394b005809cSMichael Große            ],
395b005809cSMichael Große            'week' => [
396b005809cSMichael Große                'before' => false,
397b005809cSMichael Große                'after' => '1 week ago',
398b005809cSMichael Große                'label' => $lang['search_past_7_days'],
399b005809cSMichael Große            ],
400b005809cSMichael Große            'month' => [
401b005809cSMichael Große                'before' => false,
402b005809cSMichael Große                'after' => '1 month ago',
403b005809cSMichael Große                'label' => $lang['search_past_month'],
404b005809cSMichael Große            ],
405b005809cSMichael Große            'year' => [
406b005809cSMichael Große                'before' => false,
407b005809cSMichael Große                'after' => '1 year ago',
408b005809cSMichael Große                'label' => $lang['search_past_year'],
409b005809cSMichael Große            ],
410b005809cSMichael Große        ];
411b005809cSMichael Große        $activeOption = 'any';
412b005809cSMichael Große        foreach ($options as $key => $option) {
4131265b193SMichael Große            if ($INPUT->str('dta') === $option['after']) {
414b005809cSMichael Große                $activeOption = $key;
415b005809cSMichael Große                break;
416b005809cSMichael Große            }
417b005809cSMichael Große        }
418b005809cSMichael Große
419*4bdf82b5SAndreas Gohr        $searchForm->addTagOpen('div')->addClass('toggle');
420b005809cSMichael Große        // render current
421*4bdf82b5SAndreas Gohr        $currentWrapper = $searchForm->addTagOpen('div')->addClass('current');
4221265b193SMichael Große        if ($INPUT->has('dtb') || $INPUT->has('dta')) {
423*4bdf82b5SAndreas Gohr            $currentWrapper->addClass('changed');
424bbc1da2eSMichael Große        }
425b005809cSMichael Große        $searchForm->addHTML($options[$activeOption]['label']);
426b005809cSMichael Große        $searchForm->addTagClose('div');
427bbc1da2eSMichael Große
428b005809cSMichael Große        // render options list
429*4bdf82b5SAndreas Gohr        $searchForm->addTagOpen('ul');
430b005809cSMichael Große
431b005809cSMichael Große        foreach ($options as $key => $option) {
432*4bdf82b5SAndreas Gohr            $listItem = $searchForm->addTagOpen('li');
433b005809cSMichael Große
434b005809cSMichael Große            if ($key === $activeOption) {
435*4bdf82b5SAndreas Gohr                $listItem->addClass('active');
436b005809cSMichael Große                $searchForm->addHTML($option['label']);
437bbc1da2eSMichael Große            } else {
43818856c5dSMichael Große                $this->searchState->addSearchLinkTime(
439bbc1da2eSMichael Große                    $searchForm,
440b005809cSMichael Große                    $option['label'],
441b005809cSMichael Große                    $option['after'],
442b005809cSMichael Große                    $option['before']
443bbc1da2eSMichael Große                );
444bbc1da2eSMichael Große            }
445b005809cSMichael Große            $searchForm->addTagClose('li');
446bbc1da2eSMichael Große        }
447b005809cSMichael Große        $searchForm->addTagClose('ul');
448bbc1da2eSMichael Große
449bbc1da2eSMichael Große        $searchForm->addTagClose('div');
450bbc1da2eSMichael Große    }
451bbc1da2eSMichael Große
452bbc1da2eSMichael Große
453bbc1da2eSMichael Große    /**
45421fcef82SMichael Große     * Build the intro text for the search page
45521fcef82SMichael Große     *
45621fcef82SMichael Große     * @param string $query the search query
45721fcef82SMichael Große     *
45821fcef82SMichael Große     * @return string
45921fcef82SMichael Große     */
46021fcef82SMichael Große    protected function getSearchIntroHTML($query)
46121fcef82SMichael Große    {
46221fcef82SMichael Große        global $ID, $lang;
46321fcef82SMichael Große
46421fcef82SMichael Große        $intro = p_locale_xhtml('searchpage');
46521fcef82SMichael Große        // allow use of placeholder in search intro
46621fcef82SMichael Große        $pagecreateinfo = (auth_quickaclcheck($ID) >= AUTH_CREATE) ? $lang['searchcreatepage'] : '';
46721fcef82SMichael Große        $intro = str_replace(
46821fcef82SMichael Große            array('@QUERY@', '@SEARCH@', '@CREATEPAGEINFO@'),
46921fcef82SMichael Große            array(hsc(rawurlencode($query)), hsc($query), $pagecreateinfo),
47021fcef82SMichael Große            $intro
47121fcef82SMichael Große        );
47221fcef82SMichael Große        return $intro;
47321fcef82SMichael Große    }
47421fcef82SMichael Große
47521fcef82SMichael Große    /**
47621fcef82SMichael Große     * Build HTML for a list of pages with matching pagenames
47721fcef82SMichael Große     *
47821fcef82SMichael Große     * @param array $data search results
47921fcef82SMichael Große     *
48021fcef82SMichael Große     * @return string
48121fcef82SMichael Große     */
48221fcef82SMichael Große    protected function getPageLookupHTML($data)
48321fcef82SMichael Große    {
48421fcef82SMichael Große        if (empty($data)) {
48521fcef82SMichael Große            return '';
48621fcef82SMichael Große        }
48721fcef82SMichael Große
48821fcef82SMichael Große        global $lang;
48921fcef82SMichael Große
49021fcef82SMichael Große        $html = '<div class="search_quickresult">';
49121fcef82SMichael Große        $html .= '<h3>' . $lang['quickhits'] . ':</h3>';
49221fcef82SMichael Große        $html .= '<ul class="search_quickhits">';
49321fcef82SMichael Große        foreach ($data as $id => $title) {
4944eab6f7cSMichael Große            $link = html_wikilink(':' . $id);
4954eab6f7cSMichael Große            $eventData = [
4964eab6f7cSMichael Große                'listItemContent' => [$link],
4974eab6f7cSMichael Große                'page' => $id,
4984eab6f7cSMichael Große            ];
4994eab6f7cSMichael Große            trigger_event('SEARCH_RESULT_PAGELOOKUP', $eventData);
5004eab6f7cSMichael Große            $html .= '<li>' . implode('', $eventData['listItemContent']) . '</li>';
50121fcef82SMichael Große        }
50221fcef82SMichael Große        $html .= '</ul> ';
50321fcef82SMichael Große        //clear float (see http://www.complexspiral.com/publications/containing-floats/)
50421fcef82SMichael Große        $html .= '<div class="clearer"></div>';
50521fcef82SMichael Große        $html .= '</div>';
50621fcef82SMichael Große
50721fcef82SMichael Große        return $html;
50821fcef82SMichael Große    }
50921fcef82SMichael Große
51021fcef82SMichael Große    /**
51121fcef82SMichael Große     * Build HTML for fulltext search results or "no results" message
51221fcef82SMichael Große     *
51321fcef82SMichael Große     * @param array $data      the results of the fulltext search
51421fcef82SMichael Große     * @param array $highlight the terms to be highlighted in the results
51521fcef82SMichael Große     *
51621fcef82SMichael Große     * @return string
51721fcef82SMichael Große     */
51821fcef82SMichael Große    protected function getFulltextResultsHTML($data, $highlight)
51921fcef82SMichael Große    {
52021fcef82SMichael Große        global $lang;
52121fcef82SMichael Große
52221fcef82SMichael Große        if (empty($data)) {
52321fcef82SMichael Große            return '<div class="nothing">' . $lang['nothingfound'] . '</div>';
52421fcef82SMichael Große        }
52521fcef82SMichael Große
52621fcef82SMichael Große        $html = '';
52721fcef82SMichael Große        $html .= '<dl class="search_results">';
52821fcef82SMichael Große        $num = 1;
5294c924eb8SMichael Große
53021fcef82SMichael Große        foreach ($data as $id => $cnt) {
5314eab6f7cSMichael Große            $resultLink = html_wikilink(':' . $id, null, $highlight);
5324c924eb8SMichael Große
5334c924eb8SMichael Große            $resultHeader = [$resultLink];
5344c924eb8SMichael Große
5354eab6f7cSMichael Große
5364c924eb8SMichael Große            $restrictQueryToNSLink = $this->restrictQueryToNSLink(getNS($id));
5374c924eb8SMichael Große            if ($restrictQueryToNSLink) {
5384c924eb8SMichael Große                $resultHeader[] = $restrictQueryToNSLink;
5394c924eb8SMichael Große            }
5404c924eb8SMichael Große
5419a75abfbSMichael Große            $snippet = '';
5429a75abfbSMichael Große            $lastMod = '';
5439a75abfbSMichael Große            $mtime = filemtime(wikiFN($id));
5449a75abfbSMichael Große            if ($cnt !== 0) {
5459a75abfbSMichael Große                $resultHeader[] = $cnt . ' ' . $lang['hits'];
5469a75abfbSMichael Große                if ($num < FT_SNIPPET_NUMBER) { // create snippets for the first number of matches only
5479a75abfbSMichael Große                    $snippet = '<dd>' . ft_snippet($id, $highlight) . '</dd>';
5489a75abfbSMichael Große                    $lastMod = '<span class="search_results__lastmod">' . $lang['lastmod'] . ' ';
5499a75abfbSMichael Große                    $lastMod .= '<time datetime="' . date_iso8601($mtime) . '">' . dformat($mtime) . '</time>';
5509a75abfbSMichael Große                    $lastMod .= '</span>';
5519a75abfbSMichael Große                }
5529a75abfbSMichael Große                $num++;
5539a75abfbSMichael Große            }
5549a75abfbSMichael Große
5559a75abfbSMichael Große            $metaLine = '<div class="search_results__metaLine">';
5569a75abfbSMichael Große            $metaLine .= $lastMod;
5579a75abfbSMichael Große            $metaLine .= '</div>';
5589a75abfbSMichael Große
5599a75abfbSMichael Große
5604eab6f7cSMichael Große            $eventData = [
5614c924eb8SMichael Große                'resultHeader' => $resultHeader,
5629a75abfbSMichael Große                'resultBody' => [$metaLine, $snippet],
5634eab6f7cSMichael Große                'page' => $id,
5644eab6f7cSMichael Große            ];
5654eab6f7cSMichael Große            trigger_event('SEARCH_RESULT_FULLPAGE', $eventData);
5664eab6f7cSMichael Große            $html .= '<div class="search_fullpage_result">';
5674eab6f7cSMichael Große            $html .= '<dt>' . implode(' ', $eventData['resultHeader']) . '</dt>';
5684eab6f7cSMichael Große            $html .= implode('', $eventData['resultBody']);
5694eab6f7cSMichael Große            $html .= '</div>';
57021fcef82SMichael Große        }
57121fcef82SMichael Große        $html .= '</dl>';
57221fcef82SMichael Große
57321fcef82SMichael Große        return $html;
57421fcef82SMichael Große    }
5754c924eb8SMichael Große
5764c924eb8SMichael Große    /**
5774c924eb8SMichael Große     * create a link to restrict the current query to a namespace
5784c924eb8SMichael Große     *
5794c924eb8SMichael Große     * @param bool|string $ns the namespace to which to restrict the query
5804c924eb8SMichael Große     *
5814c924eb8SMichael Große     * @return bool|string
5824c924eb8SMichael Große     */
5834c924eb8SMichael Große    protected function restrictQueryToNSLink($ns)
5844c924eb8SMichael Große    {
5854c924eb8SMichael Große        if (!$ns) {
5864c924eb8SMichael Große            return false;
5874c924eb8SMichael Große        }
588df977249SMichael Große        if (!$this->isNamespaceAssistanceAvailable($this->parsedQuery)) {
5894c924eb8SMichael Große            return false;
5904c924eb8SMichael Große        }
5914c924eb8SMichael Große        if (!empty($this->parsedQuery['ns']) && $this->parsedQuery['ns'][0] === $ns) {
5924c924eb8SMichael Große            return false;
5934c924eb8SMichael Große        }
5944c924eb8SMichael Große        $name = '@' . $ns;
595bbc1da2eSMichael Große        $tmpForm = new Form();
59618856c5dSMichael Große        $this->searchState->addSeachLinkNS($tmpForm, $name, $ns);
597bbc1da2eSMichael Große        return $tmpForm->toHTML();
5984c924eb8SMichael Große    }
59921fcef82SMichael Große}
600