[MediaWiki-commits] [Gerrit] mediawiki/core[master]: Add HTMLFormField class for MWRestrictions and use it for bo...
jenkins-bot has submitted this change and it was merged. Change subject: Add HTMLFormField class for MWRestrictions and use it for bot passwords .. Add HTMLFormField class for MWRestrictions and use it for bot passwords Change-Id: Ib50238e3be5eec63eb5df97154b60dc4ca33d581 --- M autoload.php M includes/htmlform/HTMLFormField.php A includes/htmlform/fields/HTMLRestrictionsField.php M includes/specials/SpecialBotPasswords.php M includes/utils/MWRestrictions.php M languages/i18n/en.json M languages/i18n/qqq.json A tests/phpunit/includes/htmlform/HTMLRestrictionsFieldTest.php 8 files changed, 202 insertions(+), 18 deletions(-) Approvals: Anomie: Looks good to me, approved jenkins-bot: Verified diff --git a/autoload.php b/autoload.php index 1e98f63..2c196dd 100644 --- a/autoload.php +++ b/autoload.php @@ -544,6 +544,7 @@ 'HTMLMultiSelectField' => __DIR__ . '/includes/htmlform/fields/HTMLMultiSelectField.php', 'HTMLNestedFilterable' => __DIR__ . '/includes/htmlform/HTMLNestedFilterable.php', 'HTMLRadioField' => __DIR__ . '/includes/htmlform/fields/HTMLRadioField.php', + 'HTMLRestrictionsField' => __DIR__ . '/includes/htmlform/fields/HTMLRestrictionsField.php', 'HTMLSelectAndOtherField' => __DIR__ . '/includes/htmlform/fields/HTMLSelectAndOtherField.php', 'HTMLSelectField' => __DIR__ . '/includes/htmlform/fields/HTMLSelectField.php', 'HTMLSelectLimitField' => __DIR__ . '/includes/htmlform/fields/HTMLSelectLimitField.php', diff --git a/includes/htmlform/HTMLFormField.php b/includes/htmlform/HTMLFormField.php index 8604ba2..4afdea7 100644 --- a/includes/htmlform/HTMLFormField.php +++ b/includes/htmlform/HTMLFormField.php @@ -821,7 +821,7 @@ /** * Determine the help text to display * @since 1.20 -* @return string HTML +* @return string|null HTML */ public function getHelpText() { $helptext = null; diff --git a/includes/htmlform/fields/HTMLRestrictionsField.php b/includes/htmlform/fields/HTMLRestrictionsField.php new file mode 100644 index 000..8dc16bf --- /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 +*/ + 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 True on success, or String 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' )->parse(); + } + + 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(
[MediaWiki-commits] [Gerrit] mediawiki/core[master]: Add HTMLFormField class for MWRestrictions and use it for bo...
Gergő Tisza has uploaded a new change for review. https://gerrit.wikimedia.org/r/311889 Change subject: Add HTMLFormField class for MWRestrictions and use it for bot passwords .. Add HTMLFormField class for MWRestrictions and use it for bot passwords Change-Id: Ib50238e3be5eec63eb5df97154b60dc4ca33d581 --- M autoload.php M includes/htmlform/HTMLFormField.php A includes/htmlform/fields/HTMLRestrictionsField.php M includes/specials/SpecialBotPasswords.php M includes/utils/MWRestrictions.php M languages/i18n/en.json M languages/i18n/qqq.json A tests/phpunit/includes/htmlform/HTMLRestrictionsFieldTest.php 8 files changed, 200 insertions(+), 18 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/89/311889/1 diff --git a/autoload.php b/autoload.php index 198e477..1b21365 100644 --- a/autoload.php +++ b/autoload.php @@ -544,6 +544,7 @@ 'HTMLMultiSelectField' => __DIR__ . '/includes/htmlform/fields/HTMLMultiSelectField.php', 'HTMLNestedFilterable' => __DIR__ . '/includes/htmlform/HTMLNestedFilterable.php', 'HTMLRadioField' => __DIR__ . '/includes/htmlform/fields/HTMLRadioField.php', + 'HTMLRestrictionsField' => __DIR__ . '/includes/htmlform/fields/HTMLRestrictionsField.php', 'HTMLSelectAndOtherField' => __DIR__ . '/includes/htmlform/fields/HTMLSelectAndOtherField.php', 'HTMLSelectField' => __DIR__ . '/includes/htmlform/fields/HTMLSelectField.php', 'HTMLSelectLimitField' => __DIR__ . '/includes/htmlform/fields/HTMLSelectLimitField.php', diff --git a/includes/htmlform/HTMLFormField.php b/includes/htmlform/HTMLFormField.php index 8604ba2..4afdea7 100644 --- a/includes/htmlform/HTMLFormField.php +++ b/includes/htmlform/HTMLFormField.php @@ -821,7 +821,7 @@ /** * Determine the help text to display * @since 1.20 -* @return string HTML +* @return string|null HTML */ public function getHelpText() { $helptext = null; diff --git a/includes/htmlform/fields/HTMLRestrictionsField.php b/includes/htmlform/fields/HTMLRestrictionsField.php new file mode 100644 index 000..f97962f --- /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 +*/ + 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 True on success, or String 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' )->parse(); + } + + 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 ) ) { +