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*0cba610bSSatoshi Saharause dokuwiki\Search\FulltextSearch; 8*0cba610bSSatoshi Saharause dokuwiki\Search\QueryParser; 9*0cba610bSSatoshi Sahara 10*0cba610bSSatoshi Saharause const dokuwiki\Search\FT_SNIPPET_NUMBER; 11427ed988SMichael Große 1221fcef82SMichael Großeclass Search extends Ui 1321fcef82SMichael Große{ 1421fcef82SMichael Große protected $query; 154c924eb8SMichael Große protected $parsedQuery; 1618856c5dSMichael Große protected $searchState; 1721fcef82SMichael Große protected $pageLookupResults = array(); 1821fcef82SMichael Große protected $fullTextResults = array(); 1921fcef82SMichael Große protected $highlight = array(); 2021fcef82SMichael Große 2121fcef82SMichael Große /** 2221fcef82SMichael Große * Search constructor. 236639a152SMichael Große * 24fc46ed58SMichael Große * @param array $pageLookupResults pagename lookup results in the form [pagename => pagetitle] 25fc46ed58SMichael Große * @param array $fullTextResults fulltext search results in the form [pagename => #hits] 26fc46ed58SMichael Große * @param array $highlight array of strings to be highlighted 2721fcef82SMichael Große */ 286639a152SMichael Große public function __construct(array $pageLookupResults, array $fullTextResults, $highlight) 2921fcef82SMichael Große { 30d09b5b64SMichael Große global $QUERY; 31d09b5b64SMichael Große 32d09b5b64SMichael Große $this->query = $QUERY; 33*0cba610bSSatoshi Sahara $this->parsedQuery = QueryParser::convert($QUERY); 3418856c5dSMichael Große $this->searchState = new SearchState($this->parsedQuery); 3521fcef82SMichael Große 366639a152SMichael Große $this->pageLookupResults = $pageLookupResults; 376639a152SMichael Große $this->fullTextResults = $fullTextResults; 3821fcef82SMichael Große $this->highlight = $highlight; 39b3cfe85aSMichael Große } 40bbc1da2eSMichael Große 41b3cfe85aSMichael Große /** 4221fcef82SMichael Große * display the search result 4321fcef82SMichael Große * 4421fcef82SMichael Große * @return void 4521fcef82SMichael Große */ 4621fcef82SMichael Große public function show() 4721fcef82SMichael Große { 4821fcef82SMichael Große $searchHTML = ''; 4921fcef82SMichael Große 5021fcef82SMichael Große $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 93cbb44eabSAndreas Gohr Event::createAndTrigger('FORM_SEARCH_OUTPUT', $searchForm); 9481a0edd9SMichael Große 95427ed988SMichael Große return $searchForm->toHTML(); 96427ed988SMichael Große } 97427ed988SMichael Große 98be76738bSMichael Große /** 99be76738bSMichael Große * Add elements to adjust how the results are sorted 100be76738bSMichael Große * 101be76738bSMichael Große * @param Form $searchForm 102be76738bSMichael Große */ 103b005809cSMichael Große protected function addSortTool(Form $searchForm) 104b005809cSMichael Große { 105b005809cSMichael Große global $INPUT, $lang; 106b005809cSMichael Große 107b005809cSMichael Große $options = [ 108b005809cSMichael Große 'hits' => [ 109b005809cSMichael Große 'label' => $lang['search_sort_by_hits'], 110b005809cSMichael Große 'sort' => '', 111b005809cSMichael Große ], 112b005809cSMichael Große 'mtime' => [ 113b005809cSMichael Große 'label' => $lang['search_sort_by_mtime'], 114b005809cSMichael Große 'sort' => 'mtime', 115b005809cSMichael Große ], 116b005809cSMichael Große ]; 117b005809cSMichael Große $activeOption = 'hits'; 118b005809cSMichael Große 1191265b193SMichael Große if ($INPUT->str('srt') === 'mtime') { 120b005809cSMichael Große $activeOption = 'mtime'; 121b005809cSMichael Große } 122b005809cSMichael Große 1232171f9cbSAndreas Gohr $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true'); 124b005809cSMichael Große // render current 1254bdf82b5SAndreas Gohr $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); 126b005809cSMichael Große if ($activeOption !== 'hits') { 1274bdf82b5SAndreas Gohr $currentWrapper->addClass('changed'); 128b005809cSMichael Große } 129b005809cSMichael Große $searchForm->addHTML($options[$activeOption]['label']); 130b005809cSMichael Große $searchForm->addTagClose('div'); 131b005809cSMichael Große 132b005809cSMichael Große // render options list 1332171f9cbSAndreas Gohr $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false'); 134b005809cSMichael Große 135b005809cSMichael Große foreach ($options as $key => $option) { 1364bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 137b005809cSMichael Große 138b005809cSMichael Große if ($key === $activeOption) { 1394bdf82b5SAndreas Gohr $listItem->addClass('active'); 140b005809cSMichael Große $searchForm->addHTML($option['label']); 141b005809cSMichael Große } else { 14252d4cd42SMichael Große $link = $this->searchState->withSorting($option['sort'])->getSearchLink($option['label']); 14352d4cd42SMichael Große $searchForm->addHTML($link); 144b005809cSMichael Große } 145b005809cSMichael Große $searchForm->addTagClose('li'); 146b005809cSMichael Große } 147b005809cSMichael Große $searchForm->addTagClose('ul'); 148b005809cSMichael Große 149b005809cSMichael Große $searchForm->addTagClose('div'); 150b005809cSMichael Große 151b005809cSMichael Große } 152b005809cSMichael Große 153be76738bSMichael Große /** 154be76738bSMichael Große * Check if the query is simple enough to modify its namespace limitations without breaking the rest of the query 155be76738bSMichael Große * 156be76738bSMichael Große * @param array $parsedQuery 157be76738bSMichael Große * 158be76738bSMichael Große * @return bool 159be76738bSMichael Große */ 160df977249SMichael Große protected function isNamespaceAssistanceAvailable(array $parsedQuery) { 161df977249SMichael Große if (preg_match('/[\(\)\|]/', $parsedQuery['query']) === 1) { 162bb8ef867SMichael Große return false; 163bb8ef867SMichael Große } 164df977249SMichael Große 165df977249SMichael Große return true; 166df977249SMichael Große } 167df977249SMichael Große 168be76738bSMichael Große /** 169be76738bSMichael Große * Check if the query is simple enough to modify the fragment search behavior without breaking the rest of the query 170be76738bSMichael Große * 171be76738bSMichael Große * @param array $parsedQuery 172be76738bSMichael Große * 173be76738bSMichael Große * @return bool 174be76738bSMichael Große */ 175df977249SMichael Große protected function isFragmentAssistanceAvailable(array $parsedQuery) { 176df977249SMichael Große if (preg_match('/[\(\)\|]/', $parsedQuery['query']) === 1) { 177bb8ef867SMichael Große return false; 178bb8ef867SMichael Große } 179bb8ef867SMichael Große 180bb8ef867SMichael Große if (!empty($parsedQuery['phrases'])) { 181bb8ef867SMichael Große return false; 182bb8ef867SMichael Große } 183bb8ef867SMichael Große 184bb8ef867SMichael Große return true; 185bb8ef867SMichael Große } 186bb8ef867SMichael Große 187bb8ef867SMichael Große /** 188bb8ef867SMichael Große * Add the elements to be used for search assistance 189bb8ef867SMichael Große * 190bb8ef867SMichael Große * @param Form $searchForm 191bb8ef867SMichael Große */ 19218856c5dSMichael Große protected function addSearchAssistanceElements(Form $searchForm) 193bb8ef867SMichael Große { 194bb8ef867SMichael Große $searchForm->addTagOpen('div') 1954bdf82b5SAndreas Gohr ->addClass('advancedOptions') 1962171f9cbSAndreas Gohr ->attr('style', 'display: none;') 1972171f9cbSAndreas Gohr ->attr('aria-hidden', 'true'); 198bb8ef867SMichael Große 19918856c5dSMichael Große $this->addFragmentBehaviorLinks($searchForm); 20018856c5dSMichael Große $this->addNamespaceSelector($searchForm); 20118856c5dSMichael Große $this->addDateSelector($searchForm); 202b005809cSMichael Große $this->addSortTool($searchForm); 203bb8ef867SMichael Große 204bb8ef867SMichael Große $searchForm->addTagClose('div'); 205bb8ef867SMichael Große } 206bb8ef867SMichael Große 207be76738bSMichael Große /** 208be76738bSMichael Große * Add the elements to adjust the fragment search behavior 209be76738bSMichael Große * 210be76738bSMichael Große * @param Form $searchForm 211be76738bSMichael Große */ 21218856c5dSMichael Große protected function addFragmentBehaviorLinks(Form $searchForm) 2134d0cb6e1SMichael Große { 214df977249SMichael Große if (!$this->isFragmentAssistanceAvailable($this->parsedQuery)) { 215df977249SMichael Große return; 216df977249SMichael Große } 217b005809cSMichael Große global $lang; 2184d0cb6e1SMichael Große 219b005809cSMichael Große $options = [ 220b005809cSMichael Große 'exact' => [ 221b005809cSMichael Große 'label' => $lang['search_exact_match'], 222b005809cSMichael Große 'and' => array_map(function ($term) { 223b005809cSMichael Große return trim($term, '*'); 224b005809cSMichael Große }, $this->parsedQuery['and']), 225df977249SMichael Große 'not' => array_map(function ($term) { 226df977249SMichael Große return trim($term, '*'); 227df977249SMichael Große }, $this->parsedQuery['not']), 228b005809cSMichael Große ], 229b005809cSMichael Große 'starts' => [ 230b005809cSMichael Große 'label' => $lang['search_starts_with'], 231b005809cSMichael Große 'and' => array_map(function ($term) { 232b005809cSMichael Große return trim($term, '*') . '*'; 233df977249SMichael Große }, $this->parsedQuery['and']), 234df977249SMichael Große 'not' => array_map(function ($term) { 235df977249SMichael Große return trim($term, '*') . '*'; 236df977249SMichael Große }, $this->parsedQuery['not']), 237b005809cSMichael Große ], 238b005809cSMichael Große 'ends' => [ 239b005809cSMichael Große 'label' => $lang['search_ends_with'], 240b005809cSMichael Große 'and' => array_map(function ($term) { 241b005809cSMichael Große return '*' . trim($term, '*'); 242df977249SMichael Große }, $this->parsedQuery['and']), 243df977249SMichael Große 'not' => array_map(function ($term) { 244df977249SMichael Große return '*' . trim($term, '*'); 245df977249SMichael Große }, $this->parsedQuery['not']), 246b005809cSMichael Große ], 247b005809cSMichael Große 'contains' => [ 248b005809cSMichael Große 'label' => $lang['search_contains'], 249b005809cSMichael Große 'and' => array_map(function ($term) { 250b005809cSMichael Große return '*' . trim($term, '*') . '*'; 251df977249SMichael Große }, $this->parsedQuery['and']), 252df977249SMichael Große 'not' => array_map(function ($term) { 253df977249SMichael Große return '*' . trim($term, '*') . '*'; 254df977249SMichael Große }, $this->parsedQuery['not']), 255b005809cSMichael Große ] 256b005809cSMichael Große ]; 257b005809cSMichael Große 258b005809cSMichael Große // detect current 259c6b5b74aSMichael Große $activeOption = 'custom'; 260b005809cSMichael Große foreach ($options as $key => $option) { 261b005809cSMichael Große if ($this->parsedQuery['and'] === $option['and']) { 262b005809cSMichael Große $activeOption = $key; 263b005809cSMichael Große } 264b005809cSMichael Große } 265c6b5b74aSMichael Große if ($activeOption === 'custom') { 266c6b5b74aSMichael Große $options = array_merge(['custom' => [ 267c6b5b74aSMichael Große 'label' => $lang['search_custom_match'], 268c6b5b74aSMichael Große ]], $options); 269c6b5b74aSMichael Große } 270b005809cSMichael Große 2712171f9cbSAndreas Gohr $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true'); 272b005809cSMichael Große // render current 2734bdf82b5SAndreas Gohr $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); 274b005809cSMichael Große if ($activeOption !== 'exact') { 2754bdf82b5SAndreas Gohr $currentWrapper->addClass('changed'); 276b005809cSMichael Große } 277b005809cSMichael Große $searchForm->addHTML($options[$activeOption]['label']); 278b005809cSMichael Große $searchForm->addTagClose('div'); 279b005809cSMichael Große 280b005809cSMichael Große // render options list 2812171f9cbSAndreas Gohr $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false'); 282b005809cSMichael Große 283b005809cSMichael Große foreach ($options as $key => $option) { 2844bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 285b005809cSMichael Große 286b005809cSMichael Große if ($key === $activeOption) { 2874bdf82b5SAndreas Gohr $listItem->addClass('active'); 288b005809cSMichael Große $searchForm->addHTML($option['label']); 289b005809cSMichael Große } else { 29052d4cd42SMichael Große $link = $this->searchState 29152d4cd42SMichael Große ->withFragments($option['and'], $option['not']) 29252d4cd42SMichael Große ->getSearchLink($option['label']) 29352d4cd42SMichael Große ; 29452d4cd42SMichael Große $searchForm->addHTML($link); 295b005809cSMichael Große } 296b005809cSMichael Große $searchForm->addTagClose('li'); 297b005809cSMichael Große } 298b005809cSMichael Große $searchForm->addTagClose('ul'); 2994d0cb6e1SMichael Große 3004d0cb6e1SMichael Große $searchForm->addTagClose('div'); 301b005809cSMichael Große 302b005809cSMichael Große // render options list 3034d0cb6e1SMichael Große } 3044d0cb6e1SMichael Große 305bb8ef867SMichael Große /** 306bb8ef867SMichael Große * Add the elements for the namespace selector 307bb8ef867SMichael Große * 308bb8ef867SMichael Große * @param Form $searchForm 309bb8ef867SMichael Große */ 31018856c5dSMichael Große protected function addNamespaceSelector(Form $searchForm) 311bb8ef867SMichael Große { 312df977249SMichael Große if (!$this->isNamespaceAssistanceAvailable($this->parsedQuery)) { 313df977249SMichael Große return; 314df977249SMichael Große } 315df977249SMichael Große 316b005809cSMichael Große global $lang; 317b005809cSMichael Große 31818856c5dSMichael Große $baseNS = empty($this->parsedQuery['ns']) ? '' : $this->parsedQuery['ns'][0]; 319bbc1da2eSMichael Große $extraNS = $this->getAdditionalNamespacesFromResults($baseNS); 3204d0cb6e1SMichael Große 3212171f9cbSAndreas Gohr $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true'); 322b005809cSMichael Große // render current 3234bdf82b5SAndreas Gohr $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); 324bbc1da2eSMichael Große if ($baseNS) { 3254bdf82b5SAndreas Gohr $currentWrapper->addClass('changed'); 326b005809cSMichael Große $searchForm->addHTML('@' . $baseNS); 327b005809cSMichael Große } else { 328b005809cSMichael Große $searchForm->addHTML($lang['search_any_ns']); 329b005809cSMichael Große } 330b005809cSMichael Große $searchForm->addTagClose('div'); 331b005809cSMichael Große 332b005809cSMichael Große // render options list 3332171f9cbSAndreas Gohr $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false'); 334b005809cSMichael Große 3354bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 336b005809cSMichael Große if ($baseNS) { 3374bdf82b5SAndreas Gohr $listItem->addClass('active'); 33852d4cd42SMichael Große $link = $this->searchState->withNamespace('')->getSearchLink($lang['search_any_ns']); 33952d4cd42SMichael Große $searchForm->addHTML($link); 340b005809cSMichael Große } else { 341b005809cSMichael Große $searchForm->addHTML($lang['search_any_ns']); 342bb8ef867SMichael Große } 343b005809cSMichael Große $searchForm->addTagClose('li'); 344bb8ef867SMichael Große 34518856c5dSMichael Große foreach ($extraNS as $ns => $count) { 3464bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 3471d918893SAndreas Gohr $label = $ns . ($count ? " <bdi>($count)</bdi>" : ''); 3484d0cb6e1SMichael Große 349b005809cSMichael Große if ($ns === $baseNS) { 3504bdf82b5SAndreas Gohr $listItem->addClass('active'); 351b005809cSMichael Große $searchForm->addHTML($label); 352b005809cSMichael Große } else { 35352d4cd42SMichael Große $link = $this->searchState->withNamespace($ns)->getSearchLink($label); 35452d4cd42SMichael Große $searchForm->addHTML($link); 355bb8ef867SMichael Große } 356b005809cSMichael Große $searchForm->addTagClose('li'); 357bb8ef867SMichael Große } 358b005809cSMichael Große $searchForm->addTagClose('ul'); 359bb8ef867SMichael Große 360bb8ef867SMichael Große $searchForm->addTagClose('div'); 361b005809cSMichael Große 362bb8ef867SMichael Große } 363bb8ef867SMichael Große 364bb8ef867SMichael Große /** 365bb8ef867SMichael Große * Parse the full text results for their top namespaces below the given base namespace 366bb8ef867SMichael Große * 367bb8ef867SMichael Große * @param string $baseNS the namespace within which was searched, empty string for root namespace 368bb8ef867SMichael Große * 369bb8ef867SMichael Große * @return array an associative array with namespace => #number of found pages, sorted descending 370bb8ef867SMichael Große */ 371bb8ef867SMichael Große protected function getAdditionalNamespacesFromResults($baseNS) 372bb8ef867SMichael Große { 373bb8ef867SMichael Große $namespaces = []; 374bb8ef867SMichael Große $baseNSLength = strlen($baseNS); 375bb8ef867SMichael Große foreach ($this->fullTextResults as $page => $numberOfHits) { 376bb8ef867SMichael Große $namespace = getNS($page); 377bb8ef867SMichael Große if (!$namespace) { 378bb8ef867SMichael Große continue; 379bb8ef867SMichael Große } 380bb8ef867SMichael Große if ($namespace === $baseNS) { 381bb8ef867SMichael Große continue; 382bb8ef867SMichael Große } 383bb8ef867SMichael Große $firstColon = strpos((string)$namespace, ':', $baseNSLength + 1) ?: strlen($namespace); 384bb8ef867SMichael Große $subtopNS = substr($namespace, 0, $firstColon); 385bb8ef867SMichael Große if (empty($namespaces[$subtopNS])) { 386bb8ef867SMichael Große $namespaces[$subtopNS] = 0; 387bb8ef867SMichael Große } 388bb8ef867SMichael Große $namespaces[$subtopNS] += 1; 389bb8ef867SMichael Große } 39055dc8783SMichael Große ksort($namespaces); 391bb8ef867SMichael Große arsort($namespaces); 392bb8ef867SMichael Große return $namespaces; 393bb8ef867SMichael Große } 394bb8ef867SMichael Große 395bb8ef867SMichael Große /** 396bbc1da2eSMichael Große * @ToDo: custom date input 397bbc1da2eSMichael Große * 398bbc1da2eSMichael Große * @param Form $searchForm 399bbc1da2eSMichael Große */ 400b005809cSMichael Große protected function addDateSelector(Form $searchForm) 401b005809cSMichael Große { 402b005809cSMichael Große global $INPUT, $lang; 403bbc1da2eSMichael Große 404b005809cSMichael Große $options = [ 405b005809cSMichael Große 'any' => [ 406b005809cSMichael Große 'before' => false, 407b005809cSMichael Große 'after' => false, 408b005809cSMichael Große 'label' => $lang['search_any_time'], 409b005809cSMichael Große ], 410b005809cSMichael Große 'week' => [ 411b005809cSMichael Große 'before' => false, 412b005809cSMichael Große 'after' => '1 week ago', 413b005809cSMichael Große 'label' => $lang['search_past_7_days'], 414b005809cSMichael Große ], 415b005809cSMichael Große 'month' => [ 416b005809cSMichael Große 'before' => false, 417b005809cSMichael Große 'after' => '1 month ago', 418b005809cSMichael Große 'label' => $lang['search_past_month'], 419b005809cSMichael Große ], 420b005809cSMichael Große 'year' => [ 421b005809cSMichael Große 'before' => false, 422b005809cSMichael Große 'after' => '1 year ago', 423b005809cSMichael Große 'label' => $lang['search_past_year'], 424b005809cSMichael Große ], 425b005809cSMichael Große ]; 426b005809cSMichael Große $activeOption = 'any'; 427b005809cSMichael Große foreach ($options as $key => $option) { 428422bbbc6SMichael Große if ($INPUT->str('min') === $option['after']) { 429b005809cSMichael Große $activeOption = $key; 430b005809cSMichael Große break; 431b005809cSMichael Große } 432b005809cSMichael Große } 433b005809cSMichael Große 4342171f9cbSAndreas Gohr $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true'); 435b005809cSMichael Große // render current 4364bdf82b5SAndreas Gohr $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); 437422bbbc6SMichael Große if ($INPUT->has('max') || $INPUT->has('min')) { 4384bdf82b5SAndreas Gohr $currentWrapper->addClass('changed'); 439bbc1da2eSMichael Große } 440b005809cSMichael Große $searchForm->addHTML($options[$activeOption]['label']); 441b005809cSMichael Große $searchForm->addTagClose('div'); 442bbc1da2eSMichael Große 443b005809cSMichael Große // render options list 4442171f9cbSAndreas Gohr $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false'); 445b005809cSMichael Große 446b005809cSMichael Große foreach ($options as $key => $option) { 4474bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 448b005809cSMichael Große 449b005809cSMichael Große if ($key === $activeOption) { 4504bdf82b5SAndreas Gohr $listItem->addClass('active'); 451b005809cSMichael Große $searchForm->addHTML($option['label']); 452bbc1da2eSMichael Große } else { 45352d4cd42SMichael Große $link = $this->searchState 45452d4cd42SMichael Große ->withTimeLimitations($option['after'], $option['before']) 45552d4cd42SMichael Große ->getSearchLink($option['label']) 45652d4cd42SMichael Große ; 45752d4cd42SMichael Große $searchForm->addHTML($link); 458bbc1da2eSMichael Große } 459b005809cSMichael Große $searchForm->addTagClose('li'); 460bbc1da2eSMichael Große } 461b005809cSMichael Große $searchForm->addTagClose('ul'); 462bbc1da2eSMichael Große 463bbc1da2eSMichael Große $searchForm->addTagClose('div'); 464bbc1da2eSMichael Große } 465bbc1da2eSMichael Große 466bbc1da2eSMichael Große 467bbc1da2eSMichael Große /** 46821fcef82SMichael Große * Build the intro text for the search page 46921fcef82SMichael Große * 47021fcef82SMichael Große * @param string $query the search query 47121fcef82SMichael Große * 47221fcef82SMichael Große * @return string 47321fcef82SMichael Große */ 47421fcef82SMichael Große protected function getSearchIntroHTML($query) 47521fcef82SMichael Große { 4762ce8affcSMichael Große global $lang; 47721fcef82SMichael Große 47821fcef82SMichael Große $intro = p_locale_xhtml('searchpage'); 4792ce8affcSMichael Große 4802ce8affcSMichael Große $queryPagename = $this->createPagenameFromQuery($this->parsedQuery); 4812ce8affcSMichael Große $createQueryPageLink = html_wikilink($queryPagename . '?do=edit', $queryPagename); 4822ce8affcSMichael Große 4832ce8affcSMichael Große $pagecreateinfo = ''; 4842ce8affcSMichael Große if (auth_quickaclcheck($queryPagename) >= AUTH_CREATE) { 4852ce8affcSMichael Große $pagecreateinfo = sprintf($lang['searchcreatepage'], $createQueryPageLink); 4862ce8affcSMichael Große } 48721fcef82SMichael Große $intro = str_replace( 48821fcef82SMichael Große array('@QUERY@', '@SEARCH@', '@CREATEPAGEINFO@'), 48921fcef82SMichael Große array(hsc(rawurlencode($query)), hsc($query), $pagecreateinfo), 49021fcef82SMichael Große $intro 49121fcef82SMichael Große ); 4922ce8affcSMichael Große 49321fcef82SMichael Große return $intro; 49421fcef82SMichael Große } 49521fcef82SMichael Große 49621fcef82SMichael Große /** 4972ce8affcSMichael Große * Create a pagename based the parsed search query 4982ce8affcSMichael Große * 4992ce8affcSMichael Große * @param array $parsedQuery 5002ce8affcSMichael Große * 5012ce8affcSMichael Große * @return string pagename constructed from the parsed query 5022ce8affcSMichael Große */ 50342690e4dSMichael Große public function createPagenameFromQuery($parsedQuery) 5042ce8affcSMichael Große { 505e180e453SPhy $cleanedQuery = cleanID($parsedQuery['query']); // already strtolowered 5068cbc5ee8SAndreas Gohr if ($cleanedQuery === \dokuwiki\Utf8\PhpString::strtolower($parsedQuery['query'])) { 50742690e4dSMichael Große return ':' . $cleanedQuery; 50842690e4dSMichael Große } 5092ce8affcSMichael Große $pagename = ''; 5102ce8affcSMichael Große if (!empty($parsedQuery['ns'])) { 51142690e4dSMichael Große $pagename .= ':' . cleanID($parsedQuery['ns'][0]); 5122ce8affcSMichael Große } 5132ce8affcSMichael Große $pagename .= ':' . cleanID(implode(' ' , $parsedQuery['highlight'])); 5142ce8affcSMichael Große return $pagename; 5152ce8affcSMichael Große } 5162ce8affcSMichael Große 5172ce8affcSMichael Große /** 51821fcef82SMichael Große * Build HTML for a list of pages with matching pagenames 51921fcef82SMichael Große * 52021fcef82SMichael Große * @param array $data search results 52121fcef82SMichael Große * 52221fcef82SMichael Große * @return string 52321fcef82SMichael Große */ 52421fcef82SMichael Große protected function getPageLookupHTML($data) 52521fcef82SMichael Große { 52621fcef82SMichael Große if (empty($data)) { 52721fcef82SMichael Große return ''; 52821fcef82SMichael Große } 52921fcef82SMichael Große 53021fcef82SMichael Große global $lang; 53121fcef82SMichael Große 53221fcef82SMichael Große $html = '<div class="search_quickresult">'; 5336d55fda7SMichael Große $html .= '<h2>' . $lang['quickhits'] . ':</h2>'; 53421fcef82SMichael Große $html .= '<ul class="search_quickhits">'; 53521fcef82SMichael Große foreach ($data as $id => $title) { 5365d87aa31SMichael Große $name = null; 5375d87aa31SMichael Große if (!useHeading('navigation') && $ns = getNS($id)) { 5385d87aa31SMichael Große $name = shorten(noNS($id), ' (' . $ns . ')', 30); 5395d87aa31SMichael Große } 5405d87aa31SMichael Große $link = html_wikilink(':' . $id, $name); 5414eab6f7cSMichael Große $eventData = [ 5424eab6f7cSMichael Große 'listItemContent' => [$link], 5434eab6f7cSMichael Große 'page' => $id, 5444eab6f7cSMichael Große ]; 545cbb44eabSAndreas Gohr Event::createAndTrigger('SEARCH_RESULT_PAGELOOKUP', $eventData); 5464eab6f7cSMichael Große $html .= '<li>' . implode('', $eventData['listItemContent']) . '</li>'; 54721fcef82SMichael Große } 54821fcef82SMichael Große $html .= '</ul> '; 54921fcef82SMichael Große //clear float (see http://www.complexspiral.com/publications/containing-floats/) 55021fcef82SMichael Große $html .= '<div class="clearer"></div>'; 55121fcef82SMichael Große $html .= '</div>'; 55221fcef82SMichael Große 55321fcef82SMichael Große return $html; 55421fcef82SMichael Große } 55521fcef82SMichael Große 55621fcef82SMichael Große /** 55721fcef82SMichael Große * Build HTML for fulltext search results or "no results" message 55821fcef82SMichael Große * 55921fcef82SMichael Große * @param array $data the results of the fulltext search 56021fcef82SMichael Große * @param array $highlight the terms to be highlighted in the results 56121fcef82SMichael Große * 56221fcef82SMichael Große * @return string 56321fcef82SMichael Große */ 56421fcef82SMichael Große protected function getFulltextResultsHTML($data, $highlight) 56521fcef82SMichael Große { 56621fcef82SMichael Große global $lang; 56721fcef82SMichael Große 56821fcef82SMichael Große if (empty($data)) { 56921fcef82SMichael Große return '<div class="nothing">' . $lang['nothingfound'] . '</div>'; 57021fcef82SMichael Große } 57121fcef82SMichael Große 5722ce8affcSMichael Große $html = '<div class="search_fulltextresult">'; 5736d55fda7SMichael Große $html .= '<h2>' . $lang['search_fullresults'] . ':</h2>'; 5742ce8affcSMichael Große 57521fcef82SMichael Große $html .= '<dl class="search_results">'; 5768225e1abSMichael Große $num = 0; 5778225e1abSMichael Große $position = 0; 5784c924eb8SMichael Große 57921fcef82SMichael Große foreach ($data as $id => $cnt) { 58078d786c9SMichael Große $position += 1; 5814eab6f7cSMichael Große $resultLink = html_wikilink(':' . $id, null, $highlight); 5824c924eb8SMichael Große 5834c924eb8SMichael Große $resultHeader = [$resultLink]; 5844c924eb8SMichael Große 5854eab6f7cSMichael Große 5864c924eb8SMichael Große $restrictQueryToNSLink = $this->restrictQueryToNSLink(getNS($id)); 5874c924eb8SMichael Große if ($restrictQueryToNSLink) { 5884c924eb8SMichael Große $resultHeader[] = $restrictQueryToNSLink; 5894c924eb8SMichael Große } 5904c924eb8SMichael Große 5915d06a1e4SMichael Große $resultBody = []; 5929a75abfbSMichael Große $mtime = filemtime(wikiFN($id)); 5935d06a1e4SMichael Große $lastMod = '<span class="lastmod">' . $lang['lastmod'] . '</span> '; 59464159a61SAndreas Gohr $lastMod .= '<time datetime="' . date_iso8601($mtime) . '" title="' . dformat($mtime) . '">' . 59564159a61SAndreas Gohr dformat($mtime, '%f') . 59664159a61SAndreas Gohr '</time>'; 597f0861d1fSMichael Große $resultBody['meta'] = $lastMod; 5985d06a1e4SMichael Große if ($cnt !== 0) { 5998225e1abSMichael Große $num++; 600b12bcb77SAnika Henke $hits = '<span class="hits">' . $cnt . ' ' . $lang['hits'] . '</span>, '; 601f0861d1fSMichael Große $resultBody['meta'] = $hits . $resultBody['meta']; 6028225e1abSMichael Große if ($num <= FT_SNIPPET_NUMBER) { // create snippets for the first number of matches only 603*0cba610bSSatoshi Sahara $resultBody['snippet'] = FulltextSearch::snippet($id, $highlight); 6049a75abfbSMichael Große } 6059a75abfbSMichael Große } 6069a75abfbSMichael Große 6074eab6f7cSMichael Große $eventData = [ 6084c924eb8SMichael Große 'resultHeader' => $resultHeader, 6095d06a1e4SMichael Große 'resultBody' => $resultBody, 6104eab6f7cSMichael Große 'page' => $id, 61178d786c9SMichael Große 'position' => $position, 6124eab6f7cSMichael Große ]; 613cbb44eabSAndreas Gohr Event::createAndTrigger('SEARCH_RESULT_FULLPAGE', $eventData); 6145d06a1e4SMichael Große $html .= '<div class="search_fullpage_result">'; 6154eab6f7cSMichael Große $html .= '<dt>' . implode(' ', $eventData['resultHeader']) . '</dt>'; 6165d06a1e4SMichael Große foreach ($eventData['resultBody'] as $class => $htmlContent) { 6175d06a1e4SMichael Große $html .= "<dd class=\"$class\">$htmlContent</dd>"; 6185d06a1e4SMichael Große } 6195d06a1e4SMichael Große $html .= '</div>'; 62021fcef82SMichael Große } 62121fcef82SMichael Große $html .= '</dl>'; 62221fcef82SMichael Große 6232ce8affcSMichael Große $html .= '</div>'; 6242ce8affcSMichael Große 62521fcef82SMichael Große return $html; 62621fcef82SMichael Große } 6274c924eb8SMichael Große 6284c924eb8SMichael Große /** 6294c924eb8SMichael Große * create a link to restrict the current query to a namespace 6304c924eb8SMichael Große * 631ec27794fSMichael Große * @param false|string $ns the namespace to which to restrict the query 6324c924eb8SMichael Große * 633ec27794fSMichael Große * @return false|string 6344c924eb8SMichael Große */ 6354c924eb8SMichael Große protected function restrictQueryToNSLink($ns) 6364c924eb8SMichael Große { 6374c924eb8SMichael Große if (!$ns) { 6384c924eb8SMichael Große return false; 6394c924eb8SMichael Große } 640df977249SMichael Große if (!$this->isNamespaceAssistanceAvailable($this->parsedQuery)) { 6414c924eb8SMichael Große return false; 6424c924eb8SMichael Große } 6434c924eb8SMichael Große if (!empty($this->parsedQuery['ns']) && $this->parsedQuery['ns'][0] === $ns) { 6444c924eb8SMichael Große return false; 6454c924eb8SMichael Große } 64652d4cd42SMichael Große 6474c924eb8SMichael Große $name = '@' . $ns; 64852d4cd42SMichael Große return $this->searchState->withNamespace($ns)->getSearchLink($name); 6494c924eb8SMichael Große } 65021fcef82SMichael Große} 651