Merge "Special:JavaScriptTest: send RL errors to the js console"
[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 $searchWidget = new SearchInputWidget( [
83 'id' => 'searchText',
84 'name' => 'search',
85 'autofocus' => trim( $term ) === '',
86 'value' => $term,
87 'dataLocation' => 'content',
88 'infusable' => true,
89 ] );
90
91 $layout = new \OOUI\ActionFieldLayout( $searchWidget, new \OOUI\ButtonInputWidget( [
92 'type' => 'submit',
93 'label' => $this->specialSearch->msg( 'searchbutton' )->text(),
94 'flags' => [ 'progressive', 'primary' ],
95 ] ), [
96 'align' => 'top',
97 ] );
98
99 $html =
100 Html::hidden( 'title', $this->specialSearch->getPageTitle()->getPrefixedText() ) .
101 Html::hidden( 'profile', $profile ) .
102 Html::hidden( 'fulltext', '1' ) .
103 $layout;
104
105 if ( $totalResults > 0 && $offset < $totalResults ) {
106 $html .= Xml::tags(
107 'div',
108 [ 'class' => 'results-info' ],
109 $this->specialSearch->msg( 'search-showingresults' )
110 ->numParams( $offset + 1, $offset + $numResults, $totalResults )
111 ->numParams( $numResults )
112 ->parse()
113 );
114 }
115
116 return $html;
117 }
118
119 /**
120 * Generates HTML for the list of available search profiles.
121 *
122 * @param string $profile The currently selected profile
123 * @param string $term The user provided search terms
124 * @return string HTML
125 */
126 protected function profileTabsHtml( $profile, $term ) {
127 $bareterm = $this->startsWithImage( $term )
128 ? substr( $term, strpos( $term, ':' ) + 1 )
129 : $term;
130 $lang = $this->specialSearch->getLanguage();
131 $items = [];
132 foreach ( $this->profiles as $id => $profileConfig ) {
133 $profileConfig['parameters']['profile'] = $id;
134 $tooltipParam = isset( $profileConfig['namespace-messages'] )
135 ? $lang->commaList( $profileConfig['namespace-messages'] )
136 : null;
137 $items[] = Xml::tags(
138 'li',
139 [ 'class' => $profile === $id ? 'current' : 'normal' ],
140 $this->makeSearchLink(
141 $bareterm,
142 $this->specialSearch->msg( $profileConfig['message'] )->text(),
143 $this->specialSearch->msg( $profileConfig['tooltip'], $tooltipParam )->text(),
144 $profileConfig['parameters']
145 )
146 );
147 }
148
149 return
150 "<div class='search-types'>" .
151 "<ul>" . implode( '', $items ) . "</ul>" .
152 "</div>";
153 }
154
155 /**
156 * Check if query starts with image: prefix
157 *
158 * @param string $term The string to check
159 * @return bool
160 */
161 protected function startsWithImage( $term ) {
162 global $wgContLang;
163
164 $parts = explode( ':', $term );
165 return count( $parts ) > 1
166 ? $wgContLang->getNsIndex( $parts[0] ) === NS_FILE
167 : false;
168 }
169
170 /**
171 * Make a search link with some target namespaces
172 *
173 * @param string $term The term to search for
174 * @param string $label Link's text
175 * @param string $tooltip Link's tooltip
176 * @param array $params Query string parameters
177 * @return string HTML fragment
178 */
179 protected function makeSearchLink( $term, $label, $tooltip, array $params = [] ) {
180 $params += [
181 'search' => $term,
182 'fulltext' => 1,
183 ];
184
185 return Xml::element(
186 'a',
187 [
188 'href' => $this->specialSearch->getPageTitle()->getLocalURL( $params ),
189 'title' => $tooltip,
190 ],
191 $label
192 );
193 }
194
195 /**
196 * Generates HTML for advanced options available with the currently
197 * selected search profile.
198 *
199 * @param string $term User provided search term
200 * @param bool $isPowerSearch Is the advanced search profile enabled?
201 * @param string $profile The current search profile
202 * @return string HTML
203 */
204 protected function optionsHtml( $term, $isPowerSearch, $profile ) {
205 $html = '';
206 $opts = [
207 'profile' => $profile,
208 ];
209
210 if ( $isPowerSearch ) {
211 $html .= $this->powerSearchBox( $term, $opts );
212 } else {
213 $form = '';
214 Hooks::run( 'SpecialSearchProfileForm', [
215 $this->specialSearch, &$form, $profile, $term, $opts
216 ] );
217 $html .= $form;
218 }
219
220 return $html;
221 }
222
223 /**
224 * @param string $term The current search term
225 * @param array $opts Additional key/value pairs that will be submitted
226 * with the generated form.
227 * @return string HTML
228 */
229 protected function powerSearchBox( $term, array $opts ) {
230 global $wgContLang;
231
232 $rows = [];
233 $activeNamespaces = $this->specialSearch->getNamespaces();
234 foreach ( $this->searchConfig->searchableNamespaces() as $namespace => $name ) {
235 $subject = MWNamespace::getSubject( $namespace );
236 if ( !isset( $rows[$subject] ) ) {
237 $rows[$subject] = "";
238 }
239
240 $name = $wgContLang->getConverter()->convertNamespace( $namespace );
241 if ( $name === '' ) {
242 $name = $this->specialSearch->msg( 'blanknamespace' )->text();
243 }
244
245 $rows[$subject] .=
246 '<td>' .
247 Xml::checkLabel(
248 $name,
249 "ns{$namespace}",
250 "mw-search-ns{$namespace}",
251 in_array( $namespace, $activeNamespaces )
252 ) .
253 '</td>';
254 }
255
256 // Lays out namespaces in multiple floating two-column tables so they'll
257 // be arranged nicely while still accomodating diferent screen widths
258 $tableRows = [];
259 foreach ( $rows as $row ) {
260 $tableRows[] = "<tr>{$row}</tr>";
261 }
262 $namespaceTables = [];
263 foreach ( array_chunk( $tableRows, 4 ) as $chunk ) {
264 $namespaceTables[] = implode( '', $chunk );
265 }
266
267 $showSections = [
268 'namespaceTables' => "<table>" . implode( '</table><table>', $namespaceTables ) . '</table>',
269 ];
270 Hooks::run( 'SpecialSearchPowerBox', [ &$showSections, $term, $opts ] );
271
272 $hidden = '';
273 foreach ( $opts as $key => $value ) {
274 $hidden .= Html::hidden( $key, $value );
275 }
276
277 $divider = "<div class='divider'></div>";
278
279 // Stuff to feed SpecialSearch::saveNamespaces()
280 $user = $this->specialSearch->getUser();
281 $remember = '';
282 if ( $user->isLoggedIn() ) {
283 $remember = $divider . Xml::checkLabel(
284 $this->specialSearch->msg( 'powersearch-remember' )->text(),
285 'nsRemember',
286 'mw-search-powersearch-remember',
287 false,
288 // The token goes here rather than in a hidden field so it
289 // is only sent when necessary (not every form submission)
290 [ 'value' => $user->getEditToken(
291 'searchnamespace',
292 $this->specialSearch->getRequest()
293 ) ]
294 );
295 }
296
297 return
298 "<fieldset id='mw-searchoptions'>" .
299 "<legend>" . $this->specialSearch->msg( 'powersearch-legend' )->escaped() . '</legend>' .
300 "<h4>" . $this->specialSearch->msg( 'powersearch-ns' )->parse() . '</h4>' .
301 // populated by js if available
302 "<div id='mw-search-togglebox'></div>" .
303 $divider .
304 implode(
305 $divider,
306 $showSections
307 ) .
308 $hidden .
309 $remember .
310 "</fieldset>";
311 }
312 }