MediaWiki  1.28.0
HTMLTextField.php
Go to the documentation of this file.
1 <?php
2 
3 class HTMLTextField extends HTMLFormField {
4  protected $mPlaceholder = '';
5 
15  public function __construct( $params ) {
16  parent::__construct( $params );
17 
18  if ( isset( $params['placeholder-message'] ) ) {
19  $this->mPlaceholder = $this->getMessage( $params['placeholder-message'] )->parse();
20  } elseif ( isset( $params['placeholder'] ) ) {
21  $this->mPlaceholder = $params['placeholder'];
22  }
23  }
24 
25  function getSize() {
26  return isset( $this->mParams['size'] ) ? $this->mParams['size'] : 45;
27  }
28 
29  function getSpellCheck() {
30  $val = isset( $this->mParams['spellcheck'] ) ? $this->mParams['spellcheck'] : null;
31  if ( is_bool( $val ) ) {
32  // "spellcheck" attribute literally requires "true" or "false" to work.
33  return $val === true ? 'true' : 'false';
34  }
35  return null;
36  }
37 
38  public function isPersistent() {
39  if ( isset( $this->mParams['persistent'] ) ) {
40  return $this->mParams['persistent'];
41  }
42  // don't put passwords into the HTML body, they could get cached or otherwise leaked
43  return !( isset( $this->mParams['type'] ) && $this->mParams['type'] === 'password' );
44  }
45 
46  function getInputHTML( $value ) {
47  if ( !$this->isPersistent() ) {
48  $value = '';
49  }
50 
51  $attribs = [
52  'id' => $this->mID,
53  'name' => $this->mName,
54  'size' => $this->getSize(),
55  'value' => $value,
56  'dir' => $this->mDir,
57  'spellcheck' => $this->getSpellCheck(),
58  ] + $this->getTooltipAndAccessKey() + $this->getDataAttribs();
59 
60  if ( $this->mClass !== '' ) {
61  $attribs['class'] = $this->mClass;
62  }
63  if ( $this->mPlaceholder !== '' ) {
64  $attribs['placeholder'] = $this->mPlaceholder;
65  }
66 
67  # @todo Enforce pattern, step, required, readonly on the server side as
68  # well
69  $allowedParams = [
70  'type',
71  'min',
72  'max',
73  'pattern',
74  'title',
75  'step',
76  'list',
77  'maxlength',
78  'tabindex',
79  'disabled',
80  'required',
81  'autofocus',
82  'multiple',
83  'readonly'
84  ];
85 
86  $attribs += $this->getAttributes( $allowedParams );
87 
88  # Extract 'type'
89  $type = $this->getType( $attribs );
90  return Html::input( $this->mName, $value, $type, $attribs );
91  }
92 
93  protected function getType( &$attribs ) {
94  $type = isset( $attribs['type'] ) ? $attribs['type'] : 'text';
95  unset( $attribs['type'] );
96 
97  # Implement tiny differences between some field variants
98  # here, rather than creating a new class for each one which
99  # is essentially just a clone of this one.
100  if ( isset( $this->mParams['type'] ) ) {
101  switch ( $this->mParams['type'] ) {
102  case 'int':
103  $type = 'number';
104  break;
105  case 'float':
106  $type = 'number';
107  $attribs['step'] = 'any';
108  break;
109  # Pass through
110  case 'email':
111  case 'password':
112  case 'file':
113  case 'url':
114  $type = $this->mParams['type'];
115  break;
116  }
117  }
118 
119  return $type;
120  }
121 
122  function getInputOOUI( $value ) {
123  if ( !$this->isPersistent() ) {
124  $value = '';
125  }
126 
127  $attribs = $this->getTooltipAndAccessKey();
128 
129  if ( $this->mClass !== '' ) {
130  $attribs['classes'] = [ $this->mClass ];
131  }
132  if ( $this->mPlaceholder !== '' ) {
133  $attribs['placeholder'] = $this->mPlaceholder;
134  }
135 
136  # @todo Enforce pattern, step, required, readonly on the server side as
137  # well
138  $allowedParams = [
139  'autofocus',
140  'autosize',
141  'disabled',
142  'flags',
143  'indicator',
144  'maxlength',
145  'readonly',
146  'required',
147  'tabindex',
148  'type',
149  ];
150 
151  $attribs += OOUI\Element::configFromHtmlAttributes(
152  $this->getAttributes( $allowedParams )
153  );
154 
155  $type = $this->getType( $attribs );
156 
157  return $this->getInputWidget( [
158  'id' => $this->mID,
159  'name' => $this->mName,
160  'value' => $value,
161  'type' => $type,
162  ] + $attribs );
163  }
164 
165  protected function getInputWidget( $params ) {
166  return new OOUI\TextInputWidget( $params );
167  }
168 
174  protected function getDataAttribs() {
175  return [];
176  }
177 }
getMessage($value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name, or a name + parameters array) into a Message.
getTooltipAndAccessKey()
Returns the attributes required for the tooltip and accesskey.
$value
getDataAttribs()
Returns an array of data-* attributes to add to the field.
getInputOOUI($value)
__construct($params)
$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:1936
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
getInputHTML($value)
The parent class to generate form fields.
static input($name, $value= '', $type= 'text', array $attribs=[])
Convenience function to produce an "" element.
Definition: Html.php:675
getInputWidget($params)
getAttributes(array $list)
Returns the given attributes from the parameters.
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 one of or reset 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:2491
getType(&$attribs)