3 namespace MediaWiki\Widget\Search
;
7 use MediaWiki\MediaWikiServices
;
8 use MediaWiki\Widget\SearchInputWidget
;
9 use SearchEngineConfig
;
13 class SearchFormWidget
{
14 /** @var SpecialSearch */
15 protected $specialSearch;
16 /** @var SearchEngineConfig */
17 protected $searchConfig;
22 * @param SpecialSearch $specialSearch
23 * @param SearchEngineConfig $searchConfig
24 * @param array $profiles
26 public function __construct(
27 SpecialSearch
$specialSearch,
28 SearchEngineConfig
$searchConfig,
31 $this->specialSearch
= $specialSearch;
32 $this->searchConfig
= $searchConfig;
33 $this->profiles
= $profiles;
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?
45 public function render(
53 return '<div class="mw-search-form-wrapper">' .
57 'id' => $isPowerSearch ?
'powersearch' : 'search',
59 'action' => wfScript(),
62 '<div id="mw-search-top-table">' .
63 $this->shortDialogHtml( $profile, $term, $numResults, $totalResults, $offset ) .
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>" .
70 $this->optionsHtml( $term, $isPowerSearch, $profile ) .
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
83 protected function shortDialogHtml( $profile, $term, $numResults, $totalResults, $offset ) {
86 $searchWidget = new SearchInputWidget( [
89 'autofocus' => trim( $term ) === '',
91 'dataLocation' => 'content',
95 $layout = new \OOUI\
ActionFieldLayout( $searchWidget, new \OOUI\
ButtonInputWidget( [
97 'label' => $this->specialSearch
->msg( 'searchbutton' )->text(),
98 'flags' => [ 'progressive', 'primary' ],
105 if ( $this->specialSearch
->getPrefix() !== '' ) {
106 $html .= Html
::hidden( 'prefix', $this->specialSearch
->getPrefix() );
109 if ( $totalResults > 0 && $offset < $totalResults ) {
113 'class' => 'results-info',
114 'data-mw-num-results-offset' => $offset,
115 'data-mw-num-results-total' => $totalResults
117 $this->specialSearch
->msg( 'search-showingresults' )
118 ->numParams( $offset +
1, $offset +
$numResults, $totalResults )
119 ->numParams( $numResults )
125 Html
::hidden( 'title', $this->specialSearch
->getPageTitle()->getPrefixedText() ) .
126 Html
::hidden( 'profile', $profile ) .
127 Html
::hidden( 'fulltext', '1' );
133 * Generates HTML for the list of available search profiles.
135 * @param string $profile The currently selected profile
136 * @param string $term The user provided search terms
137 * @return string HTML
139 protected function profileTabsHtml( $profile, $term ) {
140 $bareterm = $this->startsWithImage( $term )
141 ?
substr( $term, strpos( $term, ':' ) +
1 )
143 $lang = $this->specialSearch
->getLanguage();
145 foreach ( $this->profiles
as $id => $profileConfig ) {
146 $profileConfig['parameters']['profile'] = $id;
147 $tooltipParam = isset( $profileConfig['namespace-messages'] )
148 ?
$lang->commaList( $profileConfig['namespace-messages'] )
150 $items[] = Xml
::tags(
152 [ 'class' => $profile === $id ?
'current' : 'normal' ],
153 $this->makeSearchLink(
155 $this->specialSearch
->msg( $profileConfig['message'] )->text(),
156 $this->specialSearch
->msg( $profileConfig['tooltip'], $tooltipParam )->text(),
157 $profileConfig['parameters']
162 return "<div class='search-types'>" .
163 "<ul>" . implode( '', $items ) . "</ul>" .
168 * Check if query starts with image: prefix
170 * @param string $term The string to check
173 protected function startsWithImage( $term ) {
174 $parts = explode( ':', $term );
175 return count( $parts ) > 1
176 ? MediaWikiServices
::getInstance()->getContentLanguage()->getNsIndex( $parts[0] ) ===
182 * Make a search link with some target namespaces
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
190 protected function makeSearchLink( $term, $label, $tooltip, array $params = [] ) {
199 'href' => $this->specialSearch
->getPageTitle()->getLocalURL( $params ),
207 * Generates HTML for advanced options available with the currently
208 * selected search profile.
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
215 protected function optionsHtml( $term, $isPowerSearch, $profile ) {
218 if ( $isPowerSearch ) {
219 $html .= $this->powerSearchBox( $term, [] );
222 Hooks
::run( 'SpecialSearchProfileForm', [
223 $this->specialSearch
, &$form, $profile, $term, []
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
237 protected function powerSearchBox( $term, array $opts ) {
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] = "";
248 $name = $langConverter->convertNamespace( $namespace );
249 if ( $name === '' ) {
250 $name = $this->specialSearch
->msg( 'blanknamespace' )->text();
258 "mw-search-ns{$namespace}",
259 in_array( $namespace, $activeNamespaces )
264 // Lays out namespaces in multiple floating two-column tables so they'll
265 // be arranged nicely while still accomodating diferent screen widths
267 foreach ( $rows as $row ) {
268 $tableRows[] = "<tr>{$row}</tr>";
270 $namespaceTables = [];
271 foreach ( array_chunk( $tableRows, 4 ) as $chunk ) {
272 $namespaceTables[] = implode( '', $chunk );
276 'namespaceTables' => "<table>" . implode( '</table><table>', $namespaceTables ) . '</table>',
278 Hooks
::run( 'SpecialSearchPowerBox', [ &$showSections, $term, &$opts ] );
281 foreach ( $opts as $key => $value ) {
282 $hidden .= Html
::hidden( $key, $value );
285 $divider = "<div class='divider'></div>";
287 // Stuff to feed SpecialSearch::saveNamespaces()
288 $user = $this->specialSearch
->getUser();
290 if ( $user->isLoggedIn() ) {
291 $remember = $divider . Xml
::checkLabel(
292 $this->specialSearch
->msg( 'powersearch-remember' )->text(),
294 'mw-search-powersearch-remember',
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(
300 $this->specialSearch
->getRequest()
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() . '"/>' .