| 1 |
<?php |
| 2 |
|
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Specials; |
| 24 |
|
| 25 |
use ISearchResultSet; |
| 26 |
use MediaWiki\Config\ServiceOptions; |
| 27 |
use MediaWiki\Content\IContentHandlerFactory; |
| 28 |
use MediaWiki\Deferred\DeferredUpdates; |
| 29 |
use MediaWiki\Html\Html; |
| 30 |
use MediaWiki\Interwiki\InterwikiLookup; |
| 31 |
use MediaWiki\Languages\LanguageConverterFactory; |
| 32 |
use MediaWiki\MainConfigNames; |
| 33 |
use MediaWiki\Message\Message; |
| 34 |
use MediaWiki\Output\OutputPage; |
| 35 |
use MediaWiki\Request\WebRequest; |
| 36 |
use MediaWiki\Search\SearchResultThumbnailProvider; |
| 37 |
use MediaWiki\Search\SearchWidgets\BasicSearchResultSetWidget; |
| 38 |
use MediaWiki\Search\SearchWidgets\DidYouMeanWidget; |
| 39 |
use MediaWiki\Search\SearchWidgets\FullSearchResultWidget; |
| 40 |
use MediaWiki\Search\SearchWidgets\InterwikiSearchResultSetWidget; |
| 41 |
use MediaWiki\Search\SearchWidgets\InterwikiSearchResultWidget; |
| 42 |
use MediaWiki\Search\SearchWidgets\SearchFormWidget; |
| 43 |
use MediaWiki\Search\TitleMatcher; |
| 44 |
use MediaWiki\SpecialPage\SpecialPage; |
| 45 |
use MediaWiki\Status\Status; |
| 46 |
use MediaWiki\Title\NamespaceInfo; |
| 47 |
use MediaWiki\Title\Title; |
| 48 |
use MediaWiki\User\Options\UserOptionsManager; |
| 49 |
use MediaWiki\Xml\Xml; |
| 50 |
use RepoGroup; |
| 51 |
use SearchEngine; |
| 52 |
use SearchEngineConfig; |
| 53 |
use SearchEngineFactory; |
| 54 |
use Wikimedia\Rdbms\ReadOnlyMode; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
class SpecialSearch extends SpecialPage { |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
protected $profile; |
| 72 |
|
| 73 |
|
| 74 |
protected $searchEngine; |
| 75 |
|
| 76 |
|
| 77 |
protected $searchEngineType = null; |
| 78 |
|
| 79 |
|
| 80 |
protected $extraParams = []; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
protected $mPrefix; |
| 87 |
|
| 88 |
protected int $limit; |
| 89 |
protected int $offset; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
protected $namespaces; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
protected $fulltext; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
protected $sort = SearchEngine::DEFAULT_SORT; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
protected $runSuggestion = true; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
protected $searchConfig; |
| 116 |
|
| 117 |
private SearchEngineFactory $searchEngineFactory; |
| 118 |
private NamespaceInfo $nsInfo; |
| 119 |
private IContentHandlerFactory $contentHandlerFactory; |
| 120 |
private InterwikiLookup $interwikiLookup; |
| 121 |
private ReadOnlyMode $readOnlyMode; |
| 122 |
private UserOptionsManager $userOptionsManager; |
| 123 |
private LanguageConverterFactory $languageConverterFactory; |
| 124 |
private RepoGroup $repoGroup; |
| 125 |
private SearchResultThumbnailProvider $thumbnailProvider; |
| 126 |
private TitleMatcher $titleMatcher; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
private $loadStatus; |
| 133 |
|
| 134 |
private const NAMESPACES_CURRENT = 'sense'; |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public function __construct( |
| 150 |
SearchEngineConfig $searchConfig, |
| 151 |
SearchEngineFactory $searchEngineFactory, |
| 152 |
NamespaceInfo $nsInfo, |
| 153 |
IContentHandlerFactory $contentHandlerFactory, |
| 154 |
InterwikiLookup $interwikiLookup, |
| 155 |
ReadOnlyMode $readOnlyMode, |
| 156 |
UserOptionsManager $userOptionsManager, |
| 157 |
LanguageConverterFactory $languageConverterFactory, |
| 158 |
RepoGroup $repoGroup, |
| 159 |
SearchResultThumbnailProvider $thumbnailProvider, |
| 160 |
TitleMatcher $titleMatcher |
| 161 |
) { |
| 162 |
parent::__construct( 'Search' ); |
| 163 |
$this->searchConfig = $searchConfig; |
| 164 |
$this->searchEngineFactory = $searchEngineFactory; |
| 165 |
$this->nsInfo = $nsInfo; |
| 166 |
$this->contentHandlerFactory = $contentHandlerFactory; |
| 167 |
$this->interwikiLookup = $interwikiLookup; |
| 168 |
$this->readOnlyMode = $readOnlyMode; |
| 169 |
$this->userOptionsManager = $userOptionsManager; |
| 170 |
$this->languageConverterFactory = $languageConverterFactory; |
| 171 |
$this->repoGroup = $repoGroup; |
| 172 |
$this->thumbnailProvider = $thumbnailProvider; |
| 173 |
$this->titleMatcher = $titleMatcher; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
public function execute( $par ) { |
| 182 |
$request = $this->getRequest(); |
| 183 |
$out = $this->getOutput(); |
| 184 |
|
| 185 |
|
| 186 |
$term = str_replace( "\n", " ", $request->getText( 'search' ) ); |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
if ( $par !== null && $par !== '' && $term === '' ) { |
| 193 |
$query = $request->getValues(); |
| 194 |
unset( $query['title'] ); |
| 195 |
|
| 196 |
|
| 197 |
$query['search'] = str_replace( '_', ' ', $par ); |
| 198 |
$out->redirect( $this->getPageTitle()->getFullURL( $query ), 301 ); |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
$this->load(); |
| 204 |
|
| 205 |
|
| 206 |
if ( $request->getCheck( 'nsRemember' ) ) { |
| 207 |
$this->saveNamespaces(); |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
$query = $request->getValues(); |
| 212 |
unset( $query['title'], $query['nsRemember'] ); |
| 213 |
$out->redirect( $this->getPageTitle()->getFullURL( $query ) ); |
| 214 |
return; |
| 215 |
} |
| 216 |
|
| 217 |
if ( !$request->getVal( 'fulltext' ) && !$request->getCheck( 'offset' ) ) { |
| 218 |
$url = $this->goResult( $term ); |
| 219 |
if ( $url !== null ) { |
| 220 |
|
| 221 |
$out->redirect( $url ); |
| 222 |
return; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
$title = Title::newFromText( $term ); |
| 227 |
if ( $title !== null ) { |
| 228 |
$this->getHookRunner()->onSpecialSearchNogomatch( $title ); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
$this->setupPage( $term ); |
| 233 |
|
| 234 |
if ( $this->getConfig()->get( MainConfigNames::DisableTextSearch ) ) { |
| 235 |
$searchForwardUrl = $this->getConfig()->get( MainConfigNames::SearchForwardUrl ); |
| 236 |
if ( $searchForwardUrl ) { |
| 237 |
$url = str_replace( '$1', urlencode( $term ), $searchForwardUrl ); |
| 238 |
$out->redirect( $url ); |
| 239 |
} else { |
| 240 |
$out->addHTML( $this->showGoogleSearch( $term ) ); |
| 241 |
} |
| 242 |
|
| 243 |
return; |
| 244 |
} |
| 245 |
|
| 246 |
$this->showResults( $term ); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
private function showGoogleSearch( $term ) { |
| 258 |
return "<fieldset>" . |
| 259 |
"<legend>" . |
| 260 |
$this->msg( 'search-external' )->escaped() . |
| 261 |
"</legend>" . |
| 262 |
"<p class='mw-searchdisabled'>" . |
| 263 |
$this->msg( 'searchdisabled' )->escaped() . |
| 264 |
"</p>" . |
| 265 |
|
| 266 |
$this->msg( 'googlesearch' )->rawParams( |
| 267 |
htmlspecialchars( $term ), |
| 268 |
'UTF-8', |
| 269 |
$this->msg( 'searchbutton' )->escaped() |
| 270 |
)->text() . |
| 271 |
"</fieldset>"; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
public function load() { |
| 280 |
$this->loadStatus = new Status(); |
| 281 |
|
| 282 |
$request = $this->getRequest(); |
| 283 |
$this->searchEngineType = $request->getVal( 'srbackend' ); |
| 284 |
|
| 285 |
[ $this->limit, $this->offset ] = $request->getLimitOffsetForUser( |
| 286 |
$this->getUser(), |
| 287 |
20, |
| 288 |
'searchlimit' |
| 289 |
); |
| 290 |
$this->mPrefix = $request->getVal( 'prefix', '' ); |
| 291 |
if ( $this->mPrefix !== '' ) { |
| 292 |
$this->setExtraParam( 'prefix', $this->mPrefix ); |
| 293 |
} |
| 294 |
|
| 295 |
$sort = $request->getVal( 'sort', SearchEngine::DEFAULT_SORT ); |
| 296 |
$validSorts = $this->getSearchEngine()->getValidSorts(); |
| 297 |
if ( !in_array( $sort, $validSorts ) ) { |
| 298 |
$this->loadStatus->warning( 'search-invalid-sort-order', $sort, |
| 299 |
implode( ', ', $validSorts ) ); |
| 300 |
} elseif ( $sort !== $this->sort ) { |
| 301 |
$this->sort = $sort; |
| 302 |
$this->setExtraParam( 'sort', $this->sort ); |
| 303 |
} |
| 304 |
|
| 305 |
$user = $this->getUser(); |
| 306 |
|
| 307 |
|
| 308 |
$nslist = $this->powerSearch( $request ); |
| 309 |
if ( $nslist === [] ) { |
| 310 |
|
| 311 |
$nslist = $this->searchConfig->userNamespaces( $user ); |
| 312 |
} |
| 313 |
|
| 314 |
$profile = null; |
| 315 |
if ( $nslist === [] ) { |
| 316 |
$profile = 'default'; |
| 317 |
} |
| 318 |
|
| 319 |
$profile = $request->getVal( 'profile', $profile ); |
| 320 |
$profiles = $this->getSearchProfiles(); |
| 321 |
if ( $profile === null ) { |
| 322 |
|
| 323 |
$profile = 'advanced'; |
| 324 |
foreach ( $profiles as $key => $data ) { |
| 325 |
if ( $nslist === $data['namespaces'] && $key !== 'advanced' ) { |
| 326 |
$profile = $key; |
| 327 |
} |
| 328 |
} |
| 329 |
$this->namespaces = $nslist; |
| 330 |
} elseif ( $profile === 'advanced' ) { |
| 331 |
$this->namespaces = $nslist; |
| 332 |
} elseif ( isset( $profiles[$profile]['namespaces'] ) ) { |
| 333 |
$this->namespaces = $profiles[$profile]['namespaces']; |
| 334 |
} else { |
| 335 |
|
| 336 |
$this->loadStatus->warning( 'search-unknown-profile', $profile ); |
| 337 |
$profile = 'default'; |
| 338 |
$this->namespaces = $profiles['default']['namespaces']; |
| 339 |
} |
| 340 |
|
| 341 |
$this->fulltext = $request->getVal( 'fulltext' ); |
| 342 |
$this->runSuggestion = (bool)$request->getVal( 'runsuggestion', '1' ); |
| 343 |
$this->profile = $profile; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
public function goResult( $term ) { |
| 353 |
|
| 354 |
if ( Title::newFromText( $term ) === null ) { |
| 355 |
return null; |
| 356 |
} |
| 357 |
|
| 358 |
$title = $this->titleMatcher->getNearMatch( $term ); |
| 359 |
if ( $title === null ) { |
| 360 |
return null; |
| 361 |
} |
| 362 |
$url = null; |
| 363 |
if ( !$this->getHookRunner()->onSpecialSearchGoResult( $term, $title, $url ) ) { |
| 364 |
return null; |
| 365 |
} |
| 366 |
|
| 367 |
if ( |
| 368 |
|
| 369 |
|
| 370 |
!$this->redirectOnExactMatch() |
| 371 |
|
| 372 |
|
| 373 |
&& !$title->isExternal() |
| 374 |
|
| 375 |
|
| 376 |
&& !( $title->getNamespace() !== NS_MAIN && strpos( $term, ':' ) > 0 ) |
| 377 |
) { |
| 378 |
return null; |
| 379 |
} |
| 380 |
|
| 381 |
return $url ?? $title->getFullUrlForRedirect(); |
| 382 |
} |
| 383 |
|
| 384 |
private function redirectOnExactMatch() { |
| 385 |
if ( !$this->getConfig()->get( MainConfigNames::SearchMatchRedirectPreference ) ) { |
| 386 |
|
| 387 |
return $this->userOptionsManager->getDefaultOption( |
| 388 |
'search-match-redirect', |
| 389 |
$this->getUser() |
| 390 |
); |
| 391 |
} else { |
| 392 |
|
| 393 |
return $this->userOptionsManager->getOption( $this->getUser(), 'search-match-redirect' ); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
public function showResults( $term ) { |
| 401 |
if ( $this->searchEngineType !== null ) { |
| 402 |
$this->setExtraParam( 'srbackend', $this->searchEngineType ); |
| 403 |
} |
| 404 |
|
| 405 |
$out = $this->getOutput(); |
| 406 |
$widgetOptions = $this->getConfig()->get( MainConfigNames::SpecialSearchFormOptions ); |
| 407 |
$formWidget = new SearchFormWidget( |
| 408 |
new ServiceOptions( |
| 409 |
SearchFormWidget::CONSTRUCTOR_OPTIONS, |
| 410 |
$this->getConfig() |
| 411 |
), |
| 412 |
$this, |
| 413 |
$this->searchConfig, |
| 414 |
$this->getHookContainer(), |
| 415 |
$this->languageConverterFactory->getLanguageConverter( $this->getLanguage() ), |
| 416 |
$this->nsInfo, |
| 417 |
$this->getSearchProfiles() |
| 418 |
); |
| 419 |
$filePrefix = $this->getContentLanguage()->getFormattedNsText( NS_FILE ) . ':'; |
| 420 |
if ( trim( $term ) === '' || $filePrefix === trim( $term ) ) { |
| 421 |
|
| 422 |
if ( !$this->getHookRunner()->onSpecialSearchResultsPrepend( $this, $out, $term ) ) { |
| 423 |
|
| 424 |
return; |
| 425 |
} |
| 426 |
$out->enableOOUI(); |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
$out->addHTML( $formWidget->render( |
| 431 |
$this->profile, $term, 0, 0, $this->offset, $this->isPowerSearch(), $widgetOptions |
| 432 |
) ); |
| 433 |
return; |
| 434 |
} |
| 435 |
|
| 436 |
$engine = $this->getSearchEngine(); |
| 437 |
$engine->setFeatureData( 'rewrite', $this->runSuggestion ); |
| 438 |
$engine->setLimitOffset( $this->limit, $this->offset ); |
| 439 |
$engine->setNamespaces( $this->namespaces ); |
| 440 |
$engine->setSort( $this->sort ); |
| 441 |
$engine->prefix = $this->mPrefix; |
| 442 |
|
| 443 |
$this->getHookRunner()->onSpecialSearchSetupEngine( $this, $this->profile, $engine ); |
| 444 |
if ( !$this->getHookRunner()->onSpecialSearchResultsPrepend( $this, $out, $term ) ) { |
| 445 |
|
| 446 |
return; |
| 447 |
} |
| 448 |
|
| 449 |
$title = Title::newFromText( $term ); |
| 450 |
$languageConverter = $this->languageConverterFactory->getLanguageConverter( $this->getContentLanguage() ); |
| 451 |
if ( $languageConverter->hasVariants() ) { |
| 452 |
|
| 453 |
|
| 454 |
$variantTerm = $term; |
| 455 |
$languageConverter->findVariantLink( $variantTerm, $title ); |
| 456 |
} |
| 457 |
|
| 458 |
$showSuggestion = $title === null || !$title->isKnown(); |
| 459 |
$engine->setShowSuggestion( $showSuggestion ); |
| 460 |
|
| 461 |
$rewritten = $engine->replacePrefixes( $term ); |
| 462 |
if ( $rewritten !== $term ) { |
| 463 |
wfDeprecatedMsg( 'SearchEngine::replacePrefixes() was overridden by ' . |
| 464 |
get_class( $engine ) . ', this is deprecated since MediaWiki 1.32', |
| 465 |
'1.32', false, false ); |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
$titleMatches = $engine->searchTitle( $rewritten ); |
| 470 |
$textMatches = $engine->searchText( $rewritten ); |
| 471 |
|
| 472 |
$textStatus = null; |
| 473 |
if ( $textMatches instanceof Status ) { |
| 474 |
$textStatus = $textMatches; |
| 475 |
$textMatches = $textStatus->getValue(); |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
$titleMatchesNum = $textMatchesNum = $numTitleMatches = $numTextMatches = 0; |
| 480 |
if ( $titleMatches ) { |
| 481 |
$titleMatchesNum = $titleMatches->numRows(); |
| 482 |
$numTitleMatches = $titleMatches->getTotalHits(); |
| 483 |
} |
| 484 |
if ( $textMatches ) { |
| 485 |
$textMatchesNum = $textMatches->numRows(); |
| 486 |
$numTextMatches = $textMatches->getTotalHits(); |
| 487 |
if ( $textMatchesNum > 0 ) { |
| 488 |
$engine->augmentSearchResults( $textMatches ); |
| 489 |
} |
| 490 |
} |
| 491 |
$num = $titleMatchesNum + $textMatchesNum; |
| 492 |
$totalRes = $numTitleMatches + $numTextMatches; |
| 493 |
|
| 494 |
|
| 495 |
$out->enableOOUI(); |
| 496 |
$out->addHTML( $formWidget->render( |
| 497 |
$this->profile, $term, $num, $totalRes, $this->offset, $this->isPowerSearch(), $widgetOptions |
| 498 |
) ); |
| 499 |
|
| 500 |
|
| 501 |
if ( $textMatches ) { |
| 502 |
$dymWidget = new DidYouMeanWidget( $this ); |
| 503 |
$out->addHTML( $dymWidget->render( $term, $textMatches ) ); |
| 504 |
} |
| 505 |
|
| 506 |
$hasSearchErrors = $textStatus && $textStatus->getMessages() !== []; |
| 507 |
$hasInlineIwResults = $textMatches && |
| 508 |
$textMatches->hasInterwikiResults( ISearchResultSet::INLINE_RESULTS ); |
| 509 |
$hasSecondaryIwResults = $textMatches && |
| 510 |
$textMatches->hasInterwikiResults( ISearchResultSet::SECONDARY_RESULTS ); |
| 511 |
|
| 512 |
$classNames = [ 'searchresults' ]; |
| 513 |
if ( $hasSecondaryIwResults ) { |
| 514 |
$classNames[] = 'mw-searchresults-has-iw'; |
| 515 |
} |
| 516 |
if ( $this->offset > 0 ) { |
| 517 |
$classNames[] = 'mw-searchresults-has-offset'; |
| 518 |
} |
| 519 |
$out->addHTML( '<div class="' . implode( ' ', $classNames ) . '">' ); |
| 520 |
|
| 521 |
$out->addHTML( '<div class="mw-search-results-info">' ); |
| 522 |
|
| 523 |
if ( $hasSearchErrors || $this->loadStatus->getMessages() ) { |
| 524 |
if ( $textStatus === null ) { |
| 525 |
$textStatus = $this->loadStatus; |
| 526 |
} else { |
| 527 |
$textStatus->merge( $this->loadStatus ); |
| 528 |
} |
| 529 |
[ $error, $warning ] = $textStatus->splitByErrorType(); |
| 530 |
if ( $error->getMessages() ) { |
| 531 |
$out->addHTML( Html::errorBox( |
| 532 |
$error->getHTML( 'search-error' ) |
| 533 |
) ); |
| 534 |
} |
| 535 |
if ( $warning->getMessages() ) { |
| 536 |
$out->addHTML( Html::warningBox( |
| 537 |
$warning->getHTML( 'search-warning' ) |
| 538 |
) ); |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
if ( $num === 0 && !$hasSearchErrors ) { |
| 544 |
$out->wrapWikiMsg( "<p class=\"mw-search-nonefound\">\n$1</p>", [ |
| 545 |
$hasInlineIwResults ? 'search-nonefound-thiswiki' : 'search-nonefound', |
| 546 |
wfEscapeWikiText( $term ), |
| 547 |
$term |
| 548 |
] ); |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
$this->showCreateLink( $title, $num, $titleMatches, $textMatches ); |
| 553 |
|
| 554 |
$this->getHookRunner()->onSpecialSearchResults( $term, $titleMatches, $textMatches ); |
| 555 |
|
| 556 |
|
| 557 |
$out->addHTML( '</div>' ); |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
$linkRenderer = $this->getLinkRenderer(); |
| 562 |
$mainResultWidget = new FullSearchResultWidget( |
| 563 |
$this, |
| 564 |
$linkRenderer, |
| 565 |
$this->getHookContainer(), |
| 566 |
$this->repoGroup, |
| 567 |
$this->thumbnailProvider, |
| 568 |
$this->userOptionsManager |
| 569 |
); |
| 570 |
|
| 571 |
$sidebarResultWidget = new InterwikiSearchResultWidget( $this, $linkRenderer ); |
| 572 |
$sidebarResultsWidget = new InterwikiSearchResultSetWidget( |
| 573 |
$this, |
| 574 |
$sidebarResultWidget, |
| 575 |
$linkRenderer, |
| 576 |
$this->interwikiLookup, |
| 577 |
$engine->getFeatureData( 'show-multimedia-search-results' ) |
| 578 |
); |
| 579 |
|
| 580 |
$widget = new BasicSearchResultSetWidget( $this, $mainResultWidget, $sidebarResultsWidget ); |
| 581 |
|
| 582 |
$out->addHTML( '<div class="mw-search-visualclear"></div>' ); |
| 583 |
$this->prevNextLinks( $totalRes, $textMatches, $term, 'mw-search-pager-top', $out ); |
| 584 |
|
| 585 |
$out->addHTML( $widget->render( |
| 586 |
$term, $this->offset, $titleMatches, $textMatches |
| 587 |
) ); |
| 588 |
|
| 589 |
$out->addHTML( '<div class="mw-search-visualclear"></div>' ); |
| 590 |
$this->prevNextLinks( $totalRes, $textMatches, $term, 'mw-search-pager-bottom', $out ); |
| 591 |
|
| 592 |
|
| 593 |
$out->addHTML( "</div>" ); |
| 594 |
|
| 595 |
$this->getHookRunner()->onSpecialSearchResultsAppend( $this, $out, $term ); |
| 596 |
} |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
protected function showCreateLink( $title, $num, $titleMatches, $textMatches ) { |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
if ( $title === null || $title->getDBkey() === '' |
| 609 |
|| ( $titleMatches !== null && $titleMatches->searchContainedSyntax() ) |
| 610 |
|| ( $textMatches !== null && $textMatches->searchContainedSyntax() ) |
| 611 |
) { |
| 612 |
|
| 613 |
|
| 614 |
$this->getOutput()->addHTML( '<p></p>' ); |
| 615 |
|
| 616 |
return; |
| 617 |
} |
| 618 |
|
| 619 |
$messageName = 'searchmenu-new-nocreate'; |
| 620 |
$linkClass = 'mw-search-createlink'; |
| 621 |
|
| 622 |
if ( !$title->isExternal() ) { |
| 623 |
if ( $title->isKnown() ) { |
| 624 |
$messageName = 'searchmenu-exists'; |
| 625 |
$linkClass = 'mw-search-exists'; |
| 626 |
} elseif ( |
| 627 |
$this->contentHandlerFactory->getContentHandler( $title->getContentModel() ) |
| 628 |
->supportsDirectEditing() |
| 629 |
&& $this->getAuthority()->probablyCan( 'edit', $title ) |
| 630 |
) { |
| 631 |
$messageName = 'searchmenu-new'; |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
$params = [ |
| 636 |
$messageName, |
| 637 |
wfEscapeWikiText( $title->getPrefixedText() ), |
| 638 |
Message::numParam( $num ) |
| 639 |
]; |
| 640 |
$this->getHookRunner()->onSpecialSearchCreateLink( $title, $params ); |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
if ( $messageName ) { |
| 645 |
$this->getOutput()->wrapWikiMsg( "<p class=\"$linkClass\">\n$1</p>", $params ); |
| 646 |
} else { |
| 647 |
|
| 648 |
$this->getOutput()->addHTML( '<p></p>' ); |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
protected function setupPage( $term ) { |
| 659 |
$out = $this->getOutput(); |
| 660 |
|
| 661 |
$this->setHeaders(); |
| 662 |
$this->outputHeader(); |
| 663 |
|
| 664 |
|
| 665 |
$out->getMetadata()->setPreventClickjacking( false ); |
| 666 |
$this->addHelpLink( 'Help:Searching' ); |
| 667 |
|
| 668 |
if ( strval( $term ) !== '' ) { |
| 669 |
$out->setPageTitleMsg( $this->msg( 'searchresults' ) ); |
| 670 |
$out->setHTMLTitle( $this->msg( 'pagetitle' ) |
| 671 |
->plaintextParams( $this->msg( 'searchresults-title' )->plaintextParams( $term )->text() ) |
| 672 |
->inContentLanguage()->text() |
| 673 |
); |
| 674 |
} |
| 675 |
|
| 676 |
if ( $this->mPrefix !== '' ) { |
| 677 |
$subtitle = $this->msg( 'search-filter-title-prefix' )->plaintextParams( $this->mPrefix ); |
| 678 |
$params = $this->powerSearchOptions(); |
| 679 |
unset( $params['prefix'] ); |
| 680 |
$params += [ |
| 681 |
'search' => $term, |
| 682 |
'fulltext' => 1, |
| 683 |
]; |
| 684 |
|
| 685 |
$subtitle .= ' ('; |
| 686 |
$subtitle .= Xml::element( |
| 687 |
'a', |
| 688 |
[ |
| 689 |
'href' => $this->getPageTitle()->getLocalURL( $params ), |
| 690 |
'title' => $this->msg( 'search-filter-title-prefix-reset' )->text(), |
| 691 |
], |
| 692 |
$this->msg( 'search-filter-title-prefix-reset' )->text() |
| 693 |
); |
| 694 |
$subtitle .= ')'; |
| 695 |
$out->setSubtitle( $subtitle ); |
| 696 |
} |
| 697 |
|
| 698 |
$out->addJsConfigVars( [ 'searchTerm' => $term ] ); |
| 699 |
$out->addModules( 'mediawiki.special.search' ); |
| 700 |
$out->addModuleStyles( [ |
| 701 |
'mediawiki.special', 'mediawiki.special.search.styles', |
| 702 |
'mediawiki.widgets.SearchInputWidget.styles', |
| 703 |
] ); |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
protected function isPowerSearch() { |
| 712 |
return $this->profile === 'advanced'; |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
protected function powerSearch( &$request ) { |
| 723 |
$arr = []; |
| 724 |
foreach ( $this->searchConfig->searchableNamespaces() as $ns => $name ) { |
| 725 |
if ( $request->getCheck( 'ns' . $ns ) ) { |
| 726 |
$arr[] = $ns; |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
return $arr; |
| 731 |
} |
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
public function powerSearchOptions() { |
| 741 |
$opt = []; |
| 742 |
if ( $this->isPowerSearch() ) { |
| 743 |
foreach ( $this->namespaces as $n ) { |
| 744 |
$opt['ns' . $n] = 1; |
| 745 |
} |
| 746 |
} else { |
| 747 |
$opt['profile'] = $this->profile; |
| 748 |
} |
| 749 |
|
| 750 |
return $opt + $this->extraParams; |
| 751 |
} |
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
protected function saveNamespaces() { |
| 759 |
$user = $this->getUser(); |
| 760 |
$request = $this->getRequest(); |
| 761 |
|
| 762 |
if ( $user->isRegistered() && |
| 763 |
$user->matchEditToken( |
| 764 |
$request->getVal( 'nsRemember' ), |
| 765 |
'searchnamespace', |
| 766 |
$request |
| 767 |
) && !$this->readOnlyMode->isReadOnly() |
| 768 |
) { |
| 769 |
|
| 770 |
|
| 771 |
foreach ( $this->nsInfo->getValidNamespaces() as $n ) { |
| 772 |
$this->userOptionsManager->setOption( $user, 'searchNs' . $n, false ); |
| 773 |
} |
| 774 |
|
| 775 |
|
| 776 |
foreach ( $this->namespaces as $n ) { |
| 777 |
$this->userOptionsManager->setOption( $user, 'searchNs' . $n, true ); |
| 778 |
} |
| 779 |
|
| 780 |
DeferredUpdates::addCallableUpdate( static function () use ( $user ) { |
| 781 |
$user->saveSettings(); |
| 782 |
} ); |
| 783 |
|
| 784 |
return true; |
| 785 |
} |
| 786 |
|
| 787 |
return false; |
| 788 |
} |
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
protected function getSearchProfiles() { |
| 795 |
|
| 796 |
$nsAllSet = array_keys( $this->searchConfig->searchableNamespaces() ); |
| 797 |
$defaultNs = $this->searchConfig->defaultNamespaces(); |
| 798 |
$profiles = [ |
| 799 |
'default' => [ |
| 800 |
'message' => 'searchprofile-articles', |
| 801 |
'tooltip' => 'searchprofile-articles-tooltip', |
| 802 |
'namespaces' => $defaultNs, |
| 803 |
'namespace-messages' => $this->searchConfig->namespacesAsText( |
| 804 |
$defaultNs |
| 805 |
), |
| 806 |
], |
| 807 |
'images' => [ |
| 808 |
'message' => 'searchprofile-images', |
| 809 |
'tooltip' => 'searchprofile-images-tooltip', |
| 810 |
'namespaces' => [ NS_FILE ], |
| 811 |
], |
| 812 |
'all' => [ |
| 813 |
'message' => 'searchprofile-everything', |
| 814 |
'tooltip' => 'searchprofile-everything-tooltip', |
| 815 |
'namespaces' => $nsAllSet, |
| 816 |
], |
| 817 |
'advanced' => [ |
| 818 |
'message' => 'searchprofile-advanced', |
| 819 |
'tooltip' => 'searchprofile-advanced-tooltip', |
| 820 |
'namespaces' => self::NAMESPACES_CURRENT, |
| 821 |
] |
| 822 |
]; |
| 823 |
|
| 824 |
$this->getHookRunner()->onSpecialSearchProfiles( $profiles ); |
| 825 |
|
| 826 |
foreach ( $profiles as &$data ) { |
| 827 |
if ( !is_array( $data['namespaces'] ) ) { |
| 828 |
continue; |
| 829 |
} |
| 830 |
sort( $data['namespaces'] ); |
| 831 |
} |
| 832 |
|
| 833 |
return $profiles; |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
public function getSearchEngine() { |
| 842 |
if ( $this->searchEngine === null ) { |
| 843 |
$this->searchEngine = $this->searchEngineFactory->create( $this->searchEngineType ); |
| 844 |
} |
| 845 |
|
| 846 |
return $this->searchEngine; |
| 847 |
} |
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
public function getProfile() { |
| 854 |
return $this->profile; |
| 855 |
} |
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
public function getNamespaces() { |
| 862 |
return $this->namespaces; |
| 863 |
} |
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
public function setExtraParam( $key, $value ) { |
| 875 |
$this->extraParams[$key] = $value; |
| 876 |
} |
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
public function getPrefix() { |
| 887 |
return $this->mPrefix; |
| 888 |
} |
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
private function prevNextLinks( |
| 898 |
?int $totalRes, |
| 899 |
?ISearchResultSet $textMatches, |
| 900 |
string $term, |
| 901 |
string $class, |
| 902 |
OutputPage $out |
| 903 |
) { |
| 904 |
if ( $totalRes > $this->limit || $this->offset ) { |
| 905 |
|
| 906 |
|
| 907 |
if ( $textMatches && $textMatches->getOffset() !== null ) { |
| 908 |
$offset = $textMatches->getOffset(); |
| 909 |
} else { |
| 910 |
$offset = $this->offset; |
| 911 |
} |
| 912 |
|
| 913 |
|
| 914 |
$newSearchTerm = $term; |
| 915 |
if ( $textMatches && $textMatches->hasRewrittenQuery() ) { |
| 916 |
$newSearchTerm = $textMatches->getQueryAfterRewrite(); |
| 917 |
} |
| 918 |
|
| 919 |
$prevNext = |
| 920 |
|
| 921 |
$this->buildPrevNextNavigation( $offset, $this->limit, |
| 922 |
$this->powerSearchOptions() + [ 'search' => $newSearchTerm ], |
| 923 |
$this->limit + $this->offset >= $totalRes ); |
| 924 |
$out->addHTML( "<div class='{$class}'>{$prevNext}</div>\n" ); |
| 925 |
} |
| 926 |
} |
| 927 |
|
| 928 |
protected function getGroupName() { |
| 929 |
return 'pages'; |
| 930 |
} |
| 931 |
} |
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
class_alias( SpecialSearch::class, 'SpecialSearch' ); |