MediaWiki  master
HTMLRestrictionsField.php
Go to the documentation of this file.
1 <?php
2 
3 use Wikimedia\IPUtils;
4 
18  protected const DEFAULT_ROWS = 5;
19 
24  public function __construct( array $params ) {
25  parent::__construct( $params );
26  if ( !$this->mLabel ) {
27  $this->mLabel = $this->msg( 'restrictionsfield-label' )->parse();
28  }
29  }
30 
31  public function getHelpText() {
32  $helpText = parent::getHelpText();
33  if ( $helpText === null ) {
34  $helpText = $this->msg( 'restrictionsfield-help' )->parse();
35  }
36  return $helpText;
37  }
38 
43  public function loadDataFromRequest( $request ) {
44  if ( !$request->getCheck( $this->mName ) ) {
45  return $this->getDefault();
46  }
47 
48  $value = rtrim( $request->getText( $this->mName ), "\r\n" );
49  $ips = $value === '' ? [] : explode( "\n", $value );
50  try {
51  return MWRestrictions::newFromArray( [ 'IPAddresses' => $ips ] );
52  } catch ( InvalidArgumentException $e ) {
53  return $value;
54  }
55  }
56 
60  public function getDefault() {
61  $default = parent::getDefault();
62  if ( $default === null ) {
63  $default = MWRestrictions::newDefault();
64  }
65  return $default;
66  }
67 
75  public function validate( $value, $alldata ) {
76  if ( $this->isHidden( $alldata ) ) {
77  return true;
78  }
79 
80  if (
81  isset( $this->mParams['required'] ) && $this->mParams['required'] !== false
82  && $value instanceof MWRestrictions && !$value->toArray()['IPAddresses']
83  ) {
84  return $this->msg( 'htmlform-required' );
85  }
86 
87  if ( is_string( $value ) ) {
88  // MWRestrictions::newFromArray failed; one of the IP ranges must be invalid
89  $status = Status::newGood();
90  foreach ( explode( "\n", $value ) as $range ) {
91  if ( !IPUtils::isIPAddress( $range ) ) {
92  $status->fatal( 'restrictionsfield-badip', $range );
93  }
94  }
95  if ( $status->isOK() ) {
96  $status->fatal( 'unknown-error' );
97  }
98  return $status->getMessage();
99  }
100 
101  if ( isset( $this->mValidationCallback ) ) {
102  return call_user_func( $this->mValidationCallback, $value, $alldata, $this->mParent );
103  }
104 
105  return true;
106  }
107 
112  public function getInputHTML( $value ) {
113  if ( $value instanceof MWRestrictions ) {
114  $value = implode( "\n", $value->toArray()['IPAddresses'] );
115  }
116  return parent::getInputHTML( $value );
117  }
118 
123  public function getInputOOUI( $value ) {
124  if ( $value instanceof MWRestrictions ) {
125  $value = implode( "\n", $value->toArray()['IPAddresses'] );
126  }
127  return parent::getInputOOUI( $value );
128  }
129 }
MWRestrictions
A class to check request restrictions expressed as a JSON object.
Definition: MWRestrictions.php:26
HTMLRestrictionsField\getDefault
getDefault()
Definition: HTMLRestrictionsField.php:60
HTMLRestrictionsField\DEFAULT_ROWS
const DEFAULT_ROWS
Definition: HTMLRestrictionsField.php:18
HTMLRestrictionsField\getHelpText
getHelpText()
Determine the help text to display Stable to override.
Definition: HTMLRestrictionsField.php:31
HTMLRestrictionsField
Class for updating an MWRestrictions value (which is, currently, basically just an IP address list).
Definition: HTMLRestrictionsField.php:17
HTMLRestrictionsField\loadDataFromRequest
loadDataFromRequest( $request)
Definition: HTMLRestrictionsField.php:43
MWRestrictions\newDefault
static newDefault()
Definition: MWRestrictions.php:43
HTMLFormField\isHidden
isHidden( $alldata)
Test whether this field is supposed to be hidden, based on the values of the other form fields.
Definition: HTMLFormField.php:274
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:82
HTMLRestrictionsField\getInputHTML
getInputHTML( $value)
Definition: HTMLRestrictionsField.php:112
MWRestrictions\toArray
toArray()
Return the restrictions as an array.
Definition: MWRestrictions.php:102
HTMLRestrictionsField\validate
validate( $value, $alldata)
Definition: HTMLRestrictionsField.php:75
MWRestrictions\newFromArray
static newFromArray(array $restrictions)
Definition: MWRestrictions.php:52
HTMLFormField\msg
msg( $key,... $params)
Get a translated interface message.
Definition: HTMLFormField.php:87
HTMLRestrictionsField\__construct
__construct(array $params)
Stable to call .
Definition: HTMLRestrictionsField.php:24
HTMLRestrictionsField\getInputOOUI
getInputOOUI( $value)
Definition: HTMLRestrictionsField.php:123
HTMLTextAreaField
Definition: HTMLTextAreaField.php:6