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*2d85e841SAndreas Gohruse dokuwiki\Utf8\Sort; 8427ed988SMichael Große 921fcef82SMichael Großeclass Search extends Ui 1021fcef82SMichael Große{ 1121fcef82SMichael Große protected $query; 124c924eb8SMichael Große protected $parsedQuery; 1318856c5dSMichael Große protected $searchState; 1421fcef82SMichael Große protected $pageLookupResults = array(); 1521fcef82SMichael Große protected $fullTextResults = array(); 1621fcef82SMichael Große protected $highlight = array(); 1721fcef82SMichael Große 1821fcef82SMichael Große /** 1921fcef82SMichael Große * Search constructor. 206639a152SMichael Große * 21fc46ed58SMichael Große * @param array $pageLookupResults pagename lookup results in the form [pagename => pagetitle] 22fc46ed58SMichael Große * @param array $fullTextResults fulltext search results in the form [pagename => #hits] 23fc46ed58SMichael Große * @param array $highlight array of strings to be highlighted 2421fcef82SMichael Große */ 256639a152SMichael Große public function __construct(array $pageLookupResults, array $fullTextResults, $highlight) 2621fcef82SMichael Große { 27d09b5b64SMichael Große global $QUERY; 284c924eb8SMichael Große $Indexer = idx_get_indexer(); 29d09b5b64SMichael Große 30d09b5b64SMichael Große $this->query = $QUERY; 31bbc1da2eSMichael Große $this->parsedQuery = ft_queryParser($Indexer, $QUERY); 3218856c5dSMichael Große $this->searchState = new SearchState($this->parsedQuery); 3321fcef82SMichael Große 346639a152SMichael Große $this->pageLookupResults = $pageLookupResults; 356639a152SMichael Große $this->fullTextResults = $fullTextResults; 3621fcef82SMichael Große $this->highlight = $highlight; 37b3cfe85aSMichael Große } 38bbc1da2eSMichael Große 39b3cfe85aSMichael Große /** 4021fcef82SMichael Große * display the search result 4121fcef82SMichael Große * 4221fcef82SMichael Große * @return void 4321fcef82SMichael Große */ 4421fcef82SMichael Große public function show() 4521fcef82SMichael Große { 4621fcef82SMichael Große $searchHTML = ''; 4721fcef82SMichael Große 4821fcef82SMichael Große $searchHTML .= $this->getSearchIntroHTML($this->query); 4921fcef82SMichael Große 502ce8affcSMichael Große $searchHTML .= $this->getSearchFormHTML($this->query); 512ce8affcSMichael Große 5221fcef82SMichael Große $searchHTML .= $this->getPageLookupHTML($this->pageLookupResults); 5321fcef82SMichael Große 5421fcef82SMichael Große $searchHTML .= $this->getFulltextResultsHTML($this->fullTextResults, $this->highlight); 5521fcef82SMichael Große 5621fcef82SMichael Große echo $searchHTML; 5721fcef82SMichael Große } 5821fcef82SMichael Große 5921fcef82SMichael Große /** 60427ed988SMichael Große * Get a form which can be used to adjust/refine the search 61427ed988SMichael Große * 62427ed988SMichael Große * @param string $query 63427ed988SMichael Große * 64427ed988SMichael Große * @return string 65427ed988SMichael Große */ 66427ed988SMichael Große protected function getSearchFormHTML($query) 67427ed988SMichael Große { 68bbc1da2eSMichael Große global $lang, $ID, $INPUT; 69427ed988SMichael Große 707fa270bcSMichael Große $searchForm = (new Form(['method' => 'get'], true))->addClass('search-results-form'); 71bb8ef867SMichael Große $searchForm->setHiddenField('do', 'search'); 72d22b78c8SMichael Große $searchForm->setHiddenField('id', $ID); 731265b193SMichael Große $searchForm->setHiddenField('sf', '1'); 74422bbbc6SMichael Große if ($INPUT->has('min')) { 75422bbbc6SMichael Große $searchForm->setHiddenField('min', $INPUT->str('min')); 76bbc1da2eSMichael Große } 77422bbbc6SMichael Große if ($INPUT->has('max')) { 78422bbbc6SMichael Große $searchForm->setHiddenField('max', $INPUT->str('max')); 79bbc1da2eSMichael Große } 801265b193SMichael Große if ($INPUT->has('srt')) { 811265b193SMichael Große $searchForm->setHiddenField('srt', $INPUT->str('srt')); 828d0e286aSMichael Große } 834bdf82b5SAndreas Gohr $searchForm->addFieldsetOpen()->addClass('search-form'); 84d22b78c8SMichael Große $searchForm->addTextInput('q')->val($query)->useInput(false); 85427ed988SMichael Große $searchForm->addButton('', $lang['btn_search'])->attr('type', 'submit'); 86bb8ef867SMichael Große 8718856c5dSMichael Große $this->addSearchAssistanceElements($searchForm); 88bb8ef867SMichael Große 89427ed988SMichael Große $searchForm->addFieldsetClose(); 90427ed988SMichael Große 91cbb44eabSAndreas Gohr Event::createAndTrigger('FORM_SEARCH_OUTPUT', $searchForm); 9281a0edd9SMichael Große 93427ed988SMichael Große return $searchForm->toHTML(); 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 } 150b005809cSMichael Große 151be76738bSMichael Große /** 152be76738bSMichael Große * Check if the query is simple enough to modify its namespace limitations without breaking the rest of the query 153be76738bSMichael Große * 154be76738bSMichael Große * @param array $parsedQuery 155be76738bSMichael Große * 156be76738bSMichael Große * @return bool 157be76738bSMichael Große */ 158df977249SMichael Große protected function isNamespaceAssistanceAvailable(array $parsedQuery) { 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 */ 173df977249SMichael Große protected function isFragmentAssistanceAvailable(array $parsedQuery) { 174df977249SMichael Große if (preg_match('/[\(\)\|]/', $parsedQuery['query']) === 1) { 175bb8ef867SMichael Große return false; 176bb8ef867SMichael Große } 177bb8ef867SMichael Große 178bb8ef867SMichael Große if (!empty($parsedQuery['phrases'])) { 179bb8ef867SMichael Große return false; 180bb8ef867SMichael Große } 181bb8ef867SMichael Große 182bb8ef867SMichael Große return true; 183bb8ef867SMichael Große } 184bb8ef867SMichael Große 185bb8ef867SMichael Große /** 186bb8ef867SMichael Große * Add the elements to be used for search assistance 187bb8ef867SMichael Große * 188bb8ef867SMichael Große * @param Form $searchForm 189bb8ef867SMichael Große */ 19018856c5dSMichael Große protected function addSearchAssistanceElements(Form $searchForm) 191bb8ef867SMichael Große { 192bb8ef867SMichael Große $searchForm->addTagOpen('div') 1934bdf82b5SAndreas Gohr ->addClass('advancedOptions') 1942171f9cbSAndreas Gohr ->attr('style', 'display: none;') 1952171f9cbSAndreas Gohr ->attr('aria-hidden', 'true'); 196bb8ef867SMichael Große 19718856c5dSMichael Große $this->addFragmentBehaviorLinks($searchForm); 19818856c5dSMichael Große $this->addNamespaceSelector($searchForm); 19918856c5dSMichael Große $this->addDateSelector($searchForm); 200b005809cSMichael Große $this->addSortTool($searchForm); 201bb8ef867SMichael Große 202bb8ef867SMichael Große $searchForm->addTagClose('div'); 203bb8ef867SMichael Große } 204bb8ef867SMichael Große 205be76738bSMichael Große /** 206be76738bSMichael Große * Add the elements to adjust the fragment search behavior 207be76738bSMichael Große * 208be76738bSMichael Große * @param Form $searchForm 209be76738bSMichael Große */ 21018856c5dSMichael Große protected function addFragmentBehaviorLinks(Form $searchForm) 2114d0cb6e1SMichael Große { 212df977249SMichael Große if (!$this->isFragmentAssistanceAvailable($this->parsedQuery)) { 213df977249SMichael Große return; 214df977249SMichael Große } 215b005809cSMichael Große global $lang; 2164d0cb6e1SMichael Große 217b005809cSMichael Große $options = [ 218b005809cSMichael Große 'exact' => [ 219b005809cSMichael Große 'label' => $lang['search_exact_match'], 220b005809cSMichael Große 'and' => array_map(function ($term) { 221b005809cSMichael Große return trim($term, '*'); 222b005809cSMichael Große }, $this->parsedQuery['and']), 223df977249SMichael Große 'not' => array_map(function ($term) { 224df977249SMichael Große return trim($term, '*'); 225df977249SMichael Große }, $this->parsedQuery['not']), 226b005809cSMichael Große ], 227b005809cSMichael Große 'starts' => [ 228b005809cSMichael Große 'label' => $lang['search_starts_with'], 229b005809cSMichael Große 'and' => array_map(function ($term) { 230b005809cSMichael Große return trim($term, '*') . '*'; 231df977249SMichael Große }, $this->parsedQuery['and']), 232df977249SMichael Große 'not' => array_map(function ($term) { 233df977249SMichael Große return trim($term, '*') . '*'; 234df977249SMichael Große }, $this->parsedQuery['not']), 235b005809cSMichael Große ], 236b005809cSMichael Große 'ends' => [ 237b005809cSMichael Große 'label' => $lang['search_ends_with'], 238b005809cSMichael Große 'and' => array_map(function ($term) { 239b005809cSMichael Große return '*' . trim($term, '*'); 240df977249SMichael Große }, $this->parsedQuery['and']), 241df977249SMichael Große 'not' => array_map(function ($term) { 242df977249SMichael Große return '*' . trim($term, '*'); 243df977249SMichael Große }, $this->parsedQuery['not']), 244b005809cSMichael Große ], 245b005809cSMichael Große 'contains' => [ 246b005809cSMichael Große 'label' => $lang['search_contains'], 247b005809cSMichael Große 'and' => array_map(function ($term) { 248b005809cSMichael Große return '*' . trim($term, '*') . '*'; 249df977249SMichael Große }, $this->parsedQuery['and']), 250df977249SMichael Große 'not' => array_map(function ($term) { 251df977249SMichael Große return '*' . trim($term, '*') . '*'; 252df977249SMichael Große }, $this->parsedQuery['not']), 253b005809cSMichael Große ] 254b005809cSMichael Große ]; 255b005809cSMichael Große 256b005809cSMichael Große // detect current 257c6b5b74aSMichael Große $activeOption = 'custom'; 258b005809cSMichael Große foreach ($options as $key => $option) { 259b005809cSMichael Große if ($this->parsedQuery['and'] === $option['and']) { 260b005809cSMichael Große $activeOption = $key; 261b005809cSMichael Große } 262b005809cSMichael Große } 263c6b5b74aSMichael Große if ($activeOption === 'custom') { 264c6b5b74aSMichael Große $options = array_merge(['custom' => [ 265c6b5b74aSMichael Große 'label' => $lang['search_custom_match'], 266c6b5b74aSMichael Große ]], $options); 267c6b5b74aSMichael Große } 268b005809cSMichael Große 2692171f9cbSAndreas Gohr $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true'); 270b005809cSMichael Große // render current 2714bdf82b5SAndreas Gohr $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); 272b005809cSMichael Große if ($activeOption !== 'exact') { 2734bdf82b5SAndreas Gohr $currentWrapper->addClass('changed'); 274b005809cSMichael Große } 275b005809cSMichael Große $searchForm->addHTML($options[$activeOption]['label']); 276b005809cSMichael Große $searchForm->addTagClose('div'); 277b005809cSMichael Große 278b005809cSMichael Große // render options list 2792171f9cbSAndreas Gohr $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false'); 280b005809cSMichael Große 281b005809cSMichael Große foreach ($options as $key => $option) { 2824bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 283b005809cSMichael Große 284b005809cSMichael Große if ($key === $activeOption) { 2854bdf82b5SAndreas Gohr $listItem->addClass('active'); 286b005809cSMichael Große $searchForm->addHTML($option['label']); 287b005809cSMichael Große } else { 28852d4cd42SMichael Große $link = $this->searchState 28952d4cd42SMichael Große ->withFragments($option['and'], $option['not']) 29052d4cd42SMichael Große ->getSearchLink($option['label']) 29152d4cd42SMichael Große ; 29252d4cd42SMichael Große $searchForm->addHTML($link); 293b005809cSMichael Große } 294b005809cSMichael Große $searchForm->addTagClose('li'); 295b005809cSMichael Große } 296b005809cSMichael Große $searchForm->addTagClose('ul'); 2974d0cb6e1SMichael Große 2984d0cb6e1SMichael Große $searchForm->addTagClose('div'); 299b005809cSMichael Große 300b005809cSMichael Große // render options list 3014d0cb6e1SMichael Große } 3024d0cb6e1SMichael Große 303bb8ef867SMichael Große /** 304bb8ef867SMichael Große * Add the elements for the namespace selector 305bb8ef867SMichael Große * 306bb8ef867SMichael Große * @param Form $searchForm 307bb8ef867SMichael Große */ 30818856c5dSMichael Große protected function addNamespaceSelector(Form $searchForm) 309bb8ef867SMichael Große { 310df977249SMichael Große if (!$this->isNamespaceAssistanceAvailable($this->parsedQuery)) { 311df977249SMichael Große return; 312df977249SMichael Große } 313df977249SMichael Große 314b005809cSMichael Große global $lang; 315b005809cSMichael Große 31618856c5dSMichael Große $baseNS = empty($this->parsedQuery['ns']) ? '' : $this->parsedQuery['ns'][0]; 317bbc1da2eSMichael Große $extraNS = $this->getAdditionalNamespacesFromResults($baseNS); 3184d0cb6e1SMichael Große 3192171f9cbSAndreas Gohr $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true'); 320b005809cSMichael Große // render current 3214bdf82b5SAndreas Gohr $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); 322bbc1da2eSMichael Große if ($baseNS) { 3234bdf82b5SAndreas Gohr $currentWrapper->addClass('changed'); 324b005809cSMichael Große $searchForm->addHTML('@' . $baseNS); 325b005809cSMichael Große } else { 326b005809cSMichael Große $searchForm->addHTML($lang['search_any_ns']); 327b005809cSMichael Große } 328b005809cSMichael Große $searchForm->addTagClose('div'); 329b005809cSMichael Große 330b005809cSMichael Große // render options list 3312171f9cbSAndreas Gohr $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false'); 332b005809cSMichael Große 3334bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 334b005809cSMichael Große if ($baseNS) { 3354bdf82b5SAndreas Gohr $listItem->addClass('active'); 33652d4cd42SMichael Große $link = $this->searchState->withNamespace('')->getSearchLink($lang['search_any_ns']); 33752d4cd42SMichael Große $searchForm->addHTML($link); 338b005809cSMichael Große } else { 339b005809cSMichael Große $searchForm->addHTML($lang['search_any_ns']); 340bb8ef867SMichael Große } 341b005809cSMichael Große $searchForm->addTagClose('li'); 342bb8ef867SMichael Große 34318856c5dSMichael Große foreach ($extraNS as $ns => $count) { 3444bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 3451d918893SAndreas Gohr $label = $ns . ($count ? " <bdi>($count)</bdi>" : ''); 3464d0cb6e1SMichael Große 347b005809cSMichael Große if ($ns === $baseNS) { 3484bdf82b5SAndreas Gohr $listItem->addClass('active'); 349b005809cSMichael Große $searchForm->addHTML($label); 350b005809cSMichael Große } else { 35152d4cd42SMichael Große $link = $this->searchState->withNamespace($ns)->getSearchLink($label); 35252d4cd42SMichael Große $searchForm->addHTML($link); 353bb8ef867SMichael Große } 354b005809cSMichael Große $searchForm->addTagClose('li'); 355bb8ef867SMichael Große } 356b005809cSMichael Große $searchForm->addTagClose('ul'); 357bb8ef867SMichael Große 358bb8ef867SMichael Große $searchForm->addTagClose('div'); 359b005809cSMichael Große 360bb8ef867SMichael Große } 361bb8ef867SMichael Große 362bb8ef867SMichael Große /** 363bb8ef867SMichael Große * Parse the full text results for their top namespaces below the given base namespace 364bb8ef867SMichael Große * 365bb8ef867SMichael Große * @param string $baseNS the namespace within which was searched, empty string for root namespace 366bb8ef867SMichael Große * 367bb8ef867SMichael Große * @return array an associative array with namespace => #number of found pages, sorted descending 368bb8ef867SMichael Große */ 369bb8ef867SMichael Große protected function getAdditionalNamespacesFromResults($baseNS) 370bb8ef867SMichael Große { 371bb8ef867SMichael Große $namespaces = []; 372bb8ef867SMichael Große $baseNSLength = strlen($baseNS); 373bb8ef867SMichael Große foreach ($this->fullTextResults as $page => $numberOfHits) { 374bb8ef867SMichael Große $namespace = getNS($page); 375bb8ef867SMichael Große if (!$namespace) { 376bb8ef867SMichael Große continue; 377bb8ef867SMichael Große } 378bb8ef867SMichael Große if ($namespace === $baseNS) { 379bb8ef867SMichael Große continue; 380bb8ef867SMichael Große } 381bb8ef867SMichael Große $firstColon = strpos((string)$namespace, ':', $baseNSLength + 1) ?: strlen($namespace); 382bb8ef867SMichael Große $subtopNS = substr($namespace, 0, $firstColon); 383bb8ef867SMichael Große if (empty($namespaces[$subtopNS])) { 384bb8ef867SMichael Große $namespaces[$subtopNS] = 0; 385bb8ef867SMichael Große } 386bb8ef867SMichael Große $namespaces[$subtopNS] += 1; 387bb8ef867SMichael Große } 388*2d85e841SAndreas Gohr Sort::ksort($namespaces); 389bb8ef867SMichael Große arsort($namespaces); 390bb8ef867SMichael Große return $namespaces; 391bb8ef867SMichael Große } 392bb8ef867SMichael Große 393bb8ef867SMichael Große /** 394bbc1da2eSMichael Große * @ToDo: custom date input 395bbc1da2eSMichael Große * 396bbc1da2eSMichael Große * @param Form $searchForm 397bbc1da2eSMichael Große */ 398b005809cSMichael Große protected function addDateSelector(Form $searchForm) 399b005809cSMichael Große { 400b005809cSMichael Große global $INPUT, $lang; 401bbc1da2eSMichael Große 402b005809cSMichael Große $options = [ 403b005809cSMichael Große 'any' => [ 404b005809cSMichael Große 'before' => false, 405b005809cSMichael Große 'after' => false, 406b005809cSMichael Große 'label' => $lang['search_any_time'], 407b005809cSMichael Große ], 408b005809cSMichael Große 'week' => [ 409b005809cSMichael Große 'before' => false, 410b005809cSMichael Große 'after' => '1 week ago', 411b005809cSMichael Große 'label' => $lang['search_past_7_days'], 412b005809cSMichael Große ], 413b005809cSMichael Große 'month' => [ 414b005809cSMichael Große 'before' => false, 415b005809cSMichael Große 'after' => '1 month ago', 416b005809cSMichael Große 'label' => $lang['search_past_month'], 417b005809cSMichael Große ], 418b005809cSMichael Große 'year' => [ 419b005809cSMichael Große 'before' => false, 420b005809cSMichael Große 'after' => '1 year ago', 421b005809cSMichael Große 'label' => $lang['search_past_year'], 422b005809cSMichael Große ], 423b005809cSMichael Große ]; 424b005809cSMichael Große $activeOption = 'any'; 425b005809cSMichael Große foreach ($options as $key => $option) { 426422bbbc6SMichael Große if ($INPUT->str('min') === $option['after']) { 427b005809cSMichael Große $activeOption = $key; 428b005809cSMichael Große break; 429b005809cSMichael Große } 430b005809cSMichael Große } 431b005809cSMichael Große 4322171f9cbSAndreas Gohr $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true'); 433b005809cSMichael Große // render current 4344bdf82b5SAndreas Gohr $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); 435422bbbc6SMichael Große if ($INPUT->has('max') || $INPUT->has('min')) { 4364bdf82b5SAndreas Gohr $currentWrapper->addClass('changed'); 437bbc1da2eSMichael Große } 438b005809cSMichael Große $searchForm->addHTML($options[$activeOption]['label']); 439b005809cSMichael Große $searchForm->addTagClose('div'); 440bbc1da2eSMichael Große 441b005809cSMichael Große // render options list 4422171f9cbSAndreas Gohr $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false'); 443b005809cSMichael Große 444b005809cSMichael Große foreach ($options as $key => $option) { 4454bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 446b005809cSMichael Große 447b005809cSMichael Große if ($key === $activeOption) { 4484bdf82b5SAndreas Gohr $listItem->addClass('active'); 449b005809cSMichael Große $searchForm->addHTML($option['label']); 450bbc1da2eSMichael Große } else { 45152d4cd42SMichael Große $link = $this->searchState 45252d4cd42SMichael Große ->withTimeLimitations($option['after'], $option['before']) 45352d4cd42SMichael Große ->getSearchLink($option['label']) 45452d4cd42SMichael Große ; 45552d4cd42SMichael Große $searchForm->addHTML($link); 456bbc1da2eSMichael Große } 457b005809cSMichael Große $searchForm->addTagClose('li'); 458bbc1da2eSMichael Große } 459b005809cSMichael Große $searchForm->addTagClose('ul'); 460bbc1da2eSMichael Große 461bbc1da2eSMichael Große $searchForm->addTagClose('div'); 462bbc1da2eSMichael Große } 463bbc1da2eSMichael Große 464bbc1da2eSMichael Große 465bbc1da2eSMichael Große /** 46621fcef82SMichael Große * Build the intro text for the search page 46721fcef82SMichael Große * 46821fcef82SMichael Große * @param string $query the search query 46921fcef82SMichael Große * 47021fcef82SMichael Große * @return string 47121fcef82SMichael Große */ 47221fcef82SMichael Große protected function getSearchIntroHTML($query) 47321fcef82SMichael Große { 4742ce8affcSMichael Große global $lang; 47521fcef82SMichael Große 47621fcef82SMichael Große $intro = p_locale_xhtml('searchpage'); 4772ce8affcSMichael Große 4782ce8affcSMichael Große $queryPagename = $this->createPagenameFromQuery($this->parsedQuery); 4792ce8affcSMichael Große $createQueryPageLink = html_wikilink($queryPagename . '?do=edit', $queryPagename); 4802ce8affcSMichael Große 4812ce8affcSMichael Große $pagecreateinfo = ''; 4822ce8affcSMichael Große if (auth_quickaclcheck($queryPagename) >= AUTH_CREATE) { 4832ce8affcSMichael Große $pagecreateinfo = sprintf($lang['searchcreatepage'], $createQueryPageLink); 4842ce8affcSMichael Große } 48521fcef82SMichael Große $intro = str_replace( 48621fcef82SMichael Große array('@QUERY@', '@SEARCH@', '@CREATEPAGEINFO@'), 48721fcef82SMichael Große array(hsc(rawurlencode($query)), hsc($query), $pagecreateinfo), 48821fcef82SMichael Große $intro 48921fcef82SMichael Große ); 4902ce8affcSMichael Große 49121fcef82SMichael Große return $intro; 49221fcef82SMichael Große } 49321fcef82SMichael Große 49421fcef82SMichael Große /** 4952ce8affcSMichael Große * Create a pagename based the parsed search query 4962ce8affcSMichael Große * 4972ce8affcSMichael Große * @param array $parsedQuery 4982ce8affcSMichael Große * 4992ce8affcSMichael Große * @return string pagename constructed from the parsed query 5002ce8affcSMichael Große */ 50142690e4dSMichael Große public function createPagenameFromQuery($parsedQuery) 5022ce8affcSMichael Große { 503e180e453SPhy $cleanedQuery = cleanID($parsedQuery['query']); // already strtolowered 5048cbc5ee8SAndreas Gohr if ($cleanedQuery === \dokuwiki\Utf8\PhpString::strtolower($parsedQuery['query'])) { 50542690e4dSMichael Große return ':' . $cleanedQuery; 50642690e4dSMichael Große } 5072ce8affcSMichael Große $pagename = ''; 5082ce8affcSMichael Große if (!empty($parsedQuery['ns'])) { 50942690e4dSMichael Große $pagename .= ':' . cleanID($parsedQuery['ns'][0]); 5102ce8affcSMichael Große } 5112ce8affcSMichael Große $pagename .= ':' . cleanID(implode(' ' , $parsedQuery['highlight'])); 5122ce8affcSMichael Große return $pagename; 5132ce8affcSMichael Große } 5142ce8affcSMichael Große 5152ce8affcSMichael Große /** 51621fcef82SMichael Große * Build HTML for a list of pages with matching pagenames 51721fcef82SMichael Große * 51821fcef82SMichael Große * @param array $data search results 51921fcef82SMichael Große * 52021fcef82SMichael Große * @return string 52121fcef82SMichael Große */ 52221fcef82SMichael Große protected function getPageLookupHTML($data) 52321fcef82SMichael Große { 52421fcef82SMichael Große if (empty($data)) { 52521fcef82SMichael Große return ''; 52621fcef82SMichael Große } 52721fcef82SMichael Große 52821fcef82SMichael Große global $lang; 52921fcef82SMichael Große 53021fcef82SMichael Große $html = '<div class="search_quickresult">'; 5316d55fda7SMichael Große $html .= '<h2>' . $lang['quickhits'] . ':</h2>'; 53221fcef82SMichael Große $html .= '<ul class="search_quickhits">'; 53321fcef82SMichael Große foreach ($data as $id => $title) { 5345d87aa31SMichael Große $name = null; 5355d87aa31SMichael Große if (!useHeading('navigation') && $ns = getNS($id)) { 5365d87aa31SMichael Große $name = shorten(noNS($id), ' (' . $ns . ')', 30); 5375d87aa31SMichael Große } 5385d87aa31SMichael Große $link = html_wikilink(':' . $id, $name); 5394eab6f7cSMichael Große $eventData = [ 5404eab6f7cSMichael Große 'listItemContent' => [$link], 5414eab6f7cSMichael Große 'page' => $id, 5424eab6f7cSMichael Große ]; 543cbb44eabSAndreas Gohr Event::createAndTrigger('SEARCH_RESULT_PAGELOOKUP', $eventData); 5444eab6f7cSMichael Große $html .= '<li>' . implode('', $eventData['listItemContent']) . '</li>'; 54521fcef82SMichael Große } 54621fcef82SMichael Große $html .= '</ul> '; 54721fcef82SMichael Große //clear float (see http://www.complexspiral.com/publications/containing-floats/) 54821fcef82SMichael Große $html .= '<div class="clearer"></div>'; 54921fcef82SMichael Große $html .= '</div>'; 55021fcef82SMichael Große 55121fcef82SMichael Große return $html; 55221fcef82SMichael Große } 55321fcef82SMichael Große 55421fcef82SMichael Große /** 55521fcef82SMichael Große * Build HTML for fulltext search results or "no results" message 55621fcef82SMichael Große * 55721fcef82SMichael Große * @param array $data the results of the fulltext search 55821fcef82SMichael Große * @param array $highlight the terms to be highlighted in the results 55921fcef82SMichael Große * 56021fcef82SMichael Große * @return string 56121fcef82SMichael Große */ 56221fcef82SMichael Große protected function getFulltextResultsHTML($data, $highlight) 56321fcef82SMichael Große { 56421fcef82SMichael Große global $lang; 56521fcef82SMichael Große 56621fcef82SMichael Große if (empty($data)) { 56721fcef82SMichael Große return '<div class="nothing">' . $lang['nothingfound'] . '</div>'; 56821fcef82SMichael Große } 56921fcef82SMichael Große 5702ce8affcSMichael Große $html = '<div class="search_fulltextresult">'; 5716d55fda7SMichael Große $html .= '<h2>' . $lang['search_fullresults'] . ':</h2>'; 5722ce8affcSMichael Große 57321fcef82SMichael Große $html .= '<dl class="search_results">'; 5748225e1abSMichael Große $num = 0; 5758225e1abSMichael Große $position = 0; 5764c924eb8SMichael Große 57721fcef82SMichael Große foreach ($data as $id => $cnt) { 57878d786c9SMichael Große $position += 1; 5794eab6f7cSMichael Große $resultLink = html_wikilink(':' . $id, null, $highlight); 5804c924eb8SMichael Große 5814c924eb8SMichael Große $resultHeader = [$resultLink]; 5824c924eb8SMichael Große 5834eab6f7cSMichael Große 5844c924eb8SMichael Große $restrictQueryToNSLink = $this->restrictQueryToNSLink(getNS($id)); 5854c924eb8SMichael Große if ($restrictQueryToNSLink) { 5864c924eb8SMichael Große $resultHeader[] = $restrictQueryToNSLink; 5874c924eb8SMichael Große } 5884c924eb8SMichael Große 5895d06a1e4SMichael Große $resultBody = []; 5909a75abfbSMichael Große $mtime = filemtime(wikiFN($id)); 5915d06a1e4SMichael Große $lastMod = '<span class="lastmod">' . $lang['lastmod'] . '</span> '; 59264159a61SAndreas Gohr $lastMod .= '<time datetime="' . date_iso8601($mtime) . '" title="' . dformat($mtime) . '">' . 59364159a61SAndreas Gohr dformat($mtime, '%f') . 59464159a61SAndreas Gohr '</time>'; 595f0861d1fSMichael Große $resultBody['meta'] = $lastMod; 5965d06a1e4SMichael Große if ($cnt !== 0) { 5978225e1abSMichael Große $num++; 598b12bcb77SAnika Henke $hits = '<span class="hits">' . $cnt . ' ' . $lang['hits'] . '</span>, '; 599f0861d1fSMichael Große $resultBody['meta'] = $hits . $resultBody['meta']; 6008225e1abSMichael Große if ($num <= FT_SNIPPET_NUMBER) { // create snippets for the first number of matches only 601f0861d1fSMichael Große $resultBody['snippet'] = ft_snippet($id, $highlight); 6029a75abfbSMichael Große } 6039a75abfbSMichael Große } 6049a75abfbSMichael Große 6054eab6f7cSMichael Große $eventData = [ 6064c924eb8SMichael Große 'resultHeader' => $resultHeader, 6075d06a1e4SMichael Große 'resultBody' => $resultBody, 6084eab6f7cSMichael Große 'page' => $id, 60978d786c9SMichael Große 'position' => $position, 6104eab6f7cSMichael Große ]; 611cbb44eabSAndreas Gohr Event::createAndTrigger('SEARCH_RESULT_FULLPAGE', $eventData); 6125d06a1e4SMichael Große $html .= '<div class="search_fullpage_result">'; 6134eab6f7cSMichael Große $html .= '<dt>' . implode(' ', $eventData['resultHeader']) . '</dt>'; 6145d06a1e4SMichael Große foreach ($eventData['resultBody'] as $class => $htmlContent) { 6155d06a1e4SMichael Große $html .= "<dd class=\"$class\">$htmlContent</dd>"; 6165d06a1e4SMichael Große } 6175d06a1e4SMichael Große $html .= '</div>'; 61821fcef82SMichael Große } 61921fcef82SMichael Große $html .= '</dl>'; 62021fcef82SMichael Große 6212ce8affcSMichael Große $html .= '</div>'; 6222ce8affcSMichael Große 62321fcef82SMichael Große return $html; 62421fcef82SMichael Große } 6254c924eb8SMichael Große 6264c924eb8SMichael Große /** 6274c924eb8SMichael Große * create a link to restrict the current query to a namespace 6284c924eb8SMichael Große * 629ec27794fSMichael Große * @param false|string $ns the namespace to which to restrict the query 6304c924eb8SMichael Große * 631ec27794fSMichael Große * @return false|string 6324c924eb8SMichael Große */ 6334c924eb8SMichael Große protected function restrictQueryToNSLink($ns) 6344c924eb8SMichael Große { 6354c924eb8SMichael Große if (!$ns) { 6364c924eb8SMichael Große return false; 6374c924eb8SMichael Große } 638df977249SMichael Große if (!$this->isNamespaceAssistanceAvailable($this->parsedQuery)) { 6394c924eb8SMichael Große return false; 6404c924eb8SMichael Große } 6414c924eb8SMichael Große if (!empty($this->parsedQuery['ns']) && $this->parsedQuery['ns'][0] === $ns) { 6424c924eb8SMichael Große return false; 6434c924eb8SMichael Große } 64452d4cd42SMichael Große 6454c924eb8SMichael Große $name = '@' . $ns; 64652d4cd42SMichael Große return $this->searchState->withNamespace($ns)->getSearchLink($name); 6474c924eb8SMichael Große } 64821fcef82SMichael Große} 649