3 namespace MediaWiki\HTMLForm\Field
;
5 use MediaWiki\HTMLForm\HTMLFormField
;
6 use MediaWiki\MediaWikiServices
;
7 use MediaWiki\Message\Message
;
8 use MediaWiki\Request\WebRequest
;
12 * Class for updating an MWRestrictions value (which is, currently, basically just an IP address
15 * Will be represented as a textarea with one address per line, with intelligent defaults for
16 * label, help text and row count.
18 * The value returned will be an MWRestrictions or the input string if it was not a list of
22 class HTMLRestrictionsField
extends HTMLFormField
{
23 protected const DEFAULT_ROWS
= 5;
25 private HTMLTextAreaField
$ipField;
26 private HTMLTagMultiselectField
$pagesField;
32 public function __construct( array $params ) {
33 parent
::__construct( $params );
34 $this->ipField
= new HTMLTextAreaField( [
35 'parent' => $params['parent'],
36 'fieldname' => $params['fieldname'] . '-ip',
37 'rows' => self
::DEFAULT_ROWS
,
38 'required' => $params['required'] ??
false,
39 'help-message' => 'restrictionsfield-help',
40 'label-message' => 'restrictionsfield-label',
43 // Cannot really use a TitlesMultiselect field as the pages could be
45 $this->pagesField
= new HTMLTagMultiselectField( [
46 'parent' => $params['parent'],
47 'fieldname' => $params['fieldname'] . '-pages',
48 'label-message' => 'restrictionsfields-pages-label',
49 'help-message' => 'restrictionsfields-pages-help',
50 'allowArbitrary' => true,
57 * @param WebRequest $request
58 * @return MWRestrictions Restrictions object
60 public function loadDataFromRequest( $request ) {
61 if ( !$request->getCheck( $this->mName
. '-ip' ) ) {
62 return $this->getDefault();
65 $ipValue = rtrim( $request->getText( $this->mName
. '-ip' ), "\r\n" );
66 $ips = $ipValue === '' ?
[] : explode( "\n", $ipValue );
67 $pagesValue = $request->getText( $this->mName
. '-pages' );
68 $pageList = $pagesValue ?
explode( "\n", $pagesValue ) : [];
69 return MWRestrictions
::newFromArray( [ 'IPAddresses' => $ips, 'Pages' => $pageList ] );
73 * @return MWRestrictions
75 public function getDefault() {
76 return parent
::getDefault() ?? MWRestrictions
::newDefault();
80 * @param MWRestrictions $value The value the field was submitted with
81 * @param array $alldata The data collected from the form
83 * @return bool|string|Message True on success, or String/Message error to display, or
84 * false to fail validation without displaying an error.
86 public function validate( $value, $alldata ) {
87 if ( $this->isHidden( $alldata ) ) {
92 isset( $this->mParams
['required'] ) && $this->mParams
['required'] !== false
93 && !$value->toArray()['IPAddresses']
95 return $this->msg( 'htmlform-required' );
98 if ( !$value->validity
->isGood() ) {
99 $statusFormatter = MediaWikiServices
::getInstance()->getFormatterFactory()->getStatusFormatter(
100 $this->mParent
->getContext()
102 return $statusFormatter->getMessage( $value->validity
);
105 if ( $this->mValidationCallback
!== null ) {
106 return call_user_func( $this->mValidationCallback
, $value, $alldata, $this->mParent
);
113 * @param MWRestrictions $value
116 public function getInputHTML( $value ) {
117 $ipValue = implode( "\n", $value->toArray()['IPAddresses'] );
118 $pagesValue = implode( "\n", $value->toArray()['Pages'] ??
[] );
120 $this->ipField
->getDiv( $ipValue ) .
121 $this->pagesField
->getDiv( $pagesValue )
126 * @param MWRestrictions $value
128 * @suppress PhanParamSignatureMismatch
130 public function getInputOOUI( $value ) {
131 $ipValue = implode( "\n", $value->toArray()['IPAddresses'] );
132 $pagesValue = implode( "\n", $value->toArray()['Pages'] ??
[] );
134 $this->ipField
->getOOUI( $ipValue ) .
135 $this->pagesField
->getOOUI( $pagesValue )
140 /** @deprecated class alias since 1.42 */
141 class_alias( HTMLRestrictionsField
::class, 'HTMLRestrictionsField' );