3 namespace MediaWiki\Widget\Search
;
5 use MediaWiki\Interwiki\InterwikiLookup
;
6 use MediaWiki\Linker\LinkRenderer
;
14 * Renders one or more SearchResultSets into a sidebar grouped by
15 * interwiki prefix. Includes a per-wiki header indicating where
16 * the results are from.
18 class InterwikiSearchResultSetWidget
implements SearchResultSetWidget
{
19 /** @var SpecialSearch $specialSearch */
20 protected $specialSearch;
21 /** @var SearchResultWidget $resultWidget */
22 protected $resultWidget;
23 /** @var string[]|null $customCaptions */
24 protected $customCaptions;
25 /** @var LinkRenderer $linkRenderer */
26 protected $linkRenderer;
27 /** @var InterwikiLookup $iwLookup */
29 /** @var \OutputPage $output */
31 /** @var bool $showMultimedia */
32 protected $showMultimedia;
34 public function __construct(
35 SpecialSearch
$specialSearch,
36 SearchResultWidget
$resultWidget,
37 LinkRenderer
$linkRenderer,
38 InterwikiLookup
$iwLookup,
39 $showMultimedia = false
41 $this->specialSearch
= $specialSearch;
42 $this->resultWidget
= $resultWidget;
43 $this->linkRenderer
= $linkRenderer;
44 $this->iwLookup
= $iwLookup;
45 $this->output
= $specialSearch->getOutput();
46 $this->showMultimedia
= $showMultimedia;
50 * @param string $term User provided search term
51 * @param SearchResultSet|SearchResultSet[] $resultSets List of interwiki
55 public function render( $term, $resultSets ) {
56 if ( !is_array( $resultSets ) ) {
57 $resultSets = [ $resultSets ];
60 $this->loadCustomCaptions();
62 if ( $this->showMultimedia
) {
63 $this->output
->addModules( 'mediawiki.special.search.commonsInterwikiWidget' );
65 $this->output
->addModuleStyles( 'mediawiki.special.search.interwikiwidget.styles' );
68 foreach ( $resultSets as $resultSet ) {
69 foreach ( $resultSet as $result ) {
70 if ( !$result->isBrokenTitle() ) {
71 $iwResults[$result->getTitle()->getInterwiki()][] = $result;
77 $iwResultListOutput = '';
79 foreach ( $iwResults as $iwPrefix => $results ) {
80 // TODO: Assumes interwiki results are never paginated
82 $iwResultItemOutput = '';
84 foreach ( $results as $result ) {
85 $iwResultItemOutput .= $this->resultWidget
->render( $result, $term, $position++
);
88 $footerHtml = $this->footerHtml( $term, $iwPrefix );
89 $iwResultListOutput .= Html
::rawElement( 'li',
91 'class' => 'iw-resultset',
92 'data-iw-resultset-pos' => $iwResultSetPos,
93 'data-iw-resultset-source' => $iwPrefix
103 return Html
::rawElement(
105 [ 'id' => 'mw-interwiki-results' ],
108 [ 'class' => 'iw-headline' ],
109 $this->specialSearch
->msg( 'search-interwiki-caption' )->parse()
112 'ul', [ 'class' => 'iw-results', ], $iwResultListOutput
118 * Generates an HTML footer for the given interwiki prefix
120 * @param string $term User provided search term
121 * @param string $iwPrefix Interwiki prefix of wiki to show footer for
122 * @return string HTML
124 protected function footerHtml( $term, $iwPrefix ) {
125 $href = Title
::makeTitle( NS_SPECIAL
, 'Search', null, $iwPrefix )->getLocalURL(
126 [ 'search' => $term, 'fulltext' => 1 ]
129 $interwiki = $this->iwLookup
->fetch( $iwPrefix );
130 $parsed = wfParseUrl( wfExpandUrl( $interwiki ?
$interwiki->getURL() : '/' ) );
132 $caption = $this->customCaptions
[$iwPrefix] ??
133 $this->specialSearch
->msg( 'search-interwiki-default', $parsed['host'] )->escaped();
135 $searchLink = Html
::rawElement( 'em', null,
136 Html
::rawElement( 'a', [ 'href' => $href, 'target' => '_blank' ], $caption )
139 return Html
::rawElement( 'div',
140 [ 'class' => 'iw-result__footer' ],
141 $this->iwIcon( $iwPrefix ) . $searchLink );
144 protected function loadCustomCaptions() {
145 if ( $this->customCaptions
!== null ) {
149 $this->customCaptions
= [];
150 $customLines = explode( "\n", $this->specialSearch
->msg( 'search-interwiki-custom' )->escaped() );
151 foreach ( $customLines as $line ) {
152 $parts = explode( ':', $line, 2 );
153 if ( count( $parts ) === 2 ) {
154 $this->customCaptions
[$parts[0]] = $parts[1];
160 * Generates a custom OOUI icon element with a favicon as the image.
161 * The favicon image URL is generated by parsing the interwiki URL
162 * and returning the default location of the favicon for that domain,
163 * which is assumed to be '/favicon.ico'.
165 * @param string $iwPrefix Interwiki prefix
166 * @return OOUI\IconWidget
168 protected function iwIcon( $iwPrefix ) {
169 $interwiki = $this->iwLookup
->fetch( $iwPrefix );
170 $parsed = wfParseUrl( wfExpandUrl( $interwiki ?
$interwiki->getURL() : '/' ) );
172 $iwIconUrl = $parsed['scheme'] .
173 $parsed['delimiter'] .
175 ( isset( $parsed['port'] ) ?
':' . $parsed['port'] : '' ) .
178 $iwIcon = new OOUI\
IconWidget( [
182 $iwIcon->setAttributes( [ 'style' => "background-image:url($iwIconUrl);" ] );