MediaWiki REL1_35
HTMLRestrictionsField.php
Go to the documentation of this file.
1<?php
2
3use Wikimedia\IPUtils;
4
18 protected const DEFAULT_ROWS = 5;
19
20 /*
21 * @stable to call
22 */
23 public function __construct( array $params ) {
24 parent::__construct( $params );
25 if ( !$this->mLabel ) {
26 $this->mLabel = $this->msg( 'restrictionsfield-label' )->parse();
27 }
28 }
29
30 public function getHelpText() {
31 $helpText = parent::getHelpText();
32 if ( $helpText === null ) {
33 $helpText = $this->msg( 'restrictionsfield-help' )->parse();
34 }
35 return $helpText;
36 }
37
42 public function loadDataFromRequest( $request ) {
43 if ( !$request->getCheck( $this->mName ) ) {
44 return $this->getDefault();
45 }
46
47 $value = rtrim( $request->getText( $this->mName ), "\r\n" );
48 $ips = $value === '' ? [] : explode( "\n", $value );
49 try {
50 return MWRestrictions::newFromArray( [ 'IPAddresses' => $ips ] );
51 } catch ( InvalidArgumentException $e ) {
52 return $value;
53 }
54 }
55
59 public function getDefault() {
60 $default = parent::getDefault();
61 if ( $default === null ) {
62 $default = MWRestrictions::newDefault();
63 }
64 return $default;
65 }
66
74 public function validate( $value, $alldata ) {
75 if ( $this->isHidden( $alldata ) ) {
76 return true;
77 }
78
79 if (
80 isset( $this->mParams['required'] ) && $this->mParams['required'] !== false
81 && $value instanceof MWRestrictions && !$value->toArray()['IPAddresses']
82 ) {
83 return $this->msg( 'htmlform-required' );
84 }
85
86 if ( is_string( $value ) ) {
87 // MWRestrictions::newFromArray failed; one of the IP ranges must be invalid
88 $status = Status::newGood();
89 foreach ( explode( "\n", $value ) as $range ) {
90 if ( !IPUtils::isIPAddress( $range ) ) {
91 $status->fatal( 'restrictionsfield-badip', $range );
92 }
93 }
94 if ( $status->isOK() ) {
95 $status->fatal( 'unknown-error' );
96 }
97 return $status->getMessage();
98 }
99
100 if ( isset( $this->mValidationCallback ) ) {
101 return call_user_func( $this->mValidationCallback, $value, $alldata, $this->mParent );
102 }
103
104 return true;
105 }
106
111 public function getInputHTML( $value ) {
112 if ( $value instanceof MWRestrictions ) {
113 $value = implode( "\n", $value->toArray()['IPAddresses'] );
114 }
115 return parent::getInputHTML( $value );
116 }
117
122 public function getInputOOUI( $value ) {
123 if ( $value instanceof MWRestrictions ) {
124 $value = implode( "\n", $value->toArray()['IPAddresses'] );
125 }
126 return parent::getInputOOUI( $value );
127 }
128}
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 Stable to override.
A class to check request restrictions expressed as a JSON object.
static newFromArray(array $restrictions)
toArray()
Return the restrictions as an array.