Completed
Branch master (0c9f05)
by
unknown
29:21
created

HTMLUserTextField::shouldInfuseOOUI()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
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
 *
14
 * @since 1.26
15
 */
16
class HTMLUserTextField extends HTMLTextField {
17
	public function __construct( $params ) {
18
		$params += [
19
			'exists' => false,
20
			'ipallowed' => false,
21
		];
22
23
		parent::__construct( $params );
24
	}
25
26
	public function validate( $value, $alldata ) {
27
		// check, if a user exists with the given username
28
		$user = User::newFromName( $value, false );
29
30
		if ( !$user ) {
31
			return $this->msg( 'htmlform-user-not-valid', $value )->parse();
32
		} elseif (
33
			( $this->mParams['exists'] && $user->getId() === 0 ) &&
34
			!( $this->mParams['ipallowed'] && User::isIP( $value ) )
35
		) {
36
			return $this->msg( 'htmlform-user-not-exists', $user->getName() )->parse();
37
		}
38
39
		return parent::validate( $value, $alldata );
40
	}
41
42
	protected function getInputWidget( $params ) {
43
		$this->mParent->getOutput()->addModules( 'mediawiki.widgets.UserInputWidget' );
44
45
		return new UserInputWidget( $params );
46
	}
47
48
	protected function shouldInfuseOOUI() {
49
		return true;
50
	}
51
52
	public function getInputHtml( $value ) {
53
		// add the required module and css class for user suggestions in non-OOUI mode
54
		$this->mParent->getOutput()->addModules( 'mediawiki.userSuggest' );
55
		$this->mClass .= ' mw-autocomplete-user';
56
57
		// return parent html
58
		return parent::getInputHTML( $value );
59
	}
60
}
61