Debian Codegraph @

extensions/ConfirmEdit/FancyCaptcha/includes/HTMLFancyCaptchaField.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\ConfirmEdit\FancyCaptcha;
4
5 use MediaWiki\Html\Html;
6 use MediaWiki\HTMLForm\HTMLFormField;
7
8 /**
9 * Captcha input field for FancyCaptcha that displays a question and returns the answer.
10 * Does not include the captcha ID; that must be included in the form as a separate hidden field.
11 */
12 class HTMLFancyCaptchaField extends HTMLFormField {
13 /** @var string */
14 protected $imageUrl;
15
16 /** @var bool */
17 protected $showCreateHelp;
18
19 /** @var string */
20 protected $mClass = 'captcha';
21
22 /**
23 * Apart from normal HTMLFormField parameters, recognizes the following keys:
24 * - 'imageUrl': (string, required) src of the captcha image
25 * - 'showCreateHelp': (bool) show some extra messaging that's only relevant at account creation
26 * @param array $params
27 */
28 public function __construct( array $params ) {
29 parent::__construct( $params );
30 $this->imageUrl = $params['imageUrl'];
31 $this->showCreateHelp = !empty( $params['showCreateHelp'] );
32 }
33
34 /**
35 * @inheritDoc
36 */
37 public function getInputHTML( $value ) {
38 $out = $this->mParent->getOutput();
39
40 // Uses addModuleStyles so it is loaded even when JS is disabled.
41 $out->addModuleStyles( [ 'codex-styles', 'ext.confirmEdit.fancyCaptcha.styles' ] );
42
43 // Loaded only for clients with JS enabled
44 $out->addModules( 'ext.confirmEdit.fancyCaptcha' );
45
46 $captchaReload = Html::element(
47 'small',
48 [ 'class' => 'confirmedit-captcha-reload fancycaptcha-reload' ],
49 $this->mParent->msg( 'fancycaptcha-reload-text' )->text()
50 );
51
52 $attribs = [
53 'type' => 'text',
54 'id' => $this->mID,
55 'name' => $this->mName,
56 'class' => 'cdx-text-input__input',
57 // max_length in captcha.py plus fudge factor
58 'size' => '12',
59 'dir' => $this->mDir,
60 'autocomplete' => 'off',
61 'autocorrect' => 'off',
62 'autocapitalize' => 'off',
63 'placeholder' => $this->mParent->msg( 'fancycaptcha-imgcaptcha-ph' )->text()
64 ];
65 $attribs += $this->getAttributes( [ 'tabindex', 'required', 'autofocus' ] );
66
67 $html = Html::openElement( 'div', [ 'class' => 'fancycaptcha-captcha-container' ] )
68 . Html::openElement( 'div', [ 'class' => 'fancycaptcha-captcha-and-reload' ] )
69 . Html::openElement( 'div', [ 'class' => 'fancycaptcha-image-container' ] )
70 . Html::element( 'img', [
71 'class' => 'fancycaptcha-image',
72 'src' => $this->imageUrl,
73 'alt' => ''
74 ] ) . $captchaReload . Html::closeElement( 'div' ) . Html::closeElement( 'div' ) . "\n"
75 . Html::openElement( 'div', [ 'class' => 'cdx-text-input' ] )
76 . Html::element( 'input', $attribs ) . Html::closeElement( 'div' );
77
78 if ( $this->showCreateHelp ) {
79 // use raw element, the message will contain a link
80 $html .= Html::rawElement( 'small', [
81 'class' => 'mw-createacct-captcha-assisted'
82 ], $this->mParent->msg( 'createacct-imgcaptcha-help' )->parse() );
83 }
84
85 $html .= Html::closeElement( 'div' );
86
87 return $html;
88 }
89
90 /**
91 * @inheritDoc
92 */
93 public function getLabel() {
94 // slight abuse of what getLabel() should mean; $mLabel is used for the pre-label text
95 // as the actual label is always the same
96 return $this->mParent->msg( 'captcha-label' )->escaped() . ' '
97 . $this->mParent->msg( 'fancycaptcha-captcha' )->escaped();
98 }
99
100 /**
101 * @inheritDoc
102 */
103 public function getLabelHtml( $cellAttributes = [] ) {
104 $labelHtml = parent::getLabelHtml( $cellAttributes );
105 if ( $this->mLabel ) {
106 // use raw element, the message will contain a link
107 $labelHtml = Html::rawElement( 'p', [], $this->mLabel ) . $labelHtml;
108 }
109 return $labelHtml;
110 }
111 }