Florianschmidtwelzow has uploaded a new change for review. https://gerrit.wikimedia.org/r/301147
Change subject: Deprecate getForm() and replace by getFormInformation() ...................................................................... Deprecate getForm() and replace by getFormInformation() This commit deprecates SimpleCaptcha::getForm() and replaces it by it's more informative counterpart getFormInformation(), which returns an array, which provides some more information about the form as only the html. The information included in the array are: * html: The HTML of the CAPTCHA form (this is the same as what you expected from getForm() so far) * 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). getForm() will still return the html of the form but also handles the other information of getFormInformation(). 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, 135 insertions(+), 75 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ConfirmEdit refs/changes/47/301147/1 diff --git a/FancyCaptcha/FancyCaptcha.class.php b/FancyCaptcha/FancyCaptcha.class.php index 2711e04..0206056 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..1d9c42c 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( OutputPage $out, $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..cf1fa62 100644 --- a/ReCaptcha/ReCaptcha.class.php +++ b/ReCaptcha/ReCaptcha.class.php @@ -10,12 +10,7 @@ // reCAPTHCA error code returned from recaptcha_check_answer private $recaptcha_error = null; - /** - * 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 +18,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..fc676dd 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, $wgLanguageCode; + $lang = htmlspecialchars( urlencode( $wgLanguageCode ) ); - // 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..2817ac8 100644 --- a/SimpleCaptcha/Captcha.php +++ b/SimpleCaptcha/Captcha.php @@ -78,32 +78,83 @@ } /** + * Like getFormInformation() with the difference, that it adds the meta information + * returned from getFormInformation() directly to the passed OutputPage object. + * + * @deprecated You should use addFormToOutput() or getFormInformation() + * + * @param OutputPage $out + * @param $tabIndex + * @return mixed + */ + public function getForm( OutputPage $out, $tabIndex ) { + wfDeprecated( __METHOD__ ); + $formInformation = $this->getFormInformation( $tabIndex ); + $formMetainfo = $formInformation; + unset( $formMetainfo['html'] ); + $this->addFormToOutput( $out, $formMetainfo ); + return $formInformation['html']; + } + + /** * Insert a captcha prompt into the edit form. * This sample implementation generates a simple arithmetic operation; * it would be easy to defeat by machine. * * Override this! * - * @return string HTML + * @patam integer $tabIndex The tab index to use for the form input + * @return array An array of information of the form. Possible values: + * - html: The HTML of the form + * - modules: ResourceLoader modules to add to the output + * - moduleStyles: ResourceLoader style modules to add to the output + * - headItems: An array of head items to add to the output (see OutputPage::addHeadItems()) */ - 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 ] ) + ]; + } + + /** + * Adds the CAPTCHA form to the given output object. If $formInformation is provided, this + * array will be used instead of the response of self::getForm() + * @param OutputPage $out The OutputPage object to which the form should be added + * @param array $formInformation + * @param integer $tabIndex See self::getForm + */ + public function addFormToOutput( OutputPage $out, $formInformation = [], $tabIndex = 1 ) { + if ( !is_array( $formInformation ) || !$formInformation ) { + $formInformation = $this->getFormInformation( $tabIndex ); + } + 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 +180,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 +196,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 +225,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->addFormToOutput( $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 +266,10 @@ 'event' => 'captcha.display', 'type' => 'accountcreation', ] ); + $formInformation = $this->getFormInformation( 8 ); + $formMetainfo = $formInformation; + unset( $formMetainfo['html'] ); + $this->addFormToOutput( $wgOut, $formMetainfo ); $captcha = "<div class='captcha'>" . $wgOut->parse( $this->getMessage( 'createaccount' )->text() ) . // FIXME: Hardcoded tab index @@ -217,7 +278,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 +319,13 @@ 'perUser' => $perUserTriggered ] ); $this->action = 'badlogin'; + $formInformation = $this->getFormInformation(); + $formMetainfo = $formInformation; + unset( $formMetainfo['html'] ); + $this->addFormToOutput( $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: newchange Gerrit-Change-Id: I25f344538052fc18993c43185fbd97804a7cfc81 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/ConfirmEdit Gerrit-Branch: master Gerrit-Owner: Florianschmidtwelzow <florian.schmidt.stargatewis...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits