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