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