MediaWiki  master
HTMLUsersMultiselectField.php
Go to the documentation of this file.
1 <?php
2 
6 use 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  $params = [ 'name' => $this->mName ];
91 
92  if ( isset( $this->mParams['id'] ) ) {
93  $params['id'] = $this->mParams['id'];
94  }
95 
96  if ( isset( $this->mParams['disabled'] ) ) {
97  $params['disabled'] = $this->mParams['disabled'];
98  }
99 
100  if ( isset( $this->mParams['default'] ) ) {
101  $params['default'] = $this->mParams['default'];
102  }
103 
104  $params['placeholder'] = $this->mParams['placeholder'] ??
105  $this->msg( 'mw-widgets-usersmultiselect-placeholder' )->plain();
106 
107  if ( isset( $this->mParams['max'] ) ) {
108  $params['tagLimit'] = $this->mParams['max'];
109  }
110 
111  if ( isset( $this->mParams['ipallowed'] ) ) {
112  $params['ipAllowed'] = $this->mParams['ipallowed'];
113  }
114 
115  if ( isset( $this->mParams['iprange'] ) ) {
116  $params['ipRangeAllowed'] = $this->mParams['iprange'];
117  }
118 
119  if ( isset( $this->mParams['iprangelimits'] ) ) {
120  $params['ipRangeLimits'] = $this->mParams['iprangelimits'];
121  }
122 
123  if ( isset( $this->mParams['input'] ) ) {
124  $params['input'] = $this->mParams['input'];
125  }
126 
127  if ( $value !== null ) {
128  // $value is a string, but the widget expects an array
129  $params['default'] = $value === '' ? [] : explode( "\n", $value );
130  }
131 
132  // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
133  $params['infusable'] = true;
134  $params['classes'] = [ 'mw-htmlform-autoinfuse' ];
135  $widget = new UsersMultiselectWidget( $params );
136  $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
137 
138  return $widget;
139  }
140 
141  protected function shouldInfuseOOUI() {
142  return true;
143  }
144 
145  protected function getOOUIModules() {
146  return [ 'mediawiki.widgets.UsersMultiselectWidget' ];
147  }
148 
149 }
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 "...
Service locator for MediaWiki core services.
Shared interface for rigor levels when dealing with User methods.