MediaWiki master
HTMLRestrictionsField.php
Go to the documentation of this file.
1<?php
2
4
10
23 protected const DEFAULT_ROWS = 5;
24
25 private HTMLTextAreaField $ipField;
26 private HTMLTagMultiselectField $pagesField;
27
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',
41 ] );
42
43 // Cannot really use a TitlesMultiselect field as the pages could be
44 // on other wikis!
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,
51 'required' => false,
52 'max' => 25,
53 ] );
54 }
55
60 public function loadDataFromRequest( $request ) {
61 if ( !$request->getCheck( $this->mName . '-ip' ) ) {
62 return $this->getDefault();
63 }
64
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 ] );
70 }
71
75 public function getDefault() {
76 return parent::getDefault() ?? MWRestrictions::newDefault();
77 }
78
86 public function validate( $value, $alldata ) {
87 if ( $this->isHidden( $alldata ) ) {
88 return true;
89 }
90
91 if (
92 isset( $this->mParams['required'] ) && $this->mParams['required'] !== false
93 && !$value->toArray()['IPAddresses']
94 ) {
95 return $this->msg( 'htmlform-required' );
96 }
97
98 if ( !$value->validity->isGood() ) {
99 $statusFormatter = MediaWikiServices::getInstance()->getFormatterFactory()->getStatusFormatter(
100 $this->mParent->getContext()
101 );
102 return $statusFormatter->getMessage( $value->validity );
103 }
104
105 if ( isset( $this->mValidationCallback ) ) {
106 return call_user_func( $this->mValidationCallback, $value, $alldata, $this->mParent );
107 }
108
109 return true;
110 }
111
116 public function getInputHTML( $value ) {
117 $ipValue = implode( "\n", $value->toArray()['IPAddresses'] );
118 $pagesValue = implode( "\n", $value->toArray()['Pages'] ?? [] );
119 return (
120 $this->ipField->getDiv( $ipValue ) .
121 $this->pagesField->getDiv( $pagesValue )
122 );
123 }
124
130 public function getInputOOUI( $value ) {
131 $ipValue = implode( "\n", $value->toArray()['IPAddresses'] );
132 $pagesValue = implode( "\n", $value->toArray()['Pages'] ?? [] );
133 return (
134 $this->ipField->getOOUI( $ipValue ) .
135 $this->pagesField->getOOUI( $pagesValue )
136 );
137 }
138}
139
141class_alias( HTMLRestrictionsField::class, 'HTMLRestrictionsField' );
array $params
The job parameters.
A class to check request restrictions expressed as a JSON object.
static newFromArray(array $restrictions)
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.
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:155
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form,...