3 namespace MediaWiki\Widget\Search
;
5 use MediaWiki\Interwiki\InterwikiLookup
;
6 use MediaWiki\Linker\LinkRenderer
;
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 */
28 public function __construct(
29 SpecialSearch
$specialSearch,
30 SearchResultWidget
$resultWidget,
31 LinkRenderer
$linkRenderer,
32 InterwikiLookup
$iwLookup
34 $this->specialSearch
= $specialSearch;
35 $this->resultWidget
= $resultWidget;
36 $this->linkRenderer
= $linkRenderer;
37 $this->iwLookup
= $iwLookup;
41 * @param string $term User provided search term
42 * @param SearchResultSet|SearchResultSet[] $resultSets List of interwiki
46 public function render( $term, $resultSets ) {
47 if ( !is_array( $resultSets ) ) {
48 $resultSets = [ $resultSets ];
51 $this->loadCustomCaptions();
54 foreach ( $resultSets as $resultSet ) {
55 $result = $resultSet->next();
57 if ( !$result->isBrokenTitle() ) {
58 $iwResults[$result->getTitle()->getInterwiki()][] = $result;
60 $result = $resultSet->next();
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
70 foreach ( $results as $result ) {
71 $out .= $this->resultWidget
->render( $result, $term, $position++
);
77 "<div id='mw-search-interwiki'>" .
78 "<div id='mw-search-interwiki-caption'>" .
79 $this->specialSearch
->msg( 'search-interwiki-caption' )->text() .
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
92 protected function headerHtml( $iwPrefix, $term ) {
93 if ( isset( $this->customCaptions
[$iwPrefix] ) ) {
94 $caption = $this->customCaptions
[$iwPrefix];
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(),
110 "<div class='mw-search-interwiki-project'>" .
111 "<span class='mw-search-interwiki-more'>{$searchLink}</span>" .
116 protected function loadCustomCaptions() {
117 if ( $this->customCaptions
!== null ) {
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];