MediaWiki master
HTMLRestrictionsField.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\HTMLForm\Field;
4
5use MediaWiki\HTMLForm\HTMLFormField;
6use MediaWiki\MediaWikiServices;
7use MediaWiki\Message\Message;
8use MediaWiki\Request\WebRequest;
9use MediaWiki\Utils\MWRestrictions;
10
22 protected const DEFAULT_ROWS = 5;
23
24 private HTMLTextAreaField $ipField;
25 private HTMLTagMultiselectField $pagesField;
26
31 public function __construct( array $params ) {
32 parent::__construct( $params );
33 $this->ipField = new HTMLTextAreaField( [
34 'parent' => $params['parent'],
35 'fieldname' => $params['fieldname'] . '-ip',
36 'rows' => self::DEFAULT_ROWS,
37 'required' => $params['required'] ?? false,
38 'help-message' => 'restrictionsfield-help',
39 'label-message' => 'restrictionsfield-label',
40 ] );
41
42 // Cannot really use a TitlesMultiselect field as the pages could be
43 // on other wikis!
44 $this->pagesField = new HTMLTagMultiselectField( [
45 'parent' => $params['parent'],
46 'fieldname' => $params['fieldname'] . '-pages',
47 'label-message' => 'restrictionsfields-pages-label',
48 'help-message' => 'restrictionsfields-pages-help',
49 'allowArbitrary' => true,
50 'required' => false,
51 'max' => 25,
52 ] );
53 }
54
59 public function loadDataFromRequest( $request ) {
60 if ( !$request->getCheck( $this->mName . '-ip' ) ) {
61 return $this->getDefault();
62 }
63
64 $ipValue = rtrim( $request->getText( $this->mName . '-ip' ), "\r\n" );
65 $ips = $ipValue === '' ? [] : explode( "\n", $ipValue );
66 $pagesValue = $request->getText( $this->mName . '-pages' );
67 $pageList = $pagesValue ? explode( "\n", $pagesValue ) : [];
68 return MWRestrictions::newFromArray( [ 'IPAddresses' => $ips, 'Pages' => $pageList ] );
69 }
70
74 public function getDefault() {
75 return parent::getDefault() ?? MWRestrictions::newDefault();
76 }
77
85 public function validate( $value, $alldata ) {
86 if ( $this->isHidden( $alldata ) ) {
87 return true;
88 }
89
90 if (
91 isset( $this->mParams['required'] ) && $this->mParams['required'] !== false
92 && !$value->toArray()['IPAddresses']
93 ) {
94 return $this->msg( 'htmlform-required' );
95 }
96
97 if ( !$value->validity->isGood() ) {
98 $statusFormatter = MediaWikiServices::getInstance()->getFormatterFactory()->getStatusFormatter(
99 $this->mParent->getContext()
100 );
101 return $statusFormatter->getMessage( $value->validity );
102 }
103
104 if ( $this->mValidationCallback !== null ) {
105 return ( $this->mValidationCallback )( $value, $alldata, $this->mParent );
106 }
107
108 return true;
109 }
110
115 public function getInputHTML( $value ) {
116 $ipValue = implode( "\n", $value->toArray()['IPAddresses'] );
117 $pagesValue = implode( "\n", $value->toArray()['Pages'] ?? [] );
118 return (
119 $this->ipField->getDiv( $ipValue ) .
120 $this->pagesField->getDiv( $pagesValue )
121 );
122 }
123
129 public function getInputOOUI( $value ) {
130 $ipValue = implode( "\n", $value->toArray()['IPAddresses'] );
131 $pagesValue = implode( "\n", $value->toArray()['Pages'] ?? [] );
132 return (
133 $this->ipField->getOOUI( $ipValue ) .
134 $this->pagesField->getOOUI( $pagesValue )
135 );
136 }
137}
138
140class_alias( HTMLRestrictionsField::class, 'HTMLRestrictionsField' );
Class for updating an MWRestrictions value (which is, currently, basically just an IP address list).
Implements a tag multiselect input field for arbitrary values.
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.
static getInstance()
Returns the global default instance of the top level service locator.
static newFromArray(array $restrictions)