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 */
20 protected $specialSearch;
21 /** @var SearchResultWidget */
22 protected $resultWidget;
23 /** @var string[]|null */
24 protected $customCaptions;
25 /** @var LinkRenderer */
26 protected $linkRenderer;
27 /** @var InterwikiLookup */
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;
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 if ( $this->showMultimedia
) {
62 $this->output
->addModules( 'mediawiki.special.search.commonsInterwikiWidget' );
64 $this->output
->addModuleStyles( 'mediawiki.special.search.interwikiwidget.styles' );
67 foreach ( $resultSets as $resultSet ) {
68 $result = $resultSet->next();
70 if ( !$result->isBrokenTitle() ) {
71 $iwResults[$result->getTitle()->getInterwiki()][] = $result;
73 $result = $resultSet->next();
78 $iwResultListOutput = '';
80 foreach ( $iwResults as $iwPrefix => $results ) {
81 // TODO: Assumes interwiki results are never paginated
83 $iwResultItemOutput = '';
85 foreach ( $results as $result ) {
86 $iwResultItemOutput .= $this->resultWidget
->render( $result, $term, $position++
);
89 $footerHtml = $this->footerHtml( $term, $iwPrefix );
90 $iwResultListOutput .= Html
::rawElement( 'li',
92 'class' => 'iw-resultset',
93 'data-iw-resultset-pos' => $iwResultSetPos,
94 'data-iw-resultset-source' => $iwPrefix
104 return Html
::rawElement(
106 [ 'id' => 'mw-interwiki-results' ],
109 [ 'class' => 'iw-headline' ],
110 $this->specialSearch
->msg( 'search-interwiki-caption' )->parse()
113 'ul', [ 'class' => 'iw-results', ], $iwResultListOutput
119 * Generates an HTML footer for the given interwiki prefix
121 * @param string $term User provided search term
122 * @param string $iwPrefix Interwiki prefix of wiki to show footer for
123 * @return string HTML
125 protected function footerHtml( $term, $iwPrefix ) {
127 $href = Title
::makeTitle( NS_SPECIAL
, 'Search', null, $iwPrefix )->getLocalURL(
128 [ 'search' => $term, 'fulltext' => 1 ]
131 $interwiki = $this->iwLookup
->fetch( $iwPrefix );
132 $parsed = wfParseUrl( wfExpandUrl( $interwiki ?
$interwiki->getURL() : '/' ) );
134 if ( isset( $this->customCaptions
[$iwPrefix] ) ) {
135 $caption = $this->customCaptions
[$iwPrefix];
137 $caption = $this->specialSearch
->msg( 'search-interwiki-default', $parsed['host'] )->escaped();
140 $searchLink = Html
::rawElement( 'em', null,
141 Html
::rawElement( 'a', [ 'href' => $href, 'target' => '_blank' ], $caption )
144 return Html
::rawElement( 'div',
145 [ 'class' => 'iw-result__footer' ],
146 $this->iwIcon( $iwPrefix ) . $searchLink );
149 protected function loadCustomCaptions() {
150 if ( $this->customCaptions
!== null ) {
154 $this->customCaptions
= [];
155 $customLines = explode( "\n", $this->specialSearch
->msg( 'search-interwiki-custom' )->escaped() );
156 foreach ( $customLines as $line ) {
157 $parts = explode( ':', $line, 2 );
158 if ( count( $parts ) === 2 ) {
159 $this->customCaptions
[$parts[0]] = $parts[1];
165 * Generates a custom OOUI icon element with a favicon as the image.
166 * The favicon image URL is generated by parsing the interwiki URL
167 * and returning the default location of the favicon for that domain,
168 * which is assumed to be '/favicon.ico'.
170 * @param string $iwPrefix Interwiki prefix
171 * @return OOUI\IconWidget
173 protected function iwIcon( $iwPrefix ) {
175 $interwiki = $this->iwLookup
->fetch( $iwPrefix );
176 $parsed = wfParseUrl( wfExpandUrl( $interwiki ?
$interwiki->getURL() : '/' ) );
178 $iwIconUrl = $parsed['scheme'] .
179 $parsed['delimiter'] .
181 ( isset ( $parsed['port'] ) ?
':' . $parsed['port'] : '' ) .
184 $iwIcon = new OOUI\
IconWidget( [
188 $iwIcon->setAttributes( [ 'style' => "background-image:url($iwIconUrl);" ] );