Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / htmlform / fields / HTMLRestrictionsField.php
blob90e2b21878dcccc85c46f9fde89e5d5244019293
1 <?php
3 namespace MediaWiki\HTMLForm\Field;
5 use MediaWiki\HTMLForm\HTMLFormField;
6 use MediaWiki\MediaWikiServices;
7 use MediaWiki\Message\Message;
8 use MediaWiki\Request\WebRequest;
9 use MWRestrictions;
11 /**
12 * Class for updating an MWRestrictions value (which is, currently, basically just an IP address
13 * list).
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
19 * valid IP ranges.
22 class HTMLRestrictionsField extends HTMLFormField {
23 protected const DEFAULT_ROWS = 5;
25 private HTMLTextAreaField $ipField;
26 private HTMLTagMultiselectField $pagesField;
28 /**
29 * @stable to call
30 * @inheritDoc
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 ] );
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 ] );
56 /**
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 ] );
72 /**
73 * @return MWRestrictions
75 public function getDefault() {
76 return parent::getDefault() ?? MWRestrictions::newDefault();
79 /**
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 ) ) {
88 return true;
91 if (
92 isset( $this->mParams['required'] ) && $this->mParams['required'] !== false
93 && !$value->toArray()['IPAddresses']
94 ) {
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 );
109 return true;
113 * @param MWRestrictions $value
114 * @return string
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 )
126 * @param MWRestrictions $value
127 * @return string
128 * @suppress PhanParamSignatureMismatch
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 )
140 /** @deprecated class alias since 1.42 */
141 class_alias( HTMLRestrictionsField::class, 'HTMLRestrictionsField' );