MediaWiki  master
DidYouMeanWidget.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Html;
7 use SpecialSearch;
8 
15  protected $specialSearch;
16 
18  $this->specialSearch = $specialSearch;
19  }
20 
26  public function render( $term, ISearchResultSet $resultSet ) {
27  if ( $resultSet->hasRewrittenQuery() ) {
28  $html = $this->rewrittenHtml( $term, $resultSet );
29  } elseif ( $resultSet->hasSuggestion() ) {
30  $html = $this->suggestionHtml( $resultSet );
31  } else {
32  return '';
33  }
34 
35  return Html::rawElement( 'div', [ 'class' => 'searchdidyoumean' ], $html );
36  }
37 
47  protected function rewrittenHtml( $term, ISearchResultSet $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  'fulltext' => 1,
53  ];
54  $stParams = array_merge( $params, $this->specialSearch->powerSearchOptions() );
55 
56  $linkRenderer = $this->specialSearch->getLinkRenderer();
57  $snippet = $resultSet->getQueryAfterRewriteSnippet();
58  if ( $snippet === '' || $snippet === null ) {
59  // This should never happen. But if it did happen we would render
60  // links as `Special:Search` which is even more useless. Since this
61  // was only documented but not enforced previously emit a
62  // deprecation warning and in the future we can simply fail on bad
63  // inputs
65  get_class( $resultSet ) . '::getQueryAfterRewriteSnippet returning empty snippet ' .
66  'was deprecated in MediaWiki 1.35',
67  '1.34', false, false
68  );
69  $snippet = $resultSet->getQueryAfterRewrite();
70  }
71  $rewritten = $linkRenderer->makeKnownLink(
72  $this->specialSearch->getPageTitle(),
73  $snippet,
74  [ 'id' => 'mw-search-DYM-rewritten' ],
75  $stParams
76  );
77 
78  $stParams['search'] = $term;
79  $stParams['runsuggestion'] = 0;
80  $original = $linkRenderer->makeKnownLink(
81  $this->specialSearch->getPageTitle(),
82  $term,
83  [ 'id' => 'mw-search-DYM-original' ],
84  $stParams
85  );
86 
87  return $this->specialSearch->msg( 'search-rewritten' )
88  ->rawParams( $rewritten, $original )
89  ->escaped();
90  }
91 
100  protected function suggestionHtml( ISearchResultSet $resultSet ) {
101  $params = [
102  'search' => $resultSet->getSuggestionQuery(),
103  'fulltext' => 1,
104  ];
105  $stParams = array_merge( $params, $this->specialSearch->powerSearchOptions() );
106 
107  $snippet = $resultSet->getSuggestionSnippet();
108  if ( $snippet === '' || $snippet === null ) {
109  // This should never happen. But if it did happen we would render
110  // links as `Special:Search` which is even more useless. Since this
111  // was only documented but not enforced previously emit a
112  // deprecation warning and in the future we can simply fail on bad
113  // inputs
115  get_class( $resultSet ) . '::getSuggestionSnippet returning empty snippet ' .
116  'was deprecated in MediaWiki 1.35',
117  '1.34', false, false
118  );
119  $snippet = $resultSet->getSuggestionSnippet();
120  }
121  $suggest = $this->specialSearch->getLinkRenderer()->makeKnownLink(
122  $this->specialSearch->getPageTitle(),
123  $snippet,
124  [ 'id' => 'mw-search-DYM-suggestion' ],
125  $stParams
126  );
127 
128  return $this->specialSearch->msg( 'search-suggest' )
129  ->rawParams( $suggest )->parse();
130  }
131 }
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:13
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:17
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:100
MediaWiki\Search\SearchWidgets\DidYouMeanWidget\$specialSearch
SpecialSearch $specialSearch
Definition: DidYouMeanWidget.php:15
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:1028
MediaWiki\Search\SearchWidgets\DidYouMeanWidget\render
render( $term, ISearchResultSet $resultSet)
Definition: DidYouMeanWidget.php:26
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
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:213
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:47
Html
This class is a collection of static functions that serve two purposes:
Definition: Html.php:50