MediaWiki  1.23.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;
55  /* @} */
56 
67  protected $options = array();
68 
69  # Setting up
70 
78  public function add( $name, $default, $type = self::AUTO ) {
79  $option = array();
80  $option['default'] = $default;
81  $option['value'] = null;
82  $option['consumed'] = false;
83 
84  if ( $type !== self::AUTO ) {
85  $option['type'] = $type;
86  } else {
87  $option['type'] = self::guessType( $default );
88  }
89 
90  $this->options[$name] = $option;
91  }
92 
98  public function delete( $name ) {
99  $this->validateName( $name, true );
100  unset( $this->options[$name] );
101  }
102 
114  public static function guessType( $data ) {
115  if ( is_bool( $data ) ) {
116  return self::BOOL;
117  } elseif ( is_int( $data ) ) {
118  return self::INT;
119  } elseif ( is_float( $data ) ) {
120  return self::FLOAT;
121  } elseif ( is_string( $data ) ) {
122  return self::STRING;
123  } else {
124  throw new MWException( 'Unsupported datatype' );
125  }
126  }
127 
128  # Handling values
129 
138  public function validateName( $name, $strict = false ) {
139  if ( !isset( $this->options[$name] ) ) {
140  if ( $strict ) {
141  throw new MWException( "Invalid option $name" );
142  } else {
143  return false;
144  }
145  }
146 
147  return true;
148  }
149 
158  public function setValue( $name, $value, $force = false ) {
159  $this->validateName( $name, true );
160 
161  if ( !$force && $value === $this->options[$name]['default'] ) {
162  // null default values as unchanged
163  $this->options[$name]['value'] = null;
164  } else {
165  $this->options[$name]['value'] = $value;
166  }
167  }
168 
175  public function getValue( $name ) {
176  $this->validateName( $name, true );
177 
178  return $this->getValueReal( $this->options[$name] );
179  }
180 
187  protected function getValueReal( $option ) {
188  if ( $option['value'] !== null ) {
189  return $option['value'];
190  } else {
191  return $option['default'];
192  }
193  }
194 
200  public function reset( $name ) {
201  $this->validateName( $name, true );
202  $this->options[$name]['value'] = null;
203  }
204 
214  public function consumeValue( $name ) {
215  $this->validateName( $name, true );
216  $this->options[$name]['consumed'] = true;
217 
218  return $this->getValueReal( $this->options[$name] );
219  }
220 
230  public function consumeValues( $names ) {
231  $out = array();
232 
233  foreach ( $names as $name ) {
234  $this->validateName( $name, true );
235  $this->options[$name]['consumed'] = true;
236  $out[] = $this->getValueReal( $this->options[$name] );
237  }
238 
239  return $out;
240  }
241 
245  public function validateIntBounds( $name, $min, $max ) {
246  $this->validateBounds( $name, $min, $max );
247  }
248 
260  public function validateBounds( $name, $min, $max ) {
261  $this->validateName( $name, true );
262  $type = $this->options[$name]['type'];
263 
264  if ( $type !== self::INT && $type !== self::FLOAT ) {
265  throw new MWException( "Option $name is not of type INT or FLOAT" );
266  }
267 
268  $value = $this->getValueReal( $this->options[$name] );
269  $value = max( $min, min( $max, $value ) );
270 
271  $this->setValue( $name, $value );
272  }
273 
280  public function getUnconsumedValues( $all = false ) {
281  $values = array();
282 
283  foreach ( $this->options as $name => $data ) {
284  if ( !$data['consumed'] ) {
285  if ( $all || $data['value'] !== null ) {
286  $values[$name] = $this->getValueReal( $data );
287  }
288  }
289  }
290 
291  return $values;
292  }
293 
298  public function getChangedValues() {
299  $values = array();
300 
301  foreach ( $this->options as $name => $data ) {
302  if ( $data['value'] !== null ) {
303  $values[$name] = $data['value'];
304  }
305  }
306 
307  return $values;
308  }
309 
314  public function getAllValues() {
315  $values = array();
316 
317  foreach ( $this->options as $name => $data ) {
318  $values[$name] = $this->getValueReal( $data );
319  }
320 
321  return $values;
322  }
323 
324  # Reading values
325 
336  public function fetchValuesFromRequest( WebRequest $r, $optionKeys = null ) {
337  if ( !$optionKeys ) {
338  $optionKeys = array_keys( $this->options );
339  }
340 
341  foreach ( $optionKeys as $name ) {
342  $default = $this->options[$name]['default'];
343  $type = $this->options[$name]['type'];
344 
345  switch ( $type ) {
346  case self::BOOL:
347  $value = $r->getBool( $name, $default );
348  break;
349  case self::INT:
350  $value = $r->getInt( $name, $default );
351  break;
352  case self::FLOAT:
353  $value = $r->getFloat( $name, $default );
354  break;
355  case self::STRING:
356  $value = $r->getText( $name, $default );
357  break;
358  case self::INTNULL:
359  $value = $r->getIntOrNull( $name );
360  break;
361  default:
362  throw new MWException( 'Unsupported datatype' );
363  }
364 
365  if ( $value !== null ) {
366  $this->options[$name]['value'] = $value === $default ? null : $value;
367  }
368  }
369  }
370 
375  /* @{ */
380  public function offsetExists( $name ) {
381  return isset( $this->options[$name] );
382  }
383 
388  public function offsetGet( $name ) {
389  return $this->getValue( $name );
390  }
391 
395  public function offsetSet( $name, $value ) {
396  $this->setValue( $name, $value );
397  }
398 
402  public function offsetUnset( $name ) {
403  $this->delete( $name );
404  }
405  /* @} */
406 }
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
FormOptions\offsetUnset
offsetUnset( $name)
Delete the option.
Definition: FormOptions.php:402
FormOptions\getUnconsumedValues
getUnconsumedValues( $all=false)
Get all remaining values which have not been consumed by consumeValue() or consumeValues().
Definition: FormOptions.php:280
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:175
FormOptions\offsetGet
offsetGet( $name)
Retrieve an option value.
Definition: FormOptions.php:388
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:472
FormOptions\reset
reset( $name)
Delete the option value.
Definition: FormOptions.php:200
FormOptions\validateIntBounds
validateIntBounds( $name, $min, $max)
Definition: FormOptions.php:245
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:336
FormOptions\consumeValue
consumeValue( $name)
Get the value of given option and mark it as 'consumed'.
Definition: FormOptions.php:214
FormOptions\consumeValues
consumeValues( $names)
Get the values of given options and mark them as 'consumed'.
Definition: FormOptions.php:230
FormOptions\add
add( $name, $default, $type=self::AUTO)
Add an option to be handled by this FormOptions instance.
Definition: FormOptions.php:78
WebRequest\getText
getText( $name, $default='')
Fetch a text string from the given array or return $default if it's not set.
Definition: WebRequest.php:545
MWException
MediaWiki exception.
Definition: MWException.php:26
$out
$out
Definition: UtfNormalGenerate.php:167
WebRequest\getFloat
getFloat( $name, $default=0)
Fetch a floating point value from the input or return $default if not set.
Definition: WebRequest.php:489
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
FormOptions\setValue
setValue( $name, $value, $force=false)
Use to set the value of an option.
Definition: FormOptions.php:158
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
$value
$value
Definition: styleTest.css.php:45
FormOptions\$options
$options
Map of known option names to information about them.
Definition: FormOptions.php:67
FormOptions\validateBounds
validateBounds( $name, $min, $max)
Constrain a numeric value for a given option to a given range.
Definition: FormOptions.php:260
FormOptions\offsetSet
offsetSet( $name, $value)
Set an option to given value.
Definition: FormOptions.php:395
FormOptions\guessType
static guessType( $data)
Used to find out which type the data is.
Definition: FormOptions.php:114
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\getValueReal
getValueReal( $option)
Return current option value, based on a structure taken from $options.
Definition: FormOptions.php:187
FormOptions\getAllValues
getAllValues()
Format options to an array ( name => value )
Definition: FormOptions.php:314
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,...
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:460
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
FormOptions\offsetExists
offsetExists( $name)
Whether the option exists.
Definition: FormOptions.php:380
FormOptions\validateName
validateName( $name, $strict=false)
Verify that the given option name exists.
Definition: FormOptions.php:138
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:298
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:502
FormOptions\BOOL
const BOOL
Boolean type, maps guessType() to WebRequest::getBool()
Definition: FormOptions.php:50
$type
$type
Definition: testCompression.php:46