MediaWiki  1.30.0
HTMLAutoCompleteSelectField.php
Go to the documentation of this file.
1 <?php
2 
34  protected $autocompleteData = [];
35 
36  public function __construct( $params ) {
37  $params += [
38  'require-match' => false,
39  ];
40 
41  // FIXME B/C, remove in 1.30
42  if (
43  array_key_exists( 'autocomplete', $params )
44  && !array_key_exists( 'autocomplete-data', $params )
45  ) {
46  $params['autocomplete-data'] = $params['autocomplete'];
47  unset( $params['autocomplete'] );
48  }
49  if (
50  array_key_exists( 'autocomplete-messages', $params )
51  && !array_key_exists( 'autocomplete-data-messages', $params )
52  ) {
53  $params['autocomplete-data-messages'] = $params['autocomplete-messages'];
54  unset( $params['autocomplete-messages'] );
55  }
56 
57  parent::__construct( $params );
58 
59  if ( array_key_exists( 'autocomplete-data-messages', $this->mParams ) ) {
60  foreach ( $this->mParams['autocomplete-data-messages'] as $key => $value ) {
61  $key = $this->msg( $key )->plain();
62  $this->autocompleteData[$key] = strval( $value );
63  }
64  } elseif ( array_key_exists( 'autocomplete-data', $this->mParams ) ) {
65  foreach ( $this->mParams['autocomplete-data'] as $key => $value ) {
66  $this->autocompleteData[$key] = strval( $value );
67  }
68  }
69  if ( !is_array( $this->autocompleteData ) || !$this->autocompleteData ) {
70  throw new MWException( 'HTMLAutoCompleteSelectField called without any autocompletions' );
71  }
72 
73  $this->getOptions();
74  if ( $this->mOptions && !in_array( 'other', $this->mOptions, true ) ) {
75  if ( isset( $params['other-message'] ) ) {
76  $msg = $this->getMessage( $params['other-message'] )->text();
77  } elseif ( isset( $params['other'] ) ) {
78  $msg = $params['other'];
79  } else {
80  $msg = wfMessage( 'htmlform-selectorother-other' )->text();
81  }
82  $this->mOptions[$msg] = 'other';
83  }
84  }
85 
86  public function loadDataFromRequest( $request ) {
87  if ( $request->getCheck( $this->mName ) ) {
88  $val = $request->getText( $this->mName . '-select', 'other' );
89 
90  if ( $val === 'other' ) {
91  $val = $request->getText( $this->mName );
92  if ( isset( $this->autocompleteData[$val] ) ) {
93  $val = $this->autocompleteData[$val];
94  }
95  }
96 
97  return $val;
98  } else {
99  return $this->getDefault();
100  }
101  }
102 
103  public function validate( $value, $alldata ) {
104  $p = parent::validate( $value, $alldata );
105 
106  if ( $p !== true ) {
107  return $p;
108  }
109 
110  $validOptions = HTMLFormField::flattenOptions( $this->getOptions() ?: [] );
111 
112  if ( in_array( strval( $value ), $validOptions, true ) ) {
113  return true;
114  } elseif ( in_array( strval( $value ), $this->autocompleteData, true ) ) {
115  return true;
116  } elseif ( $this->mParams['require-match'] ) {
117  return $this->msg( 'htmlform-select-badoption' );
118  }
119 
120  return true;
121  }
122 
123  // FIXME Ewww, this shouldn't be adding any attributes not requested in $list :(
124  public function getAttributes( array $list ) {
125  $attribs = [
126  'type' => 'text',
127  'data-autocomplete' => FormatJson::encode( array_keys( $this->autocompleteData ) ),
128  ] + parent::getAttributes( $list );
129 
130  if ( $this->getOptions() ) {
131  $attribs['data-hide-if'] = FormatJson::encode(
132  [ '!==', $this->mName . '-select', 'other' ]
133  );
134  }
135 
136  return $attribs;
137  }
138 
139  public function getInputHTML( $value ) {
140  $oldClass = $this->mClass;
141  $this->mClass = (array)$this->mClass;
142 
143  $valInSelect = false;
144  $ret = '';
145 
146  if ( $this->getOptions() ) {
147  if ( $value !== false ) {
148  $value = strval( $value );
149  $valInSelect = in_array(
151  );
152  }
153 
154  $selected = $valInSelect ? $value : 'other';
155  $select = new XmlSelect( $this->mName . '-select', $this->mID . '-select', $selected );
156  $select->addOptions( $this->getOptions() );
157  $select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
158 
159  if ( !empty( $this->mParams['disabled'] ) ) {
160  $select->setAttribute( 'disabled', 'disabled' );
161  }
162 
163  if ( isset( $this->mParams['tabindex'] ) ) {
164  $select->setAttribute( 'tabindex', $this->mParams['tabindex'] );
165  }
166 
167  $ret = $select->getHTML() . "<br />\n";
168 
169  $this->mClass[] = 'mw-htmlform-hide-if';
170  }
171 
172  if ( $valInSelect ) {
173  $value = '';
174  } else {
175  $key = array_search( strval( $value ), $this->autocompleteData, true );
176  if ( $key !== false ) {
177  $value = $key;
178  }
179  }
180 
181  $this->mClass[] = 'mw-htmlform-autocomplete';
182  $ret .= parent::getInputHTML( $valInSelect ? '' : $value );
183  $this->mClass = $oldClass;
184 
185  return $ret;
186  }
187 
193  public function getInputOOUI( $value ) {
194  // To be implemented, for now override the function from HTMLTextField
195  return false;
196  }
197 }
HTMLFormField\getOptions
getOptions()
Fetch the array of options from the field's parameters.
Definition: HTMLFormField.php:1071
HTMLAutoCompleteSelectField\validate
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
Definition: HTMLAutoCompleteSelectField.php:103
$params
$params
Definition: styleTest.css.php:40
HTMLAutoCompleteSelectField
Text field for selecting a value from a large list of possible values, with auto-completion and optio...
Definition: HTMLAutoCompleteSelectField.php:33
HTMLAutoCompleteSelectField\loadDataFromRequest
loadDataFromRequest( $request)
Get the value that this input has been set to from a posted form, or the input's default value if it ...
Definition: HTMLAutoCompleteSelectField.php:86
HTMLFormField\getMessage
getMessage( $value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name,...
Definition: HTMLFormField.php:1165
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
HTMLTextField
<input> field.
Definition: HTMLTextField.php:11
XmlSelect
Class for generating HTML <select> or <datalist> elements.
Definition: XmlSelect.php:26
FormatJson\encode
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:127
MWException
MediaWiki exception.
Definition: MWException.php:26
HTMLAutoCompleteSelectField\__construct
__construct( $params)
Definition: HTMLAutoCompleteSelectField.php:36
$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:1965
HTMLAutoCompleteSelectField\$autocompleteData
$autocompleteData
Definition: HTMLAutoCompleteSelectField.php:34
HTMLAutoCompleteSelectField\getAttributes
getAttributes(array $list)
Returns the given attributes from the parameters.
Definition: HTMLAutoCompleteSelectField.php:124
$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:2581
$value
$value
Definition: styleTest.css.php:45
HTMLFormField\getDefault
getDefault()
Definition: HTMLFormField.php:972
HTMLFormField\msg
msg()
Get a translated interface message.
Definition: HTMLFormField.php:77
$ret
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 & $ret
Definition: hooks.txt:1965
HTMLAutoCompleteSelectField\getInputHTML
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself.
Definition: HTMLAutoCompleteSelectField.php:139
HTMLAutoCompleteSelectField\getInputOOUI
getInputOOUI( $value)
Get the OOUI version of this input.
Definition: HTMLAutoCompleteSelectField.php:193
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
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:1110
wfMessage
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt
array
the array() calling protocol came about after MediaWiki 1.4rc1.