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