HTMLRestrictionsField.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Class for updating an MWRestrictions value (which is, currently, basically just an IP address
  4. * list).
  5. *
  6. * Will be represented as a textarea with one address per line, with intelligent defaults for
  7. * label, help text and row count.
  8. *
  9. * The value returned will be an MWRestrictions or the input string if it was not a list of
  10. * valid IP ranges.
  11. */
  12. class HTMLRestrictionsField extends HTMLTextAreaField {
  13. const DEFAULT_ROWS = 5;
  14. public function __construct( array $params ) {
  15. parent::__construct( $params );
  16. if ( !$this->mLabel ) {
  17. $this->mLabel = $this->msg( 'restrictionsfield-label' )->parse();
  18. }
  19. }
  20. public function getHelpText() {
  21. $helpText = parent::getHelpText();
  22. if ( $helpText === null ) {
  23. $helpText = $this->msg( 'restrictionsfield-help' )->parse();
  24. }
  25. return $helpText;
  26. }
  27. /**
  28. * @param WebRequest $request
  29. * @return string|MWRestrictions Restrictions object or original string if invalid
  30. */
  31. public function loadDataFromRequest( $request ) {
  32. if ( !$request->getCheck( $this->mName ) ) {
  33. return $this->getDefault();
  34. }
  35. $value = rtrim( $request->getText( $this->mName ), "\r\n" );
  36. $ips = $value === '' ? [] : explode( "\n", $value );
  37. try {
  38. return MWRestrictions::newFromArray( [ 'IPAddresses' => $ips ] );
  39. } catch ( InvalidArgumentException $e ) {
  40. return $value;
  41. }
  42. }
  43. /**
  44. * @return MWRestrictions
  45. */
  46. public function getDefault() {
  47. $default = parent::getDefault();
  48. if ( $default === null ) {
  49. $default = MWRestrictions::newDefault();
  50. }
  51. return $default;
  52. }
  53. /**
  54. * @param string|MWRestrictions $value The value the field was submitted with
  55. * @param array $alldata The data collected from the form
  56. *
  57. * @return bool|string|Message True on success, or String/Message error to display, or
  58. * false to fail validation without displaying an error.
  59. */
  60. public function validate( $value, $alldata ) {
  61. if ( $this->isHidden( $alldata ) ) {
  62. return true;
  63. }
  64. if (
  65. isset( $this->mParams['required'] ) && $this->mParams['required'] !== false
  66. && $value instanceof MWRestrictions && !$value->toArray()['IPAddresses']
  67. ) {
  68. return $this->msg( 'htmlform-required' );
  69. }
  70. if ( is_string( $value ) ) {
  71. // MWRestrictions::newFromArray failed; one of the IP ranges must be invalid
  72. $status = Status::newGood();
  73. foreach ( explode( "\n", $value ) as $range ) {
  74. if ( !\IP::isIPAddress( $range ) ) {
  75. $status->fatal( 'restrictionsfield-badip', $range );
  76. }
  77. }
  78. if ( $status->isOK() ) {
  79. $status->fatal( 'unknown-error' );
  80. }
  81. return $status->getMessage();
  82. }
  83. if ( isset( $this->mValidationCallback ) ) {
  84. return call_user_func( $this->mValidationCallback, $value, $alldata, $this->mParent );
  85. }
  86. return true;
  87. }
  88. /**
  89. * @param string|MWRestrictions $value
  90. * @return string
  91. */
  92. public function getInputHTML( $value ) {
  93. if ( $value instanceof MWRestrictions ) {
  94. $value = implode( "\n", $value->toArray()['IPAddresses'] );
  95. }
  96. return parent::getInputHTML( $value );
  97. }
  98. /**
  99. * @param MWRestrictions $value
  100. * @return string
  101. */
  102. public function getInputOOUI( $value ) {
  103. if ( $value instanceof MWRestrictions ) {
  104. $value = implode( "\n", $value->toArray()['IPAddresses'] );
  105. }
  106. return parent::getInputOOUI( $value );
  107. }
  108. }