MediaWiki REL1_39
HTMLRestrictionsField.php
Go to the documentation of this file.
1<?php
2
3use 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}
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).
getHelpText()
Determine the help text to display.
A class to check request restrictions expressed as a JSON object.
toArray()
Return the restrictions as an array.