Thanks link and other links should be styled consistently with other links
[mediawiki.git] / includes / skins / components / SkinComponentSearch.php
blobe5798141a6d8f1efae5eb2ccd82f7b21799a436d
1 <?php
3 namespace MediaWiki\Skin;
5 use InvalidArgumentException;
6 use MediaWiki\Config\Config;
7 use MediaWiki\Html\Html;
8 use MediaWiki\Linker\Linker;
9 use MediaWiki\MainConfigNames;
10 use MediaWiki\Message\Message;
11 use MediaWiki\Title\Title;
12 use MessageLocalizer;
14 /**
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 * http://www.gnu.org/copyleft/gpl.html
30 * @internal for use inside Skin and SkinTemplate classes only
32 class SkinComponentSearch implements SkinComponent {
33 /** @var Config */
34 private $config;
35 /** @var MessageLocalizer */
36 private $localizer;
37 /** @var Title */
38 private $searchTitle;
39 /** @var array|null */
40 private $cachedData;
42 /**
43 * @param Config $config
44 * @param MessageLocalizer $localizer
45 * @param Title $searchTitle
47 public function __construct(
48 Config $config,
49 MessageLocalizer $localizer,
50 Title $searchTitle
51 ) {
52 $this->config = $config;
53 $this->localizer = $localizer;
54 $this->searchTitle = $searchTitle;
55 $this->cachedData = null;
58 private function getMessageLocalizer(): MessageLocalizer {
59 return $this->localizer;
62 private function msg( string $key ): Message {
63 return $this->localizer->msg( $key );
66 private function getConfig(): Config {
67 return $this->config;
70 /**
71 * @param array $attrs (optional) will be passed to tooltipAndAccesskeyAttribs
72 * and decorate the resulting input
73 * @return string of HTML input
75 private function makeSearchInput( array $attrs = [] ) {
76 return Html::element( 'input', $this->getSearchInputAttributes( $attrs ) );
79 /**
80 * @param string $mode representing the type of button wanted
81 * either `go` OR `fulltext`.
82 * @param array $attrs (optional)
83 * @return string of HTML button
85 private function makeSearchButton( string $mode, array $attrs = [] ) {
86 switch ( $mode ) {
87 case 'go':
88 case 'fulltext':
89 $realAttrs = [
90 'type' => 'submit',
91 'name' => $mode,
92 'value' => $this->msg( $mode == 'go' ? 'searcharticle' : 'searchbutton' )->text(),
94 $realAttrs = array_merge(
95 $realAttrs,
96 Linker::tooltipAndAccesskeyAttribs(
97 "search-$mode",
98 [],
99 null,
100 $this->getMessageLocalizer()
102 $attrs
104 return Html::element( 'input', $realAttrs );
105 default:
106 throw new InvalidArgumentException( 'Unknown mode passed to ' . __METHOD__ );
111 * @param array $attrs (optional) will be passed to tooltipAndAccesskeyAttribs
112 * and decorate the resulting input
113 * @return array attributes of HTML input
115 private function getSearchInputAttributes( array $attrs = [] ) {
116 $autoCapHint = $this->getConfig()->get( MainConfigNames::CapitalLinks );
117 $realAttrs = [
118 'type' => 'search',
119 'name' => 'search',
120 'placeholder' => $this->msg( 'searchsuggest-search' )->text(),
121 'aria-label' => $this->msg( 'searchsuggest-search' )->text(),
122 // T251664: Disable autocapitalization of input
123 // method when using fully case-sensitive titles.
124 'autocapitalize' => $autoCapHint ? 'sentences' : 'none',
127 return array_merge(
128 $realAttrs,
129 Linker::tooltipAndAccesskeyAttribs(
130 'search',
132 null,
133 $this->getMessageLocalizer()
135 $attrs
140 * @inheritDoc
141 * Since 1.38:
142 * - string html-button-fulltext-attributes HTML attributes for usage on a button
143 * that redirects user to a search page with the current query.
144 * - string html-button-go-attributes HTML attributes for usage on a search
145 * button that redirects user to a title that matches the query.
146 * - string html-input-attributes HTML attributes for input on an input field
147 * that is used to construct a search query.
148 * Since 1.35:
149 * - string form-action Where the form should post to e.g. /w/index.php
150 * - string html-button-search Search button with label
151 * derived from `html-button-go-attributes`.
152 * - string html-button-search-fallback Search button with label
153 * derived from `html-button-fulltext-attributes`.
154 * - string html-input An input element derived from `html-input-attributes`.
156 public function getTemplateData(): array {
157 // Optimization: Generate once.
158 if ( $this->cachedData ) {
159 return $this->cachedData;
162 $config = $this->getConfig();
163 $localizer = $this->getMessageLocalizer();
164 $searchButtonAttributes = [
165 'class' => 'searchButton'
167 $fallbackButtonAttributes = [
168 'class' => 'searchButton mw-fallbackSearchButton'
170 $buttonAttributes = [
171 'type' => 'submit',
174 $searchTitle = $this->searchTitle;
176 $inputAttrs = $this->getSearchInputAttributes( [] );
177 $goButtonAttributes = $searchButtonAttributes + $buttonAttributes + [
178 'name' => 'go',
179 ] + Linker::tooltipAndAccesskeyAttribs(
180 'search-go',
182 null,
183 $localizer
185 $fulltextButtonAttributes = $fallbackButtonAttributes + $buttonAttributes + [
186 'name' => 'fulltext'
187 ] + Linker::tooltipAndAccesskeyAttribs(
188 'search-fulltext',
190 null,
191 $localizer
194 $this->cachedData = [
195 'search-special-page-title' => $searchTitle->getText(),
196 'form-action' => $config->get( MainConfigNames::Script ),
197 'html-button-search-fallback' => $this->makeSearchButton(
198 'fulltext',
199 $fallbackButtonAttributes + [
200 'id' => 'mw-searchButton',
203 'html-button-search' => $this->makeSearchButton(
204 'go',
205 $searchButtonAttributes + [
206 'id' => 'searchButton',
209 'html-input' => $this->makeSearchInput( [ 'id' => 'searchInput' ] ),
210 'msg-search' => $this->msg( 'search' )->text(),
211 'page-title' => $searchTitle->getPrefixedDBkey(),
212 'array-button-go-attributes' => $goButtonAttributes,
213 'html-button-go-attributes' => Html::expandAttributes(
214 $goButtonAttributes
216 'array-button-fulltext-attributes' => $fulltextButtonAttributes,
217 'html-button-fulltext-attributes' => Html::expandAttributes(
218 $fulltextButtonAttributes
220 'array-input-attributes' => $inputAttrs,
221 'html-input-attributes' => Html::expandAttributes(
222 $inputAttrs
225 return $this->cachedData;