MediaWiki REL1_31
SearchFormWidget.php
Go to the documentation of this file.
1<?php
2
4
5use Hooks;
6use Html;
11use 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
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 "<div class='search-types'>" .
157 "<ul>" . implode( '', $items ) . "</ul>" .
158 "</div>";
159 }
160
167 protected function startsWithImage( $term ) {
168 global $wgContLang;
169
170 $parts = explode( ':', $term );
171 return count( $parts ) > 1
172 ? $wgContLang->getNsIndex( $parts[0] ) === NS_FILE
173 : false;
174 }
175
185 protected function makeSearchLink( $term, $label, $tooltip, array $params = [] ) {
186 $params += [
187 'search' => $term,
188 'fulltext' => 1,
189 ];
190
191 return Xml::element(
192 'a',
193 [
194 'href' => $this->specialSearch->getPageTitle()->getLocalURL( $params ),
195 'title' => $tooltip,
196 ],
197 $label
198 );
199 }
200
210 protected function optionsHtml( $term, $isPowerSearch, $profile ) {
211 $html = '';
212
213 if ( $isPowerSearch ) {
214 $html .= $this->powerSearchBox( $term, [] );
215 } else {
216 $form = '';
217 Hooks::run( 'SpecialSearchProfileForm', [
218 $this->specialSearch, &$form, $profile, $term, []
219 ] );
220 $html .= $form;
221 }
222
223 return $html;
224 }
225
232 protected function powerSearchBox( $term, array $opts ) {
233 global $wgContLang;
234
235 $rows = [];
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] = "";
241 }
242
243 $name = $wgContLang->getConverter()->convertNamespace( $namespace );
244 if ( $name === '' ) {
245 $name = $this->specialSearch->msg( 'blanknamespace' )->text();
246 }
247
248 $rows[$subject] .=
249 '<td>' .
250 Xml::checkLabel(
251 $name,
252 "ns{$namespace}",
253 "mw-search-ns{$namespace}",
254 in_array( $namespace, $activeNamespaces )
255 ) .
256 '</td>';
257 }
258
259 // Lays out namespaces in multiple floating two-column tables so they'll
260 // be arranged nicely while still accomodating diferent screen widths
261 $tableRows = [];
262 foreach ( $rows as $row ) {
263 $tableRows[] = "<tr>{$row}</tr>";
264 }
265 $namespaceTables = [];
266 foreach ( array_chunk( $tableRows, 4 ) as $chunk ) {
267 $namespaceTables[] = implode( '', $chunk );
268 }
269
270 $showSections = [
271 'namespaceTables' => "<table>" . implode( '</table><table>', $namespaceTables ) . '</table>',
272 ];
273 Hooks::run( 'SpecialSearchPowerBox', [ &$showSections, $term, $opts ] );
274
275 $hidden = '';
276 foreach ( $opts as $key => $value ) {
277 $hidden .= Html::hidden( $key, $value );
278 }
279
280 $divider = "<div class='divider'></div>";
281
282 // Stuff to feed SpecialSearch::saveNamespaces()
283 $user = $this->specialSearch->getUser();
284 $remember = '';
285 if ( $user->isLoggedIn() ) {
286 $remember = $divider . Xml::checkLabel(
287 $this->specialSearch->msg( 'powersearch-remember' )->text(),
288 'nsRemember',
289 'mw-search-powersearch-remember',
290 false,
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(
294 'searchnamespace',
295 $this->specialSearch->getRequest()
296 ) ]
297 );
298 }
299
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>" .
305 $divider .
306 implode(
307 $divider,
308 $showSections
309 ) .
310 $hidden .
311 $remember .
312 "</fieldset>";
313 }
314}
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
Hooks class.
Definition Hooks.php:34
This class is a collection of static functions that serve two purposes:
Definition Html.php:48
This is a utility class with only static functions for dealing with namespaces that encodes all the "...
profileTabsHtml( $profile, $term)
Generates HTML for the list of available search profiles.
shortDialogHtml( $profile, $term, $numResults, $totalResults, $offset)
startsWithImage( $term)
Check if query starts with image: prefix.
render( $profile, $term, $numResults, $totalResults, $offset, $isPowerSearch)
makeSearchLink( $term, $label, $tooltip, array $params=[])
Make a search link with some target namespaces.
__construct(SpecialSearch $specialSearch, SearchEngineConfig $searchConfig, array $profiles)
optionsHtml( $term, $isPowerSearch, $profile)
Generates HTML for advanced options available with the currently selected search profile.
Configuration handling class for SearchEngine.
implements Special:Search - Run text & title search and display the output
Module of static functions for generating XML.
Definition Xml.php:26
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 local content language as $wgContLang
Definition design.txt:57
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:2783
For QUnit the mediawiki tests qunit testrunner dependency will be added to any module whereas SearchGetNearMatch runs after $term
Definition hooks.txt:2845
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:2013
const NS_FILE
Definition Defines.php:80
$params
if(!isset( $args[0])) $lang