Merge "Add 3D filetype for STL files"
[lhc/web/wiklou.git] / includes / widget / search / SearchFormWidget.php
1 <?php
2
3 namespace MediaWiki\Widget\Search;
4
5 use Hooks;
6 use Html;
7 use MediaWiki\Widget\SearchInputWidget;
8 use MWNamespace;
9 use SearchEngineConfig;
10 use SpecialSearch;
11 use Xml;
12
13 class SearchFormWidget {
14 /** @var SpecialSearch */
15 protected $specialSearch;
16 /** @var SearchEngineConfig */
17 protected $searchConfig;
18 /** @var array */
19 protected $profiles;
20
21 /**
22 * @param SpecialSearch $specialSearch
23 * @param SearchEngineConfig $searchConfig
24 * @param array $profiles
25 */
26 public function __construct(
27 SpecialSearch $specialSearch,
28 SearchEngineConfig $searchConfig,
29 array $profiles
30 ) {
31 $this->specialSearch = $specialSearch;
32 $this->searchConfig = $searchConfig;
33 $this->profiles = $profiles;
34 }
35
36 /**
37 * @param string $profile The current search profile
38 * @param string $term The current search term
39 * @param int $numResults The number of results shown
40 * @param int $totalResults The total estimated results found
41 * @param int $offset Current offset in search results
42 * @param bool $isPowerSearch Is the 'advanced' section open?
43 * @return string HTML
44 */
45 public function render(
46 $profile,
47 $term,
48 $numResults,
49 $totalResults,
50 $offset,
51 $isPowerSearch
52 ) {
53 return Xml::openElement(
54 'form',
55 [
56 'id' => $isPowerSearch ? 'powersearch' : 'search',
57 'method' => 'get',
58 'action' => wfScript(),
59 ]
60 ) .
61 '<div id="mw-search-top-table">' .
62 $this->shortDialogHtml( $profile, $term, $numResults, $totalResults, $offset ) .
63 '</div>' .
64 "<div class='mw-search-visualclear'></div>" .
65 "<div class='mw-search-profile-tabs'>" .
66 $this->profileTabsHtml( $profile, $term ) .
67 "<div style='clear:both'></div>" .
68 "</div>" .
69 $this->optionsHtml( $term, $isPowerSearch, $profile ) .
70 '</form>';
71 }
72
73 /**
74 * @param string $profile The current search profile
75 * @param string $term The current search term
76 * @param int $numResults The number of results shown
77 * @param int $totalResults The total estimated results found
78 * @param int $offset Current offset in search results
79 * @return string HTML
80 */
81 protected function shortDialogHtml( $profile, $term, $numResults, $totalResults, $offset ) {
82 $html = '';
83
84 $searchWidget = new SearchInputWidget( [
85 'id' => 'searchText',
86 'name' => 'search',
87 'autofocus' => trim( $term ) === '',
88 'value' => $term,
89 'dataLocation' => 'content',
90 'infusable' => true,
91 ] );
92
93 $layout = new \OOUI\ActionFieldLayout( $searchWidget, new \OOUI\ButtonInputWidget( [
94 'type' => 'submit',
95 'label' => $this->specialSearch->msg( 'searchbutton' )->text(),
96 'flags' => [ 'progressive', 'primary' ],
97 ] ), [
98 'align' => 'top',
99 ] );
100
101 $html .= $layout;
102
103 if ( $totalResults > 0 && $offset < $totalResults ) {
104 $html .= Xml::tags(
105 'div',
106 [
107 'class' => 'results-info',
108 'data-mw-num-results-offset' => $offset,
109 'data-mw-num-results-total' => $totalResults
110 ],
111 $this->specialSearch->msg( 'search-showingresults' )
112 ->numParams( $offset + 1, $offset + $numResults, $totalResults )
113 ->numParams( $numResults )
114 ->parse()
115 );
116 }
117
118 $html .=
119 Html::hidden( 'title', $this->specialSearch->getPageTitle()->getPrefixedText() ) .
120 Html::hidden( 'profile', $profile ) .
121 Html::hidden( 'fulltext', '1' );
122
123 return $html;
124 }
125
126 /**
127 * Generates HTML for the list of available search profiles.
128 *
129 * @param string $profile The currently selected profile
130 * @param string $term The user provided search terms
131 * @return string HTML
132 */
133 protected function profileTabsHtml( $profile, $term ) {
134 $bareterm = $this->startsWithImage( $term )
135 ? substr( $term, strpos( $term, ':' ) + 1 )
136 : $term;
137 $lang = $this->specialSearch->getLanguage();
138 $items = [];
139 foreach ( $this->profiles as $id => $profileConfig ) {
140 $profileConfig['parameters']['profile'] = $id;
141 $tooltipParam = isset( $profileConfig['namespace-messages'] )
142 ? $lang->commaList( $profileConfig['namespace-messages'] )
143 : null;
144 $items[] = Xml::tags(
145 'li',
146 [ 'class' => $profile === $id ? 'current' : 'normal' ],
147 $this->makeSearchLink(
148 $bareterm,
149 $this->specialSearch->msg( $profileConfig['message'] )->text(),
150 $this->specialSearch->msg( $profileConfig['tooltip'], $tooltipParam )->text(),
151 $profileConfig['parameters']
152 )
153 );
154 }
155
156 return
157 "<div class='search-types'>" .
158 "<ul>" . implode( '', $items ) . "</ul>" .
159 "</div>";
160 }
161
162 /**
163 * Check if query starts with image: prefix
164 *
165 * @param string $term The string to check
166 * @return bool
167 */
168 protected function startsWithImage( $term ) {
169 global $wgContLang;
170
171 $parts = explode( ':', $term );
172 return count( $parts ) > 1
173 ? $wgContLang->getNsIndex( $parts[0] ) === NS_FILE
174 : false;
175 }
176
177 /**
178 * Make a search link with some target namespaces
179 *
180 * @param string $term The term to search for
181 * @param string $label Link's text
182 * @param string $tooltip Link's tooltip
183 * @param array $params Query string parameters
184 * @return string HTML fragment
185 */
186 protected function makeSearchLink( $term, $label, $tooltip, array $params = [] ) {
187 $params += [
188 'search' => $term,
189 'fulltext' => 1,
190 ];
191
192 return Xml::element(
193 'a',
194 [
195 'href' => $this->specialSearch->getPageTitle()->getLocalURL( $params ),
196 'title' => $tooltip,
197 ],
198 $label
199 );
200 }
201
202 /**
203 * Generates HTML for advanced options available with the currently
204 * selected search profile.
205 *
206 * @param string $term User provided search term
207 * @param bool $isPowerSearch Is the advanced search profile enabled?
208 * @param string $profile The current search profile
209 * @return string HTML
210 */
211 protected function optionsHtml( $term, $isPowerSearch, $profile ) {
212 $html = '';
213
214 if ( $isPowerSearch ) {
215 $html .= $this->powerSearchBox( $term, [] );
216 } else {
217 $form = '';
218 Hooks::run( 'SpecialSearchProfileForm', [
219 $this->specialSearch, &$form, $profile, $term, []
220 ] );
221 $html .= $form;
222 }
223
224 return $html;
225 }
226
227 /**
228 * @param string $term The current search term
229 * @param array $opts Additional key/value pairs that will be submitted
230 * with the generated form.
231 * @return string HTML
232 */
233 protected function powerSearchBox( $term, array $opts ) {
234 global $wgContLang;
235
236 $rows = [];
237 $activeNamespaces = $this->specialSearch->getNamespaces();
238 foreach ( $this->searchConfig->searchableNamespaces() as $namespace => $name ) {
239 $subject = MWNamespace::getSubject( $namespace );
240 if ( !isset( $rows[$subject] ) ) {
241 $rows[$subject] = "";
242 }
243
244 $name = $wgContLang->getConverter()->convertNamespace( $namespace );
245 if ( $name === '' ) {
246 $name = $this->specialSearch->msg( 'blanknamespace' )->text();
247 }
248
249 $rows[$subject] .=
250 '<td>' .
251 Xml::checkLabel(
252 $name,
253 "ns{$namespace}",
254 "mw-search-ns{$namespace}",
255 in_array( $namespace, $activeNamespaces )
256 ) .
257 '</td>';
258 }
259
260 // Lays out namespaces in multiple floating two-column tables so they'll
261 // be arranged nicely while still accomodating diferent screen widths
262 $tableRows = [];
263 foreach ( $rows as $row ) {
264 $tableRows[] = "<tr>{$row}</tr>";
265 }
266 $namespaceTables = [];
267 foreach ( array_chunk( $tableRows, 4 ) as $chunk ) {
268 $namespaceTables[] = implode( '', $chunk );
269 }
270
271 $showSections = [
272 'namespaceTables' => "<table>" . implode( '</table><table>', $namespaceTables ) . '</table>',
273 ];
274 Hooks::run( 'SpecialSearchPowerBox', [ &$showSections, $term, $opts ] );
275
276 $hidden = '';
277 foreach ( $opts as $key => $value ) {
278 $hidden .= Html::hidden( $key, $value );
279 }
280
281 $divider = "<div class='divider'></div>";
282
283 // Stuff to feed SpecialSearch::saveNamespaces()
284 $user = $this->specialSearch->getUser();
285 $remember = '';
286 if ( $user->isLoggedIn() ) {
287 $remember = $divider . Xml::checkLabel(
288 $this->specialSearch->msg( 'powersearch-remember' )->text(),
289 'nsRemember',
290 'mw-search-powersearch-remember',
291 false,
292 // The token goes here rather than in a hidden field so it
293 // is only sent when necessary (not every form submission)
294 [ 'value' => $user->getEditToken(
295 'searchnamespace',
296 $this->specialSearch->getRequest()
297 ) ]
298 );
299 }
300
301 return
302 "<fieldset id='mw-searchoptions'>" .
303 "<legend>" . $this->specialSearch->msg( 'powersearch-legend' )->escaped() . '</legend>' .
304 "<h4>" . $this->specialSearch->msg( 'powersearch-ns' )->parse() . '</h4>' .
305 // populated by js if available
306 "<div id='mw-search-togglebox'></div>" .
307 $divider .
308 implode(
309 $divider,
310 $showSections
311 ) .
312 $hidden .
313 $remember .
314 "</fieldset>";
315 }
316 }