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; 1121fcef82SMichael Große protected $pageLookupResults = array(); 1221fcef82SMichael Große protected $fullTextResults = array(); 1321fcef82SMichael Große protected $highlight = array(); 1421fcef82SMichael Große 1521fcef82SMichael Große /** 1621fcef82SMichael Große * Search constructor. 1721fcef82SMichael Große * 1821fcef82SMichael Große * @param string $query the search query 1921fcef82SMichael Große */ 20d09b5b64SMichael Große public function __construct() 2121fcef82SMichael Große { 22d09b5b64SMichael Große global $QUERY; 234c924eb8SMichael Große $Indexer = idx_get_indexer(); 24d09b5b64SMichael Große 25d09b5b64SMichael Große $this->query = $QUERY; 26bbc1da2eSMichael Große $this->parsedQuery = ft_queryParser($Indexer, $QUERY); 2721fcef82SMichael Große } 2821fcef82SMichael Große 2921fcef82SMichael Große /** 3021fcef82SMichael Große * run the search 3121fcef82SMichael Große */ 3221fcef82SMichael Große public function execute() 3321fcef82SMichael Große { 3421fcef82SMichael Große $this->pageLookupResults = ft_pageLookup($this->query, true, useHeading('navigation')); 3521fcef82SMichael Große $this->fullTextResults = ft_pageSearch($this->query, $highlight); 3621fcef82SMichael Große $this->highlight = $highlight; 37bbc1da2eSMichael Große 38bbc1da2eSMichael Große // fixme: find better place for this 39bbc1da2eSMichael Große global $INPUT; 40bbc1da2eSMichael Große if ($INPUT->has('after') || $INPUT->has('before')) { 41bbc1da2eSMichael Große $after = $INPUT->str('after'); 42bbc1da2eSMichael Große $after = is_int($after) ? $after : strtotime($after); 43bbc1da2eSMichael Große 44bbc1da2eSMichael Große $before = $INPUT->str('before'); 45bbc1da2eSMichael Große $before = is_int($before) ? $before : strtotime($before); 46bbc1da2eSMichael Große 47bbc1da2eSMichael Große // todo: should we filter $this->pageLookupResults as well? 48bbc1da2eSMichael Große foreach ($this->fullTextResults as $id => $cnt) { 49bbc1da2eSMichael Große $mTime = filemtime(wikiFN($id)); 50bbc1da2eSMichael Große if ($after && $after > $mTime) { 51bbc1da2eSMichael Große unset($this->fullTextResults[$id]); 52bbc1da2eSMichael Große continue; 53bbc1da2eSMichael Große } 54bbc1da2eSMichael Große if ($before && $before < $mTime) { 55bbc1da2eSMichael Große unset($this->fullTextResults[$id]); 56bbc1da2eSMichael Große } 57bbc1da2eSMichael Große } 58bbc1da2eSMichael Große } 5921fcef82SMichael Große } 6021fcef82SMichael Große 6121fcef82SMichael Große /** 6221fcef82SMichael Große * display the search result 6321fcef82SMichael Große * 6421fcef82SMichael Große * @return void 6521fcef82SMichael Große */ 6621fcef82SMichael Große public function show() 6721fcef82SMichael Große { 6821fcef82SMichael Große $searchHTML = ''; 6921fcef82SMichael Große 70427ed988SMichael Große $searchHTML .= $this->getSearchFormHTML($this->query); 71427ed988SMichael Große 7221fcef82SMichael Große $searchHTML .= $this->getSearchIntroHTML($this->query); 7321fcef82SMichael Große 7421fcef82SMichael Große $searchHTML .= $this->getPageLookupHTML($this->pageLookupResults); 7521fcef82SMichael Große 7621fcef82SMichael Große $searchHTML .= $this->getFulltextResultsHTML($this->fullTextResults, $this->highlight); 7721fcef82SMichael Große 7821fcef82SMichael Große echo $searchHTML; 7921fcef82SMichael Große } 8021fcef82SMichael Große 8121fcef82SMichael Große /** 82427ed988SMichael Große * Get a form which can be used to adjust/refine the search 83427ed988SMichael Große * 84427ed988SMichael Große * @param string $query 85427ed988SMichael Große * 86427ed988SMichael Große * @return string 87427ed988SMichael Große */ 88427ed988SMichael Große protected function getSearchFormHTML($query) 89427ed988SMichael Große { 90bbc1da2eSMichael Große global $lang, $ID, $INPUT; 91427ed988SMichael Große 92bb8ef867SMichael Große $searchForm = (new Form())->attrs(['method' => 'get'])->addClass('search-results-form'); 93bb8ef867SMichael Große $searchForm->setHiddenField('do', 'search'); 94*d22b78c8SMichael Große $searchForm->setHiddenField('id', $ID); 95d09b5b64SMichael Große $searchForm->setHiddenField('searchPageForm', '1'); 96bbc1da2eSMichael Große if ($INPUT->has('after')) { 97bbc1da2eSMichael Große $searchForm->setHiddenField('after', $INPUT->str('after')); 98bbc1da2eSMichael Große } 99bbc1da2eSMichael Große if ($INPUT->has('before')) { 100bbc1da2eSMichael Große $searchForm->setHiddenField('before', $INPUT->str('before')); 101bbc1da2eSMichael Große } 102bb8ef867SMichael Große $searchForm->addFieldsetOpen()->addClass('search-results-form__fieldset'); 103*d22b78c8SMichael Große $searchForm->addTextInput('q')->val($query)->useInput(false); 104427ed988SMichael Große $searchForm->addButton('', $lang['btn_search'])->attr('type', 'submit'); 105bb8ef867SMichael Große 1064c924eb8SMichael Große if ($this->isSearchAssistanceAvailable($this->parsedQuery)) { 1074c924eb8SMichael Große $this->addSearchAssistanceElements($searchForm, $this->parsedQuery); 108bb8ef867SMichael Große } else { 109bb8ef867SMichael Große $searchForm->addClass('search-results-form--no-assistance'); 110bb8ef867SMichael Große $searchForm->addTagOpen('span')->addClass('search-results-form__no-assistance-message'); 111bb8ef867SMichael Große $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.'); 112bb8ef867SMichael Große $searchForm->addTagClose('span'); 113bb8ef867SMichael Große } 114bb8ef867SMichael Große 115427ed988SMichael Große $searchForm->addFieldsetClose(); 116427ed988SMichael Große 11781a0edd9SMichael Große trigger_event('SEARCH_FORM_DISPLAY', $searchForm); 11881a0edd9SMichael Große 119427ed988SMichael Große return $searchForm->toHTML(); 120427ed988SMichael Große } 121427ed988SMichael Große 122427ed988SMichael Große /** 123bb8ef867SMichael Große * Decide if the given query is simple enough to provide search assistance 124bb8ef867SMichael Große * 125bb8ef867SMichael Große * @param array $parsedQuery 126bb8ef867SMichael Große * 127bb8ef867SMichael Große * @return bool 128bb8ef867SMichael Große */ 129bb8ef867SMichael Große protected function isSearchAssistanceAvailable(array $parsedQuery) 130bb8ef867SMichael Große { 131bb8ef867SMichael Große if (count($parsedQuery['words']) > 1) { 132bb8ef867SMichael Große return false; 133bb8ef867SMichael Große } 134bb8ef867SMichael Große if (!empty($parsedQuery['not'])) { 135bb8ef867SMichael Große return false; 136bb8ef867SMichael Große } 137bb8ef867SMichael Große 138bb8ef867SMichael Große if (!empty($parsedQuery['phrases'])) { 139bb8ef867SMichael Große return false; 140bb8ef867SMichael Große } 141bb8ef867SMichael Große 142bb8ef867SMichael Große if (!empty($parsedQuery['notns'])) { 143bb8ef867SMichael Große return false; 144bb8ef867SMichael Große } 145bb8ef867SMichael Große if (count($parsedQuery['ns']) > 1) { 146bb8ef867SMichael Große return false; 147bb8ef867SMichael Große } 148bb8ef867SMichael Große 149bb8ef867SMichael Große return true; 150bb8ef867SMichael Große } 151bb8ef867SMichael Große 152bb8ef867SMichael Große /** 153bb8ef867SMichael Große * Add the elements to be used for search assistance 154bb8ef867SMichael Große * 155bb8ef867SMichael Große * @param Form $searchForm 156bb8ef867SMichael Große * @param array $parsedQuery 157bb8ef867SMichael Große */ 158bb8ef867SMichael Große protected function addSearchAssistanceElements(Form $searchForm, array $parsedQuery) 159bb8ef867SMichael Große { 160bb8ef867SMichael Große $searchForm->addButton('toggleAssistant', 'toggle search assistant') 161bb8ef867SMichael Große ->attr('type', 'button') 162bb8ef867SMichael Große ->id('search-results-form__show-assistance-button') 163bb8ef867SMichael Große ->addClass('search-results-form__show-assistance-button'); 164bb8ef867SMichael Große 165bb8ef867SMichael Große $searchForm->addTagOpen('div') 166bb8ef867SMichael Große ->addClass('js-advancedSearchOptions') 167bb8ef867SMichael Große ->attr('style', 'display: none;'); 168bb8ef867SMichael Große 1694d0cb6e1SMichael Große $this->addFragmentBehaviorLinks($searchForm, $parsedQuery); 170bb8ef867SMichael Große $this->addNamespaceSelector($searchForm, $parsedQuery); 171bbc1da2eSMichael Große $this->addDateSelector($searchForm, $parsedQuery); 172bb8ef867SMichael Große 173bb8ef867SMichael Große $searchForm->addTagClose('div'); 174bb8ef867SMichael Große } 175bb8ef867SMichael Große 1764d0cb6e1SMichael Große protected function addFragmentBehaviorLinks(Form $searchForm, array $parsedQuery) 1774d0cb6e1SMichael Große { 1784d0cb6e1SMichael Große $searchForm->addTagOpen('div')->addClass('search-results-form__subwrapper'); 179bbc1da2eSMichael Große $searchForm->addHTML('fragment behavior: '); 1804d0cb6e1SMichael Große 1814d0cb6e1SMichael Große $this->addSearchLink( 1824d0cb6e1SMichael Große $searchForm, 183bbc1da2eSMichael Große 'exact match', 184bbc1da2eSMichael Große array_map(function($term){return trim($term, '*');},$this->parsedQuery['and']) 1854d0cb6e1SMichael Große ); 1864d0cb6e1SMichael Große 187bbc1da2eSMichael Große $searchForm->addHTML(', '); 1884d0cb6e1SMichael Große 1894d0cb6e1SMichael Große $this->addSearchLink( 1904d0cb6e1SMichael Große $searchForm, 1914d0cb6e1SMichael Große 'starts with', 192bbc1da2eSMichael Große array_map(function($term){return trim($term, '*') . '*';},$this->parsedQuery['and']) 1934d0cb6e1SMichael Große ); 1944d0cb6e1SMichael Große 195bbc1da2eSMichael Große $searchForm->addHTML(', '); 1964d0cb6e1SMichael Große 1974d0cb6e1SMichael Große $this->addSearchLink( 1984d0cb6e1SMichael Große $searchForm, 1994d0cb6e1SMichael Große 'ends with', 200bbc1da2eSMichael Große array_map(function($term){return '*' . trim($term, '*');},$this->parsedQuery['and']) 2014d0cb6e1SMichael Große ); 2024d0cb6e1SMichael Große 203bbc1da2eSMichael Große $searchForm->addHTML(', '); 2044d0cb6e1SMichael Große 2054d0cb6e1SMichael Große $this->addSearchLink( 2064d0cb6e1SMichael Große $searchForm, 2074d0cb6e1SMichael Große 'contains', 208bbc1da2eSMichael Große array_map(function($term){return '*' . trim($term, '*') . '*';},$this->parsedQuery['and']) 2094d0cb6e1SMichael Große ); 2104d0cb6e1SMichael Große 2114d0cb6e1SMichael Große $searchForm->addTagClose('div'); 2124d0cb6e1SMichael Große } 2134d0cb6e1SMichael Große 214bbc1da2eSMichael Große protected function addSearchLink( 215bbc1da2eSMichael Große Form $searchForm, 216bbc1da2eSMichael Große $label, 217bbc1da2eSMichael Große array $and = null, 218bbc1da2eSMichael Große array $ns = null, 219bbc1da2eSMichael Große array $not = null, 220bbc1da2eSMichael Große array $notns = null, 221bbc1da2eSMichael Große array $phrases = null, 222bbc1da2eSMichael Große $after = null, 223bbc1da2eSMichael Große $before = null 224bbc1da2eSMichael Große ) { 225*d22b78c8SMichael Große global $INPUT, $ID; 226bbc1da2eSMichael Große if (null === $and) { 227bbc1da2eSMichael Große $and = $this->parsedQuery['and']; 228bbc1da2eSMichael Große } 229bbc1da2eSMichael Große if (null === $ns) { 230bbc1da2eSMichael Große $ns = $this->parsedQuery['ns']; 231bbc1da2eSMichael Große } 232bbc1da2eSMichael Große if (null === $not) { 233bbc1da2eSMichael Große $not = $this->parsedQuery['not']; 234bbc1da2eSMichael Große } 235bbc1da2eSMichael Große if (null === $phrases) { 236bbc1da2eSMichael Große $phrases = $this->parsedQuery['phrases']; 237bbc1da2eSMichael Große } 238bbc1da2eSMichael Große if (null === $notns) { 239bbc1da2eSMichael Große $notns = $this->parsedQuery['notns']; 240bbc1da2eSMichael Große } 241bbc1da2eSMichael Große if (null === $after) { 242bbc1da2eSMichael Große $after = $INPUT->str('after'); 243bbc1da2eSMichael Große } 244bbc1da2eSMichael Große if (null === $before) { 245bbc1da2eSMichael Große $before = $INPUT->str('before'); 246bbc1da2eSMichael Große } 247bbc1da2eSMichael Große 2484d0cb6e1SMichael Große $newQuery = ft_queryUnparser_simple( 2494d0cb6e1SMichael Große $and, 250bbc1da2eSMichael Große $not, 251bbc1da2eSMichael Große $phrases, 2524d0cb6e1SMichael Große $ns, 253bbc1da2eSMichael Große $notns 2544d0cb6e1SMichael Große ); 255*d22b78c8SMichael Große $hrefAttributes = ['do' => 'search', 'searchPageForm' => '1', 'q' => $newQuery]; 256bbc1da2eSMichael Große if ($after) { 257bbc1da2eSMichael Große $hrefAttributes['after'] = $after; 258bbc1da2eSMichael Große } 259bbc1da2eSMichael Große if ($before) { 260bbc1da2eSMichael Große $hrefAttributes['before'] = $before; 261bbc1da2eSMichael Große } 2624d0cb6e1SMichael Große $searchForm->addTagOpen('a') 2634d0cb6e1SMichael Große ->attrs([ 264*d22b78c8SMichael Große 'href' => wl($ID, $hrefAttributes, false, '&') 2654d0cb6e1SMichael Große ]) 2664d0cb6e1SMichael Große ; 2674d0cb6e1SMichael Große $searchForm->addHTML($label); 2684d0cb6e1SMichael Große $searchForm->addTagClose('a'); 2694d0cb6e1SMichael Große } 2704d0cb6e1SMichael Große 271bb8ef867SMichael Große /** 272bb8ef867SMichael Große * Add the elements for the namespace selector 273bb8ef867SMichael Große * 274bb8ef867SMichael Große * @param Form $searchForm 275bb8ef867SMichael Große * @param array $parsedQuery 276bb8ef867SMichael Große */ 277bb8ef867SMichael Große protected function addNamespaceSelector(Form $searchForm, array $parsedQuery) 278bb8ef867SMichael Große { 279bb8ef867SMichael Große $baseNS = empty($parsedQuery['ns']) ? '' : $parsedQuery['ns'][0]; 280bb8ef867SMichael Große $searchForm->addTagOpen('div')->addClass('search-results-form__subwrapper'); 2814d0cb6e1SMichael Große 282bbc1da2eSMichael Große $extraNS = $this->getAdditionalNamespacesFromResults($baseNS); 283bbc1da2eSMichael Große if (!empty($extraNS) || $baseNS) { 2844d0cb6e1SMichael Große $searchForm->addTagOpen('div'); 285bbc1da2eSMichael Große $searchForm->addHTML('limit to namespace: '); 2864d0cb6e1SMichael Große 287bbc1da2eSMichael Große if ($baseNS) { 2884d0cb6e1SMichael Große $this->addSearchLink( 2894d0cb6e1SMichael Große $searchForm, 290bbc1da2eSMichael Große '(remove limit)', 291bbc1da2eSMichael Große null, 292bbc1da2eSMichael Große [], 293bbc1da2eSMichael Große null, 2944d0cb6e1SMichael Große [] 2954d0cb6e1SMichael Große ); 296bb8ef867SMichael Große } 297bb8ef867SMichael Große 2984d0cb6e1SMichael Große foreach ($extraNS as $extraNS => $count) { 2994d0cb6e1SMichael Große $searchForm->addHTML(' '); 300bb8ef867SMichael Große $label = $extraNS . ($count ? " ($count)" : ''); 3014d0cb6e1SMichael Große 302bbc1da2eSMichael Große $this->addSearchLink($searchForm, $label, null, [$extraNS], null, []); 303bb8ef867SMichael Große } 3044d0cb6e1SMichael Große $searchForm->addTagClose('div'); 305bb8ef867SMichael Große } 306bb8ef867SMichael Große 307bb8ef867SMichael Große $searchForm->addTagClose('div'); 308bb8ef867SMichael Große } 309bb8ef867SMichael Große 310bb8ef867SMichael Große /** 311bb8ef867SMichael Große * Parse the full text results for their top namespaces below the given base namespace 312bb8ef867SMichael Große * 313bb8ef867SMichael Große * @param string $baseNS the namespace within which was searched, empty string for root namespace 314bb8ef867SMichael Große * 315bb8ef867SMichael Große * @return array an associative array with namespace => #number of found pages, sorted descending 316bb8ef867SMichael Große */ 317bb8ef867SMichael Große protected function getAdditionalNamespacesFromResults($baseNS) 318bb8ef867SMichael Große { 319bb8ef867SMichael Große $namespaces = []; 320bb8ef867SMichael Große $baseNSLength = strlen($baseNS); 321bb8ef867SMichael Große foreach ($this->fullTextResults as $page => $numberOfHits) { 322bb8ef867SMichael Große $namespace = getNS($page); 323bb8ef867SMichael Große if (!$namespace) { 324bb8ef867SMichael Große continue; 325bb8ef867SMichael Große } 326bb8ef867SMichael Große if ($namespace === $baseNS) { 327bb8ef867SMichael Große continue; 328bb8ef867SMichael Große } 329bb8ef867SMichael Große $firstColon = strpos((string)$namespace, ':', $baseNSLength + 1) ?: strlen($namespace); 330bb8ef867SMichael Große $subtopNS = substr($namespace, 0, $firstColon); 331bb8ef867SMichael Große if (empty($namespaces[$subtopNS])) { 332bb8ef867SMichael Große $namespaces[$subtopNS] = 0; 333bb8ef867SMichael Große } 334bb8ef867SMichael Große $namespaces[$subtopNS] += 1; 335bb8ef867SMichael Große } 336bb8ef867SMichael Große arsort($namespaces); 337bb8ef867SMichael Große return $namespaces; 338bb8ef867SMichael Große } 339bb8ef867SMichael Große 340bb8ef867SMichael Große /** 341bbc1da2eSMichael Große * @ToDo: we need to remember this date when clicking on other links 342bbc1da2eSMichael Große * @ToDo: custom date input 343bbc1da2eSMichael Große * 344bbc1da2eSMichael Große * @param Form $searchForm 345bbc1da2eSMichael Große * @param $parsedQuery 346bbc1da2eSMichael Große */ 347bbc1da2eSMichael Große protected function addDateSelector(Form $searchForm, $parsedQuery) { 348bbc1da2eSMichael Große $searchForm->addTagOpen('div')->addClass('search-results-form__subwrapper'); 349bbc1da2eSMichael Große $searchForm->addHTML('limit by date: '); 350bbc1da2eSMichael Große 351bbc1da2eSMichael Große global $INPUT; 352bbc1da2eSMichael Große if ($INPUT->has('before') || $INPUT->has('after')) { 353bbc1da2eSMichael Große $this->addSearchLink( 354bbc1da2eSMichael Große $searchForm, 355bbc1da2eSMichael Große '(remove limit)', 356bbc1da2eSMichael Große null, 357bbc1da2eSMichael Große null, 358bbc1da2eSMichael Große null, 359bbc1da2eSMichael Große null, 360bbc1da2eSMichael Große null, 361bbc1da2eSMichael Große false, 362bbc1da2eSMichael Große false 363bbc1da2eSMichael Große ); 364bbc1da2eSMichael Große 365bbc1da2eSMichael Große $searchForm->addHTML(', '); 366bbc1da2eSMichael Große } 367bbc1da2eSMichael Große 368bbc1da2eSMichael Große if ($INPUT->str('after') === '1 week ago') { 369bbc1da2eSMichael Große $searchForm->addHTML('<span class="active">past 7 days</span>'); 370bbc1da2eSMichael Große } else { 371bbc1da2eSMichael Große $this->addSearchLink( 372bbc1da2eSMichael Große $searchForm, 373bbc1da2eSMichael Große 'past 7 days', 374bbc1da2eSMichael Große null, 375bbc1da2eSMichael Große null, 376bbc1da2eSMichael Große null, 377bbc1da2eSMichael Große null, 378bbc1da2eSMichael Große null, 379bbc1da2eSMichael Große '1 week ago', 380bbc1da2eSMichael Große false 381bbc1da2eSMichael Große ); 382bbc1da2eSMichael Große } 383bbc1da2eSMichael Große 384bbc1da2eSMichael Große $searchForm->addHTML(', '); 385bbc1da2eSMichael Große 386bbc1da2eSMichael Große if ($INPUT->str('after') === '1 month ago') { 387bbc1da2eSMichael Große $searchForm->addHTML('<span class="active">past month</span>'); 388bbc1da2eSMichael Große } else { 389bbc1da2eSMichael Große $this->addSearchLink( 390bbc1da2eSMichael Große $searchForm, 391bbc1da2eSMichael Große 'past month', 392bbc1da2eSMichael Große null, 393bbc1da2eSMichael Große null, 394bbc1da2eSMichael Große null, 395bbc1da2eSMichael Große null, 396bbc1da2eSMichael Große null, 397bbc1da2eSMichael Große '1 month ago', 398bbc1da2eSMichael Große false 399bbc1da2eSMichael Große ); 400bbc1da2eSMichael Große } 401bbc1da2eSMichael Große 402bbc1da2eSMichael Große $searchForm->addHTML(', '); 403bbc1da2eSMichael Große 404bbc1da2eSMichael Große if ($INPUT->str('after') === '1 year ago') { 405bbc1da2eSMichael Große $searchForm->addHTML('<span class="active">past year</span>'); 406bbc1da2eSMichael Große } else { 407bbc1da2eSMichael Große $this->addSearchLink( 408bbc1da2eSMichael Große $searchForm, 409bbc1da2eSMichael Große 'past year', 410bbc1da2eSMichael Große null, 411bbc1da2eSMichael Große null, 412bbc1da2eSMichael Große null, 413bbc1da2eSMichael Große null, 414bbc1da2eSMichael Große null, 415bbc1da2eSMichael Große '1 year ago', 416bbc1da2eSMichael Große false 417bbc1da2eSMichael Große ); 418bbc1da2eSMichael Große } 419bbc1da2eSMichael Große 420bbc1da2eSMichael Große $searchForm->addTagClose('div'); 421bbc1da2eSMichael Große } 422bbc1da2eSMichael Große 423bbc1da2eSMichael Große 424bbc1da2eSMichael Große /** 42521fcef82SMichael Große * Build the intro text for the search page 42621fcef82SMichael Große * 42721fcef82SMichael Große * @param string $query the search query 42821fcef82SMichael Große * 42921fcef82SMichael Große * @return string 43021fcef82SMichael Große */ 43121fcef82SMichael Große protected function getSearchIntroHTML($query) 43221fcef82SMichael Große { 43321fcef82SMichael Große global $ID, $lang; 43421fcef82SMichael Große 43521fcef82SMichael Große $intro = p_locale_xhtml('searchpage'); 43621fcef82SMichael Große // allow use of placeholder in search intro 43721fcef82SMichael Große $pagecreateinfo = (auth_quickaclcheck($ID) >= AUTH_CREATE) ? $lang['searchcreatepage'] : ''; 43821fcef82SMichael Große $intro = str_replace( 43921fcef82SMichael Große array('@QUERY@', '@SEARCH@', '@CREATEPAGEINFO@'), 44021fcef82SMichael Große array(hsc(rawurlencode($query)), hsc($query), $pagecreateinfo), 44121fcef82SMichael Große $intro 44221fcef82SMichael Große ); 44321fcef82SMichael Große return $intro; 44421fcef82SMichael Große } 44521fcef82SMichael Große 44621fcef82SMichael Große /** 44721fcef82SMichael Große * Build HTML for a list of pages with matching pagenames 44821fcef82SMichael Große * 44921fcef82SMichael Große * @param array $data search results 45021fcef82SMichael Große * 45121fcef82SMichael Große * @return string 45221fcef82SMichael Große */ 45321fcef82SMichael Große protected function getPageLookupHTML($data) 45421fcef82SMichael Große { 45521fcef82SMichael Große if (empty($data)) { 45621fcef82SMichael Große return ''; 45721fcef82SMichael Große } 45821fcef82SMichael Große 45921fcef82SMichael Große global $lang; 46021fcef82SMichael Große 46121fcef82SMichael Große $html = '<div class="search_quickresult">'; 46221fcef82SMichael Große $html .= '<h3>' . $lang['quickhits'] . ':</h3>'; 46321fcef82SMichael Große $html .= '<ul class="search_quickhits">'; 46421fcef82SMichael Große foreach ($data as $id => $title) { 4654eab6f7cSMichael Große $link = html_wikilink(':' . $id); 4664eab6f7cSMichael Große $eventData = [ 4674eab6f7cSMichael Große 'listItemContent' => [$link], 4684eab6f7cSMichael Große 'page' => $id, 4694eab6f7cSMichael Große ]; 4704eab6f7cSMichael Große trigger_event('SEARCH_RESULT_PAGELOOKUP', $eventData); 4714eab6f7cSMichael Große $html .= '<li>' . implode('', $eventData['listItemContent']) . '</li>'; 47221fcef82SMichael Große } 47321fcef82SMichael Große $html .= '</ul> '; 47421fcef82SMichael Große //clear float (see http://www.complexspiral.com/publications/containing-floats/) 47521fcef82SMichael Große $html .= '<div class="clearer"></div>'; 47621fcef82SMichael Große $html .= '</div>'; 47721fcef82SMichael Große 47821fcef82SMichael Große return $html; 47921fcef82SMichael Große } 48021fcef82SMichael Große 48121fcef82SMichael Große /** 48221fcef82SMichael Große * Build HTML for fulltext search results or "no results" message 48321fcef82SMichael Große * 48421fcef82SMichael Große * @param array $data the results of the fulltext search 48521fcef82SMichael Große * @param array $highlight the terms to be highlighted in the results 48621fcef82SMichael Große * 48721fcef82SMichael Große * @return string 48821fcef82SMichael Große */ 48921fcef82SMichael Große protected function getFulltextResultsHTML($data, $highlight) 49021fcef82SMichael Große { 49121fcef82SMichael Große global $lang; 49221fcef82SMichael Große 49321fcef82SMichael Große if (empty($data)) { 49421fcef82SMichael Große return '<div class="nothing">' . $lang['nothingfound'] . '</div>'; 49521fcef82SMichael Große } 49621fcef82SMichael Große 49721fcef82SMichael Große $html = ''; 49821fcef82SMichael Große $html .= '<dl class="search_results">'; 49921fcef82SMichael Große $num = 1; 5004c924eb8SMichael Große 50121fcef82SMichael Große foreach ($data as $id => $cnt) { 5024eab6f7cSMichael Große $resultLink = html_wikilink(':' . $id, null, $highlight); 5034c924eb8SMichael Große 5044c924eb8SMichael Große $resultHeader = [$resultLink]; 5054c924eb8SMichael Große 5064eab6f7cSMichael Große 5074c924eb8SMichael Große $restrictQueryToNSLink = $this->restrictQueryToNSLink(getNS($id)); 5084c924eb8SMichael Große if ($restrictQueryToNSLink) { 5094c924eb8SMichael Große $resultHeader[] = $restrictQueryToNSLink; 5104c924eb8SMichael Große } 5114c924eb8SMichael Große 5129a75abfbSMichael Große $snippet = ''; 5139a75abfbSMichael Große $lastMod = ''; 5149a75abfbSMichael Große $mtime = filemtime(wikiFN($id)); 5159a75abfbSMichael Große if ($cnt !== 0) { 5169a75abfbSMichael Große $resultHeader[] = $cnt . ' ' . $lang['hits']; 5179a75abfbSMichael Große if ($num < FT_SNIPPET_NUMBER) { // create snippets for the first number of matches only 5189a75abfbSMichael Große $snippet = '<dd>' . ft_snippet($id, $highlight) . '</dd>'; 5199a75abfbSMichael Große $lastMod = '<span class="search_results__lastmod">'. $lang['lastmod'] . ' '; 5209a75abfbSMichael Große $lastMod .= '<time datetime="' . date_iso8601($mtime) . '">'. dformat($mtime) . '</time>'; 5219a75abfbSMichael Große $lastMod .= '</span>'; 5229a75abfbSMichael Große } 5239a75abfbSMichael Große $num++; 5249a75abfbSMichael Große } 5259a75abfbSMichael Große 5269a75abfbSMichael Große $metaLine = '<div class="search_results__metaLine">'; 5279a75abfbSMichael Große $metaLine .= $lastMod; 5289a75abfbSMichael Große $metaLine .= '</div>'; 5299a75abfbSMichael Große 5309a75abfbSMichael Große 5314eab6f7cSMichael Große $eventData = [ 5324c924eb8SMichael Große 'resultHeader' => $resultHeader, 5339a75abfbSMichael Große 'resultBody' => [$metaLine, $snippet], 5344eab6f7cSMichael Große 'page' => $id, 5354eab6f7cSMichael Große ]; 5364eab6f7cSMichael Große trigger_event('SEARCH_RESULT_FULLPAGE', $eventData); 5374eab6f7cSMichael Große $html .= '<div class="search_fullpage_result">'; 5384eab6f7cSMichael Große $html .= '<dt>' . implode(' ', $eventData['resultHeader']) . '</dt>'; 5394eab6f7cSMichael Große $html .= implode('', $eventData['resultBody']); 5404eab6f7cSMichael Große $html .= '</div>'; 54121fcef82SMichael Große } 54221fcef82SMichael Große $html .= '</dl>'; 54321fcef82SMichael Große 54421fcef82SMichael Große return $html; 54521fcef82SMichael Große } 5464c924eb8SMichael Große 5474c924eb8SMichael Große /** 5484c924eb8SMichael Große * create a link to restrict the current query to a namespace 5494c924eb8SMichael Große * 5504c924eb8SMichael Große * @param bool|string $ns the namespace to which to restrict the query 5514c924eb8SMichael Große * 5524c924eb8SMichael Große * @return bool|string 5534c924eb8SMichael Große */ 5544c924eb8SMichael Große protected function restrictQueryToNSLink($ns) 5554c924eb8SMichael Große { 5564c924eb8SMichael Große if (!$ns) { 5574c924eb8SMichael Große return false; 5584c924eb8SMichael Große } 5594c924eb8SMichael Große if (!$this->isSearchAssistanceAvailable($this->parsedQuery)) { 5604c924eb8SMichael Große return false; 5614c924eb8SMichael Große } 5624c924eb8SMichael Große if (!empty($this->parsedQuery['ns']) && $this->parsedQuery['ns'][0] === $ns) { 5634c924eb8SMichael Große return false; 5644c924eb8SMichael Große } 5654c924eb8SMichael Große $name = '@' . $ns; 566bbc1da2eSMichael Große $tmpForm = new Form(); 567bbc1da2eSMichael Große $this->addSearchLink($tmpForm, $name, null, [$ns], null, []); 568bbc1da2eSMichael Große return $tmpForm->toHTML(); 5694c924eb8SMichael Große } 57021fcef82SMichael Große} 571