MediaWiki  master
DidYouMeanWidget.php
Go to the documentation of this file.
1 <?php
2 
4 
6 use SpecialSearch;
7 
14  protected $specialSearch;
15 
17  $this->specialSearch = $specialSearch;
18  }
19 
25  public function render( $term, ISearchResultSet $resultSet ) {
26  if ( $resultSet->hasRewrittenQuery() ) {
27  $html = $this->rewrittenHtml( $term, $resultSet );
28  } elseif ( $resultSet->hasSuggestion() ) {
29  $html = $this->suggestionHtml( $resultSet );
30  } else {
31  return '';
32  }
33 
34  return "<div class='searchdidyoumean'>$html</div>";
35  }
36 
46  protected function rewrittenHtml( $term, ISearchResultSet $resultSet ) {
47  $params = [
48  'search' => $resultSet->getQueryAfterRewrite(),
49  // Don't magic this link into a 'go' link, it should always
50  // show search results.
51  'fulltext' => 1,
52  ];
53  $stParams = array_merge( $params, $this->specialSearch->powerSearchOptions() );
54 
55  $linkRenderer = $this->specialSearch->getLinkRenderer();
56  $snippet = $resultSet->getQueryAfterRewriteSnippet();
57  if ( $snippet === '' || $snippet === null ) {
58  // This should never happen. But if it did happen we would render
59  // links as `Special:Search` which is even more useless. Since this
60  // was only documented but not enforced previously emit a
61  // deprecation warning and in the future we can simply fail on bad
62  // inputs
64  get_class( $resultSet ) . '::getQueryAfterRewriteSnippet returning empty snippet',
65  '1.34'
66  );
67  $snippet = $resultSet->getQueryAfterRewrite();
68  }
69  $rewritten = $linkRenderer->makeKnownLink(
70  $this->specialSearch->getPageTitle(),
71  $snippet,
72  [ 'id' => 'mw-search-DYM-rewritten' ],
73  $stParams
74  );
75 
76  $stParams['search'] = $term;
77  $stParams['runsuggestion'] = 0;
78  $original = $linkRenderer->makeKnownLink(
79  $this->specialSearch->getPageTitle(),
80  $term,
81  [ 'id' => 'mw-search-DYM-original' ],
82  $stParams
83  );
84 
85  return $this->specialSearch->msg( 'search-rewritten' )
86  ->rawParams( $rewritten, $original )
87  ->escaped();
88  }
89 
98  protected function suggestionHtml( ISearchResultSet $resultSet ) {
99  $params = [
100  'search' => $resultSet->getSuggestionQuery(),
101  'fulltext' => 1,
102  ];
103  $stParams = array_merge( $params, $this->specialSearch->powerSearchOptions() );
104 
105  $snippet = $resultSet->getSuggestionSnippet();
106  if ( $snippet === '' || $snippet === null ) {
107  // This should never happen. But if it did happen we would render
108  // links as `Special:Search` which is even more useless. Since this
109  // was only documented but not enforced previously emit a
110  // deprecation warning and in the future we can simply fail on bad
111  // inputs
112  wfDeprecated(
113  get_class( $resultSet ) . '::getSuggestionSnippet returning empty snippet',
114  '1.34'
115  );
116  $snippet = $resultSet->getSuggestionSnippet();
117  }
118  $suggest = $this->specialSearch->getLinkRenderer()->makeKnownLink(
119  $this->specialSearch->getPageTitle(),
120  $snippet,
121  [ 'id' => 'mw-search-DYM-suggestion' ],
122  $stParams
123  );
124 
125  return $this->specialSearch->msg( 'search-suggest' )
126  ->rawParams( $suggest )->parse();
127  }
128 }
MediaWiki\Search\SearchWidgets\DidYouMeanWidget
Renders a suggested search for the user, or tells the user a suggested search was run instead of the ...
Definition: DidYouMeanWidget.php:12
ISearchResultSet\hasSuggestion
hasSuggestion()
Some search modes return a suggested alternate term if there are no exact hits.
ISearchResultSet\hasRewrittenQuery
hasRewrittenQuery()
Some search modes will run an alternative query that it thinks gives a better result than the provide...
MediaWiki\Search\SearchWidgets\DidYouMeanWidget\__construct
__construct(SpecialSearch $specialSearch)
Definition: DidYouMeanWidget.php:16
MediaWiki\Search\SearchWidgets\DidYouMeanWidget\suggestionHtml
suggestionHtml(ISearchResultSet $resultSet)
Generates HTML shown to the user when we have a suggestion about a query that might give more/better ...
Definition: DidYouMeanWidget.php:98
MediaWiki\Search\SearchWidgets\DidYouMeanWidget\$specialSearch
SpecialSearch $specialSearch
Definition: DidYouMeanWidget.php:14
ISearchResultSet\getQueryAfterRewrite
getQueryAfterRewrite()
ISearchResultSet\getSuggestionSnippet
getSuggestionSnippet()
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Definition: GlobalFunctions.php:1030
MediaWiki\Search\SearchWidgets\DidYouMeanWidget\render
render( $term, ISearchResultSet $resultSet)
Definition: DidYouMeanWidget.php:25
ISearchResultSet\getQueryAfterRewriteSnippet
getQueryAfterRewriteSnippet()
ISearchResultSet
A set of SearchEngine results.
Definition: ISearchResultSet.php:11
SpecialSearch
implements Special:Search - Run text & title search and display the output
Definition: SpecialSearch.php:38
ISearchResultSet\getSuggestionQuery
getSuggestionQuery()
MediaWiki\Search\SearchWidgets
Definition: BasicSearchResultSetWidget.php:3
MediaWiki\Search\SearchWidgets\DidYouMeanWidget\rewrittenHtml
rewrittenHtml( $term, ISearchResultSet $resultSet)
Generates HTML shown to user when their query has been internally rewritten, and the results of the r...
Definition: DidYouMeanWidget.php:46