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 SimpleSearchResultSetWidget 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 */
29 public function __construct(
30 SpecialSearch $specialSearch,
31 SearchResultWidget $resultWidget,
32 LinkRenderer $linkRenderer,
33 InterwikiLookup $iwLookup
35 $this->specialSearch = $specialSearch;
36 $this->resultWidget = $resultWidget;
37 $this->linkRenderer = $linkRenderer;
38 $this->iwLookup = $iwLookup;
42 * @param string $term User provided search term
43 * @param SearchResultSet|SearchResultSet[] $resultSets List of interwiki
47 public function render( $term, $resultSets ) {
48 if ( !is_array( $resultSets ) ) {
49 $resultSets = [ $resultSets ];
52 $this->loadCustomCaptions();
55 foreach ( $resultSets as $resultSet ) {
56 $result = $resultSet->next();
58 if ( !$result->isBrokenTitle() ) {
59 $iwResults[$result->getTitle()->getInterwiki()][] = $result;
61 $result = $resultSet->next();
66 foreach ( $iwResults as $iwPrefix => $results ) {
67 $out .= $this->headerHtml( $iwPrefix, $term );
68 $out .= "<ul class='mw-search-iwresults'>";
69 // TODO: Assumes interwiki results are never paginated
71 foreach ( $results as $result ) {
72 $out .= $this->resultWidget->render( $result, $term, $position++ );
78 "<div id='mw-search-interwiki'>" .
79 "<div id='mw-search-interwiki-caption'>" .
80 $this->specialSearch->msg( 'search-interwiki-caption' )->parse() .
87 * Generates an appropriate HTML header for the given interwiki prefix
89 * @param string $iwPrefix Interwiki prefix of wiki to show header for
90 * @param string $term User provided search term
93 protected function headerHtml( $iwPrefix, $term ) {
94 if ( isset( $this->customCaptions[$iwPrefix] ) ) {
95 $caption = $this->customCaptions[$iwPrefix];
97 $interwiki = $this->iwLookup->fetch( $iwPrefix );
98 $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
99 $caption = $this->specialSearch->msg( 'search-interwiki-default', $parsed['host'] )->escaped();
102 $href = Title::makeTitle( NS_SPECIAL, 'Search', null, $iwPrefix )->getLocalURL(
103 [ 'search' => $term, 'fulltext' => 1 ]
105 $searchLink = Html::rawElement(
108 $this->specialSearch->msg( 'search-interwiki-more' )->escaped()
112 "<div class='mw-search-interwiki-project'>" .
113 "<span class='mw-search-interwiki-more'>{$searchLink}</span>" .
118 protected function loadCustomCaptions() {
119 if ( $this->customCaptions !== null ) {
123 $this->customCaptions = [];
124 $customLines = explode( "\n", $this->specialSearch->msg( 'search-interwiki-custom' )->escaped() );
125 foreach ( $customLines as $line ) {
126 $parts = explode( ':', $line, 2 );
127 if ( count( $parts ) === 2 ) {
128 $this->customCaptions[$parts[0]] = $parts[1];