121fcef82SMichael Große<?php 221fcef82SMichael Große 321fcef82SMichael Großenamespace dokuwiki\Ui; 421fcef82SMichael Große 5cbb44eabSAndreas Gohruse dokuwiki\Extension\Event; 664159a61SAndreas Gohruse dokuwiki\Form\Form; 779a2d784SGerrit Uitslaguse dokuwiki\Utf8\PhpString; 82d85e841SAndreas Gohruse dokuwiki\Utf8\Sort; 9427ed988SMichael Große 1021fcef82SMichael Großeclass Search extends Ui 1121fcef82SMichael Große{ 1221fcef82SMichael Große protected $query; 134c924eb8SMichael Große protected $parsedQuery; 1418856c5dSMichael Große protected $searchState; 15*e2d055f5SAndreas Gohr protected $pageLookupResults = []; 16*e2d055f5SAndreas Gohr protected $fullTextResults = []; 17*e2d055f5SAndreas Gohr protected $highlight = []; 1821fcef82SMichael Große 1921fcef82SMichael Große /** 2021fcef82SMichael Große * Search constructor. 216639a152SMichael Große * 22fc46ed58SMichael Große * @param array $pageLookupResults pagename lookup results in the form [pagename => pagetitle] 23fc46ed58SMichael Große * @param array $fullTextResults fulltext search results in the form [pagename => #hits] 24fc46ed58SMichael Große * @param array $highlight array of strings to be highlighted 2521fcef82SMichael Große */ 266639a152SMichael Große public function __construct(array $pageLookupResults, array $fullTextResults, $highlight) 2721fcef82SMichael Große { 28d09b5b64SMichael Große global $QUERY; 294c924eb8SMichael Große $Indexer = idx_get_indexer(); 30d09b5b64SMichael Große 31d09b5b64SMichael Große $this->query = $QUERY; 32bbc1da2eSMichael Große $this->parsedQuery = ft_queryParser($Indexer, $QUERY); 3318856c5dSMichael Große $this->searchState = new SearchState($this->parsedQuery); 3421fcef82SMichael Große 356639a152SMichael Große $this->pageLookupResults = $pageLookupResults; 366639a152SMichael Große $this->fullTextResults = $fullTextResults; 3721fcef82SMichael Große $this->highlight = $highlight; 38b3cfe85aSMichael Große } 39bbc1da2eSMichael Große 40b3cfe85aSMichael Große /** 4121fcef82SMichael Große * display the search result 4221fcef82SMichael Große * 4321fcef82SMichael Große * @return void 4421fcef82SMichael Große */ 4521fcef82SMichael Große public function show() 4621fcef82SMichael Große { 4779a2d784SGerrit Uitslag $searchHTML = $this->getSearchIntroHTML($this->query); 4821fcef82SMichael Große 492ce8affcSMichael Große $searchHTML .= $this->getSearchFormHTML($this->query); 502ce8affcSMichael Große 5121fcef82SMichael Große $searchHTML .= $this->getPageLookupHTML($this->pageLookupResults); 5221fcef82SMichael Große 5321fcef82SMichael Große $searchHTML .= $this->getFulltextResultsHTML($this->fullTextResults, $this->highlight); 5421fcef82SMichael Große 5521fcef82SMichael Große echo $searchHTML; 5621fcef82SMichael Große } 5721fcef82SMichael Große 5821fcef82SMichael Große /** 59427ed988SMichael Große * Get a form which can be used to adjust/refine the search 60427ed988SMichael Große * 61427ed988SMichael Große * @param string $query 62427ed988SMichael Große * 63427ed988SMichael Große * @return string 64427ed988SMichael Große */ 65427ed988SMichael Große protected function getSearchFormHTML($query) 66427ed988SMichael Große { 67bbc1da2eSMichael Große global $lang, $ID, $INPUT; 68427ed988SMichael Große 697fa270bcSMichael Große $searchForm = (new Form(['method' => 'get'], true))->addClass('search-results-form'); 70bb8ef867SMichael Große $searchForm->setHiddenField('do', 'search'); 71d22b78c8SMichael Große $searchForm->setHiddenField('id', $ID); 721265b193SMichael Große $searchForm->setHiddenField('sf', '1'); 73422bbbc6SMichael Große if ($INPUT->has('min')) { 74422bbbc6SMichael Große $searchForm->setHiddenField('min', $INPUT->str('min')); 75bbc1da2eSMichael Große } 76422bbbc6SMichael Große if ($INPUT->has('max')) { 77422bbbc6SMichael Große $searchForm->setHiddenField('max', $INPUT->str('max')); 78bbc1da2eSMichael Große } 791265b193SMichael Große if ($INPUT->has('srt')) { 801265b193SMichael Große $searchForm->setHiddenField('srt', $INPUT->str('srt')); 818d0e286aSMichael Große } 824bdf82b5SAndreas Gohr $searchForm->addFieldsetOpen()->addClass('search-form'); 83d22b78c8SMichael Große $searchForm->addTextInput('q')->val($query)->useInput(false); 84427ed988SMichael Große $searchForm->addButton('', $lang['btn_search'])->attr('type', 'submit'); 85bb8ef867SMichael Große 8618856c5dSMichael Große $this->addSearchAssistanceElements($searchForm); 87bb8ef867SMichael Große 88427ed988SMichael Große $searchForm->addFieldsetClose(); 89427ed988SMichael Große 90c6977b3aSSatoshi Sahara return $searchForm->toHTML('Search'); 91427ed988SMichael Große } 92427ed988SMichael Große 93be76738bSMichael Große /** 94be76738bSMichael Große * Add elements to adjust how the results are sorted 95be76738bSMichael Große * 96be76738bSMichael Große * @param Form $searchForm 97be76738bSMichael Große */ 98b005809cSMichael Große protected function addSortTool(Form $searchForm) 99b005809cSMichael Große { 100b005809cSMichael Große global $INPUT, $lang; 101b005809cSMichael Große 102b005809cSMichael Große $options = [ 103b005809cSMichael Große 'hits' => [ 104b005809cSMichael Große 'label' => $lang['search_sort_by_hits'], 105b005809cSMichael Große 'sort' => '', 106b005809cSMichael Große ], 107b005809cSMichael Große 'mtime' => [ 108b005809cSMichael Große 'label' => $lang['search_sort_by_mtime'], 109b005809cSMichael Große 'sort' => 'mtime', 110b005809cSMichael Große ], 111b005809cSMichael Große ]; 112b005809cSMichael Große $activeOption = 'hits'; 113b005809cSMichael Große 1141265b193SMichael Große if ($INPUT->str('srt') === 'mtime') { 115b005809cSMichael Große $activeOption = 'mtime'; 116b005809cSMichael Große } 117b005809cSMichael Große 1182171f9cbSAndreas Gohr $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true'); 119b005809cSMichael Große // render current 1204bdf82b5SAndreas Gohr $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); 121b005809cSMichael Große if ($activeOption !== 'hits') { 1224bdf82b5SAndreas Gohr $currentWrapper->addClass('changed'); 123b005809cSMichael Große } 124b005809cSMichael Große $searchForm->addHTML($options[$activeOption]['label']); 125b005809cSMichael Große $searchForm->addTagClose('div'); 126b005809cSMichael Große 127b005809cSMichael Große // render options list 1282171f9cbSAndreas Gohr $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false'); 129b005809cSMichael Große 130b005809cSMichael Große foreach ($options as $key => $option) { 1314bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 132b005809cSMichael Große 133b005809cSMichael Große if ($key === $activeOption) { 1344bdf82b5SAndreas Gohr $listItem->addClass('active'); 135b005809cSMichael Große $searchForm->addHTML($option['label']); 136b005809cSMichael Große } else { 13752d4cd42SMichael Große $link = $this->searchState->withSorting($option['sort'])->getSearchLink($option['label']); 13852d4cd42SMichael Große $searchForm->addHTML($link); 139b005809cSMichael Große } 140b005809cSMichael Große $searchForm->addTagClose('li'); 141b005809cSMichael Große } 142b005809cSMichael Große $searchForm->addTagClose('ul'); 143b005809cSMichael Große 144b005809cSMichael Große $searchForm->addTagClose('div'); 145b005809cSMichael Große 146b005809cSMichael Große } 147b005809cSMichael Große 148be76738bSMichael Große /** 149be76738bSMichael Große * Check if the query is simple enough to modify its namespace limitations without breaking the rest of the query 150be76738bSMichael Große * 151be76738bSMichael Große * @param array $parsedQuery 152be76738bSMichael Große * 153be76738bSMichael Große * @return bool 154be76738bSMichael Große */ 155*e2d055f5SAndreas Gohr protected function isNamespaceAssistanceAvailable(array $parsedQuery) 156*e2d055f5SAndreas Gohr { 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 164be76738bSMichael Große /** 165be76738bSMichael Große * Check if the query is simple enough to modify the fragment search behavior without breaking the rest of the query 166be76738bSMichael Große * 167be76738bSMichael Große * @param array $parsedQuery 168be76738bSMichael Große * 169be76738bSMichael Große * @return bool 170be76738bSMichael Große */ 171*e2d055f5SAndreas Gohr protected function isFragmentAssistanceAvailable(array $parsedQuery) 172*e2d055f5SAndreas Gohr { 173df977249SMichael Große if (preg_match('/[\(\)\|]/', $parsedQuery['query']) === 1) { 174bb8ef867SMichael Große return false; 175bb8ef867SMichael Große } 176bb8ef867SMichael Große 177bb8ef867SMichael Große if (!empty($parsedQuery['phrases'])) { 178bb8ef867SMichael Große return false; 179bb8ef867SMichael Große } 180bb8ef867SMichael Große 181bb8ef867SMichael Große return true; 182bb8ef867SMichael Große } 183bb8ef867SMichael Große 184bb8ef867SMichael Große /** 185bb8ef867SMichael Große * Add the elements to be used for search assistance 186bb8ef867SMichael Große * 187bb8ef867SMichael Große * @param Form $searchForm 188bb8ef867SMichael Große */ 18918856c5dSMichael Große protected function addSearchAssistanceElements(Form $searchForm) 190bb8ef867SMichael Große { 191bb8ef867SMichael Große $searchForm->addTagOpen('div') 1924bdf82b5SAndreas Gohr ->addClass('advancedOptions') 1932171f9cbSAndreas Gohr ->attr('style', 'display: none;') 1942171f9cbSAndreas Gohr ->attr('aria-hidden', 'true'); 195bb8ef867SMichael Große 19618856c5dSMichael Große $this->addFragmentBehaviorLinks($searchForm); 19718856c5dSMichael Große $this->addNamespaceSelector($searchForm); 19818856c5dSMichael Große $this->addDateSelector($searchForm); 199b005809cSMichael Große $this->addSortTool($searchForm); 200bb8ef867SMichael Große 201bb8ef867SMichael Große $searchForm->addTagClose('div'); 202bb8ef867SMichael Große } 203bb8ef867SMichael Große 204be76738bSMichael Große /** 205be76738bSMichael Große * Add the elements to adjust the fragment search behavior 206be76738bSMichael Große * 207be76738bSMichael Große * @param Form $searchForm 208be76738bSMichael Große */ 20918856c5dSMichael Große protected function addFragmentBehaviorLinks(Form $searchForm) 2104d0cb6e1SMichael Große { 211df977249SMichael Große if (!$this->isFragmentAssistanceAvailable($this->parsedQuery)) { 212df977249SMichael Große return; 213df977249SMichael Große } 214b005809cSMichael Große global $lang; 2154d0cb6e1SMichael Große 216b005809cSMichael Große $options = [ 217b005809cSMichael Große 'exact' => [ 218b005809cSMichael Große 'label' => $lang['search_exact_match'], 219*e2d055f5SAndreas Gohr 'and' => array_map(static fn($term) => trim($term, '*'), $this->parsedQuery['and']), 220*e2d055f5SAndreas Gohr 'not' => array_map(static fn($term) => trim($term, '*'), $this->parsedQuery['not']), 221b005809cSMichael Große ], 222b005809cSMichael Große 'starts' => [ 223b005809cSMichael Große 'label' => $lang['search_starts_with'], 224*e2d055f5SAndreas Gohr 'and' => array_map(static fn($term) => trim($term, '*') . '*', $this->parsedQuery['and']), 225*e2d055f5SAndreas Gohr 'not' => array_map(static fn($term) => trim($term, '*') . '*', $this->parsedQuery['not']), 226b005809cSMichael Große ], 227b005809cSMichael Große 'ends' => [ 228b005809cSMichael Große 'label' => $lang['search_ends_with'], 229*e2d055f5SAndreas Gohr 'and' => array_map(static fn($term) => '*' . trim($term, '*'), $this->parsedQuery['and']), 230*e2d055f5SAndreas Gohr 'not' => array_map(static fn($term) => '*' . trim($term, '*'), $this->parsedQuery['not']), 231b005809cSMichael Große ], 232b005809cSMichael Große 'contains' => [ 233b005809cSMichael Große 'label' => $lang['search_contains'], 234*e2d055f5SAndreas Gohr 'and' => array_map(static fn($term) => '*' . trim($term, '*') . '*', $this->parsedQuery['and']), 235*e2d055f5SAndreas Gohr 'not' => array_map(static fn($term) => '*' . trim($term, '*') . '*', $this->parsedQuery['not']), 236b005809cSMichael Große ] 237b005809cSMichael Große ]; 238b005809cSMichael Große 239b005809cSMichael Große // detect current 240c6b5b74aSMichael Große $activeOption = 'custom'; 241b005809cSMichael Große foreach ($options as $key => $option) { 242b005809cSMichael Große if ($this->parsedQuery['and'] === $option['and']) { 243b005809cSMichael Große $activeOption = $key; 244b005809cSMichael Große } 245b005809cSMichael Große } 246c6b5b74aSMichael Große if ($activeOption === 'custom') { 247c6b5b74aSMichael Große $options = array_merge(['custom' => [ 248c6b5b74aSMichael Große 'label' => $lang['search_custom_match'], 249c6b5b74aSMichael Große ]], $options); 250c6b5b74aSMichael Große } 251b005809cSMichael Große 2522171f9cbSAndreas Gohr $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true'); 253b005809cSMichael Große // render current 2544bdf82b5SAndreas Gohr $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); 255b005809cSMichael Große if ($activeOption !== 'exact') { 2564bdf82b5SAndreas Gohr $currentWrapper->addClass('changed'); 257b005809cSMichael Große } 258b005809cSMichael Große $searchForm->addHTML($options[$activeOption]['label']); 259b005809cSMichael Große $searchForm->addTagClose('div'); 260b005809cSMichael Große 261b005809cSMichael Große // render options list 2622171f9cbSAndreas Gohr $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false'); 263b005809cSMichael Große 264b005809cSMichael Große foreach ($options as $key => $option) { 2654bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 266b005809cSMichael Große 267b005809cSMichael Große if ($key === $activeOption) { 2684bdf82b5SAndreas Gohr $listItem->addClass('active'); 269b005809cSMichael Große $searchForm->addHTML($option['label']); 270b005809cSMichael Große } else { 27152d4cd42SMichael Große $link = $this->searchState 27252d4cd42SMichael Große ->withFragments($option['and'], $option['not']) 273*e2d055f5SAndreas Gohr ->getSearchLink($option['label']); 27452d4cd42SMichael Große $searchForm->addHTML($link); 275b005809cSMichael Große } 276b005809cSMichael Große $searchForm->addTagClose('li'); 277b005809cSMichael Große } 278b005809cSMichael Große $searchForm->addTagClose('ul'); 2794d0cb6e1SMichael Große 2804d0cb6e1SMichael Große $searchForm->addTagClose('div'); 281b005809cSMichael Große 282b005809cSMichael Große // render options list 2834d0cb6e1SMichael Große } 2844d0cb6e1SMichael Große 285bb8ef867SMichael Große /** 286bb8ef867SMichael Große * Add the elements for the namespace selector 287bb8ef867SMichael Große * 288bb8ef867SMichael Große * @param Form $searchForm 289bb8ef867SMichael Große */ 29018856c5dSMichael Große protected function addNamespaceSelector(Form $searchForm) 291bb8ef867SMichael Große { 292df977249SMichael Große if (!$this->isNamespaceAssistanceAvailable($this->parsedQuery)) { 293df977249SMichael Große return; 294df977249SMichael Große } 295df977249SMichael Große 296b005809cSMichael Große global $lang; 297b005809cSMichael Große 29818856c5dSMichael Große $baseNS = empty($this->parsedQuery['ns']) ? '' : $this->parsedQuery['ns'][0]; 299bbc1da2eSMichael Große $extraNS = $this->getAdditionalNamespacesFromResults($baseNS); 3004d0cb6e1SMichael Große 3012171f9cbSAndreas Gohr $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true'); 302b005809cSMichael Große // render current 3034bdf82b5SAndreas Gohr $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); 304bbc1da2eSMichael Große if ($baseNS) { 3054bdf82b5SAndreas Gohr $currentWrapper->addClass('changed'); 306b005809cSMichael Große $searchForm->addHTML('@' . $baseNS); 307b005809cSMichael Große } else { 308b005809cSMichael Große $searchForm->addHTML($lang['search_any_ns']); 309b005809cSMichael Große } 310b005809cSMichael Große $searchForm->addTagClose('div'); 311b005809cSMichael Große 312b005809cSMichael Große // render options list 3132171f9cbSAndreas Gohr $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false'); 314b005809cSMichael Große 3154bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 316b005809cSMichael Große if ($baseNS) { 3174bdf82b5SAndreas Gohr $listItem->addClass('active'); 31852d4cd42SMichael Große $link = $this->searchState->withNamespace('')->getSearchLink($lang['search_any_ns']); 31952d4cd42SMichael Große $searchForm->addHTML($link); 320b005809cSMichael Große } else { 321b005809cSMichael Große $searchForm->addHTML($lang['search_any_ns']); 322bb8ef867SMichael Große } 323b005809cSMichael Große $searchForm->addTagClose('li'); 324bb8ef867SMichael Große 32518856c5dSMichael Große foreach ($extraNS as $ns => $count) { 3264bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 3271d918893SAndreas Gohr $label = $ns . ($count ? " <bdi>($count)</bdi>" : ''); 3284d0cb6e1SMichael Große 329b005809cSMichael Große if ($ns === $baseNS) { 3304bdf82b5SAndreas Gohr $listItem->addClass('active'); 331b005809cSMichael Große $searchForm->addHTML($label); 332b005809cSMichael Große } else { 33352d4cd42SMichael Große $link = $this->searchState->withNamespace($ns)->getSearchLink($label); 33452d4cd42SMichael Große $searchForm->addHTML($link); 335bb8ef867SMichael Große } 336b005809cSMichael Große $searchForm->addTagClose('li'); 337bb8ef867SMichael Große } 338b005809cSMichael Große $searchForm->addTagClose('ul'); 339bb8ef867SMichael Große 340bb8ef867SMichael Große $searchForm->addTagClose('div'); 341b005809cSMichael Große 342bb8ef867SMichael Große } 343bb8ef867SMichael Große 344bb8ef867SMichael Große /** 345bb8ef867SMichael Große * Parse the full text results for their top namespaces below the given base namespace 346bb8ef867SMichael Große * 347bb8ef867SMichael Große * @param string $baseNS the namespace within which was searched, empty string for root namespace 348bb8ef867SMichael Große * 349bb8ef867SMichael Große * @return array an associative array with namespace => #number of found pages, sorted descending 350bb8ef867SMichael Große */ 351bb8ef867SMichael Große protected function getAdditionalNamespacesFromResults($baseNS) 352bb8ef867SMichael Große { 353bb8ef867SMichael Große $namespaces = []; 354bb8ef867SMichael Große $baseNSLength = strlen($baseNS); 355bb8ef867SMichael Große foreach ($this->fullTextResults as $page => $numberOfHits) { 356bb8ef867SMichael Große $namespace = getNS($page); 357bb8ef867SMichael Große if (!$namespace) { 358bb8ef867SMichael Große continue; 359bb8ef867SMichael Große } 360bb8ef867SMichael Große if ($namespace === $baseNS) { 361bb8ef867SMichael Große continue; 362bb8ef867SMichael Große } 363bb8ef867SMichael Große $firstColon = strpos((string)$namespace, ':', $baseNSLength + 1) ?: strlen($namespace); 364bb8ef867SMichael Große $subtopNS = substr($namespace, 0, $firstColon); 365bb8ef867SMichael Große if (empty($namespaces[$subtopNS])) { 366bb8ef867SMichael Große $namespaces[$subtopNS] = 0; 367bb8ef867SMichael Große } 368*e2d055f5SAndreas Gohr ++$namespaces[$subtopNS]; 369bb8ef867SMichael Große } 3702d85e841SAndreas Gohr Sort::ksort($namespaces); 371bb8ef867SMichael Große arsort($namespaces); 372bb8ef867SMichael Große return $namespaces; 373bb8ef867SMichael Große } 374bb8ef867SMichael Große 375bb8ef867SMichael Große /** 376bbc1da2eSMichael Große * @ToDo: custom date input 377bbc1da2eSMichael Große * 378bbc1da2eSMichael Große * @param Form $searchForm 379bbc1da2eSMichael Große */ 380b005809cSMichael Große protected function addDateSelector(Form $searchForm) 381b005809cSMichael Große { 382b005809cSMichael Große global $INPUT, $lang; 383bbc1da2eSMichael Große 384b005809cSMichael Große $options = [ 385b005809cSMichael Große 'any' => [ 386b005809cSMichael Große 'before' => false, 387b005809cSMichael Große 'after' => false, 388b005809cSMichael Große 'label' => $lang['search_any_time'], 389b005809cSMichael Große ], 390b005809cSMichael Große 'week' => [ 391b005809cSMichael Große 'before' => false, 392b005809cSMichael Große 'after' => '1 week ago', 393b005809cSMichael Große 'label' => $lang['search_past_7_days'], 394b005809cSMichael Große ], 395b005809cSMichael Große 'month' => [ 396b005809cSMichael Große 'before' => false, 397b005809cSMichael Große 'after' => '1 month ago', 398b005809cSMichael Große 'label' => $lang['search_past_month'], 399b005809cSMichael Große ], 400b005809cSMichael Große 'year' => [ 401b005809cSMichael Große 'before' => false, 402b005809cSMichael Große 'after' => '1 year ago', 403b005809cSMichael Große 'label' => $lang['search_past_year'], 404b005809cSMichael Große ], 405b005809cSMichael Große ]; 406b005809cSMichael Große $activeOption = 'any'; 407b005809cSMichael Große foreach ($options as $key => $option) { 408422bbbc6SMichael Große if ($INPUT->str('min') === $option['after']) { 409b005809cSMichael Große $activeOption = $key; 410b005809cSMichael Große break; 411b005809cSMichael Große } 412b005809cSMichael Große } 413b005809cSMichael Große 4142171f9cbSAndreas Gohr $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true'); 415b005809cSMichael Große // render current 4164bdf82b5SAndreas Gohr $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); 417422bbbc6SMichael Große if ($INPUT->has('max') || $INPUT->has('min')) { 4184bdf82b5SAndreas Gohr $currentWrapper->addClass('changed'); 419bbc1da2eSMichael Große } 420b005809cSMichael Große $searchForm->addHTML($options[$activeOption]['label']); 421b005809cSMichael Große $searchForm->addTagClose('div'); 422bbc1da2eSMichael Große 423b005809cSMichael Große // render options list 4242171f9cbSAndreas Gohr $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false'); 425b005809cSMichael Große 426b005809cSMichael Große foreach ($options as $key => $option) { 4274bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 428b005809cSMichael Große 429b005809cSMichael Große if ($key === $activeOption) { 4304bdf82b5SAndreas Gohr $listItem->addClass('active'); 431b005809cSMichael Große $searchForm->addHTML($option['label']); 432bbc1da2eSMichael Große } else { 43352d4cd42SMichael Große $link = $this->searchState 43452d4cd42SMichael Große ->withTimeLimitations($option['after'], $option['before']) 435*e2d055f5SAndreas Gohr ->getSearchLink($option['label']); 43652d4cd42SMichael Große $searchForm->addHTML($link); 437bbc1da2eSMichael Große } 438b005809cSMichael Große $searchForm->addTagClose('li'); 439bbc1da2eSMichael Große } 440b005809cSMichael Große $searchForm->addTagClose('ul'); 441bbc1da2eSMichael Große 442bbc1da2eSMichael Große $searchForm->addTagClose('div'); 443bbc1da2eSMichael Große } 444bbc1da2eSMichael Große 445bbc1da2eSMichael Große 446bbc1da2eSMichael Große /** 44721fcef82SMichael Große * Build the intro text for the search page 44821fcef82SMichael Große * 44921fcef82SMichael Große * @param string $query the search query 45021fcef82SMichael Große * 45121fcef82SMichael Große * @return string 45221fcef82SMichael Große */ 45321fcef82SMichael Große protected function getSearchIntroHTML($query) 45421fcef82SMichael Große { 4552ce8affcSMichael Große global $lang; 45621fcef82SMichael Große 45721fcef82SMichael Große $intro = p_locale_xhtml('searchpage'); 4582ce8affcSMichael Große 4592ce8affcSMichael Große $queryPagename = $this->createPagenameFromQuery($this->parsedQuery); 4602ce8affcSMichael Große $createQueryPageLink = html_wikilink($queryPagename . '?do=edit', $queryPagename); 4612ce8affcSMichael Große 4622ce8affcSMichael Große $pagecreateinfo = ''; 4632ce8affcSMichael Große if (auth_quickaclcheck($queryPagename) >= AUTH_CREATE) { 4642ce8affcSMichael Große $pagecreateinfo = sprintf($lang['searchcreatepage'], $createQueryPageLink); 4652ce8affcSMichael Große } 46679a2d784SGerrit Uitslag return str_replace( 467*e2d055f5SAndreas Gohr ['@QUERY@', '@SEARCH@', '@CREATEPAGEINFO@'], 468*e2d055f5SAndreas Gohr [hsc(rawurlencode($query)), hsc($query), $pagecreateinfo], 46921fcef82SMichael Große $intro 47021fcef82SMichael Große ); 47121fcef82SMichael Große } 47221fcef82SMichael Große 47321fcef82SMichael Große /** 4742ce8affcSMichael Große * Create a pagename based the parsed search query 4752ce8affcSMichael Große * 4762ce8affcSMichael Große * @param array $parsedQuery 4772ce8affcSMichael Große * 4782ce8affcSMichael Große * @return string pagename constructed from the parsed query 4792ce8affcSMichael Große */ 48042690e4dSMichael Große public function createPagenameFromQuery($parsedQuery) 4812ce8affcSMichael Große { 482e180e453SPhy $cleanedQuery = cleanID($parsedQuery['query']); // already strtolowered 48379a2d784SGerrit Uitslag if ($cleanedQuery === PhpString::strtolower($parsedQuery['query'])) { 48442690e4dSMichael Große return ':' . $cleanedQuery; 48542690e4dSMichael Große } 4862ce8affcSMichael Große $pagename = ''; 4872ce8affcSMichael Große if (!empty($parsedQuery['ns'])) { 48842690e4dSMichael Große $pagename .= ':' . cleanID($parsedQuery['ns'][0]); 4892ce8affcSMichael Große } 4902ce8affcSMichael Große $pagename .= ':' . cleanID(implode(' ', $parsedQuery['highlight'])); 4912ce8affcSMichael Große return $pagename; 4922ce8affcSMichael Große } 4932ce8affcSMichael Große 4942ce8affcSMichael Große /** 49521fcef82SMichael Große * Build HTML for a list of pages with matching pagenames 49621fcef82SMichael Große * 49721fcef82SMichael Große * @param array $data search results 49821fcef82SMichael Große * 49921fcef82SMichael Große * @return string 50021fcef82SMichael Große */ 50121fcef82SMichael Große protected function getPageLookupHTML($data) 50221fcef82SMichael Große { 50321fcef82SMichael Große if (empty($data)) { 50421fcef82SMichael Große return ''; 50521fcef82SMichael Große } 50621fcef82SMichael Große 50721fcef82SMichael Große global $lang; 50821fcef82SMichael Große 50921fcef82SMichael Große $html = '<div class="search_quickresult">'; 5106d55fda7SMichael Große $html .= '<h2>' . $lang['quickhits'] . ':</h2>'; 51121fcef82SMichael Große $html .= '<ul class="search_quickhits">'; 512*e2d055f5SAndreas Gohr foreach (array_keys($data) as $id) { 5135d87aa31SMichael Große $name = null; 5145d87aa31SMichael Große if (!useHeading('navigation') && $ns = getNS($id)) { 5155d87aa31SMichael Große $name = shorten(noNS($id), ' (' . $ns . ')', 30); 5165d87aa31SMichael Große } 5175d87aa31SMichael Große $link = html_wikilink(':' . $id, $name); 5184eab6f7cSMichael Große $eventData = [ 5194eab6f7cSMichael Große 'listItemContent' => [$link], 5204eab6f7cSMichael Große 'page' => $id, 5214eab6f7cSMichael Große ]; 522cbb44eabSAndreas Gohr Event::createAndTrigger('SEARCH_RESULT_PAGELOOKUP', $eventData); 5234eab6f7cSMichael Große $html .= '<li>' . implode('', $eventData['listItemContent']) . '</li>'; 52421fcef82SMichael Große } 52521fcef82SMichael Große $html .= '</ul> '; 52621fcef82SMichael Große //clear float (see http://www.complexspiral.com/publications/containing-floats/) 52721fcef82SMichael Große $html .= '<div class="clearer"></div>'; 52821fcef82SMichael Große $html .= '</div>'; 52921fcef82SMichael Große 53021fcef82SMichael Große return $html; 53121fcef82SMichael Große } 53221fcef82SMichael Große 53321fcef82SMichael Große /** 53421fcef82SMichael Große * Build HTML for fulltext search results or "no results" message 53521fcef82SMichael Große * 53621fcef82SMichael Große * @param array $data the results of the fulltext search 53721fcef82SMichael Große * @param array $highlight the terms to be highlighted in the results 53821fcef82SMichael Große * 53921fcef82SMichael Große * @return string 54021fcef82SMichael Große */ 54121fcef82SMichael Große protected function getFulltextResultsHTML($data, $highlight) 54221fcef82SMichael Große { 54321fcef82SMichael Große global $lang; 54421fcef82SMichael Große 54521fcef82SMichael Große if (empty($data)) { 54621fcef82SMichael Große return '<div class="nothing">' . $lang['nothingfound'] . '</div>'; 54721fcef82SMichael Große } 54821fcef82SMichael Große 5492ce8affcSMichael Große $html = '<div class="search_fulltextresult">'; 5506d55fda7SMichael Große $html .= '<h2>' . $lang['search_fullresults'] . ':</h2>'; 5512ce8affcSMichael Große 55221fcef82SMichael Große $html .= '<dl class="search_results">'; 5538225e1abSMichael Große $num = 0; 5548225e1abSMichael Große $position = 0; 5554c924eb8SMichael Große 55621fcef82SMichael Große foreach ($data as $id => $cnt) { 557*e2d055f5SAndreas Gohr ++$position; 5584eab6f7cSMichael Große $resultLink = html_wikilink(':' . $id, null, $highlight); 5594c924eb8SMichael Große 5604c924eb8SMichael Große $resultHeader = [$resultLink]; 5614c924eb8SMichael Große 5624eab6f7cSMichael Große 5634c924eb8SMichael Große $restrictQueryToNSLink = $this->restrictQueryToNSLink(getNS($id)); 5644c924eb8SMichael Große if ($restrictQueryToNSLink) { 5654c924eb8SMichael Große $resultHeader[] = $restrictQueryToNSLink; 5664c924eb8SMichael Große } 5674c924eb8SMichael Große 5685d06a1e4SMichael Große $resultBody = []; 5699a75abfbSMichael Große $mtime = filemtime(wikiFN($id)); 5705d06a1e4SMichael Große $lastMod = '<span class="lastmod">' . $lang['lastmod'] . '</span> '; 57164159a61SAndreas Gohr $lastMod .= '<time datetime="' . date_iso8601($mtime) . '" title="' . dformat($mtime) . '">' . 57264159a61SAndreas Gohr dformat($mtime, '%f') . 57364159a61SAndreas Gohr '</time>'; 574f0861d1fSMichael Große $resultBody['meta'] = $lastMod; 5755d06a1e4SMichael Große if ($cnt !== 0) { 5768225e1abSMichael Große $num++; 577b12bcb77SAnika Henke $hits = '<span class="hits">' . $cnt . ' ' . $lang['hits'] . '</span>, '; 578f0861d1fSMichael Große $resultBody['meta'] = $hits . $resultBody['meta']; 5798225e1abSMichael Große if ($num <= FT_SNIPPET_NUMBER) { // create snippets for the first number of matches only 580f0861d1fSMichael Große $resultBody['snippet'] = ft_snippet($id, $highlight); 5819a75abfbSMichael Große } 5829a75abfbSMichael Große } 5839a75abfbSMichael Große 5844eab6f7cSMichael Große $eventData = [ 5854c924eb8SMichael Große 'resultHeader' => $resultHeader, 5865d06a1e4SMichael Große 'resultBody' => $resultBody, 5874eab6f7cSMichael Große 'page' => $id, 58878d786c9SMichael Große 'position' => $position, 5894eab6f7cSMichael Große ]; 590cbb44eabSAndreas Gohr Event::createAndTrigger('SEARCH_RESULT_FULLPAGE', $eventData); 5915d06a1e4SMichael Große $html .= '<div class="search_fullpage_result">'; 5924eab6f7cSMichael Große $html .= '<dt>' . implode(' ', $eventData['resultHeader']) . '</dt>'; 5935d06a1e4SMichael Große foreach ($eventData['resultBody'] as $class => $htmlContent) { 5945d06a1e4SMichael Große $html .= "<dd class=\"$class\">$htmlContent</dd>"; 5955d06a1e4SMichael Große } 5965d06a1e4SMichael Große $html .= '</div>'; 59721fcef82SMichael Große } 59821fcef82SMichael Große $html .= '</dl>'; 59921fcef82SMichael Große 6002ce8affcSMichael Große $html .= '</div>'; 6012ce8affcSMichael Große 60221fcef82SMichael Große return $html; 60321fcef82SMichael Große } 6044c924eb8SMichael Große 6054c924eb8SMichael Große /** 6064c924eb8SMichael Große * create a link to restrict the current query to a namespace 6074c924eb8SMichael Große * 608ec27794fSMichael Große * @param false|string $ns the namespace to which to restrict the query 6094c924eb8SMichael Große * 610ec27794fSMichael Große * @return false|string 6114c924eb8SMichael Große */ 6124c924eb8SMichael Große protected function restrictQueryToNSLink($ns) 6134c924eb8SMichael Große { 6144c924eb8SMichael Große if (!$ns) { 6154c924eb8SMichael Große return false; 6164c924eb8SMichael Große } 617df977249SMichael Große if (!$this->isNamespaceAssistanceAvailable($this->parsedQuery)) { 6184c924eb8SMichael Große return false; 6194c924eb8SMichael Große } 6204c924eb8SMichael Große if (!empty($this->parsedQuery['ns']) && $this->parsedQuery['ns'][0] === $ns) { 6214c924eb8SMichael Große return false; 6224c924eb8SMichael Große } 62352d4cd42SMichael Große 6244c924eb8SMichael Große $name = '@' . $ns; 62552d4cd42SMichael Große return $this->searchState->withNamespace($ns)->getSearchLink($name); 6264c924eb8SMichael Große } 62721fcef82SMichael Große} 628