Merge "Add 3D filetype for STL files"
[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 SearchResultSet;
8 use SpecialSearch;
9 use Title;
10 use Html;
11 use OOUI;
12
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.
17 */
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;
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 * @param string $term User provided search term
50 * @param SearchResultSet|SearchResultSet[] $resultSets List of interwiki
51 * results to render.
52 * @return string HTML
53 */
54 public function render( $term, $resultSets ) {
55 if ( !is_array( $resultSets ) ) {
56 $resultSets = [ $resultSets ];
57 }
58
59 $this->loadCustomCaptions();
60
61 if ( $this->showMultimedia ) {
62 $this->output->addModules( 'mediawiki.special.search.commonsInterwikiWidget' );
63 }
64 $this->output->addModuleStyles( 'mediawiki.special.search.interwikiwidget.styles' );
65
66 $iwResults = [];
67 foreach ( $resultSets as $resultSet ) {
68 $result = $resultSet->next();
69 while ( $result ) {
70 if ( !$result->isBrokenTitle() ) {
71 $iwResults[$result->getTitle()->getInterwiki()][] = $result;
72 }
73 $result = $resultSet->next();
74 }
75 }
76
77 $iwResultSetPos = 1;
78 $iwResultListOutput = '';
79
80 foreach ( $iwResults as $iwPrefix => $results ) {
81 // TODO: Assumes interwiki results are never paginated
82 $position = 0;
83 $iwResultItemOutput = '';
84
85 foreach ( $results as $result ) {
86 $iwResultItemOutput .= $this->resultWidget->render( $result, $term, $position++ );
87 }
88
89 $footerHtml = $this->footerHtml( $term, $iwPrefix );
90 $iwResultListOutput .= Html::rawElement( 'li',
91 [
92 'class' => 'iw-resultset',
93 'data-iw-resultset-pos' => $iwResultSetPos,
94 'data-iw-resultset-source' => $iwPrefix
95 ],
96
97 $iwResultItemOutput .
98 $footerHtml
99 );
100
101 $iwResultSetPos++;
102 }
103
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()
111 ) .
112 Html::rawElement(
113 'ul', [ 'class' => 'iw-results', ], $iwResultListOutput
114 )
115 );
116 }
117
118 /**
119 * Generates an HTML footer for the given interwiki prefix
120 *
121 * @param string $term User provided search term
122 * @param string $iwPrefix Interwiki prefix of wiki to show footer for
123 * @return string HTML
124 */
125 protected function footerHtml( $term, $iwPrefix ) {
126
127 $href = Title::makeTitle( NS_SPECIAL, 'Search', null, $iwPrefix )->getLocalURL(
128 [ 'search' => $term, 'fulltext' => 1 ]
129 );
130
131 $interwiki = $this->iwLookup->fetch( $iwPrefix );
132 $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
133
134 if ( isset( $this->customCaptions[$iwPrefix] ) ) {
135 $caption = $this->customCaptions[$iwPrefix];
136 } else {
137 $caption = $this->specialSearch->msg( 'search-interwiki-default', $parsed['host'] )->escaped();
138 }
139
140 $searchLink = Html::rawElement( 'em', null,
141 Html::rawElement( 'a', [ 'href' => $href, 'target' => '_blank' ], $caption )
142 );
143
144 return Html::rawElement( 'div',
145 [ 'class' => 'iw-result__footer' ],
146 $this->iwIcon( $iwPrefix ) . $searchLink );
147 }
148
149 protected function loadCustomCaptions() {
150 if ( $this->customCaptions !== null ) {
151 return;
152 }
153
154 $this->customCaptions = [];
155 $customLines = explode( "\n", $this->specialSearch->msg( 'search-interwiki-custom' )->escaped() );
156 foreach ( $customLines as $line ) {
157 $parts = explode( ':', $line, 2 );
158 if ( count( $parts ) === 2 ) {
159 $this->customCaptions[$parts[0]] = $parts[1];
160 }
161 }
162 }
163
164 /**
165 * Generates a custom OOUI icon element with a favicon as the image.
166 * The favicon image URL is generated by parsing the interwiki URL
167 * and returning the default location of the favicon for that domain,
168 * which is assumed to be '/favicon.ico'.
169 *
170 * @param string $iwPrefix Interwiki prefix
171 * @return OOUI\IconWidget
172 **/
173 protected function iwIcon( $iwPrefix ) {
174
175 $interwiki = $this->iwLookup->fetch( $iwPrefix );
176 $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
177
178 $iwIconUrl = $parsed['scheme'] .
179 $parsed['delimiter'] .
180 $parsed['host'] .
181 ( isset ( $parsed['port'] ) ? ':' . $parsed['port'] : '' ) .
182 '/favicon.ico';
183
184 $iwIcon = new OOUI\IconWidget( [
185 'icon' => 'favicon'
186 ] );
187
188 $iwIcon->setAttributes( [ 'style' => "background-image:url($iwIconUrl);" ] );
189
190 return $iwIcon;
191 }
192 }