Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / includes / widget / search / InterwikiSearchResultSetWidget.php
1 <?php
2
3 namespace MediaWiki\Widget\Search;
4
5 use MediaWiki\Interwiki\InterwikiLookup;
6 use MediaWiki\Linker\LinkRenderer;
7 use ISearchResultSet;
8 use SpecialSearch;
9 use Title;
10 use Html;
11 use OOUI;
12
13 /**
14 * Renders one or more ISearchResultSets into a sidebar grouped by
15 * interwiki prefix. Includes a per-wiki header indicating where
16 * the results are from.
17 */
18 class InterwikiSearchResultSetWidget implements SearchResultSetWidget {
19 /** @var SpecialSearch $specialSearch */
20 protected $specialSearch;
21 /** @var SearchResultWidget $resultWidget */
22 protected $resultWidget;
23 /** @var string[]|null $customCaptions */
24 protected $customCaptions;
25 /** @var LinkRenderer $linkRenderer */
26 protected $linkRenderer;
27 /** @var InterwikiLookup $iwLookup */
28 protected $iwLookup;
29 /** @var \OutputPage $output */
30 protected $output;
31 /** @var bool $showMultimedia */
32 protected $showMultimedia;
33
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;
47 }
48
49 /**
50 * @param string $term User provided search term
51 * @param ISearchResultSet|ISearchResultSet[] $resultSets List of interwiki
52 * results to render.
53 * @return string HTML
54 */
55 public function render( $term, $resultSets ) {
56 if ( !is_array( $resultSets ) ) {
57 $resultSets = [ $resultSets ];
58 }
59
60 $this->loadCustomCaptions();
61
62 if ( $this->showMultimedia ) {
63 $this->output->addModules( 'mediawiki.special.search.commonsInterwikiWidget' );
64 }
65 $this->output->addModuleStyles( 'mediawiki.special.search.interwikiwidget.styles' );
66
67 $iwResults = [];
68 foreach ( $resultSets as $resultSet ) {
69 foreach ( $resultSet as $result ) {
70 if ( !$result->isBrokenTitle() ) {
71 $iwResults[$result->getTitle()->getInterwiki()][] = $result;
72 }
73 }
74 }
75
76 $iwResultSetPos = 1;
77 $iwResultListOutput = '';
78
79 foreach ( $iwResults as $iwPrefix => $results ) {
80 // TODO: Assumes interwiki results are never paginated
81 $position = 0;
82 $iwResultItemOutput = '';
83
84 foreach ( $results as $result ) {
85 $iwResultItemOutput .= $this->resultWidget->render( $result, $position++ );
86 }
87
88 $footerHtml = $this->footerHtml( $term, $iwPrefix );
89 $iwResultListOutput .= Html::rawElement( 'li',
90 [
91 'class' => 'iw-resultset',
92 'data-iw-resultset-pos' => $iwResultSetPos,
93 'data-iw-resultset-source' => $iwPrefix
94 ],
95
96 $iwResultItemOutput .
97 $footerHtml
98 );
99
100 $iwResultSetPos++;
101 }
102
103 return Html::rawElement(
104 'div',
105 [ 'id' => 'mw-interwiki-results' ],
106 Html::rawElement(
107 'p',
108 [ 'class' => 'iw-headline' ],
109 $this->specialSearch->msg( 'search-interwiki-caption' )->parse()
110 ) .
111 Html::rawElement(
112 'ul', [ 'class' => 'iw-results', ], $iwResultListOutput
113 )
114 );
115 }
116
117 /**
118 * Generates an HTML footer for the given interwiki prefix
119 *
120 * @param string $term User provided search term
121 * @param string $iwPrefix Interwiki prefix of wiki to show footer for
122 * @return string HTML
123 */
124 protected function footerHtml( $term, $iwPrefix ) {
125 $href = Title::makeTitle( NS_SPECIAL, 'Search', null, $iwPrefix )->getLocalURL(
126 [ 'search' => $term, 'fulltext' => 1 ]
127 );
128
129 $interwiki = $this->iwLookup->fetch( $iwPrefix );
130 $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
131
132 $caption = $this->customCaptions[$iwPrefix] ??
133 $this->specialSearch->msg( 'search-interwiki-default', $parsed['host'] )->escaped();
134
135 $searchLink = Html::rawElement( 'em', null,
136 Html::rawElement( 'a', [ 'href' => $href, 'target' => '_blank' ], $caption )
137 );
138
139 return Html::rawElement( 'div',
140 [ 'class' => 'iw-result__footer' ],
141 $this->iwIcon( $iwPrefix ) . $searchLink );
142 }
143
144 protected function loadCustomCaptions() {
145 if ( $this->customCaptions !== null ) {
146 return;
147 }
148
149 $this->customCaptions = [];
150 $customLines = explode( "\n", $this->specialSearch->msg( 'search-interwiki-custom' )->escaped() );
151 foreach ( $customLines as $line ) {
152 $parts = explode( ':', $line, 2 );
153 if ( count( $parts ) === 2 ) {
154 $this->customCaptions[$parts[0]] = $parts[1];
155 }
156 }
157 }
158
159 /**
160 * Generates a custom OOUI icon element with a favicon as the image.
161 * The favicon image URL is generated by parsing the interwiki URL
162 * and returning the default location of the favicon for that domain,
163 * which is assumed to be '/favicon.ico'.
164 *
165 * @param string $iwPrefix Interwiki prefix
166 * @return OOUI\IconWidget
167 */
168 protected function iwIcon( $iwPrefix ) {
169 $interwiki = $this->iwLookup->fetch( $iwPrefix );
170 $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
171
172 $iwIconUrl = $parsed['scheme'] .
173 $parsed['delimiter'] .
174 $parsed['host'] .
175 ( isset( $parsed['port'] ) ? ':' . $parsed['port'] : '' ) .
176 '/favicon.ico';
177
178 $iwIcon = new OOUI\IconWidget( [
179 'icon' => 'favicon'
180 ] );
181
182 $iwIcon->setAttributes( [ 'style' => "background-image:url($iwIconUrl);" ] );
183
184 return $iwIcon;
185 }
186 }