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 */
32 public function __construct(
33 SpecialSearch
$specialSearch,
34 SearchResultWidget
$resultWidget,
35 LinkRenderer
$linkRenderer,
36 InterwikiLookup
$iwLookup
38 $this->specialSearch
= $specialSearch;
39 $this->resultWidget
= $resultWidget;
40 $this->linkRenderer
= $linkRenderer;
41 $this->iwLookup
= $iwLookup;
42 $this->output
= $specialSearch->getOutput();
45 * @param string $term User provided search term
46 * @param SearchResultSet|SearchResultSet[] $resultSets List of interwiki
50 public function render( $term, $resultSets ) {
51 if ( !is_array( $resultSets ) ) {
52 $resultSets = [ $resultSets ];
55 $this->loadCustomCaptions();
57 $this->output
->addModules( 'mediawiki.special.search.commonsInterwikiWidget' );
58 $this->output
->addModuleStyles( 'mediawiki.special.search.interwikiwidget.styles' );
61 foreach ( $resultSets as $resultSet ) {
62 $result = $resultSet->next();
64 if ( !$result->isBrokenTitle() ) {
65 $iwResults[$result->getTitle()->getInterwiki()][] = $result;
67 $result = $resultSet->next();
72 $iwResultListOutput = '';
74 foreach ( $iwResults as $iwPrefix => $results ) {
75 // TODO: Assumes interwiki results are never paginated
77 $iwResultItemOutput = '';
79 foreach ( $results as $result ) {
80 $iwResultItemOutput .= $this->resultWidget
->render( $result, $term, $position++
);
83 $footerHtml = $this->footerHtml( $term, $iwPrefix );
84 $iwResultListOutput .= Html
::rawElement( 'li',
86 'class' => 'iw-resultset',
87 'data-iw-resultset-pos' => $iwResultSetPos
97 return Html
::rawElement(
99 [ 'id' => 'mw-interwiki-results' ],
102 [ 'class' => 'iw-headline' ],
103 $this->specialSearch
->msg( 'search-interwiki-caption' )->parse()
106 'ul', [ 'class' => 'iw-results', ], $iwResultListOutput
112 * Generates an HTML footer for the given interwiki prefix
114 * @param string $term User provided search term
115 * @param string $iwPrefix Interwiki prefix of wiki to show footer for
116 * @return string HTML
118 protected function footerHtml( $term, $iwPrefix ) {
120 $href = Title
::makeTitle( NS_SPECIAL
, 'Search', null, $iwPrefix )->getLocalURL(
121 [ 'search' => $term, 'fulltext' => 1 ]
124 $interwiki = $this->iwLookup
->fetch( $iwPrefix );
125 $parsed = wfParseUrl( wfExpandUrl( $interwiki ?
$interwiki->getURL() : '/' ) );
127 if ( isset( $this->customCaptions
[$iwPrefix] ) ) {
128 $caption = $this->customCaptions
[$iwPrefix];
130 $caption = $this->specialSearch
->msg( 'search-interwiki-default', $parsed['host'] )->escaped();
133 $searchLink = Html
::rawElement( 'em', null,
134 Html
::rawElement( 'a', [ 'href' => $href, 'target' => '_blank' ], $caption )
137 return Html
::rawElement( 'div',
138 [ 'class' => 'iw-result__footer' ],
139 $this->iwIcon( $iwPrefix ) . $searchLink );
142 protected function loadCustomCaptions() {
143 if ( $this->customCaptions
!== null ) {
147 $this->customCaptions
= [];
148 $customLines = explode( "\n", $this->specialSearch
->msg( 'search-interwiki-custom' )->escaped() );
149 foreach ( $customLines as $line ) {
150 $parts = explode( ':', $line, 2 );
151 if ( count( $parts ) === 2 ) {
152 $this->customCaptions
[$parts[0]] = $parts[1];
158 * Generates a custom OOUI icon element with a favicon as the image.
159 * The favicon image URL is generated by parsing the interwiki URL
160 * and returning the default location of the favicon for that domain,
161 * which is assumed to be '/favicon.ico'.
163 * @param string $iwPrefix Interwiki prefix
164 * @return OOUI\IconWidget
166 protected function iwIcon( $iwPrefix ) {
168 $interwiki = $this->iwLookup
->fetch( $iwPrefix );
169 $parsed = wfParseUrl( wfExpandUrl( $interwiki ?
$interwiki->getURL() : '/' ) );
171 $iwIconUrl = $parsed['scheme'] .
172 $parsed['delimiter'] .
174 ( $parsed['port'] ?
':' . $parsed['port'] : '' ) .
177 $iwIcon = new OOUI\
IconWidget( [
181 $iwIcon->setAttributes( [ 'style' => "background-image:url($iwIconUrl);" ] );