Add sslCAFile option to DatabaseMysqli
[mediawiki.git] / includes / widget / search / InterwikiSearchResultSetWidget.php
blob81a1a43111c6df8a06773ea2892666f9cba6d91e
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;
11 use OOUI;
13 /**
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 */
28 protected $iwLookup;
29 /** @var $output */
30 protected $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
40 ) {
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;
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 if ( $this->showMultimedia ) {
62 $this->output->addModules( 'mediawiki.special.search.commonsInterwikiWidget' );
64 $this->output->addModuleStyles( 'mediawiki.special.search.interwikiwidget.styles' );
66 $iwResults = [];
67 foreach ( $resultSets as $resultSet ) {
68 $result = $resultSet->next();
69 while ( $result ) {
70 if ( !$result->isBrokenTitle() ) {
71 $iwResults[$result->getTitle()->getInterwiki()][] = $result;
73 $result = $resultSet->next();
77 $iwResultSetPos = 1;
78 $iwResultListOutput = '';
80 foreach ( $iwResults as $iwPrefix => $results ) {
81 // TODO: Assumes interwiki results are never paginated
82 $position = 0;
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
97 $iwResultItemOutput .
98 $footerHtml
101 $iwResultSetPos++;
104 return Html::rawElement(
105 'div',
106 [ 'id' => 'mw-interwiki-results' ],
107 Html::rawElement(
108 'p',
109 [ 'class' => 'iw-headline' ],
110 $this->specialSearch->msg( 'search-interwiki-caption' )->parse()
112 Html::rawElement(
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 ) {
126 $href = Title::makeTitle( NS_SPECIAL, 'Search', null, $iwPrefix )->getLocalURL(
127 [ 'search' => $term, 'fulltext' => 1 ]
130 $interwiki = $this->iwLookup->fetch( $iwPrefix );
131 $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
133 if ( isset( $this->customCaptions[$iwPrefix] ) ) {
134 $caption = $this->customCaptions[$iwPrefix];
135 } else {
136 $caption = $this->specialSearch->msg( 'search-interwiki-default', $parsed['host'] )->escaped();
139 $searchLink = Html::rawElement( 'em', null,
140 Html::rawElement( 'a', [ 'href' => $href, 'target' => '_blank' ], $caption )
143 return Html::rawElement( 'div',
144 [ 'class' => 'iw-result__footer' ],
145 $this->iwIcon( $iwPrefix ) . $searchLink );
148 protected function loadCustomCaptions() {
149 if ( $this->customCaptions !== null ) {
150 return;
153 $this->customCaptions = [];
154 $customLines = explode( "\n", $this->specialSearch->msg( 'search-interwiki-custom' )->escaped() );
155 foreach ( $customLines as $line ) {
156 $parts = explode( ':', $line, 2 );
157 if ( count( $parts ) === 2 ) {
158 $this->customCaptions[$parts[0]] = $parts[1];
164 * Generates a custom OOUI icon element with a favicon as the image.
165 * The favicon image URL is generated by parsing the interwiki URL
166 * and returning the default location of the favicon for that domain,
167 * which is assumed to be '/favicon.ico'.
169 * @param string $iwPrefix Interwiki prefix
170 * @return OOUI\IconWidget
172 protected function iwIcon( $iwPrefix ) {
173 $interwiki = $this->iwLookup->fetch( $iwPrefix );
174 $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
176 $iwIconUrl = $parsed['scheme'] .
177 $parsed['delimiter'] .
178 $parsed['host'] .
179 ( isset( $parsed['port'] ) ? ':' . $parsed['port'] : '' ) .
180 '/favicon.ico';
182 $iwIcon = new OOUI\IconWidget( [
183 'icon' => 'favicon'
184 ] );
186 $iwIcon->setAttributes( [ 'style' => "background-image:url($iwIconUrl);" ] );
188 return $iwIcon;