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