4 * Captcha input field for FancyCaptcha that displays a question and returns the answer.
5 * Does not include the captcha ID; that must be included in the form as a separate hidden field.
7 class HTMLFancyCaptchaField extends HTMLFormField {
12 protected $showCreateHelp;
14 protected $mClass = 'captcha';
17 * Apart from normal HTMLFormField parameters, recognizes the following keys:
18 * - 'imageUrl': (string, required) src of the captcha image
19 * - 'showCreateHelp': (bool) show some extra messaging that's only relevant at account creation
20 * @param array $params
22 public function __construct( array $params ) {
23 parent::__construct( $params );
24 $this->imageUrl = $params['imageUrl'];
25 $this->showCreateHelp = !empty( $params['showCreateHelp'] );
28 public function getInputHTML( $value ) {
31 $out = $this->mParent->getOutput();
33 // Uses addModuleStyles so it is loaded when JS is disabled.
34 $out->addModuleStyles( 'ext.confirmEdit.fancyCaptcha.styles' );
37 // Loaded only if JS is enabled
38 $out->addModules( 'ext.confirmEdit.fancyCaptcha' );
40 $captchaReload = Html::element(
42 [ 'class' => 'confirmedit-captcha-reload fancycaptcha-reload' ],
43 $this->mParent->msg( 'fancycaptcha-reload-text' )->text()
52 'name' => $this->mName,
53 'class' => 'mw-ui-input',
54 'size' => '12', // max_length in captcha.py plus fudge factor
56 'autocomplete' => 'off',
57 'autocorrect' => 'off',
58 'autocapitalize' => 'off',
59 'placeholder' => $this->mParent->msg( 'fancycaptcha-imgcaptcha-ph' )->text()
61 $attribs += $this->getAttributes( [ 'tabindex', 'required', 'autofocus' ] );
63 $html = Html::openElement( 'div', [ 'class' => 'fancycaptcha-captcha-container' ] )
64 . Html::openElement( 'div', [ 'class' => 'fancycaptcha-captcha-and-reload' ] )
65 . Html::openElement( 'div', [ 'class' => 'fancycaptcha-image-container' ] )
66 . Html::element( 'img', [
67 'class' => 'fancycaptcha-image',
68 'src' => $this->imageUrl,
70 ] ) . $captchaReload . Html::closeElement( 'div' ) . Html::closeElement( 'div' ) . "\n"
71 . Html::element( 'input', $attribs );
73 if ( $this->showCreateHelp ) {
74 // use raw element, the message will contain a link
75 $html .= Html::rawElement( 'small', [
76 'class' => 'mw-createacct-captcha-assisted'
77 ], $this->mParent->msg( 'createacct-imgcaptcha-help' )->parse() );
80 $html .= Html::closeElement( 'div' );
85 public function getLabel() {
86 // slight abuse of what getLabel() should mean; $mLabel is used for the pre-label text
87 // as the actual label is always the same
88 return $this->mParent->msg( 'captcha-label' )->text() . ' '
89 . $this->mParent->msg( 'fancycaptcha-captcha' )->text();
92 public function getLabelHtml( $cellAttributes = [] ) {
93 $labelHtml = parent::getLabelHtml( $cellAttributes );
94 if ( $this->mLabel ) {
95 // use raw element, the message will contain a link
96 $labelHtml = Html::rawElement( 'p', [], $this->mLabel ) . $labelHtml;