121fcef82SMichael Große<?php 221fcef82SMichael Große 321fcef82SMichael Großenamespace dokuwiki\Ui; 421fcef82SMichael Große 5427ed988SMichael Großeuse \dokuwiki\Form\Form; 6427ed988SMichael Große 721fcef82SMichael Großeclass Search extends Ui 821fcef82SMichael Große{ 921fcef82SMichael Große protected $query; 104c924eb8SMichael Große protected $parsedQuery; 1118856c5dSMichael Große protected $searchState; 1221fcef82SMichael Große protected $pageLookupResults = array(); 1321fcef82SMichael Große protected $fullTextResults = array(); 1421fcef82SMichael Große protected $highlight = array(); 1521fcef82SMichael Große 1621fcef82SMichael Große /** 1721fcef82SMichael Große * Search constructor. 186639a152SMichael Große * 196639a152SMichael Große * @param array $pageLookupResults 206639a152SMichael Große * @param array $fullTextResults 216639a152SMichael Große * @param string $highlight 2221fcef82SMichael Große */ 236639a152SMichael Große public function __construct(array $pageLookupResults, array $fullTextResults, $highlight) 2421fcef82SMichael Große { 25d09b5b64SMichael Große global $QUERY; 264c924eb8SMichael Große $Indexer = idx_get_indexer(); 27d09b5b64SMichael Große 28d09b5b64SMichael Große $this->query = $QUERY; 29bbc1da2eSMichael Große $this->parsedQuery = ft_queryParser($Indexer, $QUERY); 3018856c5dSMichael Große $this->searchState = new SearchState($this->parsedQuery); 3121fcef82SMichael Große 326639a152SMichael Große $this->pageLookupResults = $pageLookupResults; 336639a152SMichael Große $this->fullTextResults = $fullTextResults; 3421fcef82SMichael Große $this->highlight = $highlight; 35b3cfe85aSMichael Große } 36bbc1da2eSMichael Große 37b3cfe85aSMichael Große /** 3821fcef82SMichael Große * display the search result 3921fcef82SMichael Große * 4021fcef82SMichael Große * @return void 4121fcef82SMichael Große */ 4221fcef82SMichael Große public function show() 4321fcef82SMichael Große { 4421fcef82SMichael Große $searchHTML = ''; 4521fcef82SMichael Große 4621fcef82SMichael Große $searchHTML .= $this->getSearchIntroHTML($this->query); 4721fcef82SMichael Große 482ce8affcSMichael Große $searchHTML .= $this->getSearchFormHTML($this->query); 492ce8affcSMichael Große 5021fcef82SMichael Große $searchHTML .= $this->getPageLookupHTML($this->pageLookupResults); 5121fcef82SMichael Große 5221fcef82SMichael Große $searchHTML .= $this->getFulltextResultsHTML($this->fullTextResults, $this->highlight); 5321fcef82SMichael Große 5421fcef82SMichael Große echo $searchHTML; 5521fcef82SMichael Große } 5621fcef82SMichael Große 5721fcef82SMichael Große /** 58427ed988SMichael Große * Get a form which can be used to adjust/refine the search 59427ed988SMichael Große * 60427ed988SMichael Große * @param string $query 61427ed988SMichael Große * 62427ed988SMichael Große * @return string 63427ed988SMichael Große */ 64427ed988SMichael Große protected function getSearchFormHTML($query) 65427ed988SMichael Große { 66bbc1da2eSMichael Große global $lang, $ID, $INPUT; 67427ed988SMichael Große 68bb8ef867SMichael Große $searchForm = (new Form())->attrs(['method' => 'get'])->addClass('search-results-form'); 69bb8ef867SMichael Große $searchForm->setHiddenField('do', 'search'); 70d22b78c8SMichael Große $searchForm->setHiddenField('id', $ID); 711265b193SMichael Große $searchForm->setHiddenField('sf', '1'); 721265b193SMichael Große if ($INPUT->has('dta')) { 731265b193SMichael Große $searchForm->setHiddenField('dta', $INPUT->str('dta')); 74bbc1da2eSMichael Große } 751265b193SMichael Große if ($INPUT->has('dtb')) { 761265b193SMichael Große $searchForm->setHiddenField('dtb', $INPUT->str('dtb')); 77bbc1da2eSMichael Große } 781265b193SMichael Große if ($INPUT->has('srt')) { 791265b193SMichael Große $searchForm->setHiddenField('srt', $INPUT->str('srt')); 808d0e286aSMichael Große } 814bdf82b5SAndreas Gohr $searchForm->addFieldsetOpen()->addClass('search-form'); 82d22b78c8SMichael Große $searchForm->addTextInput('q')->val($query)->useInput(false); 83427ed988SMichael Große $searchForm->addButton('', $lang['btn_search'])->attr('type', 'submit'); 84bb8ef867SMichael Große 8518856c5dSMichael Große $this->addSearchAssistanceElements($searchForm); 86bb8ef867SMichael Große 87427ed988SMichael Große $searchForm->addFieldsetClose(); 88427ed988SMichael Große 8916ece95cSMichael Große trigger_event('FORM_SEARCH_OUTPUT', $searchForm); 9081a0edd9SMichael Große 91427ed988SMichael Große return $searchForm->toHTML(); 92427ed988SMichael Große } 93427ed988SMichael Große 94b005809cSMichael Große protected function addSortTool(Form $searchForm) 95b005809cSMichael Große { 96b005809cSMichael Große global $INPUT, $lang; 97b005809cSMichael Große 98b005809cSMichael Große $options = [ 99b005809cSMichael Große 'hits' => [ 100b005809cSMichael Große 'label' => $lang['search_sort_by_hits'], 101b005809cSMichael Große 'sort' => '', 102b005809cSMichael Große ], 103b005809cSMichael Große 'mtime' => [ 104b005809cSMichael Große 'label' => $lang['search_sort_by_mtime'], 105b005809cSMichael Große 'sort' => 'mtime', 106b005809cSMichael Große ], 107b005809cSMichael Große ]; 108b005809cSMichael Große $activeOption = 'hits'; 109b005809cSMichael Große 1101265b193SMichael Große if ($INPUT->str('srt') === 'mtime') { 111b005809cSMichael Große $activeOption = 'mtime'; 112b005809cSMichael Große } 113b005809cSMichael Große 1144bdf82b5SAndreas Gohr $searchForm->addTagOpen('div')->addClass('toggle'); 115b005809cSMichael Große // render current 1164bdf82b5SAndreas Gohr $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); 117b005809cSMichael Große if ($activeOption !== 'hits') { 1184bdf82b5SAndreas Gohr $currentWrapper->addClass('changed'); 119b005809cSMichael Große } 120b005809cSMichael Große $searchForm->addHTML($options[$activeOption]['label']); 121b005809cSMichael Große $searchForm->addTagClose('div'); 122b005809cSMichael Große 123b005809cSMichael Große // render options list 1244bdf82b5SAndreas Gohr $searchForm->addTagOpen('ul'); 125b005809cSMichael Große 126b005809cSMichael Große foreach ($options as $key => $option) { 1274bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 128b005809cSMichael Große 129b005809cSMichael Große if ($key === $activeOption) { 1304bdf82b5SAndreas Gohr $listItem->addClass('active'); 131b005809cSMichael Große $searchForm->addHTML($option['label']); 132b005809cSMichael Große } else { 13352d4cd42SMichael Große $link = $this->searchState->withSorting($option['sort'])->getSearchLink($option['label']); 13452d4cd42SMichael Große $searchForm->addHTML($link); 135b005809cSMichael Große } 136b005809cSMichael Große $searchForm->addTagClose('li'); 137b005809cSMichael Große } 138b005809cSMichael Große $searchForm->addTagClose('ul'); 139b005809cSMichael Große 140b005809cSMichael Große $searchForm->addTagClose('div'); 141b005809cSMichael Große 142b005809cSMichael Große } 143b005809cSMichael Große 144df977249SMichael Große protected function isNamespaceAssistanceAvailable(array $parsedQuery) { 145df977249SMichael Große if (preg_match('/[\(\)\|]/', $parsedQuery['query']) === 1) { 146bb8ef867SMichael Große return false; 147bb8ef867SMichael Große } 148df977249SMichael Große 149df977249SMichael Große return true; 150df977249SMichael Große } 151df977249SMichael Große 152df977249SMichael Große protected function isFragmentAssistanceAvailable(array $parsedQuery) { 153df977249SMichael Große if (preg_match('/[\(\)\|]/', $parsedQuery['query']) === 1) { 154bb8ef867SMichael Große return false; 155bb8ef867SMichael Große } 156bb8ef867SMichael Große 157bb8ef867SMichael Große if (!empty($parsedQuery['phrases'])) { 158bb8ef867SMichael Große return false; 159bb8ef867SMichael Große } 160bb8ef867SMichael Große 161bb8ef867SMichael Große return true; 162bb8ef867SMichael Große } 163bb8ef867SMichael Große 164bb8ef867SMichael Große /** 165bb8ef867SMichael Große * Add the elements to be used for search assistance 166bb8ef867SMichael Große * 167bb8ef867SMichael Große * @param Form $searchForm 168bb8ef867SMichael Große */ 16918856c5dSMichael Große protected function addSearchAssistanceElements(Form $searchForm) 170bb8ef867SMichael Große { 171*c5bd5721SMichael Große global $lang; 172*c5bd5721SMichael Große $searchForm->addButton('toggleAssistant', $lang['search_toggle_tools']) 173bb8ef867SMichael Große ->attr('type', 'button') 1744bdf82b5SAndreas Gohr ->addClass('toggleAssistant'); 175bb8ef867SMichael Große 176bb8ef867SMichael Große $searchForm->addTagOpen('div') 1774bdf82b5SAndreas Gohr ->addClass('advancedOptions') 178bb8ef867SMichael Große ->attr('style', 'display: none;'); 179bb8ef867SMichael Große 18018856c5dSMichael Große $this->addFragmentBehaviorLinks($searchForm); 18118856c5dSMichael Große $this->addNamespaceSelector($searchForm); 18218856c5dSMichael Große $this->addDateSelector($searchForm); 183b005809cSMichael Große $this->addSortTool($searchForm); 184bb8ef867SMichael Große 185bb8ef867SMichael Große $searchForm->addTagClose('div'); 186bb8ef867SMichael Große } 187bb8ef867SMichael Große 18818856c5dSMichael Große protected function addFragmentBehaviorLinks(Form $searchForm) 1894d0cb6e1SMichael Große { 190df977249SMichael Große if (!$this->isFragmentAssistanceAvailable($this->parsedQuery)) { 191df977249SMichael Große return; 192df977249SMichael Große } 193b005809cSMichael Große global $lang; 1944d0cb6e1SMichael Große 195b005809cSMichael Große $options = [ 196b005809cSMichael Große 'exact' => [ 197b005809cSMichael Große 'label' => $lang['search_exact_match'], 198b005809cSMichael Große 'and' => array_map(function ($term) { 199b005809cSMichael Große return trim($term, '*'); 200b005809cSMichael Große }, $this->parsedQuery['and']), 201df977249SMichael Große 'not' => array_map(function ($term) { 202df977249SMichael Große return trim($term, '*'); 203df977249SMichael Große }, $this->parsedQuery['not']), 204b005809cSMichael Große ], 205b005809cSMichael Große 'starts' => [ 206b005809cSMichael Große 'label' => $lang['search_starts_with'], 207b005809cSMichael Große 'and' => array_map(function ($term) { 208b005809cSMichael Große return trim($term, '*') . '*'; 209df977249SMichael Große }, $this->parsedQuery['and']), 210df977249SMichael Große 'not' => array_map(function ($term) { 211df977249SMichael Große return trim($term, '*') . '*'; 212df977249SMichael Große }, $this->parsedQuery['not']), 213b005809cSMichael Große ], 214b005809cSMichael Große 'ends' => [ 215b005809cSMichael Große 'label' => $lang['search_ends_with'], 216b005809cSMichael Große 'and' => array_map(function ($term) { 217b005809cSMichael Große return '*' . trim($term, '*'); 218df977249SMichael Große }, $this->parsedQuery['and']), 219df977249SMichael Große 'not' => array_map(function ($term) { 220df977249SMichael Große return '*' . trim($term, '*'); 221df977249SMichael Große }, $this->parsedQuery['not']), 222b005809cSMichael Große ], 223b005809cSMichael Große 'contains' => [ 224b005809cSMichael Große 'label' => $lang['search_contains'], 225b005809cSMichael Große 'and' => array_map(function ($term) { 226b005809cSMichael Große return '*' . trim($term, '*') . '*'; 227df977249SMichael Große }, $this->parsedQuery['and']), 228df977249SMichael Große 'not' => array_map(function ($term) { 229df977249SMichael Große return '*' . trim($term, '*') . '*'; 230df977249SMichael Große }, $this->parsedQuery['not']), 231b005809cSMichael Große ] 232b005809cSMichael Große ]; 233b005809cSMichael Große 234b005809cSMichael Große // detect current 235c6b5b74aSMichael Große $activeOption = 'custom'; 236b005809cSMichael Große foreach ($options as $key => $option) { 237b005809cSMichael Große if ($this->parsedQuery['and'] === $option['and']) { 238b005809cSMichael Große $activeOption = $key; 239b005809cSMichael Große } 240b005809cSMichael Große } 241c6b5b74aSMichael Große if ($activeOption === 'custom') { 242c6b5b74aSMichael Große $options = array_merge(['custom' => [ 243c6b5b74aSMichael Große 'label' => $lang['search_custom_match'], 244c6b5b74aSMichael Große ]], $options); 245c6b5b74aSMichael Große } 246b005809cSMichael Große 2474bdf82b5SAndreas Gohr $searchForm->addTagOpen('div')->addClass('toggle'); 248b005809cSMichael Große // render current 2494bdf82b5SAndreas Gohr $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); 250b005809cSMichael Große if ($activeOption !== 'exact') { 2514bdf82b5SAndreas Gohr $currentWrapper->addClass('changed'); 252b005809cSMichael Große } 253b005809cSMichael Große $searchForm->addHTML($options[$activeOption]['label']); 254b005809cSMichael Große $searchForm->addTagClose('div'); 255b005809cSMichael Große 256b005809cSMichael Große // render options list 2574bdf82b5SAndreas Gohr $searchForm->addTagOpen('ul'); 258b005809cSMichael Große 259b005809cSMichael Große foreach ($options as $key => $option) { 2604bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 261b005809cSMichael Große 262b005809cSMichael Große if ($key === $activeOption) { 2634bdf82b5SAndreas Gohr $listItem->addClass('active'); 264b005809cSMichael Große $searchForm->addHTML($option['label']); 265b005809cSMichael Große } else { 26652d4cd42SMichael Große $link = $this->searchState 26752d4cd42SMichael Große ->withFragments($option['and'], $option['not']) 26852d4cd42SMichael Große ->getSearchLink($option['label']) 26952d4cd42SMichael Große ; 27052d4cd42SMichael Große $searchForm->addHTML($link); 271b005809cSMichael Große } 272b005809cSMichael Große $searchForm->addTagClose('li'); 273b005809cSMichael Große } 274b005809cSMichael Große $searchForm->addTagClose('ul'); 2754d0cb6e1SMichael Große 2764d0cb6e1SMichael Große $searchForm->addTagClose('div'); 277b005809cSMichael Große 278b005809cSMichael Große // render options list 2794d0cb6e1SMichael Große } 2804d0cb6e1SMichael Große 281bb8ef867SMichael Große /** 282bb8ef867SMichael Große * Add the elements for the namespace selector 283bb8ef867SMichael Große * 284bb8ef867SMichael Große * @param Form $searchForm 285bb8ef867SMichael Große */ 28618856c5dSMichael Große protected function addNamespaceSelector(Form $searchForm) 287bb8ef867SMichael Große { 288df977249SMichael Große if (!$this->isNamespaceAssistanceAvailable($this->parsedQuery)) { 289df977249SMichael Große return; 290df977249SMichael Große } 291df977249SMichael Große 292b005809cSMichael Große global $lang; 293b005809cSMichael Große 29418856c5dSMichael Große $baseNS = empty($this->parsedQuery['ns']) ? '' : $this->parsedQuery['ns'][0]; 295bbc1da2eSMichael Große $extraNS = $this->getAdditionalNamespacesFromResults($baseNS); 2964d0cb6e1SMichael Große 2974bdf82b5SAndreas Gohr $searchForm->addTagOpen('div')->addClass('toggle'); 298b005809cSMichael Große // render current 2994bdf82b5SAndreas Gohr $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); 300bbc1da2eSMichael Große if ($baseNS) { 3014bdf82b5SAndreas Gohr $currentWrapper->addClass('changed'); 302b005809cSMichael Große $searchForm->addHTML('@' . $baseNS); 303b005809cSMichael Große } else { 304b005809cSMichael Große $searchForm->addHTML($lang['search_any_ns']); 305b005809cSMichael Große } 306b005809cSMichael Große $searchForm->addTagClose('div'); 307b005809cSMichael Große 308b005809cSMichael Große // render options list 3094bdf82b5SAndreas Gohr $searchForm->addTagOpen('ul'); 310b005809cSMichael Große 3114bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 312b005809cSMichael Große if ($baseNS) { 3134bdf82b5SAndreas Gohr $listItem->addClass('active'); 31452d4cd42SMichael Große $link = $this->searchState->withNamespace('')->getSearchLink($lang['search_any_ns']); 31552d4cd42SMichael Große $searchForm->addHTML($link); 316b005809cSMichael Große } else { 317b005809cSMichael Große $searchForm->addHTML($lang['search_any_ns']); 318bb8ef867SMichael Große } 319b005809cSMichael Große $searchForm->addTagClose('li'); 320bb8ef867SMichael Große 32118856c5dSMichael Große foreach ($extraNS as $ns => $count) { 3224bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 32318856c5dSMichael Große $label = $ns . ($count ? " ($count)" : ''); 3244d0cb6e1SMichael Große 325b005809cSMichael Große if ($ns === $baseNS) { 3264bdf82b5SAndreas Gohr $listItem->addClass('active'); 327b005809cSMichael Große $searchForm->addHTML($label); 328b005809cSMichael Große } else { 32952d4cd42SMichael Große $link = $this->searchState->withNamespace($ns)->getSearchLink($label); 33052d4cd42SMichael Große $searchForm->addHTML($link); 331bb8ef867SMichael Große } 332b005809cSMichael Große $searchForm->addTagClose('li'); 333bb8ef867SMichael Große } 334b005809cSMichael Große $searchForm->addTagClose('ul'); 335bb8ef867SMichael Große 336bb8ef867SMichael Große $searchForm->addTagClose('div'); 337b005809cSMichael Große 338bb8ef867SMichael Große } 339bb8ef867SMichael Große 340bb8ef867SMichael Große /** 341bb8ef867SMichael Große * Parse the full text results for their top namespaces below the given base namespace 342bb8ef867SMichael Große * 343bb8ef867SMichael Große * @param string $baseNS the namespace within which was searched, empty string for root namespace 344bb8ef867SMichael Große * 345bb8ef867SMichael Große * @return array an associative array with namespace => #number of found pages, sorted descending 346bb8ef867SMichael Große */ 347bb8ef867SMichael Große protected function getAdditionalNamespacesFromResults($baseNS) 348bb8ef867SMichael Große { 349bb8ef867SMichael Große $namespaces = []; 350bb8ef867SMichael Große $baseNSLength = strlen($baseNS); 351bb8ef867SMichael Große foreach ($this->fullTextResults as $page => $numberOfHits) { 352bb8ef867SMichael Große $namespace = getNS($page); 353bb8ef867SMichael Große if (!$namespace) { 354bb8ef867SMichael Große continue; 355bb8ef867SMichael Große } 356bb8ef867SMichael Große if ($namespace === $baseNS) { 357bb8ef867SMichael Große continue; 358bb8ef867SMichael Große } 359bb8ef867SMichael Große $firstColon = strpos((string)$namespace, ':', $baseNSLength + 1) ?: strlen($namespace); 360bb8ef867SMichael Große $subtopNS = substr($namespace, 0, $firstColon); 361bb8ef867SMichael Große if (empty($namespaces[$subtopNS])) { 362bb8ef867SMichael Große $namespaces[$subtopNS] = 0; 363bb8ef867SMichael Große } 364bb8ef867SMichael Große $namespaces[$subtopNS] += 1; 365bb8ef867SMichael Große } 36655dc8783SMichael Große ksort($namespaces); 367bb8ef867SMichael Große arsort($namespaces); 368bb8ef867SMichael Große return $namespaces; 369bb8ef867SMichael Große } 370bb8ef867SMichael Große 371bb8ef867SMichael Große /** 372bbc1da2eSMichael Große * @ToDo: custom date input 373bbc1da2eSMichael Große * 374bbc1da2eSMichael Große * @param Form $searchForm 375bbc1da2eSMichael Große */ 376b005809cSMichael Große protected function addDateSelector(Form $searchForm) 377b005809cSMichael Große { 378b005809cSMichael Große global $INPUT, $lang; 379bbc1da2eSMichael Große 380b005809cSMichael Große $options = [ 381b005809cSMichael Große 'any' => [ 382b005809cSMichael Große 'before' => false, 383b005809cSMichael Große 'after' => false, 384b005809cSMichael Große 'label' => $lang['search_any_time'], 385b005809cSMichael Große ], 386b005809cSMichael Große 'week' => [ 387b005809cSMichael Große 'before' => false, 388b005809cSMichael Große 'after' => '1 week ago', 389b005809cSMichael Große 'label' => $lang['search_past_7_days'], 390b005809cSMichael Große ], 391b005809cSMichael Große 'month' => [ 392b005809cSMichael Große 'before' => false, 393b005809cSMichael Große 'after' => '1 month ago', 394b005809cSMichael Große 'label' => $lang['search_past_month'], 395b005809cSMichael Große ], 396b005809cSMichael Große 'year' => [ 397b005809cSMichael Große 'before' => false, 398b005809cSMichael Große 'after' => '1 year ago', 399b005809cSMichael Große 'label' => $lang['search_past_year'], 400b005809cSMichael Große ], 401b005809cSMichael Große ]; 402b005809cSMichael Große $activeOption = 'any'; 403b005809cSMichael Große foreach ($options as $key => $option) { 4041265b193SMichael Große if ($INPUT->str('dta') === $option['after']) { 405b005809cSMichael Große $activeOption = $key; 406b005809cSMichael Große break; 407b005809cSMichael Große } 408b005809cSMichael Große } 409b005809cSMichael Große 4104bdf82b5SAndreas Gohr $searchForm->addTagOpen('div')->addClass('toggle'); 411b005809cSMichael Große // render current 4124bdf82b5SAndreas Gohr $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); 4131265b193SMichael Große if ($INPUT->has('dtb') || $INPUT->has('dta')) { 4144bdf82b5SAndreas Gohr $currentWrapper->addClass('changed'); 415bbc1da2eSMichael Große } 416b005809cSMichael Große $searchForm->addHTML($options[$activeOption]['label']); 417b005809cSMichael Große $searchForm->addTagClose('div'); 418bbc1da2eSMichael Große 419b005809cSMichael Große // render options list 4204bdf82b5SAndreas Gohr $searchForm->addTagOpen('ul'); 421b005809cSMichael Große 422b005809cSMichael Große foreach ($options as $key => $option) { 4234bdf82b5SAndreas Gohr $listItem = $searchForm->addTagOpen('li'); 424b005809cSMichael Große 425b005809cSMichael Große if ($key === $activeOption) { 4264bdf82b5SAndreas Gohr $listItem->addClass('active'); 427b005809cSMichael Große $searchForm->addHTML($option['label']); 428bbc1da2eSMichael Große } else { 42952d4cd42SMichael Große $link = $this->searchState 43052d4cd42SMichael Große ->withTimeLimitations($option['after'], $option['before']) 43152d4cd42SMichael Große ->getSearchLink($option['label']) 43252d4cd42SMichael Große ; 43352d4cd42SMichael Große $searchForm->addHTML($link); 434bbc1da2eSMichael Große } 435b005809cSMichael Große $searchForm->addTagClose('li'); 436bbc1da2eSMichael Große } 437b005809cSMichael Große $searchForm->addTagClose('ul'); 438bbc1da2eSMichael Große 439bbc1da2eSMichael Große $searchForm->addTagClose('div'); 440bbc1da2eSMichael Große } 441bbc1da2eSMichael Große 442bbc1da2eSMichael Große 443bbc1da2eSMichael Große /** 44421fcef82SMichael Große * Build the intro text for the search page 44521fcef82SMichael Große * 44621fcef82SMichael Große * @param string $query the search query 44721fcef82SMichael Große * 44821fcef82SMichael Große * @return string 44921fcef82SMichael Große */ 45021fcef82SMichael Große protected function getSearchIntroHTML($query) 45121fcef82SMichael Große { 4522ce8affcSMichael Große global $lang; 45321fcef82SMichael Große 45421fcef82SMichael Große $intro = p_locale_xhtml('searchpage'); 4552ce8affcSMichael Große 4562ce8affcSMichael Große $queryPagename = $this->createPagenameFromQuery($this->parsedQuery); 4572ce8affcSMichael Große $createQueryPageLink = html_wikilink($queryPagename . '?do=edit', $queryPagename); 4582ce8affcSMichael Große 4592ce8affcSMichael Große $pagecreateinfo = ''; 4602ce8affcSMichael Große if (auth_quickaclcheck($queryPagename) >= AUTH_CREATE) { 4612ce8affcSMichael Große $pagecreateinfo = sprintf($lang['searchcreatepage'], $createQueryPageLink); 4622ce8affcSMichael Große } 46321fcef82SMichael Große $intro = str_replace( 46421fcef82SMichael Große array('@QUERY@', '@SEARCH@', '@CREATEPAGEINFO@'), 46521fcef82SMichael Große array(hsc(rawurlencode($query)), hsc($query), $pagecreateinfo), 46621fcef82SMichael Große $intro 46721fcef82SMichael Große ); 4682ce8affcSMichael Große 46921fcef82SMichael Große return $intro; 47021fcef82SMichael Große } 47121fcef82SMichael Große 47221fcef82SMichael Große /** 4732ce8affcSMichael Große * Create a pagename based the parsed search query 4742ce8affcSMichael Große * 4752ce8affcSMichael Große * @param array $parsedQuery 4762ce8affcSMichael Große * 4772ce8affcSMichael Große * @return string pagename constructed from the parsed query 4782ce8affcSMichael Große */ 4792ce8affcSMichael Große protected function createPagenameFromQuery($parsedQuery) 4802ce8affcSMichael Große { 4812ce8affcSMichael Große $pagename = ''; 4822ce8affcSMichael Große if (!empty($parsedQuery['ns'])) { 4832ce8affcSMichael Große $pagename .= cleanID($parsedQuery['ns'][0]); 4842ce8affcSMichael Große } 4852ce8affcSMichael Große $pagename .= ':' . cleanID(implode(' ' , $parsedQuery['highlight'])); 4862ce8affcSMichael Große return $pagename; 4872ce8affcSMichael Große } 4882ce8affcSMichael Große 4892ce8affcSMichael Große /** 49021fcef82SMichael Große * Build HTML for a list of pages with matching pagenames 49121fcef82SMichael Große * 49221fcef82SMichael Große * @param array $data search results 49321fcef82SMichael Große * 49421fcef82SMichael Große * @return string 49521fcef82SMichael Große */ 49621fcef82SMichael Große protected function getPageLookupHTML($data) 49721fcef82SMichael Große { 49821fcef82SMichael Große if (empty($data)) { 49921fcef82SMichael Große return ''; 50021fcef82SMichael Große } 50121fcef82SMichael Große 50221fcef82SMichael Große global $lang; 50321fcef82SMichael Große 50421fcef82SMichael Große $html = '<div class="search_quickresult">'; 50521fcef82SMichael Große $html .= '<h3>' . $lang['quickhits'] . ':</h3>'; 50621fcef82SMichael Große $html .= '<ul class="search_quickhits">'; 50721fcef82SMichael Große foreach ($data as $id => $title) { 5084eab6f7cSMichael Große $link = html_wikilink(':' . $id); 5094eab6f7cSMichael Große $eventData = [ 5104eab6f7cSMichael Große 'listItemContent' => [$link], 5114eab6f7cSMichael Große 'page' => $id, 5124eab6f7cSMichael Große ]; 5134eab6f7cSMichael Große trigger_event('SEARCH_RESULT_PAGELOOKUP', $eventData); 5144eab6f7cSMichael Große $html .= '<li>' . implode('', $eventData['listItemContent']) . '</li>'; 51521fcef82SMichael Große } 51621fcef82SMichael Große $html .= '</ul> '; 51721fcef82SMichael Große //clear float (see http://www.complexspiral.com/publications/containing-floats/) 51821fcef82SMichael Große $html .= '<div class="clearer"></div>'; 51921fcef82SMichael Große $html .= '</div>'; 52021fcef82SMichael Große 52121fcef82SMichael Große return $html; 52221fcef82SMichael Große } 52321fcef82SMichael Große 52421fcef82SMichael Große /** 52521fcef82SMichael Große * Build HTML for fulltext search results or "no results" message 52621fcef82SMichael Große * 52721fcef82SMichael Große * @param array $data the results of the fulltext search 52821fcef82SMichael Große * @param array $highlight the terms to be highlighted in the results 52921fcef82SMichael Große * 53021fcef82SMichael Große * @return string 53121fcef82SMichael Große */ 53221fcef82SMichael Große protected function getFulltextResultsHTML($data, $highlight) 53321fcef82SMichael Große { 53421fcef82SMichael Große global $lang; 53521fcef82SMichael Große 53621fcef82SMichael Große if (empty($data)) { 53721fcef82SMichael Große return '<div class="nothing">' . $lang['nothingfound'] . '</div>'; 53821fcef82SMichael Große } 53921fcef82SMichael Große 5402ce8affcSMichael Große $html = '<div class="search_fulltextresult">'; 5412ce8affcSMichael Große $html .= '<h3>' . $lang['search_fullresults'] . ':</h3>'; 5422ce8affcSMichael Große 54321fcef82SMichael Große $html .= '<dl class="search_results">'; 54421fcef82SMichael Große $num = 1; 5454c924eb8SMichael Große 54621fcef82SMichael Große foreach ($data as $id => $cnt) { 5474eab6f7cSMichael Große $resultLink = html_wikilink(':' . $id, null, $highlight); 5484c924eb8SMichael Große 5494c924eb8SMichael Große $resultHeader = [$resultLink]; 5504c924eb8SMichael Große 5514eab6f7cSMichael Große 5524c924eb8SMichael Große $restrictQueryToNSLink = $this->restrictQueryToNSLink(getNS($id)); 5534c924eb8SMichael Große if ($restrictQueryToNSLink) { 5544c924eb8SMichael Große $resultHeader[] = $restrictQueryToNSLink; 5554c924eb8SMichael Große } 5564c924eb8SMichael Große 5579a75abfbSMichael Große $snippet = ''; 5589a75abfbSMichael Große $lastMod = ''; 5599a75abfbSMichael Große $mtime = filemtime(wikiFN($id)); 5609a75abfbSMichael Große if ($cnt !== 0) { 5619a75abfbSMichael Große $resultHeader[] = $cnt . ' ' . $lang['hits']; 5629a75abfbSMichael Große if ($num < FT_SNIPPET_NUMBER) { // create snippets for the first number of matches only 5639a75abfbSMichael Große $snippet = '<dd>' . ft_snippet($id, $highlight) . '</dd>'; 5649a75abfbSMichael Große $lastMod = '<span class="search_results__lastmod">' . $lang['lastmod'] . ' '; 5659a75abfbSMichael Große $lastMod .= '<time datetime="' . date_iso8601($mtime) . '">' . dformat($mtime) . '</time>'; 5669a75abfbSMichael Große $lastMod .= '</span>'; 5679a75abfbSMichael Große } 5689a75abfbSMichael Große $num++; 5699a75abfbSMichael Große } 5709a75abfbSMichael Große 5719a75abfbSMichael Große $metaLine = '<div class="search_results__metaLine">'; 5729a75abfbSMichael Große $metaLine .= $lastMod; 5739a75abfbSMichael Große $metaLine .= '</div>'; 5749a75abfbSMichael Große 5759a75abfbSMichael Große 5764eab6f7cSMichael Große $eventData = [ 5774c924eb8SMichael Große 'resultHeader' => $resultHeader, 5789a75abfbSMichael Große 'resultBody' => [$metaLine, $snippet], 5794eab6f7cSMichael Große 'page' => $id, 5804eab6f7cSMichael Große ]; 5814eab6f7cSMichael Große trigger_event('SEARCH_RESULT_FULLPAGE', $eventData); 5824eab6f7cSMichael Große $html .= '<div class="search_fullpage_result">'; 5834eab6f7cSMichael Große $html .= '<dt>' . implode(' ', $eventData['resultHeader']) . '</dt>'; 5844eab6f7cSMichael Große $html .= implode('', $eventData['resultBody']); 5854eab6f7cSMichael Große $html .= '</div>'; 58621fcef82SMichael Große } 58721fcef82SMichael Große $html .= '</dl>'; 58821fcef82SMichael Große 5892ce8affcSMichael Große $html .= '</div>'; 5902ce8affcSMichael Große 59121fcef82SMichael Große return $html; 59221fcef82SMichael Große } 5934c924eb8SMichael Große 5944c924eb8SMichael Große /** 5954c924eb8SMichael Große * create a link to restrict the current query to a namespace 5964c924eb8SMichael Große * 5974c924eb8SMichael Große * @param bool|string $ns the namespace to which to restrict the query 5984c924eb8SMichael Große * 5994c924eb8SMichael Große * @return bool|string 6004c924eb8SMichael Große */ 6014c924eb8SMichael Große protected function restrictQueryToNSLink($ns) 6024c924eb8SMichael Große { 6034c924eb8SMichael Große if (!$ns) { 6044c924eb8SMichael Große return false; 6054c924eb8SMichael Große } 606df977249SMichael Große if (!$this->isNamespaceAssistanceAvailable($this->parsedQuery)) { 6074c924eb8SMichael Große return false; 6084c924eb8SMichael Große } 6094c924eb8SMichael Große if (!empty($this->parsedQuery['ns']) && $this->parsedQuery['ns'][0] === $ns) { 6104c924eb8SMichael Große return false; 6114c924eb8SMichael Große } 61252d4cd42SMichael Große 6134c924eb8SMichael Große $name = '@' . $ns; 61452d4cd42SMichael Große return $this->searchState->withNamespace($ns)->getSearchLink($name); 6154c924eb8SMichael Große } 61621fcef82SMichael Große} 617