MediaWiki REL1_40
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 $canonicalUser = false;
37 if ( IPUtils::isIPAddress( $user ) ) {
38 $parsedIPRange = IPUtils::parseRange( $user );
39 if ( !in_array( $parsedIPRange, $listOfIps ) ) {
40 $canonicalUser = IPUtils::sanitizeRange( $user );
41 $listOfIps[] = $parsedIPRange;
42 }
43 } else {
44 $canonicalUser = $userNameUtils->getCanonical(
45 $user, UserRigorOptions::RIGOR_NONE );
46 }
47 if ( $canonicalUser !== false ) {
48 $normalizedUsers[] = $canonicalUser;
49 }
50 }
51 // Remove any duplicate usernames
52 $uniqueUsers = array_unique( $normalizedUsers );
53
54 // This function is expected to return a string
55 return implode( "\n", $uniqueUsers );
56 }
57
58 public function validate( $value, $alldata ) {
59 if ( !$this->mParams['exists'] ) {
60 return true;
61 }
62
63 if ( $value === null ) {
64 return false;
65 }
66
67 // $value is a string, because HTMLForm fields store their values as strings
68 $usersArray = explode( "\n", $value );
69
70 if ( isset( $this->mParams['max'] ) && ( count( $usersArray ) > $this->mParams['max'] ) ) {
71 return $this->msg( 'htmlform-multiselect-toomany', $this->mParams['max'] );
72 }
73
74 foreach ( $usersArray as $username ) {
75 $result = parent::validate( $username, $alldata );
76 if ( $result !== true ) {
77 return $result;
78 }
79 }
80
81 return true;
82 }
83
84 public function getInputHTML( $value ) {
85 $this->mParent->getOutput()->enableOOUI();
86 return $this->getInputOOUI( $value );
87 }
88
89 public function getInputOOUI( $value ) {
90 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.TagMultiselectWidget.styles' );
91
92 $params = [ 'name' => $this->mName ];
93
94 if ( isset( $this->mParams['id'] ) ) {
95 $params['id'] = $this->mParams['id'];
96 }
97
98 if ( isset( $this->mParams['disabled'] ) ) {
99 $params['disabled'] = $this->mParams['disabled'];
100 }
101
102 if ( isset( $this->mParams['default'] ) ) {
103 $params['default'] = $this->mParams['default'];
104 }
105
106 $params['placeholder'] = $this->mParams['placeholder'] ??
107 $this->msg( 'mw-widgets-usersmultiselect-placeholder' )->plain();
108
109 if ( isset( $this->mParams['max'] ) ) {
110 $params['tagLimit'] = $this->mParams['max'];
111 }
112
113 if ( isset( $this->mParams['ipallowed'] ) ) {
114 $params['ipAllowed'] = $this->mParams['ipallowed'];
115 }
116
117 if ( isset( $this->mParams['iprange'] ) ) {
118 $params['ipRangeAllowed'] = $this->mParams['iprange'];
119 }
120
121 if ( isset( $this->mParams['iprangelimits'] ) ) {
122 $params['ipRangeLimits'] = $this->mParams['iprangelimits'];
123 }
124
125 if ( isset( $this->mParams['input'] ) ) {
126 $params['input'] = $this->mParams['input'];
127 }
128
129 if ( $value !== null ) {
130 // $value is a string, but the widget expects an array
131 $params['default'] = $value === '' ? [] : explode( "\n", $value );
132 }
133
134 // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
135 $params['infusable'] = true;
136 $params['classes'] = [ 'mw-htmlform-autoinfuse' ];
137
138 return $this->getInputWidget( $params );
139 }
140
144 protected function getInputWidget( $params ) {
145 $widget = new UsersMultiselectWidget( $params );
146 $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
147 return $widget;
148 }
149
150 protected function shouldInfuseOOUI() {
151 return true;
152 }
153
154 protected function getOOUIModules() {
155 return [ 'mediawiki.widgets.UsersMultiselectWidget' ];
156 }
157
158}
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 "...
getInputWidget( $params)
to overrideWidget
Service locator for MediaWiki core services.
Shared interface for rigor levels when dealing with User methods.