| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
use MediaWiki\Config\Config; |
| 29 |
use MediaWiki\Content\Content; |
| 30 |
use MediaWiki\HookContainer\HookContainer; |
| 31 |
use MediaWiki\HookContainer\HookRunner; |
| 32 |
use MediaWiki\MediaWikiServices; |
| 33 |
use MediaWiki\Search\TitleMatcher; |
| 34 |
use MediaWiki\Status\Status; |
| 35 |
use MediaWiki\Title\Title; |
| 36 |
use MediaWiki\User\User; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
abstract class SearchEngine { |
| 44 |
public const DEFAULT_SORT = 'relevance'; |
| 45 |
|
| 46 |
|
| 47 |
public $prefix = ''; |
| 48 |
|
| 49 |
|
| 50 |
public $namespaces = [ NS_MAIN ]; |
| 51 |
|
| 52 |
|
| 53 |
protected $limit = 10; |
| 54 |
|
| 55 |
|
| 56 |
protected $offset = 0; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
protected $searchTerms = []; |
| 63 |
|
| 64 |
|
| 65 |
protected $showSuggestion = true; |
| 66 |
|
| 67 |
private $sort = self::DEFAULT_SORT; |
| 68 |
|
| 69 |
|
| 70 |
protected $features = []; |
| 71 |
|
| 72 |
|
| 73 |
private $hookContainer; |
| 74 |
|
| 75 |
|
| 76 |
private $hookRunner; |
| 77 |
|
| 78 |
|
| 79 |
public const COMPLETION_PROFILE_TYPE = 'completionSearchProfile'; |
| 80 |
|
| 81 |
|
| 82 |
public const FT_QUERY_INDEP_PROFILE_TYPE = 'fulltextQueryIndepProfile'; |
| 83 |
|
| 84 |
|
| 85 |
protected const CHARS_ALL = 1; |
| 86 |
|
| 87 |
|
| 88 |
protected const CHARS_NO_SYNTAX = 2; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public function searchText( $term ) { |
| 101 |
return $this->maybePaginate( function () use ( $term ) { |
| 102 |
return $this->doSearchText( $term ); |
| 103 |
} ); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
protected function doSearchText( $term ) { |
| 116 |
return null; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public function searchArchiveTitle( $term ) { |
| 134 |
return $this->doSearchArchiveTitle( $term ); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
protected function doSearchArchiveTitle( $term ) { |
| 147 |
return Status::newGood( [] ); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public function searchTitle( $term ) { |
| 162 |
return $this->maybePaginate( function () use ( $term ) { |
| 163 |
return $this->doSearchTitle( $term ); |
| 164 |
} ); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
protected function doSearchTitle( $term ) { |
| 177 |
return null; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
private function maybePaginate( Closure $fn ) { |
| 189 |
if ( $this instanceof PaginatingSearchEngine ) { |
| 190 |
return $fn(); |
| 191 |
} |
| 192 |
$this->limit++; |
| 193 |
try { |
| 194 |
$resultSetOrStatus = $fn(); |
| 195 |
} finally { |
| 196 |
$this->limit--; |
| 197 |
} |
| 198 |
|
| 199 |
$resultSet = null; |
| 200 |
if ( $resultSetOrStatus instanceof ISearchResultSet ) { |
| 201 |
$resultSet = $resultSetOrStatus; |
| 202 |
} elseif ( $resultSetOrStatus instanceof Status && |
| 203 |
$resultSetOrStatus->getValue() instanceof ISearchResultSet |
| 204 |
) { |
| 205 |
$resultSet = $resultSetOrStatus->getValue(); |
| 206 |
} |
| 207 |
if ( $resultSet ) { |
| 208 |
$resultSet->shrink( $this->limit ); |
| 209 |
} |
| 210 |
|
| 211 |
return $resultSetOrStatus; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
public function supports( $feature ) { |
| 222 |
switch ( $feature ) { |
| 223 |
case 'search-update': |
| 224 |
return true; |
| 225 |
case 'title-suffix-filter': |
| 226 |
default: |
| 227 |
return false; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
public function setFeatureData( $feature, $data ) { |
| 238 |
$this->features[$feature] = $data; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public function getFeatureData( $feature ) { |
| 249 |
return $this->features[$feature] ?? null; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
public function normalizeText( $string ) { |
| 261 |
|
| 262 |
return MediaWikiServices::getInstance()->getContentLanguage()->segmentByWord( $string ); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
public function getNearMatcher( Config $config ) { |
| 272 |
return MediaWikiServices::getInstance()->getTitleMatcher(); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
protected static function defaultNearMatcher() { |
| 282 |
wfDeprecated( __METHOD__, '1.40' ); |
| 283 |
return MediaWikiServices::getInstance()->getTitleMatcher(); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
public function legalSearchChars( $type = self::CHARS_ALL ) { |
| 293 |
return "A-Za-z_'.0-9\\x80-\\xFF\\-"; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
public function setLimitOffset( $limit, $offset = 0 ) { |
| 304 |
$this->limit = intval( $limit ); |
| 305 |
$this->offset = intval( $offset ); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
public function setNamespaces( $namespaces ) { |
| 315 |
if ( $namespaces ) { |
| 316 |
|
| 317 |
$validNs = MediaWikiServices::getInstance()->getSearchEngineConfig()->searchableNamespaces(); |
| 318 |
$namespaces = array_filter( $namespaces, static function ( $ns ) use( $validNs ) { |
| 319 |
return $ns < 0 || isset( $validNs[$ns] ); |
| 320 |
} ); |
| 321 |
} else { |
| 322 |
$namespaces = []; |
| 323 |
} |
| 324 |
$this->namespaces = $namespaces; |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
public function setShowSuggestion( $showSuggestion ) { |
| 335 |
$this->showSuggestion = $showSuggestion; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
public function getValidSorts() { |
| 348 |
return [ self::DEFAULT_SORT ]; |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
public function setSort( $sort ) { |
| 359 |
if ( !in_array( $sort, $this->getValidSorts() ) ) { |
| 360 |
throw new InvalidArgumentException( "Invalid sort: $sort. " . |
| 361 |
"Must be one of: " . implode( ', ', $this->getValidSorts() ) ); |
| 362 |
} |
| 363 |
$this->sort = $sort; |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
public function getSort() { |
| 373 |
return $this->sort; |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
public function replacePrefixes( $query ) { |
| 386 |
return $query; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
public static function parseNamespacePrefixes( |
| 403 |
$query, |
| 404 |
$withAllKeyword = true, |
| 405 |
$withPrefixSearchExtractNamespaceHook = false |
| 406 |
) { |
| 407 |
$parsed = $query; |
| 408 |
if ( strpos( $query, ':' ) === false ) { |
| 409 |
return false; |
| 410 |
} |
| 411 |
$extractedNamespace = null; |
| 412 |
|
| 413 |
$allQuery = false; |
| 414 |
if ( $withAllKeyword ) { |
| 415 |
$allkeywords = []; |
| 416 |
|
| 417 |
$allkeywords[] = wfMessage( 'searchall' )->inContentLanguage()->text() . ":"; |
| 418 |
|
| 419 |
if ( !in_array( 'all:', $allkeywords ) ) { |
| 420 |
$allkeywords[] = 'all:'; |
| 421 |
} |
| 422 |
|
| 423 |
foreach ( $allkeywords as $kw ) { |
| 424 |
if ( str_starts_with( $query, $kw ) ) { |
| 425 |
$parsed = substr( $query, strlen( $kw ) ); |
| 426 |
$allQuery = true; |
| 427 |
break; |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
if ( !$allQuery && strpos( $query, ':' ) !== false ) { |
| 433 |
$prefix = str_replace( ' ', '_', substr( $query, 0, strpos( $query, ':' ) ) ); |
| 434 |
$services = MediaWikiServices::getInstance(); |
| 435 |
$index = $services->getContentLanguage()->getNsIndex( $prefix ); |
| 436 |
if ( $index !== false ) { |
| 437 |
$extractedNamespace = [ $index ]; |
| 438 |
$parsed = substr( $query, strlen( $prefix ) + 1 ); |
| 439 |
} elseif ( $withPrefixSearchExtractNamespaceHook ) { |
| 440 |
$hookNamespaces = [ NS_MAIN ]; |
| 441 |
$hookQuery = $query; |
| 442 |
( new HookRunner( $services->getHookContainer() ) ) |
| 443 |
->onPrefixSearchExtractNamespace( $hookNamespaces, $hookQuery ); |
| 444 |
if ( $hookQuery !== $query ) { |
| 445 |
$parsed = $hookQuery; |
| 446 |
$extractedNamespace = $hookNamespaces; |
| 447 |
} else { |
| 448 |
return false; |
| 449 |
} |
| 450 |
} else { |
| 451 |
return false; |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
return [ $parsed, $extractedNamespace ]; |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
public static function userHighlightPrefs() { |
| 466 |
$contextlines = SearchHighlighter::DEFAULT_CONTEXT_LINES; |
| 467 |
$contextchars = SearchHighlighter::DEFAULT_CONTEXT_CHARS; |
| 468 |
return [ $contextlines, $contextchars ]; |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
public function update( $id, $title, $text ) { |
| 481 |
|
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
public function updateTitle( $id, $title ) { |
| 493 |
|
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
public function delete( $id, $title ) { |
| 505 |
|
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
public function getTextFromContent( Title $t, ?Content $c = null ) { |
| 520 |
return $c ? $c->getTextForSearchIndex() : ''; |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
public function textAlreadyUpdatedForIndex() { |
| 532 |
return false; |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
protected function normalizeNamespaces( $search ) { |
| 542 |
$queryAndNs = self::parseNamespacePrefixes( $search, false, true ); |
| 543 |
if ( $queryAndNs !== false ) { |
| 544 |
$this->setNamespaces( $queryAndNs[1] ); |
| 545 |
return $queryAndNs[0]; |
| 546 |
} |
| 547 |
return $search; |
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
protected function completionSearchBackendOverfetch( $search ) { |
| 558 |
$this->limit++; |
| 559 |
try { |
| 560 |
return $this->completionSearchBackend( $search ); |
| 561 |
} finally { |
| 562 |
$this->limit--; |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
protected function completionSearchBackend( $search ) { |
| 577 |
$results = []; |
| 578 |
|
| 579 |
$search = trim( $search ); |
| 580 |
|
| 581 |
if ( !in_array( NS_SPECIAL, $this->namespaces ) && |
| 582 |
!$this->getHookRunner()->onPrefixSearchBackend( |
| 583 |
$this->namespaces, $search, $this->limit, $results, $this->offset ) |
| 584 |
) { |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
return SearchSuggestionSet::fromStrings( $results ); |
| 589 |
} else { |
| 590 |
|
| 591 |
$results = $this->simplePrefixSearch( $search ); |
| 592 |
return SearchSuggestionSet::fromTitles( $results ); |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
public function completionSearch( $search ) { |
| 602 |
if ( trim( $search ) === '' ) { |
| 603 |
return SearchSuggestionSet::emptySuggestionSet(); |
| 604 |
} |
| 605 |
$search = $this->normalizeNamespaces( $search ); |
| 606 |
$suggestions = $this->completionSearchBackendOverfetch( $search ); |
| 607 |
return $this->processCompletionResults( $search, $suggestions ); |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
public function completionSearchWithVariants( $search ) { |
| 618 |
if ( trim( $search ) === '' ) { |
| 619 |
return SearchSuggestionSet::emptySuggestionSet(); |
| 620 |
} |
| 621 |
$search = $this->normalizeNamespaces( $search ); |
| 622 |
|
| 623 |
$results = $this->completionSearchBackendOverfetch( $search ); |
| 624 |
$fallbackLimit = 1 + $this->limit - $results->getSize(); |
| 625 |
if ( $fallbackLimit > 0 ) { |
| 626 |
$services = MediaWikiServices::getInstance(); |
| 627 |
$fallbackSearches = $services->getLanguageConverterFactory() |
| 628 |
->getLanguageConverter( $services->getContentLanguage() ) |
| 629 |
->autoConvertToAllVariants( $search ); |
| 630 |
$fallbackSearches = array_diff( array_unique( $fallbackSearches ), [ $search ] ); |
| 631 |
|
| 632 |
foreach ( $fallbackSearches as $fbs ) { |
| 633 |
$this->setLimitOffset( $fallbackLimit ); |
| 634 |
$fallbackSearchResult = $this->completionSearch( $fbs ); |
| 635 |
$results->appendAll( $fallbackSearchResult ); |
| 636 |
$fallbackLimit -= $fallbackSearchResult->getSize(); |
| 637 |
if ( $fallbackLimit <= 0 ) { |
| 638 |
break; |
| 639 |
} |
| 640 |
} |
| 641 |
} |
| 642 |
return $this->processCompletionResults( $search, $results ); |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
public function extractTitles( SearchSuggestionSet $completionResults ) { |
| 651 |
return $completionResults->map( static function ( SearchSuggestion $sugg ) { |
| 652 |
return $sugg->getSuggestedTitle(); |
| 653 |
} ); |
| 654 |
} |
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
protected function processCompletionResults( $search, SearchSuggestionSet $suggestions ) { |
| 664 |
|
| 665 |
|
| 666 |
$suggestions->shrink( $this->limit ); |
| 667 |
|
| 668 |
$search = trim( $search ); |
| 669 |
|
| 670 |
$linkBatchFactory = MediaWikiServices::getInstance()->getLinkBatchFactory(); |
| 671 |
$lb = $linkBatchFactory->newLinkBatch( $suggestions->map( static function ( SearchSuggestion $sugg ) { |
| 672 |
return $sugg->getSuggestedTitle(); |
| 673 |
} ) ); |
| 674 |
$lb->setCaller( __METHOD__ ); |
| 675 |
$lb->execute(); |
| 676 |
|
| 677 |
$diff = $suggestions->filter( static function ( SearchSuggestion $sugg ) { |
| 678 |
return $sugg->getSuggestedTitle()->isKnown(); |
| 679 |
} ); |
| 680 |
if ( $diff > 0 ) { |
| 681 |
MediaWikiServices::getInstance()->getStatsdDataFactory() |
| 682 |
->updateCount( 'search.completion.missing', $diff ); |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
$results = $suggestions->map( static function ( SearchSuggestion $sugg ) { |
| 689 |
return $sugg->getSuggestedTitle()->getPrefixedText(); |
| 690 |
} ); |
| 691 |
|
| 692 |
$rescorer = new SearchExactMatchRescorer(); |
| 693 |
if ( $this->offset === 0 ) { |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
$rescoredResults = $rescorer->rescore( $search, $this->namespaces, $results, $this->limit ); |
| 700 |
} else { |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
$rescoredResults = $results; |
| 705 |
} |
| 706 |
|
| 707 |
if ( count( $rescoredResults ) > 0 ) { |
| 708 |
$found = array_search( $rescoredResults[0], $results ); |
| 709 |
if ( $found === false ) { |
| 710 |
|
| 711 |
|
| 712 |
$exactMatch = SearchSuggestion::fromTitle( 0, Title::newFromText( $rescoredResults[0] ) ); |
| 713 |
$suggestions->prepend( $exactMatch ); |
| 714 |
if ( $rescorer->getReplacedRedirect() !== null ) { |
| 715 |
|
| 716 |
|
| 717 |
$suggestions->remove( SearchSuggestion::fromTitle( 0, |
| 718 |
Title::newFromText( $rescorer->getReplacedRedirect() ) ) ); |
| 719 |
} |
| 720 |
$suggestions->shrink( $this->limit ); |
| 721 |
} else { |
| 722 |
|
| 723 |
if ( $found > 0 ) { |
| 724 |
$suggestions->rescore( $found ); |
| 725 |
} |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
return $suggestions; |
| 730 |
} |
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
public function defaultPrefixSearch( $search ) { |
| 738 |
if ( trim( $search ) === '' ) { |
| 739 |
return []; |
| 740 |
} |
| 741 |
|
| 742 |
$search = $this->normalizeNamespaces( $search ); |
| 743 |
return $this->simplePrefixSearch( $search ); |
| 744 |
} |
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
protected function simplePrefixSearch( $search ) { |
| 753 |
|
| 754 |
$backend = new TitlePrefixSearch; |
| 755 |
return $backend->defaultSearchBackend( $this->namespaces, $search, $this->limit, $this->offset ); |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
public function getProfiles( $profileType, ?User $user = null ) { |
| 777 |
return null; |
| 778 |
} |
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
public function makeSearchFieldMapping( $name, $type ) { |
| 791 |
return new NullIndexField(); |
| 792 |
} |
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
public function getSearchIndexFields() { |
| 800 |
$models = MediaWikiServices::getInstance()->getContentHandlerFactory()->getContentModels(); |
| 801 |
$fields = []; |
| 802 |
$seenHandlers = new SplObjectStorage(); |
| 803 |
foreach ( $models as $model ) { |
| 804 |
try { |
| 805 |
$handler = MediaWikiServices::getInstance() |
| 806 |
->getContentHandlerFactory() |
| 807 |
->getContentHandler( $model ); |
| 808 |
} catch ( MWUnknownContentModelException $e ) { |
| 809 |
|
| 810 |
continue; |
| 811 |
} |
| 812 |
|
| 813 |
if ( $seenHandlers->offsetExists( $handler ) ) { |
| 814 |
|
| 815 |
continue; |
| 816 |
} |
| 817 |
$seenHandlers->offsetSet( $handler ); |
| 818 |
$handlerFields = $handler->getFieldsForSearchIndex( $this ); |
| 819 |
foreach ( $handlerFields as $fieldName => $fieldData ) { |
| 820 |
if ( empty( $fields[$fieldName] ) ) { |
| 821 |
$fields[$fieldName] = $fieldData; |
| 822 |
} else { |
| 823 |
|
| 824 |
$mergeDef = $fields[$fieldName]->merge( $fieldData ); |
| 825 |
if ( !$mergeDef ) { |
| 826 |
throw new InvalidArgumentException( "Duplicate field $fieldName for model $model" ); |
| 827 |
} |
| 828 |
$fields[$fieldName] = $mergeDef; |
| 829 |
} |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
$this->getHookRunner()->onSearchIndexFields( $fields, $this ); |
| 834 |
return $fields; |
| 835 |
} |
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
public function augmentSearchResults( ISearchResultSet $resultSet ) { |
| 843 |
$setAugmentors = []; |
| 844 |
$rowAugmentors = []; |
| 845 |
$this->getHookRunner()->onSearchResultsAugment( $setAugmentors, $rowAugmentors ); |
| 846 |
if ( !$setAugmentors && !$rowAugmentors ) { |
| 847 |
|
| 848 |
return; |
| 849 |
} |
| 850 |
|
| 851 |
|
| 852 |
foreach ( $rowAugmentors as $name => $row ) { |
| 853 |
if ( isset( $setAugmentors[$name] ) ) { |
| 854 |
throw new InvalidArgumentException( "Both row and set augmentors are defined for $name" ); |
| 855 |
} |
| 856 |
$setAugmentors[$name] = new PerRowAugmentor( $row ); |
| 857 |
} |
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
foreach ( $setAugmentors as $name => $augmentor ) { |
| 864 |
$data = $augmentor->augmentAll( $resultSet ); |
| 865 |
if ( $data ) { |
| 866 |
$resultSet->setAugmentedData( $name, $data ); |
| 867 |
} |
| 868 |
} |
| 869 |
} |
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
public function setHookContainer( HookContainer $hookContainer ) { |
| 877 |
$this->hookContainer = $hookContainer; |
| 878 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 879 |
} |
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
protected function getHookContainer(): HookContainer { |
| 888 |
if ( !$this->hookContainer ) { |
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
$this->hookContainer = MediaWikiServices::getInstance()->getHookContainer(); |
| 893 |
} |
| 894 |
return $this->hookContainer; |
| 895 |
} |
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
protected function getHookRunner(): HookRunner { |
| 906 |
if ( !$this->hookRunner ) { |
| 907 |
$this->hookRunner = new HookRunner( $this->getHookContainer() ); |
| 908 |
} |
| 909 |
return $this->hookRunner; |
| 910 |
} |
| 911 |
|
| 912 |
} |