3 namespace MediaWiki\Widget\Search
;
6 use MediaWiki\Linker\LinkRenderer
;
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' );
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
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]
45 if ( $titleSnippet ) {
46 $titleSnippet = new HtmlArmor( $titleSnippet );
51 $link = $this->linkRenderer
->makeLink( $title, $titleSnippet );
53 $redirectTitle = $result->getRedirectTitle();
55 if ( $redirectTitle !== null ) {
57 $redirectText = $result->getRedirectSnippet();
59 if ( $redirectText ) {
60 $redirectText = new HtmlArmor( $redirectText );
65 $redirect = Html
::rawElement( 'span', [ 'class' => 'iw-result__redirect' ],
66 $this->specialSearch
->msg( 'search-redirect' )->rawParams(
67 $this->linkRenderer
->makeLink( $redirectTitle, $redirectText )
72 switch ( $displayType ) {
74 return "<div class='iw-result__content'>" .
75 "<span class='iw-result__title'>{$link} {$redirect}: </span>" .
79 return "<div class='iw-result__content'>{$snippet}</div>" .
80 "<div class='iw-result__title'>{$link} {$redirect}</div>";
82 return "<div class='iw-result__title'>{$link} {$redirect}</div>" .
83 "<div class='iw-result__content'>{$snippet}</div>";