xref: /dokuwiki/inc/Ui/Search.php (revision 6d55fda7de37f2a4bca72abd080dc59986d75e36)
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     *
19fc46ed58SMichael Große     * @param array $pageLookupResults pagename lookup results in the form [pagename => pagetitle]
20fc46ed58SMichael Große     * @param array $fullTextResults fulltext search results in the form [pagename => #hits]
21fc46ed58SMichael Große     * @param array $highlight  array of strings to be highlighted
2221fcef82SMichael Große     */
236639a152SMichael Große    public function __construct(array $pageLookupResults, array $fullTextResults, $highlight)
2421fcef82SMichael Große    {
25d09b5b64SMichael Große        global $QUERY;
264c924eb8SMichael Große        $Indexer = idx_get_indexer();
27d09b5b64SMichael Große
28d09b5b64SMichael Große        $this->query = $QUERY;
29bbc1da2eSMichael Große        $this->parsedQuery = ft_queryParser($Indexer, $QUERY);
3018856c5dSMichael Große        $this->searchState = new SearchState($this->parsedQuery);
3121fcef82SMichael Große
326639a152SMichael Große        $this->pageLookupResults = $pageLookupResults;
336639a152SMichael Große        $this->fullTextResults = $fullTextResults;
3421fcef82SMichael Große        $this->highlight = $highlight;
35b3cfe85aSMichael Große    }
36bbc1da2eSMichael Große
37b3cfe85aSMichael Große    /**
3821fcef82SMichael Große     * display the search result
3921fcef82SMichael Große     *
4021fcef82SMichael Große     * @return void
4121fcef82SMichael Große     */
4221fcef82SMichael Große    public function show()
4321fcef82SMichael Große    {
4421fcef82SMichael Große        $searchHTML = '';
4521fcef82SMichael Große
4621fcef82SMichael Große        $searchHTML .= $this->getSearchIntroHTML($this->query);
4721fcef82SMichael Große
482ce8affcSMichael Große        $searchHTML .= $this->getSearchFormHTML($this->query);
492ce8affcSMichael Große
5021fcef82SMichael Große        $searchHTML .= $this->getPageLookupHTML($this->pageLookupResults);
5121fcef82SMichael Große
5221fcef82SMichael Große        $searchHTML .= $this->getFulltextResultsHTML($this->fullTextResults, $this->highlight);
5321fcef82SMichael Große
5421fcef82SMichael Große        echo $searchHTML;
5521fcef82SMichael Große    }
5621fcef82SMichael Große
5721fcef82SMichael Große    /**
58427ed988SMichael Große     * Get a form which can be used to adjust/refine the search
59427ed988SMichael Große     *
60427ed988SMichael Große     * @param string $query
61427ed988SMichael Große     *
62427ed988SMichael Große     * @return string
63427ed988SMichael Große     */
64427ed988SMichael Große    protected function getSearchFormHTML($query)
65427ed988SMichael Große    {
66bbc1da2eSMichael Große        global $lang, $ID, $INPUT;
67427ed988SMichael Große
687fa270bcSMichael Große        $searchForm = (new Form(['method' => 'get'], true))->addClass('search-results-form');
69bb8ef867SMichael Große        $searchForm->setHiddenField('do', 'search');
70d22b78c8SMichael Große        $searchForm->setHiddenField('id', $ID);
711265b193SMichael Große        $searchForm->setHiddenField('sf', '1');
72422bbbc6SMichael Große        if ($INPUT->has('min')) {
73422bbbc6SMichael Große            $searchForm->setHiddenField('min', $INPUT->str('min'));
74bbc1da2eSMichael Große        }
75422bbbc6SMichael Große        if ($INPUT->has('max')) {
76422bbbc6SMichael Große            $searchForm->setHiddenField('max', $INPUT->str('max'));
77bbc1da2eSMichael Große        }
781265b193SMichael Große        if ($INPUT->has('srt')) {
791265b193SMichael Große            $searchForm->setHiddenField('srt', $INPUT->str('srt'));
808d0e286aSMichael Große        }
814bdf82b5SAndreas Gohr        $searchForm->addFieldsetOpen()->addClass('search-form');
82d22b78c8SMichael Große        $searchForm->addTextInput('q')->val($query)->useInput(false);
83427ed988SMichael Große        $searchForm->addButton('', $lang['btn_search'])->attr('type', 'submit');
84bb8ef867SMichael Große
8518856c5dSMichael Große        $this->addSearchAssistanceElements($searchForm);
86bb8ef867SMichael Große
87427ed988SMichael Große        $searchForm->addFieldsetClose();
88427ed988SMichael Große
8916ece95cSMichael Große        trigger_event('FORM_SEARCH_OUTPUT', $searchForm);
9081a0edd9SMichael Große
91427ed988SMichael Große        return $searchForm->toHTML();
92427ed988SMichael Große    }
93427ed988SMichael Große
94be76738bSMichael Große    /**
95be76738bSMichael Große     * Add elements to adjust how the results are sorted
96be76738bSMichael Große     *
97be76738bSMichael Große     * @param Form $searchForm
98be76738bSMichael Große     */
99b005809cSMichael Große    protected function addSortTool(Form $searchForm)
100b005809cSMichael Große    {
101b005809cSMichael Große        global $INPUT, $lang;
102b005809cSMichael Große
103b005809cSMichael Große        $options = [
104b005809cSMichael Große            'hits' => [
105b005809cSMichael Große                'label' => $lang['search_sort_by_hits'],
106b005809cSMichael Große                'sort' => '',
107b005809cSMichael Große            ],
108b005809cSMichael Große            'mtime' => [
109b005809cSMichael Große                'label' => $lang['search_sort_by_mtime'],
110b005809cSMichael Große                'sort' => 'mtime',
111b005809cSMichael Große            ],
112b005809cSMichael Große        ];
113b005809cSMichael Große        $activeOption = 'hits';
114b005809cSMichael Große
1151265b193SMichael Große        if ($INPUT->str('srt') === 'mtime') {
116b005809cSMichael Große            $activeOption = 'mtime';
117b005809cSMichael Große        }
118b005809cSMichael Große
1192171f9cbSAndreas Gohr        $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true');
120b005809cSMichael Große        // render current
1214bdf82b5SAndreas Gohr        $currentWrapper = $searchForm->addTagOpen('div')->addClass('current');
122b005809cSMichael Große        if ($activeOption !== 'hits') {
1234bdf82b5SAndreas Gohr            $currentWrapper->addClass('changed');
124b005809cSMichael Große        }
125b005809cSMichael Große        $searchForm->addHTML($options[$activeOption]['label']);
126b005809cSMichael Große        $searchForm->addTagClose('div');
127b005809cSMichael Große
128b005809cSMichael Große        // render options list
1292171f9cbSAndreas Gohr        $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false');
130b005809cSMichael Große
131b005809cSMichael Große        foreach ($options as $key => $option) {
1324bdf82b5SAndreas Gohr            $listItem = $searchForm->addTagOpen('li');
133b005809cSMichael Große
134b005809cSMichael Große            if ($key === $activeOption) {
1354bdf82b5SAndreas Gohr                $listItem->addClass('active');
136b005809cSMichael Große                $searchForm->addHTML($option['label']);
137b005809cSMichael Große            } else {
13852d4cd42SMichael Große                $link = $this->searchState->withSorting($option['sort'])->getSearchLink($option['label']);
13952d4cd42SMichael Große                $searchForm->addHTML($link);
140b005809cSMichael Große            }
141b005809cSMichael Große            $searchForm->addTagClose('li');
142b005809cSMichael Große        }
143b005809cSMichael Große        $searchForm->addTagClose('ul');
144b005809cSMichael Große
145b005809cSMichael Große        $searchForm->addTagClose('div');
146b005809cSMichael Große
147b005809cSMichael Große    }
148b005809cSMichael Große
149be76738bSMichael Große    /**
150be76738bSMichael Große     * Check if the query is simple enough to modify its namespace limitations without breaking the rest of the query
151be76738bSMichael Große     *
152be76738bSMichael Große     * @param array $parsedQuery
153be76738bSMichael Große     *
154be76738bSMichael Große     * @return bool
155be76738bSMichael Große     */
156df977249SMichael Große    protected function isNamespaceAssistanceAvailable(array $parsedQuery) {
157df977249SMichael Große        if (preg_match('/[\(\)\|]/', $parsedQuery['query']) === 1) {
158bb8ef867SMichael Große            return false;
159bb8ef867SMichael Große        }
160df977249SMichael Große
161df977249SMichael Große        return true;
162df977249SMichael Große    }
163df977249SMichael Große
164be76738bSMichael Große    /**
165be76738bSMichael Große     * Check if the query is simple enough to modify the fragment search behavior without breaking the rest of the query
166be76738bSMichael Große     *
167be76738bSMichael Große     * @param array $parsedQuery
168be76738bSMichael Große     *
169be76738bSMichael Große     * @return bool
170be76738bSMichael Große     */
171df977249SMichael Große    protected function isFragmentAssistanceAvailable(array $parsedQuery) {
172df977249SMichael Große        if (preg_match('/[\(\)\|]/', $parsedQuery['query']) === 1) {
173bb8ef867SMichael Große            return false;
174bb8ef867SMichael Große        }
175bb8ef867SMichael Große
176bb8ef867SMichael Große        if (!empty($parsedQuery['phrases'])) {
177bb8ef867SMichael Große            return false;
178bb8ef867SMichael Große        }
179bb8ef867SMichael Große
180bb8ef867SMichael Große        return true;
181bb8ef867SMichael Große    }
182bb8ef867SMichael Große
183bb8ef867SMichael Große    /**
184bb8ef867SMichael Große     * Add the elements to be used for search assistance
185bb8ef867SMichael Große     *
186bb8ef867SMichael Große     * @param Form $searchForm
187bb8ef867SMichael Große     */
18818856c5dSMichael Große    protected function addSearchAssistanceElements(Form $searchForm)
189bb8ef867SMichael Große    {
190bb8ef867SMichael Große        $searchForm->addTagOpen('div')
1914bdf82b5SAndreas Gohr            ->addClass('advancedOptions')
1922171f9cbSAndreas Gohr            ->attr('style', 'display: none;')
1932171f9cbSAndreas Gohr            ->attr('aria-hidden', 'true');
194bb8ef867SMichael Große
19518856c5dSMichael Große        $this->addFragmentBehaviorLinks($searchForm);
19618856c5dSMichael Große        $this->addNamespaceSelector($searchForm);
19718856c5dSMichael Große        $this->addDateSelector($searchForm);
198b005809cSMichael Große        $this->addSortTool($searchForm);
199bb8ef867SMichael Große
200bb8ef867SMichael Große        $searchForm->addTagClose('div');
201bb8ef867SMichael Große    }
202bb8ef867SMichael Große
203be76738bSMichael Große    /**
204be76738bSMichael Große     *  Add the elements to adjust the fragment search behavior
205be76738bSMichael Große     *
206be76738bSMichael Große     * @param Form $searchForm
207be76738bSMichael Große     */
20818856c5dSMichael Große    protected function addFragmentBehaviorLinks(Form $searchForm)
2094d0cb6e1SMichael Große    {
210df977249SMichael Große        if (!$this->isFragmentAssistanceAvailable($this->parsedQuery)) {
211df977249SMichael Große            return;
212df977249SMichael Große        }
213b005809cSMichael Große        global $lang;
2144d0cb6e1SMichael Große
215b005809cSMichael Große        $options = [
216b005809cSMichael Große            'exact' => [
217b005809cSMichael Große                'label' => $lang['search_exact_match'],
218b005809cSMichael Große                'and' => array_map(function ($term) {
219b005809cSMichael Große                    return trim($term, '*');
220b005809cSMichael Große                }, $this->parsedQuery['and']),
221df977249SMichael Große                'not' => array_map(function ($term) {
222df977249SMichael Große                    return trim($term, '*');
223df977249SMichael Große                }, $this->parsedQuery['not']),
224b005809cSMichael Große            ],
225b005809cSMichael Große            'starts' => [
226b005809cSMichael Große                'label' => $lang['search_starts_with'],
227b005809cSMichael Große                'and' => array_map(function ($term) {
228b005809cSMichael Große                    return trim($term, '*') . '*';
229df977249SMichael Große                }, $this->parsedQuery['and']),
230df977249SMichael Große                'not' => array_map(function ($term) {
231df977249SMichael Große                    return trim($term, '*') . '*';
232df977249SMichael Große                }, $this->parsedQuery['not']),
233b005809cSMichael Große            ],
234b005809cSMichael Große            'ends' => [
235b005809cSMichael Große                'label' => $lang['search_ends_with'],
236b005809cSMichael Große                'and' => array_map(function ($term) {
237b005809cSMichael Große                    return '*' . trim($term, '*');
238df977249SMichael Große                }, $this->parsedQuery['and']),
239df977249SMichael Große                'not' => array_map(function ($term) {
240df977249SMichael Große                    return '*' . trim($term, '*');
241df977249SMichael Große                }, $this->parsedQuery['not']),
242b005809cSMichael Große            ],
243b005809cSMichael Große            'contains' => [
244b005809cSMichael Große                'label' => $lang['search_contains'],
245b005809cSMichael Große                'and' => array_map(function ($term) {
246b005809cSMichael Große                    return '*' . trim($term, '*') . '*';
247df977249SMichael Große                }, $this->parsedQuery['and']),
248df977249SMichael Große                'not' => array_map(function ($term) {
249df977249SMichael Große                    return '*' . trim($term, '*') . '*';
250df977249SMichael Große                }, $this->parsedQuery['not']),
251b005809cSMichael Große            ]
252b005809cSMichael Große        ];
253b005809cSMichael Große
254b005809cSMichael Große        // detect current
255c6b5b74aSMichael Große        $activeOption = 'custom';
256b005809cSMichael Große        foreach ($options as $key => $option) {
257b005809cSMichael Große            if ($this->parsedQuery['and'] === $option['and']) {
258b005809cSMichael Große                $activeOption = $key;
259b005809cSMichael Große            }
260b005809cSMichael Große        }
261c6b5b74aSMichael Große        if ($activeOption === 'custom') {
262c6b5b74aSMichael Große            $options = array_merge(['custom' => [
263c6b5b74aSMichael Große                'label' => $lang['search_custom_match'],
264c6b5b74aSMichael Große            ]], $options);
265c6b5b74aSMichael Große        }
266b005809cSMichael Große
2672171f9cbSAndreas Gohr        $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true');
268b005809cSMichael Große        // render current
2694bdf82b5SAndreas Gohr        $currentWrapper = $searchForm->addTagOpen('div')->addClass('current');
270b005809cSMichael Große        if ($activeOption !== 'exact') {
2714bdf82b5SAndreas Gohr            $currentWrapper->addClass('changed');
272b005809cSMichael Große        }
273b005809cSMichael Große        $searchForm->addHTML($options[$activeOption]['label']);
274b005809cSMichael Große        $searchForm->addTagClose('div');
275b005809cSMichael Große
276b005809cSMichael Große        // render options list
2772171f9cbSAndreas Gohr        $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false');
278b005809cSMichael Große
279b005809cSMichael Große        foreach ($options as $key => $option) {
2804bdf82b5SAndreas Gohr            $listItem = $searchForm->addTagOpen('li');
281b005809cSMichael Große
282b005809cSMichael Große            if ($key === $activeOption) {
2834bdf82b5SAndreas Gohr                $listItem->addClass('active');
284b005809cSMichael Große                $searchForm->addHTML($option['label']);
285b005809cSMichael Große            } else {
28652d4cd42SMichael Große                $link = $this->searchState
28752d4cd42SMichael Große                    ->withFragments($option['and'], $option['not'])
28852d4cd42SMichael Große                    ->getSearchLink($option['label'])
28952d4cd42SMichael Große                ;
29052d4cd42SMichael Große                $searchForm->addHTML($link);
291b005809cSMichael Große            }
292b005809cSMichael Große            $searchForm->addTagClose('li');
293b005809cSMichael Große        }
294b005809cSMichael Große        $searchForm->addTagClose('ul');
2954d0cb6e1SMichael Große
2964d0cb6e1SMichael Große        $searchForm->addTagClose('div');
297b005809cSMichael Große
298b005809cSMichael Große        // render options list
2994d0cb6e1SMichael Große    }
3004d0cb6e1SMichael Große
301bb8ef867SMichael Große    /**
302bb8ef867SMichael Große     * Add the elements for the namespace selector
303bb8ef867SMichael Große     *
304bb8ef867SMichael Große     * @param Form $searchForm
305bb8ef867SMichael Große     */
30618856c5dSMichael Große    protected function addNamespaceSelector(Form $searchForm)
307bb8ef867SMichael Große    {
308df977249SMichael Große        if (!$this->isNamespaceAssistanceAvailable($this->parsedQuery)) {
309df977249SMichael Große            return;
310df977249SMichael Große        }
311df977249SMichael Große
312b005809cSMichael Große        global $lang;
313b005809cSMichael Große
31418856c5dSMichael Große        $baseNS = empty($this->parsedQuery['ns']) ? '' : $this->parsedQuery['ns'][0];
315bbc1da2eSMichael Große        $extraNS = $this->getAdditionalNamespacesFromResults($baseNS);
3164d0cb6e1SMichael Große
3172171f9cbSAndreas Gohr        $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true');
318b005809cSMichael Große        // render current
3194bdf82b5SAndreas Gohr        $currentWrapper = $searchForm->addTagOpen('div')->addClass('current');
320bbc1da2eSMichael Große        if ($baseNS) {
3214bdf82b5SAndreas Gohr            $currentWrapper->addClass('changed');
322b005809cSMichael Große            $searchForm->addHTML('@' . $baseNS);
323b005809cSMichael Große        } else {
324b005809cSMichael Große            $searchForm->addHTML($lang['search_any_ns']);
325b005809cSMichael Große        }
326b005809cSMichael Große        $searchForm->addTagClose('div');
327b005809cSMichael Große
328b005809cSMichael Große        // render options list
3292171f9cbSAndreas Gohr        $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false');
330b005809cSMichael Große
3314bdf82b5SAndreas Gohr        $listItem = $searchForm->addTagOpen('li');
332b005809cSMichael Große        if ($baseNS) {
3334bdf82b5SAndreas Gohr            $listItem->addClass('active');
33452d4cd42SMichael Große            $link = $this->searchState->withNamespace('')->getSearchLink($lang['search_any_ns']);
33552d4cd42SMichael Große            $searchForm->addHTML($link);
336b005809cSMichael Große        } else {
337b005809cSMichael Große            $searchForm->addHTML($lang['search_any_ns']);
338bb8ef867SMichael Große        }
339b005809cSMichael Große        $searchForm->addTagClose('li');
340bb8ef867SMichael Große
34118856c5dSMichael Große        foreach ($extraNS as $ns => $count) {
3424bdf82b5SAndreas Gohr            $listItem = $searchForm->addTagOpen('li');
3431d918893SAndreas Gohr            $label = $ns . ($count ? " <bdi>($count)</bdi>" : '');
3444d0cb6e1SMichael Große
345b005809cSMichael Große            if ($ns === $baseNS) {
3464bdf82b5SAndreas Gohr                $listItem->addClass('active');
347b005809cSMichael Große                $searchForm->addHTML($label);
348b005809cSMichael Große            } else {
34952d4cd42SMichael Große                $link = $this->searchState->withNamespace($ns)->getSearchLink($label);
35052d4cd42SMichael Große                $searchForm->addHTML($link);
351bb8ef867SMichael Große            }
352b005809cSMichael Große            $searchForm->addTagClose('li');
353bb8ef867SMichael Große        }
354b005809cSMichael Große        $searchForm->addTagClose('ul');
355bb8ef867SMichael Große
356bb8ef867SMichael Große        $searchForm->addTagClose('div');
357b005809cSMichael Große
358bb8ef867SMichael Große    }
359bb8ef867SMichael Große
360bb8ef867SMichael Große    /**
361bb8ef867SMichael Große     * Parse the full text results for their top namespaces below the given base namespace
362bb8ef867SMichael Große     *
363bb8ef867SMichael Große     * @param string $baseNS the namespace within which was searched, empty string for root namespace
364bb8ef867SMichael Große     *
365bb8ef867SMichael Große     * @return array an associative array with namespace => #number of found pages, sorted descending
366bb8ef867SMichael Große     */
367bb8ef867SMichael Große    protected function getAdditionalNamespacesFromResults($baseNS)
368bb8ef867SMichael Große    {
369bb8ef867SMichael Große        $namespaces = [];
370bb8ef867SMichael Große        $baseNSLength = strlen($baseNS);
371bb8ef867SMichael Große        foreach ($this->fullTextResults as $page => $numberOfHits) {
372bb8ef867SMichael Große            $namespace = getNS($page);
373bb8ef867SMichael Große            if (!$namespace) {
374bb8ef867SMichael Große                continue;
375bb8ef867SMichael Große            }
376bb8ef867SMichael Große            if ($namespace === $baseNS) {
377bb8ef867SMichael Große                continue;
378bb8ef867SMichael Große            }
379bb8ef867SMichael Große            $firstColon = strpos((string)$namespace, ':', $baseNSLength + 1) ?: strlen($namespace);
380bb8ef867SMichael Große            $subtopNS = substr($namespace, 0, $firstColon);
381bb8ef867SMichael Große            if (empty($namespaces[$subtopNS])) {
382bb8ef867SMichael Große                $namespaces[$subtopNS] = 0;
383bb8ef867SMichael Große            }
384bb8ef867SMichael Große            $namespaces[$subtopNS] += 1;
385bb8ef867SMichael Große        }
38655dc8783SMichael Große        ksort($namespaces);
387bb8ef867SMichael Große        arsort($namespaces);
388bb8ef867SMichael Große        return $namespaces;
389bb8ef867SMichael Große    }
390bb8ef867SMichael Große
391bb8ef867SMichael Große    /**
392bbc1da2eSMichael Große     * @ToDo: custom date input
393bbc1da2eSMichael Große     *
394bbc1da2eSMichael Große     * @param Form $searchForm
395bbc1da2eSMichael Große     */
396b005809cSMichael Große    protected function addDateSelector(Form $searchForm)
397b005809cSMichael Große    {
398b005809cSMichael Große        global $INPUT, $lang;
399bbc1da2eSMichael Große
400b005809cSMichael Große        $options = [
401b005809cSMichael Große            'any' => [
402b005809cSMichael Große                'before' => false,
403b005809cSMichael Große                'after' => false,
404b005809cSMichael Große                'label' => $lang['search_any_time'],
405b005809cSMichael Große            ],
406b005809cSMichael Große            'week' => [
407b005809cSMichael Große                'before' => false,
408b005809cSMichael Große                'after' => '1 week ago',
409b005809cSMichael Große                'label' => $lang['search_past_7_days'],
410b005809cSMichael Große            ],
411b005809cSMichael Große            'month' => [
412b005809cSMichael Große                'before' => false,
413b005809cSMichael Große                'after' => '1 month ago',
414b005809cSMichael Große                'label' => $lang['search_past_month'],
415b005809cSMichael Große            ],
416b005809cSMichael Große            'year' => [
417b005809cSMichael Große                'before' => false,
418b005809cSMichael Große                'after' => '1 year ago',
419b005809cSMichael Große                'label' => $lang['search_past_year'],
420b005809cSMichael Große            ],
421b005809cSMichael Große        ];
422b005809cSMichael Große        $activeOption = 'any';
423b005809cSMichael Große        foreach ($options as $key => $option) {
424422bbbc6SMichael Große            if ($INPUT->str('min') === $option['after']) {
425b005809cSMichael Große                $activeOption = $key;
426b005809cSMichael Große                break;
427b005809cSMichael Große            }
428b005809cSMichael Große        }
429b005809cSMichael Große
4302171f9cbSAndreas Gohr        $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true');
431b005809cSMichael Große        // render current
4324bdf82b5SAndreas Gohr        $currentWrapper = $searchForm->addTagOpen('div')->addClass('current');
433422bbbc6SMichael Große        if ($INPUT->has('max') || $INPUT->has('min')) {
4344bdf82b5SAndreas Gohr            $currentWrapper->addClass('changed');
435bbc1da2eSMichael Große        }
436b005809cSMichael Große        $searchForm->addHTML($options[$activeOption]['label']);
437b005809cSMichael Große        $searchForm->addTagClose('div');
438bbc1da2eSMichael Große
439b005809cSMichael Große        // render options list
4402171f9cbSAndreas Gohr        $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false');
441b005809cSMichael Große
442b005809cSMichael Große        foreach ($options as $key => $option) {
4434bdf82b5SAndreas Gohr            $listItem = $searchForm->addTagOpen('li');
444b005809cSMichael Große
445b005809cSMichael Große            if ($key === $activeOption) {
4464bdf82b5SAndreas Gohr                $listItem->addClass('active');
447b005809cSMichael Große                $searchForm->addHTML($option['label']);
448bbc1da2eSMichael Große            } else {
44952d4cd42SMichael Große                $link = $this->searchState
45052d4cd42SMichael Große                    ->withTimeLimitations($option['after'], $option['before'])
45152d4cd42SMichael Große                    ->getSearchLink($option['label'])
45252d4cd42SMichael Große                ;
45352d4cd42SMichael Große                $searchForm->addHTML($link);
454bbc1da2eSMichael Große            }
455b005809cSMichael Große            $searchForm->addTagClose('li');
456bbc1da2eSMichael Große        }
457b005809cSMichael Große        $searchForm->addTagClose('ul');
458bbc1da2eSMichael Große
459bbc1da2eSMichael Große        $searchForm->addTagClose('div');
460bbc1da2eSMichael Große    }
461bbc1da2eSMichael Große
462bbc1da2eSMichael Große
463bbc1da2eSMichael Große    /**
46421fcef82SMichael Große     * Build the intro text for the search page
46521fcef82SMichael Große     *
46621fcef82SMichael Große     * @param string $query the search query
46721fcef82SMichael Große     *
46821fcef82SMichael Große     * @return string
46921fcef82SMichael Große     */
47021fcef82SMichael Große    protected function getSearchIntroHTML($query)
47121fcef82SMichael Große    {
4722ce8affcSMichael Große        global $lang;
47321fcef82SMichael Große
47421fcef82SMichael Große        $intro = p_locale_xhtml('searchpage');
4752ce8affcSMichael Große
4762ce8affcSMichael Große        $queryPagename = $this->createPagenameFromQuery($this->parsedQuery);
4772ce8affcSMichael Große        $createQueryPageLink = html_wikilink($queryPagename . '?do=edit', $queryPagename);
4782ce8affcSMichael Große
4792ce8affcSMichael Große        $pagecreateinfo = '';
4802ce8affcSMichael Große        if (auth_quickaclcheck($queryPagename) >= AUTH_CREATE) {
4812ce8affcSMichael Große            $pagecreateinfo = sprintf($lang['searchcreatepage'], $createQueryPageLink);
4822ce8affcSMichael Große        }
48321fcef82SMichael Große        $intro = str_replace(
48421fcef82SMichael Große            array('@QUERY@', '@SEARCH@', '@CREATEPAGEINFO@'),
48521fcef82SMichael Große            array(hsc(rawurlencode($query)), hsc($query), $pagecreateinfo),
48621fcef82SMichael Große            $intro
48721fcef82SMichael Große        );
4882ce8affcSMichael Große
48921fcef82SMichael Große        return $intro;
49021fcef82SMichael Große    }
49121fcef82SMichael Große
49221fcef82SMichael Große    /**
4932ce8affcSMichael Große     * Create a pagename based the parsed search query
4942ce8affcSMichael Große     *
4952ce8affcSMichael Große     * @param array $parsedQuery
4962ce8affcSMichael Große     *
4972ce8affcSMichael Große     * @return string pagename constructed from the parsed query
4982ce8affcSMichael Große     */
4992ce8affcSMichael Große    protected function createPagenameFromQuery($parsedQuery)
5002ce8affcSMichael Große    {
5012ce8affcSMichael Große        $pagename = '';
5022ce8affcSMichael Große        if (!empty($parsedQuery['ns'])) {
5032ce8affcSMichael Große            $pagename .= cleanID($parsedQuery['ns'][0]);
5042ce8affcSMichael Große        }
5052ce8affcSMichael Große        $pagename .= ':' . cleanID(implode(' ' , $parsedQuery['highlight']));
5062ce8affcSMichael Große        return $pagename;
5072ce8affcSMichael Große    }
5082ce8affcSMichael Große
5092ce8affcSMichael Große    /**
51021fcef82SMichael Große     * Build HTML for a list of pages with matching pagenames
51121fcef82SMichael Große     *
51221fcef82SMichael Große     * @param array $data search results
51321fcef82SMichael Große     *
51421fcef82SMichael Große     * @return string
51521fcef82SMichael Große     */
51621fcef82SMichael Große    protected function getPageLookupHTML($data)
51721fcef82SMichael Große    {
51821fcef82SMichael Große        if (empty($data)) {
51921fcef82SMichael Große            return '';
52021fcef82SMichael Große        }
52121fcef82SMichael Große
52221fcef82SMichael Große        global $lang;
52321fcef82SMichael Große
52421fcef82SMichael Große        $html = '<div class="search_quickresult">';
525*6d55fda7SMichael Große        $html .= '<h2>' . $lang['quickhits'] . ':</h2>';
52621fcef82SMichael Große        $html .= '<ul class="search_quickhits">';
52721fcef82SMichael Große        foreach ($data as $id => $title) {
5285d87aa31SMichael Große            $name = null;
5295d87aa31SMichael Große            if (!useHeading('navigation') && $ns = getNS($id)) {
5305d87aa31SMichael Große                $name = shorten(noNS($id), ' (' . $ns . ')', 30);
5315d87aa31SMichael Große            }
5325d87aa31SMichael Große            $link = html_wikilink(':' . $id, $name);
5334eab6f7cSMichael Große            $eventData = [
5344eab6f7cSMichael Große                'listItemContent' => [$link],
5354eab6f7cSMichael Große                'page' => $id,
5364eab6f7cSMichael Große            ];
5374eab6f7cSMichael Große            trigger_event('SEARCH_RESULT_PAGELOOKUP', $eventData);
5384eab6f7cSMichael Große            $html .= '<li>' . implode('', $eventData['listItemContent']) . '</li>';
53921fcef82SMichael Große        }
54021fcef82SMichael Große        $html .= '</ul> ';
54121fcef82SMichael Große        //clear float (see http://www.complexspiral.com/publications/containing-floats/)
54221fcef82SMichael Große        $html .= '<div class="clearer"></div>';
54321fcef82SMichael Große        $html .= '</div>';
54421fcef82SMichael Große
54521fcef82SMichael Große        return $html;
54621fcef82SMichael Große    }
54721fcef82SMichael Große
54821fcef82SMichael Große    /**
54921fcef82SMichael Große     * Build HTML for fulltext search results or "no results" message
55021fcef82SMichael Große     *
55121fcef82SMichael Große     * @param array $data      the results of the fulltext search
55221fcef82SMichael Große     * @param array $highlight the terms to be highlighted in the results
55321fcef82SMichael Große     *
55421fcef82SMichael Große     * @return string
55521fcef82SMichael Große     */
55621fcef82SMichael Große    protected function getFulltextResultsHTML($data, $highlight)
55721fcef82SMichael Große    {
55821fcef82SMichael Große        global $lang;
55921fcef82SMichael Große
56021fcef82SMichael Große        if (empty($data)) {
56121fcef82SMichael Große            return '<div class="nothing">' . $lang['nothingfound'] . '</div>';
56221fcef82SMichael Große        }
56321fcef82SMichael Große
5642ce8affcSMichael Große        $html = '<div class="search_fulltextresult">';
565*6d55fda7SMichael Große        $html .= '<h2>' . $lang['search_fullresults'] . ':</h2>';
5662ce8affcSMichael Große
56721fcef82SMichael Große        $html .= '<dl class="search_results">';
56821fcef82SMichael Große        $num = 1;
56978d786c9SMichael Große        $position = 1;
5704c924eb8SMichael Große
57121fcef82SMichael Große        foreach ($data as $id => $cnt) {
57278d786c9SMichael Große            $position += 1;
5734eab6f7cSMichael Große            $resultLink = html_wikilink(':' . $id, null, $highlight);
5744c924eb8SMichael Große
5754c924eb8SMichael Große            $resultHeader = [$resultLink];
5764c924eb8SMichael Große
5774eab6f7cSMichael Große
5784c924eb8SMichael Große            $restrictQueryToNSLink = $this->restrictQueryToNSLink(getNS($id));
5794c924eb8SMichael Große            if ($restrictQueryToNSLink) {
5804c924eb8SMichael Große                $resultHeader[] = $restrictQueryToNSLink;
5814c924eb8SMichael Große            }
5824c924eb8SMichael Große
5839a75abfbSMichael Große            $snippet = '';
5845d06a1e4SMichael Große            $hits = '';
5855d06a1e4SMichael Große            $resultBody = [];
5869a75abfbSMichael Große            $mtime = filemtime(wikiFN($id));
5875d06a1e4SMichael Große            $lastMod = '<span class="lastmod">' . $lang['lastmod'] . '</span> ';
5880fc38806SAndreas Gohr            $lastMod .= '<time datetime="' . date_iso8601($mtime) . '" title="'.dformat($mtime).'">' . dformat($mtime, '%f') . '</time>';
5895d06a1e4SMichael Große            if ($cnt !== 0) {
5905d06a1e4SMichael Große                $hits = '<span class="hits">' . $cnt . ' ' . $lang['hits'] . '</span>';
5915d06a1e4SMichael Große                if ($num < FT_SNIPPET_NUMBER) { // create snippets for the first number of matches only
5925d06a1e4SMichael Große                    $snippet = ft_snippet($id, $highlight);
5939a75abfbSMichael Große                }
5949a75abfbSMichael Große                $num++;
5959a75abfbSMichael Große            }
5969a75abfbSMichael Große
5975d06a1e4SMichael Große            $resultBody['meta'] = $hits . $lastMod;
5985d06a1e4SMichael Große
5995d06a1e4SMichael Große            if ($snippet) {
6005d06a1e4SMichael Große                $resultBody['snippet'] = $snippet;
6015d06a1e4SMichael Große            }
6029a75abfbSMichael Große
6034eab6f7cSMichael Große            $eventData = [
6044c924eb8SMichael Große                'resultHeader' => $resultHeader,
6055d06a1e4SMichael Große                'resultBody' => $resultBody,
6064eab6f7cSMichael Große                'page' => $id,
60778d786c9SMichael Große                'position' => $position,
6084eab6f7cSMichael Große            ];
6094eab6f7cSMichael Große            trigger_event('SEARCH_RESULT_FULLPAGE', $eventData);
6105d06a1e4SMichael Große            $html .= '<div class="search_fullpage_result">';
6114eab6f7cSMichael Große            $html .= '<dt>' . implode(' ', $eventData['resultHeader']) . '</dt>';
6125d06a1e4SMichael Große            foreach ($eventData['resultBody'] as $class => $htmlContent) {
6135d06a1e4SMichael Große                $html .= "<dd class=\"$class\">$htmlContent</dd>";
6145d06a1e4SMichael Große            }
6155d06a1e4SMichael Große            $html .= '</div>';
61621fcef82SMichael Große        }
61721fcef82SMichael Große        $html .= '</dl>';
61821fcef82SMichael Große
6192ce8affcSMichael Große        $html .= '</div>';
6202ce8affcSMichael Große
62121fcef82SMichael Große        return $html;
62221fcef82SMichael Große    }
6234c924eb8SMichael Große
6244c924eb8SMichael Große    /**
6254c924eb8SMichael Große     * create a link to restrict the current query to a namespace
6264c924eb8SMichael Große     *
627ec27794fSMichael Große     * @param false|string $ns the namespace to which to restrict the query
6284c924eb8SMichael Große     *
629ec27794fSMichael Große     * @return false|string
6304c924eb8SMichael Große     */
6314c924eb8SMichael Große    protected function restrictQueryToNSLink($ns)
6324c924eb8SMichael Große    {
6334c924eb8SMichael Große        if (!$ns) {
6344c924eb8SMichael Große            return false;
6354c924eb8SMichael Große        }
636df977249SMichael Große        if (!$this->isNamespaceAssistanceAvailable($this->parsedQuery)) {
6374c924eb8SMichael Große            return false;
6384c924eb8SMichael Große        }
6394c924eb8SMichael Große        if (!empty($this->parsedQuery['ns']) && $this->parsedQuery['ns'][0] === $ns) {
6404c924eb8SMichael Große            return false;
6414c924eb8SMichael Große        }
64252d4cd42SMichael Große
6434c924eb8SMichael Große        $name = '@' . $ns;
64452d4cd42SMichael Große        return $this->searchState->withNamespace($ns)->getSearchLink($name);
6454c924eb8SMichael Große    }
64621fcef82SMichael Große}
647