MediaWiki  1.29.1
HTMLTextField.php
Go to the documentation of this file.
1 <?php
2 
12  protected $mPlaceholder = '';
13 
15  protected $autocomplete;
16 
26  public function __construct( $params ) {
27  if ( isset( $params['autocomplete'] ) && is_bool( $params['autocomplete'] ) ) {
28  $params['autocomplete'] = $params['autocomplete'] ? 'on' : 'off';
29  }
30 
31  parent::__construct( $params );
32 
33  if ( isset( $params['placeholder-message'] ) ) {
34  $this->mPlaceholder = $this->getMessage( $params['placeholder-message'] )->parse();
35  } elseif ( isset( $params['placeholder'] ) ) {
36  $this->mPlaceholder = $params['placeholder'];
37  }
38  }
39 
40  public function getSize() {
41  return isset( $this->mParams['size'] ) ? $this->mParams['size'] : 45;
42  }
43 
44  public function getSpellCheck() {
45  $val = isset( $this->mParams['spellcheck'] ) ? $this->mParams['spellcheck'] : null;
46  if ( is_bool( $val ) ) {
47  // "spellcheck" attribute literally requires "true" or "false" to work.
48  return $val === true ? 'true' : 'false';
49  }
50  return null;
51  }
52 
53  public function isPersistent() {
54  if ( isset( $this->mParams['persistent'] ) ) {
55  return $this->mParams['persistent'];
56  }
57  // don't put passwords into the HTML body, they could get cached or otherwise leaked
58  return !( isset( $this->mParams['type'] ) && $this->mParams['type'] === 'password' );
59  }
60 
61  public function getInputHTML( $value ) {
62  if ( !$this->isPersistent() ) {
63  $value = '';
64  }
65 
66  $attribs = [
67  'id' => $this->mID,
68  'name' => $this->mName,
69  'size' => $this->getSize(),
70  'value' => $value,
71  'dir' => $this->mDir,
72  'spellcheck' => $this->getSpellCheck(),
73  ] + $this->getTooltipAndAccessKey() + $this->getDataAttribs();
74 
75  if ( $this->mClass !== '' ) {
76  $attribs['class'] = $this->mClass;
77  }
78  if ( $this->mPlaceholder !== '' ) {
79  $attribs['placeholder'] = $this->mPlaceholder;
80  }
81 
82  # @todo Enforce pattern, step, required, readonly on the server side as
83  # well
84  $allowedParams = [
85  'type',
86  'min',
87  'max',
88  'pattern',
89  'title',
90  'step',
91  'list',
92  'maxlength',
93  'tabindex',
94  'disabled',
95  'required',
96  'autofocus',
97  'multiple',
98  'readonly',
99  'autocomplete',
100  ];
101 
102  $attribs += $this->getAttributes( $allowedParams );
103 
104  # Extract 'type'
105  $type = $this->getType( $attribs );
106  return Html::input( $this->mName, $value, $type, $attribs );
107  }
108 
109  protected function getType( &$attribs ) {
110  $type = isset( $attribs['type'] ) ? $attribs['type'] : 'text';
111  unset( $attribs['type'] );
112 
113  # Implement tiny differences between some field variants
114  # here, rather than creating a new class for each one which
115  # is essentially just a clone of this one.
116  if ( isset( $this->mParams['type'] ) ) {
117  switch ( $this->mParams['type'] ) {
118  case 'int':
119  $type = 'number';
120  break;
121  case 'float':
122  $type = 'number';
123  $attribs['step'] = 'any';
124  break;
125  # Pass through
126  case 'email':
127  case 'password':
128  case 'file':
129  case 'url':
130  $type = $this->mParams['type'];
131  break;
132  }
133  }
134 
135  return $type;
136  }
137 
138  public function getInputOOUI( $value ) {
139  if ( !$this->isPersistent() ) {
140  $value = '';
141  }
142 
143  $attribs = $this->getTooltipAndAccessKey();
144 
145  if ( $this->mClass !== '' ) {
146  $attribs['classes'] = [ $this->mClass ];
147  }
148  if ( $this->mPlaceholder !== '' ) {
149  $attribs['placeholder'] = $this->mPlaceholder;
150  }
151 
152  # @todo Enforce pattern, step, required, readonly on the server side as
153  # well
154  $allowedParams = [
155  'autofocus',
156  'autosize',
157  'disabled',
158  'flags',
159  'indicator',
160  'maxlength',
161  'readonly',
162  'required',
163  'tabindex',
164  'type',
165  'autocomplete',
166  ];
167 
168  $attribs += OOUI\Element::configFromHtmlAttributes(
169  $this->getAttributes( $allowedParams )
170  );
171 
172  // FIXME T150983 downgrade autocomplete
173  if ( isset( $attribs['autocomplete'] ) ) {
174  if ( $attribs['autocomplete'] === 'on' ) {
175  $attribs['autocomplete'] = true;
176  } elseif ( $attribs['autocomplete'] === 'off' ) {
177  $attribs['autocomplete'] = false;
178  } else {
179  unset( $attribs['autocomplete'] );
180  }
181  }
182 
183  $type = $this->getType( $attribs );
184 
185  return $this->getInputWidget( [
186  'id' => $this->mID,
187  'name' => $this->mName,
188  'value' => $value,
189  'type' => $type,
190  'dir' => $this->mDir,
191  ] + $attribs );
192  }
193 
194  protected function getInputWidget( $params ) {
195  return new OOUI\TextInputWidget( $params );
196  }
197 
203  protected function getDataAttribs() {
204  return [];
205  }
206 }
HTMLTextField\$autocomplete
bool $autocomplete
HTML autocomplete attribute.
Definition: HTMLTextField.php:15
HTMLTextField\getInputWidget
getInputWidget( $params)
Definition: HTMLTextField.php:194
$params
$params
Definition: styleTest.css.php:40
HTMLTextField\getDataAttribs
getDataAttribs()
Returns an array of data-* attributes to add to the field.
Definition: HTMLTextField.php:203
HTMLTextField\getSpellCheck
getSpellCheck()
Definition: HTMLTextField.php:44
$type
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition: hooks.txt:2536
HTMLFormField\getMessage
getMessage( $value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name,...
Definition: HTMLFormField.php:1181
HTMLFormField\$mClass
$mClass
Definition: HTMLFormField.php:16
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
Html\input
static input( $name, $value='', $type='text', array $attribs=[])
Convenience function to produce an "<input>" element.
Definition: Html.php:663
HTMLTextField
<input> field.
Definition: HTMLTextField.php:11
HTMLFormField\$mDir
$mDir
Definition: HTMLFormField.php:13
HTMLTextField\$mPlaceholder
$mPlaceholder
Definition: HTMLTextField.php:12
HTMLFormField\$mName
$mName
Definition: HTMLFormField.php:12
HTMLTextField\isPersistent
isPersistent()
Definition: HTMLTextField.php:53
HTMLFormField
The parent class to generate form fields.
Definition: HTMLFormField.php:7
$attribs
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 after processing & $attribs
Definition: hooks.txt:1956
HTMLFormField\$mID
$mID
Definition: HTMLFormField.php:15
$value
$value
Definition: styleTest.css.php:45
HTMLTextField\__construct
__construct( $params)
Definition: HTMLTextField.php:26
HTMLFormField\getTooltipAndAccessKey
getTooltipAndAccessKey()
Returns the attributes required for the tooltip and accesskey.
Definition: HTMLFormField.php:983
HTMLTextField\getType
getType(&$attribs)
Definition: HTMLTextField.php:109
HTMLTextField\getSize
getSize()
Definition: HTMLTextField.php:40
HTMLTextField\getInputHTML
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself.
Definition: HTMLTextField.php:61
HTMLTextField\getInputOOUI
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.
Definition: HTMLTextField.php:138
HTMLFormField\getAttributes
getAttributes(array $list)
Returns the given attributes from the parameters.
Definition: HTMLFormField.php:997