1<?php 2 3namespace dokuwiki\Ui; 4 5use \dokuwiki\Form\Form; 6 7class Search extends Ui 8{ 9 protected $query; 10 protected $parsedQuery; 11 protected $searchState; 12 protected $pageLookupResults = array(); 13 protected $fullTextResults = array(); 14 protected $highlight = array(); 15 16 /** 17 * Search constructor. 18 */ 19 public function __construct() 20 { 21 global $QUERY; 22 $Indexer = idx_get_indexer(); 23 24 $this->query = $QUERY; 25 $this->parsedQuery = ft_queryParser($Indexer, $QUERY); 26 $this->searchState = new SearchState($this->parsedQuery); 27 } 28 29 /** 30 * run the search 31 */ 32 public function execute() 33 { 34 $this->pageLookupResults = ft_pageLookup($this->query, true, useHeading('navigation')); 35 $this->fullTextResults = ft_pageSearch($this->query, $highlight); 36 $this->highlight = $highlight; 37 } 38 39 /** 40 * display the search result 41 * 42 * @return void 43 */ 44 public function show() 45 { 46 $searchHTML = ''; 47 48 $searchHTML .= $this->getSearchFormHTML($this->query); 49 50 $searchHTML .= $this->getSearchIntroHTML($this->query); 51 52 $searchHTML .= $this->getPageLookupHTML($this->pageLookupResults); 53 54 $searchHTML .= $this->getFulltextResultsHTML($this->fullTextResults, $this->highlight); 55 56 echo $searchHTML; 57 } 58 59 /** 60 * Get a form which can be used to adjust/refine the search 61 * 62 * @param string $query 63 * 64 * @return string 65 */ 66 protected function getSearchFormHTML($query) 67 { 68 global $lang, $ID, $INPUT; 69 70 $searchForm = (new Form())->attrs(['method' => 'get'])->addClass('search-results-form'); 71 $searchForm->setHiddenField('do', 'search'); 72 $searchForm->setHiddenField('id', $ID); 73 $searchForm->setHiddenField('searchPageForm', '1'); 74 if ($INPUT->has('after')) { 75 $searchForm->setHiddenField('after', $INPUT->str('after')); 76 } 77 if ($INPUT->has('before')) { 78 $searchForm->setHiddenField('before', $INPUT->str('before')); 79 } 80 $searchForm->addFieldsetOpen()->addClass('search-results-form__fieldset'); 81 $searchForm->addTextInput('q')->val($query)->useInput(false); 82 $searchForm->addButton('', $lang['btn_search'])->attr('type', 'submit'); 83 84 if ($this->isSearchAssistanceAvailable($this->parsedQuery)) { 85 $this->addSearchAssistanceElements($searchForm); 86 } else { 87 $searchForm->addClass('search-results-form--no-assistance'); 88 $searchForm->addTagOpen('span')->addClass('search-results-form__no-assistance-message'); 89 $searchForm->addHTML('FIXME Your query is too complex. Search assistance is unavailable. See <a href="https://doku.wiki/search">doku.wiki/search</a> for more help.'); 90 $searchForm->addTagClose('span'); 91 } 92 93 $searchForm->addFieldsetClose(); 94 95 trigger_event('SEARCH_FORM_DISPLAY', $searchForm); 96 97 return $searchForm->toHTML(); 98 } 99 100 /** 101 * Decide if the given query is simple enough to provide search assistance 102 * 103 * @param array $parsedQuery 104 * 105 * @return bool 106 */ 107 protected function isSearchAssistanceAvailable(array $parsedQuery) 108 { 109 if (count($parsedQuery['words']) > 1) { 110 return false; 111 } 112 if (!empty($parsedQuery['not'])) { 113 return false; 114 } 115 116 if (!empty($parsedQuery['phrases'])) { 117 return false; 118 } 119 120 if (!empty($parsedQuery['notns'])) { 121 return false; 122 } 123 if (count($parsedQuery['ns']) > 1) { 124 return false; 125 } 126 127 return true; 128 } 129 130 /** 131 * Add the elements to be used for search assistance 132 * 133 * @param Form $searchForm 134 */ 135 protected function addSearchAssistanceElements(Form $searchForm) 136 { 137 $searchForm->addButton('toggleAssistant', 'toggle search assistant') 138 ->attr('type', 'button') 139 ->id('search-results-form__show-assistance-button') 140 ->addClass('search-results-form__show-assistance-button'); 141 142 $searchForm->addTagOpen('div') 143 ->addClass('js-advancedSearchOptions') 144 ->attr('style', 'display: none;'); 145 146 $this->addFragmentBehaviorLinks($searchForm); 147 $this->addNamespaceSelector($searchForm); 148 $this->addDateSelector($searchForm); 149 150 $searchForm->addTagClose('div'); 151 } 152 153 protected function addFragmentBehaviorLinks(Form $searchForm) 154 { 155 $searchForm->addTagOpen('div')->addClass('search-results-form__subwrapper'); 156 $searchForm->addHTML('fragment behavior: '); 157 158 $this->searchState->addSearchLinkFragment( 159 $searchForm, 160 'exact match', 161 array_map(function($term){return trim($term, '*');},$this->parsedQuery['and']) 162 ); 163 164 $searchForm->addHTML(', '); 165 166 $this->searchState->addSearchLinkFragment( 167 $searchForm, 168 'starts with', 169 array_map(function($term){return trim($term, '*') . '*';},$this->parsedQuery['and']) 170 ); 171 172 $searchForm->addHTML(', '); 173 174 $this->searchState->addSearchLinkFragment( 175 $searchForm, 176 'ends with', 177 array_map(function($term){return '*' . trim($term, '*');},$this->parsedQuery['and']) 178 ); 179 180 $searchForm->addHTML(', '); 181 182 $this->searchState->addSearchLinkFragment( 183 $searchForm, 184 'contains', 185 array_map(function($term){return '*' . trim($term, '*') . '*';},$this->parsedQuery['and']) 186 ); 187 188 $searchForm->addTagClose('div'); 189 } 190 191 /** 192 * Add the elements for the namespace selector 193 * 194 * @param Form $searchForm 195 */ 196 protected function addNamespaceSelector(Form $searchForm) 197 { 198 $baseNS = empty($this->parsedQuery['ns']) ? '' : $this->parsedQuery['ns'][0]; 199 $searchForm->addTagOpen('div')->addClass('search-results-form__subwrapper'); 200 201 $extraNS = $this->getAdditionalNamespacesFromResults($baseNS); 202 if (!empty($extraNS) || $baseNS) { 203 $searchForm->addTagOpen('div'); 204 $searchForm->addHTML('limit to namespace: '); 205 206 if ($baseNS) { 207 $this->searchState->addSeachLinkNS( 208 $searchForm, 209 '(remove limit)', 210 '' 211 ); 212 } 213 214 foreach ($extraNS as $ns => $count) { 215 $searchForm->addHTML(' '); 216 $label = $ns . ($count ? " ($count)" : ''); 217 218 $this->searchState->addSeachLinkNS($searchForm, $label, $ns); 219 } 220 $searchForm->addTagClose('div'); 221 } 222 223 $searchForm->addTagClose('div'); 224 } 225 226 /** 227 * Parse the full text results for their top namespaces below the given base namespace 228 * 229 * @param string $baseNS the namespace within which was searched, empty string for root namespace 230 * 231 * @return array an associative array with namespace => #number of found pages, sorted descending 232 */ 233 protected function getAdditionalNamespacesFromResults($baseNS) 234 { 235 $namespaces = []; 236 $baseNSLength = strlen($baseNS); 237 foreach ($this->fullTextResults as $page => $numberOfHits) { 238 $namespace = getNS($page); 239 if (!$namespace) { 240 continue; 241 } 242 if ($namespace === $baseNS) { 243 continue; 244 } 245 $firstColon = strpos((string)$namespace, ':', $baseNSLength + 1) ?: strlen($namespace); 246 $subtopNS = substr($namespace, 0, $firstColon); 247 if (empty($namespaces[$subtopNS])) { 248 $namespaces[$subtopNS] = 0; 249 } 250 $namespaces[$subtopNS] += 1; 251 } 252 arsort($namespaces); 253 return $namespaces; 254 } 255 256 /** 257 * @ToDo: custom date input 258 * 259 * @param Form $searchForm 260 */ 261 protected function addDateSelector(Form $searchForm) { 262 $searchForm->addTagOpen('div')->addClass('search-results-form__subwrapper'); 263 $searchForm->addHTML('limit by date: '); 264 265 global $INPUT; 266 if ($INPUT->has('before') || $INPUT->has('after')) { 267 $this->searchState->addSearchLinkTime( 268 $searchForm, 269 '(remove limit)', 270 false, 271 false 272 ); 273 274 $searchForm->addHTML(', '); 275 } 276 277 if ($INPUT->str('after') === '1 week ago') { 278 $searchForm->addHTML('<span class="active">past 7 days</span>'); 279 } else { 280 $this->searchState->addSearchLinkTime( 281 $searchForm, 282 'past 7 days', 283 '1 week ago', 284 false 285 ); 286 } 287 288 $searchForm->addHTML(', '); 289 290 if ($INPUT->str('after') === '1 month ago') { 291 $searchForm->addHTML('<span class="active">past month</span>'); 292 } else { 293 $this->searchState->addSearchLinkTime( 294 $searchForm, 295 'past month', 296 '1 month ago', 297 false 298 ); 299 } 300 301 $searchForm->addHTML(', '); 302 303 if ($INPUT->str('after') === '1 year ago') { 304 $searchForm->addHTML('<span class="active">past year</span>'); 305 } else { 306 $this->searchState->addSearchLinkTime( 307 $searchForm, 308 'past year', 309 '1 year ago', 310 false 311 ); 312 } 313 314 $searchForm->addTagClose('div'); 315 } 316 317 318 /** 319 * Build the intro text for the search page 320 * 321 * @param string $query the search query 322 * 323 * @return string 324 */ 325 protected function getSearchIntroHTML($query) 326 { 327 global $ID, $lang; 328 329 $intro = p_locale_xhtml('searchpage'); 330 // allow use of placeholder in search intro 331 $pagecreateinfo = (auth_quickaclcheck($ID) >= AUTH_CREATE) ? $lang['searchcreatepage'] : ''; 332 $intro = str_replace( 333 array('@QUERY@', '@SEARCH@', '@CREATEPAGEINFO@'), 334 array(hsc(rawurlencode($query)), hsc($query), $pagecreateinfo), 335 $intro 336 ); 337 return $intro; 338 } 339 340 /** 341 * Build HTML for a list of pages with matching pagenames 342 * 343 * @param array $data search results 344 * 345 * @return string 346 */ 347 protected function getPageLookupHTML($data) 348 { 349 if (empty($data)) { 350 return ''; 351 } 352 353 global $lang; 354 355 $html = '<div class="search_quickresult">'; 356 $html .= '<h3>' . $lang['quickhits'] . ':</h3>'; 357 $html .= '<ul class="search_quickhits">'; 358 foreach ($data as $id => $title) { 359 $link = html_wikilink(':' . $id); 360 $eventData = [ 361 'listItemContent' => [$link], 362 'page' => $id, 363 ]; 364 trigger_event('SEARCH_RESULT_PAGELOOKUP', $eventData); 365 $html .= '<li>' . implode('', $eventData['listItemContent']) . '</li>'; 366 } 367 $html .= '</ul> '; 368 //clear float (see http://www.complexspiral.com/publications/containing-floats/) 369 $html .= '<div class="clearer"></div>'; 370 $html .= '</div>'; 371 372 return $html; 373 } 374 375 /** 376 * Build HTML for fulltext search results or "no results" message 377 * 378 * @param array $data the results of the fulltext search 379 * @param array $highlight the terms to be highlighted in the results 380 * 381 * @return string 382 */ 383 protected function getFulltextResultsHTML($data, $highlight) 384 { 385 global $lang; 386 387 if (empty($data)) { 388 return '<div class="nothing">' . $lang['nothingfound'] . '</div>'; 389 } 390 391 $html = ''; 392 $html .= '<dl class="search_results">'; 393 $num = 1; 394 395 foreach ($data as $id => $cnt) { 396 $resultLink = html_wikilink(':' . $id, null, $highlight); 397 398 $resultHeader = [$resultLink]; 399 400 401 $restrictQueryToNSLink = $this->restrictQueryToNSLink(getNS($id)); 402 if ($restrictQueryToNSLink) { 403 $resultHeader[] = $restrictQueryToNSLink; 404 } 405 406 $snippet = ''; 407 $lastMod = ''; 408 $mtime = filemtime(wikiFN($id)); 409 if ($cnt !== 0) { 410 $resultHeader[] = $cnt . ' ' . $lang['hits']; 411 if ($num < FT_SNIPPET_NUMBER) { // create snippets for the first number of matches only 412 $snippet = '<dd>' . ft_snippet($id, $highlight) . '</dd>'; 413 $lastMod = '<span class="search_results__lastmod">'. $lang['lastmod'] . ' '; 414 $lastMod .= '<time datetime="' . date_iso8601($mtime) . '">'. dformat($mtime) . '</time>'; 415 $lastMod .= '</span>'; 416 } 417 $num++; 418 } 419 420 $metaLine = '<div class="search_results__metaLine">'; 421 $metaLine .= $lastMod; 422 $metaLine .= '</div>'; 423 424 425 $eventData = [ 426 'resultHeader' => $resultHeader, 427 'resultBody' => [$metaLine, $snippet], 428 'page' => $id, 429 ]; 430 trigger_event('SEARCH_RESULT_FULLPAGE', $eventData); 431 $html .= '<div class="search_fullpage_result">'; 432 $html .= '<dt>' . implode(' ', $eventData['resultHeader']) . '</dt>'; 433 $html .= implode('', $eventData['resultBody']); 434 $html .= '</div>'; 435 } 436 $html .= '</dl>'; 437 438 return $html; 439 } 440 441 /** 442 * create a link to restrict the current query to a namespace 443 * 444 * @param bool|string $ns the namespace to which to restrict the query 445 * 446 * @return bool|string 447 */ 448 protected function restrictQueryToNSLink($ns) 449 { 450 if (!$ns) { 451 return false; 452 } 453 if (!$this->isSearchAssistanceAvailable($this->parsedQuery)) { 454 return false; 455 } 456 if (!empty($this->parsedQuery['ns']) && $this->parsedQuery['ns'][0] === $ns) { 457 return false; 458 } 459 $name = '@' . $ns; 460 $tmpForm = new Form(); 461 $this->searchState->addSeachLinkNS($tmpForm, $name, $ns); 462 return $tmpForm->toHTML(); 463 } 464} 465