query = $QUERY; $this->parsedQuery = ft_queryParser($Indexer, $QUERY); $this->searchState = new SearchState($this->parsedQuery); $this->pageLookupResults = $pageLookupResults; $this->fullTextResults = $fullTextResults; $this->highlight = $highlight; } /** * display the search result * * @return void */ public function show() { $searchHTML = ''; $searchHTML .= $this->getSearchFormHTML($this->query); $searchHTML .= $this->getSearchIntroHTML($this->query); $searchHTML .= $this->getPageLookupHTML($this->pageLookupResults); $searchHTML .= $this->getFulltextResultsHTML($this->fullTextResults, $this->highlight); echo $searchHTML; } /** * Get a form which can be used to adjust/refine the search * * @param string $query * * @return string */ protected function getSearchFormHTML($query) { global $lang, $ID, $INPUT; $searchForm = (new Form())->attrs(['method' => 'get'])->addClass('search-results-form'); $searchForm->setHiddenField('do', 'search'); $searchForm->setHiddenField('id', $ID); $searchForm->setHiddenField('searchPageForm', '1'); if ($INPUT->has('after')) { $searchForm->setHiddenField('after', $INPUT->str('after')); } if ($INPUT->has('before')) { $searchForm->setHiddenField('before', $INPUT->str('before')); } if ($INPUT->has('sort')) { $searchForm->setHiddenField('sort', $INPUT->str('sort')); } $searchForm->addFieldsetOpen()->addClass('search-results-form__fieldset'); $searchForm->addTextInput('q')->val($query)->useInput(false); $searchForm->addButton('', $lang['btn_search'])->attr('type', 'submit'); if ($this->isSearchAssistanceAvailable($this->parsedQuery)) { $this->addSearchAssistanceElements($searchForm); } else { $searchForm->addClass('search-results-form--no-assistance'); $searchForm->addTagOpen('span')->addClass('search-results-form__no-assistance-message'); $searchForm->addHTML('FIXME Your query is too complex. Search assistance is unavailable. See doku.wiki/search for more help.'); $searchForm->addTagClose('span'); } $searchForm->addFieldsetClose(); trigger_event('SEARCH_FORM_DISPLAY', $searchForm); return $searchForm->toHTML(); } protected function addSortTool(Form $searchForm) { global $INPUT, $lang; $options = [ 'hits' => [ 'label' => $lang['search_sort_by_hits'], 'sort' => '', ], 'mtime' => [ 'label' => $lang['search_sort_by_mtime'], 'sort' => 'mtime', ], ]; $activeOption = 'hits'; if ($INPUT->str('sort') === 'mtime') { $activeOption = 'mtime'; } $searchForm->addTagOpen('div')->addClass('search-tool js-search-tool'); // render current $currentWrapper = $searchForm->addTagOpen('div')->addClass('search-tool__current js-current'); if ($activeOption !== 'hits') { $currentWrapper->addClass('search-tool__current--changed'); } $searchForm->addHTML($options[$activeOption]['label']); $searchForm->addTagClose('div'); // render options list $searchForm->addTagOpen('ul')->addClass('search-tool__options-list js-optionsList'); foreach ($options as $key => $option) { $listItem = $searchForm->addTagOpen('li')->addClass('search-tool__options-list-item'); if ($key === $activeOption) { $listItem->addClass('search-tool__options-list-item--active'); $searchForm->addHTML($option['label']); } else { $this->searchState->addSearchLinkSort( $searchForm, $option['label'], $option['sort'] ); } $searchForm->addTagClose('li'); } $searchForm->addTagClose('ul'); $searchForm->addTagClose('div'); } /** * Decide if the given query is simple enough to provide search assistance * * @param array $parsedQuery * * @return bool */ protected function isSearchAssistanceAvailable(array $parsedQuery) { if (count($parsedQuery['words']) > 1) { return false; } if (!empty($parsedQuery['not'])) { return false; } if (!empty($parsedQuery['phrases'])) { return false; } if (!empty($parsedQuery['notns'])) { return false; } if (count($parsedQuery['ns']) > 1) { return false; } return true; } /** * Add the elements to be used for search assistance * * @param Form $searchForm */ protected function addSearchAssistanceElements(Form $searchForm) { $searchForm->addButton('toggleAssistant', 'toggle search assistant') ->attr('type', 'button') ->id('search-results-form__show-assistance-button') ->addClass('search-results-form__show-assistance-button'); $searchForm->addTagOpen('div') ->addClass('js-advancedSearchOptions') ->attr('style', 'display: none;'); $this->addFragmentBehaviorLinks($searchForm); $this->addNamespaceSelector($searchForm); $this->addDateSelector($searchForm); $this->addSortTool($searchForm); $searchForm->addTagClose('div'); } protected function addFragmentBehaviorLinks(Form $searchForm) { global $lang; $options = [ 'exact' => [ 'label' => $lang['search_exact_match'], 'and' => array_map(function ($term) { return trim($term, '*'); }, $this->parsedQuery['and']), ], 'starts' => [ 'label' => $lang['search_starts_with'], 'and' => array_map(function ($term) { return trim($term, '*') . '*'; }, $this->parsedQuery['and']) ], 'ends' => [ 'label' => $lang['search_ends_with'], 'and' => array_map(function ($term) { return '*' . trim($term, '*'); }, $this->parsedQuery['and']) ], 'contains' => [ 'label' => $lang['search_contains'], 'and' => array_map(function ($term) { return '*' . trim($term, '*') . '*'; }, $this->parsedQuery['and']) ] ]; // detect current $activeOption = 'exact'; foreach ($options as $key => $option) { if ($this->parsedQuery['and'] === $option['and']) { $activeOption = $key; } } $searchForm->addTagOpen('div')->addClass('search-tool js-search-tool'); // render current $currentWrapper = $searchForm->addTagOpen('div')->addClass('search-tool__current js-current'); if ($activeOption !== 'exact') { $currentWrapper->addClass('search-tool__current--changed'); } $searchForm->addHTML($options[$activeOption]['label']); $searchForm->addTagClose('div'); // render options list $searchForm->addTagOpen('ul')->addClass('search-tool__options-list js-optionsList'); foreach ($options as $key => $option) { $listItem = $searchForm->addTagOpen('li')->addClass('search-tool__options-list-item'); if ($key === $activeOption) { $listItem->addClass('search-tool__options-list-item--active'); $searchForm->addHTML($option['label']); } else { $this->searchState->addSearchLinkFragment( $searchForm, $option['label'], $option['and'] ); } $searchForm->addTagClose('li'); } $searchForm->addTagClose('ul'); $searchForm->addTagClose('div'); // render options list } /** * Add the elements for the namespace selector * * @param Form $searchForm */ protected function addNamespaceSelector(Form $searchForm) { global $lang; $baseNS = empty($this->parsedQuery['ns']) ? '' : $this->parsedQuery['ns'][0]; $extraNS = $this->getAdditionalNamespacesFromResults($baseNS); $searchForm->addTagOpen('div')->addClass('search-tool js-search-tool'); // render current $currentWrapper = $searchForm->addTagOpen('div')->addClass('search-tool__current js-current'); if ($baseNS) { $currentWrapper->addClass('search-tool__current--changed'); $searchForm->addHTML('@' . $baseNS); } else { $searchForm->addHTML($lang['search_any_ns']); } $searchForm->addTagClose('div'); // render options list $searchForm->addTagOpen('ul')->addClass('search-tool__options-list js-optionsList'); $listItem = $searchForm->addTagOpen('li')->addClass('search-tool__options-list-item'); if ($baseNS) { $listItem->addClass('search-tool__options-list-item--active'); $this->searchState->addSeachLinkNS( $searchForm, $lang['search_any_ns'], '' ); } else { $searchForm->addHTML($lang['search_any_ns']); } $searchForm->addTagClose('li'); foreach ($extraNS as $ns => $count) { $listItem = $searchForm->addTagOpen('li')->addClass('search-tool__options-list-item'); $label = $ns . ($count ? " ($count)" : ''); if ($ns === $baseNS) { $listItem->addClass('search-tool__options-list-item--active'); $searchForm->addHTML($label); } else { $this->searchState->addSeachLinkNS( $searchForm, $label, $ns ); } $searchForm->addTagClose('li'); } $searchForm->addTagClose('ul'); $searchForm->addTagClose('div'); } /** * Parse the full text results for their top namespaces below the given base namespace * * @param string $baseNS the namespace within which was searched, empty string for root namespace * * @return array an associative array with namespace => #number of found pages, sorted descending */ protected function getAdditionalNamespacesFromResults($baseNS) { $namespaces = []; $baseNSLength = strlen($baseNS); foreach ($this->fullTextResults as $page => $numberOfHits) { $namespace = getNS($page); if (!$namespace) { continue; } if ($namespace === $baseNS) { continue; } $firstColon = strpos((string)$namespace, ':', $baseNSLength + 1) ?: strlen($namespace); $subtopNS = substr($namespace, 0, $firstColon); if (empty($namespaces[$subtopNS])) { $namespaces[$subtopNS] = 0; } $namespaces[$subtopNS] += 1; } arsort($namespaces); return $namespaces; } /** * @ToDo: custom date input * * @param Form $searchForm */ protected function addDateSelector(Form $searchForm) { global $INPUT, $lang; $options = [ 'any' => [ 'before' => false, 'after' => false, 'label' => $lang['search_any_time'], ], 'week' => [ 'before' => false, 'after' => '1 week ago', 'label' => $lang['search_past_7_days'], ], 'month' => [ 'before' => false, 'after' => '1 month ago', 'label' => $lang['search_past_month'], ], 'year' => [ 'before' => false, 'after' => '1 year ago', 'label' => $lang['search_past_year'], ], ]; $activeOption = 'any'; foreach ($options as $key => $option) { if ($INPUT->str('after') === $option['after']) { $activeOption = $key; break; } } $searchForm->addTagOpen('div')->addClass('search-tool js-search-tool'); // render current $currentWrapper = $searchForm->addTagOpen('div')->addClass('search-tool__current js-current'); if ($INPUT->has('before') || $INPUT->has('after')) { $currentWrapper->addClass('search-tool__current--changed'); } $searchForm->addHTML($options[$activeOption]['label']); $searchForm->addTagClose('div'); // render options list $searchForm->addTagOpen('ul')->addClass('search-tool__options-list js-optionsList'); foreach ($options as $key => $option) { $listItem = $searchForm->addTagOpen('li')->addClass('search-tool__options-list-item'); if ($key === $activeOption) { $listItem->addClass('search-tool__options-list-item--active'); $searchForm->addHTML($option['label']); } else { $this->searchState->addSearchLinkTime( $searchForm, $option['label'], $option['after'], $option['before'] ); } $searchForm->addTagClose('li'); } $searchForm->addTagClose('ul'); $searchForm->addTagClose('div'); } /** * Build the intro text for the search page * * @param string $query the search query * * @return string */ protected function getSearchIntroHTML($query) { global $ID, $lang; $intro = p_locale_xhtml('searchpage'); // allow use of placeholder in search intro $pagecreateinfo = (auth_quickaclcheck($ID) >= AUTH_CREATE) ? $lang['searchcreatepage'] : ''; $intro = str_replace( array('@QUERY@', '@SEARCH@', '@CREATEPAGEINFO@'), array(hsc(rawurlencode($query)), hsc($query), $pagecreateinfo), $intro ); return $intro; } /** * Build HTML for a list of pages with matching pagenames * * @param array $data search results * * @return string */ protected function getPageLookupHTML($data) { if (empty($data)) { return ''; } global $lang; $html = '
'; $html .= '

' . $lang['quickhits'] . ':

'; $html .= ' '; //clear float (see http://www.complexspiral.com/publications/containing-floats/) $html .= '
'; $html .= '
'; return $html; } /** * Build HTML for fulltext search results or "no results" message * * @param array $data the results of the fulltext search * @param array $highlight the terms to be highlighted in the results * * @return string */ protected function getFulltextResultsHTML($data, $highlight) { global $lang; if (empty($data)) { return '
' . $lang['nothingfound'] . '
'; } $html = ''; $html .= '
'; $num = 1; foreach ($data as $id => $cnt) { $resultLink = html_wikilink(':' . $id, null, $highlight); $resultHeader = [$resultLink]; $restrictQueryToNSLink = $this->restrictQueryToNSLink(getNS($id)); if ($restrictQueryToNSLink) { $resultHeader[] = $restrictQueryToNSLink; } $snippet = ''; $lastMod = ''; $mtime = filemtime(wikiFN($id)); if ($cnt !== 0) { $resultHeader[] = $cnt . ' ' . $lang['hits']; if ($num < FT_SNIPPET_NUMBER) { // create snippets for the first number of matches only $snippet = '
' . ft_snippet($id, $highlight) . '
'; $lastMod = '' . $lang['lastmod'] . ' '; $lastMod .= ''; $lastMod .= ''; } $num++; } $metaLine = '
'; $metaLine .= $lastMod; $metaLine .= '
'; $eventData = [ 'resultHeader' => $resultHeader, 'resultBody' => [$metaLine, $snippet], 'page' => $id, ]; trigger_event('SEARCH_RESULT_FULLPAGE', $eventData); $html .= '
'; $html .= '
' . implode(' ', $eventData['resultHeader']) . '
'; $html .= implode('', $eventData['resultBody']); $html .= '
'; } $html .= '
'; return $html; } /** * create a link to restrict the current query to a namespace * * @param bool|string $ns the namespace to which to restrict the query * * @return bool|string */ protected function restrictQueryToNSLink($ns) { if (!$ns) { return false; } if (!$this->isSearchAssistanceAvailable($this->parsedQuery)) { return false; } if (!empty($this->parsedQuery['ns']) && $this->parsedQuery['ns'][0] === $ns) { return false; } $name = '@' . $ns; $tmpForm = new Form(); $this->searchState->addSeachLinkNS($tmpForm, $name, $ns); return $tmpForm->toHTML(); } }