MediaWiki  master
HTMLRestrictionsField.php
Go to the documentation of this file.
1 <?php
2 
3 use Wikimedia\IPUtils;
4 
16  protected const DEFAULT_ROWS = 5;
17 
18  public function __construct( array $params ) {
19  parent::__construct( $params );
20  if ( !$this->mLabel ) {
21  $this->mLabel = $this->msg( 'restrictionsfield-label' )->parse();
22  }
23  }
24 
25  public function getHelpText() {
26  $helpText = parent::getHelpText();
27  if ( $helpText === null ) {
28  $helpText = $this->msg( 'restrictionsfield-help' )->parse();
29  }
30  return $helpText;
31  }
32 
37  public function loadDataFromRequest( $request ) {
38  if ( !$request->getCheck( $this->mName ) ) {
39  return $this->getDefault();
40  }
41 
42  $value = rtrim( $request->getText( $this->mName ), "\r\n" );
43  $ips = $value === '' ? [] : explode( "\n", $value );
44  try {
45  return MWRestrictions::newFromArray( [ 'IPAddresses' => $ips ] );
46  } catch ( InvalidArgumentException $e ) {
47  return $value;
48  }
49  }
50 
54  public function getDefault() {
55  $default = parent::getDefault();
56  if ( $default === null ) {
57  $default = MWRestrictions::newDefault();
58  }
59  return $default;
60  }
61 
69  public function validate( $value, $alldata ) {
70  if ( $this->isHidden( $alldata ) ) {
71  return true;
72  }
73 
74  if (
75  isset( $this->mParams['required'] ) && $this->mParams['required'] !== false
76  && $value instanceof MWRestrictions && !$value->toArray()['IPAddresses']
77  ) {
78  return $this->msg( 'htmlform-required' );
79  }
80 
81  if ( is_string( $value ) ) {
82  // MWRestrictions::newFromArray failed; one of the IP ranges must be invalid
83  $status = Status::newGood();
84  foreach ( explode( "\n", $value ) as $range ) {
85  if ( !IPUtils::isIPAddress( $range ) ) {
86  $status->fatal( 'restrictionsfield-badip', $range );
87  }
88  }
89  if ( $status->isOK() ) {
90  $status->fatal( 'unknown-error' );
91  }
92  return $status->getMessage();
93  }
94 
95  if ( isset( $this->mValidationCallback ) ) {
96  return call_user_func( $this->mValidationCallback, $value, $alldata, $this->mParent );
97  }
98 
99  return true;
100  }
101 
106  public function getInputHTML( $value ) {
107  if ( $value instanceof MWRestrictions ) {
108  $value = implode( "\n", $value->toArray()['IPAddresses'] );
109  }
110  return parent::getInputHTML( $value );
111  }
112 
117  public function getInputOOUI( $value ) {
118  if ( $value instanceof MWRestrictions ) {
119  $value = implode( "\n", $value->toArray()['IPAddresses'] );
120  }
121  return parent::getInputOOUI( $value );
122  }
123 }
MWRestrictions
A class to check request restrictions expressed as a JSON object.
Definition: MWRestrictions.php:26
HTMLRestrictionsField\getDefault
getDefault()
Definition: HTMLRestrictionsField.php:54
HTMLRestrictionsField\DEFAULT_ROWS
const DEFAULT_ROWS
Definition: HTMLRestrictionsField.php:16
HTMLRestrictionsField\getHelpText
getHelpText()
Determine the help text to display.
Definition: HTMLRestrictionsField.php:25
HTMLRestrictionsField
Class for updating an MWRestrictions value (which is, currently, basically just an IP address list).
Definition: HTMLRestrictionsField.php:15
HTMLRestrictionsField\loadDataFromRequest
loadDataFromRequest( $request)
Definition: HTMLRestrictionsField.php:37
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:269
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:81
HTMLRestrictionsField\getInputHTML
getInputHTML( $value)
Definition: HTMLRestrictionsField.php:106
MWRestrictions\toArray
toArray()
Return the restrictions as an array.
Definition: MWRestrictions.php:102
HTMLRestrictionsField\validate
validate( $value, $alldata)
Definition: HTMLRestrictionsField.php:69
MWRestrictions\newFromArray
static newFromArray(array $restrictions)
Definition: MWRestrictions.php:52
HTMLFormField\msg
msg( $key,... $params)
Get a translated interface message.
Definition: HTMLFormField.php:83
HTMLRestrictionsField\__construct
__construct(array $params)
Definition: HTMLRestrictionsField.php:18
HTMLRestrictionsField\getInputOOUI
getInputOOUI( $value)
Definition: HTMLRestrictionsField.php:117
HTMLTextAreaField
Definition: HTMLTextAreaField.php:3