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  $original = $term;
79 
80  return $this->specialSearch->msg( 'search-rewritten' )
81  ->rawParams( $rewritten )
82  ->params( $original )
83  ->escaped();
84  }
85 
94  protected function suggestionHtml( ISearchResultSet $resultSet ) {
95  $params = [
96  'search' => $resultSet->getSuggestionQuery(),
97  'fulltext' => 1,
98  ];
99  $stParams = array_merge( $params, $this->specialSearch->powerSearchOptions() );
100 
101  $snippet = $resultSet->getSuggestionSnippet();
102  if ( $snippet === '' || $snippet === null ) {
103  // This should never happen. But if it did happen we would render
104  // links as `Special:Search` which is even more useless. Since this
105  // was only documented but not enforced previously emit a
106  // deprecation warning and in the future we can simply fail on bad
107  // inputs
109  get_class( $resultSet ) . '::getSuggestionSnippet returning empty snippet ' .
110  'was deprecated in MediaWiki 1.35',
111  '1.34', false, false
112  );
113  $snippet = $resultSet->getSuggestionSnippet();
114  }
115  $suggest = $this->specialSearch->getLinkRenderer()->makeKnownLink(
116  $this->specialSearch->getPageTitle(),
117  $snippet,
118  [ 'id' => 'mw-search-DYM-suggestion' ],
119  $stParams
120  );
121 
122  return $this->specialSearch->msg( 'search-suggest' )
123  ->rawParams( $suggest )->parse();
124  }
125 }
wfDeprecatedMsg( $msg, $version=false, $component=false, $callerOffset=2)
Log a deprecation warning with arbitrary message text.
This class is a collection of static functions that serve two purposes:
Definition: Html.php:51
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:214
Renders a suggested search for the user, or tells the user a suggested search was run instead of the ...
rewrittenHtml( $term, ISearchResultSet $resultSet)
Generates HTML shown to user when their query has been internally rewritten, and the results of the r...
render( $term, ISearchResultSet $resultSet)
suggestionHtml(ISearchResultSet $resultSet)
Generates HTML shown to the user when we have a suggestion about a query that might give more/better ...
implements Special:Search - Run text & title search and display the output
A set of SearchEngine results.
hasRewrittenQuery()
Some search modes will run an alternative query that it thinks gives a better result than the provide...
hasSuggestion()
Some search modes return a suggested alternate term if there are no exact hits.