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