From 6f78a3ac576bde59bdd60dc91a527f999d55e007 Mon Sep 17 00:00:00 2001 From: Kosta Harlan Date: Tue, 9 Jul 2024 09:29:18 +0200 Subject: [PATCH] SimpleCaptcha: Show captcha-edit message if forceShowCaptcha is set Why: - forceShowCaptcha is set if another extension wants to ensure that a CAPTCHA is presented to the user. In this case, the 'captcha-edit' message is more appropriate than 'captcha-edit-fail' as 'captcha-edit-fail' implies that the user already tried and failed to solve the CAPTCHA. What: - Use 'captcha-edit' message on confirm edit hook failure if 'shouldForceShowCaptcha' is set - ... but use 'captcha-edit-fail' if the user submitted an attempt. Note that the updated message only works in the source editor, not in VisualEditor or DiscussionTools Depends-On: I5a0698d84932a474800a68dba9b76b3433b19290 Bug: T20110 Change-Id: Ie13181b78b8e2903c6cc0f0f778689bcc8b8ce2e --- SimpleCaptcha/SimpleCaptcha.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/SimpleCaptcha/SimpleCaptcha.php b/SimpleCaptcha/SimpleCaptcha.php index 8f4f77e4d..3d06ef59c 100644 --- a/SimpleCaptcha/SimpleCaptcha.php +++ b/SimpleCaptcha/SimpleCaptcha.php @@ -884,7 +884,24 @@ class SimpleCaptcha { // this can't be done for addurl trigger, because this requires one "free" save // for the user, which we don't know, when he did it. if ( $this->action === 'edit' ) { - $status->fatal( 'captcha-edit-fail' ); + // Default message is that the user failed a CAPTCHA, so show 'captcha-edit-fail'. + $message = 'captcha-edit-fail'; + if ( $this->shouldForceShowCaptcha() ) { + // If an extension set the forceShowCaptcha property, then it likely means + // that the user already submitted an edit, and so the 'captcha-edit' + // message is more appropriate. + $message = 'captcha-edit'; + [ $_index, $word ] = $this->getCaptchaParamsFromRequest( + RequestContext::getMain()->getRequest() + ); + // But if there's a word supplied in the request, then we should + // use 'captcha-edit-fail' as it indicates a failed attempt + // at solving the CAPTCHA by the user. + if ( $word ) { + $message = 'captcha-edit-fail'; + } + } + $status->fatal( $message ); } $this->addCaptchaAPI( $status->statusData ); $key = CacheKeyHelper::getKeyForPage( $page );