InterwikiSearchResultWidget.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace MediaWiki\Widget\Search;
  3. use HtmlArmor;
  4. use MediaWiki\Linker\LinkRenderer;
  5. use SearchResult;
  6. use SpecialSearch;
  7. use Html;
  8. /**
  9. * Renders an enhanced interwiki result
  10. */
  11. class InterwikiSearchResultWidget implements SearchResultWidget {
  12. /** @var SpecialSearch */
  13. protected $specialSearch;
  14. /** @var LinkRenderer */
  15. protected $linkRenderer;
  16. public function __construct( SpecialSearch $specialSearch, LinkRenderer $linkRenderer ) {
  17. $this->specialSearch = $specialSearch;
  18. $this->linkRenderer = $linkRenderer;
  19. }
  20. /**
  21. * @param SearchResult $result The result to render
  22. * @param int $position The result position, including offset
  23. * @return string HTML
  24. */
  25. public function render( SearchResult $result, $position ) {
  26. $title = $result->getTitle();
  27. $titleSnippet = $result->getTitleSnippet();
  28. $snippet = $result->getTextSnippet();
  29. if ( $titleSnippet ) {
  30. $titleSnippet = new HtmlArmor( $titleSnippet );
  31. } else {
  32. $titleSnippet = null;
  33. }
  34. $link = $this->linkRenderer->makeLink( $title, $titleSnippet );
  35. $redirectTitle = $result->getRedirectTitle();
  36. $redirect = '';
  37. if ( $redirectTitle !== null ) {
  38. $redirectText = $result->getRedirectSnippet();
  39. if ( $redirectText ) {
  40. $redirectText = new HtmlArmor( $redirectText );
  41. } else {
  42. $redirectText = null;
  43. }
  44. $redirect = Html::rawElement( 'span', [ 'class' => 'iw-result__redirect' ],
  45. $this->specialSearch->msg( 'search-redirect' )->rawParams(
  46. $this->linkRenderer->makeLink( $redirectTitle, $redirectText )
  47. )->escaped()
  48. );
  49. }
  50. return Html::rawElement( 'div', [ 'class' => 'iw-result__title' ], $link . ' ' . $redirect ) .
  51. Html::rawElement( 'div', [ 'class' => 'iw-result__content' ], $snippet );
  52. }
  53. }