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; 70cba610bSSatoshi Saharause dokuwiki\Search\FulltextSearch; 8*0b1bbbbbSAndreas Gohruse dokuwiki\Search\Query\QueryParser; 979a2d784SGerrit Uitslaguse dokuwiki\Utf8\PhpString; 102d85e841SAndreas Gohruse dokuwiki\Utf8\Sort; 110cba610bSSatoshi Sahara 12427ed988SMichael Große 13b9c8f036SAndreas Gohr 1421fcef82SMichael Großeclass Search extends Ui 1521fcef82SMichael Große{ 1621fcef82SMichael Große protected $query; 174c924eb8SMichael Große protected $parsedQuery; 1818856c5dSMichael Große protected $searchState; 19e2d055f5SAndreas Gohr protected $pageLookupResults = []; 20e2d055f5SAndreas Gohr protected $fullTextResults = []; 21e2d055f5SAndreas Gohr protected $highlight = []; 2221fcef82SMichael Große 2321fcef82SMichael Große /** 2421fcef82SMichael Große * Search constructor. 256639a152SMichael Große * 26fc46ed58SMichael Große * @param array $pageLookupResults pagename lookup results in the form [pagename => pagetitle] 27fc46ed58SMichael Große * @param array $fullTextResults fulltext search results in the form [pagename => #hits] 28fc46ed58SMichael Große * @param array $highlight array of strings to be highlighted 2921fcef82SMichael Große */ 306639a152SMichael Große public function __construct(array $pageLookupResults, array $fullTextResults, $highlight) 3121fcef82SMichael Große { 32d09b5b64SMichael Große global $QUERY; 33d09b5b64SMichael Große 34d09b5b64SMichael Große $this->query = $QUERY; 359329b002SSatoshi Sahara $this->parsedQuery = (new QueryParser)->convert($QUERY); 3618856c5dSMichael Große $this->searchState = new SearchState($this->parsedQuery); 3721fcef82SMichael Große 386639a152SMichael Große $this->pageLookupResults = $pageLookupResults; 396639a152SMichael Große $this->fullTextResults = $fullTextResults; 4021fcef82SMichael Große $this->highlight = $highlight; 41b3cfe85aSMichael Große } 42bbc1da2eSMichael Große 43b3cfe85aSMichael Große /** 4421fcef82SMichael Große * display the search result 4521fcef82SMichael Große * 4621fcef82SMichael Große * @return void 4721fcef82SMichael Große */ 4821fcef82SMichael Große public function show() 4921fcef82SMichael Große { 5079a2d784SGerrit Uitslag $searchHTML = $this->getSearchIntroHTML($this->query); 5121fcef82SMichael Große 522ce8affcSMichael Große $searchHTML .= $this->getSearchFormHTML($this->query); 532ce8affcSMichael Große 5421fcef82SMichael Große $searchHTML .= $this->getPageLookupHTML($this->pageLookupResults); 5521fcef82SMichael Große 5621fcef82SMichael Große $searchHTML .= $this->getFulltextResultsHTML($this->fullTextResults, $this->highlight); 5721fcef82SMichael Große 5821fcef82SMichael Große echo $searchHTML; 5921fcef82SMichael Große } 6021fcef82SMichael Große 6121fcef82SMichael Große /** 62427ed988SMichael Große * Get a form which can be used to adjust/refine the search 63427ed988SMichael Große * 64427ed988SMichael Große * @param string $query 65427ed988SMichael Große * 66427ed988SMichael Große * @return string 67427ed988SMichael Große */ 68427ed988SMichael Große protected function getSearchFormHTML($query) 69427ed988SMichael Große { 70bbc1da2eSMichael Große global $lang, $ID, $INPUT; 71427ed988SMichael Große 727fa270bcSMichael Große $searchForm = (new Form(['method' => 'get'], true))->addClass('search-results-form'); 73bb8ef867SMichael Große $searchForm->setHiddenField('do', 'search'); 74d22b78c8SMichael Große $searchForm->setHiddenField('id', $ID); 751265b193SMichael Große $searchForm->setHiddenField('sf', '1'); 76422bbbc6SMichael Große if ($INPUT->has('min')) { 77422bbbc6SMichael Große $searchForm->setHiddenField('min', $INPUT->str('min')); 78bbc1da2eSMichael Große } 79422bbbc6SMichael Große if ($INPUT->has('max')) { 80422bbbc6SMichael Große $searchForm->setHiddenField('max', $INPUT->str('max')); 81bbc1da2eSMichael Große } 821265b193SMichael Große if ($INPUT->has('srt')) { 831265b193SMichael Große $searchForm->setHiddenField('srt', $INPUT->str('srt')); 848d0e286aSMichael Große } 854bdf82b5SAndreas Gohr $searchForm->addFieldsetOpen()->addClass('search-form'); 86d22b78c8SMichael Große $searchForm->addTextInput('q')->val($query)->useInput(false); 87427ed988SMichael Große $searchForm->addButton('', $lang['btn_search'])->attr('type', 'submit'); 88bb8ef867SMichael Große 8918856c5dSMichael Große $this->addSearchAssistanceElements($searchForm); 90bb8ef867SMichael Große 91427ed988SMichael Große $searchForm->addFieldsetClose(); 92427ed988SMichael Große 93c6977b3aSSatoshi Sahara return $searchForm->toHTML('Search'); 94427ed988SMichael Große } 95427ed988SMichael Große 96be76738bSMichael Große /** 97be76738bSMichael Große * Add elements to adjust how the results are sorted 98be76738bSMichael Große * 99be76738bSMichael Große * @param Form $searchForm 100be76738bSMichael Große */ 101b005809cSMichael Große protected function addSortTool(Form $searchForm) 102b005809cSMichael Große { 103b005809cSMichael Große global $INPUT, $lang; 104b005809cSMichael Große 105b005809cSMichael Große $options = [ 106b005809cSMichael Große 'hits' => [ 107b005809cSMichael Große 'label' => $lang['search_sort_by_hits'], 108b005809cSMichael Große 'sort' => '', 109b005809cSMichael Große ], 110b005809cSMichael Große 'mtime' => [ 111b005809cSMichael Große 'label' => $lang['search_sort_by_mtime'], 112b005809cSMichael Große 'sort' => 'mtime', 113b005809cSMichael Große ], 114b005809cSMichael Große ]; 115b005809cSMichael Große $activeOption = 'hits'; 116b005809cSMichael Große 1171265b193SMichael Große if ($INPUT->str('srt') === 'mtime') { 118b005809cSMichael Große $activeOption = 'mtime'; 119b005809cSMichael Große } 120b005809cSMichael Große 1212171f9cbSAndreas Gohr $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true'); 122b005809cSMichael Große // render current 1234bdf82b5SAndreas Gohr $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); 124b005809cSMichael Große if ($activeOption !== 'hits') { 1254bdf82b5SAndreas Gohr $currentWrapper->addClass('changed'); 126b005809cSMichael Große } 127b005809cSMichael Große $searchForm->addHTML($options[$activeOption]['label']); 128b005809cSMichael Große $searchForm->addTagClose('div'); 129b005809cSMichael Große 130b005809cSMichael Große // render options list 1312171f9cbSAndreas Gohr $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false'); 132b005809cSMichael Große 133b005809cSMichael Große foreach ($options as $key => $option) { 1344bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 135b005809cSMichael Große 136b005809cSMichael Große if ($key === $activeOption) { 1374bdf82b5SAndreas Gohr $listItem->addClass('active'); 138b005809cSMichael Große $searchForm->addHTML($option['label']); 139b005809cSMichael Große } else { 14052d4cd42SMichael Große $link = $this->searchState->withSorting($option['sort'])->getSearchLink($option['label']); 14152d4cd42SMichael Große $searchForm->addHTML($link); 142b005809cSMichael Große } 143b005809cSMichael Große $searchForm->addTagClose('li'); 144b005809cSMichael Große } 145b005809cSMichael Große $searchForm->addTagClose('ul'); 146b005809cSMichael Große 147b005809cSMichael Große $searchForm->addTagClose('div'); 148b005809cSMichael Große } 149b005809cSMichael Große 150be76738bSMichael Große /** 151be76738bSMichael Große * Check if the query is simple enough to modify its namespace limitations without breaking the rest of the query 152be76738bSMichael Große * 153be76738bSMichael Große * @param array $parsedQuery 154be76738bSMichael Große * 155be76738bSMichael Große * @return bool 156be76738bSMichael Große */ 157e2d055f5SAndreas Gohr protected function isNamespaceAssistanceAvailable(array $parsedQuery) 158e2d055f5SAndreas Gohr { 159df977249SMichael Große if (preg_match('/[\(\)\|]/', $parsedQuery['query']) === 1) { 160bb8ef867SMichael Große return false; 161bb8ef867SMichael Große } 162df977249SMichael Große 163df977249SMichael Große return true; 164df977249SMichael Große } 165df977249SMichael Große 166be76738bSMichael Große /** 167be76738bSMichael Große * Check if the query is simple enough to modify the fragment search behavior without breaking the rest of the query 168be76738bSMichael Große * 169be76738bSMichael Große * @param array $parsedQuery 170be76738bSMichael Große * 171be76738bSMichael Große * @return bool 172be76738bSMichael Große */ 173e2d055f5SAndreas Gohr protected function isFragmentAssistanceAvailable(array $parsedQuery) 174e2d055f5SAndreas Gohr { 175df977249SMichael Große if (preg_match('/[\(\)\|]/', $parsedQuery['query']) === 1) { 176bb8ef867SMichael Große return false; 177bb8ef867SMichael Große } 178bb8ef867SMichael Große 179bb8ef867SMichael Große if (!empty($parsedQuery['phrases'])) { 180bb8ef867SMichael Große return false; 181bb8ef867SMichael Große } 182bb8ef867SMichael Große 183bb8ef867SMichael Große return true; 184bb8ef867SMichael Große } 185bb8ef867SMichael Große 186bb8ef867SMichael Große /** 187bb8ef867SMichael Große * Add the elements to be used for search assistance 188bb8ef867SMichael Große * 189bb8ef867SMichael Große * @param Form $searchForm 190bb8ef867SMichael Große */ 19118856c5dSMichael Große protected function addSearchAssistanceElements(Form $searchForm) 192bb8ef867SMichael Große { 193bb8ef867SMichael Große $searchForm->addTagOpen('div') 1944bdf82b5SAndreas Gohr ->addClass('advancedOptions') 1952171f9cbSAndreas Gohr ->attr('style', 'display: none;') 1962171f9cbSAndreas Gohr ->attr('aria-hidden', 'true'); 197bb8ef867SMichael Große 19818856c5dSMichael Große $this->addFragmentBehaviorLinks($searchForm); 19918856c5dSMichael Große $this->addNamespaceSelector($searchForm); 20018856c5dSMichael Große $this->addDateSelector($searchForm); 201b005809cSMichael Große $this->addSortTool($searchForm); 202bb8ef867SMichael Große 203bb8ef867SMichael Große $searchForm->addTagClose('div'); 204bb8ef867SMichael Große } 205bb8ef867SMichael Große 206be76738bSMichael Große /** 207be76738bSMichael Große * Add the elements to adjust the fragment search behavior 208be76738bSMichael Große * 209be76738bSMichael Große * @param Form $searchForm 210be76738bSMichael Große */ 21118856c5dSMichael Große protected function addFragmentBehaviorLinks(Form $searchForm) 2124d0cb6e1SMichael Große { 213df977249SMichael Große if (!$this->isFragmentAssistanceAvailable($this->parsedQuery)) { 214df977249SMichael Große return; 215df977249SMichael Große } 216b005809cSMichael Große global $lang; 2174d0cb6e1SMichael Große 218b005809cSMichael Große $options = [ 219b005809cSMichael Große 'exact' => [ 220b005809cSMichael Große 'label' => $lang['search_exact_match'], 221e2d055f5SAndreas Gohr 'and' => array_map(static fn($term) => trim($term, '*'), $this->parsedQuery['and']), 222e2d055f5SAndreas Gohr 'not' => array_map(static fn($term) => trim($term, '*'), $this->parsedQuery['not']), 223b005809cSMichael Große ], 224b005809cSMichael Große 'starts' => [ 225b005809cSMichael Große 'label' => $lang['search_starts_with'], 226e2d055f5SAndreas Gohr 'and' => array_map(static fn($term) => trim($term, '*') . '*', $this->parsedQuery['and']), 227e2d055f5SAndreas Gohr 'not' => array_map(static fn($term) => trim($term, '*') . '*', $this->parsedQuery['not']), 228b005809cSMichael Große ], 229b005809cSMichael Große 'ends' => [ 230b005809cSMichael Große 'label' => $lang['search_ends_with'], 231e2d055f5SAndreas Gohr 'and' => array_map(static fn($term) => '*' . trim($term, '*'), $this->parsedQuery['and']), 232e2d055f5SAndreas Gohr 'not' => array_map(static fn($term) => '*' . trim($term, '*'), $this->parsedQuery['not']), 233b005809cSMichael Große ], 234b005809cSMichael Große 'contains' => [ 235b005809cSMichael Große 'label' => $lang['search_contains'], 236e2d055f5SAndreas Gohr 'and' => array_map(static fn($term) => '*' . trim($term, '*') . '*', $this->parsedQuery['and']), 237e2d055f5SAndreas Gohr 'not' => array_map(static fn($term) => '*' . trim($term, '*') . '*', $this->parsedQuery['not']), 238b005809cSMichael Große ] 239b005809cSMichael Große ]; 240b005809cSMichael Große 241b005809cSMichael Große // detect current 242c6b5b74aSMichael Große $activeOption = 'custom'; 243b005809cSMichael Große foreach ($options as $key => $option) { 244b005809cSMichael Große if ($this->parsedQuery['and'] === $option['and']) { 245b005809cSMichael Große $activeOption = $key; 246b005809cSMichael Große } 247b005809cSMichael Große } 248c6b5b74aSMichael Große if ($activeOption === 'custom') { 249c6b5b74aSMichael Große $options = array_merge(['custom' => [ 250c6b5b74aSMichael Große 'label' => $lang['search_custom_match'], 251c6b5b74aSMichael Große ]], $options); 252c6b5b74aSMichael Große } 253b005809cSMichael Große 2542171f9cbSAndreas Gohr $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true'); 255b005809cSMichael Große // render current 2564bdf82b5SAndreas Gohr $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); 257b005809cSMichael Große if ($activeOption !== 'exact') { 2584bdf82b5SAndreas Gohr $currentWrapper->addClass('changed'); 259b005809cSMichael Große } 260b005809cSMichael Große $searchForm->addHTML($options[$activeOption]['label']); 261b005809cSMichael Große $searchForm->addTagClose('div'); 262b005809cSMichael Große 263b005809cSMichael Große // render options list 2642171f9cbSAndreas Gohr $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false'); 265b005809cSMichael Große 266b005809cSMichael Große foreach ($options as $key => $option) { 2674bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 268b005809cSMichael Große 269b005809cSMichael Große if ($key === $activeOption) { 2704bdf82b5SAndreas Gohr $listItem->addClass('active'); 271b005809cSMichael Große $searchForm->addHTML($option['label']); 272b005809cSMichael Große } else { 27352d4cd42SMichael Große $link = $this->searchState 27452d4cd42SMichael Große ->withFragments($option['and'], $option['not']) 275e2d055f5SAndreas Gohr ->getSearchLink($option['label']); 27652d4cd42SMichael Große $searchForm->addHTML($link); 277b005809cSMichael Große } 278b005809cSMichael Große $searchForm->addTagClose('li'); 279b005809cSMichael Große } 280b005809cSMichael Große $searchForm->addTagClose('ul'); 2814d0cb6e1SMichael Große 2824d0cb6e1SMichael Große $searchForm->addTagClose('div'); 283b005809cSMichael Große 284b005809cSMichael Große // render options list 2854d0cb6e1SMichael Große } 2864d0cb6e1SMichael Große 287bb8ef867SMichael Große /** 288bb8ef867SMichael Große * Add the elements for the namespace selector 289bb8ef867SMichael Große * 290bb8ef867SMichael Große * @param Form $searchForm 291bb8ef867SMichael Große */ 29218856c5dSMichael Große protected function addNamespaceSelector(Form $searchForm) 293bb8ef867SMichael Große { 294df977249SMichael Große if (!$this->isNamespaceAssistanceAvailable($this->parsedQuery)) { 295df977249SMichael Große return; 296df977249SMichael Große } 297df977249SMichael Große 298b005809cSMichael Große global $lang; 299b005809cSMichael Große 30018856c5dSMichael Große $baseNS = empty($this->parsedQuery['ns']) ? '' : $this->parsedQuery['ns'][0]; 301bbc1da2eSMichael Große $extraNS = $this->getAdditionalNamespacesFromResults($baseNS); 3024d0cb6e1SMichael Große 3032171f9cbSAndreas Gohr $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true'); 304b005809cSMichael Große // render current 3054bdf82b5SAndreas Gohr $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); 306bbc1da2eSMichael Große if ($baseNS) { 3074bdf82b5SAndreas Gohr $currentWrapper->addClass('changed'); 30803fdedf7SAndreas Gohr $searchForm->addHTML('@' . hsc($baseNS)); 309b005809cSMichael Große } else { 310b005809cSMichael Große $searchForm->addHTML($lang['search_any_ns']); 311b005809cSMichael Große } 312b005809cSMichael Große $searchForm->addTagClose('div'); 313b005809cSMichael Große 314b005809cSMichael Große // render options list 3152171f9cbSAndreas Gohr $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false'); 316b005809cSMichael Große 3174bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 318b005809cSMichael Große if ($baseNS) { 3194bdf82b5SAndreas Gohr $listItem->addClass('active'); 32052d4cd42SMichael Große $link = $this->searchState->withNamespace('')->getSearchLink($lang['search_any_ns']); 32152d4cd42SMichael Große $searchForm->addHTML($link); 322b005809cSMichael Große } else { 323b005809cSMichael Große $searchForm->addHTML($lang['search_any_ns']); 324bb8ef867SMichael Große } 325b005809cSMichael Große $searchForm->addTagClose('li'); 326bb8ef867SMichael Große 32718856c5dSMichael Große foreach ($extraNS as $ns => $count) { 3284bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 32903fdedf7SAndreas Gohr $label = hsc($ns) . ($count ? " <bdi>($count)</bdi>" : ''); 3304d0cb6e1SMichael Große 331b005809cSMichael Große if ($ns === $baseNS) { 3324bdf82b5SAndreas Gohr $listItem->addClass('active'); 333b005809cSMichael Große $searchForm->addHTML($label); 334b005809cSMichael Große } else { 33552d4cd42SMichael Große $link = $this->searchState->withNamespace($ns)->getSearchLink($label); 33652d4cd42SMichael Große $searchForm->addHTML($link); 337bb8ef867SMichael Große } 338b005809cSMichael Große $searchForm->addTagClose('li'); 339bb8ef867SMichael Große } 340b005809cSMichael Große $searchForm->addTagClose('ul'); 341bb8ef867SMichael Große 342bb8ef867SMichael Große $searchForm->addTagClose('div'); 343bb8ef867SMichael Große } 344bb8ef867SMichael Große 345bb8ef867SMichael Große /** 346bb8ef867SMichael Große * Parse the full text results for their top namespaces below the given base namespace 347bb8ef867SMichael Große * 348bb8ef867SMichael Große * @param string $baseNS the namespace within which was searched, empty string for root namespace 349bb8ef867SMichael Große * 350bb8ef867SMichael Große * @return array an associative array with namespace => #number of found pages, sorted descending 351bb8ef867SMichael Große */ 352bb8ef867SMichael Große protected function getAdditionalNamespacesFromResults($baseNS) 353bb8ef867SMichael Große { 354bb8ef867SMichael Große $namespaces = []; 355bb8ef867SMichael Große $baseNSLength = strlen($baseNS); 356bb8ef867SMichael Große foreach ($this->fullTextResults as $page => $numberOfHits) { 357bb8ef867SMichael Große $namespace = getNS($page); 358bb8ef867SMichael Große if (!$namespace) { 359bb8ef867SMichael Große continue; 360bb8ef867SMichael Große } 361bb8ef867SMichael Große if ($namespace === $baseNS) { 362bb8ef867SMichael Große continue; 363bb8ef867SMichael Große } 364bb8ef867SMichael Große $firstColon = strpos((string)$namespace, ':', $baseNSLength + 1) ?: strlen($namespace); 365bb8ef867SMichael Große $subtopNS = substr($namespace, 0, $firstColon); 366bb8ef867SMichael Große if (empty($namespaces[$subtopNS])) { 367bb8ef867SMichael Große $namespaces[$subtopNS] = 0; 368bb8ef867SMichael Große } 369e2d055f5SAndreas Gohr ++$namespaces[$subtopNS]; 370bb8ef867SMichael Große } 3712d85e841SAndreas Gohr Sort::ksort($namespaces); 372bb8ef867SMichael Große arsort($namespaces); 373bb8ef867SMichael Große return $namespaces; 374bb8ef867SMichael Große } 375bb8ef867SMichael Große 376bb8ef867SMichael Große /** 377bbc1da2eSMichael Große * @ToDo: custom date input 378bbc1da2eSMichael Große * 379bbc1da2eSMichael Große * @param Form $searchForm 380bbc1da2eSMichael Große */ 381b005809cSMichael Große protected function addDateSelector(Form $searchForm) 382b005809cSMichael Große { 383b005809cSMichael Große global $INPUT, $lang; 384bbc1da2eSMichael Große 385b005809cSMichael Große $options = [ 386b005809cSMichael Große 'any' => [ 387b005809cSMichael Große 'before' => false, 388b005809cSMichael Große 'after' => false, 389b005809cSMichael Große 'label' => $lang['search_any_time'], 390b005809cSMichael Große ], 391b005809cSMichael Große 'week' => [ 392b005809cSMichael Große 'before' => false, 393b005809cSMichael Große 'after' => '1 week ago', 394b005809cSMichael Große 'label' => $lang['search_past_7_days'], 395b005809cSMichael Große ], 396b005809cSMichael Große 'month' => [ 397b005809cSMichael Große 'before' => false, 398b005809cSMichael Große 'after' => '1 month ago', 399b005809cSMichael Große 'label' => $lang['search_past_month'], 400b005809cSMichael Große ], 401b005809cSMichael Große 'year' => [ 402b005809cSMichael Große 'before' => false, 403b005809cSMichael Große 'after' => '1 year ago', 404b005809cSMichael Große 'label' => $lang['search_past_year'], 405b005809cSMichael Große ], 406b005809cSMichael Große ]; 407b005809cSMichael Große $activeOption = 'any'; 408b005809cSMichael Große foreach ($options as $key => $option) { 409422bbbc6SMichael Große if ($INPUT->str('min') === $option['after']) { 410b005809cSMichael Große $activeOption = $key; 411b005809cSMichael Große break; 412b005809cSMichael Große } 413b005809cSMichael Große } 414b005809cSMichael Große 4152171f9cbSAndreas Gohr $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true'); 416b005809cSMichael Große // render current 4174bdf82b5SAndreas Gohr $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); 418422bbbc6SMichael Große if ($INPUT->has('max') || $INPUT->has('min')) { 4194bdf82b5SAndreas Gohr $currentWrapper->addClass('changed'); 420bbc1da2eSMichael Große } 421b005809cSMichael Große $searchForm->addHTML($options[$activeOption]['label']); 422b005809cSMichael Große $searchForm->addTagClose('div'); 423bbc1da2eSMichael Große 424b005809cSMichael Große // render options list 4252171f9cbSAndreas Gohr $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false'); 426b005809cSMichael Große 427b005809cSMichael Große foreach ($options as $key => $option) { 4284bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 429b005809cSMichael Große 430b005809cSMichael Große if ($key === $activeOption) { 4314bdf82b5SAndreas Gohr $listItem->addClass('active'); 432b005809cSMichael Große $searchForm->addHTML($option['label']); 433bbc1da2eSMichael Große } else { 43452d4cd42SMichael Große $link = $this->searchState 43552d4cd42SMichael Große ->withTimeLimitations($option['after'], $option['before']) 436e2d055f5SAndreas Gohr ->getSearchLink($option['label']); 43752d4cd42SMichael Große $searchForm->addHTML($link); 438bbc1da2eSMichael Große } 439b005809cSMichael Große $searchForm->addTagClose('li'); 440bbc1da2eSMichael Große } 441b005809cSMichael Große $searchForm->addTagClose('ul'); 442bbc1da2eSMichael Große 443bbc1da2eSMichael Große $searchForm->addTagClose('div'); 444bbc1da2eSMichael Große } 445bbc1da2eSMichael Große 446bbc1da2eSMichael Große 447bbc1da2eSMichael Große /** 44821fcef82SMichael Große * Build the intro text for the search page 44921fcef82SMichael Große * 45021fcef82SMichael Große * @param string $query the search query 45121fcef82SMichael Große * 45221fcef82SMichael Große * @return string 45321fcef82SMichael Große */ 45421fcef82SMichael Große protected function getSearchIntroHTML($query) 45521fcef82SMichael Große { 4562ce8affcSMichael Große global $lang; 45721fcef82SMichael Große 45821fcef82SMichael Große $intro = p_locale_xhtml('searchpage'); 4592ce8affcSMichael Große 4602ce8affcSMichael Große $queryPagename = $this->createPagenameFromQuery($this->parsedQuery); 4612ce8affcSMichael Große $createQueryPageLink = html_wikilink($queryPagename . '?do=edit', $queryPagename); 4622ce8affcSMichael Große 4632ce8affcSMichael Große $pagecreateinfo = ''; 4642ce8affcSMichael Große if (auth_quickaclcheck($queryPagename) >= AUTH_CREATE) { 4652ce8affcSMichael Große $pagecreateinfo = sprintf($lang['searchcreatepage'], $createQueryPageLink); 4662ce8affcSMichael Große } 46779a2d784SGerrit Uitslag return str_replace( 468e2d055f5SAndreas Gohr ['@QUERY@', '@SEARCH@', '@CREATEPAGEINFO@'], 469e2d055f5SAndreas Gohr [hsc(rawurlencode($query)), hsc($query), $pagecreateinfo], 47021fcef82SMichael Große $intro 47121fcef82SMichael Große ); 47221fcef82SMichael Große } 47321fcef82SMichael Große 47421fcef82SMichael Große /** 4752ce8affcSMichael Große * Create a pagename based the parsed search query 4762ce8affcSMichael Große * 4772ce8affcSMichael Große * @param array $parsedQuery 4782ce8affcSMichael Große * 4792ce8affcSMichael Große * @return string pagename constructed from the parsed query 4802ce8affcSMichael Große */ 48142690e4dSMichael Große public function createPagenameFromQuery($parsedQuery) 4822ce8affcSMichael Große { 483e180e453SPhy $cleanedQuery = cleanID($parsedQuery['query']); // already strtolowered 48479a2d784SGerrit Uitslag if ($cleanedQuery === PhpString::strtolower($parsedQuery['query'])) { 48542690e4dSMichael Große return ':' . $cleanedQuery; 48642690e4dSMichael Große } 4872ce8affcSMichael Große $pagename = ''; 4882ce8affcSMichael Große if (!empty($parsedQuery['ns'])) { 48942690e4dSMichael Große $pagename .= ':' . cleanID($parsedQuery['ns'][0]); 4902ce8affcSMichael Große } 4912ce8affcSMichael Große $pagename .= ':' . cleanID(implode(' ', $parsedQuery['highlight'])); 4922ce8affcSMichael Große return $pagename; 4932ce8affcSMichael Große } 4942ce8affcSMichael Große 4952ce8affcSMichael Große /** 49621fcef82SMichael Große * Build HTML for a list of pages with matching pagenames 49721fcef82SMichael Große * 49821fcef82SMichael Große * @param array $data search results 49921fcef82SMichael Große * 50021fcef82SMichael Große * @return string 50121fcef82SMichael Große */ 50221fcef82SMichael Große protected function getPageLookupHTML($data) 50321fcef82SMichael Große { 50421fcef82SMichael Große if (empty($data)) { 50521fcef82SMichael Große return ''; 50621fcef82SMichael Große } 50721fcef82SMichael Große 50821fcef82SMichael Große global $lang; 50921fcef82SMichael Große 51021fcef82SMichael Große $html = '<div class="search_quickresult">'; 5116d55fda7SMichael Große $html .= '<h2>' . $lang['quickhits'] . ':</h2>'; 51221fcef82SMichael Große $html .= '<ul class="search_quickhits">'; 513e2d055f5SAndreas Gohr foreach (array_keys($data) as $id) { 5145d87aa31SMichael Große $name = null; 5155d87aa31SMichael Große if (!useHeading('navigation') && $ns = getNS($id)) { 5165d87aa31SMichael Große $name = shorten(noNS($id), ' (' . $ns . ')', 30); 5175d87aa31SMichael Große } 5185d87aa31SMichael Große $link = html_wikilink(':' . $id, $name); 5194eab6f7cSMichael Große $eventData = [ 5204eab6f7cSMichael Große 'listItemContent' => [$link], 5214eab6f7cSMichael Große 'page' => $id, 5224eab6f7cSMichael Große ]; 523cbb44eabSAndreas Gohr Event::createAndTrigger('SEARCH_RESULT_PAGELOOKUP', $eventData); 5244eab6f7cSMichael Große $html .= '<li>' . implode('', $eventData['listItemContent']) . '</li>'; 52521fcef82SMichael Große } 52621fcef82SMichael Große $html .= '</ul> '; 52721fcef82SMichael Große //clear float (see http://www.complexspiral.com/publications/containing-floats/) 52821fcef82SMichael Große $html .= '<div class="clearer"></div>'; 52921fcef82SMichael Große $html .= '</div>'; 53021fcef82SMichael Große 53121fcef82SMichael Große return $html; 53221fcef82SMichael Große } 53321fcef82SMichael Große 53421fcef82SMichael Große /** 53521fcef82SMichael Große * Build HTML for fulltext search results or "no results" message 53621fcef82SMichael Große * 53721fcef82SMichael Große * @param array $data the results of the fulltext search 53821fcef82SMichael Große * @param array $highlight the terms to be highlighted in the results 53921fcef82SMichael Große * 54021fcef82SMichael Große * @return string 54121fcef82SMichael Große */ 54221fcef82SMichael Große protected function getFulltextResultsHTML($data, $highlight) 54321fcef82SMichael Große { 54421fcef82SMichael Große global $lang; 54521fcef82SMichael Große 54621fcef82SMichael Große if (empty($data)) { 54721fcef82SMichael Große return '<div class="nothing">' . $lang['nothingfound'] . '</div>'; 54821fcef82SMichael Große } 54921fcef82SMichael Große 5502ce8affcSMichael Große $html = '<div class="search_fulltextresult">'; 5516d55fda7SMichael Große $html .= '<h2>' . $lang['search_fullresults'] . ':</h2>'; 5522ce8affcSMichael Große 55321fcef82SMichael Große $html .= '<dl class="search_results">'; 5548225e1abSMichael Große $num = 0; 5558225e1abSMichael Große $position = 0; 556cc3a3cdeSSatoshi Sahara $FulltextSearch = new FulltextSearch(); 5574c924eb8SMichael Große 55821fcef82SMichael Große foreach ($data as $id => $cnt) { 559e2d055f5SAndreas Gohr ++$position; 5604eab6f7cSMichael Große $resultLink = html_wikilink(':' . $id, null, $highlight); 5614c924eb8SMichael Große 5624c924eb8SMichael Große $resultHeader = [$resultLink]; 5634c924eb8SMichael Große 5644eab6f7cSMichael Große 5654c924eb8SMichael Große $restrictQueryToNSLink = $this->restrictQueryToNSLink(getNS($id)); 5664c924eb8SMichael Große if ($restrictQueryToNSLink) { 5674c924eb8SMichael Große $resultHeader[] = $restrictQueryToNSLink; 5684c924eb8SMichael Große } 5694c924eb8SMichael Große 5705d06a1e4SMichael Große $resultBody = []; 5719a75abfbSMichael Große $mtime = filemtime(wikiFN($id)); 5725d06a1e4SMichael Große $lastMod = '<span class="lastmod">' . $lang['lastmod'] . '</span> '; 57364159a61SAndreas Gohr $lastMod .= '<time datetime="' . date_iso8601($mtime) . '" title="' . dformat($mtime) . '">' . 57464159a61SAndreas Gohr dformat($mtime, '%f') . 57564159a61SAndreas Gohr '</time>'; 576f0861d1fSMichael Große $resultBody['meta'] = $lastMod; 5775d06a1e4SMichael Große if ($cnt !== 0) { 5788225e1abSMichael Große $num++; 579b12bcb77SAnika Henke $hits = '<span class="hits">' . $cnt . ' ' . $lang['hits'] . '</span>, '; 580f0861d1fSMichael Große $resultBody['meta'] = $hits . $resultBody['meta']; 581*0b1bbbbbSAndreas Gohr if ($num <= $FulltextSearch->getMaxSnippets()) { 5829329b002SSatoshi Sahara $resultBody['snippet'] = $FulltextSearch->snippet($id, $highlight); 5839a75abfbSMichael Große } 5849a75abfbSMichael Große } 5859a75abfbSMichael Große 5864eab6f7cSMichael Große $eventData = [ 5874c924eb8SMichael Große 'resultHeader' => $resultHeader, 5885d06a1e4SMichael Große 'resultBody' => $resultBody, 5894eab6f7cSMichael Große 'page' => $id, 59078d786c9SMichael Große 'position' => $position, 5914eab6f7cSMichael Große ]; 592cbb44eabSAndreas Gohr Event::createAndTrigger('SEARCH_RESULT_FULLPAGE', $eventData); 5935d06a1e4SMichael Große $html .= '<div class="search_fullpage_result">'; 5944eab6f7cSMichael Große $html .= '<dt>' . implode(' ', $eventData['resultHeader']) . '</dt>'; 5955d06a1e4SMichael Große foreach ($eventData['resultBody'] as $class => $htmlContent) { 5965d06a1e4SMichael Große $html .= "<dd class=\"$class\">$htmlContent</dd>"; 5975d06a1e4SMichael Große } 5985d06a1e4SMichael Große $html .= '</div>'; 59921fcef82SMichael Große } 60021fcef82SMichael Große $html .= '</dl>'; 60121fcef82SMichael Große 6022ce8affcSMichael Große $html .= '</div>'; 6032ce8affcSMichael Große 60421fcef82SMichael Große return $html; 60521fcef82SMichael Große } 6064c924eb8SMichael Große 6074c924eb8SMichael Große /** 6084c924eb8SMichael Große * create a link to restrict the current query to a namespace 6094c924eb8SMichael Große * 610ec27794fSMichael Große * @param false|string $ns the namespace to which to restrict the query 6114c924eb8SMichael Große * 612ec27794fSMichael Große * @return false|string 6134c924eb8SMichael Große */ 6144c924eb8SMichael Große protected function restrictQueryToNSLink($ns) 6154c924eb8SMichael Große { 6164c924eb8SMichael Große if (!$ns) { 6174c924eb8SMichael Große return false; 6184c924eb8SMichael Große } 619df977249SMichael Große if (!$this->isNamespaceAssistanceAvailable($this->parsedQuery)) { 6204c924eb8SMichael Große return false; 6214c924eb8SMichael Große } 6224c924eb8SMichael Große if (!empty($this->parsedQuery['ns']) && $this->parsedQuery['ns'][0] === $ns) { 6234c924eb8SMichael Große return false; 6244c924eb8SMichael Große } 62552d4cd42SMichael Große 6264c924eb8SMichael Große $name = '@' . $ns; 62752d4cd42SMichael Große return $this->searchState->withNamespace($ns)->getSearchLink($name); 6284c924eb8SMichael Große } 62921fcef82SMichael Große} 630