MediaWiki  1.32.5
SearchFormWidget.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Hooks;
6 use Html;
12 use Xml;
13 
16  protected $specialSearch;
18  protected $searchConfig;
20  protected $profiles;
21 
27  public function __construct(
31  ) {
32  $this->specialSearch = $specialSearch;
33  $this->searchConfig = $searchConfig;
34  $this->profiles = $profiles;
35  }
36 
46  public function render(
47  $profile,
48  $term,
49  $numResults,
50  $totalResults,
51  $offset,
52  $isPowerSearch
53  ) {
54  return '<div class="mw-search-form-wrapper">' .
56  'form',
57  [
58  'id' => $isPowerSearch ? 'powersearch' : 'search',
59  'method' => 'get',
60  'action' => wfScript(),
61  ]
62  ) .
63  '<div id="mw-search-top-table">' .
64  $this->shortDialogHtml( $profile, $term, $numResults, $totalResults, $offset ) .
65  '</div>' .
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>" .
70  "</div>" .
71  $this->optionsHtml( $term, $isPowerSearch, $profile ) .
72  '</form>' .
73  '</div>';
74  }
75 
84  protected function shortDialogHtml( $profile, $term, $numResults, $totalResults, $offset ) {
85  $html = '';
86 
87  $searchWidget = new SearchInputWidget( [
88  'id' => 'searchText',
89  'name' => 'search',
90  'autofocus' => trim( $term ) === '',
91  'value' => $term,
92  'dataLocation' => 'content',
93  'infusable' => true,
94  ] );
95 
96  $layout = new \OOUI\ActionFieldLayout( $searchWidget, new \OOUI\ButtonInputWidget( [
97  'type' => 'submit',
98  'label' => $this->specialSearch->msg( 'searchbutton' )->text(),
99  'flags' => [ 'progressive', 'primary' ],
100  ] ), [
101  'align' => 'top',
102  ] );
103 
104  $html .= $layout;
105 
106  if ( $this->specialSearch->getPrefix() !== '' ) {
107  $html .= Html::hidden( 'prefix', $this->specialSearch->getPrefix() );
108  }
109 
110  if ( $totalResults > 0 && $offset < $totalResults ) {
111  $html .= Xml::tags(
112  'div',
113  [
114  'class' => 'results-info',
115  'data-mw-num-results-offset' => $offset,
116  'data-mw-num-results-total' => $totalResults
117  ],
118  $this->specialSearch->msg( 'search-showingresults' )
119  ->numParams( $offset + 1, $offset + $numResults, $totalResults )
120  ->numParams( $numResults )
121  ->parse()
122  );
123  }
124 
125  $html .=
126  Html::hidden( 'title', $this->specialSearch->getPageTitle()->getPrefixedText() ) .
127  Html::hidden( 'profile', $profile ) .
128  Html::hidden( 'fulltext', '1' );
129 
130  return $html;
131  }
132 
140  protected function profileTabsHtml( $profile, $term ) {
141  $bareterm = $this->startsWithImage( $term )
142  ? substr( $term, strpos( $term, ':' ) + 1 )
143  : $term;
144  $lang = $this->specialSearch->getLanguage();
145  $items = [];
146  foreach ( $this->profiles as $id => $profileConfig ) {
147  $profileConfig['parameters']['profile'] = $id;
148  $tooltipParam = isset( $profileConfig['namespace-messages'] )
149  ? $lang->commaList( $profileConfig['namespace-messages'] )
150  : null;
151  $items[] = Xml::tags(
152  'li',
153  [ 'class' => $profile === $id ? 'current' : 'normal' ],
154  $this->makeSearchLink(
155  $bareterm,
156  $this->specialSearch->msg( $profileConfig['message'] )->text(),
157  $this->specialSearch->msg( $profileConfig['tooltip'], $tooltipParam )->text(),
158  $profileConfig['parameters']
159  )
160  );
161  }
162 
163  return "<div class='search-types'>" .
164  "<ul>" . implode( '', $items ) . "</ul>" .
165  "</div>";
166  }
167 
174  protected function startsWithImage( $term ) {
175  $parts = explode( ':', $term );
176  return count( $parts ) > 1
177  ? MediaWikiServices::getInstance()->getContentLanguage()->getNsIndex( $parts[0] ) ===
178  NS_FILE
179  : false;
180  }
181 
191  protected function makeSearchLink( $term, $label, $tooltip, array $params = [] ) {
192  $params += [
193  'search' => $term,
194  'fulltext' => 1,
195  ];
196 
197  return Xml::element(
198  'a',
199  [
200  'href' => $this->specialSearch->getPageTitle()->getLocalURL( $params ),
201  'title' => $tooltip,
202  ],
203  $label
204  );
205  }
206 
216  protected function optionsHtml( $term, $isPowerSearch, $profile ) {
217  $html = '';
218 
219  if ( $isPowerSearch ) {
220  $html .= $this->powerSearchBox( $term, [] );
221  } else {
222  $form = '';
223  Hooks::run( 'SpecialSearchProfileForm', [
224  $this->specialSearch, &$form, $profile, $term, []
225  ] );
226  $html .= $form;
227  }
228 
229  return $html;
230  }
231 
238  protected function powerSearchBox( $term, array $opts ) {
239  $rows = [];
240  $activeNamespaces = $this->specialSearch->getNamespaces();
241  $langConverter = MediaWikiServices::getInstance()->getContentLanguage()->getConverter();
242  foreach ( $this->searchConfig->searchableNamespaces() as $namespace => $name ) {
243  $subject = MWNamespace::getSubject( $namespace );
244  if ( !isset( $rows[$subject] ) ) {
245  $rows[$subject] = "";
246  }
247 
248  $name = $langConverter->convertNamespace( $namespace );
249  if ( $name === '' ) {
250  $name = $this->specialSearch->msg( 'blanknamespace' )->text();
251  }
252 
253  $rows[$subject] .=
254  '<td>' .
256  $name,
257  "ns{$namespace}",
258  "mw-search-ns{$namespace}",
259  in_array( $namespace, $activeNamespaces )
260  ) .
261  '</td>';
262  }
263 
264  // Lays out namespaces in multiple floating two-column tables so they'll
265  // be arranged nicely while still accomodating diferent screen widths
266  $tableRows = [];
267  foreach ( $rows as $row ) {
268  $tableRows[] = "<tr>{$row}</tr>";
269  }
270  $namespaceTables = [];
271  foreach ( array_chunk( $tableRows, 4 ) as $chunk ) {
272  $namespaceTables[] = implode( '', $chunk );
273  }
274 
275  $showSections = [
276  'namespaceTables' => "<table>" . implode( '</table><table>', $namespaceTables ) . '</table>',
277  ];
278  Hooks::run( 'SpecialSearchPowerBox', [ &$showSections, $term, &$opts ] );
279 
280  $hidden = '';
281  foreach ( $opts as $key => $value ) {
282  $hidden .= Html::hidden( $key, $value );
283  }
284 
285  $divider = "<div class='divider'></div>";
286 
287  // Stuff to feed SpecialSearch::saveNamespaces()
288  $user = $this->specialSearch->getUser();
289  $remember = '';
290  if ( $user->isLoggedIn() ) {
291  $remember = $divider . Xml::checkLabel(
292  $this->specialSearch->msg( 'powersearch-remember' )->text(),
293  'nsRemember',
294  'mw-search-powersearch-remember',
295  false,
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(
299  'searchnamespace',
300  $this->specialSearch->getRequest()
301  ) ]
302  );
303  }
304 
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>" .
310  $divider .
311  implode(
312  $divider,
313  $showSections
314  ) .
315  $hidden .
316  $remember .
317  "</fieldset>";
318  }
319 }
MediaWiki\Widget\Search\SearchFormWidget\$specialSearch
SpecialSearch $specialSearch
Definition: SearchFormWidget.php:16
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:244
$term
For QUnit the mediawiki tests qunit testrunner dependency will be added to any module whereas SearchGetNearMatch runs after $term
Definition: hooks.txt:2899
MediaWiki\Widget\Search\SearchFormWidget\optionsHtml
optionsHtml( $term, $isPowerSearch, $profile)
Generates HTML for advanced options available with the currently selected search profile.
Definition: SearchFormWidget.php:216
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
captcha-old.count
count
Definition: captcha-old.py:249
MediaWiki\Widget\Search
Definition: BasicSearchResultSetWidget.php:3
NS_FILE
const NS_FILE
Definition: Defines.php:70
$params
$params
Definition: styleTest.css.php:44
MediaWiki\Widget\Search\SearchFormWidget
Definition: SearchFormWidget.php:14
Xml\openElement
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:110
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MediaWiki\Widget\Search\SearchFormWidget\shortDialogHtml
shortDialogHtml( $profile, $term, $numResults, $totalResults, $offset)
Definition: SearchFormWidget.php:84
$html
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition: hooks.txt:2044
wfScript
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
Definition: GlobalFunctions.php:2771
MediaWiki\Widget\Search\SearchFormWidget\powerSearchBox
powerSearchBox( $term, array $opts)
Definition: SearchFormWidget.php:238
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
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
MWNamespace
This is a utility class with only static functions for dealing with namespaces that encodes all the "...
Definition: MWNamespace.php:32
MediaWiki\Widget\SearchInputWidget
Search input widget.
Definition: SearchInputWidget.php:11
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
MediaWiki\Widget\Search\SearchFormWidget\startsWithImage
startsWithImage( $term)
Check if query starts with image: prefix.
Definition: SearchFormWidget.php:174
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
Html\hidden
static hidden( $name, $value, array $attribs=[])
Convenience function to produce an input element with type=hidden.
Definition: Html.php:795
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:120
MediaWiki\Widget\Search\SearchFormWidget\$searchConfig
SearchEngineConfig $searchConfig
Definition: SearchFormWidget.php:18
$value
$value
Definition: styleTest.css.php:49
Xml\tags
static tags( $element, $attribs, $contents)
Same as Xml::element(), but does not escape contents.
Definition: Xml.php:132
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:27
MediaWiki\Widget\Search\SearchFormWidget\makeSearchLink
makeSearchLink( $term, $label, $tooltip, array $params=[])
Make a search link with some target namespaces.
Definition: SearchFormWidget.php:191
MediaWiki\Widget\Search\SearchFormWidget\$profiles
array $profiles
Definition: SearchFormWidget.php:20
$rows
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction $rows
Definition: hooks.txt:2683
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
SearchEngineConfig
Configuration handling class for SearchEngine.
Definition: SearchEngineConfig.php:9
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
MWNamespace\getSubject
static getSubject( $index)
Get the subject namespace index for a given namespace Special namespaces (NS_MEDIA,...
Definition: MWNamespace.php:143
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
Hooks
Hooks class.
Definition: Hooks.php:34
MediaWiki\Widget\Search\SearchFormWidget\render
render( $profile, $term, $numResults, $totalResults, $offset, $isPowerSearch)
Definition: SearchFormWidget.php:46
MediaWiki\Widget\Search\SearchFormWidget\profileTabsHtml
profileTabsHtml( $profile, $term)
Generates HTML for the list of available search profiles.
Definition: SearchFormWidget.php:140
Xml\checkLabel
static checkLabel( $label, $name, $id, $checked=false, $attribs=[])
Convenience function to build an HTML checkbox with a label.
Definition: Xml.php:421
Html
This class is a collection of static functions that serve two purposes:
Definition: Html.php:49