]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - extensions/ConfirmEdit/FancyCaptcha/HTMLFancyCaptchaField.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / extensions / ConfirmEdit / FancyCaptcha / HTMLFancyCaptchaField.php
1 <?php
2
3 /**
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.
6  */
7 class HTMLFancyCaptchaField extends HTMLFormField {
8         /** @var string */
9         protected $imageUrl;
10
11         /** @var bool */
12         protected $showCreateHelp;
13
14         protected $mClass = 'captcha';
15
16         /**
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
21          */
22         public function __construct( array $params ) {
23                 parent::__construct( $params );
24                 $this->imageUrl = $params['imageUrl'];
25                 $this->showCreateHelp = !empty( $params['showCreateHelp'] );
26         }
27
28         public function getInputHTML( $value ) {
29                 global $wgEnableAPI;
30
31                 $out = $this->mParent->getOutput();
32
33                 // Uses addModuleStyles so it is loaded when JS is disabled.
34                 $out->addModuleStyles( 'ext.confirmEdit.fancyCaptcha.styles' );
35
36                 if ( $wgEnableAPI ) {
37                         // Loaded only if JS is enabled
38                         $out->addModules( 'ext.confirmEdit.fancyCaptcha' );
39
40                         $captchaReload = Html::element(
41                                 'small',
42                                 [ 'class' => 'confirmedit-captcha-reload fancycaptcha-reload' ],
43                                 $this->mParent->msg( 'fancycaptcha-reload-text' )->text()
44                         );
45                 } else {
46                         $captchaReload = '';
47                 }
48
49                 $attribs = [
50                         'type' => 'text',
51                         'id'   => $this->mID,
52                         'name' => $this->mName,
53                         'class' => 'mw-ui-input',
54                         'size' => '12',  // max_length in captcha.py plus fudge factor
55                         'dir' => $this->mDir,
56                         'autocomplete' => 'off',
57                         'autocorrect' => 'off',
58                         'autocapitalize' => 'off',
59                         'placeholder' => $this->mParent->msg( 'fancycaptcha-imgcaptcha-ph' )->text()
60                 ];
61                 $attribs += $this->getAttributes( [ 'tabindex', 'required', 'autofocus' ] );
62
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,
69                                 'alt'    => ''
70                         ] ) . $captchaReload . Html::closeElement( 'div' ) . Html::closeElement( 'div' ) . "\n"
71                         . Html::element( 'input', $attribs );
72
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() );
78                 }
79
80                 $html .= Html::closeElement( 'div' );
81
82                 return $html;
83         }
84
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();
90         }
91
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;
97                 }
98                 return $labelHtml;
99         }
100 }