Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / includes / widget / search / InterwikiSearchResultSetWidget.php
blobc7384835621939e77a40e3806966355185708da2
1 <?php
3 namespace MediaWiki\Widget\Search;
5 use MediaWiki\Interwiki\InterwikiLookup;
6 use MediaWiki\Linker\LinkRenderer;
7 use SearchResultSet;
8 use SpecialSearch;
9 use Title;
11 /**
12 * Renders one or more SearchResultSets into a sidebar grouped by
13 * interwiki prefix. Includes a per-wiki header indicating where
14 * the results are from.
16 class InterwikiSearchResultSetWidget {
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;
28 public function __construct(
29 SpecialSearch $specialSearch,
30 SearchResultWidget $resultWidget,
31 LinkRenderer $linkRenderer,
32 InterwikiLookup $iwLookup
33 ) {
34 $this->specialSearch = $specialSearch;
35 $this->resultWidget = $resultWidget;
36 $this->linkRenderer = $linkRenderer;
37 $this->iwLookup = $iwLookup;
40 /**
41 * @param string $term User provided search term
42 * @param SearchResultSet|SearchResultSet[] $resultSets List of interwiki
43 * results to render.
44 * @return string HTML
46 public function render( $term, $resultSets ) {
47 if ( !is_array( $resultSets ) ) {
48 $resultSets = [ $resultSets ];
51 $this->loadCustomCaptions();
53 $iwResults = [];
54 foreach ( $resultSets as $resultSet ) {
55 $result = $resultSet->next();
56 while ( $result ) {
57 if ( !$result->isBrokenTitle() ) {
58 $iwResults[$result->getTitle()->getInterwiki()][] = $result;
60 $result = $resultSet->next();
64 $out = '';
65 foreach ( $iwResults as $iwPrefix => $results ) {
66 $out .= $this->headerHtml( $iwPrefix, $term );
67 $out .= "<ul class='mw-search-iwresults'>";
68 // TODO: Assumes interwiki results are never paginated
69 $position = 0;
70 foreach ( $results as $result ) {
71 $out .= $this->resultWidget->render( $result, $term, $position++ );
73 $out .= "</ul>";
76 return
77 "<div id='mw-search-interwiki'>" .
78 "<div id='mw-search-interwiki-caption'>" .
79 $this->specialSearch->msg( 'search-interwiki-caption' )->text() .
80 '</div>' .
81 $out .
82 "</div>";
85 /**
86 * Generates an appropriate HTML header for the given interwiki prefix
88 * @param string $iwPrefix Interwiki prefix of wiki to show header for
89 * @param string $term User provided search term
90 * @return string HTML
92 protected function headerHtml( $iwPrefix, $term ) {
93 if ( isset( $this->customCaptions[$iwPrefix] ) ) {
94 $caption = $this->customCaptions[$iwPrefix];
95 } else {
96 $interwiki = $this->iwLookup->fetch( $iwPrefix );
97 $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
98 $caption = $this->specialSearch->msg( 'search-interwiki-default', $parsed['host'] )->text();
100 $searchLink = $this->linkRenderer->makeLink(
101 Title::newFromText( "$iwPrefix:Special:Search" ),
102 $this->specialSearch->msg( 'search-interwiki-more' )->text(),
105 'search' => $term,
106 'fulltext' => 1,
109 return
110 "<div class='mw-search-interwiki-project'>" .
111 "<span class='mw-search-interwiki-more'>{$searchLink}</span>" .
112 $caption .
113 "</div>";
116 protected function loadCustomCaptions() {
117 if ( $this->customCaptions !== null ) {
118 return;
121 $this->customCaptions = [];
122 $customLines = explode( "\n", $this->specialSearch->msg( 'search-interwiki-custom' )->text() );
123 foreach ( $customLines as $line ) {
124 $parts = explode( ':', $line, 2 );
125 if ( count( $parts ) === 2 ) {
126 $this->customCaptions[$parts[0]] = $parts[1];