MediaWiki master
HTMLUsersMultiselectField.php
Go to the documentation of this file.
1<?php
2
4
8use Wikimedia\IPUtils;
9
25 public function loadDataFromRequest( $request ) {
26 $value = $request->getText( $this->mName, $this->getDefault() ?? '' );
27
28 $usersArray = explode( "\n", $value );
29 // Remove empty lines
30 $usersArray = array_values( array_filter( $usersArray, static function ( $username ) {
31 return trim( $username ) !== '';
32 } ) );
33
34 // Normalize usernames
35 $normalizedUsers = [];
36 $userNameUtils = MediaWikiServices::getInstance()->getUserNameUtils();
37 $listOfIps = [];
38 foreach ( $usersArray as $user ) {
39 $canonicalUser = false;
40 if ( IPUtils::isIPAddress( $user ) ) {
41 $parsedIPRange = IPUtils::parseRange( $user );
42 if ( !in_array( $parsedIPRange, $listOfIps ) ) {
43 $canonicalUser = IPUtils::sanitizeRange( $user );
44 $listOfIps[] = $parsedIPRange;
45 }
46 } else {
47 $canonicalUser = $userNameUtils->getCanonical(
48 $user, UserRigorOptions::RIGOR_NONE );
49 }
50 if ( $canonicalUser !== false ) {
51 $normalizedUsers[] = $canonicalUser;
52 }
53 }
54 // Remove any duplicate usernames
55 $uniqueUsers = array_unique( $normalizedUsers );
56
57 // This function is expected to return a string
58 return implode( "\n", $uniqueUsers );
59 }
60
62 public function validate( $value, $alldata ) {
63 if ( !$this->mParams['exists'] ) {
64 return true;
65 }
66
67 if ( $value === null ) {
68 return false;
69 }
70
71 // $value is a string, because HTMLForm fields store their values as strings
72 $usersArray = explode( "\n", $value );
73
74 if ( isset( $this->mParams['max'] ) && ( count( $usersArray ) > $this->mParams['max'] ) ) {
75 return $this->msg( 'htmlform-multiselect-toomany', $this->mParams['max'] );
76 }
77
78 foreach ( $usersArray as $username ) {
79 $result = parent::validate( $username, $alldata );
80 if ( $result !== true ) {
81 return $result;
82 }
83 }
84
85 return true;
86 }
87
89 public function getInputHTML( $value ) {
90 $this->mParent->getOutput()->enableOOUI();
91 return $this->getInputOOUI( $value );
92 }
93
95 public function getInputOOUI( $value ) {
96 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.TagMultiselectWidget.styles' );
97
98 $params = [ 'name' => $this->mName ];
99
100 if ( isset( $this->mParams['id'] ) ) {
101 $params['id'] = $this->mParams['id'];
102 }
103
104 if ( isset( $this->mParams['disabled'] ) ) {
105 $params['disabled'] = $this->mParams['disabled'];
106 }
107
108 if ( isset( $this->mParams['default'] ) ) {
109 $params['default'] = $this->mParams['default'];
110 }
111
112 $params['placeholder'] = $this->mParams['placeholder'] ??
113 $this->msg( 'mw-widgets-usersmultiselect-placeholder' )->plain();
114
115 if ( isset( $this->mParams['max'] ) ) {
116 $params['tagLimit'] = $this->mParams['max'];
117 }
118
119 if ( isset( $this->mParams['ipallowed'] ) ) {
120 $params['ipAllowed'] = $this->mParams['ipallowed'];
121 }
122
123 if ( isset( $this->mParams['iprange'] ) ) {
124 $params['ipRangeAllowed'] = $this->mParams['iprange'];
125 }
126
127 if ( isset( $this->mParams['iprangelimits'] ) ) {
128 $params['ipRangeLimits'] = $this->mParams['iprangelimits'];
129 }
130
131 if ( isset( $this->mParams['excludenamed'] ) ) {
132 $params['excludeNamed'] = $this->mParams['excludenamed'];
133 }
134
135 if ( isset( $this->mParams['excludetemp'] ) ) {
136 $params['excludeTemp'] = $this->mParams['excludetemp'];
137 }
138
139 if ( isset( $this->mParams['input'] ) ) {
140 $params['input'] = $this->mParams['input'];
141 }
142
143 if ( $value !== null ) {
144 // $value is a string, but the widget expects an array
145 $params['default'] = $value === '' ? [] : explode( "\n", $value );
146 }
147
148 // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
149 $params['infusable'] = true;
150 $params['classes'] = [ 'mw-htmlform-autoinfuse' ];
151
152 return $this->getInputWidget( $params );
153 }
154
158 protected function getInputWidget( $params ) {
159 $widget = new UsersMultiselectWidget( $params );
160 $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
161 return $widget;
162 }
163
165 protected function shouldInfuseOOUI() {
166 return true;
167 }
168
170 protected function getOOUIModules() {
171 return [ 'mediawiki.widgets.UsersMultiselectWidget' ];
172 }
173
174}
175
177class_alias( HTMLUsersMultiselectField::class, 'HTMLUsersMultiselectField' );
Implements a text input field for user names.
Implements a tag multiselect input field for user names.
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.Defaults to false, which getOOUI will interpret as "...
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.Don't forget to call pare...
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
loadDataFromRequest( $request)
Get the value that this input has been set to from a posted form, or the input's default value if it ...
shouldInfuseOOUI()
Whether the field should be automatically infused.Note that all OOUI HTMLForm fields are infusable (y...
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
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.
Shared interface for rigor levels when dealing with User methods.