MediaWiki  master
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'] )->text();
35  } elseif ( isset( $params['placeholder'] ) ) {
36  $this->mPlaceholder = $params['placeholder'];
37  }
38  }
39 
40  public function getSize() {
41  return $this->mParams['size'] ?? 45;
42  }
43 
44  public function getSpellCheck() {
45  $val = $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  'step',
89  'title',
90  'maxlength',
91  'tabindex',
92  'disabled',
93  'required',
94  'autofocus',
95  'readonly',
96  'autocomplete',
97  // Only used in HTML mode:
98  'pattern',
99  'list',
100  'multiple',
101  ];
102 
103  $attribs += $this->getAttributes( $allowedParams );
104 
105  # Extract 'type'
106  $type = $this->getType( $attribs );
107  return Html::input( $this->mName, $value, $type, $attribs );
108  }
109 
110  protected function getType( &$attribs ) {
111  $type = $attribs['type'] ?? 'text';
112  unset( $attribs['type'] );
113 
114  # Implement tiny differences between some field variants
115  # here, rather than creating a new class for each one which
116  # is essentially just a clone of this one.
117  if ( isset( $this->mParams['type'] ) ) {
118  switch ( $this->mParams['type'] ) {
119  case 'int':
120  $type = 'number';
121  $attribs['step'] = 1;
122  break;
123  case 'float':
124  $type = 'number';
125  $attribs['step'] = 'any';
126  break;
127  # Pass through
128  case 'email':
129  case 'password':
130  case 'file':
131  case 'url':
132  $type = $this->mParams['type'];
133  break;
134  case 'textwithbutton':
135  $type = $this->mParams['inputtype'] ?? 'text';
136  break;
137  }
138  }
139 
140  return $type;
141  }
142 
143  public function getInputOOUI( $value ) {
144  if ( !$this->isPersistent() ) {
145  $value = '';
146  }
147 
149 
150  if ( $this->mClass !== '' ) {
151  $attribs['classes'] = [ $this->mClass ];
152  }
153  if ( $this->mPlaceholder !== '' ) {
154  $attribs['placeholder'] = $this->mPlaceholder;
155  }
156 
157  # @todo Enforce pattern, step, required, readonly on the server side as
158  # well
159  $allowedParams = [
160  'type',
161  'min',
162  'max',
163  'step',
164  'title',
165  'maxlength',
166  'tabindex',
167  'disabled',
168  'required',
169  'autofocus',
170  'readonly',
171  'autocomplete',
172  // Only used in OOUI mode:
173  'autosize',
174  'flags',
175  'indicator',
176  ];
177 
178  $attribs += OOUI\Element::configFromHtmlAttributes(
179  $this->getAttributes( $allowedParams )
180  );
181 
182  // FIXME T150983 downgrade autocomplete
183  if ( isset( $attribs['autocomplete'] ) ) {
184  if ( $attribs['autocomplete'] === 'on' ) {
185  $attribs['autocomplete'] = true;
186  } elseif ( $attribs['autocomplete'] === 'off' ) {
187  $attribs['autocomplete'] = false;
188  } else {
189  unset( $attribs['autocomplete'] );
190  }
191  }
192 
193  $type = $this->getType( $attribs );
194  if ( isset( $attribs['step'] ) && $attribs['step'] === 'any' ) {
195  $attribs['step'] = null;
196  }
197 
198  return $this->getInputWidget( [
199  'id' => $this->mID,
200  'name' => $this->mName,
201  'value' => $value,
202  'type' => $type,
203  'dir' => $this->mDir,
204  ] + $attribs );
205  }
206 
207  protected function getInputWidget( $params ) {
208  return new OOUI\TextInputWidget( $params );
209  }
210 
216  protected function getDataAttribs() {
217  return [];
218  }
219 }
getTooltipAndAccessKey()
Returns the attributes required for the tooltip and accesskey, for Html::element() etc...
$value
__construct( $params)
getInputWidget( $params)
getDataAttribs()
Returns an array of data-* attributes to add to the field.
getTooltipAndAccessKeyOOUI()
Returns the attributes required for the tooltip and accesskey, for OOUI widgets&#39; config.
$params
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:1982
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
static input( $name, $value='', $type='text', array $attribs=[])
Convenience function to produce an "<input>" element.
Definition: Html.php:668
bool $autocomplete
HTML autocomplete attribute.
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
getInputOOUI( $value)
getMessage( $value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name, or a name + parameters array) into a Message.
<input> field.
getAttributes(array $list)
Returns the given attributes from the parameters.
getInputHTML( $value)
getType(&$attribs)