MediaWiki  1.29.2
SearchFormWidget.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Hooks;
6 use Html;
11 use Xml;
12 
15  protected $specialSearch;
17  protected $searchConfig;
19  protected $profiles;
20 
26  public function __construct(
30  ) {
31  $this->specialSearch = $specialSearch;
32  $this->searchConfig = $searchConfig;
33  $this->profiles = $profiles;
34  }
35 
45  public function render(
46  $profile,
47  $term,
48  $numResults,
49  $totalResults,
50  $offset,
51  $isPowerSearch
52  ) {
53  return Xml::openElement(
54  'form',
55  [
56  'id' => $isPowerSearch ? 'powersearch' : 'search',
57  'method' => 'get',
58  'action' => wfScript(),
59  ]
60  ) .
61  '<div id="mw-search-top-table">' .
62  $this->shortDialogHtml( $profile, $term, $numResults, $totalResults, $offset ) .
63  '</div>' .
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>" .
68  "</div>" .
69  $this->optionsHtml( $term, $isPowerSearch, $profile ) .
70  '</form>';
71  }
72 
81  protected function shortDialogHtml( $profile, $term, $numResults, $totalResults, $offset ) {
82  $html = '';
83 
84  $searchWidget = new SearchInputWidget( [
85  'id' => 'searchText',
86  'name' => 'search',
87  'autofocus' => trim( $term ) === '',
88  'value' => $term,
89  'dataLocation' => 'content',
90  'infusable' => true,
91  ] );
92 
93  $layout = new \OOUI\ActionFieldLayout( $searchWidget, new \OOUI\ButtonInputWidget( [
94  'type' => 'submit',
95  'label' => $this->specialSearch->msg( 'searchbutton' )->text(),
96  'flags' => [ 'progressive', 'primary' ],
97  ] ), [
98  'align' => 'top',
99  ] );
100 
101  $html .= $layout;
102 
103  if ( $totalResults > 0 && $offset < $totalResults ) {
104  $html .= Xml::tags(
105  'div',
106  [
107  'class' => 'results-info',
108  'data-mw-num-results-offset' => $offset,
109  'data-mw-num-results-total' => $totalResults
110  ],
111  $this->specialSearch->msg( 'search-showingresults' )
112  ->numParams( $offset + 1, $offset + $numResults, $totalResults )
113  ->numParams( $numResults )
114  ->parse()
115  );
116  }
117 
118  $html .=
119  Html::hidden( 'title', $this->specialSearch->getPageTitle()->getPrefixedText() ) .
120  Html::hidden( 'profile', $profile ) .
121  Html::hidden( 'fulltext', '1' );
122 
123  return $html;
124  }
125 
133  protected function profileTabsHtml( $profile, $term ) {
134  $bareterm = $this->startsWithImage( $term )
135  ? substr( $term, strpos( $term, ':' ) + 1 )
136  : $term;
137  $lang = $this->specialSearch->getLanguage();
138  $items = [];
139  foreach ( $this->profiles as $id => $profileConfig ) {
140  $profileConfig['parameters']['profile'] = $id;
141  $tooltipParam = isset( $profileConfig['namespace-messages'] )
142  ? $lang->commaList( $profileConfig['namespace-messages'] )
143  : null;
144  $items[] = Xml::tags(
145  'li',
146  [ 'class' => $profile === $id ? 'current' : 'normal' ],
147  $this->makeSearchLink(
148  $bareterm,
149  $this->specialSearch->msg( $profileConfig['message'] )->text(),
150  $this->specialSearch->msg( $profileConfig['tooltip'], $tooltipParam )->text(),
151  $profileConfig['parameters']
152  )
153  );
154  }
155 
156  return
157  "<div class='search-types'>" .
158  "<ul>" . implode( '', $items ) . "</ul>" .
159  "</div>";
160  }
161 
168  protected function startsWithImage( $term ) {
170 
171  $parts = explode( ':', $term );
172  return count( $parts ) > 1
173  ? $wgContLang->getNsIndex( $parts[0] ) === NS_FILE
174  : false;
175  }
176 
186  protected function makeSearchLink( $term, $label, $tooltip, array $params = [] ) {
187  $params += [
188  'search' => $term,
189  'fulltext' => 1,
190  ];
191 
192  return Xml::element(
193  'a',
194  [
195  'href' => $this->specialSearch->getPageTitle()->getLocalURL( $params ),
196  'title' => $tooltip,
197  ],
198  $label
199  );
200  }
201 
211  protected function optionsHtml( $term, $isPowerSearch, $profile ) {
212  $html = '';
213 
214  if ( $isPowerSearch ) {
215  $html .= $this->powerSearchBox( $term, [] );
216  } else {
217  $form = '';
218  Hooks::run( 'SpecialSearchProfileForm', [
219  $this->specialSearch, &$form, $profile, $term, []
220  ] );
221  $html .= $form;
222  }
223 
224  return $html;
225  }
226 
233  protected function powerSearchBox( $term, array $opts ) {
235 
236  $rows = [];
237  $activeNamespaces = $this->specialSearch->getNamespaces();
238  foreach ( $this->searchConfig->searchableNamespaces() as $namespace => $name ) {
239  $subject = MWNamespace::getSubject( $namespace );
240  if ( !isset( $rows[$subject] ) ) {
241  $rows[$subject] = "";
242  }
243 
244  $name = $wgContLang->getConverter()->convertNamespace( $namespace );
245  if ( $name === '' ) {
246  $name = $this->specialSearch->msg( 'blanknamespace' )->text();
247  }
248 
249  $rows[$subject] .=
250  '<td>' .
252  $name,
253  "ns{$namespace}",
254  "mw-search-ns{$namespace}",
255  in_array( $namespace, $activeNamespaces )
256  ) .
257  '</td>';
258  }
259 
260  // Lays out namespaces in multiple floating two-column tables so they'll
261  // be arranged nicely while still accomodating diferent screen widths
262  $tableRows = [];
263  foreach ( $rows as $row ) {
264  $tableRows[] = "<tr>{$row}</tr>";
265  }
266  $namespaceTables = [];
267  foreach ( array_chunk( $tableRows, 4 ) as $chunk ) {
268  $namespaceTables[] = implode( '', $chunk );
269  }
270 
271  $showSections = [
272  'namespaceTables' => "<table>" . implode( '</table><table>', $namespaceTables ) . '</table>',
273  ];
274  Hooks::run( 'SpecialSearchPowerBox', [ &$showSections, $term, $opts ] );
275 
276  $hidden = '';
277  foreach ( $opts as $key => $value ) {
278  $hidden .= Html::hidden( $key, $value );
279  }
280 
281  $divider = "<div class='divider'></div>";
282 
283  // Stuff to feed SpecialSearch::saveNamespaces()
284  $user = $this->specialSearch->getUser();
285  $remember = '';
286  if ( $user->isLoggedIn() ) {
287  $remember = $divider . Xml::checkLabel(
288  $this->specialSearch->msg( 'powersearch-remember' )->text(),
289  'nsRemember',
290  'mw-search-powersearch-remember',
291  false,
292  // The token goes here rather than in a hidden field so it
293  // is only sent when necessary (not every form submission)
294  [ 'value' => $user->getEditToken(
295  'searchnamespace',
296  $this->specialSearch->getRequest()
297  ) ]
298  );
299  }
300 
301  return
302  "<fieldset id='mw-searchoptions'>" .
303  "<legend>" . $this->specialSearch->msg( 'powersearch-legend' )->escaped() . '</legend>' .
304  "<h4>" . $this->specialSearch->msg( 'powersearch-ns' )->parse() . '</h4>' .
305  // populated by js if available
306  "<div id='mw-search-togglebox'></div>" .
307  $divider .
308  implode(
309  $divider,
310  $showSections
311  ) .
312  $hidden .
313  $remember .
314  "</fieldset>";
315  }
316 }
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:211
Xml\tags
static tags( $element, $attribs=null, $contents)
Same as Xml::element(), but does not escape contents.
Definition: Xml.php:131
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
captcha-old.count
count
Definition: captcha-old.py:225
MediaWiki\Widget\Search
Definition: BasicSearchResultSetWidget.php:3
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
$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:246
NS_FILE
const NS_FILE
Definition: Defines.php:68
$params
$params
Definition: styleTest.css.php:40
$term
external whereas SearchGetNearMatch runs after $term
Definition: hooks.txt:2759
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:304
MediaWiki\Widget\Search\SearchFormWidget
Definition: SearchFormWidget.php:13
Xml\openElement
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:109
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:81
$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:1956
wfScript
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
Definition: GlobalFunctions.php:3138
MediaWiki\Widget\Search\SearchFormWidget\powerSearchBox
powerSearchBox( $term, array $opts)
Definition: SearchFormWidget.php:233
Xml\element
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:39
MWNamespace
This is a utility class with only static functions for dealing with namespaces that encodes all the "...
Definition: MWNamespace.php:32
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
MediaWiki\Widget\SearchInputWidget
Search input widget.
Definition: SearchInputWidget.php:13
MediaWiki\Widget\Search\SearchFormWidget\startsWithImage
startsWithImage( $term)
Check if query starts with image: prefix.
Definition: SearchFormWidget.php:168
Html\hidden
static hidden( $name, $value, array $attribs=[])
Convenience function to produce an input element with type=hidden.
Definition: Html.php:746
MediaWiki\Widget\Search\SearchFormWidget\$searchConfig
SearchEngineConfig $searchConfig
Definition: SearchFormWidget.php:17
$value
$value
Definition: styleTest.css.php:45
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:186
MediaWiki\Widget\Search\SearchFormWidget\$profiles
array $profiles
Definition: SearchFormWidget.php:19
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
MWNamespace\getSubject
static getSubject( $index)
Get the subject namespace index for a given namespace Special namespaces (NS_MEDIA,...
Definition: MWNamespace.php:121
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
Xml
Module of static functions for generating XML.
Definition: Xml.php:26
Hooks
Hooks class.
Definition: Hooks.php:34
MediaWiki\Widget\Search\SearchFormWidget\render
render( $profile, $term, $numResults, $totalResults, $offset, $isPowerSearch)
Definition: SearchFormWidget.php:45
array
the array() calling protocol came about after MediaWiki 1.4rc1.
MediaWiki\Widget\Search\SearchFormWidget\profileTabsHtml
profileTabsHtml( $profile, $term)
Generates HTML for the list of available search profiles.
Definition: SearchFormWidget.php:133
Xml\checkLabel
static checkLabel( $label, $name, $id, $checked=false, $attribs=[])
Convenience function to build an HTML checkbox with a label.
Definition: Xml.php:419
Html
This class is a collection of static functions that serve two purposes:
Definition: Html.php:48
$wgContLang
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the content language as $wgContLang
Definition: design.txt:56