2016-09-21 01:33:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
2023-09-07 11:46:15 +00:00
|
|
|
use MediaWiki\Request\WebRequest;
|
2023-08-25 12:29:41 +00:00
|
|
|
use MediaWiki\Status\Status;
|
2019-06-25 18:53:15 +00:00
|
|
|
use Wikimedia\IPUtils;
|
|
|
|
|
|
2016-09-21 01:33:26 +00:00
|
|
|
/**
|
|
|
|
|
* Class for updating an MWRestrictions value (which is, currently, basically just an IP address
|
|
|
|
|
* list).
|
|
|
|
|
*
|
|
|
|
|
* Will be represented as a textarea with one address per line, with intelligent defaults for
|
|
|
|
|
* label, help text and row count.
|
|
|
|
|
*
|
|
|
|
|
* The value returned will be an MWRestrictions or the input string if it was not a list of
|
|
|
|
|
* valid IP ranges.
|
2020-07-10 19:23:59 +00:00
|
|
|
*
|
2016-09-21 01:33:26 +00:00
|
|
|
*/
|
|
|
|
|
class HTMLRestrictionsField extends HTMLTextAreaField {
|
2020-05-15 22:07:42 +00:00
|
|
|
protected const DEFAULT_ROWS = 5;
|
2016-09-21 01:33:26 +00:00
|
|
|
|
2020-07-23 09:41:58 +00:00
|
|
|
/**
|
2020-07-10 19:23:59 +00:00
|
|
|
* @stable to call
|
2020-07-23 09:41:58 +00:00
|
|
|
* @inheritDoc
|
2020-07-10 19:23:59 +00:00
|
|
|
*/
|
2016-09-21 01:33:26 +00:00
|
|
|
public function __construct( array $params ) {
|
|
|
|
|
parent::__construct( $params );
|
|
|
|
|
if ( !$this->mLabel ) {
|
|
|
|
|
$this->mLabel = $this->msg( 'restrictionsfield-label' )->parse();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getHelpText() {
|
2022-12-05 20:37:13 +00:00
|
|
|
return parent::getHelpText() ?? $this->msg( 'restrictionsfield-help' )->parse();
|
2016-09-21 01:33:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param WebRequest $request
|
|
|
|
|
* @return string|MWRestrictions Restrictions object or original string if invalid
|
|
|
|
|
*/
|
2016-11-04 10:40:42 +00:00
|
|
|
public function loadDataFromRequest( $request ) {
|
2016-09-21 01:33:26 +00:00
|
|
|
if ( !$request->getCheck( $this->mName ) ) {
|
|
|
|
|
return $this->getDefault();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$value = rtrim( $request->getText( $this->mName ), "\r\n" );
|
2017-09-20 20:01:34 +00:00
|
|
|
$ips = $value === '' ? [] : explode( "\n", $value );
|
2016-09-21 01:33:26 +00:00
|
|
|
try {
|
|
|
|
|
return MWRestrictions::newFromArray( [ 'IPAddresses' => $ips ] );
|
|
|
|
|
} catch ( InvalidArgumentException $e ) {
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return MWRestrictions
|
|
|
|
|
*/
|
|
|
|
|
public function getDefault() {
|
2022-12-05 20:37:13 +00:00
|
|
|
return parent::getDefault() ?? MWRestrictions::newDefault();
|
2016-09-21 01:33:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string|MWRestrictions $value The value the field was submitted with
|
|
|
|
|
* @param array $alldata The data collected from the form
|
|
|
|
|
*
|
2016-11-02 17:13:43 +00:00
|
|
|
* @return bool|string|Message True on success, or String/Message error to display, or
|
2016-09-21 01:33:26 +00:00
|
|
|
* false to fail validation without displaying an error.
|
|
|
|
|
*/
|
|
|
|
|
public function validate( $value, $alldata ) {
|
2022-02-28 15:05:45 +00:00
|
|
|
if ( $this->isHidden( $alldata ) ) {
|
2016-09-21 01:33:26 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
isset( $this->mParams['required'] ) && $this->mParams['required'] !== false
|
|
|
|
|
&& $value instanceof MWRestrictions && !$value->toArray()['IPAddresses']
|
|
|
|
|
) {
|
2016-11-02 17:13:43 +00:00
|
|
|
return $this->msg( 'htmlform-required' );
|
2016-09-21 01:33:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( is_string( $value ) ) {
|
|
|
|
|
// MWRestrictions::newFromArray failed; one of the IP ranges must be invalid
|
|
|
|
|
$status = Status::newGood();
|
2020-06-27 01:13:01 +00:00
|
|
|
foreach ( explode( "\n", $value ) as $range ) {
|
2019-06-25 18:53:15 +00:00
|
|
|
if ( !IPUtils::isIPAddress( $range ) ) {
|
2016-09-21 01:33:26 +00:00
|
|
|
$status->fatal( 'restrictionsfield-badip', $range );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( $status->isOK() ) {
|
|
|
|
|
$status->fatal( 'unknown-error' );
|
|
|
|
|
}
|
2016-11-02 17:13:43 +00:00
|
|
|
return $status->getMessage();
|
2016-09-21 01:33:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 ) {
|
2017-09-20 20:01:34 +00:00
|
|
|
$value = implode( "\n", $value->toArray()['IPAddresses'] );
|
2016-09-21 01:33:26 +00:00
|
|
|
}
|
|
|
|
|
return parent::getInputHTML( $value );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param MWRestrictions $value
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getInputOOUI( $value ) {
|
|
|
|
|
if ( $value instanceof MWRestrictions ) {
|
2017-09-20 20:01:34 +00:00
|
|
|
$value = implode( "\n", $value->toArray()['IPAddresses'] );
|
2016-09-21 01:33:26 +00:00
|
|
|
}
|
|
|
|
|
return parent::getInputOOUI( $value );
|
|
|
|
|
}
|
|
|
|
|
}
|