query = $QUERY; $this->parsedQuery = $parsedQuery; } /** * run the search */ public function execute() { $this->pageLookupResults = ft_pageLookup($this->query, true, useHeading('navigation')); $this->fullTextResults = ft_pageSearch($this->query, $highlight); $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; $searchForm = (new Form())->attrs(['method' => 'get'])->addClass('search-results-form'); $searchForm->setHiddenField('do', 'search'); $searchForm->setHiddenField('from', $ID); $searchForm->setHiddenField('searchPageForm', '1'); $searchForm->addFieldsetOpen()->addClass('search-results-form__fieldset'); $searchForm->addTextInput('id')->val($query)->useInput(false); $searchForm->addButton('', $lang['btn_search'])->attr('type', 'submit'); if ($this->isSearchAssistanceAvailable($this->parsedQuery)) { $this->addSearchAssistanceElements($searchForm, $this->parsedQuery); } 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(); } /** * 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 * @param array $parsedQuery */ protected function addSearchAssistanceElements(Form $searchForm, array $parsedQuery) { $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, $parsedQuery); $this->addNamespaceSelector($searchForm, $parsedQuery); $searchForm->addTagClose('div'); } protected function addFragmentBehaviorLinks(Form $searchForm, array $parsedQuery) { $searchForm->addTagOpen('div')->addClass('search-results-form__subwrapper'); $this->addSearchLink( $searchForm, 'exact Match', array_map(function($term){return trim($term, '*');},$this->parsedQuery['and']), $this->parsedQuery['ns'] ); $searchForm->addHTML(' '); $this->addSearchLink( $searchForm, 'starts with', array_map(function($term){return trim($term, '*') . '*';},$this->parsedQuery['and']), $this->parsedQuery['ns'] ); $searchForm->addHTML(' '); $this->addSearchLink( $searchForm, 'ends with', array_map(function($term){return '*' . trim($term, '*');},$this->parsedQuery['and']), $this->parsedQuery['ns'] ); $searchForm->addHTML(' '); $this->addSearchLink( $searchForm, 'contains', array_map(function($term){return '*' . trim($term, '*') . '*';},$this->parsedQuery['and']), $this->parsedQuery['ns'] ); $searchForm->addTagClose('div'); } protected function addSearchLink(Form $searchForm, $label, $and, $ns) { $newQuery = ft_queryUnparser_simple( $and, [], [], $ns, [] ); $searchForm->addTagOpen('a') ->attrs([ 'href' => wl($newQuery, ['do' => 'search', 'searchPageForm' => '1'], false, '&') ]) ; $searchForm->addHTML($label); $searchForm->addTagClose('a'); } /** * Add the elements for the namespace selector * * @param Form $searchForm * @param array $parsedQuery */ protected function addNamespaceSelector(Form $searchForm, array $parsedQuery) { $baseNS = empty($parsedQuery['ns']) ? '' : $parsedQuery['ns'][0]; $searchForm->addTagOpen('div')->addClass('search-results-form__subwrapper'); if ($baseNS) { $searchForm->addTagOpen('div'); $this->addSearchLink( $searchForm, 'remove current namespace restriction', $this->parsedQuery['and'], [] ); $searchForm->addTagClose('div'); } $extraNS = $this->getAdditionalNamespacesFromResults($baseNS); if (!empty($extraNS)) { $searchForm->addTagOpen('div'); $searchForm->addHTML('first level ns below current: '); foreach ($extraNS as $extraNS => $count) { $searchForm->addHTML(' '); $label = $extraNS . ($count ? " ($count)" : ''); $this->addSearchLink($searchForm, $label, $this->parsedQuery['and'], [$extraNS]); } $searchForm->addTagClose('div'); } $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; } /** * 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 = '