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  return parent::getHelpText() ?? $this->msg( 'restrictionsfield-help' )->parse();
33  }
34 
39  public function loadDataFromRequest( $request ) {
40  if ( !$request->getCheck( $this->mName ) ) {
41  return $this->getDefault();
42  }
43 
44  $value = rtrim( $request->getText( $this->mName ), "\r\n" );
45  $ips = $value === '' ? [] : explode( "\n", $value );
46  try {
47  return MWRestrictions::newFromArray( [ 'IPAddresses' => $ips ] );
48  } catch ( InvalidArgumentException $e ) {
49  return $value;
50  }
51  }
52 
56  public function getDefault() {
57  return parent::getDefault() ?? MWRestrictions::newDefault();
58  }
59 
67  public function validate( $value, $alldata ) {
68  if ( $this->isHidden( $alldata ) ) {
69  return true;
70  }
71 
72  if (
73  isset( $this->mParams['required'] ) && $this->mParams['required'] !== false
74  && $value instanceof MWRestrictions && !$value->toArray()['IPAddresses']
75  ) {
76  return $this->msg( 'htmlform-required' );
77  }
78 
79  if ( is_string( $value ) ) {
80  // MWRestrictions::newFromArray failed; one of the IP ranges must be invalid
81  $status = Status::newGood();
82  foreach ( explode( "\n", $value ) as $range ) {
83  if ( !IPUtils::isIPAddress( $range ) ) {
84  $status->fatal( 'restrictionsfield-badip', $range );
85  }
86  }
87  if ( $status->isOK() ) {
88  $status->fatal( 'unknown-error' );
89  }
90  return $status->getMessage();
91  }
92 
93  if ( isset( $this->mValidationCallback ) ) {
94  return call_user_func( $this->mValidationCallback, $value, $alldata, $this->mParent );
95  }
96 
97  return true;
98  }
99 
104  public function getInputHTML( $value ) {
105  if ( $value instanceof MWRestrictions ) {
106  $value = implode( "\n", $value->toArray()['IPAddresses'] );
107  }
108  return parent::getInputHTML( $value );
109  }
110 
115  public function getInputOOUI( $value ) {
116  if ( $value instanceof MWRestrictions ) {
117  $value = implode( "\n", $value->toArray()['IPAddresses'] );
118  }
119  return parent::getInputOOUI( $value );
120  }
121 }
isHidden( $alldata)
Test whether this field is supposed to be hidden, based on the values of the other form fields.
msg( $key,... $params)
Get a translated interface message.
Class for updating an MWRestrictions value (which is, currently, basically just an IP address list).
validate( $value, $alldata)
getHelpText()
Determine the help text to display.
A class to check request restrictions expressed as a JSON object.
static newFromArray(array $restrictions)
static newDefault()
toArray()
Return the restrictions as an array.
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:85