3 namespace MediaWiki\Widget\Search
;
5 use MediaWiki\Interwiki\InterwikiLookup
;
6 use MediaWiki\Linker\LinkRenderer
;
13 * Renders one or more SearchResultSets into a sidebar grouped by
14 * interwiki prefix. Includes a per-wiki header indicating where
15 * the results are from.
17 class InterwikiSearchResultSetWidget
implements SearchResultSetWidget
{
18 /** @var SpecialSearch */
19 protected $specialSearch;
20 /** @var SearchResultWidget */
21 protected $resultWidget;
22 /** @var string[]|null */
23 protected $customCaptions;
24 /** @var LinkRenderer */
25 protected $linkRenderer;
26 /** @var InterwikiLookup */
30 /** @var $iwPrefixDisplayTypes */
31 protected $iwPrefixDisplayTypes;
33 public function __construct(
34 SpecialSearch
$specialSearch,
35 SearchResultWidget
$resultWidget,
36 LinkRenderer
$linkRenderer,
37 InterwikiLookup
$iwLookup
39 $this->specialSearch
= $specialSearch;
40 $this->resultWidget
= $resultWidget;
41 $this->linkRenderer
= $linkRenderer;
42 $this->iwLookup
= $iwLookup;
43 $this->output
= $specialSearch->getOutput();
44 $this->iwPrefixDisplayTypes
= $specialSearch->getConfig()->get(
45 'InterwikiPrefixDisplayTypes'
49 * @param string $term User provided search term
50 * @param SearchResultSet|SearchResultSet[] $resultSets List of interwiki
54 public function render( $term, $resultSets ) {
55 if ( !is_array( $resultSets ) ) {
56 $resultSets = [ $resultSets ];
59 $this->loadCustomCaptions();
61 $this->output
->addModules( 'mediawiki.special.search.commonsInterwikiWidget' );
62 $this->output
->addModuleStyles( 'mediawiki.special.search.interwikiwidget.styles' );
65 foreach ( $resultSets as $resultSet ) {
66 $result = $resultSet->next();
68 if ( !$result->isBrokenTitle() ) {
69 $iwResults[$result->getTitle()->getInterwiki()][] = $result;
71 $result = $resultSet->next();
76 $iwResultListOutput = '';
78 foreach ( $iwResults as $iwPrefix => $results ) {
79 // TODO: Assumes interwiki results are never paginated
81 $iwResultItemOutput = '';
83 $iwDisplayType = isset( $this->iwPrefixDisplayTypes
[$iwPrefix] )
84 ?
$this->iwPrefixDisplayTypes
[$iwPrefix]
87 foreach ( $results as $result ) {
88 $iwResultItemOutput .= $this->resultWidget
->render( $result, $term, $position++
);
91 $headerHtml = $this->headerHtml( $term, $iwPrefix );
92 $footerHtml = $this->footerHtml( $term, $iwPrefix );
93 $iwResultListOutput .= Html
::rawElement( 'li',
95 'class' => 'iw-resultset iw-resultset--' . $iwDisplayType,
96 'data-iw-resultset-pos' => $iwResultSetPos
106 return Html
::rawElement(
108 [ 'id' => 'mw-interwiki-results' ],
111 [ 'class' => 'iw-headline' ],
112 $this->specialSearch
->msg( 'search-interwiki-caption' )->parse()
115 'ul', [ 'class' => 'iw-results', ], $iwResultListOutput
121 * Generates an appropriate HTML header for the given interwiki prefix
123 * @param string $term User provided search term
124 * @param string $iwPrefix Interwiki prefix of wiki to show header for
125 * @return string HTML
127 protected function headerHtml( $term, $iwPrefix ) {
129 $iwDisplayType = isset( $this->iwPrefixDisplayTypes
[$iwPrefix] )
130 ?
$this->iwPrefixDisplayTypes
[$iwPrefix]
133 if ( isset( $this->customCaptions
[$iwPrefix] ) ) {
134 /* customCaptions composed by loadCustomCaptions() with pre-escaped content.*/
135 $caption = $this->customCaptions
[$iwPrefix];
137 $interwiki = $this->iwLookup
->fetch( $iwPrefix );
138 $parsed = wfParseUrl( wfExpandUrl( $interwiki ?
$interwiki->getURL() : '/' ) );
139 $caption = $this->specialSearch
->msg( 'search-interwiki-default', $parsed['host'] )->escaped();
142 return Html
::rawElement( 'div', [ 'class' => 'iw-result__header' ],
143 Html
::rawElement( 'span', [ 'class' => 'iw-result__icon iw-result__icon--' . $iwDisplayType ] )
149 * Generates an HTML footer for the given interwiki prefix
151 * @param string $term User provided search term
152 * @param string $iwPrefix Interwiki prefix of wiki to show footer for
153 * @return string HTML
155 protected function footerHtml( $term, $iwPrefix ) {
157 $href = Title
::makeTitle( NS_SPECIAL
, 'Search', null, $iwPrefix )->getLocalURL(
158 [ 'search' => $term, 'fulltext' => 1 ]
161 $searchLink = Html
::rawElement(
164 $this->specialSearch
->msg( 'search-interwiki-more' )->escaped()
167 return Html
::rawElement( 'div', [ 'class' => 'iw-result__footer' ], $searchLink );
170 protected function loadCustomCaptions() {
171 if ( $this->customCaptions
!== null ) {
175 $this->customCaptions
= [];
176 $customLines = explode( "\n", $this->specialSearch
->msg( 'search-interwiki-custom' )->escaped() );
177 foreach ( $customLines as $line ) {
178 $parts = explode( ':', $line, 2 );
179 if ( count( $parts ) === 2 ) {
180 $this->customCaptions
[$parts[0]] = $parts[1];