Merge "RevisionStoreDbTestBase, remove redundant needsDB override"
[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 MWNamespace;
10 use SearchEngineConfig;
11 use SpecialSearch;
12 use Xml;
13
14 class SearchFormWidget {
15 /** @var SpecialSearch */
16 protected $specialSearch;
17 /** @var SearchEngineConfig */
18 protected $searchConfig;
19 /** @var array */
20 protected $profiles;
21
22 /**
23 * @param SpecialSearch $specialSearch
24 * @param SearchEngineConfig $searchConfig
25 * @param array $profiles
26 */
27 public function __construct(
28 SpecialSearch $specialSearch,
29 SearchEngineConfig $searchConfig,
30 array $profiles
31 ) {
32 $this->specialSearch = $specialSearch;
33 $this->searchConfig = $searchConfig;
34 $this->profiles = $profiles;
35 }
36
37 /**
38 * @param string $profile The current search profile
39 * @param string $term The current search term
40 * @param int $numResults The number of results shown
41 * @param int $totalResults The total estimated results found
42 * @param int $offset Current offset in search results
43 * @param bool $isPowerSearch Is the 'advanced' section open?
44 * @return string HTML
45 */
46 public function render(
47 $profile,
48 $term,
49 $numResults,
50 $totalResults,
51 $offset,
52 $isPowerSearch
53 ) {
54 return '<div class="mw-search-form-wrapper">' .
55 Xml::openElement(
56 'form',
57 [
58 'id' => $isPowerSearch ? 'powersearch' : 'search',
59 'method' => 'get',
60 'action' => wfScript(),
61 ]
62 ) .
63 '<div id="mw-search-top-table">' .
64 $this->shortDialogHtml( $profile, $term, $numResults, $totalResults, $offset ) .
65 '</div>' .
66 "<div class='mw-search-visualclear'></div>" .
67 "<div class='mw-search-profile-tabs'>" .
68 $this->profileTabsHtml( $profile, $term ) .
69 "<div style='clear:both'></div>" .
70 "</div>" .
71 $this->optionsHtml( $term, $isPowerSearch, $profile ) .
72 '</form>' .
73 '</div>';
74 }
75
76 /**
77 * @param string $profile The current search profile
78 * @param string $term The current search term
79 * @param int $numResults The number of results shown
80 * @param int $totalResults The total estimated results found
81 * @param int $offset Current offset in search results
82 * @return string HTML
83 */
84 protected function shortDialogHtml( $profile, $term, $numResults, $totalResults, $offset ) {
85 $html = '';
86
87 $searchWidget = new SearchInputWidget( [
88 'id' => 'searchText',
89 'name' => 'search',
90 'autofocus' => trim( $term ) === '',
91 'value' => $term,
92 'dataLocation' => 'content',
93 'infusable' => true,
94 ] );
95
96 $layout = new \OOUI\ActionFieldLayout( $searchWidget, new \OOUI\ButtonInputWidget( [
97 'type' => 'submit',
98 'label' => $this->specialSearch->msg( 'searchbutton' )->text(),
99 'flags' => [ 'progressive', 'primary' ],
100 ] ), [
101 'align' => 'top',
102 ] );
103
104 $html .= $layout;
105
106 if ( $this->specialSearch->getPrefix() !== '' ) {
107 $html .= Html::hidden( 'prefix', $this->specialSearch->getPrefix() );
108 }
109
110 if ( $totalResults > 0 && $offset < $totalResults ) {
111 $html .= Xml::tags(
112 'div',
113 [
114 'class' => 'results-info',
115 'data-mw-num-results-offset' => $offset,
116 'data-mw-num-results-total' => $totalResults
117 ],
118 $this->specialSearch->msg( 'search-showingresults' )
119 ->numParams( $offset + 1, $offset + $numResults, $totalResults )
120 ->numParams( $numResults )
121 ->parse()
122 );
123 }
124
125 $html .=
126 Html::hidden( 'title', $this->specialSearch->getPageTitle()->getPrefixedText() ) .
127 Html::hidden( 'profile', $profile ) .
128 Html::hidden( 'fulltext', '1' );
129
130 return $html;
131 }
132
133 /**
134 * Generates HTML for the list of available search profiles.
135 *
136 * @param string $profile The currently selected profile
137 * @param string $term The user provided search terms
138 * @return string HTML
139 */
140 protected function profileTabsHtml( $profile, $term ) {
141 $bareterm = $this->startsWithImage( $term )
142 ? substr( $term, strpos( $term, ':' ) + 1 )
143 : $term;
144 $lang = $this->specialSearch->getLanguage();
145 $items = [];
146 foreach ( $this->profiles as $id => $profileConfig ) {
147 $profileConfig['parameters']['profile'] = $id;
148 $tooltipParam = isset( $profileConfig['namespace-messages'] )
149 ? $lang->commaList( $profileConfig['namespace-messages'] )
150 : null;
151 $items[] = Xml::tags(
152 'li',
153 [ 'class' => $profile === $id ? 'current' : 'normal' ],
154 $this->makeSearchLink(
155 $bareterm,
156 $this->specialSearch->msg( $profileConfig['message'] )->text(),
157 $this->specialSearch->msg( $profileConfig['tooltip'], $tooltipParam )->text(),
158 $profileConfig['parameters']
159 )
160 );
161 }
162
163 return "<div class='search-types'>" .
164 "<ul>" . implode( '', $items ) . "</ul>" .
165 "</div>";
166 }
167
168 /**
169 * Check if query starts with image: prefix
170 *
171 * @param string $term The string to check
172 * @return bool
173 */
174 protected function startsWithImage( $term ) {
175 $parts = explode( ':', $term );
176 return count( $parts ) > 1
177 ? MediaWikiServices::getInstance()->getContentLanguage()->getNsIndex( $parts[0] ) ===
178 NS_FILE
179 : false;
180 }
181
182 /**
183 * Make a search link with some target namespaces
184 *
185 * @param string $term The term to search for
186 * @param string $label Link's text
187 * @param string $tooltip Link's tooltip
188 * @param array $params Query string parameters
189 * @return string HTML fragment
190 */
191 protected function makeSearchLink( $term, $label, $tooltip, array $params = [] ) {
192 $params += [
193 'search' => $term,
194 'fulltext' => 1,
195 ];
196
197 return Xml::element(
198 'a',
199 [
200 'href' => $this->specialSearch->getPageTitle()->getLocalURL( $params ),
201 'title' => $tooltip,
202 ],
203 $label
204 );
205 }
206
207 /**
208 * Generates HTML for advanced options available with the currently
209 * selected search profile.
210 *
211 * @param string $term User provided search term
212 * @param bool $isPowerSearch Is the advanced search profile enabled?
213 * @param string $profile The current search profile
214 * @return string HTML
215 */
216 protected function optionsHtml( $term, $isPowerSearch, $profile ) {
217 $html = '';
218
219 if ( $isPowerSearch ) {
220 $html .= $this->powerSearchBox( $term, [] );
221 } else {
222 $form = '';
223 Hooks::run( 'SpecialSearchProfileForm', [
224 $this->specialSearch, &$form, $profile, $term, []
225 ] );
226 $html .= $form;
227 }
228
229 return $html;
230 }
231
232 /**
233 * @param string $term The current search term
234 * @param array $opts Additional key/value pairs that will be submitted
235 * with the generated form.
236 * @return string HTML
237 */
238 protected function powerSearchBox( $term, array $opts ) {
239 $rows = [];
240 $activeNamespaces = $this->specialSearch->getNamespaces();
241 $langConverter = MediaWikiServices::getInstance()->getContentLanguage()->getConverter();
242 foreach ( $this->searchConfig->searchableNamespaces() as $namespace => $name ) {
243 $subject = MWNamespace::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 // populated by js if available
309 "<div id='mw-search-togglebox'></div>" .
310 $divider .
311 implode(
312 $divider,
313 $showSections
314 ) .
315 $hidden .
316 $remember .
317 "</fieldset>";
318 }
319 }