1911c7909f7fe5fd327591d313dea8f40c98664f
[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
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.
16 */
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;
32
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'
46 );
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 $this->output->addModules( 'mediawiki.special.search.commonsInterwikiWidget' );
62 $this->output->addModuleStyles( 'mediawiki.special.search.interwikiwidget.styles' );
63
64 $iwResults = [];
65 foreach ( $resultSets as $resultSet ) {
66 $result = $resultSet->next();
67 while ( $result ) {
68 if ( !$result->isBrokenTitle() ) {
69 $iwResults[$result->getTitle()->getInterwiki()][] = $result;
70 }
71 $result = $resultSet->next();
72 }
73 }
74
75 $iwResultSetPos = 1;
76 $iwResultListOutput = '';
77
78 foreach ( $iwResults as $iwPrefix => $results ) {
79 // TODO: Assumes interwiki results are never paginated
80 $position = 0;
81 $iwResultItemOutput = '';
82
83 $iwDisplayType = isset( $this->iwPrefixDisplayTypes[$iwPrefix] )
84 ? $this->iwPrefixDisplayTypes[$iwPrefix]
85 : "";
86
87 foreach ( $results as $result ) {
88 $iwResultItemOutput .= $this->resultWidget->render( $result, $term, $position++ );
89 }
90
91 $headerHtml = $this->headerHtml( $term, $iwPrefix );
92 $footerHtml = $this->footerHtml( $term, $iwPrefix );
93 $iwResultListOutput .= Html::rawElement( 'li',
94 [
95 'class' => 'iw-resultset iw-resultset--' . $iwDisplayType,
96 'data-iw-resultset-pos' => $iwResultSetPos
97 ],
98 $headerHtml .
99 $iwResultItemOutput .
100 $footerHtml
101 );
102
103 $iwResultSetPos++;
104 }
105
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()
113 ) .
114 Html::rawElement(
115 'ul', [ 'class' => 'iw-results', ], $iwResultListOutput
116 )
117 );
118 }
119
120 /**
121 * Generates an appropriate HTML header for the given interwiki prefix
122 *
123 * @param string $term User provided search term
124 * @param string $iwPrefix Interwiki prefix of wiki to show header for
125 * @return string HTML
126 */
127 protected function headerHtml( $term, $iwPrefix ) {
128
129 $iwDisplayType = isset( $this->iwPrefixDisplayTypes[$iwPrefix] )
130 ? $this->iwPrefixDisplayTypes[$iwPrefix]
131 : "";
132
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();
140 }
141
142 return Html::rawElement( 'div', [ 'class' => 'iw-result__header' ],
143 Html::rawElement( 'span', [ 'class' => 'iw-result__icon iw-result__icon--' . $iwDisplayType ] )
144 . $caption
145 );
146 }
147
148 /**
149 * Generates an HTML footer for the given interwiki prefix
150 *
151 * @param string $term User provided search term
152 * @param string $iwPrefix Interwiki prefix of wiki to show footer for
153 * @return string HTML
154 */
155 protected function footerHtml( $term, $iwPrefix ) {
156
157 $href = Title::makeTitle( NS_SPECIAL, 'Search', null, $iwPrefix )->getLocalURL(
158 [ 'search' => $term, 'fulltext' => 1 ]
159 );
160
161 $searchLink = Html::rawElement(
162 'a',
163 [ 'href' => $href ],
164 $this->specialSearch->msg( 'search-interwiki-more-results' )->escaped()
165 );
166
167 return Html::rawElement( 'div', [ 'class' => 'iw-result__footer' ], $searchLink );
168 }
169
170 protected function loadCustomCaptions() {
171 if ( $this->customCaptions !== null ) {
172 return;
173 }
174
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];
181 }
182 }
183 }
184 }