MediaWiki  1.34.0
DidYouMeanWidget.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use HtmlArmor;
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 "<div class='searchdidyoumean'>$html</div>";
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  $rewritten = $linkRenderer->makeKnownLink(
59  $this->specialSearch->getPageTitle(),
60  $snippet ? new HtmlArmor( $snippet ) : null,
61  [ 'id' => 'mw-search-DYM-rewritten' ],
62  $stParams
63  );
64 
65  $stParams['search'] = $term;
66  $stParams['runsuggestion'] = 0;
67  $original = $linkRenderer->makeKnownLink(
68  $this->specialSearch->getPageTitle(),
69  $term,
70  [ 'id' => 'mw-search-DYM-original' ],
71  $stParams
72  );
73 
74  return $this->specialSearch->msg( 'search-rewritten' )
75  ->rawParams( $rewritten, $original )
76  ->escaped();
77  }
78 
87  protected function suggestionHtml( ISearchResultSet $resultSet ) {
88  $params = [
89  'search' => $resultSet->getSuggestionQuery(),
90  'fulltext' => 1,
91  ];
92  $stParams = array_merge( $params, $this->specialSearch->powerSearchOptions() );
93 
94  $snippet = $resultSet->getSuggestionSnippet();
95  $suggest = $this->specialSearch->getLinkRenderer()->makeKnownLink(
96  $this->specialSearch->getPageTitle(),
97  $snippet ? new HtmlArmor( $snippet ) : null,
98  [ 'id' => 'mw-search-DYM-suggestion' ],
99  $stParams
100  );
101 
102  return $this->specialSearch->msg( 'search-suggest' )
103  ->rawParams( $suggest )->parse();
104  }
105 }
ISearchResultSet\hasSuggestion
hasSuggestion()
Some search modes return a suggested alternate term if there are no exact hits.
HtmlArmor
Marks HTML that shouldn't be escaped.
Definition: HtmlArmor.php:28
ISearchResultSet\hasRewrittenQuery
hasRewrittenQuery()
Some search modes will run an alternative query that it thinks gives a better result than the provide...
MediaWiki\Widget\Search\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:87
MediaWiki\Widget\Search
Definition: BasicSearchResultSetWidget.php:3
MediaWiki\Widget\Search\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
ISearchResultSet\getQueryAfterRewrite
getQueryAfterRewrite()
ISearchResultSet\getSuggestionSnippet
getSuggestionSnippet()
ISearchResultSet\getQueryAfterRewriteSnippet
getQueryAfterRewriteSnippet()
MediaWiki\Widget\Search\DidYouMeanWidget\__construct
__construct(SpecialSearch $specialSearch)
Definition: DidYouMeanWidget.php:17
MediaWiki\Widget\Search\DidYouMeanWidget
Renders a suggested search for the user, or tells the user a suggested search was run instead of the ...
Definition: DidYouMeanWidget.php:13
MediaWiki\Widget\Search\DidYouMeanWidget\$specialSearch
SpecialSearch $specialSearch
Definition: DidYouMeanWidget.php:15
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\Widget\Search\DidYouMeanWidget\render
render( $term, ISearchResultSet $resultSet)
Definition: DidYouMeanWidget.php:26