MediaWiki  1.29.2
FormOptions.php
Go to the documentation of this file.
1 <?php
35 class FormOptions implements ArrayAccess {
39  /* @{ */
41  const AUTO = -1;
43  const STRING = 0;
45  const INT = 1;
48  const FLOAT = 4;
50  const BOOL = 2;
54  const INTNULL = 3;
57  const ARR = 5;
58  /* @} */
59 
70  protected $options = [];
71 
72  # Setting up
73 
81  public function add( $name, $default, $type = self::AUTO ) {
82  $option = [];
83  $option['default'] = $default;
84  $option['value'] = null;
85  $option['consumed'] = false;
86 
87  if ( $type !== self::AUTO ) {
88  $option['type'] = $type;
89  } else {
90  $option['type'] = self::guessType( $default );
91  }
92 
93  $this->options[$name] = $option;
94  }
95 
101  public function delete( $name ) {
102  $this->validateName( $name, true );
103  unset( $this->options[$name] );
104  }
105 
117  public static function guessType( $data ) {
118  if ( is_bool( $data ) ) {
119  return self::BOOL;
120  } elseif ( is_int( $data ) ) {
121  return self::INT;
122  } elseif ( is_float( $data ) ) {
123  return self::FLOAT;
124  } elseif ( is_string( $data ) ) {
125  return self::STRING;
126  } elseif ( is_array( $data ) ) {
127  return self::ARR;
128  } else {
129  throw new MWException( 'Unsupported datatype' );
130  }
131  }
132 
133  # Handling values
134 
143  public function validateName( $name, $strict = false ) {
144  if ( !isset( $this->options[$name] ) ) {
145  if ( $strict ) {
146  throw new MWException( "Invalid option $name" );
147  } else {
148  return false;
149  }
150  }
151 
152  return true;
153  }
154 
163  public function setValue( $name, $value, $force = false ) {
164  $this->validateName( $name, true );
165 
166  if ( !$force && $value === $this->options[$name]['default'] ) {
167  // null default values as unchanged
168  $this->options[$name]['value'] = null;
169  } else {
170  $this->options[$name]['value'] = $value;
171  }
172  }
173 
180  public function getValue( $name ) {
181  $this->validateName( $name, true );
182 
183  return $this->getValueReal( $this->options[$name] );
184  }
185 
192  protected function getValueReal( $option ) {
193  if ( $option['value'] !== null ) {
194  return $option['value'];
195  } else {
196  return $option['default'];
197  }
198  }
199 
205  public function reset( $name ) {
206  $this->validateName( $name, true );
207  $this->options[$name]['value'] = null;
208  }
209 
219  public function consumeValue( $name ) {
220  $this->validateName( $name, true );
221  $this->options[$name]['consumed'] = true;
222 
223  return $this->getValueReal( $this->options[$name] );
224  }
225 
235  public function consumeValues( $names ) {
236  $out = [];
237 
238  foreach ( $names as $name ) {
239  $this->validateName( $name, true );
240  $this->options[$name]['consumed'] = true;
241  $out[] = $this->getValueReal( $this->options[$name] );
242  }
243 
244  return $out;
245  }
246 
250  public function validateIntBounds( $name, $min, $max ) {
251  $this->validateBounds( $name, $min, $max );
252  }
253 
265  public function validateBounds( $name, $min, $max ) {
266  $this->validateName( $name, true );
267  $type = $this->options[$name]['type'];
268 
269  if ( $type !== self::INT && $type !== self::FLOAT ) {
270  throw new MWException( "Option $name is not of type INT or FLOAT" );
271  }
272 
273  $value = $this->getValueReal( $this->options[$name] );
274  $value = max( $min, min( $max, $value ) );
275 
276  $this->setValue( $name, $value );
277  }
278 
285  public function getUnconsumedValues( $all = false ) {
286  $values = [];
287 
288  foreach ( $this->options as $name => $data ) {
289  if ( !$data['consumed'] ) {
290  if ( $all || $data['value'] !== null ) {
291  $values[$name] = $this->getValueReal( $data );
292  }
293  }
294  }
295 
296  return $values;
297  }
298 
303  public function getChangedValues() {
304  $values = [];
305 
306  foreach ( $this->options as $name => $data ) {
307  if ( $data['value'] !== null ) {
308  $values[$name] = $data['value'];
309  }
310  }
311 
312  return $values;
313  }
314 
319  public function getAllValues() {
320  $values = [];
321 
322  foreach ( $this->options as $name => $data ) {
323  $values[$name] = $this->getValueReal( $data );
324  }
325 
326  return $values;
327  }
328 
329  # Reading values
330 
341  public function fetchValuesFromRequest( WebRequest $r, $optionKeys = null ) {
342  if ( !$optionKeys ) {
343  $optionKeys = array_keys( $this->options );
344  }
345 
346  foreach ( $optionKeys as $name ) {
347  $default = $this->options[$name]['default'];
348  $type = $this->options[$name]['type'];
349 
350  switch ( $type ) {
351  case self::BOOL:
352  $value = $r->getBool( $name, $default );
353  break;
354  case self::INT:
355  $value = $r->getInt( $name, $default );
356  break;
357  case self::FLOAT:
358  $value = $r->getFloat( $name, $default );
359  break;
360  case self::STRING:
361  $value = $r->getText( $name, $default );
362  break;
363  case self::INTNULL:
364  $value = $r->getIntOrNull( $name );
365  break;
366  case self::ARR:
367  $value = $r->getArray( $name );
368  break;
369  default:
370  throw new MWException( 'Unsupported datatype' );
371  }
372 
373  if ( $value !== null ) {
374  $this->options[$name]['value'] = $value === $default ? null : $value;
375  }
376  }
377  }
378 
383  /* @{ */
389  public function offsetExists( $name ) {
390  return isset( $this->options[$name] );
391  }
392 
398  public function offsetGet( $name ) {
399  return $this->getValue( $name );
400  }
401 
407  public function offsetSet( $name, $value ) {
408  $this->setValue( $name, $value );
409  }
410 
415  public function offsetUnset( $name ) {
416  $this->delete( $name );
417  }
418  /* @} */
419 }
FormOptions\offsetUnset
offsetUnset( $name)
Delete the option.
Definition: FormOptions.php:415
FormOptions\getUnconsumedValues
getUnconsumedValues( $all=false)
Get all remaining values which have not been consumed by consumeValue() or consumeValues().
Definition: FormOptions.php:285
FormOptions\FLOAT
const FLOAT
Float type, maps guessType() to WebRequest::getFloat()
Definition: FormOptions.php:48
FormOptions\getValue
getValue( $name)
Get the value for the given option name.
Definition: FormOptions.php:180
FormOptions\offsetGet
offsetGet( $name)
Retrieve an option value.
Definition: FormOptions.php:398
FormOptions\INTNULL
const INTNULL
Integer type or null, maps to WebRequest::getIntOrNull() This is useful for the namespace selector.
Definition: FormOptions.php:54
WebRequest\getIntOrNull
getIntOrNull( $name)
Fetch an integer value from the input or return null if empty.
Definition: WebRequest.php:535
FormOptions\reset
reset( $name)
Delete the option value.
Definition: FormOptions.php:205
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:304
FormOptions\validateIntBounds
validateIntBounds( $name, $min, $max)
Definition: FormOptions.php:250
FormOptions\ARR
const ARR
Array type, maps guessType() to WebRequest::getArray()
Definition: FormOptions.php:57
$type
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 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:2536
FormOptions\fetchValuesFromRequest
fetchValuesFromRequest(WebRequest $r, $optionKeys=null)
Fetch values for all options (or selected options) from the given WebRequest, making them available f...
Definition: FormOptions.php:341
FormOptions\consumeValue
consumeValue( $name)
Get the value of given option and mark it as 'consumed'.
Definition: FormOptions.php:219
FormOptions\consumeValues
consumeValues( $names)
Get the values of given options and mark them as 'consumed'.
Definition: FormOptions.php:235
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
FormOptions\add
add( $name, $default, $type=self::AUTO)
Add an option to be handled by this FormOptions instance.
Definition: FormOptions.php:81
WebRequest\getText
getText( $name, $default='')
Fetch a text string from the given array or return $default if it's not set.
Definition: WebRequest.php:607
MWException
MediaWiki exception.
Definition: MWException.php:26
WebRequest\getArray
getArray( $name, $default=null)
Fetch an array from the input or return $default if it's not set.
Definition: WebRequest.php:487
FormOptions\setValue
setValue( $name, $value, $force=false)
Use to set the value of an option.
Definition: FormOptions.php:163
$value
$value
Definition: styleTest.css.php:45
FormOptions\$options
$options
Map of known option names to information about them.
Definition: FormOptions.php:70
FormOptions\validateBounds
validateBounds( $name, $min, $max)
Constrain a numeric value for a given option to a given range.
Definition: FormOptions.php:265
FormOptions\offsetSet
offsetSet( $name, $value)
Set an option to given value.
Definition: FormOptions.php:407
FormOptions\guessType
static guessType( $data)
Used to find out which type the data is.
Definition: FormOptions.php:117
FormOptions\getValueReal
getValueReal( $option)
Return current option value, based on a structure taken from $options.
Definition: FormOptions.php:192
options
We ve cleaned up the code here by removing clumps of infrequently used code and moving them off somewhere else It s much easier for someone working with this code to see what s _really_ going and make changes or fix bugs In we can take all the code that deals with the little used title reversing options(say) and put it in one place. Instead of having little title-reversing if-blocks spread all over the codebase in showAnArticle
FormOptions\getAllValues
getAllValues()
Format options to an array ( name => value )
Definition: FormOptions.php:319
FormOptions\STRING
const STRING
String type, maps guessType() to WebRequest::getText()
Definition: FormOptions.php:43
WebRequest
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
Definition: WebRequest.php:38
FormOptions\INT
const INT
Integer type, maps guessType() to WebRequest::getInt()
Definition: FormOptions.php:45
WebRequest\getInt
getInt( $name, $default=0)
Fetch an integer value from the input or return $default if not set.
Definition: WebRequest.php:523
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
WebRequest\getFloat
getFloat( $name, $default=0.0)
Fetch a floating point value from the input or return $default if not set.
Definition: WebRequest.php:552
FormOptions\offsetExists
offsetExists( $name)
Whether the option exists.
Definition: FormOptions.php:389
FormOptions\validateName
validateName( $name, $strict=false)
Verify that the given option name exists.
Definition: FormOptions.php:143
FormOptions
Helper class to keep track of options when mixing links and form elements.
Definition: FormOptions.php:35
FormOptions\getChangedValues
getChangedValues()
Return options modified as an array ( name => value )
Definition: FormOptions.php:303
FormOptions\AUTO
const AUTO
Mark value for automatic detection (for simple data types only)
Definition: FormOptions.php:41
WebRequest\getBool
getBool( $name, $default=false)
Fetch a boolean value from the input or return $default if not set.
Definition: WebRequest.php:565
FormOptions\BOOL
const BOOL
Boolean type, maps guessType() to WebRequest::getBool()
Definition: FormOptions.php:50
$out
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition: hooks.txt:783