121fcef82SMichael Große<?php 221fcef82SMichael Große 321fcef82SMichael Großenamespace dokuwiki\Ui; 421fcef82SMichael Große 5*cbb44eabSAndreas Gohruse dokuwiki\Extension\Event; 664159a61SAndreas Gohruse dokuwiki\Form\Form; 7427ed988SMichael Große 821fcef82SMichael Großeclass Search extends Ui 921fcef82SMichael Große{ 1021fcef82SMichael Große protected $query; 114c924eb8SMichael Große protected $parsedQuery; 1218856c5dSMichael Große protected $searchState; 1321fcef82SMichael Große protected $pageLookupResults = array(); 1421fcef82SMichael Große protected $fullTextResults = array(); 1521fcef82SMichael Große protected $highlight = array(); 1621fcef82SMichael Große 1721fcef82SMichael Große /** 1821fcef82SMichael Große * Search constructor. 196639a152SMichael Große * 20fc46ed58SMichael Große * @param array $pageLookupResults pagename lookup results in the form [pagename => pagetitle] 21fc46ed58SMichael Große * @param array $fullTextResults fulltext search results in the form [pagename => #hits] 22fc46ed58SMichael Große * @param array $highlight array of strings to be highlighted 2321fcef82SMichael Große */ 246639a152SMichael Große public function __construct(array $pageLookupResults, array $fullTextResults, $highlight) 2521fcef82SMichael Große { 26d09b5b64SMichael Große global $QUERY; 274c924eb8SMichael Große $Indexer = idx_get_indexer(); 28d09b5b64SMichael Große 29d09b5b64SMichael Große $this->query = $QUERY; 30bbc1da2eSMichael Große $this->parsedQuery = ft_queryParser($Indexer, $QUERY); 3118856c5dSMichael Große $this->searchState = new SearchState($this->parsedQuery); 3221fcef82SMichael Große 336639a152SMichael Große $this->pageLookupResults = $pageLookupResults; 346639a152SMichael Große $this->fullTextResults = $fullTextResults; 3521fcef82SMichael Große $this->highlight = $highlight; 36b3cfe85aSMichael Große } 37bbc1da2eSMichael Große 38b3cfe85aSMichael Große /** 3921fcef82SMichael Große * display the search result 4021fcef82SMichael Große * 4121fcef82SMichael Große * @return void 4221fcef82SMichael Große */ 4321fcef82SMichael Große public function show() 4421fcef82SMichael Große { 4521fcef82SMichael Große $searchHTML = ''; 4621fcef82SMichael Große 4721fcef82SMichael Große $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 90*cbb44eabSAndreas Gohr Event::createAndTrigger('FORM_SEARCH_OUTPUT', $searchForm); 9181a0edd9SMichael Große 92427ed988SMichael Große return $searchForm->toHTML(); 93427ed988SMichael Große } 94427ed988SMichael Große 95be76738bSMichael Große /** 96be76738bSMichael Große * Add elements to adjust how the results are sorted 97be76738bSMichael Große * 98be76738bSMichael Große * @param Form $searchForm 99be76738bSMichael Große */ 100b005809cSMichael Große protected function addSortTool(Form $searchForm) 101b005809cSMichael Große { 102b005809cSMichael Große global $INPUT, $lang; 103b005809cSMichael Große 104b005809cSMichael Große $options = [ 105b005809cSMichael Große 'hits' => [ 106b005809cSMichael Große 'label' => $lang['search_sort_by_hits'], 107b005809cSMichael Große 'sort' => '', 108b005809cSMichael Große ], 109b005809cSMichael Große 'mtime' => [ 110b005809cSMichael Große 'label' => $lang['search_sort_by_mtime'], 111b005809cSMichael Große 'sort' => 'mtime', 112b005809cSMichael Große ], 113b005809cSMichael Große ]; 114b005809cSMichael Große $activeOption = 'hits'; 115b005809cSMichael Große 1161265b193SMichael Große if ($INPUT->str('srt') === 'mtime') { 117b005809cSMichael Große $activeOption = 'mtime'; 118b005809cSMichael Große } 119b005809cSMichael Große 1202171f9cbSAndreas Gohr $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true'); 121b005809cSMichael Große // render current 1224bdf82b5SAndreas Gohr $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); 123b005809cSMichael Große if ($activeOption !== 'hits') { 1244bdf82b5SAndreas Gohr $currentWrapper->addClass('changed'); 125b005809cSMichael Große } 126b005809cSMichael Große $searchForm->addHTML($options[$activeOption]['label']); 127b005809cSMichael Große $searchForm->addTagClose('div'); 128b005809cSMichael Große 129b005809cSMichael Große // render options list 1302171f9cbSAndreas Gohr $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false'); 131b005809cSMichael Große 132b005809cSMichael Große foreach ($options as $key => $option) { 1334bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 134b005809cSMichael Große 135b005809cSMichael Große if ($key === $activeOption) { 1364bdf82b5SAndreas Gohr $listItem->addClass('active'); 137b005809cSMichael Große $searchForm->addHTML($option['label']); 138b005809cSMichael Große } else { 13952d4cd42SMichael Große $link = $this->searchState->withSorting($option['sort'])->getSearchLink($option['label']); 14052d4cd42SMichael Große $searchForm->addHTML($link); 141b005809cSMichael Große } 142b005809cSMichael Große $searchForm->addTagClose('li'); 143b005809cSMichael Große } 144b005809cSMichael Große $searchForm->addTagClose('ul'); 145b005809cSMichael Große 146b005809cSMichael Große $searchForm->addTagClose('div'); 147b005809cSMichael Große 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 */ 157df977249SMichael Große protected function isNamespaceAssistanceAvailable(array $parsedQuery) { 158df977249SMichael Große if (preg_match('/[\(\)\|]/', $parsedQuery['query']) === 1) { 159bb8ef867SMichael Große return false; 160bb8ef867SMichael Große } 161df977249SMichael Große 162df977249SMichael Große return true; 163df977249SMichael Große } 164df977249SMichael Große 165be76738bSMichael Große /** 166be76738bSMichael Große * Check if the query is simple enough to modify the fragment search behavior without breaking the rest of the query 167be76738bSMichael Große * 168be76738bSMichael Große * @param array $parsedQuery 169be76738bSMichael Große * 170be76738bSMichael Große * @return bool 171be76738bSMichael Große */ 172df977249SMichael Große protected function isFragmentAssistanceAvailable(array $parsedQuery) { 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'], 219b005809cSMichael Große 'and' => array_map(function ($term) { 220b005809cSMichael Große return trim($term, '*'); 221b005809cSMichael Große }, $this->parsedQuery['and']), 222df977249SMichael Große 'not' => array_map(function ($term) { 223df977249SMichael Große return trim($term, '*'); 224df977249SMichael Große }, $this->parsedQuery['not']), 225b005809cSMichael Große ], 226b005809cSMichael Große 'starts' => [ 227b005809cSMichael Große 'label' => $lang['search_starts_with'], 228b005809cSMichael Große 'and' => array_map(function ($term) { 229b005809cSMichael Große return trim($term, '*') . '*'; 230df977249SMichael Große }, $this->parsedQuery['and']), 231df977249SMichael Große 'not' => array_map(function ($term) { 232df977249SMichael Große return trim($term, '*') . '*'; 233df977249SMichael Große }, $this->parsedQuery['not']), 234b005809cSMichael Große ], 235b005809cSMichael Große 'ends' => [ 236b005809cSMichael Große 'label' => $lang['search_ends_with'], 237b005809cSMichael Große 'and' => array_map(function ($term) { 238b005809cSMichael Große return '*' . trim($term, '*'); 239df977249SMichael Große }, $this->parsedQuery['and']), 240df977249SMichael Große 'not' => array_map(function ($term) { 241df977249SMichael Große return '*' . trim($term, '*'); 242df977249SMichael Große }, $this->parsedQuery['not']), 243b005809cSMichael Große ], 244b005809cSMichael Große 'contains' => [ 245b005809cSMichael Große 'label' => $lang['search_contains'], 246b005809cSMichael Große 'and' => array_map(function ($term) { 247b005809cSMichael Große return '*' . trim($term, '*') . '*'; 248df977249SMichael Große }, $this->parsedQuery['and']), 249df977249SMichael Große 'not' => array_map(function ($term) { 250df977249SMichael Große return '*' . trim($term, '*') . '*'; 251df977249SMichael Große }, $this->parsedQuery['not']), 252b005809cSMichael Große ] 253b005809cSMichael Große ]; 254b005809cSMichael Große 255b005809cSMichael Große // detect current 256c6b5b74aSMichael Große $activeOption = 'custom'; 257b005809cSMichael Große foreach ($options as $key => $option) { 258b005809cSMichael Große if ($this->parsedQuery['and'] === $option['and']) { 259b005809cSMichael Große $activeOption = $key; 260b005809cSMichael Große } 261b005809cSMichael Große } 262c6b5b74aSMichael Große if ($activeOption === 'custom') { 263c6b5b74aSMichael Große $options = array_merge(['custom' => [ 264c6b5b74aSMichael Große 'label' => $lang['search_custom_match'], 265c6b5b74aSMichael Große ]], $options); 266c6b5b74aSMichael Große } 267b005809cSMichael Große 2682171f9cbSAndreas Gohr $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true'); 269b005809cSMichael Große // render current 2704bdf82b5SAndreas Gohr $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); 271b005809cSMichael Große if ($activeOption !== 'exact') { 2724bdf82b5SAndreas Gohr $currentWrapper->addClass('changed'); 273b005809cSMichael Große } 274b005809cSMichael Große $searchForm->addHTML($options[$activeOption]['label']); 275b005809cSMichael Große $searchForm->addTagClose('div'); 276b005809cSMichael Große 277b005809cSMichael Große // render options list 2782171f9cbSAndreas Gohr $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false'); 279b005809cSMichael Große 280b005809cSMichael Große foreach ($options as $key => $option) { 2814bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 282b005809cSMichael Große 283b005809cSMichael Große if ($key === $activeOption) { 2844bdf82b5SAndreas Gohr $listItem->addClass('active'); 285b005809cSMichael Große $searchForm->addHTML($option['label']); 286b005809cSMichael Große } else { 28752d4cd42SMichael Große $link = $this->searchState 28852d4cd42SMichael Große ->withFragments($option['and'], $option['not']) 28952d4cd42SMichael Große ->getSearchLink($option['label']) 29052d4cd42SMichael Große ; 29152d4cd42SMichael Große $searchForm->addHTML($link); 292b005809cSMichael Große } 293b005809cSMichael Große $searchForm->addTagClose('li'); 294b005809cSMichael Große } 295b005809cSMichael Große $searchForm->addTagClose('ul'); 2964d0cb6e1SMichael Große 2974d0cb6e1SMichael Große $searchForm->addTagClose('div'); 298b005809cSMichael Große 299b005809cSMichael Große // render options list 3004d0cb6e1SMichael Große } 3014d0cb6e1SMichael Große 302bb8ef867SMichael Große /** 303bb8ef867SMichael Große * Add the elements for the namespace selector 304bb8ef867SMichael Große * 305bb8ef867SMichael Große * @param Form $searchForm 306bb8ef867SMichael Große */ 30718856c5dSMichael Große protected function addNamespaceSelector(Form $searchForm) 308bb8ef867SMichael Große { 309df977249SMichael Große if (!$this->isNamespaceAssistanceAvailable($this->parsedQuery)) { 310df977249SMichael Große return; 311df977249SMichael Große } 312df977249SMichael Große 313b005809cSMichael Große global $lang; 314b005809cSMichael Große 31518856c5dSMichael Große $baseNS = empty($this->parsedQuery['ns']) ? '' : $this->parsedQuery['ns'][0]; 316bbc1da2eSMichael Große $extraNS = $this->getAdditionalNamespacesFromResults($baseNS); 3174d0cb6e1SMichael Große 3182171f9cbSAndreas Gohr $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true'); 319b005809cSMichael Große // render current 3204bdf82b5SAndreas Gohr $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); 321bbc1da2eSMichael Große if ($baseNS) { 3224bdf82b5SAndreas Gohr $currentWrapper->addClass('changed'); 323b005809cSMichael Große $searchForm->addHTML('@' . $baseNS); 324b005809cSMichael Große } else { 325b005809cSMichael Große $searchForm->addHTML($lang['search_any_ns']); 326b005809cSMichael Große } 327b005809cSMichael Große $searchForm->addTagClose('div'); 328b005809cSMichael Große 329b005809cSMichael Große // render options list 3302171f9cbSAndreas Gohr $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false'); 331b005809cSMichael Große 3324bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 333b005809cSMichael Große if ($baseNS) { 3344bdf82b5SAndreas Gohr $listItem->addClass('active'); 33552d4cd42SMichael Große $link = $this->searchState->withNamespace('')->getSearchLink($lang['search_any_ns']); 33652d4cd42SMichael Große $searchForm->addHTML($link); 337b005809cSMichael Große } else { 338b005809cSMichael Große $searchForm->addHTML($lang['search_any_ns']); 339bb8ef867SMichael Große } 340b005809cSMichael Große $searchForm->addTagClose('li'); 341bb8ef867SMichael Große 34218856c5dSMichael Große foreach ($extraNS as $ns => $count) { 3434bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 3441d918893SAndreas Gohr $label = $ns . ($count ? " <bdi>($count)</bdi>" : ''); 3454d0cb6e1SMichael Große 346b005809cSMichael Große if ($ns === $baseNS) { 3474bdf82b5SAndreas Gohr $listItem->addClass('active'); 348b005809cSMichael Große $searchForm->addHTML($label); 349b005809cSMichael Große } else { 35052d4cd42SMichael Große $link = $this->searchState->withNamespace($ns)->getSearchLink($label); 35152d4cd42SMichael Große $searchForm->addHTML($link); 352bb8ef867SMichael Große } 353b005809cSMichael Große $searchForm->addTagClose('li'); 354bb8ef867SMichael Große } 355b005809cSMichael Große $searchForm->addTagClose('ul'); 356bb8ef867SMichael Große 357bb8ef867SMichael Große $searchForm->addTagClose('div'); 358b005809cSMichael Große 359bb8ef867SMichael Große } 360bb8ef867SMichael Große 361bb8ef867SMichael Große /** 362bb8ef867SMichael Große * Parse the full text results for their top namespaces below the given base namespace 363bb8ef867SMichael Große * 364bb8ef867SMichael Große * @param string $baseNS the namespace within which was searched, empty string for root namespace 365bb8ef867SMichael Große * 366bb8ef867SMichael Große * @return array an associative array with namespace => #number of found pages, sorted descending 367bb8ef867SMichael Große */ 368bb8ef867SMichael Große protected function getAdditionalNamespacesFromResults($baseNS) 369bb8ef867SMichael Große { 370bb8ef867SMichael Große $namespaces = []; 371bb8ef867SMichael Große $baseNSLength = strlen($baseNS); 372bb8ef867SMichael Große foreach ($this->fullTextResults as $page => $numberOfHits) { 373bb8ef867SMichael Große $namespace = getNS($page); 374bb8ef867SMichael Große if (!$namespace) { 375bb8ef867SMichael Große continue; 376bb8ef867SMichael Große } 377bb8ef867SMichael Große if ($namespace === $baseNS) { 378bb8ef867SMichael Große continue; 379bb8ef867SMichael Große } 380bb8ef867SMichael Große $firstColon = strpos((string)$namespace, ':', $baseNSLength + 1) ?: strlen($namespace); 381bb8ef867SMichael Große $subtopNS = substr($namespace, 0, $firstColon); 382bb8ef867SMichael Große if (empty($namespaces[$subtopNS])) { 383bb8ef867SMichael Große $namespaces[$subtopNS] = 0; 384bb8ef867SMichael Große } 385bb8ef867SMichael Große $namespaces[$subtopNS] += 1; 386bb8ef867SMichael Große } 38755dc8783SMichael Große ksort($namespaces); 388bb8ef867SMichael Große arsort($namespaces); 389bb8ef867SMichael Große return $namespaces; 390bb8ef867SMichael Große } 391bb8ef867SMichael Große 392bb8ef867SMichael Große /** 393bbc1da2eSMichael Große * @ToDo: custom date input 394bbc1da2eSMichael Große * 395bbc1da2eSMichael Große * @param Form $searchForm 396bbc1da2eSMichael Große */ 397b005809cSMichael Große protected function addDateSelector(Form $searchForm) 398b005809cSMichael Große { 399b005809cSMichael Große global $INPUT, $lang; 400bbc1da2eSMichael Große 401b005809cSMichael Große $options = [ 402b005809cSMichael Große 'any' => [ 403b005809cSMichael Große 'before' => false, 404b005809cSMichael Große 'after' => false, 405b005809cSMichael Große 'label' => $lang['search_any_time'], 406b005809cSMichael Große ], 407b005809cSMichael Große 'week' => [ 408b005809cSMichael Große 'before' => false, 409b005809cSMichael Große 'after' => '1 week ago', 410b005809cSMichael Große 'label' => $lang['search_past_7_days'], 411b005809cSMichael Große ], 412b005809cSMichael Große 'month' => [ 413b005809cSMichael Große 'before' => false, 414b005809cSMichael Große 'after' => '1 month ago', 415b005809cSMichael Große 'label' => $lang['search_past_month'], 416b005809cSMichael Große ], 417b005809cSMichael Große 'year' => [ 418b005809cSMichael Große 'before' => false, 419b005809cSMichael Große 'after' => '1 year ago', 420b005809cSMichael Große 'label' => $lang['search_past_year'], 421b005809cSMichael Große ], 422b005809cSMichael Große ]; 423b005809cSMichael Große $activeOption = 'any'; 424b005809cSMichael Große foreach ($options as $key => $option) { 425422bbbc6SMichael Große if ($INPUT->str('min') === $option['after']) { 426b005809cSMichael Große $activeOption = $key; 427b005809cSMichael Große break; 428b005809cSMichael Große } 429b005809cSMichael Große } 430b005809cSMichael Große 4312171f9cbSAndreas Gohr $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true'); 432b005809cSMichael Große // render current 4334bdf82b5SAndreas Gohr $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); 434422bbbc6SMichael Große if ($INPUT->has('max') || $INPUT->has('min')) { 4354bdf82b5SAndreas Gohr $currentWrapper->addClass('changed'); 436bbc1da2eSMichael Große } 437b005809cSMichael Große $searchForm->addHTML($options[$activeOption]['label']); 438b005809cSMichael Große $searchForm->addTagClose('div'); 439bbc1da2eSMichael Große 440b005809cSMichael Große // render options list 4412171f9cbSAndreas Gohr $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false'); 442b005809cSMichael Große 443b005809cSMichael Große foreach ($options as $key => $option) { 4444bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 445b005809cSMichael Große 446b005809cSMichael Große if ($key === $activeOption) { 4474bdf82b5SAndreas Gohr $listItem->addClass('active'); 448b005809cSMichael Große $searchForm->addHTML($option['label']); 449bbc1da2eSMichael Große } else { 45052d4cd42SMichael Große $link = $this->searchState 45152d4cd42SMichael Große ->withTimeLimitations($option['after'], $option['before']) 45252d4cd42SMichael Große ->getSearchLink($option['label']) 45352d4cd42SMichael Große ; 45452d4cd42SMichael Große $searchForm->addHTML($link); 455bbc1da2eSMichael Große } 456b005809cSMichael Große $searchForm->addTagClose('li'); 457bbc1da2eSMichael Große } 458b005809cSMichael Große $searchForm->addTagClose('ul'); 459bbc1da2eSMichael Große 460bbc1da2eSMichael Große $searchForm->addTagClose('div'); 461bbc1da2eSMichael Große } 462bbc1da2eSMichael Große 463bbc1da2eSMichael Große 464bbc1da2eSMichael Große /** 46521fcef82SMichael Große * Build the intro text for the search page 46621fcef82SMichael Große * 46721fcef82SMichael Große * @param string $query the search query 46821fcef82SMichael Große * 46921fcef82SMichael Große * @return string 47021fcef82SMichael Große */ 47121fcef82SMichael Große protected function getSearchIntroHTML($query) 47221fcef82SMichael Große { 4732ce8affcSMichael Große global $lang; 47421fcef82SMichael Große 47521fcef82SMichael Große $intro = p_locale_xhtml('searchpage'); 4762ce8affcSMichael Große 4772ce8affcSMichael Große $queryPagename = $this->createPagenameFromQuery($this->parsedQuery); 4782ce8affcSMichael Große $createQueryPageLink = html_wikilink($queryPagename . '?do=edit', $queryPagename); 4792ce8affcSMichael Große 4802ce8affcSMichael Große $pagecreateinfo = ''; 4812ce8affcSMichael Große if (auth_quickaclcheck($queryPagename) >= AUTH_CREATE) { 4822ce8affcSMichael Große $pagecreateinfo = sprintf($lang['searchcreatepage'], $createQueryPageLink); 4832ce8affcSMichael Große } 48421fcef82SMichael Große $intro = str_replace( 48521fcef82SMichael Große array('@QUERY@', '@SEARCH@', '@CREATEPAGEINFO@'), 48621fcef82SMichael Große array(hsc(rawurlencode($query)), hsc($query), $pagecreateinfo), 48721fcef82SMichael Große $intro 48821fcef82SMichael Große ); 4892ce8affcSMichael Große 49021fcef82SMichael Große return $intro; 49121fcef82SMichael Große } 49221fcef82SMichael Große 49321fcef82SMichael Große /** 4942ce8affcSMichael Große * Create a pagename based the parsed search query 4952ce8affcSMichael Große * 4962ce8affcSMichael Große * @param array $parsedQuery 4972ce8affcSMichael Große * 4982ce8affcSMichael Große * @return string pagename constructed from the parsed query 4992ce8affcSMichael Große */ 50042690e4dSMichael Große public function createPagenameFromQuery($parsedQuery) 5012ce8affcSMichael Große { 502e180e453SPhy $cleanedQuery = cleanID($parsedQuery['query']); // already strtolowered 503e180e453SPhy if ($cleanedQuery === utf8_strtolower($parsedQuery['query'])) { 50442690e4dSMichael Große return ':' . $cleanedQuery; 50542690e4dSMichael Große } 5062ce8affcSMichael Große $pagename = ''; 5072ce8affcSMichael Große if (!empty($parsedQuery['ns'])) { 50842690e4dSMichael Große $pagename .= ':' . cleanID($parsedQuery['ns'][0]); 5092ce8affcSMichael Große } 5102ce8affcSMichael Große $pagename .= ':' . cleanID(implode(' ' , $parsedQuery['highlight'])); 5112ce8affcSMichael Große return $pagename; 5122ce8affcSMichael Große } 5132ce8affcSMichael Große 5142ce8affcSMichael Große /** 51521fcef82SMichael Große * Build HTML for a list of pages with matching pagenames 51621fcef82SMichael Große * 51721fcef82SMichael Große * @param array $data search results 51821fcef82SMichael Große * 51921fcef82SMichael Große * @return string 52021fcef82SMichael Große */ 52121fcef82SMichael Große protected function getPageLookupHTML($data) 52221fcef82SMichael Große { 52321fcef82SMichael Große if (empty($data)) { 52421fcef82SMichael Große return ''; 52521fcef82SMichael Große } 52621fcef82SMichael Große 52721fcef82SMichael Große global $lang; 52821fcef82SMichael Große 52921fcef82SMichael Große $html = '<div class="search_quickresult">'; 5306d55fda7SMichael Große $html .= '<h2>' . $lang['quickhits'] . ':</h2>'; 53121fcef82SMichael Große $html .= '<ul class="search_quickhits">'; 53221fcef82SMichael Große foreach ($data as $id => $title) { 5335d87aa31SMichael Große $name = null; 5345d87aa31SMichael Große if (!useHeading('navigation') && $ns = getNS($id)) { 5355d87aa31SMichael Große $name = shorten(noNS($id), ' (' . $ns . ')', 30); 5365d87aa31SMichael Große } 5375d87aa31SMichael Große $link = html_wikilink(':' . $id, $name); 5384eab6f7cSMichael Große $eventData = [ 5394eab6f7cSMichael Große 'listItemContent' => [$link], 5404eab6f7cSMichael Große 'page' => $id, 5414eab6f7cSMichael Große ]; 542*cbb44eabSAndreas Gohr Event::createAndTrigger('SEARCH_RESULT_PAGELOOKUP', $eventData); 5434eab6f7cSMichael Große $html .= '<li>' . implode('', $eventData['listItemContent']) . '</li>'; 54421fcef82SMichael Große } 54521fcef82SMichael Große $html .= '</ul> '; 54621fcef82SMichael Große //clear float (see http://www.complexspiral.com/publications/containing-floats/) 54721fcef82SMichael Große $html .= '<div class="clearer"></div>'; 54821fcef82SMichael Große $html .= '</div>'; 54921fcef82SMichael Große 55021fcef82SMichael Große return $html; 55121fcef82SMichael Große } 55221fcef82SMichael Große 55321fcef82SMichael Große /** 55421fcef82SMichael Große * Build HTML for fulltext search results or "no results" message 55521fcef82SMichael Große * 55621fcef82SMichael Große * @param array $data the results of the fulltext search 55721fcef82SMichael Große * @param array $highlight the terms to be highlighted in the results 55821fcef82SMichael Große * 55921fcef82SMichael Große * @return string 56021fcef82SMichael Große */ 56121fcef82SMichael Große protected function getFulltextResultsHTML($data, $highlight) 56221fcef82SMichael Große { 56321fcef82SMichael Große global $lang; 56421fcef82SMichael Große 56521fcef82SMichael Große if (empty($data)) { 56621fcef82SMichael Große return '<div class="nothing">' . $lang['nothingfound'] . '</div>'; 56721fcef82SMichael Große } 56821fcef82SMichael Große 5692ce8affcSMichael Große $html = '<div class="search_fulltextresult">'; 5706d55fda7SMichael Große $html .= '<h2>' . $lang['search_fullresults'] . ':</h2>'; 5712ce8affcSMichael Große 57221fcef82SMichael Große $html .= '<dl class="search_results">'; 5738225e1abSMichael Große $num = 0; 5748225e1abSMichael Große $position = 0; 5754c924eb8SMichael Große 57621fcef82SMichael Große foreach ($data as $id => $cnt) { 57778d786c9SMichael Große $position += 1; 5784eab6f7cSMichael Große $resultLink = html_wikilink(':' . $id, null, $highlight); 5794c924eb8SMichael Große 5804c924eb8SMichael Große $resultHeader = [$resultLink]; 5814c924eb8SMichael Große 5824eab6f7cSMichael Große 5834c924eb8SMichael Große $restrictQueryToNSLink = $this->restrictQueryToNSLink(getNS($id)); 5844c924eb8SMichael Große if ($restrictQueryToNSLink) { 5854c924eb8SMichael Große $resultHeader[] = $restrictQueryToNSLink; 5864c924eb8SMichael Große } 5874c924eb8SMichael Große 5885d06a1e4SMichael Große $resultBody = []; 5899a75abfbSMichael Große $mtime = filemtime(wikiFN($id)); 5905d06a1e4SMichael Große $lastMod = '<span class="lastmod">' . $lang['lastmod'] . '</span> '; 59164159a61SAndreas Gohr $lastMod .= '<time datetime="' . date_iso8601($mtime) . '" title="' . dformat($mtime) . '">' . 59264159a61SAndreas Gohr dformat($mtime, '%f') . 59364159a61SAndreas Gohr '</time>'; 594f0861d1fSMichael Große $resultBody['meta'] = $lastMod; 5955d06a1e4SMichael Große if ($cnt !== 0) { 5968225e1abSMichael Große $num++; 597b12bcb77SAnika Henke $hits = '<span class="hits">' . $cnt . ' ' . $lang['hits'] . '</span>, '; 598f0861d1fSMichael Große $resultBody['meta'] = $hits . $resultBody['meta']; 5998225e1abSMichael Große if ($num <= FT_SNIPPET_NUMBER) { // create snippets for the first number of matches only 600f0861d1fSMichael Große $resultBody['snippet'] = ft_snippet($id, $highlight); 6019a75abfbSMichael Große } 6029a75abfbSMichael Große } 6039a75abfbSMichael Große 6044eab6f7cSMichael Große $eventData = [ 6054c924eb8SMichael Große 'resultHeader' => $resultHeader, 6065d06a1e4SMichael Große 'resultBody' => $resultBody, 6074eab6f7cSMichael Große 'page' => $id, 60878d786c9SMichael Große 'position' => $position, 6094eab6f7cSMichael Große ]; 610*cbb44eabSAndreas Gohr Event::createAndTrigger('SEARCH_RESULT_FULLPAGE', $eventData); 6115d06a1e4SMichael Große $html .= '<div class="search_fullpage_result">'; 6124eab6f7cSMichael Große $html .= '<dt>' . implode(' ', $eventData['resultHeader']) . '</dt>'; 6135d06a1e4SMichael Große foreach ($eventData['resultBody'] as $class => $htmlContent) { 6145d06a1e4SMichael Große $html .= "<dd class=\"$class\">$htmlContent</dd>"; 6155d06a1e4SMichael Große } 6165d06a1e4SMichael Große $html .= '</div>'; 61721fcef82SMichael Große } 61821fcef82SMichael Große $html .= '</dl>'; 61921fcef82SMichael Große 6202ce8affcSMichael Große $html .= '</div>'; 6212ce8affcSMichael Große 62221fcef82SMichael Große return $html; 62321fcef82SMichael Große } 6244c924eb8SMichael Große 6254c924eb8SMichael Große /** 6264c924eb8SMichael Große * create a link to restrict the current query to a namespace 6274c924eb8SMichael Große * 628ec27794fSMichael Große * @param false|string $ns the namespace to which to restrict the query 6294c924eb8SMichael Große * 630ec27794fSMichael Große * @return false|string 6314c924eb8SMichael Große */ 6324c924eb8SMichael Große protected function restrictQueryToNSLink($ns) 6334c924eb8SMichael Große { 6344c924eb8SMichael Große if (!$ns) { 6354c924eb8SMichael Große return false; 6364c924eb8SMichael Große } 637df977249SMichael Große if (!$this->isNamespaceAssistanceAvailable($this->parsedQuery)) { 6384c924eb8SMichael Große return false; 6394c924eb8SMichael Große } 6404c924eb8SMichael Große if (!empty($this->parsedQuery['ns']) && $this->parsedQuery['ns'][0] === $ns) { 6414c924eb8SMichael Große return false; 6424c924eb8SMichael Große } 64352d4cd42SMichael Große 6444c924eb8SMichael Große $name = '@' . $ns; 64552d4cd42SMichael Große return $this->searchState->withNamespace($ns)->getSearchLink($name); 6464c924eb8SMichael Große } 64721fcef82SMichael Große} 648