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; 7*79a2d784SGerrit 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; 1521fcef82SMichael Große protected $pageLookupResults = array(); 1621fcef82SMichael Große protected $fullTextResults = array(); 1721fcef82SMichael Große protected $highlight = array(); 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 { 47*79a2d784SGerrit 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 */ 155df977249SMichael Große protected function isNamespaceAssistanceAvailable(array $parsedQuery) { 156df977249SMichael Große if (preg_match('/[\(\)\|]/', $parsedQuery['query']) === 1) { 157bb8ef867SMichael Große return false; 158bb8ef867SMichael Große } 159df977249SMichael Große 160df977249SMichael Große return true; 161df977249SMichael Große } 162df977249SMichael Große 163be76738bSMichael Große /** 164be76738bSMichael Große * Check if the query is simple enough to modify the fragment search behavior without breaking the rest of the query 165be76738bSMichael Große * 166be76738bSMichael Große * @param array $parsedQuery 167be76738bSMichael Große * 168be76738bSMichael Große * @return bool 169be76738bSMichael Große */ 170df977249SMichael Große protected function isFragmentAssistanceAvailable(array $parsedQuery) { 171df977249SMichael Große if (preg_match('/[\(\)\|]/', $parsedQuery['query']) === 1) { 172bb8ef867SMichael Große return false; 173bb8ef867SMichael Große } 174bb8ef867SMichael Große 175bb8ef867SMichael Große if (!empty($parsedQuery['phrases'])) { 176bb8ef867SMichael Große return false; 177bb8ef867SMichael Große } 178bb8ef867SMichael Große 179bb8ef867SMichael Große return true; 180bb8ef867SMichael Große } 181bb8ef867SMichael Große 182bb8ef867SMichael Große /** 183bb8ef867SMichael Große * Add the elements to be used for search assistance 184bb8ef867SMichael Große * 185bb8ef867SMichael Große * @param Form $searchForm 186bb8ef867SMichael Große */ 18718856c5dSMichael Große protected function addSearchAssistanceElements(Form $searchForm) 188bb8ef867SMichael Große { 189bb8ef867SMichael Große $searchForm->addTagOpen('div') 1904bdf82b5SAndreas Gohr ->addClass('advancedOptions') 1912171f9cbSAndreas Gohr ->attr('style', 'display: none;') 1922171f9cbSAndreas Gohr ->attr('aria-hidden', 'true'); 193bb8ef867SMichael Große 19418856c5dSMichael Große $this->addFragmentBehaviorLinks($searchForm); 19518856c5dSMichael Große $this->addNamespaceSelector($searchForm); 19618856c5dSMichael Große $this->addDateSelector($searchForm); 197b005809cSMichael Große $this->addSortTool($searchForm); 198bb8ef867SMichael Große 199bb8ef867SMichael Große $searchForm->addTagClose('div'); 200bb8ef867SMichael Große } 201bb8ef867SMichael Große 202be76738bSMichael Große /** 203be76738bSMichael Große * Add the elements to adjust the fragment search behavior 204be76738bSMichael Große * 205be76738bSMichael Große * @param Form $searchForm 206be76738bSMichael Große */ 20718856c5dSMichael Große protected function addFragmentBehaviorLinks(Form $searchForm) 2084d0cb6e1SMichael Große { 209df977249SMichael Große if (!$this->isFragmentAssistanceAvailable($this->parsedQuery)) { 210df977249SMichael Große return; 211df977249SMichael Große } 212b005809cSMichael Große global $lang; 2134d0cb6e1SMichael Große 214b005809cSMichael Große $options = [ 215b005809cSMichael Große 'exact' => [ 216b005809cSMichael Große 'label' => $lang['search_exact_match'], 217b005809cSMichael Große 'and' => array_map(function ($term) { 218b005809cSMichael Große return trim($term, '*'); 219b005809cSMichael Große }, $this->parsedQuery['and']), 220df977249SMichael Große 'not' => array_map(function ($term) { 221df977249SMichael Große return trim($term, '*'); 222df977249SMichael Große }, $this->parsedQuery['not']), 223b005809cSMichael Große ], 224b005809cSMichael Große 'starts' => [ 225b005809cSMichael Große 'label' => $lang['search_starts_with'], 226b005809cSMichael Große 'and' => array_map(function ($term) { 227b005809cSMichael Große return trim($term, '*') . '*'; 228df977249SMichael Große }, $this->parsedQuery['and']), 229df977249SMichael Große 'not' => array_map(function ($term) { 230df977249SMichael Große return trim($term, '*') . '*'; 231df977249SMichael Große }, $this->parsedQuery['not']), 232b005809cSMichael Große ], 233b005809cSMichael Große 'ends' => [ 234b005809cSMichael Große 'label' => $lang['search_ends_with'], 235b005809cSMichael Große 'and' => array_map(function ($term) { 236b005809cSMichael Große return '*' . trim($term, '*'); 237df977249SMichael Große }, $this->parsedQuery['and']), 238df977249SMichael Große 'not' => array_map(function ($term) { 239df977249SMichael Große return '*' . trim($term, '*'); 240df977249SMichael Große }, $this->parsedQuery['not']), 241b005809cSMichael Große ], 242b005809cSMichael Große 'contains' => [ 243b005809cSMichael Große 'label' => $lang['search_contains'], 244b005809cSMichael Große 'and' => array_map(function ($term) { 245b005809cSMichael Große return '*' . trim($term, '*') . '*'; 246df977249SMichael Große }, $this->parsedQuery['and']), 247df977249SMichael Große 'not' => array_map(function ($term) { 248df977249SMichael Große return '*' . trim($term, '*') . '*'; 249df977249SMichael Große }, $this->parsedQuery['not']), 250b005809cSMichael Große ] 251b005809cSMichael Große ]; 252b005809cSMichael Große 253b005809cSMichael Große // detect current 254c6b5b74aSMichael Große $activeOption = 'custom'; 255b005809cSMichael Große foreach ($options as $key => $option) { 256b005809cSMichael Große if ($this->parsedQuery['and'] === $option['and']) { 257b005809cSMichael Große $activeOption = $key; 258b005809cSMichael Große } 259b005809cSMichael Große } 260c6b5b74aSMichael Große if ($activeOption === 'custom') { 261c6b5b74aSMichael Große $options = array_merge(['custom' => [ 262c6b5b74aSMichael Große 'label' => $lang['search_custom_match'], 263c6b5b74aSMichael Große ]], $options); 264c6b5b74aSMichael Große } 265b005809cSMichael Große 2662171f9cbSAndreas Gohr $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true'); 267b005809cSMichael Große // render current 2684bdf82b5SAndreas Gohr $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); 269b005809cSMichael Große if ($activeOption !== 'exact') { 2704bdf82b5SAndreas Gohr $currentWrapper->addClass('changed'); 271b005809cSMichael Große } 272b005809cSMichael Große $searchForm->addHTML($options[$activeOption]['label']); 273b005809cSMichael Große $searchForm->addTagClose('div'); 274b005809cSMichael Große 275b005809cSMichael Große // render options list 2762171f9cbSAndreas Gohr $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false'); 277b005809cSMichael Große 278b005809cSMichael Große foreach ($options as $key => $option) { 2794bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 280b005809cSMichael Große 281b005809cSMichael Große if ($key === $activeOption) { 2824bdf82b5SAndreas Gohr $listItem->addClass('active'); 283b005809cSMichael Große $searchForm->addHTML($option['label']); 284b005809cSMichael Große } else { 28552d4cd42SMichael Große $link = $this->searchState 28652d4cd42SMichael Große ->withFragments($option['and'], $option['not']) 28752d4cd42SMichael Große ->getSearchLink($option['label']) 28852d4cd42SMichael Große ; 28952d4cd42SMichael Große $searchForm->addHTML($link); 290b005809cSMichael Große } 291b005809cSMichael Große $searchForm->addTagClose('li'); 292b005809cSMichael Große } 293b005809cSMichael Große $searchForm->addTagClose('ul'); 2944d0cb6e1SMichael Große 2954d0cb6e1SMichael Große $searchForm->addTagClose('div'); 296b005809cSMichael Große 297b005809cSMichael Große // render options list 2984d0cb6e1SMichael Große } 2994d0cb6e1SMichael Große 300bb8ef867SMichael Große /** 301bb8ef867SMichael Große * Add the elements for the namespace selector 302bb8ef867SMichael Große * 303bb8ef867SMichael Große * @param Form $searchForm 304bb8ef867SMichael Große */ 30518856c5dSMichael Große protected function addNamespaceSelector(Form $searchForm) 306bb8ef867SMichael Große { 307df977249SMichael Große if (!$this->isNamespaceAssistanceAvailable($this->parsedQuery)) { 308df977249SMichael Große return; 309df977249SMichael Große } 310df977249SMichael Große 311b005809cSMichael Große global $lang; 312b005809cSMichael Große 31318856c5dSMichael Große $baseNS = empty($this->parsedQuery['ns']) ? '' : $this->parsedQuery['ns'][0]; 314bbc1da2eSMichael Große $extraNS = $this->getAdditionalNamespacesFromResults($baseNS); 3154d0cb6e1SMichael Große 3162171f9cbSAndreas Gohr $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true'); 317b005809cSMichael Große // render current 3184bdf82b5SAndreas Gohr $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); 319bbc1da2eSMichael Große if ($baseNS) { 3204bdf82b5SAndreas Gohr $currentWrapper->addClass('changed'); 321b005809cSMichael Große $searchForm->addHTML('@' . $baseNS); 322b005809cSMichael Große } else { 323b005809cSMichael Große $searchForm->addHTML($lang['search_any_ns']); 324b005809cSMichael Große } 325b005809cSMichael Große $searchForm->addTagClose('div'); 326b005809cSMichael Große 327b005809cSMichael Große // render options list 3282171f9cbSAndreas Gohr $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false'); 329b005809cSMichael Große 3304bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 331b005809cSMichael Große if ($baseNS) { 3324bdf82b5SAndreas Gohr $listItem->addClass('active'); 33352d4cd42SMichael Große $link = $this->searchState->withNamespace('')->getSearchLink($lang['search_any_ns']); 33452d4cd42SMichael Große $searchForm->addHTML($link); 335b005809cSMichael Große } else { 336b005809cSMichael Große $searchForm->addHTML($lang['search_any_ns']); 337bb8ef867SMichael Große } 338b005809cSMichael Große $searchForm->addTagClose('li'); 339bb8ef867SMichael Große 34018856c5dSMichael Große foreach ($extraNS as $ns => $count) { 3414bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 3421d918893SAndreas Gohr $label = $ns . ($count ? " <bdi>($count)</bdi>" : ''); 3434d0cb6e1SMichael Große 344b005809cSMichael Große if ($ns === $baseNS) { 3454bdf82b5SAndreas Gohr $listItem->addClass('active'); 346b005809cSMichael Große $searchForm->addHTML($label); 347b005809cSMichael Große } else { 34852d4cd42SMichael Große $link = $this->searchState->withNamespace($ns)->getSearchLink($label); 34952d4cd42SMichael Große $searchForm->addHTML($link); 350bb8ef867SMichael Große } 351b005809cSMichael Große $searchForm->addTagClose('li'); 352bb8ef867SMichael Große } 353b005809cSMichael Große $searchForm->addTagClose('ul'); 354bb8ef867SMichael Große 355bb8ef867SMichael Große $searchForm->addTagClose('div'); 356b005809cSMichael Große 357bb8ef867SMichael Große } 358bb8ef867SMichael Große 359bb8ef867SMichael Große /** 360bb8ef867SMichael Große * Parse the full text results for their top namespaces below the given base namespace 361bb8ef867SMichael Große * 362bb8ef867SMichael Große * @param string $baseNS the namespace within which was searched, empty string for root namespace 363bb8ef867SMichael Große * 364bb8ef867SMichael Große * @return array an associative array with namespace => #number of found pages, sorted descending 365bb8ef867SMichael Große */ 366bb8ef867SMichael Große protected function getAdditionalNamespacesFromResults($baseNS) 367bb8ef867SMichael Große { 368bb8ef867SMichael Große $namespaces = []; 369bb8ef867SMichael Große $baseNSLength = strlen($baseNS); 370bb8ef867SMichael Große foreach ($this->fullTextResults as $page => $numberOfHits) { 371bb8ef867SMichael Große $namespace = getNS($page); 372bb8ef867SMichael Große if (!$namespace) { 373bb8ef867SMichael Große continue; 374bb8ef867SMichael Große } 375bb8ef867SMichael Große if ($namespace === $baseNS) { 376bb8ef867SMichael Große continue; 377bb8ef867SMichael Große } 378bb8ef867SMichael Große $firstColon = strpos((string)$namespace, ':', $baseNSLength + 1) ?: strlen($namespace); 379bb8ef867SMichael Große $subtopNS = substr($namespace, 0, $firstColon); 380bb8ef867SMichael Große if (empty($namespaces[$subtopNS])) { 381bb8ef867SMichael Große $namespaces[$subtopNS] = 0; 382bb8ef867SMichael Große } 383bb8ef867SMichael Große $namespaces[$subtopNS] += 1; 384bb8ef867SMichael Große } 3852d85e841SAndreas Gohr Sort::ksort($namespaces); 386bb8ef867SMichael Große arsort($namespaces); 387bb8ef867SMichael Große return $namespaces; 388bb8ef867SMichael Große } 389bb8ef867SMichael Große 390bb8ef867SMichael Große /** 391bbc1da2eSMichael Große * @ToDo: custom date input 392bbc1da2eSMichael Große * 393bbc1da2eSMichael Große * @param Form $searchForm 394bbc1da2eSMichael Große */ 395b005809cSMichael Große protected function addDateSelector(Form $searchForm) 396b005809cSMichael Große { 397b005809cSMichael Große global $INPUT, $lang; 398bbc1da2eSMichael Große 399b005809cSMichael Große $options = [ 400b005809cSMichael Große 'any' => [ 401b005809cSMichael Große 'before' => false, 402b005809cSMichael Große 'after' => false, 403b005809cSMichael Große 'label' => $lang['search_any_time'], 404b005809cSMichael Große ], 405b005809cSMichael Große 'week' => [ 406b005809cSMichael Große 'before' => false, 407b005809cSMichael Große 'after' => '1 week ago', 408b005809cSMichael Große 'label' => $lang['search_past_7_days'], 409b005809cSMichael Große ], 410b005809cSMichael Große 'month' => [ 411b005809cSMichael Große 'before' => false, 412b005809cSMichael Große 'after' => '1 month ago', 413b005809cSMichael Große 'label' => $lang['search_past_month'], 414b005809cSMichael Große ], 415b005809cSMichael Große 'year' => [ 416b005809cSMichael Große 'before' => false, 417b005809cSMichael Große 'after' => '1 year ago', 418b005809cSMichael Große 'label' => $lang['search_past_year'], 419b005809cSMichael Große ], 420b005809cSMichael Große ]; 421b005809cSMichael Große $activeOption = 'any'; 422b005809cSMichael Große foreach ($options as $key => $option) { 423422bbbc6SMichael Große if ($INPUT->str('min') === $option['after']) { 424b005809cSMichael Große $activeOption = $key; 425b005809cSMichael Große break; 426b005809cSMichael Große } 427b005809cSMichael Große } 428b005809cSMichael Große 4292171f9cbSAndreas Gohr $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true'); 430b005809cSMichael Große // render current 4314bdf82b5SAndreas Gohr $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); 432422bbbc6SMichael Große if ($INPUT->has('max') || $INPUT->has('min')) { 4334bdf82b5SAndreas Gohr $currentWrapper->addClass('changed'); 434bbc1da2eSMichael Große } 435b005809cSMichael Große $searchForm->addHTML($options[$activeOption]['label']); 436b005809cSMichael Große $searchForm->addTagClose('div'); 437bbc1da2eSMichael Große 438b005809cSMichael Große // render options list 4392171f9cbSAndreas Gohr $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false'); 440b005809cSMichael Große 441b005809cSMichael Große foreach ($options as $key => $option) { 4424bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 443b005809cSMichael Große 444b005809cSMichael Große if ($key === $activeOption) { 4454bdf82b5SAndreas Gohr $listItem->addClass('active'); 446b005809cSMichael Große $searchForm->addHTML($option['label']); 447bbc1da2eSMichael Große } else { 44852d4cd42SMichael Große $link = $this->searchState 44952d4cd42SMichael Große ->withTimeLimitations($option['after'], $option['before']) 45052d4cd42SMichael Große ->getSearchLink($option['label']) 45152d4cd42SMichael Große ; 45252d4cd42SMichael Große $searchForm->addHTML($link); 453bbc1da2eSMichael Große } 454b005809cSMichael Große $searchForm->addTagClose('li'); 455bbc1da2eSMichael Große } 456b005809cSMichael Große $searchForm->addTagClose('ul'); 457bbc1da2eSMichael Große 458bbc1da2eSMichael Große $searchForm->addTagClose('div'); 459bbc1da2eSMichael Große } 460bbc1da2eSMichael Große 461bbc1da2eSMichael Große 462bbc1da2eSMichael Große /** 46321fcef82SMichael Große * Build the intro text for the search page 46421fcef82SMichael Große * 46521fcef82SMichael Große * @param string $query the search query 46621fcef82SMichael Große * 46721fcef82SMichael Große * @return string 46821fcef82SMichael Große */ 46921fcef82SMichael Große protected function getSearchIntroHTML($query) 47021fcef82SMichael Große { 4712ce8affcSMichael Große global $lang; 47221fcef82SMichael Große 47321fcef82SMichael Große $intro = p_locale_xhtml('searchpage'); 4742ce8affcSMichael Große 4752ce8affcSMichael Große $queryPagename = $this->createPagenameFromQuery($this->parsedQuery); 4762ce8affcSMichael Große $createQueryPageLink = html_wikilink($queryPagename . '?do=edit', $queryPagename); 4772ce8affcSMichael Große 4782ce8affcSMichael Große $pagecreateinfo = ''; 4792ce8affcSMichael Große if (auth_quickaclcheck($queryPagename) >= AUTH_CREATE) { 4802ce8affcSMichael Große $pagecreateinfo = sprintf($lang['searchcreatepage'], $createQueryPageLink); 4812ce8affcSMichael Große } 482*79a2d784SGerrit Uitslag return str_replace( 48321fcef82SMichael Große array('@QUERY@', '@SEARCH@', '@CREATEPAGEINFO@'), 48421fcef82SMichael Große array(hsc(rawurlencode($query)), hsc($query), $pagecreateinfo), 48521fcef82SMichael Große $intro 48621fcef82SMichael Große ); 48721fcef82SMichael Große } 48821fcef82SMichael Große 48921fcef82SMichael Große /** 4902ce8affcSMichael Große * Create a pagename based the parsed search query 4912ce8affcSMichael Große * 4922ce8affcSMichael Große * @param array $parsedQuery 4932ce8affcSMichael Große * 4942ce8affcSMichael Große * @return string pagename constructed from the parsed query 4952ce8affcSMichael Große */ 49642690e4dSMichael Große public function createPagenameFromQuery($parsedQuery) 4972ce8affcSMichael Große { 498e180e453SPhy $cleanedQuery = cleanID($parsedQuery['query']); // already strtolowered 499*79a2d784SGerrit Uitslag if ($cleanedQuery === PhpString::strtolower($parsedQuery['query'])) { 50042690e4dSMichael Große return ':' . $cleanedQuery; 50142690e4dSMichael Große } 5022ce8affcSMichael Große $pagename = ''; 5032ce8affcSMichael Große if (!empty($parsedQuery['ns'])) { 50442690e4dSMichael Große $pagename .= ':' . cleanID($parsedQuery['ns'][0]); 5052ce8affcSMichael Große } 5062ce8affcSMichael Große $pagename .= ':' . cleanID(implode(' ' , $parsedQuery['highlight'])); 5072ce8affcSMichael Große return $pagename; 5082ce8affcSMichael Große } 5092ce8affcSMichael Große 5102ce8affcSMichael Große /** 51121fcef82SMichael Große * Build HTML for a list of pages with matching pagenames 51221fcef82SMichael Große * 51321fcef82SMichael Große * @param array $data search results 51421fcef82SMichael Große * 51521fcef82SMichael Große * @return string 51621fcef82SMichael Große */ 51721fcef82SMichael Große protected function getPageLookupHTML($data) 51821fcef82SMichael Große { 51921fcef82SMichael Große if (empty($data)) { 52021fcef82SMichael Große return ''; 52121fcef82SMichael Große } 52221fcef82SMichael Große 52321fcef82SMichael Große global $lang; 52421fcef82SMichael Große 52521fcef82SMichael Große $html = '<div class="search_quickresult">'; 5266d55fda7SMichael Große $html .= '<h2>' . $lang['quickhits'] . ':</h2>'; 52721fcef82SMichael Große $html .= '<ul class="search_quickhits">'; 52821fcef82SMichael Große foreach ($data as $id => $title) { 5295d87aa31SMichael Große $name = null; 5305d87aa31SMichael Große if (!useHeading('navigation') && $ns = getNS($id)) { 5315d87aa31SMichael Große $name = shorten(noNS($id), ' (' . $ns . ')', 30); 5325d87aa31SMichael Große } 5335d87aa31SMichael Große $link = html_wikilink(':' . $id, $name); 5344eab6f7cSMichael Große $eventData = [ 5354eab6f7cSMichael Große 'listItemContent' => [$link], 5364eab6f7cSMichael Große 'page' => $id, 5374eab6f7cSMichael Große ]; 538cbb44eabSAndreas Gohr Event::createAndTrigger('SEARCH_RESULT_PAGELOOKUP', $eventData); 5394eab6f7cSMichael Große $html .= '<li>' . implode('', $eventData['listItemContent']) . '</li>'; 54021fcef82SMichael Große } 54121fcef82SMichael Große $html .= '</ul> '; 54221fcef82SMichael Große //clear float (see http://www.complexspiral.com/publications/containing-floats/) 54321fcef82SMichael Große $html .= '<div class="clearer"></div>'; 54421fcef82SMichael Große $html .= '</div>'; 54521fcef82SMichael Große 54621fcef82SMichael Große return $html; 54721fcef82SMichael Große } 54821fcef82SMichael Große 54921fcef82SMichael Große /** 55021fcef82SMichael Große * Build HTML for fulltext search results or "no results" message 55121fcef82SMichael Große * 55221fcef82SMichael Große * @param array $data the results of the fulltext search 55321fcef82SMichael Große * @param array $highlight the terms to be highlighted in the results 55421fcef82SMichael Große * 55521fcef82SMichael Große * @return string 55621fcef82SMichael Große */ 55721fcef82SMichael Große protected function getFulltextResultsHTML($data, $highlight) 55821fcef82SMichael Große { 55921fcef82SMichael Große global $lang; 56021fcef82SMichael Große 56121fcef82SMichael Große if (empty($data)) { 56221fcef82SMichael Große return '<div class="nothing">' . $lang['nothingfound'] . '</div>'; 56321fcef82SMichael Große } 56421fcef82SMichael Große 5652ce8affcSMichael Große $html = '<div class="search_fulltextresult">'; 5666d55fda7SMichael Große $html .= '<h2>' . $lang['search_fullresults'] . ':</h2>'; 5672ce8affcSMichael Große 56821fcef82SMichael Große $html .= '<dl class="search_results">'; 5698225e1abSMichael Große $num = 0; 5708225e1abSMichael Große $position = 0; 5714c924eb8SMichael Große 57221fcef82SMichael Große foreach ($data as $id => $cnt) { 57378d786c9SMichael Große $position += 1; 5744eab6f7cSMichael Große $resultLink = html_wikilink(':' . $id, null, $highlight); 5754c924eb8SMichael Große 5764c924eb8SMichael Große $resultHeader = [$resultLink]; 5774c924eb8SMichael Große 5784eab6f7cSMichael Große 5794c924eb8SMichael Große $restrictQueryToNSLink = $this->restrictQueryToNSLink(getNS($id)); 5804c924eb8SMichael Große if ($restrictQueryToNSLink) { 5814c924eb8SMichael Große $resultHeader[] = $restrictQueryToNSLink; 5824c924eb8SMichael Große } 5834c924eb8SMichael Große 5845d06a1e4SMichael Große $resultBody = []; 5859a75abfbSMichael Große $mtime = filemtime(wikiFN($id)); 5865d06a1e4SMichael Große $lastMod = '<span class="lastmod">' . $lang['lastmod'] . '</span> '; 58764159a61SAndreas Gohr $lastMod .= '<time datetime="' . date_iso8601($mtime) . '" title="' . dformat($mtime) . '">' . 58864159a61SAndreas Gohr dformat($mtime, '%f') . 58964159a61SAndreas Gohr '</time>'; 590f0861d1fSMichael Große $resultBody['meta'] = $lastMod; 5915d06a1e4SMichael Große if ($cnt !== 0) { 5928225e1abSMichael Große $num++; 593b12bcb77SAnika Henke $hits = '<span class="hits">' . $cnt . ' ' . $lang['hits'] . '</span>, '; 594f0861d1fSMichael Große $resultBody['meta'] = $hits . $resultBody['meta']; 5958225e1abSMichael Große if ($num <= FT_SNIPPET_NUMBER) { // create snippets for the first number of matches only 596f0861d1fSMichael Große $resultBody['snippet'] = ft_snippet($id, $highlight); 5979a75abfbSMichael Große } 5989a75abfbSMichael Große } 5999a75abfbSMichael Große 6004eab6f7cSMichael Große $eventData = [ 6014c924eb8SMichael Große 'resultHeader' => $resultHeader, 6025d06a1e4SMichael Große 'resultBody' => $resultBody, 6034eab6f7cSMichael Große 'page' => $id, 60478d786c9SMichael Große 'position' => $position, 6054eab6f7cSMichael Große ]; 606cbb44eabSAndreas Gohr Event::createAndTrigger('SEARCH_RESULT_FULLPAGE', $eventData); 6075d06a1e4SMichael Große $html .= '<div class="search_fullpage_result">'; 6084eab6f7cSMichael Große $html .= '<dt>' . implode(' ', $eventData['resultHeader']) . '</dt>'; 6095d06a1e4SMichael Große foreach ($eventData['resultBody'] as $class => $htmlContent) { 6105d06a1e4SMichael Große $html .= "<dd class=\"$class\">$htmlContent</dd>"; 6115d06a1e4SMichael Große } 6125d06a1e4SMichael Große $html .= '</div>'; 61321fcef82SMichael Große } 61421fcef82SMichael Große $html .= '</dl>'; 61521fcef82SMichael Große 6162ce8affcSMichael Große $html .= '</div>'; 6172ce8affcSMichael Große 61821fcef82SMichael Große return $html; 61921fcef82SMichael Große } 6204c924eb8SMichael Große 6214c924eb8SMichael Große /** 6224c924eb8SMichael Große * create a link to restrict the current query to a namespace 6234c924eb8SMichael Große * 624ec27794fSMichael Große * @param false|string $ns the namespace to which to restrict the query 6254c924eb8SMichael Große * 626ec27794fSMichael Große * @return false|string 6274c924eb8SMichael Große */ 6284c924eb8SMichael Große protected function restrictQueryToNSLink($ns) 6294c924eb8SMichael Große { 6304c924eb8SMichael Große if (!$ns) { 6314c924eb8SMichael Große return false; 6324c924eb8SMichael Große } 633df977249SMichael Große if (!$this->isNamespaceAssistanceAvailable($this->parsedQuery)) { 6344c924eb8SMichael Große return false; 6354c924eb8SMichael Große } 6364c924eb8SMichael Große if (!empty($this->parsedQuery['ns']) && $this->parsedQuery['ns'][0] === $ns) { 6374c924eb8SMichael Große return false; 6384c924eb8SMichael Große } 63952d4cd42SMichael Große 6404c924eb8SMichael Große $name = '@' . $ns; 64152d4cd42SMichael Große return $this->searchState->withNamespace($ns)->getSearchLink($name); 6424c924eb8SMichael Große } 64321fcef82SMichael Große} 644