MediaWiki  master
HTMLAutoCompleteSelectField.php
Go to the documentation of this file.
1 <?php
2 
30  protected $autocomplete = [];
31 
32  function __construct( $params ) {
33  $params += [
34  'require-match' => false,
35  ];
36 
37  parent::__construct( $params );
38 
39  if ( array_key_exists( 'autocomplete-messages', $this->mParams ) ) {
40  foreach ( $this->mParams['autocomplete-messages'] as $key => $value ) {
41  $key = $this->msg( $key )->plain();
42  $this->autocomplete[$key] = strval( $value );
43  }
44  } elseif ( array_key_exists( 'autocomplete', $this->mParams ) ) {
45  foreach ( $this->mParams['autocomplete'] as $key => $value ) {
46  $this->autocomplete[$key] = strval( $value );
47  }
48  }
49  if ( !is_array( $this->autocomplete ) || !$this->autocomplete ) {
50  throw new MWException( 'HTMLAutoCompleteSelectField called without any autocompletions' );
51  }
52 
53  $this->getOptions();
54  if ( $this->mOptions && !in_array( 'other', $this->mOptions, true ) ) {
55  if ( isset( $params['other-message'] ) ) {
56  $msg = $this->getMessage( $params['other-message'] )->text();
57  } elseif ( isset( $params['other'] ) ) {
58  $msg = $params['other'];
59  } else {
60  $msg = wfMessage( 'htmlform-selectorother-other' )->text();
61  }
62  $this->mOptions[$msg] = 'other';
63  }
64  }
65 
67  if ( $request->getCheck( $this->mName ) ) {
68  $val = $request->getText( $this->mName . '-select', 'other' );
69 
70  if ( $val === 'other' ) {
71  $val = $request->getText( $this->mName );
72  if ( isset( $this->autocomplete[$val] ) ) {
73  $val = $this->autocomplete[$val];
74  }
75  }
76 
77  return $val;
78  } else {
79  return $this->getDefault();
80  }
81  }
82 
83  function validate( $value, $alldata ) {
84  $p = parent::validate( $value, $alldata );
85 
86  if ( $p !== true ) {
87  return $p;
88  }
89 
90  $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
91 
92  if ( in_array( strval( $value ), $validOptions, true ) ) {
93  return true;
94  } elseif ( in_array( strval( $value ), $this->autocomplete, true ) ) {
95  return true;
96  } elseif ( $this->mParams['require-match'] ) {
97  return $this->msg( 'htmlform-select-badoption' )->parse();
98  }
99 
100  return true;
101  }
102 
103  // FIXME Ewww, this shouldn't be adding any attributes not requested in $list :(
104  public function getAttributes( array $list ) {
105  $attribs = [
106  'type' => 'text',
107  'data-autocomplete' => FormatJson::encode( array_keys( $this->autocomplete ) ),
108  ] + parent::getAttributes( $list );
109 
110  if ( $this->getOptions() ) {
111  $attribs['data-hide-if'] = FormatJson::encode(
112  [ '!==', $this->mName . '-select', 'other' ]
113  );
114  }
115 
116  return $attribs;
117  }
118 
119  function getInputHTML( $value ) {
120  $oldClass = $this->mClass;
121  $this->mClass = (array)$this->mClass;
122 
123  $valInSelect = false;
124  $ret = '';
125 
126  if ( $this->getOptions() ) {
127  if ( $value !== false ) {
128  $value = strval( $value );
129  $valInSelect = in_array(
131  );
132  }
133 
134  $selected = $valInSelect ? $value : 'other';
135  $select = new XmlSelect( $this->mName . '-select', $this->mID . '-select', $selected );
136  $select->addOptions( $this->getOptions() );
137  $select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
138 
139  if ( !empty( $this->mParams['disabled'] ) ) {
140  $select->setAttribute( 'disabled', 'disabled' );
141  }
142 
143  if ( isset( $this->mParams['tabindex'] ) ) {
144  $select->setAttribute( 'tabindex', $this->mParams['tabindex'] );
145  }
146 
147  $ret = $select->getHTML() . "<br />\n";
148 
149  $this->mClass[] = 'mw-htmlform-hide-if';
150  }
151 
152  if ( $valInSelect ) {
153  $value = '';
154  } else {
155  $key = array_search( strval( $value ), $this->autocomplete, true );
156  if ( $key !== false ) {
157  $value = $key;
158  }
159  }
160 
161  $this->mClass[] = 'mw-htmlform-autocomplete';
162  $ret .= parent::getInputHTML( $valInSelect ? '' : $value );
163  $this->mClass = $oldClass;
164 
165  return $ret;
166  }
167 
173  function getInputOOUI( $value ) {
174  // To be implemented, for now override the function from HTMLTextField
175  return false;
176  }
177 }
the array() calling protocol came about after MediaWiki 1.4rc1.
getOptions()
Fetch the array of options from the field's parameters.
getMessage($value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name, or a name + parameters array) into a Message.
msg()
Get a translated interface message.
Class for generating HTML <select> or <datalist> elements.
Definition: XmlSelect.php:26
$value
MediaWiki exception.
Definition: MWException.php:26
static encode($value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:127
$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 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 after processing 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 unsetoffset-wrap String Wrap the message in html(usually something like"&lt
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:1816
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
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
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2458
getInputOOUI($value)
Get the OOUI version of this input.
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:1816
static flattenOptions($options)
flatten an array of options to a single array, for instance, a set of "<options>" inside "<optgroups>...
Text field for selecting a value from a large list of possible values, with auto-completion and optio...