xref: /dokuwiki/inc/Ui/Search.php (revision be76738b2e922fdef783c19c00c675e61174e143)
121fcef82SMichael Große<?php
221fcef82SMichael Große
321fcef82SMichael Großenamespace dokuwiki\Ui;
421fcef82SMichael Große
5427ed988SMichael Großeuse \dokuwiki\Form\Form;
6427ed988SMichael Große
721fcef82SMichael Großeclass Search extends Ui
821fcef82SMichael Große{
921fcef82SMichael Große    protected $query;
104c924eb8SMichael Große    protected $parsedQuery;
1118856c5dSMichael Große    protected $searchState;
1221fcef82SMichael Große    protected $pageLookupResults = array();
1321fcef82SMichael Große    protected $fullTextResults = array();
1421fcef82SMichael Große    protected $highlight = array();
1521fcef82SMichael Große
1621fcef82SMichael Große    /**
1721fcef82SMichael Große     * Search constructor.
186639a152SMichael Große     *
196639a152SMichael Große     * @param array  $pageLookupResults
206639a152SMichael Große     * @param array  $fullTextResults
216639a152SMichael Große     * @param string $highlight
2221fcef82SMichael Große     */
236639a152SMichael Große    public function __construct(array $pageLookupResults, array $fullTextResults, $highlight)
2421fcef82SMichael Große    {
25d09b5b64SMichael Große        global $QUERY;
264c924eb8SMichael Große        $Indexer = idx_get_indexer();
27d09b5b64SMichael Große
28d09b5b64SMichael Große        $this->query = $QUERY;
29bbc1da2eSMichael Große        $this->parsedQuery = ft_queryParser($Indexer, $QUERY);
3018856c5dSMichael Große        $this->searchState = new SearchState($this->parsedQuery);
3121fcef82SMichael Große
326639a152SMichael Große        $this->pageLookupResults = $pageLookupResults;
336639a152SMichael Große        $this->fullTextResults = $fullTextResults;
3421fcef82SMichael Große        $this->highlight = $highlight;
35b3cfe85aSMichael Große    }
36bbc1da2eSMichael Große
37b3cfe85aSMichael Große    /**
3821fcef82SMichael Große     * display the search result
3921fcef82SMichael Große     *
4021fcef82SMichael Große     * @return void
4121fcef82SMichael Große     */
4221fcef82SMichael Große    public function show()
4321fcef82SMichael Große    {
4421fcef82SMichael Große        $searchHTML = '';
4521fcef82SMichael Große
4621fcef82SMichael Große        $searchHTML .= $this->getSearchIntroHTML($this->query);
4721fcef82SMichael Große
482ce8affcSMichael Große        $searchHTML .= $this->getSearchFormHTML($this->query);
492ce8affcSMichael Große
5021fcef82SMichael Große        $searchHTML .= $this->getPageLookupHTML($this->pageLookupResults);
5121fcef82SMichael Große
5221fcef82SMichael Große        $searchHTML .= $this->getFulltextResultsHTML($this->fullTextResults, $this->highlight);
5321fcef82SMichael Große
5421fcef82SMichael Große        echo $searchHTML;
5521fcef82SMichael Große    }
5621fcef82SMichael Große
5721fcef82SMichael Große    /**
58427ed988SMichael Große     * Get a form which can be used to adjust/refine the search
59427ed988SMichael Große     *
60427ed988SMichael Große     * @param string $query
61427ed988SMichael Große     *
62427ed988SMichael Große     * @return string
63427ed988SMichael Große     */
64427ed988SMichael Große    protected function getSearchFormHTML($query)
65427ed988SMichael Große    {
66bbc1da2eSMichael Große        global $lang, $ID, $INPUT;
67427ed988SMichael Große
687fa270bcSMichael Große        $searchForm = (new Form(['method' => 'get'], true))->addClass('search-results-form');
69bb8ef867SMichael Große        $searchForm->setHiddenField('do', 'search');
70d22b78c8SMichael Große        $searchForm->setHiddenField('id', $ID);
711265b193SMichael Große        $searchForm->setHiddenField('sf', '1');
721265b193SMichael Große        if ($INPUT->has('dta')) {
731265b193SMichael Große            $searchForm->setHiddenField('dta', $INPUT->str('dta'));
74bbc1da2eSMichael Große        }
751265b193SMichael Große        if ($INPUT->has('dtb')) {
761265b193SMichael Große            $searchForm->setHiddenField('dtb', $INPUT->str('dtb'));
77bbc1da2eSMichael Große        }
781265b193SMichael Große        if ($INPUT->has('srt')) {
791265b193SMichael Große            $searchForm->setHiddenField('srt', $INPUT->str('srt'));
808d0e286aSMichael Große        }
814bdf82b5SAndreas Gohr        $searchForm->addFieldsetOpen()->addClass('search-form');
82d22b78c8SMichael Große        $searchForm->addTextInput('q')->val($query)->useInput(false);
83427ed988SMichael Große        $searchForm->addButton('', $lang['btn_search'])->attr('type', 'submit');
84bb8ef867SMichael Große
8518856c5dSMichael Große        $this->addSearchAssistanceElements($searchForm);
86bb8ef867SMichael Große
87427ed988SMichael Große        $searchForm->addFieldsetClose();
88427ed988SMichael Große
8916ece95cSMichael Große        trigger_event('FORM_SEARCH_OUTPUT', $searchForm);
9081a0edd9SMichael Große
91427ed988SMichael Große        return $searchForm->toHTML();
92427ed988SMichael Große    }
93427ed988SMichael Große
94*be76738bSMichael Große    /**
95*be76738bSMichael Große     * Add elements to adjust how the results are sorted
96*be76738bSMichael Große     *
97*be76738bSMichael Große     * @param Form $searchForm
98*be76738bSMichael 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
1194bdf82b5SAndreas Gohr        $searchForm->addTagOpen('div')->addClass('toggle');
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
1294bdf82b5SAndreas Gohr        $searchForm->addTagOpen('ul');
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
149*be76738bSMichael Große    /**
150*be76738bSMichael Große     * Check if the query is simple enough to modify its namespace limitations without breaking the rest of the query
151*be76738bSMichael Große     *
152*be76738bSMichael Große     * @param array $parsedQuery
153*be76738bSMichael Große     *
154*be76738bSMichael Große     * @return bool
155*be76738bSMichael 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
164*be76738bSMichael Große    /**
165*be76738bSMichael Große     * Check if the query is simple enough to modify the fragment search behavior without breaking the rest of the query
166*be76738bSMichael Große     *
167*be76738bSMichael Große     * @param array $parsedQuery
168*be76738bSMichael Große     *
169*be76738bSMichael Große     * @return bool
170*be76738bSMichael Große     */
171df977249SMichael Große    protected function isFragmentAssistanceAvailable(array $parsedQuery) {
172df977249SMichael Große        if (preg_match('/[\(\)\|]/', $parsedQuery['query']) === 1) {
173bb8ef867SMichael Große            return false;
174bb8ef867SMichael Große        }
175bb8ef867SMichael Große
176bb8ef867SMichael Große        if (!empty($parsedQuery['phrases'])) {
177bb8ef867SMichael Große            return false;
178bb8ef867SMichael Große        }
179bb8ef867SMichael Große
180bb8ef867SMichael Große        return true;
181bb8ef867SMichael Große    }
182bb8ef867SMichael Große
183bb8ef867SMichael Große    /**
184bb8ef867SMichael Große     * Add the elements to be used for search assistance
185bb8ef867SMichael Große     *
186bb8ef867SMichael Große     * @param Form $searchForm
187bb8ef867SMichael Große     */
18818856c5dSMichael Große    protected function addSearchAssistanceElements(Form $searchForm)
189bb8ef867SMichael Große    {
190c5bd5721SMichael Große        global $lang;
191c5bd5721SMichael Große        $searchForm->addButton('toggleAssistant', $lang['search_toggle_tools'])
192bb8ef867SMichael Große            ->attr('type', 'button')
1934bdf82b5SAndreas Gohr            ->addClass('toggleAssistant');
194bb8ef867SMichael Große
195bb8ef867SMichael Große        $searchForm->addTagOpen('div')
1964bdf82b5SAndreas Gohr            ->addClass('advancedOptions')
197bb8ef867SMichael Große            ->attr('style', 'display: none;');
198bb8ef867SMichael Große
19918856c5dSMichael Große        $this->addFragmentBehaviorLinks($searchForm);
20018856c5dSMichael Große        $this->addNamespaceSelector($searchForm);
20118856c5dSMichael Große        $this->addDateSelector($searchForm);
202b005809cSMichael Große        $this->addSortTool($searchForm);
203bb8ef867SMichael Große
204bb8ef867SMichael Große        $searchForm->addTagClose('div');
205bb8ef867SMichael Große    }
206bb8ef867SMichael Große
207*be76738bSMichael Große    /**
208*be76738bSMichael Große     *  Add the elements to adjust the fragment search behavior
209*be76738bSMichael Große     *
210*be76738bSMichael Große     * @param Form $searchForm
211*be76738bSMichael Große     */
21218856c5dSMichael Große    protected function addFragmentBehaviorLinks(Form $searchForm)
2134d0cb6e1SMichael Große    {
214df977249SMichael Große        if (!$this->isFragmentAssistanceAvailable($this->parsedQuery)) {
215df977249SMichael Große            return;
216df977249SMichael Große        }
217b005809cSMichael Große        global $lang;
2184d0cb6e1SMichael Große
219b005809cSMichael Große        $options = [
220b005809cSMichael Große            'exact' => [
221b005809cSMichael Große                'label' => $lang['search_exact_match'],
222b005809cSMichael Große                'and' => array_map(function ($term) {
223b005809cSMichael Große                    return trim($term, '*');
224b005809cSMichael Große                }, $this->parsedQuery['and']),
225df977249SMichael Große                'not' => array_map(function ($term) {
226df977249SMichael Große                    return trim($term, '*');
227df977249SMichael Große                }, $this->parsedQuery['not']),
228b005809cSMichael Große            ],
229b005809cSMichael Große            'starts' => [
230b005809cSMichael Große                'label' => $lang['search_starts_with'],
231b005809cSMichael Große                'and' => array_map(function ($term) {
232b005809cSMichael Große                    return trim($term, '*') . '*';
233df977249SMichael Große                }, $this->parsedQuery['and']),
234df977249SMichael Große                'not' => array_map(function ($term) {
235df977249SMichael Große                    return trim($term, '*') . '*';
236df977249SMichael Große                }, $this->parsedQuery['not']),
237b005809cSMichael Große            ],
238b005809cSMichael Große            'ends' => [
239b005809cSMichael Große                'label' => $lang['search_ends_with'],
240b005809cSMichael Große                'and' => array_map(function ($term) {
241b005809cSMichael Große                    return '*' . trim($term, '*');
242df977249SMichael Große                }, $this->parsedQuery['and']),
243df977249SMichael Große                'not' => array_map(function ($term) {
244df977249SMichael Große                    return '*' . trim($term, '*');
245df977249SMichael Große                }, $this->parsedQuery['not']),
246b005809cSMichael Große            ],
247b005809cSMichael Große            'contains' => [
248b005809cSMichael Große                'label' => $lang['search_contains'],
249b005809cSMichael Große                'and' => array_map(function ($term) {
250b005809cSMichael Große                    return '*' . trim($term, '*') . '*';
251df977249SMichael Große                }, $this->parsedQuery['and']),
252df977249SMichael Große                'not' => array_map(function ($term) {
253df977249SMichael Große                    return '*' . trim($term, '*') . '*';
254df977249SMichael Große                }, $this->parsedQuery['not']),
255b005809cSMichael Große            ]
256b005809cSMichael Große        ];
257b005809cSMichael Große
258b005809cSMichael Große        // detect current
259c6b5b74aSMichael Große        $activeOption = 'custom';
260b005809cSMichael Große        foreach ($options as $key => $option) {
261b005809cSMichael Große            if ($this->parsedQuery['and'] === $option['and']) {
262b005809cSMichael Große                $activeOption = $key;
263b005809cSMichael Große            }
264b005809cSMichael Große        }
265c6b5b74aSMichael Große        if ($activeOption === 'custom') {
266c6b5b74aSMichael Große            $options = array_merge(['custom' => [
267c6b5b74aSMichael Große                'label' => $lang['search_custom_match'],
268c6b5b74aSMichael Große            ]], $options);
269c6b5b74aSMichael Große        }
270b005809cSMichael Große
2714bdf82b5SAndreas Gohr        $searchForm->addTagOpen('div')->addClass('toggle');
272b005809cSMichael Große        // render current
2734bdf82b5SAndreas Gohr        $currentWrapper = $searchForm->addTagOpen('div')->addClass('current');
274b005809cSMichael Große        if ($activeOption !== 'exact') {
2754bdf82b5SAndreas Gohr            $currentWrapper->addClass('changed');
276b005809cSMichael Große        }
277b005809cSMichael Große        $searchForm->addHTML($options[$activeOption]['label']);
278b005809cSMichael Große        $searchForm->addTagClose('div');
279b005809cSMichael Große
280b005809cSMichael Große        // render options list
2814bdf82b5SAndreas Gohr        $searchForm->addTagOpen('ul');
282b005809cSMichael Große
283b005809cSMichael Große        foreach ($options as $key => $option) {
2844bdf82b5SAndreas Gohr            $listItem = $searchForm->addTagOpen('li');
285b005809cSMichael Große
286b005809cSMichael Große            if ($key === $activeOption) {
2874bdf82b5SAndreas Gohr                $listItem->addClass('active');
288b005809cSMichael Große                $searchForm->addHTML($option['label']);
289b005809cSMichael Große            } else {
29052d4cd42SMichael Große                $link = $this->searchState
29152d4cd42SMichael Große                    ->withFragments($option['and'], $option['not'])
29252d4cd42SMichael Große                    ->getSearchLink($option['label'])
29352d4cd42SMichael Große                ;
29452d4cd42SMichael Große                $searchForm->addHTML($link);
295b005809cSMichael Große            }
296b005809cSMichael Große            $searchForm->addTagClose('li');
297b005809cSMichael Große        }
298b005809cSMichael Große        $searchForm->addTagClose('ul');
2994d0cb6e1SMichael Große
3004d0cb6e1SMichael Große        $searchForm->addTagClose('div');
301b005809cSMichael Große
302b005809cSMichael Große        // render options list
3034d0cb6e1SMichael Große    }
3044d0cb6e1SMichael Große
305bb8ef867SMichael Große    /**
306bb8ef867SMichael Große     * Add the elements for the namespace selector
307bb8ef867SMichael Große     *
308bb8ef867SMichael Große     * @param Form $searchForm
309bb8ef867SMichael Große     */
31018856c5dSMichael Große    protected function addNamespaceSelector(Form $searchForm)
311bb8ef867SMichael Große    {
312df977249SMichael Große        if (!$this->isNamespaceAssistanceAvailable($this->parsedQuery)) {
313df977249SMichael Große            return;
314df977249SMichael Große        }
315df977249SMichael Große
316b005809cSMichael Große        global $lang;
317b005809cSMichael Große
31818856c5dSMichael Große        $baseNS = empty($this->parsedQuery['ns']) ? '' : $this->parsedQuery['ns'][0];
319bbc1da2eSMichael Große        $extraNS = $this->getAdditionalNamespacesFromResults($baseNS);
3204d0cb6e1SMichael Große
3214bdf82b5SAndreas Gohr        $searchForm->addTagOpen('div')->addClass('toggle');
322b005809cSMichael Große        // render current
3234bdf82b5SAndreas Gohr        $currentWrapper = $searchForm->addTagOpen('div')->addClass('current');
324bbc1da2eSMichael Große        if ($baseNS) {
3254bdf82b5SAndreas Gohr            $currentWrapper->addClass('changed');
326b005809cSMichael Große            $searchForm->addHTML('@' . $baseNS);
327b005809cSMichael Große        } else {
328b005809cSMichael Große            $searchForm->addHTML($lang['search_any_ns']);
329b005809cSMichael Große        }
330b005809cSMichael Große        $searchForm->addTagClose('div');
331b005809cSMichael Große
332b005809cSMichael Große        // render options list
3334bdf82b5SAndreas Gohr        $searchForm->addTagOpen('ul');
334b005809cSMichael Große
3354bdf82b5SAndreas Gohr        $listItem = $searchForm->addTagOpen('li');
336b005809cSMichael Große        if ($baseNS) {
3374bdf82b5SAndreas Gohr            $listItem->addClass('active');
33852d4cd42SMichael Große            $link = $this->searchState->withNamespace('')->getSearchLink($lang['search_any_ns']);
33952d4cd42SMichael Große            $searchForm->addHTML($link);
340b005809cSMichael Große        } else {
341b005809cSMichael Große            $searchForm->addHTML($lang['search_any_ns']);
342bb8ef867SMichael Große        }
343b005809cSMichael Große        $searchForm->addTagClose('li');
344bb8ef867SMichael Große
34518856c5dSMichael Große        foreach ($extraNS as $ns => $count) {
3464bdf82b5SAndreas Gohr            $listItem = $searchForm->addTagOpen('li');
34718856c5dSMichael Große            $label = $ns . ($count ? " ($count)" : '');
3484d0cb6e1SMichael Große
349b005809cSMichael Große            if ($ns === $baseNS) {
3504bdf82b5SAndreas Gohr                $listItem->addClass('active');
351b005809cSMichael Große                $searchForm->addHTML($label);
352b005809cSMichael Große            } else {
35352d4cd42SMichael Große                $link = $this->searchState->withNamespace($ns)->getSearchLink($label);
35452d4cd42SMichael Große                $searchForm->addHTML($link);
355bb8ef867SMichael Große            }
356b005809cSMichael Große            $searchForm->addTagClose('li');
357bb8ef867SMichael Große        }
358b005809cSMichael Große        $searchForm->addTagClose('ul');
359bb8ef867SMichael Große
360bb8ef867SMichael Große        $searchForm->addTagClose('div');
361b005809cSMichael Große
362bb8ef867SMichael Große    }
363bb8ef867SMichael Große
364bb8ef867SMichael Große    /**
365bb8ef867SMichael Große     * Parse the full text results for their top namespaces below the given base namespace
366bb8ef867SMichael Große     *
367bb8ef867SMichael Große     * @param string $baseNS the namespace within which was searched, empty string for root namespace
368bb8ef867SMichael Große     *
369bb8ef867SMichael Große     * @return array an associative array with namespace => #number of found pages, sorted descending
370bb8ef867SMichael Große     */
371bb8ef867SMichael Große    protected function getAdditionalNamespacesFromResults($baseNS)
372bb8ef867SMichael Große    {
373bb8ef867SMichael Große        $namespaces = [];
374bb8ef867SMichael Große        $baseNSLength = strlen($baseNS);
375bb8ef867SMichael Große        foreach ($this->fullTextResults as $page => $numberOfHits) {
376bb8ef867SMichael Große            $namespace = getNS($page);
377bb8ef867SMichael Große            if (!$namespace) {
378bb8ef867SMichael Große                continue;
379bb8ef867SMichael Große            }
380bb8ef867SMichael Große            if ($namespace === $baseNS) {
381bb8ef867SMichael Große                continue;
382bb8ef867SMichael Große            }
383bb8ef867SMichael Große            $firstColon = strpos((string)$namespace, ':', $baseNSLength + 1) ?: strlen($namespace);
384bb8ef867SMichael Große            $subtopNS = substr($namespace, 0, $firstColon);
385bb8ef867SMichael Große            if (empty($namespaces[$subtopNS])) {
386bb8ef867SMichael Große                $namespaces[$subtopNS] = 0;
387bb8ef867SMichael Große            }
388bb8ef867SMichael Große            $namespaces[$subtopNS] += 1;
389bb8ef867SMichael Große        }
39055dc8783SMichael Große        ksort($namespaces);
391bb8ef867SMichael Große        arsort($namespaces);
392bb8ef867SMichael Große        return $namespaces;
393bb8ef867SMichael Große    }
394bb8ef867SMichael Große
395bb8ef867SMichael Große    /**
396bbc1da2eSMichael Große     * @ToDo: custom date input
397bbc1da2eSMichael Große     *
398bbc1da2eSMichael Große     * @param Form $searchForm
399bbc1da2eSMichael Große     */
400b005809cSMichael Große    protected function addDateSelector(Form $searchForm)
401b005809cSMichael Große    {
402b005809cSMichael Große        global $INPUT, $lang;
403bbc1da2eSMichael Große
404b005809cSMichael Große        $options = [
405b005809cSMichael Große            'any' => [
406b005809cSMichael Große                'before' => false,
407b005809cSMichael Große                'after' => false,
408b005809cSMichael Große                'label' => $lang['search_any_time'],
409b005809cSMichael Große            ],
410b005809cSMichael Große            'week' => [
411b005809cSMichael Große                'before' => false,
412b005809cSMichael Große                'after' => '1 week ago',
413b005809cSMichael Große                'label' => $lang['search_past_7_days'],
414b005809cSMichael Große            ],
415b005809cSMichael Große            'month' => [
416b005809cSMichael Große                'before' => false,
417b005809cSMichael Große                'after' => '1 month ago',
418b005809cSMichael Große                'label' => $lang['search_past_month'],
419b005809cSMichael Große            ],
420b005809cSMichael Große            'year' => [
421b005809cSMichael Große                'before' => false,
422b005809cSMichael Große                'after' => '1 year ago',
423b005809cSMichael Große                'label' => $lang['search_past_year'],
424b005809cSMichael Große            ],
425b005809cSMichael Große        ];
426b005809cSMichael Große        $activeOption = 'any';
427b005809cSMichael Große        foreach ($options as $key => $option) {
4281265b193SMichael Große            if ($INPUT->str('dta') === $option['after']) {
429b005809cSMichael Große                $activeOption = $key;
430b005809cSMichael Große                break;
431b005809cSMichael Große            }
432b005809cSMichael Große        }
433b005809cSMichael Große
4344bdf82b5SAndreas Gohr        $searchForm->addTagOpen('div')->addClass('toggle');
435b005809cSMichael Große        // render current
4364bdf82b5SAndreas Gohr        $currentWrapper = $searchForm->addTagOpen('div')->addClass('current');
4371265b193SMichael Große        if ($INPUT->has('dtb') || $INPUT->has('dta')) {
4384bdf82b5SAndreas Gohr            $currentWrapper->addClass('changed');
439bbc1da2eSMichael Große        }
440b005809cSMichael Große        $searchForm->addHTML($options[$activeOption]['label']);
441b005809cSMichael Große        $searchForm->addTagClose('div');
442bbc1da2eSMichael Große
443b005809cSMichael Große        // render options list
4444bdf82b5SAndreas Gohr        $searchForm->addTagOpen('ul');
445b005809cSMichael Große
446b005809cSMichael Große        foreach ($options as $key => $option) {
4474bdf82b5SAndreas Gohr            $listItem = $searchForm->addTagOpen('li');
448b005809cSMichael Große
449b005809cSMichael Große            if ($key === $activeOption) {
4504bdf82b5SAndreas Gohr                $listItem->addClass('active');
451b005809cSMichael Große                $searchForm->addHTML($option['label']);
452bbc1da2eSMichael Große            } else {
45352d4cd42SMichael Große                $link = $this->searchState
45452d4cd42SMichael Große                    ->withTimeLimitations($option['after'], $option['before'])
45552d4cd42SMichael Große                    ->getSearchLink($option['label'])
45652d4cd42SMichael Große                ;
45752d4cd42SMichael Große                $searchForm->addHTML($link);
458bbc1da2eSMichael Große            }
459b005809cSMichael Große            $searchForm->addTagClose('li');
460bbc1da2eSMichael Große        }
461b005809cSMichael Große        $searchForm->addTagClose('ul');
462bbc1da2eSMichael Große
463bbc1da2eSMichael Große        $searchForm->addTagClose('div');
464bbc1da2eSMichael Große    }
465bbc1da2eSMichael Große
466bbc1da2eSMichael Große
467bbc1da2eSMichael Große    /**
46821fcef82SMichael Große     * Build the intro text for the search page
46921fcef82SMichael Große     *
47021fcef82SMichael Große     * @param string $query the search query
47121fcef82SMichael Große     *
47221fcef82SMichael Große     * @return string
47321fcef82SMichael Große     */
47421fcef82SMichael Große    protected function getSearchIntroHTML($query)
47521fcef82SMichael Große    {
4762ce8affcSMichael Große        global $lang;
47721fcef82SMichael Große
47821fcef82SMichael Große        $intro = p_locale_xhtml('searchpage');
4792ce8affcSMichael Große
4802ce8affcSMichael Große        $queryPagename = $this->createPagenameFromQuery($this->parsedQuery);
4812ce8affcSMichael Große        $createQueryPageLink = html_wikilink($queryPagename . '?do=edit', $queryPagename);
4822ce8affcSMichael Große
4832ce8affcSMichael Große        $pagecreateinfo = '';
4842ce8affcSMichael Große        if (auth_quickaclcheck($queryPagename) >= AUTH_CREATE) {
4852ce8affcSMichael Große            $pagecreateinfo = sprintf($lang['searchcreatepage'], $createQueryPageLink);
4862ce8affcSMichael Große        }
48721fcef82SMichael Große        $intro = str_replace(
48821fcef82SMichael Große            array('@QUERY@', '@SEARCH@', '@CREATEPAGEINFO@'),
48921fcef82SMichael Große            array(hsc(rawurlencode($query)), hsc($query), $pagecreateinfo),
49021fcef82SMichael Große            $intro
49121fcef82SMichael Große        );
4922ce8affcSMichael Große
49321fcef82SMichael Große        return $intro;
49421fcef82SMichael Große    }
49521fcef82SMichael Große
49621fcef82SMichael Große    /**
4972ce8affcSMichael Große     * Create a pagename based the parsed search query
4982ce8affcSMichael Große     *
4992ce8affcSMichael Große     * @param array $parsedQuery
5002ce8affcSMichael Große     *
5012ce8affcSMichael Große     * @return string pagename constructed from the parsed query
5022ce8affcSMichael Große     */
5032ce8affcSMichael Große    protected function createPagenameFromQuery($parsedQuery)
5042ce8affcSMichael Große    {
5052ce8affcSMichael Große        $pagename = '';
5062ce8affcSMichael Große        if (!empty($parsedQuery['ns'])) {
5072ce8affcSMichael Große            $pagename .= cleanID($parsedQuery['ns'][0]);
5082ce8affcSMichael Große        }
5092ce8affcSMichael Große        $pagename .= ':' . cleanID(implode(' ' , $parsedQuery['highlight']));
5102ce8affcSMichael Große        return $pagename;
5112ce8affcSMichael Große    }
5122ce8affcSMichael Große
5132ce8affcSMichael Große    /**
51421fcef82SMichael Große     * Build HTML for a list of pages with matching pagenames
51521fcef82SMichael Große     *
51621fcef82SMichael Große     * @param array $data search results
51721fcef82SMichael Große     *
51821fcef82SMichael Große     * @return string
51921fcef82SMichael Große     */
52021fcef82SMichael Große    protected function getPageLookupHTML($data)
52121fcef82SMichael Große    {
52221fcef82SMichael Große        if (empty($data)) {
52321fcef82SMichael Große            return '';
52421fcef82SMichael Große        }
52521fcef82SMichael Große
52621fcef82SMichael Große        global $lang;
52721fcef82SMichael Große
52821fcef82SMichael Große        $html = '<div class="search_quickresult">';
52921fcef82SMichael Große        $html .= '<h3>' . $lang['quickhits'] . ':</h3>';
53021fcef82SMichael Große        $html .= '<ul class="search_quickhits">';
53121fcef82SMichael Große        foreach ($data as $id => $title) {
5324eab6f7cSMichael Große            $link = html_wikilink(':' . $id);
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">';
5652ce8affcSMichael Große        $html .= '<h3>' . $lang['search_fullresults'] . ':</h3>';
5662ce8affcSMichael Große
56721fcef82SMichael Große        $html .= '<dl class="search_results">';
56821fcef82SMichael Große        $num = 1;
5694c924eb8SMichael Große
57021fcef82SMichael Große        foreach ($data as $id => $cnt) {
5714eab6f7cSMichael Große            $resultLink = html_wikilink(':' . $id, null, $highlight);
5724c924eb8SMichael Große
5734c924eb8SMichael Große            $resultHeader = [$resultLink];
5744c924eb8SMichael Große
5754eab6f7cSMichael Große
5764c924eb8SMichael Große            $restrictQueryToNSLink = $this->restrictQueryToNSLink(getNS($id));
5774c924eb8SMichael Große            if ($restrictQueryToNSLink) {
5784c924eb8SMichael Große                $resultHeader[] = $restrictQueryToNSLink;
5794c924eb8SMichael Große            }
5804c924eb8SMichael Große
5819a75abfbSMichael Große            $snippet = '';
5829a75abfbSMichael Große            $lastMod = '';
5839a75abfbSMichael Große            $mtime = filemtime(wikiFN($id));
5849a75abfbSMichael Große            if ($cnt !== 0) {
5859a75abfbSMichael Große                $resultHeader[] = $cnt . ' ' . $lang['hits'];
5869a75abfbSMichael Große                if ($num < FT_SNIPPET_NUMBER) { // create snippets for the first number of matches only
5879a75abfbSMichael Große                    $snippet = '<dd>' . ft_snippet($id, $highlight) . '</dd>';
5889a75abfbSMichael Große                    $lastMod = '<span class="search_results__lastmod">' . $lang['lastmod'] . ' ';
5899a75abfbSMichael Große                    $lastMod .= '<time datetime="' . date_iso8601($mtime) . '">' . dformat($mtime) . '</time>';
5909a75abfbSMichael Große                    $lastMod .= '</span>';
5919a75abfbSMichael Große                }
5929a75abfbSMichael Große                $num++;
5939a75abfbSMichael Große            }
5949a75abfbSMichael Große
5959a75abfbSMichael Große            $metaLine = '<div class="search_results__metaLine">';
5969a75abfbSMichael Große            $metaLine .= $lastMod;
5979a75abfbSMichael Große            $metaLine .= '</div>';
5989a75abfbSMichael Große
5999a75abfbSMichael Große
6004eab6f7cSMichael Große            $eventData = [
6014c924eb8SMichael Große                'resultHeader' => $resultHeader,
6029a75abfbSMichael Große                'resultBody' => [$metaLine, $snippet],
6034eab6f7cSMichael Große                'page' => $id,
6044eab6f7cSMichael Große            ];
6054eab6f7cSMichael Große            trigger_event('SEARCH_RESULT_FULLPAGE', $eventData);
6064eab6f7cSMichael Große            $html .= '<div class="search_fullpage_result">';
6074eab6f7cSMichael Große            $html .= '<dt>' . implode(' ', $eventData['resultHeader']) . '</dt>';
6084eab6f7cSMichael Große            $html .= implode('', $eventData['resultBody']);
6094eab6f7cSMichael Große            $html .= '</div>';
61021fcef82SMichael Große        }
61121fcef82SMichael Große        $html .= '</dl>';
61221fcef82SMichael Große
6132ce8affcSMichael Große        $html .= '</div>';
6142ce8affcSMichael Große
61521fcef82SMichael Große        return $html;
61621fcef82SMichael Große    }
6174c924eb8SMichael Große
6184c924eb8SMichael Große    /**
6194c924eb8SMichael Große     * create a link to restrict the current query to a namespace
6204c924eb8SMichael Große     *
6214c924eb8SMichael Große     * @param bool|string $ns the namespace to which to restrict the query
6224c924eb8SMichael Große     *
6234c924eb8SMichael Große     * @return bool|string
6244c924eb8SMichael Große     */
6254c924eb8SMichael Große    protected function restrictQueryToNSLink($ns)
6264c924eb8SMichael Große    {
6274c924eb8SMichael Große        if (!$ns) {
6284c924eb8SMichael Große            return false;
6294c924eb8SMichael Große        }
630df977249SMichael Große        if (!$this->isNamespaceAssistanceAvailable($this->parsedQuery)) {
6314c924eb8SMichael Große            return false;
6324c924eb8SMichael Große        }
6334c924eb8SMichael Große        if (!empty($this->parsedQuery['ns']) && $this->parsedQuery['ns'][0] === $ns) {
6344c924eb8SMichael Große            return false;
6354c924eb8SMichael Große        }
63652d4cd42SMichael Große
6374c924eb8SMichael Große        $name = '@' . $ns;
63852d4cd42SMichael Große        return $this->searchState->withNamespace($ns)->getSearchLink($name);
6394c924eb8SMichael Große    }
64021fcef82SMichael Große}
641