3 namespace MediaWiki\Widget\Search
;
7 use MediaWiki\MediaWikiServices
;
8 use MediaWiki\Widget\SearchInputWidget
;
10 use SearchEngineConfig
;
14 class SearchFormWidget
{
15 /** @var SpecialSearch */
16 protected $specialSearch;
17 /** @var SearchEngineConfig */
18 protected $searchConfig;
23 * @param SpecialSearch $specialSearch
24 * @param SearchEngineConfig $searchConfig
25 * @param array $profiles
27 public function __construct(
28 SpecialSearch
$specialSearch,
29 SearchEngineConfig
$searchConfig,
32 $this->specialSearch
= $specialSearch;
33 $this->searchConfig
= $searchConfig;
34 $this->profiles
= $profiles;
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?
46 public function render(
54 return '<div class="mw-search-form-wrapper">' .
58 'id' => $isPowerSearch ?
'powersearch' : 'search',
60 'action' => wfScript(),
63 '<div id="mw-search-top-table">' .
64 $this->shortDialogHtml( $profile, $term, $numResults, $totalResults, $offset ) .
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>" .
71 $this->optionsHtml( $term, $isPowerSearch, $profile ) .
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
84 protected function shortDialogHtml( $profile, $term, $numResults, $totalResults, $offset ) {
87 $searchWidget = new SearchInputWidget( [
90 'autofocus' => trim( $term ) === '',
92 'dataLocation' => 'content',
96 $layout = new \OOUI\
ActionFieldLayout( $searchWidget, new \OOUI\
ButtonInputWidget( [
98 'label' => $this->specialSearch
->msg( 'searchbutton' )->text(),
99 'flags' => [ 'progressive', 'primary' ],
106 if ( $this->specialSearch
->getPrefix() !== '' ) {
107 $html .= Html
::hidden( 'prefix', $this->specialSearch
->getPrefix() );
110 if ( $totalResults > 0 && $offset < $totalResults ) {
114 'class' => 'results-info',
115 'data-mw-num-results-offset' => $offset,
116 'data-mw-num-results-total' => $totalResults
118 $this->specialSearch
->msg( 'search-showingresults' )
119 ->numParams( $offset +
1, $offset +
$numResults, $totalResults )
120 ->numParams( $numResults )
126 Html
::hidden( 'title', $this->specialSearch
->getPageTitle()->getPrefixedText() ) .
127 Html
::hidden( 'profile', $profile ) .
128 Html
::hidden( 'fulltext', '1' );
134 * Generates HTML for the list of available search profiles.
136 * @param string $profile The currently selected profile
137 * @param string $term The user provided search terms
138 * @return string HTML
140 protected function profileTabsHtml( $profile, $term ) {
141 $bareterm = $this->startsWithImage( $term )
142 ?
substr( $term, strpos( $term, ':' ) +
1 )
144 $lang = $this->specialSearch
->getLanguage();
146 foreach ( $this->profiles
as $id => $profileConfig ) {
147 $profileConfig['parameters']['profile'] = $id;
148 $tooltipParam = isset( $profileConfig['namespace-messages'] )
149 ?
$lang->commaList( $profileConfig['namespace-messages'] )
151 $items[] = Xml
::tags(
153 [ 'class' => $profile === $id ?
'current' : 'normal' ],
154 $this->makeSearchLink(
156 $this->specialSearch
->msg( $profileConfig['message'] )->text(),
157 $this->specialSearch
->msg( $profileConfig['tooltip'], $tooltipParam )->text(),
158 $profileConfig['parameters']
163 return "<div class='search-types'>" .
164 "<ul>" . implode( '', $items ) . "</ul>" .
169 * Check if query starts with image: prefix
171 * @param string $term The string to check
174 protected function startsWithImage( $term ) {
175 $parts = explode( ':', $term );
176 return count( $parts ) > 1
177 ? MediaWikiServices
::getInstance()->getContentLanguage()->getNsIndex( $parts[0] ) ===
183 * Make a search link with some target namespaces
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
191 protected function makeSearchLink( $term, $label, $tooltip, array $params = [] ) {
200 'href' => $this->specialSearch
->getPageTitle()->getLocalURL( $params ),
208 * Generates HTML for advanced options available with the currently
209 * selected search profile.
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
216 protected function optionsHtml( $term, $isPowerSearch, $profile ) {
219 if ( $isPowerSearch ) {
220 $html .= $this->powerSearchBox( $term, [] );
223 Hooks
::run( 'SpecialSearchProfileForm', [
224 $this->specialSearch
, &$form, $profile, $term, []
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
238 protected function powerSearchBox( $term, array $opts ) {
240 $activeNamespaces = $this->specialSearch
->getNamespaces();
241 $langConverter = $this->specialSearch
->getLanguage();
242 foreach ( $this->searchConfig
->searchableNamespaces() as $namespace => $name ) {
243 $subject = MWNamespace
::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 // populated by js if available
309 "<div id='mw-search-togglebox'></div>" .