MediaWiki REL1_31
HTMLUserTextField.php
Go to the documentation of this file.
1<?php
2
4
21 public function __construct( $params ) {
23 'exists' => false,
24 'ipallowed' => false,
25 'iprange' => false,
26 'iprangelimits' => [
27 'IPv4' => '16',
28 'IPv6' => '32',
29 ],
30 ]
31 );
32
33 parent::__construct( $params );
34 }
35
36 public function validate( $value, $alldata ) {
37 // check, if a user exists with the given username
38 $user = User::newFromName( $value, false );
39 $rangeError = null;
40
41 if ( !$user ) {
42 return $this->msg( 'htmlform-user-not-valid', $value );
43 } elseif (
44 // check, if the user exists, if requested
45 ( $this->mParams['exists'] && $user->getId() === 0 ) &&
46 // check, if the username is a valid IP address, otherweise save the error message
47 !( $this->mParams['ipallowed'] && IP::isValid( $value ) ) &&
48 // check, if the username is a valid IP range, otherwise save the error message
49 !( $this->mParams['iprange'] && ( $rangeError = $this->isValidIPRange( $value ) ) === true )
50 ) {
51 if ( is_string( $rangeError ) ) {
52 return $rangeError;
53 }
54 return $this->msg( 'htmlform-user-not-exists', $user->getName() );
55 }
56
57 return parent::validate( $value, $alldata );
58 }
59
60 protected function isValidIPRange( $value ) {
61 $cidrIPRanges = $this->mParams['iprangelimits'];
62
63 if ( !IP::isValidBlock( $value ) ) {
64 return false;
65 }
66
67 list( $ip, $range ) = explode( '/', $value, 2 );
68
69 if (
70 ( IP::isIPv4( $ip ) && $cidrIPRanges['IPv4'] == 32 ) ||
71 ( IP::isIPv6( $ip ) && $cidrIPRanges['IPv6'] == 128 )
72 ) {
73 // Range block effectively disabled
74 return $this->msg( 'ip_range_toolow' )->parse();
75 }
76
77 if (
78 ( IP::isIPv4( $ip ) && $range > 32 ) ||
79 ( IP::isIPv6( $ip ) && $range > 128 )
80 ) {
81 // Dodgy range
82 return $this->msg( 'ip_range_invalid' )->parse();
83 }
84
85 if ( IP::isIPv4( $ip ) && $range < $cidrIPRanges['IPv4'] ) {
86 return $this->msg( 'ip_range_exceeded', $cidrIPRanges['IPv4'] )->parse();
87 }
88
89 if ( IP::isIPv6( $ip ) && $range < $cidrIPRanges['IPv6'] ) {
90 return $this->msg( 'ip_range_exceeded', $cidrIPRanges['IPv6'] )->parse();
91 }
92
93 return true;
94 }
95
96 protected function getInputWidget( $params ) {
97 return new UserInputWidget( $params );
98 }
99
100 protected function shouldInfuseOOUI() {
101 return true;
102 }
103
104 protected function getOOUIModules() {
105 return [ 'mediawiki.widgets.UserInputWidget' ];
106 }
107
108 public function getInputHtml( $value ) {
109 // add the required module and css class for user suggestions in non-OOUI mode
110 $this->mParent->getOutput()->addModules( 'mediawiki.userSuggest' );
111 $this->mClass .= ' mw-autocomplete-user';
112
113 // return parent html
114 return parent::getInputHTML( $value );
115 }
116}
wfArrayPlus2d(array $baseArray, array $newValues)
Merges two (possibly) 2 dimensional arrays into the target array ($baseArray).
msg()
Get a translated interface message.
<input> field.
Implements a text input field for user names.
shouldInfuseOOUI()
Whether the field should be automatically infused.
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:591
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
$params