Move ILoadBalancer to Rdbms namespace
[mediawiki.git] / includes / widget / search / DidYouMeanWidget.php
blob3aee87bf3b366ed2149b4cac3171f1d5cd02fcbb
1 <?php
3 namespace MediaWiki\Widget\Search;
5 use Linker;
6 use SearchResultSet;
7 use SpecialSearch;
9 /**
10 * Renders a suggested search for the user, or tells the user
11 * a suggested search was run instead of the one provided.
13 class DidYouMeanWidget {
14 /** @var SpecialSearch */
15 protected $specialSearch;
17 public function __construct( SpecialSearch $specialSearch ) {
18 $this->specialSearch = $specialSearch;
21 /**
22 * @param string $term The user provided search term
23 * @param SearchResultSet $resultSet
24 * @return string HTML
26 public function render( $term, SearchResultSet $resultSet ) {
27 if ( $resultSet->hasRewrittenQuery() ) {
28 $html = $this->rewrittenHtml( $term, $resultSet );
29 } elseif ( $resultSet->hasSuggestion() ) {
30 $html = $this->suggestionHtml( $resultSet );
31 } else {
32 return '';
35 return "<div class='searchdidyoumean'>$html</div>";
38 /**
39 * Generates HTML shown to user when their query has been internally
40 * rewritten, and the results of the rewritten query are being returned.
42 * @param string $term The users search input
43 * @param SearchResultSet $resultSet The response to the search request
44 * @return string HTML Links the user to their original $term query, and the
45 * one suggested by $resultSet
47 protected function rewrittenHtml( $term, SearchResultSet $resultSet ) {
48 $params = [
49 'search' => $resultSet->getQueryAfterRewrite(),
50 // Don't magic this link into a 'go' link, it should always
51 // show search results.
52 'fultext' => 1,
54 $stParams = array_merge( $params, $this->specialSearch->powerSearchOptions() );
56 $rewritten = Linker::linkKnown(
57 $this->specialSearch->getPageTitle(),
58 $resultSet->getQueryAfterRewriteSnippet() ?: null,
59 [ 'id' => 'mw-search-DYM-rewritten' ],
60 $stParams
63 $stParams['search'] = $term;
64 $stParams['runsuggestion'] = 0;
65 $original = Linker::linkKnown(
66 $this->specialSearch->getPageTitle(),
67 htmlspecialchars( $term, ENT_QUOTES, 'UTF-8' ),
68 [ 'id' => 'mwsearch-DYM-original' ],
69 $stParams
72 return $this->specialSearch->msg( 'search-rewritten' )
73 ->rawParams( $rewritten, $original )
74 ->escaped();
77 /**
78 * Generates HTML shown to the user when we have a suggestion about
79 * a query that might give more/better results than their current
80 * query.
82 * @param SearchResultSet $resultSet
83 * @return string HTML
85 protected function suggestionHtml( SearchResultSet $resultSet ) {
86 $params = [
87 'search' => $resultSet->getSuggestionQuery(),
88 'fulltext' => 1,
90 $stParams = array_merge( $params, $this->specialSearch->powerSearchOptions() );
92 $suggest = Linker::linkKnown(
93 $this->specialSearch->getPageTitle(),
94 $resultSet->getSuggestionSnippet() ?: null,
95 [ 'id' => 'mw-search-DYM-suggestion' ],
96 $stParams
99 return $this->specialSearch->msg( 'search-suggest' )
100 ->rawParams( $suggest )->parse();