Move ILoadBalancer to Rdbms namespace
[mediawiki.git] / includes / widget / search / InterwikiSearchResultWidget.php
blob44229b83a5e5eba0c58324802da8b15f46deefbe
1 <?php
3 namespace MediaWiki\Widget\Search;
5 use HtmlArmor;
6 use MediaWiki\Linker\LinkRenderer;
7 use SearchResult;
8 use SpecialSearch;
9 use Title;
10 use Html;
12 /**
13 * Renders an enhanced interwiki result
15 class InterwikiSearchResultWidget implements SearchResultWidget {
16 /** @var SpecialSearch */
17 protected $specialSearch;
18 /** @var LinkRenderer */
19 protected $linkRenderer;
20 /** @var $iwPrefixDisplayTypes */
21 protected $iwPrefixDisplayTypes;
23 public function __construct( SpecialSearch $specialSearch, LinkRenderer $linkRenderer ) {
24 $this->specialSearch = $specialSearch;
25 $this->linkRenderer = $linkRenderer;
26 $this->iwPrefixDisplayTypes = $specialSearch->getConfig()->get( 'InterwikiPrefixDisplayTypes' );
29 /**
30 * @param SearchResult $result The result to render
31 * @param string $terms Terms to be highlighted (@see SearchResult::getTextSnippet)
32 * @param int $position The result position, including offset
33 * @return string HTML
35 public function render( SearchResult $result, $terms, $position ) {
37 $title = $result->getTitle();
38 $iwPrefix = $result->getTitle()->getInterwiki();
39 $titleSnippet = $result->getTitleSnippet();
40 $snippet = $result->getTextSnippet( $terms );
41 $displayType = isset( $this->iwPrefixDisplayTypes[$iwPrefix] )
42 ? $this->iwPrefixDisplayTypes[$iwPrefix]
43 : "";
45 if ( $titleSnippet ) {
46 $titleSnippet = new HtmlArmor( $titleSnippet );
47 } else {
48 $titleSnippet = null;
51 $link = $this->linkRenderer->makeLink( $title, $titleSnippet );
53 $redirectTitle = $result->getRedirectTitle();
54 $redirect = '';
55 if ( $redirectTitle !== null ) {
57 $redirectText = $result->getRedirectSnippet();
59 if ( $redirectText ) {
60 $redirectText = new HtmlArmor( $redirectText );
61 } else {
62 $redirectText = null;
65 $redirect = Html::rawElement( 'span', [ 'class' => 'iw-result__redirect' ],
66 $this->specialSearch->msg( 'search-redirect' )->rawParams(
67 $this->linkRenderer->makeLink( $redirectTitle, $redirectText )
68 )->escaped()
72 switch ( $displayType ) {
73 case 'definition':
74 return "<div class='iw-result__content'>" .
75 "<span class='iw-result__title'>{$link} {$redirect}: </span>" .
76 $snippet .
77 "</div>";
78 case 'quotation':
79 return "<div class='iw-result__content'>{$snippet}</div>" .
80 "<div class='iw-result__title'>{$link} {$redirect}</div>";
81 default:
82 return "<div class='iw-result__title'>{$link} {$redirect}</div>" .
83 "<div class='iw-result__content'>{$snippet}</div>";