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
;
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
{
35 /** @var MessageLocalizer */
39 /** @var array|null */
43 * @param Config $config
44 * @param MessageLocalizer $localizer
45 * @param Title $searchTitle
47 public function __construct(
49 MessageLocalizer
$localizer,
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
{
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 ) );
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 = [] ) {
92 'value' => $this->msg( $mode == 'go' ?
'searcharticle' : 'searchbutton' )->text(),
94 $realAttrs = array_merge(
96 Linker
::tooltipAndAccesskeyAttribs(
100 $this->getMessageLocalizer()
104 return Html
::element( 'input', $realAttrs );
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
);
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',
129 Linker
::tooltipAndAccesskeyAttribs(
133 $this->getMessageLocalizer()
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.
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 = [
174 $searchTitle = $this->searchTitle
;
176 $inputAttrs = $this->getSearchInputAttributes( [] );
177 $goButtonAttributes = $searchButtonAttributes +
$buttonAttributes +
[
179 ] + Linker
::tooltipAndAccesskeyAttribs(
185 $fulltextButtonAttributes = $fallbackButtonAttributes +
$buttonAttributes +
[
187 ] + Linker
::tooltipAndAccesskeyAttribs(
194 $this->cachedData
= [
195 'search-special-page-title' => $searchTitle->getText(),
196 'form-action' => $config->get( MainConfigNames
::Script
),
197 'html-button-search-fallback' => $this->makeSearchButton(
199 $fallbackButtonAttributes +
[
200 'id' => 'mw-searchButton',
203 'html-button-search' => $this->makeSearchButton(
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(
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(
225 return $this->cachedData
;