Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / includes / widget / search / SimpleSearchResultWidget.php
blob9fe816af5b8f1c27e29a9863e4583d47b310f7dd
1 <?php
3 namespace MediaWiki\Widget\Search;
5 use HtmlArmor;
6 use MediaWiki\Linker\LinkRenderer;
7 use SearchResult;
8 use SpecialSearch;
9 use Title;
11 /**
12 * Renders a simple one-line result
14 class SimpleSearchResultWidget implements SearchResultWidget {
15 /** @var SpecialSearch */
16 protected $specialSearch;
17 /** @var LinkRenderer */
18 protected $linkRenderer;
20 public function __construct( SpecialSearch $specialSearch, LinkRenderer $linkRenderer ) {
21 $this->specialSearch = $specialSearch;
22 $this->linkRenderer = $linkRenderer;
25 /**
26 * @param SearchResult $result The result to render
27 * @param string $terms Terms to be highlighted (@see SearchResult::getTextSnippet)
28 * @param int $position The result position, including offset
29 * @return string HTML
31 public function render( SearchResult $result, $terms, $position ) {
32 $title = $result->getTitle();
33 $titleSnippet = $result->getTitleSnippet();
34 if ( $titleSnippet ) {
35 $titleSnippet = new HtmlArmor( $titleSnippet );
36 } else {
37 $titleSnippet = null;
40 $link = $this->linkRenderer->makeLink( $title, $titleSnippet );
42 $redirectTitle = $result->getRedirectTitle();
43 $redirect = '';
44 if ( $redirectTitle !== null ) {
45 $redirectText = $result->getRedirectSnippet();
46 if ( $redirectText ) {
47 $redirectText = new HtmlArmor( $redirectText );
48 } else {
49 $redirectText = null;
51 $redirect =
52 "<span class='searchalttitle'>" .
53 $this->specialSearch->msg( 'search-redirect' )->rawParams(
54 $this->linkRenderer->makeLink( $redirectTitle, $redirectText )
55 )->text() .
56 "</span>";
59 return "<li>{$link} {$redirect}</li>";