MediaWiki  1.29.1
DidYouMeanWidget.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Linker;
8 
15  protected $specialSearch;
16 
18  $this->specialSearch = $specialSearch;
19  }
20 
26  public function render( $term, SearchResultSet $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, SearchResultSet $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  'fultext' => 1,
53  ];
54  $stParams = array_merge( $params, $this->specialSearch->powerSearchOptions() );
55 
56  $rewritten = Linker::linkKnown(
57  $this->specialSearch->getPageTitle(),
58  $resultSet->getQueryAfterRewriteSnippet() ?: null,
59  [ 'id' => 'mw-search-DYM-rewritten' ],
60  $stParams
61  );
62 
63  $stParams['search'] = $term;
64  $stParams['runsuggestion'] = 0;
65  $original = Linker::linkKnown(
66  $this->specialSearch->getPageTitle(),
67  htmlspecialchars( $term, ENT_QUOTES, 'UTF-8' ),
68  [ 'id' => 'mwsearch-DYM-original' ],
69  $stParams
70  );
71 
72  return $this->specialSearch->msg( 'search-rewritten' )
73  ->rawParams( $rewritten, $original )
74  ->escaped();
75  }
76 
85  protected function suggestionHtml( SearchResultSet $resultSet ) {
86  $params = [
87  'search' => $resultSet->getSuggestionQuery(),
88  'fulltext' => 1,
89  ];
90  $stParams = array_merge( $params, $this->specialSearch->powerSearchOptions() );
91 
92  $suggest = Linker::linkKnown(
93  $this->specialSearch->getPageTitle(),
94  $resultSet->getSuggestionSnippet() ?: null,
95  [ 'id' => 'mw-search-DYM-suggestion' ],
96  $stParams
97  );
98 
99  return $this->specialSearch->msg( 'search-suggest' )
100  ->rawParams( $suggest )->parse();
101  }
102 }
Linker
Some internal bits split of from Skin.php.
Definition: Linker.php:34
SearchResultSet\getQueryAfterRewrite
getQueryAfterRewrite()
Definition: SearchResultSet.php:116
MediaWiki\Widget\Search
Definition: BasicSearchResultSetWidget.php:3
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
MediaWiki\Widget\Search\DidYouMeanWidget\suggestionHtml
suggestionHtml(SearchResultSet $resultSet)
Generates HTML shown to the user when we have a suggestion about a query that might give more/better ...
Definition: DidYouMeanWidget.php:85
$params
$params
Definition: styleTest.css.php:40
Linker\linkKnown
static linkKnown( $target, $html=null, $customAttribs=[], $query=[], $options=[ 'known'])
Identical to link(), except $options defaults to 'known'.
Definition: Linker.php:159
$term
external whereas SearchGetNearMatch runs after $term
Definition: hooks.txt:2759
MediaWiki\Widget\Search\DidYouMeanWidget\render
render( $term, SearchResultSet $resultSet)
Definition: DidYouMeanWidget.php:26
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$html
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition: hooks.txt:1956
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
SearchResultSet
Definition: SearchResultSet.php:27
SearchResultSet\hasSuggestion
hasSuggestion()
Some search modes return a suggested alternate term if there are no exact hits.
Definition: SearchResultSet.php:134
SearchResultSet\getSuggestionSnippet
getSuggestionSnippet()
Definition: SearchResultSet.php:148
SpecialSearch
implements Special:Search - Run text & title search and display the output
Definition: SpecialSearch.php:38
MediaWiki\Widget\Search\DidYouMeanWidget\rewrittenHtml
rewrittenHtml( $term, SearchResultSet $resultSet)
Generates HTML shown to user when their query has been internally rewritten, and the results of the r...
Definition: DidYouMeanWidget.php:47
SearchResultSet\hasRewrittenQuery
hasRewrittenQuery()
Some search modes will run an alternative query that it thinks gives a better result than the provide...
Definition: SearchResultSet.php:108
SearchResultSet\getQueryAfterRewriteSnippet
getQueryAfterRewriteSnippet()
Definition: SearchResultSet.php:124
SearchResultSet\getSuggestionQuery
getSuggestionQuery()
Definition: SearchResultSet.php:141