MediaWiki master
HTMLRestrictionsField.php
Go to the documentation of this file.
1<?php
2
5
18 protected const DEFAULT_ROWS = 5;
19
20 private HTMLTextAreaField $ipField;
21
26 public function __construct( array $params ) {
27 parent::__construct( $params );
28 $this->ipField = new HTMLTextAreaField( [
29 'parent' => $params['parent'],
30 'fieldname' => $params['fieldname'] . '-ip',
31 'rows' => self::DEFAULT_ROWS,
32 'required' => $params['required'] ?? false,
33 'help-message' => 'restrictionsfield-help',
34 'label-message' => 'restrictionsfield-label',
35 ] );
36 }
37
42 public function loadDataFromRequest( $request ) {
43 if ( !$request->getCheck( $this->mName . '-ip' ) ) {
44 return $this->getDefault();
45 }
46
47 $ipValue = rtrim( $request->getText( $this->mName . '-ip' ), "\r\n" );
48 $ips = $ipValue === '' ? [] : explode( "\n", $ipValue );
49 return MWRestrictions::newFromArray( [ 'IPAddresses' => $ips ] );
50 }
51
55 public function getDefault() {
56 return parent::getDefault() ?? MWRestrictions::newDefault();
57 }
58
66 public function validate( $value, $alldata ) {
67 if ( $this->isHidden( $alldata ) ) {
68 return true;
69 }
70
71 if (
72 isset( $this->mParams['required'] ) && $this->mParams['required'] !== false
73 && !$value->toArray()['IPAddresses']
74 ) {
75 return $this->msg( 'htmlform-required' );
76 }
77
78 if ( !$value->validity->isGood() ) {
79 $statusFormatter = MediaWikiServices::getInstance()->getFormatterFactory()->getStatusFormatter(
80 $this->mParent->getContext()
81 );
82 return $statusFormatter->getMessage( $value->validity );
83 }
84
85 if ( isset( $this->mValidationCallback ) ) {
86 return call_user_func( $this->mValidationCallback, $value, $alldata, $this->mParent );
87 }
88
89 return true;
90 }
91
96 public function getInputHTML( $value ) {
97 $ipValue = implode( "\n", $value->toArray()['IPAddresses'] );
98 return $this->ipField->getDiv( $ipValue );
99 }
100
106 public function getInputOOUI( $value ) {
107 $ipValue = implode( "\n", $value->toArray()['IPAddresses'] );
108 return $this->ipField->getOOUI( $ipValue )->toString();
109 }
110}
The parent class to generate form fields.
isHidden( $alldata)
Test whether this field is supposed to be hidden, based on the values of the other form fields.
msg( $key,... $params)
Get a translated interface message.
Class for updating an MWRestrictions value (which is, currently, basically just an IP address list).
Service locator for MediaWiki core services.
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...