2015-07-17 22:04:02 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use MediaWiki\Widget\UserInputWidget;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Implements a text input field for user names.
|
|
|
|
|
* Automatically auto-completes if using the OOUI display format.
|
|
|
|
|
*
|
|
|
|
|
* FIXME: Does not work for forms that support GET requests.
|
|
|
|
|
*
|
|
|
|
|
* Optional parameters:
|
|
|
|
|
* 'exists' - Whether to validate that the user already exists
|
|
|
|
|
*
|
|
|
|
|
* @since 1.26
|
|
|
|
|
*/
|
|
|
|
|
class HTMLUserTextField extends HTMLTextField {
|
|
|
|
|
public function __construct( $params ) {
|
|
|
|
|
$params += array(
|
|
|
|
|
'exists' => false,
|
2015-07-24 16:59:53 +00:00
|
|
|
'ipallowed' => false,
|
2015-07-17 22:04:02 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
parent::__construct( $params );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function validate( $value, $alldata ) {
|
|
|
|
|
// check, if a user exists with the given username
|
2015-07-24 16:59:53 +00:00
|
|
|
$user = User::newFromName( $value, false );
|
2015-07-17 22:04:02 +00:00
|
|
|
|
|
|
|
|
if ( !$user ) {
|
|
|
|
|
return $this->msg( 'htmlform-user-not-valid', $value )->parse();
|
2015-07-24 16:59:53 +00:00
|
|
|
} elseif (
|
|
|
|
|
( $this->mParams['exists'] && $user->getId() === 0 ) &&
|
|
|
|
|
!( $this->mParams['ipallowed'] && User::isIP( $value ) )
|
|
|
|
|
) {
|
2015-07-17 22:04:02 +00:00
|
|
|
return $this->msg( 'htmlform-user-not-exists', $user->getName() )->parse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parent::validate( $value, $alldata );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getInputWidget( $params ) {
|
2015-10-05 17:50:15 +00:00
|
|
|
$this->mParent->getOutput()->addModules( 'mediawiki.widgets.UserInputWidget' );
|
2015-07-17 22:04:02 +00:00
|
|
|
|
|
|
|
|
return new UserInputWidget( $params );
|
|
|
|
|
}
|
2015-10-29 18:00:59 +00:00
|
|
|
|
|
|
|
|
public function getInputHtml( $value ) {
|
|
|
|
|
// add the required module and css class for user suggestions in non-OOUI mode
|
|
|
|
|
$this->mParent->getOutput()->addModules( 'mediawiki.userSuggest' );
|
|
|
|
|
$this->mClass .= ' mw-autocomplete-user';
|
|
|
|
|
|
|
|
|
|
// return parent html
|
|
|
|
|
return parent::getInputHtml( $value );
|
|
|
|
|
}
|
2015-07-17 22:04:02 +00:00
|
|
|
}
|