Remove Revision::getRevisionText from migrateArchiveText
[lhc/web/wiklou.git] / includes / htmlform / fields / HTMLUserTextField.php
1 <?php
2
3 use MediaWiki\Widget\UserInputWidget;
4
5 /**
6 * Implements a text input field for user names.
7 * Automatically auto-completes if using the OOUI display format.
8 *
9 * FIXME: Does not work for forms that support GET requests.
10 *
11 * Optional parameters:
12 * 'exists' - Whether to validate that the user already exists
13 * 'ipallowed' - Whether an IP adress is interpreted as "valid"
14 * 'iprange' - Whether an IP adress range is interpreted as "valid"
15 * 'iprangelimits' - Specifies the valid IP ranges for IPv4 and IPv6 in an array.
16 * defaults to IPv4 => 16; IPv6 => 32.
17 *
18 * @since 1.26
19 */
20 class HTMLUserTextField extends HTMLTextField {
21 public function __construct( $params ) {
22 $params = wfArrayPlus2d( $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 // Default value (from getDefault()) is null, User::newFromName() expects a string
38 if ( $value === null ) {
39 $value = '';
40 }
41
42 // check, if a user exists with the given username
43 $user = User::newFromName( $value, false );
44 $rangeError = null;
45
46 if ( !$user ) {
47 return $this->msg( 'htmlform-user-not-valid', $value );
48 } elseif (
49 // check, if the user exists, if requested
50 ( $this->mParams['exists'] && $user->getId() === 0 ) &&
51 // check, if the username is a valid IP address, otherwise save the error message
52 !( $this->mParams['ipallowed'] && IP::isValid( $value ) ) &&
53 // check, if the username is a valid IP range, otherwise save the error message
54 !( $this->mParams['iprange'] && ( $rangeError = $this->isValidIPRange( $value ) ) === true )
55 ) {
56 if ( is_string( $rangeError ) ) {
57 return $rangeError;
58 }
59 return $this->msg( 'htmlform-user-not-exists', $user->getName() );
60 }
61
62 return parent::validate( $value, $alldata );
63 }
64
65 protected function isValidIPRange( $value ) {
66 $cidrIPRanges = $this->mParams['iprangelimits'];
67
68 if ( !IP::isValidRange( $value ) ) {
69 return false;
70 }
71
72 list( $ip, $range ) = explode( '/', $value, 2 );
73
74 if (
75 ( IP::isIPv4( $ip ) && $cidrIPRanges['IPv4'] == 32 ) ||
76 ( IP::isIPv6( $ip ) && $cidrIPRanges['IPv6'] == 128 )
77 ) {
78 // Range block effectively disabled
79 return $this->msg( 'ip_range_toolow' )->parse();
80 }
81
82 if (
83 ( IP::isIPv4( $ip ) && $range > 32 ) ||
84 ( IP::isIPv6( $ip ) && $range > 128 )
85 ) {
86 // Dodgy range
87 return $this->msg( 'ip_range_invalid' )->parse();
88 }
89
90 if ( IP::isIPv4( $ip ) && $range < $cidrIPRanges['IPv4'] ) {
91 return $this->msg( 'ip_range_exceeded', $cidrIPRanges['IPv4'] )->parse();
92 }
93
94 if ( IP::isIPv6( $ip ) && $range < $cidrIPRanges['IPv6'] ) {
95 return $this->msg( 'ip_range_exceeded', $cidrIPRanges['IPv6'] )->parse();
96 }
97
98 return true;
99 }
100
101 protected function getInputWidget( $params ) {
102 return new UserInputWidget( $params );
103 }
104
105 protected function shouldInfuseOOUI() {
106 return true;
107 }
108
109 protected function getOOUIModules() {
110 return [ 'mediawiki.widgets.UserInputWidget' ];
111 }
112
113 public function getInputHtml( $value ) {
114 // add the required module and css class for user suggestions in non-OOUI mode
115 $this->mParent->getOutput()->addModules( 'mediawiki.userSuggest' );
116 $this->mClass .= ' mw-autocomplete-user';
117
118 // return parent html
119 return parent::getInputHTML( $value );
120 }
121 }