MediaWiki REL1_37
DidYouMeanWidget.php
Go to the documentation of this file.
1<?php
2
4
5use Html;
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}
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:49
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.