MediaWiki  1.34.0
SearchFormWidget.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Hooks;
6 use Html;
10 use SpecialSearch;
11 use Xml;
12 
15  protected $specialSearch;
17  protected $searchConfig;
19  protected $profiles;
20 
26  public function __construct(
29  array $profiles
30  ) {
31  $this->specialSearch = $specialSearch;
32  $this->searchConfig = $searchConfig;
33  $this->profiles = $profiles;
34  }
35 
46  public function render(
47  $profile,
48  $term,
49  $numResults,
50  $totalResults,
51  $offset,
52  $isPowerSearch,
53  array $options = []
54  ) {
55  $user = $this->specialSearch->getUser();
56 
57  return '<div class="mw-search-form-wrapper">' .
59  'form',
60  [
61  'id' => $isPowerSearch ? 'powersearch' : 'search',
62  // T151903: default to POST in case JS is disabled
63  'method' => ( $isPowerSearch && $user->isLoggedIn() ) ? 'post' : 'get',
64  'action' => wfScript(),
65  ]
66  ) .
67  '<div id="mw-search-top-table">' .
68  $this->shortDialogHtml( $profile, $term, $numResults, $totalResults, $offset, $options ) .
69  '</div>' .
70  "<div class='mw-search-visualclear'></div>" .
71  "<div class='mw-search-profile-tabs'>" .
72  $this->profileTabsHtml( $profile, $term ) .
73  "<div style='clear:both'></div>" .
74  "</div>" .
75  $this->optionsHtml( $term, $isPowerSearch, $profile ) .
76  '</form>' .
77  '</div>';
78  }
79 
89  protected function shortDialogHtml(
90  $profile,
91  $term,
92  $numResults,
93  $totalResults,
94  $offset,
95  array $options = []
96  ) {
97  $html = '';
98 
99  $searchWidget = new SearchInputWidget( $options + [
100  'id' => 'searchText',
101  'name' => 'search',
102  'autofocus' => trim( $term ) === '',
103  'value' => $term,
104  'dataLocation' => 'content',
105  'infusable' => true,
106  ] );
107 
108  $layout = new \OOUI\ActionFieldLayout( $searchWidget, new \OOUI\ButtonInputWidget( [
109  'type' => 'submit',
110  'label' => $this->specialSearch->msg( 'searchbutton' )->text(),
111  'flags' => [ 'progressive', 'primary' ],
112  ] ), [
113  'align' => 'top',
114  ] );
115 
116  $html .= $layout;
117 
118  if ( $this->specialSearch->getPrefix() !== '' ) {
119  $html .= Html::hidden( 'prefix', $this->specialSearch->getPrefix() );
120  }
121 
122  if ( $totalResults > 0 && $offset < $totalResults ) {
123  $html .= Xml::tags(
124  'div',
125  [
126  'class' => 'results-info',
127  'data-mw-num-results-offset' => $offset,
128  'data-mw-num-results-total' => $totalResults
129  ],
130  $this->specialSearch->msg( 'search-showingresults' )
131  ->numParams( $offset + 1, $offset + $numResults, $totalResults )
132  ->numParams( $numResults )
133  ->parse()
134  );
135  }
136 
137  $html .=
138  Html::hidden( 'title', $this->specialSearch->getPageTitle()->getPrefixedText() ) .
139  Html::hidden( 'profile', $profile ) .
140  Html::hidden( 'fulltext', '1' );
141 
142  return $html;
143  }
144 
153  protected function profileTabsHtml( $profile, $term ) {
154  $bareterm = $this->startsWithImage( $term )
155  ? substr( $term, strpos( $term, ':' ) + 1 )
156  : $term;
157  $lang = $this->specialSearch->getLanguage();
158  $items = [];
159  foreach ( $this->profiles as $id => $profileConfig ) {
160  $profileConfig['parameters']['profile'] = $id;
161  $tooltipParam = isset( $profileConfig['namespace-messages'] )
162  ? $lang->commaList( $profileConfig['namespace-messages'] )
163  : null;
164  $items[] = Xml::tags(
165  'li',
166  [ 'class' => $profile === $id ? 'current' : 'normal' ],
167  $this->makeSearchLink(
168  $bareterm,
169  $this->specialSearch->msg( $profileConfig['message'] )->text(),
170  $this->specialSearch->msg( $profileConfig['tooltip'], $tooltipParam )->text(),
171  $profileConfig['parameters']
172  )
173  );
174  }
175 
176  return "<div class='search-types'>" .
177  "<ul>" . implode( '', $items ) . "</ul>" .
178  "</div>";
179  }
180 
187  protected function startsWithImage( $term ) {
188  $parts = explode( ':', $term );
189  return count( $parts ) > 1
190  ? MediaWikiServices::getInstance()->getContentLanguage()->getNsIndex( $parts[0] ) ===
191  NS_FILE
192  : false;
193  }
194 
204  protected function makeSearchLink( $term, $label, $tooltip, array $params = [] ) {
205  $params += [
206  'search' => $term,
207  'fulltext' => 1,
208  ];
209 
210  return Xml::element(
211  'a',
212  [
213  'href' => $this->specialSearch->getPageTitle()->getLocalURL( $params ),
214  'title' => $tooltip,
215  ],
216  $label
217  );
218  }
219 
229  protected function optionsHtml( $term, $isPowerSearch, $profile ) {
230  $html = '';
231 
232  if ( $isPowerSearch ) {
233  $html .= $this->powerSearchBox( $term, [] );
234  } else {
235  $form = '';
236  Hooks::run( 'SpecialSearchProfileForm', [
237  $this->specialSearch, &$form, $profile, $term, []
238  ] );
239  $html .= $form;
240  }
241 
242  return $html;
243  }
244 
251  protected function powerSearchBox( $term, array $opts ) {
252  $rows = [];
253  $activeNamespaces = $this->specialSearch->getNamespaces();
254  $langConverter = $this->specialSearch->getLanguage();
255  foreach ( $this->searchConfig->searchableNamespaces() as $namespace => $name ) {
256  $subject = MediaWikiServices::getInstance()->getNamespaceInfo()->
257  getSubject( $namespace );
258  if ( !isset( $rows[$subject] ) ) {
259  $rows[$subject] = "";
260  }
261 
262  $name = $langConverter->convertNamespace( $namespace );
263  if ( $name === '' ) {
264  $name = $this->specialSearch->msg( 'blanknamespace' )->text();
265  }
266 
267  $rows[$subject] .=
268  '<td>' .
270  $name,
271  "ns{$namespace}",
272  "mw-search-ns{$namespace}",
273  in_array( $namespace, $activeNamespaces )
274  ) .
275  '</td>';
276  }
277 
278  // Lays out namespaces in multiple floating two-column tables so they'll
279  // be arranged nicely while still accomodating diferent screen widths
280  $tableRows = [];
281  foreach ( $rows as $row ) {
282  $tableRows[] = "<tr>{$row}</tr>";
283  }
284  $namespaceTables = [];
285  foreach ( array_chunk( $tableRows, 4 ) as $chunk ) {
286  $namespaceTables[] = implode( '', $chunk );
287  }
288 
289  $showSections = [
290  'namespaceTables' => "<table>" . implode( '</table><table>', $namespaceTables ) . '</table>',
291  ];
292  Hooks::run( 'SpecialSearchPowerBox', [ &$showSections, $term, &$opts ] );
293 
294  $hidden = '';
295  foreach ( $opts as $key => $value ) {
296  $hidden .= Html::hidden( $key, $value );
297  }
298 
299  $divider = "<div class='divider'></div>";
300 
301  // Stuff to feed SpecialSearch::saveNamespaces()
302  $user = $this->specialSearch->getUser();
303  $remember = '';
304  if ( $user->isLoggedIn() ) {
305  $remember = $divider . Xml::checkLabel(
306  $this->specialSearch->msg( 'powersearch-remember' )->text(),
307  'nsRemember',
308  'mw-search-powersearch-remember',
309  false,
310  // The token goes here rather than in a hidden field so it
311  // is only sent when necessary (not every form submission)
312  [ 'value' => $user->getEditToken(
313  'searchnamespace',
314  $this->specialSearch->getRequest()
315  ) ]
316  );
317  }
318 
319  return "<fieldset id='mw-searchoptions'>" .
320  "<legend>" . $this->specialSearch->msg( 'powersearch-legend' )->escaped() . '</legend>' .
321  "<h4>" . $this->specialSearch->msg( 'powersearch-ns' )->parse() . '</h4>' .
322  // Handled by JavaScript if available
323  '<div id="mw-search-togglebox">' .
324  '<label>' . $this->specialSearch->msg( 'powersearch-togglelabel' )->escaped() . '</label>' .
325  '<input type="button" id="mw-search-toggleall" value="' .
326  $this->specialSearch->msg( 'powersearch-toggleall' )->escaped() . '"/>' .
327  '<input type="button" id="mw-search-togglenone" value="' .
328  $this->specialSearch->msg( 'powersearch-togglenone' )->escaped() . '"/>' .
329  '</div>' .
330  $divider .
331  implode(
332  $divider,
333  $showSections
334  ) .
335  $hidden .
336  $remember .
337  "</fieldset>";
338  }
339 }
MediaWiki\Widget\Search\SearchFormWidget\$specialSearch
SpecialSearch $specialSearch
Definition: SearchFormWidget.php:15
MediaWiki\Widget\Search\SearchFormWidget\optionsHtml
optionsHtml( $term, $isPowerSearch, $profile)
Generates HTML for advanced options available with the currently selected search profile.
Definition: SearchFormWidget.php:229
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
MediaWiki\Widget\Search
Definition: BasicSearchResultSetWidget.php:3
NS_FILE
const NS_FILE
Definition: Defines.php:66
MediaWiki\Widget\Search\SearchFormWidget
Definition: SearchFormWidget.php:13
Xml\openElement
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:108
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:138
wfScript
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
Definition: GlobalFunctions.php:2642
MediaWiki\Widget\Search\SearchFormWidget\powerSearchBox
powerSearchBox( $term, array $opts)
Definition: SearchFormWidget.php:251
MediaWiki\Widget\Search\SearchFormWidget\render
render( $profile, $term, $numResults, $totalResults, $offset, $isPowerSearch, array $options=[])
Definition: SearchFormWidget.php:46
Xml\element
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:41
MediaWiki\Widget\SearchInputWidget
Search input widget.
Definition: SearchInputWidget.php:11
MediaWiki\Widget\Search\SearchFormWidget\startsWithImage
startsWithImage( $term)
Check if query starts with image: prefix.
Definition: SearchFormWidget.php:187
MediaWiki\Widget\Search\SearchFormWidget\$searchConfig
SearchEngineConfig $searchConfig
Definition: SearchFormWidget.php:17
Xml\tags
static tags( $element, $attribs, $contents)
Same as Xml::element(), but does not escape contents.
Definition: Xml.php:130
SpecialSearch
implements Special:Search - Run text & title search and display the output
Definition: SpecialSearch.php:38
MediaWiki\Widget\Search\SearchFormWidget\__construct
__construct(SpecialSearch $specialSearch, SearchEngineConfig $searchConfig, array $profiles)
Definition: SearchFormWidget.php:26
MediaWiki\Widget\Search\SearchFormWidget\makeSearchLink
makeSearchLink( $term, $label, $tooltip, array $params=[])
Make a search link with some target namespaces.
Definition: SearchFormWidget.php:204
MediaWiki\Widget\Search\SearchFormWidget\$profiles
array $profiles
Definition: SearchFormWidget.php:19
SearchEngineConfig
Configuration handling class for SearchEngine.
Definition: SearchEngineConfig.php:9
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
Xml
Module of static functions for generating XML.
Definition: Xml.php:28
MediaWiki\Widget\Search\SearchFormWidget\shortDialogHtml
shortDialogHtml( $profile, $term, $numResults, $totalResults, $offset, array $options=[])
Definition: SearchFormWidget.php:89
Hooks
Hooks class.
Definition: Hooks.php:34
MediaWiki\Widget\Search\SearchFormWidget\profileTabsHtml
profileTabsHtml( $profile, $term)
Generates HTML for the list of available search profiles.
Definition: SearchFormWidget.php:153
Xml\checkLabel
static checkLabel( $label, $name, $id, $checked=false, $attribs=[])
Convenience function to build an HTML checkbox with a label.
Definition: Xml.php:419