SimpleSearchResultSetWidget.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace MediaWiki\Widget\Search;
  3. use MediaWiki\Interwiki\InterwikiLookup;
  4. use MediaWiki\Linker\LinkRenderer;
  5. use ISearchResultSet;
  6. use SpecialSearch;
  7. use Title;
  8. use Html;
  9. /**
  10. * Renders one or more ISearchResultSets into a sidebar grouped by
  11. * interwiki prefix. Includes a per-wiki header indicating where
  12. * the results are from.
  13. *
  14. * @deprecated since 1.31. Use InterwikiSearchResultSetWidget
  15. */
  16. class SimpleSearchResultSetWidget implements SearchResultSetWidget {
  17. /** @var SpecialSearch */
  18. protected $specialSearch;
  19. /** @var SearchResultWidget */
  20. protected $resultWidget;
  21. /** @var string[]|null */
  22. protected $customCaptions;
  23. /** @var LinkRenderer */
  24. protected $linkRenderer;
  25. /** @var InterwikiLookup */
  26. protected $iwLookup;
  27. public function __construct(
  28. SpecialSearch $specialSearch,
  29. SearchResultWidget $resultWidget,
  30. LinkRenderer $linkRenderer,
  31. InterwikiLookup $iwLookup
  32. ) {
  33. wfDeprecated( __METHOD__, '1.31' );
  34. $this->specialSearch = $specialSearch;
  35. $this->resultWidget = $resultWidget;
  36. $this->linkRenderer = $linkRenderer;
  37. $this->iwLookup = $iwLookup;
  38. }
  39. /**
  40. * @param string $term User provided search term
  41. * @param ISearchResultSet|ISearchResultSet[] $resultSets List of interwiki
  42. * results to render.
  43. * @return string HTML
  44. */
  45. public function render( $term, $resultSets ) {
  46. if ( !is_array( $resultSets ) ) {
  47. $resultSets = [ $resultSets ];
  48. }
  49. $this->loadCustomCaptions();
  50. $iwResults = [];
  51. foreach ( $resultSets as $resultSet ) {
  52. foreach ( $resultSet as $result ) {
  53. if ( !$result->isBrokenTitle() ) {
  54. $iwResults[$result->getTitle()->getInterwiki()][] = $result;
  55. }
  56. }
  57. }
  58. $out = '';
  59. foreach ( $iwResults as $iwPrefix => $results ) {
  60. $out .= $this->headerHtml( $iwPrefix, $term );
  61. $out .= "<ul class='mw-search-iwresults'>";
  62. // TODO: Assumes interwiki results are never paginated
  63. $position = 0;
  64. foreach ( $results as $result ) {
  65. $out .= $this->resultWidget->render( $result, $term, $position++ );
  66. }
  67. $out .= "</ul>";
  68. }
  69. return "<div id='mw-search-interwiki'>" .
  70. "<div id='mw-search-interwiki-caption'>" .
  71. $this->specialSearch->msg( 'search-interwiki-caption' )->parse() .
  72. '</div>' .
  73. $out .
  74. "</div>";
  75. }
  76. /**
  77. * Generates an appropriate HTML header for the given interwiki prefix
  78. *
  79. * @param string $iwPrefix Interwiki prefix of wiki to show header for
  80. * @param string $term User provided search term
  81. * @return string HTML
  82. */
  83. protected function headerHtml( $iwPrefix, $term ) {
  84. if ( isset( $this->customCaptions[$iwPrefix] ) ) {
  85. $caption = $this->customCaptions[$iwPrefix];
  86. } else {
  87. $interwiki = $this->iwLookup->fetch( $iwPrefix );
  88. $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
  89. $caption = $this->specialSearch->msg( 'search-interwiki-default', $parsed['host'] )->escaped();
  90. }
  91. $href = Title::makeTitle( NS_SPECIAL, 'Search', null, $iwPrefix )->getLocalURL(
  92. [ 'search' => $term, 'fulltext' => 1 ]
  93. );
  94. $searchLink = Html::rawElement(
  95. 'a',
  96. [ 'href' => $href ],
  97. $this->specialSearch->msg( 'search-interwiki-more' )->escaped()
  98. );
  99. return "<div class='mw-search-interwiki-project'>" .
  100. "<span class='mw-search-interwiki-more'>{$searchLink}</span>" .
  101. $caption .
  102. "</div>";
  103. }
  104. protected function loadCustomCaptions() {
  105. if ( $this->customCaptions !== null ) {
  106. return;
  107. }
  108. $this->customCaptions = [];
  109. $customLines = explode( "\n", $this->specialSearch->msg( 'search-interwiki-custom' )->escaped() );
  110. foreach ( $customLines as $line ) {
  111. $parts = explode( ':', $line, 2 );
  112. if ( count( $parts ) === 2 ) {
  113. $this->customCaptions[$parts[0]] = $parts[1];
  114. }
  115. }
  116. }
  117. }