MediaWiki  1.33.0
HTMLSelectAndOtherField.php
Go to the documentation of this file.
1 <?php
2 
14  public function __construct( $params ) {
15  if ( array_key_exists( 'other', $params ) ) {
16  // Do nothing
17  } elseif ( array_key_exists( 'other-message', $params ) ) {
18  $params['other'] = $this->getMessage( $params['other-message'] )->plain();
19  } else {
20  $params['other'] = $this->msg( 'htmlform-selectorother-other' )->plain();
21  }
22 
23  parent::__construct( $params );
24 
25  if ( $this->getOptions() === null ) {
26  // Sulk
27  throw new MWException( 'HTMLSelectAndOtherField called without any options' );
28  }
29  if ( !in_array( 'other', $this->mOptions, true ) ) {
30  // Have 'other' always as first element
31  $this->mOptions = [ $params['other'] => 'other' ] + $this->mOptions;
32  }
33  $this->mFlatOptions = self::flattenOptions( $this->getOptions() );
34  }
35 
36  public function getInputHTML( $value ) {
37  $select = parent::getInputHTML( $value[1] );
38 
39  $textAttribs = [
40  'id' => $this->mID . '-other',
41  'size' => $this->getSize(),
42  'class' => [ 'mw-htmlform-select-and-other-field' ],
43  'data-id-select' => $this->mID,
44  ];
45 
46  if ( $this->mClass !== '' ) {
47  $textAttribs['class'][] = $this->mClass;
48  }
49 
50  if ( isset( $this->mParams['maxlength-unit'] ) ) {
51  $textAttribs['data-mw-maxlength-unit'] = $this->mParams['maxlength-unit'];
52  }
53 
54  $allowedParams = [
55  'required',
56  'autofocus',
57  'multiple',
58  'disabled',
59  'tabindex',
60  'maxlength', // gets dynamic with javascript, see mediawiki.htmlform.js
61  'maxlength-unit', // 'bytes' or 'codepoints', see mediawiki.htmlform.js
62  ];
63 
64  $textAttribs += $this->getAttributes( $allowedParams );
65 
66  $textbox = Html::input( $this->mName . '-other', $value[2], 'text', $textAttribs );
67 
68  return "$select<br />\n$textbox";
69  }
70 
71  protected function getOOUIModules() {
72  return [ 'mediawiki.widgets.SelectWithInputWidget' ];
73  }
74 
75  public function getInputOOUI( $value ) {
76  $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.SelectWithInputWidget.styles' );
77 
78  # TextInput
79  $textAttribs = [
80  'name' => $this->mName . '-other',
81  'value' => $value[2],
82  ];
83 
84  $allowedParams = [
85  'required',
86  'autofocus',
87  'multiple',
88  'disabled',
89  'tabindex',
90  'maxlength',
91  ];
92 
93  $textAttribs += OOUI\Element::configFromHtmlAttributes(
94  $this->getAttributes( $allowedParams )
95  );
96 
97  if ( $this->mClass !== '' ) {
98  $textAttribs['classes'] = [ $this->mClass ];
99  }
100 
101  # DropdownInput
102  $dropdownInputAttribs = [
103  'name' => $this->mName,
104  'id' => $this->mID . '-select',
105  'options' => $this->getOptionsOOUI(),
106  'value' => $value[1],
107  ];
108 
109  $allowedParams = [
110  'tabindex',
111  'disabled',
112  ];
113 
114  $dropdownInputAttribs += OOUI\Element::configFromHtmlAttributes(
115  $this->getAttributes( $allowedParams )
116  );
117 
118  if ( $this->mClass !== '' ) {
119  $dropdownInputAttribs['classes'] = [ $this->mClass ];
120  }
121 
122  $disabled = false;
123  if ( isset( $this->mParams[ 'disabled' ] ) && $this->mParams[ 'disabled' ] ) {
124  $disabled = true;
125  }
126 
127  return $this->getInputWidget( [
128  'id' => $this->mID,
129  'disabled' => $disabled,
130  'textinput' => $textAttribs,
131  'dropdowninput' => $dropdownInputAttribs,
132  'or' => false,
133  'classes' => [ 'mw-htmlform-select-and-other-field' ],
134  'data' => [
135  'maxlengthUnit' => $this->mParams['maxlength-unit'] ?? 'bytes'
136  ],
137  ] );
138  }
139 
140  public function getInputWidget( $params ) {
142  }
143 
149  public function loadDataFromRequest( $request ) {
150  if ( $request->getCheck( $this->mName ) ) {
151  $list = $request->getText( $this->mName );
152  $text = $request->getText( $this->mName . '-other' );
153 
154  // Should be built the same as in mediawiki.htmlform.js
155  if ( $list == 'other' ) {
156  $final = $text;
157  } elseif ( !in_array( $list, $this->mFlatOptions, true ) ) {
158  # User has spoofed the select form to give an option which wasn't
159  # in the original offer. Sulk...
160  $final = $text;
161  } elseif ( $text == '' ) {
162  $final = $list;
163  } else {
164  $final = $list . $this->msg( 'colon-separator' )->inContentLanguage()->text() . $text;
165  }
166  } else {
167  $final = $this->getDefault();
168 
169  $list = 'other';
170  $text = $final;
171  foreach ( $this->mFlatOptions as $option ) {
172  $match = $option . $this->msg( 'colon-separator' )->inContentLanguage()->text();
173  if ( strpos( $text, $match ) === 0 ) {
174  $list = $option;
175  $text = substr( $text, strlen( $match ) );
176  break;
177  }
178  }
179  }
180 
181  return [ $final, $list, $text ];
182  }
183 
184  public function getSize() {
185  return $this->mParams['size'] ?? 45;
186  }
187 
188  public function validate( $value, $alldata ) {
189  # HTMLSelectField forces $value to be one of the options in the select
190  # field, which is not useful here. But we do want the validation further up
191  # the chain
192  $p = parent::validate( $value[1], $alldata );
193 
194  if ( $p !== true ) {
195  return $p;
196  }
197 
198  if ( isset( $this->mParams['required'] )
199  && $this->mParams['required'] !== false
200  && $value[1] === ''
201  ) {
202  return $this->msg( 'htmlform-required' );
203  }
204 
205  return true;
206  }
207 }
HTMLFormField\getOptions
getOptions()
Fetch the array of options from the field's parameters.
Definition: HTMLFormField.php:1052
$params
$params
Definition: styleTest.css.php:44
HTMLFormField\getMessage
getMessage( $value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name,...
Definition: HTMLFormField.php:1152
HTMLFormField\$mClass
$mClass
Definition: HTMLFormField.php:16
MediaWiki\Widget\SelectWithInputWidget
Select and input widget.
Definition: SelectWithInputWidget.php:14
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
HTMLFormField\$mOptions
array bool null $mOptions
Definition: HTMLFormField.php:23
HTMLSelectField
A select dropdown field.
Definition: HTMLSelectField.php:6
MWException
MediaWiki exception.
Definition: MWException.php:26
HTMLFormField\$mName
$mName
Definition: HTMLFormField.php:12
HTMLSelectAndOtherField
Double field with a dropdown list constructed from a system message in the format.
Definition: HTMLSelectAndOtherField.php:13
HTMLFormField\$mID
$mID
Definition: HTMLFormField.php:15
$request
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2636
HTMLSelectAndOtherField\loadDataFromRequest
loadDataFromRequest( $request)
Definition: HTMLSelectAndOtherField.php:149
$value
$value
Definition: styleTest.css.php:49
HTMLSelectAndOtherField\getInputHTML
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself.
Definition: HTMLSelectAndOtherField.php:36
HTMLFormField\getDefault
getDefault()
Definition: HTMLFormField.php:957
HTMLFormField\msg
msg()
Get a translated interface message.
Definition: HTMLFormField.php:80
HTMLSelectAndOtherField\__construct
__construct( $params)
Initialise the object.
Definition: HTMLSelectAndOtherField.php:14
HTMLSelectAndOtherField\getInputWidget
getInputWidget( $params)
Definition: HTMLSelectAndOtherField.php:140
HTMLSelectAndOtherField\getOOUIModules
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
Definition: HTMLSelectAndOtherField.php:71
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
HTMLSelectAndOtherField\validate
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
Definition: HTMLSelectAndOtherField.php:188
HTMLFormField\flattenOptions
static flattenOptions( $options)
flatten an array of options to a single array, for instance, a set of "<options>" inside "<optgroups>...
Definition: HTMLFormField.php:1091
HTMLSelectAndOtherField\getSize
getSize()
Definition: HTMLSelectAndOtherField.php:184
HTMLSelectAndOtherField\getInputOOUI
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.
Definition: HTMLSelectAndOtherField.php:75
HTMLFormField\getOptionsOOUI
getOptionsOOUI()
Get options and make them into arrays suitable for OOUI.
Definition: HTMLFormField.php:1074
HTMLFormField\getAttributes
getAttributes(array $list)
Returns the given attributes from the parameters.
Definition: HTMLFormField.php:996