xref: /dokuwiki/inc/Ui/Search.php (revision d09b5b6441fa2464fba24f84eff22348b879676c)
1<?php
2
3namespace dokuwiki\Ui;
4
5use \dokuwiki\Form\Form;
6
7class Search extends Ui
8{
9    protected $query;
10    protected $parsedQuery;
11    protected $pageLookupResults = array();
12    protected $fullTextResults = array();
13    protected $highlight = array();
14
15    /**
16     * Search constructor.
17     *
18     * @param string $query the search query
19     */
20    public function __construct()
21    {
22        global $QUERY;
23
24        $Indexer = idx_get_indexer();
25        $parsedQuery = ft_queryParser($Indexer, $QUERY);
26
27        $this->query = $QUERY;
28        $this->parsedQuery = $parsedQuery;
29    }
30
31    /**
32     * run the search
33     */
34    public function execute()
35    {
36        $this->pageLookupResults = ft_pageLookup($this->query, true, useHeading('navigation'));
37        $this->fullTextResults = ft_pageSearch($this->query, $highlight);
38        $this->highlight = $highlight;
39    }
40
41    /**
42     * display the search result
43     *
44     * @return void
45     */
46    public function show()
47    {
48        $searchHTML = '';
49
50        $searchHTML .= $this->getSearchFormHTML($this->query);
51
52        $searchHTML .= $this->getSearchIntroHTML($this->query);
53
54        $searchHTML .= $this->getPageLookupHTML($this->pageLookupResults);
55
56        $searchHTML .= $this->getFulltextResultsHTML($this->fullTextResults, $this->highlight);
57
58        echo $searchHTML;
59    }
60
61    /**
62     * Get a form which can be used to adjust/refine the search
63     *
64     * @param string $query
65     *
66     * @return string
67     */
68    protected function getSearchFormHTML($query)
69    {
70        global $lang, $ID;
71
72        $searchForm = (new Form())->attrs(['method' => 'get'])->addClass('search-results-form');
73        $searchForm->setHiddenField('do', 'search');
74        $searchForm->setHiddenField('from', $ID);
75        $searchForm->setHiddenField('searchPageForm', '1');
76        $searchForm->addFieldsetOpen()->addClass('search-results-form__fieldset');
77        $searchForm->addTextInput('id')->val($query)->useInput(false);
78        $searchForm->addButton('', $lang['btn_search'])->attr('type', 'submit');
79
80        if ($this->isSearchAssistanceAvailable($this->parsedQuery)) {
81            $this->addSearchAssistanceElements($searchForm, $this->parsedQuery);
82        } else {
83            $searchForm->addClass('search-results-form--no-assistance');
84            $searchForm->addTagOpen('span')->addClass('search-results-form__no-assistance-message');
85            $searchForm->addHTML('FIXME Your query is too complex. Search assistance is unavailable. See <a href="https://doku.wiki/search">doku.wiki/search</a> for more help.');
86            $searchForm->addTagClose('span');
87        }
88
89        $searchForm->addFieldsetClose();
90
91        trigger_event('SEARCH_FORM_DISPLAY', $searchForm);
92
93        return $searchForm->toHTML();
94    }
95
96    /**
97     * Decide if the given query is simple enough to provide search assistance
98     *
99     * @param array $parsedQuery
100     *
101     * @return bool
102     */
103    protected function isSearchAssistanceAvailable(array $parsedQuery)
104    {
105        if (count($parsedQuery['words']) > 1) {
106            return false;
107        }
108        if (!empty($parsedQuery['not'])) {
109            return false;
110        }
111
112        if (!empty($parsedQuery['phrases'])) {
113            return false;
114        }
115
116        if (!empty($parsedQuery['notns'])) {
117            return false;
118        }
119        if (count($parsedQuery['ns']) > 1) {
120            return false;
121        }
122
123        return true;
124    }
125
126    /**
127     * Add the elements to be used for search assistance
128     *
129     * @param Form  $searchForm
130     * @param array $parsedQuery
131     */
132    protected function addSearchAssistanceElements(Form $searchForm, array $parsedQuery)
133    {
134        $matchType = '';
135        $searchTerm = null;
136        if (count($parsedQuery['words']) === 1) {
137            $searchTerm = $parsedQuery['words'][0];
138            $firstChar = $searchTerm[0];
139            $lastChar = substr($searchTerm, -1);
140            $matchType = 'exact';
141
142            if ($firstChar === '*') {
143                $matchType = 'starts';
144            }
145            if ($lastChar === '*') {
146                $matchType = 'ends';
147            }
148            if ($firstChar === '*' && $lastChar === '*') {
149                $matchType = 'contains';
150            }
151            $searchTerm = trim($searchTerm, '*');
152        }
153
154        $searchForm->addTextInput(
155            'searchTerm',
156            '',
157            $searchForm->findPositionByAttribute('type', 'submit')
158        )
159            ->val($searchTerm)
160            ->attr('style', 'display: none;');
161        $searchForm->addButton('toggleAssistant', 'toggle search assistant')
162            ->attr('type', 'button')
163            ->id('search-results-form__show-assistance-button')
164            ->addClass('search-results-form__show-assistance-button');
165
166        $searchForm->addTagOpen('div')
167            ->addClass('js-advancedSearchOptions')
168            ->attr('style', 'display: none;');
169
170        $searchForm->addTagOpen('div')->addClass('search-results-form__subwrapper');
171        $searchForm->addRadioButton('matchType', 'exact Match FM')->val('exact')->attr('checked',
172            $matchType === 'exact' ?: null);
173        $searchForm->addRadioButton('matchType', 'starts with FM')->val('starts')->attr('checked',
174            $matchType === 'starts' ?: null);
175        $searchForm->addRadioButton('matchType', 'ends with FM')->val('ends')->attr('checked',
176            $matchType === 'ends' ?: null);
177        $searchForm->addRadioButton('matchType', 'contains FM')->val('contains')->attr('checked',
178            $matchType === 'contains' ?: null);
179        $searchForm->addTagClose('div');
180
181        $this->addNamespaceSelector($searchForm, $parsedQuery);
182
183        $searchForm->addTagClose('div');
184    }
185
186    /**
187     * Add the elements for the namespace selector
188     *
189     * @param Form  $searchForm
190     * @param array $parsedQuery
191     */
192    protected function addNamespaceSelector(Form $searchForm, array $parsedQuery)
193    {
194        $baseNS = empty($parsedQuery['ns']) ? '' : $parsedQuery['ns'][0];
195        $namespaces = [];
196        $searchForm->addTagOpen('div')->addClass('search-results-form__subwrapper');
197        if ($baseNS) {
198            $searchForm->addRadioButton('namespace', '(no namespace FIXME)')->val('');
199            $parts = [$baseNS => count($this->fullTextResults)];
200            $upperNameSpace = $baseNS;
201            while ($upperNameSpace = getNS($upperNameSpace)) {
202                $parts[$upperNameSpace] = 0;
203            }
204            $namespaces = array_reverse($parts);
205        };
206
207        $namespaces = array_merge($namespaces, $this->getAdditionalNamespacesFromResults($baseNS));
208
209        foreach ($namespaces as $extraNS => $count) {
210            $label = $extraNS . ($count ? " ($count)" : '');
211            $namespaceCB = $searchForm->addRadioButton('namespace', $label)->val($extraNS);
212            if ($extraNS === $baseNS) {
213                $namespaceCB->attr('checked', true);
214            }
215        }
216
217        $searchForm->addTagClose('div');
218    }
219
220    /**
221     * Parse the full text results for their top namespaces below the given base namespace
222     *
223     * @param string $baseNS the namespace within which was searched, empty string for root namespace
224     *
225     * @return array an associative array with namespace => #number of found pages, sorted descending
226     */
227    protected function getAdditionalNamespacesFromResults($baseNS)
228    {
229        $namespaces = [];
230        $baseNSLength = strlen($baseNS);
231        foreach ($this->fullTextResults as $page => $numberOfHits) {
232            $namespace = getNS($page);
233            if (!$namespace) {
234                continue;
235            }
236            if ($namespace === $baseNS) {
237                continue;
238            }
239            $firstColon = strpos((string)$namespace, ':', $baseNSLength + 1) ?: strlen($namespace);
240            $subtopNS = substr($namespace, 0, $firstColon);
241            if (empty($namespaces[$subtopNS])) {
242                $namespaces[$subtopNS] = 0;
243            }
244            $namespaces[$subtopNS] += 1;
245        }
246        arsort($namespaces);
247        return $namespaces;
248    }
249
250    /**
251     * Build the intro text for the search page
252     *
253     * @param string $query the search query
254     *
255     * @return string
256     */
257    protected function getSearchIntroHTML($query)
258    {
259        global $ID, $lang;
260
261        $intro = p_locale_xhtml('searchpage');
262        // allow use of placeholder in search intro
263        $pagecreateinfo = (auth_quickaclcheck($ID) >= AUTH_CREATE) ? $lang['searchcreatepage'] : '';
264        $intro = str_replace(
265            array('@QUERY@', '@SEARCH@', '@CREATEPAGEINFO@'),
266            array(hsc(rawurlencode($query)), hsc($query), $pagecreateinfo),
267            $intro
268        );
269        return $intro;
270    }
271
272    /**
273     * Build HTML for a list of pages with matching pagenames
274     *
275     * @param array $data search results
276     *
277     * @return string
278     */
279    protected function getPageLookupHTML($data)
280    {
281        if (empty($data)) {
282            return '';
283        }
284
285        global $lang;
286
287        $html = '<div class="search_quickresult">';
288        $html .= '<h3>' . $lang['quickhits'] . ':</h3>';
289        $html .= '<ul class="search_quickhits">';
290        foreach ($data as $id => $title) {
291            $link = html_wikilink(':' . $id);
292            $eventData = [
293                'listItemContent' => [$link],
294                'page' => $id,
295            ];
296            trigger_event('SEARCH_RESULT_PAGELOOKUP', $eventData);
297            $html .= '<li>' . implode('', $eventData['listItemContent']) . '</li>';
298        }
299        $html .= '</ul> ';
300        //clear float (see http://www.complexspiral.com/publications/containing-floats/)
301        $html .= '<div class="clearer"></div>';
302        $html .= '</div>';
303
304        return $html;
305    }
306
307    /**
308     * Build HTML for fulltext search results or "no results" message
309     *
310     * @param array $data      the results of the fulltext search
311     * @param array $highlight the terms to be highlighted in the results
312     *
313     * @return string
314     */
315    protected function getFulltextResultsHTML($data, $highlight)
316    {
317        global $lang;
318
319        if (empty($data)) {
320            return '<div class="nothing">' . $lang['nothingfound'] . '</div>';
321        }
322
323        $html = '';
324        $html .= '<dl class="search_results">';
325        $num = 1;
326
327        foreach ($data as $id => $cnt) {
328            $resultLink = html_wikilink(':' . $id, null, $highlight);
329
330            $resultHeader = [$resultLink];
331
332            $snippet = '';
333            if ($cnt !== 0) {
334                $resultHeader[] = $cnt . ' ' . $lang['hits'];
335                if ($num < FT_SNIPPET_NUMBER) { // create snippets for the first number of matches only
336                    $snippet = '<dd>' . ft_snippet($id, $highlight) . '</dd>';
337                }
338                $num++;
339            }
340
341            $restrictQueryToNSLink = $this->restrictQueryToNSLink(getNS($id));
342            if ($restrictQueryToNSLink) {
343                $resultHeader[] = $restrictQueryToNSLink;
344            }
345
346            $eventData = [
347                'resultHeader' => $resultHeader,
348                'resultBody' => [$snippet],
349                'page' => $id,
350            ];
351            trigger_event('SEARCH_RESULT_FULLPAGE', $eventData);
352            $html .= '<div class="search_fullpage_result">';
353            $html .= '<dt>' . implode(' ', $eventData['resultHeader']) . '</dt>';
354            $html .= implode('', $eventData['resultBody']);
355            $html .= '</div>';
356        }
357        $html .= '</dl>';
358
359        return $html;
360    }
361
362    /**
363     * create a link to restrict the current query to a namespace
364     *
365     * @param bool|string $ns the namespace to which to restrict the query
366     *
367     * @return bool|string
368     */
369    protected function restrictQueryToNSLink($ns)
370    {
371        if (!$ns) {
372            return false;
373        }
374        if (!$this->isSearchAssistanceAvailable($this->parsedQuery)) {
375            return false;
376        }
377        if (!empty($this->parsedQuery['ns']) && $this->parsedQuery['ns'][0] === $ns) {
378            return false;
379        }
380
381        $newQuery = ft_queryUnparser_simple(
382            $this->parsedQuery['and'],
383            [],
384            [],
385            [$ns],
386            []
387        );
388        $href = wl($newQuery, ['do' => 'search']);
389        $attributes = buildAttributes([
390            'rel' => 'nofollow',
391            'class' => 'search_namespace_link',
392        ]);
393        $name = '@' . $ns;
394        return "<a href=\"$href\" $attributes>$name</a>";
395    }
396}
397