3 namespace MediaWiki\HTMLForm\Field
;
5 use MediaWiki\MediaWikiServices
;
6 use MediaWiki\User\UserRigorOptions
;
7 use MediaWiki\Widget\UsersMultiselectWidget
;
11 * Implements a tag multiselect input field for user names.
13 * Besides the parameters recognized by HTMLUserTextField, additional recognized
15 * default - (optional) String, newline-separated list of usernames to use as preset data
16 * placeholder - (optional) Custom placeholder message for input
18 * The result is the array of usernames
21 * @note This widget is not likely to remain functional in non-OOUI forms.
23 class HTMLUsersMultiselectField
extends HTMLUserTextField
{
24 public function loadDataFromRequest( $request ) {
25 $value = $request->getText( $this->mName
, $this->getDefault() ??
'' );
27 $usersArray = explode( "\n", $value );
29 $usersArray = array_values( array_filter( $usersArray, static function ( $username ) {
30 return trim( $username ) !== '';
33 // Normalize usernames
34 $normalizedUsers = [];
35 $userNameUtils = MediaWikiServices
::getInstance()->getUserNameUtils();
37 foreach ( $usersArray as $user ) {
38 $canonicalUser = false;
39 if ( IPUtils
::isIPAddress( $user ) ) {
40 $parsedIPRange = IPUtils
::parseRange( $user );
41 if ( !in_array( $parsedIPRange, $listOfIps ) ) {
42 $canonicalUser = IPUtils
::sanitizeRange( $user );
43 $listOfIps[] = $parsedIPRange;
46 $canonicalUser = $userNameUtils->getCanonical(
47 $user, UserRigorOptions
::RIGOR_NONE
);
49 if ( $canonicalUser !== false ) {
50 $normalizedUsers[] = $canonicalUser;
53 // Remove any duplicate usernames
54 $uniqueUsers = array_unique( $normalizedUsers );
56 // This function is expected to return a string
57 return implode( "\n", $uniqueUsers );
60 public function validate( $value, $alldata ) {
61 if ( !$this->mParams
['exists'] ) {
65 if ( $value === null ) {
69 // $value is a string, because HTMLForm fields store their values as strings
70 $usersArray = explode( "\n", $value );
72 if ( isset( $this->mParams
['max'] ) && ( count( $usersArray ) > $this->mParams
['max'] ) ) {
73 return $this->msg( 'htmlform-multiselect-toomany', $this->mParams
['max'] );
76 foreach ( $usersArray as $username ) {
77 $result = parent
::validate( $username, $alldata );
78 if ( $result !== true ) {
86 public function getInputHTML( $value ) {
87 $this->mParent
->getOutput()->enableOOUI();
88 return $this->getInputOOUI( $value );
91 public function getInputOOUI( $value ) {
92 $this->mParent
->getOutput()->addModuleStyles( 'mediawiki.widgets.TagMultiselectWidget.styles' );
94 $params = [ 'name' => $this->mName
];
96 if ( isset( $this->mParams
['id'] ) ) {
97 $params['id'] = $this->mParams
['id'];
100 if ( isset( $this->mParams
['disabled'] ) ) {
101 $params['disabled'] = $this->mParams
['disabled'];
104 if ( isset( $this->mParams
['default'] ) ) {
105 $params['default'] = $this->mParams
['default'];
108 $params['placeholder'] = $this->mParams
['placeholder'] ??
109 $this->msg( 'mw-widgets-usersmultiselect-placeholder' )->plain();
111 if ( isset( $this->mParams
['max'] ) ) {
112 $params['tagLimit'] = $this->mParams
['max'];
115 if ( isset( $this->mParams
['ipallowed'] ) ) {
116 $params['ipAllowed'] = $this->mParams
['ipallowed'];
119 if ( isset( $this->mParams
['iprange'] ) ) {
120 $params['ipRangeAllowed'] = $this->mParams
['iprange'];
123 if ( isset( $this->mParams
['iprangelimits'] ) ) {
124 $params['ipRangeLimits'] = $this->mParams
['iprangelimits'];
127 if ( isset( $this->mParams
['excludenamed'] ) ) {
128 $params['excludeNamed'] = $this->mParams
['excludenamed'];
131 if ( isset( $this->mParams
['excludetemp'] ) ) {
132 $params['excludeTemp'] = $this->mParams
['excludetemp'];
135 if ( isset( $this->mParams
['input'] ) ) {
136 $params['input'] = $this->mParams
['input'];
139 if ( $value !== null ) {
140 // $value is a string, but the widget expects an array
141 $params['default'] = $value === '' ?
[] : explode( "\n", $value );
144 // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
145 $params['infusable'] = true;
146 $params['classes'] = [ 'mw-htmlform-autoinfuse' ];
148 return $this->getInputWidget( $params );
154 protected function getInputWidget( $params ) {
155 $widget = new UsersMultiselectWidget( $params );
156 $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
160 protected function shouldInfuseOOUI() {
164 protected function getOOUIModules() {
165 return [ 'mediawiki.widgets.UsersMultiselectWidget' ];
170 /** @deprecated class alias since 1.42 */
171 class_alias( HTMLUsersMultiselectField
::class, 'HTMLUsersMultiselectField' );