Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / htmlform / fields / HTMLRestrictionsField.php
blobdbf2c8f6444c8a25715317aeb16ec4c6159761d7
1 <?php
3 /**
4 * Class for updating an MWRestrictions value (which is, currently, basically just an IP address
5 * list).
7 * Will be represented as a textarea with one address per line, with intelligent defaults for
8 * label, help text and row count.
10 * The value returned will be an MWRestrictions or the input string if it was not a list of
11 * valid IP ranges.
13 class HTMLRestrictionsField extends HTMLTextAreaField {
14 const DEFAULT_ROWS = 5;
16 public function __construct( array $params ) {
17 parent::__construct( $params );
18 if ( !$this->mLabel ) {
19 $this->mLabel = $this->msg( 'restrictionsfield-label' )->parse();
23 public function getHelpText() {
24 $helpText = parent::getHelpText();
25 if ( $helpText === null ) {
26 $helpText = $this->msg( 'restrictionsfield-help' )->parse();
28 return $helpText;
31 /**
32 * @param WebRequest $request
33 * @return string|MWRestrictions Restrictions object or original string if invalid
35 public function loadDataFromRequest( $request ) {
36 if ( !$request->getCheck( $this->mName ) ) {
37 return $this->getDefault();
40 $value = rtrim( $request->getText( $this->mName ), "\r\n" );
41 $ips = $value === '' ? [] : explode( PHP_EOL, $value );
42 try {
43 return MWRestrictions::newFromArray( [ 'IPAddresses' => $ips ] );
44 } catch ( InvalidArgumentException $e ) {
45 return $value;
49 /**
50 * @return MWRestrictions
52 public function getDefault() {
53 $default = parent::getDefault();
54 if ( $default === null ) {
55 $default = MWRestrictions::newDefault();
57 return $default;
60 /**
61 * @param string|MWRestrictions $value The value the field was submitted with
62 * @param array $alldata The data collected from the form
64 * @return bool|string|Message True on success, or String/Message error to display, or
65 * false to fail validation without displaying an error.
67 public function validate( $value, $alldata ) {
68 if ( $this->isHidden( $alldata ) ) {
69 return true;
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' );
79 if ( is_string( $value ) ) {
80 // MWRestrictions::newFromArray failed; one of the IP ranges must be invalid
81 $status = Status::newGood();
82 foreach ( explode( PHP_EOL, $value ) as $range ) {
83 if ( !\IP::isIPAddress( $range ) ) {
84 $status->fatal( 'restrictionsfield-badip', $range );
87 if ( $status->isOK() ) {
88 $status->fatal( 'unknown-error' );
90 return $status->getMessage();
93 if ( isset( $this->mValidationCallback ) ) {
94 return call_user_func( $this->mValidationCallback, $value, $alldata, $this->mParent );
97 return true;
101 * @param string|MWRestrictions $value
102 * @return string
104 public function getInputHTML( $value ) {
105 if ( $value instanceof MWRestrictions ) {
106 $value = implode( PHP_EOL, $value->toArray()['IPAddresses'] );
108 return parent::getInputHTML( $value );
112 * @param MWRestrictions $value
113 * @return string
115 public function getInputOOUI( $value ) {
116 if ( $value instanceof MWRestrictions ) {
117 $value = implode( PHP_EOL, $value->toArray()['IPAddresses'] );
119 return parent::getInputOOUI( $value );