jenkins-bot has submitted this change and it was merged.
Change subject: Remove getForm() and replace by getFormInformation()
......................................................................
Remove getForm() and replace by getFormInformation()
This commit removes SimpleCaptcha::getForm() and replaces it by its more
informative
counterpart getFormInformation(), which returns an array, which provides some
more information about the form than only the html.
The information included in the array is:
* html: The HTML of the CAPTCHA form (this is the same as what you expected
from
getForm() previously)
* modules: ResourceLoader modules, if any, that should be added to the output
of the
page
* modulestyles: ResourceLoader style modules, if any, that should be added to
th
output of the page
* headitems: Head items that should be added to the output (see
OutputPage::addHeadItems)
Mostly you shouldn't need to handle the response of getFormInformation()
anymore, as there's
a new function, addFormToOutput(), which takes an instance of OutputPage as a
first parameter
and handles the response of getFormInformation for you (adds all information to
the given
OutputPage instance, if they're provided).
Bug: T141300
Depends-On: I433afd124b57526caa13a540cda48ba2b99a9bde
Change-Id: I25f344538052fc18993c43185fbd97804a7cfc81
---
M FancyCaptcha/FancyCaptcha.class.php
M MathCaptcha/MathCaptcha.class.php
M QuestyCaptcha/QuestyCaptcha.class.php
M ReCaptcha/ReCaptcha.class.php
M ReCaptchaNoCaptcha/ReCaptchaNoCaptcha.class.php
M SimpleCaptcha/Captcha.php
6 files changed, 145 insertions(+), 71 deletions(-)
Approvals:
Krinkle: Looks good to me, approved
jenkins-bot: Verified
diff --git a/FancyCaptcha/FancyCaptcha.class.php
b/FancyCaptcha/FancyCaptcha.class.php
index 2711e04..ec5d503 100644
--- a/FancyCaptcha/FancyCaptcha.class.php
+++ b/FancyCaptcha/FancyCaptcha.class.php
@@ -99,15 +99,9 @@
];
}
- /**
- * Insert the captcha prompt into the edit form.
- * @param OutputPage $out
- */
- function getForm( OutputPage $out, $tabIndex = 1 ) {
+ function getFormInformation( $tabIndex = 1 ) {
global $wgEnableAPI;
-
- // Uses addModuleStyles so it is loaded when JS is disabled.
- $out->addModuleStyles( 'ext.confirmEdit.fancyCaptcha.styles' );
+ $modules = [];
$title = SpecialPage::getTitleFor( 'Captcha', 'image' );
$info = $this->getCaptcha();
@@ -115,7 +109,7 @@
if ( $wgEnableAPI ) {
// Loaded only if JS is enabled
- $out->addModules( 'ext.confirmEdit.fancyCaptcha' );
+ $modules[] = 'ext.confirmEdit.fancyCaptcha';
$captchaReload = Html::element(
'small',
@@ -172,7 +166,12 @@
]
) . Html::closeElement( 'div' ) . Html::closeElement(
'div' ) . "\n";
- return $form;
+ return [
+ 'html' => $form,
+ 'modules' => $modules,
+ // Uses addModuleStyles so it is loaded when JS is
disabled.
+ 'modulestyles' => [
'ext.confirmEdit.fancyCaptcha.styles' ],
+ ];
}
/**
diff --git a/MathCaptcha/MathCaptcha.class.php
b/MathCaptcha/MathCaptcha.class.php
index 52b2f4c..098f190 100644
--- a/MathCaptcha/MathCaptcha.class.php
+++ b/MathCaptcha/MathCaptcha.class.php
@@ -25,11 +25,7 @@
];
}
- /**
- * Produce a nice little form
- * @param OutputPage $out
- */
- function getForm( OutputPage $out, $tabIndex = 1 ) {
+ function getFormInformation( $tabIndex = 1 ) {
list( $sum, $answer ) = $this->pickSum();
$index = $this->storeCaptcha( [ 'answer' => $answer ] );
@@ -40,7 +36,7 @@
'required'
] ) . '</td></tr></table>';
$form .= Html::hidden( 'wpCaptchaId', $index );
- return $form;
+ return [ 'html' => $form ];
}
/** Pick a random sum */
diff --git a/QuestyCaptcha/QuestyCaptcha.class.php
b/QuestyCaptcha/QuestyCaptcha.class.php
index 314e15a..dfe9fd6 100644
--- a/QuestyCaptcha/QuestyCaptcha.class.php
+++ b/QuestyCaptcha/QuestyCaptcha.class.php
@@ -50,7 +50,7 @@
return [ 'question' => $question, 'answer' => $answer ];
}
- function getForm( OutputPage $out, $tabIndex = 1 ) {
+ function getFormInformation( $tabIndex = 1 ) {
$captcha = $this->getCaptcha();
if ( !$captcha ) {
die(
@@ -58,20 +58,22 @@
);
}
$index = $this->storeCaptcha( $captcha );
- return "<p><label
for=\"wpCaptchaWord\">{$captcha['question']}</label> " .
- Html::element( 'input', [
- 'name' => 'wpCaptchaWord',
- 'id' => 'wpCaptchaWord',
- 'class' => 'mw-ui-input',
- 'required',
- 'autocomplete' => 'off',
- 'tabindex' => $tabIndex ] ) . // tab in before
the edit textarea
- "</p>\n" .
- Xml::element( 'input', [
- 'type' => 'hidden',
- 'name' => 'wpCaptchaId',
- 'id' => 'wpCaptchaId',
- 'value' => $index ] );
+ return [
+ 'html' => "<p><label
for=\"wpCaptchaWord\">{$captcha['question']}</label> " .
+ Html::element( 'input', [
+ 'name' => 'wpCaptchaWord',
+ 'id' => 'wpCaptchaWord',
+ 'class' => 'mw-ui-input',
+ 'required',
+ 'autocomplete' => 'off',
+ 'tabindex' => $tabIndex ] ) . // tab in
before the edit textarea
+ "</p>\n" .
+ Xml::element( 'input', [
+ 'type' => 'hidden',
+ 'name' => 'wpCaptchaId',
+ 'id' => 'wpCaptchaId',
+ 'value' => $index ] )
+ ];
}
function showHelp() {
diff --git a/ReCaptcha/ReCaptcha.class.php b/ReCaptcha/ReCaptcha.class.php
index e92791a..123f643 100644
--- a/ReCaptcha/ReCaptcha.class.php
+++ b/ReCaptcha/ReCaptcha.class.php
@@ -13,9 +13,8 @@
/**
* Displays the reCAPTCHA widget.
* If $this->recaptcha_error is set, it will display an error in the
widget.
- * @param OutputPage $out
*/
- function getForm( OutputPage $out, $tabIndex = 1 ) {
+ function getFormInformation( $tabIndex = 1 ) {
global $wgReCaptchaPublicKey, $wgReCaptchaTheme;
$useHttps = ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS']
== 'on' );
@@ -23,8 +22,10 @@
[ 'theme' => $wgReCaptchaTheme, 'tabindex' => $tabIndex
]
);
- return Html::inlineScript( $js ) .
- recaptcha_get_html( $wgReCaptchaPublicKey,
$this->recaptcha_error, $useHttps );
+ return [
+ 'html' => Html::inlineScript( $js ) .
+ recaptcha_get_html( $wgReCaptchaPublicKey,
$this->recaptcha_error, $useHttps )
+ ];
}
protected function getCaptchaParamsFromRequest( WebRequest $request ) {
diff --git a/ReCaptchaNoCaptcha/ReCaptchaNoCaptcha.class.php
b/ReCaptchaNoCaptcha/ReCaptchaNoCaptcha.class.php
index 1d9130b..2d8efcb 100644
--- a/ReCaptchaNoCaptcha/ReCaptchaNoCaptcha.class.php
+++ b/ReCaptchaNoCaptcha/ReCaptchaNoCaptcha.class.php
@@ -10,19 +10,12 @@
private $error = null;
/**
* Get the captcha form.
- * @return string
+ * @return array
*/
- function getForm( OutputPage $out, $tabIndex = 1 ) {
- global $wgReCaptchaSiteKey;
- $lang = htmlspecialchars( urlencode(
$out->getLanguage()->getCode() ) );
+ function getFormInformation( $tabIndex = 1 ) {
+ global $wgReCaptchaSiteKey, $wgLang;
+ $lang = htmlspecialchars( urlencode( $wgLang->getCode() ) );
- // Insert reCAPTCHA script, in display language, if available.
- // Language falls back to the browser's display language.
- // See https://developers.google.com/recaptcha/docs/faq
- $out->addHeadItem(
- 'g-recaptchascript',
- "<script
src=\"https://www.google.com/recaptcha/api.js?hl={$lang}\" async
defer></script>"
- );
$output = Html::element( 'div', [
'class' => [
'g-recaptcha',
@@ -54,7 +47,15 @@
</div>
</noscript>
HTML;
- return $output;
+ return [
+ 'html' => $output,
+ 'headitems' => [
+ // Insert reCAPTCHA script, in display
language, if available.
+ // Language falls back to the browser's display
language.
+ // See
https://developers.google.com/recaptcha/docs/faq
+ "<script
src=\"https://www.google.com/recaptcha/api.js?hl={$lang}\" async
defer></script>"
+ ]
+ ];
}
protected function logCheckError( $info ) {
diff --git a/SimpleCaptcha/Captcha.php b/SimpleCaptcha/Captcha.php
index 28debde..a4e0891 100644
--- a/SimpleCaptcha/Captcha.php
+++ b/SimpleCaptcha/Captcha.php
@@ -3,6 +3,9 @@
use MediaWiki\Auth\AuthenticationRequest;
use MediaWiki\Logger\LoggerFactory;
+/**
+ * Demo CAPTCHA (not for production usage) and base class for real CAPTCHAs
+ */
class SimpleCaptcha {
protected static $messagePrefix = 'captcha-';
@@ -84,26 +87,84 @@
*
* Override this!
*
- * @return string HTML
+ * It is not guaranteed that the CAPTCHA will load synchronously with
the main page
+ * content. So you can not rely on registering handlers before page
load. E.g.:
+ *
+ * NOT SAFE: $( window ).on( 'load', handler )
+ * SAFE: $( handler )
+ *
+ * However, if the HTML is loaded dynamically via AJAX, the following
order will
+ * be used.
+ *
+ * headitems => modulestyles + modules => add main HTML to DOM when
modulestyles +
+ * modules are ready.
+ *
+ * @param int $tabIndex Tab index to start from
+ *
+ * @return array Associative array with the following keys:
+ * string html - Main HTML
+ * array modules (optional) - Array of ResourceLoader module names
+ * array modulestyles (optional) - Array of ResourceLoader module
names to be
+ * included as style-only modules.
+ * array headitems (optional) - Head items (see
OutputPage::addHeadItems), as a numeric array
+ * of raw HTML strings. Do not use unless no other option
is feasible.
*/
- function getForm( OutputPage $out, $tabIndex = 1 ) {
+ public function getFormInformation( $tabIndex = 1 ) {
$captcha = $this->getCaptcha();
$index = $this->storeCaptcha( $captcha );
- return "<p><label for=\"wpCaptchaWord\">{$captcha['question']}
= </label>" .
- Xml::element( 'input', [
- 'name' => 'wpCaptchaWord',
- 'class' => 'mw-ui-input',
- 'id' => 'wpCaptchaWord',
- 'size' => 5,
- 'autocomplete' => 'off',
- 'tabindex' => $tabIndex ] ) . // tab in before
the edit textarea
- "</p>\n" .
- Xml::element( 'input', [
- 'type' => 'hidden',
- 'name' => 'wpCaptchaId',
- 'id' => 'wpCaptchaId',
- 'value' => $index ] );
+ return [
+ 'html' => "<p><label
for=\"wpCaptchaWord\">{$captcha['question']} = </label>" .
+ Xml::element( 'input', [
+ 'name' => 'wpCaptchaWord',
+ 'class' => 'mw-ui-input',
+ 'id' => 'wpCaptchaWord',
+ 'size' => 5,
+ 'autocomplete' => 'off',
+ 'tabindex' => $tabIndex ] ) . // tab in
before the edit textarea
+ "</p>\n" .
+ Xml::element( 'input', [
+ 'type' => 'hidden',
+ 'name' => 'wpCaptchaId',
+ 'id' => 'wpCaptchaId',
+ 'value' => $index ] )
+ ];
+ }
+
+ /**
+ * Uses getFormInformation() to get the CAPTCHA form and adds it to the
given
+ * OutputPage object.
+ *
+ * @param OutputPage $out The OutputPage object to which the form
should be added
+ * @param integer $tabIndex See self::getFormInformation
+ */
+ public function addFormToOutput( OutputPage $out, $tabIndex = 1 ) {
+ $this->addFormInformationToOutput( $out,
$this->getFormInformation( $tabIndex ) );
+ }
+
+ /**
+ * Processes the given $formInformation array and adds the options (see
getFormInformation())
+ * to the given OutputPage object.
+ *
+ * @param OutputPage $out The OutputPage object to which the form
should be added
+ * @param array $formInformation
+ */
+ public function addFormInformationToOutput( OutputPage $out, array
$formInformation ) {
+ if ( !$formInformation ) {
+ return;
+ }
+ if ( isset( $formInformation['html'] ) ) {
+ $out->addHTML( $formInformation['html'] );
+ }
+ if ( isset( $formInformation['modules'] ) ) {
+ $out->addModules( $formInformation['modules'] );
+ }
+ if ( isset( $formInformation['modulestyles'] ) ) {
+ $out->addModuleStyles( $formInformation['modulestyles']
);
+ }
+ if ( isset( $formInformation['headitems'] ) ) {
+ $out->addHeadItems( $formInformation['headitems'] );
+ }
}
/**
@@ -129,7 +190,7 @@
if ( $this->action !== 'edit' ) {
unset( $page->ConfirmEdit_ActivateCaptcha );
$out->addWikiText( $this->getMessage( $this->action
)->text() );
- $out->addHTML( $this->getForm( $out ) );
+ $this->addFormToOutput( $out );
}
}
@@ -145,7 +206,7 @@
$this->shouldCheck( $page, '', '', $context )
) {
$out->addWikiText( $this->getMessage( $this->action
)->text() );
- $out->addHTML( $this->getForm( $out ) );
+ $this->addFormToOutput( $out );
}
unset( $page->ConfirmEdit_ActivateCaptcha );
}
@@ -174,17 +235,23 @@
* @return bool true to keep running callbacks
*/
function injectEmailUser( &$form ) {
- global $wgCaptchaTriggers, $wgOut, $wgUser;
+ global $wgCaptchaTriggers;
+ $out = $form->getOutput();
+ $user = $form->getUser();
if ( $wgCaptchaTriggers['sendemail'] ) {
$this->action = 'sendemail';
- if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
+ if ( $user->isAllowed( 'skipcaptcha' ) ) {
wfDebug( "ConfirmEdit: user group allows
skipping captcha on email sending\n" );
return true;
}
+ $formInformation = $this->getFormInformation();
+ $formMetainfo = $formInformation;
+ unset( $formMetainfo['html'] );
+ $this->addFormInformationToOutput( $out, $formMetainfo
);
$form->addFooterText(
"<div class='captcha'>" .
- $wgOut->parse( $this->getMessage( 'sendemail'
)->text() ) .
- $this->getForm( $wgOut ) .
+ $out->parse( $this->getMessage( 'sendemail'
)->text() ) .
+ $formInformation['html'] .
"</div>\n" );
}
return true;
@@ -209,6 +276,10 @@
'event' => 'captcha.display',
'type' => 'accountcreation',
] );
+ $formInformation = $this->getFormInformation( 8 );
+ $formMetainfo = $formInformation;
+ unset( $formMetainfo['html'] );
+ $this->addFormInformationToOutput( $wgOut,
$formMetainfo );
$captcha = "<div class='captcha'>" .
$wgOut->parse( $this->getMessage(
'createaccount' )->text() ) .
// FIXME: Hardcoded tab index
@@ -217,7 +288,7 @@
// there may are wikis which allows to mention
the "real name",
// which would have 7 as tabIndex, so increase
// 6 by 2 and use it for the CAPTCHA -> 8 (the
submit button has a tabIndex of 10)
- $this->getForm( $wgOut, 8 ) .
+ $formInformation['html'] .
"</div>\n";
// for older MediaWiki versions
if ( is_callable( [ $template, 'extend' ] ) ) {
@@ -258,9 +329,13 @@
'perUser' => $perUserTriggered
] );
$this->action = 'badlogin';
+ $formInformation = $this->getFormInformation();
+ $formMetainfo = $formInformation;
+ unset( $formMetainfo['html'] );
+ $this->addFormInformationToOutput( $wgOut,
$formMetainfo );
$captcha = "<div class='captcha'>" .
$wgOut->parse( $this->getMessage( 'badlogin'
)->text() ) .
- $this->getForm( $wgOut ) .
+ $formInformation['html'] .
"</div>\n";
// for older MediaWiki versions
if ( is_callable( [ $template, 'extend' ] ) ) {
--
To view, visit https://gerrit.wikimedia.org/r/301147
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I25f344538052fc18993c43185fbd97804a7cfc81
Gerrit-PatchSet: 8
Gerrit-Project: mediawiki/extensions/ConfirmEdit
Gerrit-Branch: master
Gerrit-Owner: Florianschmidtwelzow <[email protected]>
Gerrit-Reviewer: Florianschmidtwelzow <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: Mattflaschen <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits