Move ILoadBalancer to Rdbms namespace
[mediawiki.git] / includes / widget / search / InterwikiSearchResultSetWidget.php
blob76b9b01839b8397de99cb6b0096df67a9d9cc3fd
1 <?php
3 namespace MediaWiki\Widget\Search;
5 use MediaWiki\Interwiki\InterwikiLookup;
6 use MediaWiki\Linker\LinkRenderer;
7 use SearchResultSet;
8 use SpecialSearch;
9 use Title;
10 use Html;
12 /**
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 */
27 protected $iwLookup;
28 /** @var $output */
29 protected $output;
30 /** @var $iwPrefixDisplayTypes */
31 protected $iwPrefixDisplayTypes;
33 public function __construct(
34 SpecialSearch $specialSearch,
35 SearchResultWidget $resultWidget,
36 LinkRenderer $linkRenderer,
37 InterwikiLookup $iwLookup
38 ) {
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'
48 /**
49 * @param string $term User provided search term
50 * @param SearchResultSet|SearchResultSet[] $resultSets List of interwiki
51 * results to render.
52 * @return string HTML
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' );
64 $iwResults = [];
65 foreach ( $resultSets as $resultSet ) {
66 $result = $resultSet->next();
67 while ( $result ) {
68 if ( !$result->isBrokenTitle() ) {
69 $iwResults[$result->getTitle()->getInterwiki()][] = $result;
71 $result = $resultSet->next();
75 $iwResultSetPos = 1;
76 $iwResultListOutput = '';
78 foreach ( $iwResults as $iwPrefix => $results ) {
79 // TODO: Assumes interwiki results are never paginated
80 $position = 0;
81 $iwResultItemOutput = '';
83 $iwDisplayType = isset( $this->iwPrefixDisplayTypes[$iwPrefix] )
84 ? $this->iwPrefixDisplayTypes[$iwPrefix]
85 : "";
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
98 $headerHtml .
99 $iwResultItemOutput .
100 $footerHtml
103 $iwResultSetPos++;
106 return Html::rawElement(
107 'div',
108 [ 'id' => 'mw-interwiki-results' ],
109 Html::rawElement(
110 'p',
111 [ 'class' => 'iw-headline' ],
112 $this->specialSearch->msg( 'search-interwiki-caption' )->parse()
114 Html::rawElement(
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]
131 : "";
133 if ( isset( $this->customCaptions[$iwPrefix] ) ) {
134 /* customCaptions composed by loadCustomCaptions() with pre-escaped content.*/
135 $caption = $this->customCaptions[$iwPrefix];
136 } else {
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 ] )
144 . $caption
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(
162 'a',
163 [ 'href' => $href ],
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 ) {
172 return;
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];