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  'was deprecated in MediaWiki 1.35',
66  '1.34', false, false
67  );
68  $snippet = $resultSet->getQueryAfterRewrite();
69  }
70  $rewritten = $linkRenderer->makeKnownLink(
71  $this->specialSearch->getPageTitle(),
72  $snippet,
73  [ 'id' => 'mw-search-DYM-rewritten' ],
74  $stParams
75  );
76 
77  $stParams['search'] = $term;
78  $stParams['runsuggestion'] = 0;
79  $original = $linkRenderer->makeKnownLink(
80  $this->specialSearch->getPageTitle(),
81  $term,
82  [ 'id' => 'mw-search-DYM-original' ],
83  $stParams
84  );
85 
86  return $this->specialSearch->msg( 'search-rewritten' )
87  ->rawParams( $rewritten, $original )
88  ->escaped();
89  }
90 
99  protected function suggestionHtml( ISearchResultSet $resultSet ) {
100  $params = [
101  'search' => $resultSet->getSuggestionQuery(),
102  'fulltext' => 1,
103  ];
104  $stParams = array_merge( $params, $this->specialSearch->powerSearchOptions() );
105 
106  $snippet = $resultSet->getSuggestionSnippet();
107  if ( $snippet === '' || $snippet === null ) {
108  // This should never happen. But if it did happen we would render
109  // links as `Special:Search` which is even more useless. Since this
110  // was only documented but not enforced previously emit a
111  // deprecation warning and in the future we can simply fail on bad
112  // inputs
114  get_class( $resultSet ) . '::getSuggestionSnippet returning empty snippet ' .
115  'was deprecated in MediaWiki 1.35',
116  '1.34', false, false
117  );
118  $snippet = $resultSet->getSuggestionSnippet();
119  }
120  $suggest = $this->specialSearch->getLinkRenderer()->makeKnownLink(
121  $this->specialSearch->getPageTitle(),
122  $snippet,
123  [ 'id' => 'mw-search-DYM-suggestion' ],
124  $stParams
125  );
126 
127  return $this->specialSearch->msg( 'search-suggest' )
128  ->rawParams( $suggest )->parse();
129  }
130 }
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:99
MediaWiki\Search\SearchWidgets\DidYouMeanWidget\$specialSearch
SpecialSearch $specialSearch
Definition: DidYouMeanWidget.php:14
ISearchResultSet\getQueryAfterRewrite
getQueryAfterRewrite()
ISearchResultSet\getSuggestionSnippet
getSuggestionSnippet()
wfDeprecatedMsg
wfDeprecatedMsg( $msg, $version=false, $component=false, $callerOffset=2)
Log a deprecation warning with arbitrary message text.
Definition: GlobalFunctions.php:1066
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:12
SpecialSearch
implements Special:Search - Run text & title search and display the output
Definition: SpecialSearch.php:41
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