3 namespace MediaWiki\Widget\Search
;
7 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 Xml
::openElement(
56 'id' => $isPowerSearch ?
'powersearch' : 'search',
58 'action' => wfScript(),
61 '<div id="mw-search-top-table">' .
62 $this->shortDialogHtml( $profile, $term, $numResults, $totalResults, $offset ) .
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>" .
69 $this->optionsHtml( $term, $isPowerSearch, $profile ) .
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
81 protected function shortDialogHtml( $profile, $term, $numResults, $totalResults, $offset ) {
84 $searchWidget = new SearchInputWidget( [
87 'autofocus' => trim( $term ) === '',
89 'dataLocation' => 'content',
93 $layout = new \OOUI\
ActionFieldLayout( $searchWidget, new \OOUI\
ButtonInputWidget( [
95 'label' => $this->specialSearch
->msg( 'searchbutton' )->text(),
96 'flags' => [ 'progressive', 'primary' ],
103 if ( $totalResults > 0 && $offset < $totalResults ) {
107 'class' => 'results-info',
108 'data-mw-num-results-offset' => $offset,
109 'data-mw-num-results-total' => $totalResults
111 $this->specialSearch
->msg( 'search-showingresults' )
112 ->numParams( $offset +
1, $offset +
$numResults, $totalResults )
113 ->numParams( $numResults )
119 Html
::hidden( 'title', $this->specialSearch
->getPageTitle()->getPrefixedText() ) .
120 Html
::hidden( 'profile', $profile ) .
121 Html
::hidden( 'fulltext', '1' );
127 * Generates HTML for the list of available search profiles.
129 * @param string $profile The currently selected profile
130 * @param string $term The user provided search terms
131 * @return string HTML
133 protected function profileTabsHtml( $profile, $term ) {
134 $bareterm = $this->startsWithImage( $term )
135 ?
substr( $term, strpos( $term, ':' ) +
1 )
137 $lang = $this->specialSearch
->getLanguage();
139 foreach ( $this->profiles
as $id => $profileConfig ) {
140 $profileConfig['parameters']['profile'] = $id;
141 $tooltipParam = isset( $profileConfig['namespace-messages'] )
142 ?
$lang->commaList( $profileConfig['namespace-messages'] )
144 $items[] = Xml
::tags(
146 [ 'class' => $profile === $id ?
'current' : 'normal' ],
147 $this->makeSearchLink(
149 $this->specialSearch
->msg( $profileConfig['message'] )->text(),
150 $this->specialSearch
->msg( $profileConfig['tooltip'], $tooltipParam )->text(),
151 $profileConfig['parameters']
156 return "<div class='search-types'>" .
157 "<ul>" . implode( '', $items ) . "</ul>" .
162 * Check if query starts with image: prefix
164 * @param string $term The string to check
167 protected function startsWithImage( $term ) {
170 $parts = explode( ':', $term );
171 return count( $parts ) > 1
172 ?
$wgContLang->getNsIndex( $parts[0] ) === NS_FILE
177 * Make a search link with some target namespaces
179 * @param string $term The term to search for
180 * @param string $label Link's text
181 * @param string $tooltip Link's tooltip
182 * @param array $params Query string parameters
183 * @return string HTML fragment
185 protected function makeSearchLink( $term, $label, $tooltip, array $params = [] ) {
194 'href' => $this->specialSearch
->getPageTitle()->getLocalURL( $params ),
202 * Generates HTML for advanced options available with the currently
203 * selected search profile.
205 * @param string $term User provided search term
206 * @param bool $isPowerSearch Is the advanced search profile enabled?
207 * @param string $profile The current search profile
208 * @return string HTML
210 protected function optionsHtml( $term, $isPowerSearch, $profile ) {
213 if ( $isPowerSearch ) {
214 $html .= $this->powerSearchBox( $term, [] );
217 Hooks
::run( 'SpecialSearchProfileForm', [
218 $this->specialSearch
, &$form, $profile, $term, []
227 * @param string $term The current search term
228 * @param array $opts Additional key/value pairs that will be submitted
229 * with the generated form.
230 * @return string HTML
232 protected function powerSearchBox( $term, array $opts ) {
236 $activeNamespaces = $this->specialSearch
->getNamespaces();
237 foreach ( $this->searchConfig
->searchableNamespaces() as $namespace => $name ) {
238 $subject = MWNamespace
::getSubject( $namespace );
239 if ( !isset( $rows[$subject] ) ) {
240 $rows[$subject] = "";
243 $name = $wgContLang->getConverter()->convertNamespace( $namespace );
244 if ( $name === '' ) {
245 $name = $this->specialSearch
->msg( 'blanknamespace' )->text();
253 "mw-search-ns{$namespace}",
254 in_array( $namespace, $activeNamespaces )
259 // Lays out namespaces in multiple floating two-column tables so they'll
260 // be arranged nicely while still accomodating diferent screen widths
262 foreach ( $rows as $row ) {
263 $tableRows[] = "<tr>{$row}</tr>";
265 $namespaceTables = [];
266 foreach ( array_chunk( $tableRows, 4 ) as $chunk ) {
267 $namespaceTables[] = implode( '', $chunk );
271 'namespaceTables' => "<table>" . implode( '</table><table>', $namespaceTables ) . '</table>',
273 Hooks
::run( 'SpecialSearchPowerBox', [ &$showSections, $term, $opts ] );
276 foreach ( $opts as $key => $value ) {
277 $hidden .= Html
::hidden( $key, $value );
280 $divider = "<div class='divider'></div>";
282 // Stuff to feed SpecialSearch::saveNamespaces()
283 $user = $this->specialSearch
->getUser();
285 if ( $user->isLoggedIn() ) {
286 $remember = $divider . Xml
::checkLabel(
287 $this->specialSearch
->msg( 'powersearch-remember' )->text(),
289 'mw-search-powersearch-remember',
291 // The token goes here rather than in a hidden field so it
292 // is only sent when necessary (not every form submission)
293 [ 'value' => $user->getEditToken(
295 $this->specialSearch
->getRequest()
300 return "<fieldset id='mw-searchoptions'>" .
301 "<legend>" . $this->specialSearch
->msg( 'powersearch-legend' )->escaped() . '</legend>' .
302 "<h4>" . $this->specialSearch
->msg( 'powersearch-ns' )->parse() . '</h4>' .
303 // populated by js if available
304 "<div id='mw-search-togglebox'></div>" .