X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/mediawiki.git/blobdiff_plain/19e297c21b10b1b8a3acad5e73fc71dcb35db44a..6932310fd58ebef145fa01eb76edf7150284d8ea:/includes/htmlform/fields/HTMLRestrictionsField.php diff --git a/includes/htmlform/fields/HTMLRestrictionsField.php b/includes/htmlform/fields/HTMLRestrictionsField.php new file mode 100644 index 00000000..dbf2c8f6 --- /dev/null +++ b/includes/htmlform/fields/HTMLRestrictionsField.php @@ -0,0 +1,121 @@ +mLabel ) { + $this->mLabel = $this->msg( 'restrictionsfield-label' )->parse(); + } + } + + public function getHelpText() { + $helpText = parent::getHelpText(); + if ( $helpText === null ) { + $helpText = $this->msg( 'restrictionsfield-help' )->parse(); + } + return $helpText; + } + + /** + * @param WebRequest $request + * @return string|MWRestrictions Restrictions object or original string if invalid + */ + public function loadDataFromRequest( $request ) { + if ( !$request->getCheck( $this->mName ) ) { + return $this->getDefault(); + } + + $value = rtrim( $request->getText( $this->mName ), "\r\n" ); + $ips = $value === '' ? [] : explode( PHP_EOL, $value ); + try { + return MWRestrictions::newFromArray( [ 'IPAddresses' => $ips ] ); + } catch ( InvalidArgumentException $e ) { + return $value; + } + } + + /** + * @return MWRestrictions + */ + public function getDefault() { + $default = parent::getDefault(); + if ( $default === null ) { + $default = MWRestrictions::newDefault(); + } + return $default; + } + + /** + * @param string|MWRestrictions $value The value the field was submitted with + * @param array $alldata The data collected from the form + * + * @return bool|string|Message True on success, or String/Message error to display, or + * false to fail validation without displaying an error. + */ + public function validate( $value, $alldata ) { + if ( $this->isHidden( $alldata ) ) { + return true; + } + + if ( + isset( $this->mParams['required'] ) && $this->mParams['required'] !== false + && $value instanceof MWRestrictions && !$value->toArray()['IPAddresses'] + ) { + return $this->msg( 'htmlform-required' ); + } + + if ( is_string( $value ) ) { + // MWRestrictions::newFromArray failed; one of the IP ranges must be invalid + $status = Status::newGood(); + foreach ( explode( PHP_EOL, $value ) as $range ) { + if ( !\IP::isIPAddress( $range ) ) { + $status->fatal( 'restrictionsfield-badip', $range ); + } + } + if ( $status->isOK() ) { + $status->fatal( 'unknown-error' ); + } + return $status->getMessage(); + } + + if ( isset( $this->mValidationCallback ) ) { + return call_user_func( $this->mValidationCallback, $value, $alldata, $this->mParent ); + } + + return true; + } + + /** + * @param string|MWRestrictions $value + * @return string + */ + public function getInputHTML( $value ) { + if ( $value instanceof MWRestrictions ) { + $value = implode( PHP_EOL, $value->toArray()['IPAddresses'] ); + } + return parent::getInputHTML( $value ); + } + + /** + * @param MWRestrictions $value + * @return string + */ + public function getInputOOUI( $value ) { + if ( $value instanceof MWRestrictions ) { + $value = implode( PHP_EOL, $value->toArray()['IPAddresses'] ); + } + return parent::getInputOOUI( $value ); + } +}