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 $params['disabled'] = $this->isDisabledNoJs( $this->mParent->mFieldData );
105
106 if ( isset( $this->mParams['default'] ) ) {
107 $params['default'] = $this->mParams['default'];
108 }
109
110 $params['placeholder'] = $this->mParams['placeholder'] ??
111 $this->msg( 'mw-widgets-usersmultiselect-placeholder' )->plain();
112
113 if ( isset( $this->mParams['max'] ) ) {
114 $params['tagLimit'] = $this->mParams['max'];
115 }
116
117 if ( isset( $this->mParams['ipallowed'] ) ) {
118 $params['ipAllowed'] = $this->mParams['ipallowed'];
119 }
120
121 if ( isset( $this->mParams['iprange'] ) ) {
122 $params['ipRangeAllowed'] = $this->mParams['iprange'];
123 }
124
125 if ( isset( $this->mParams['iprangelimits'] ) ) {
126 $params['ipRangeLimits'] = $this->mParams['iprangelimits'];
127 }
128
129 if ( isset( $this->mParams['excludenamed'] ) ) {
130 $params['excludeNamed'] = $this->mParams['excludenamed'];
131 }
132
133 if ( isset( $this->mParams['excludetemp'] ) ) {
134 $params['excludeTemp'] = $this->mParams['excludetemp'];
135 }
136
137 if ( isset( $this->mParams['input'] ) ) {
138 $params['input'] = $this->mParams['input'];
139 }
140
141 if ( $value !== null ) {
142 // $value is a string, but the widget expects an array
143 $params['default'] = $value === '' ? [] : explode( "\n", $value );
144 }
145
146 // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
147 $params['infusable'] = true;
148 $params['classes'] = [ 'mw-htmlform-autoinfuse' ];
149
150 return $this->getInputWidget( $params );
151 }
152
156 protected function getInputWidget( $params ) {
157 $widget = new UsersMultiselectWidget( $params );
158 $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
159 return $widget;
160 }
161
163 protected function shouldInfuseOOUI() {
164 return true;
165 }
166
168 protected function getOOUIModules() {
169 return [ 'mediawiki.widgets.UsersMultiselectWidget' ];
170 }
171
172}
173
175class_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.
isDisabledNoJs( $alldata)
Test whether this field is disabled in the generated HTML, either due to the 'disabled' parameter,...
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.