Initial, pre-test, commit of modified patch for Bug 31608 - API edit support for...
authorMark A. Hershberger <mah@users.mediawiki.org>
Thu, 1 Dec 2011 16:59:49 +0000 (16:59 +0000)
committerMark A. Hershberger <mah@users.mediawiki.org>
Thu, 1 Dec 2011 16:59:49 +0000 (16:59 +0000)
From the comment:

    This patch implements addCaptchaAPI and removes ConfirmEdit, since
    it's unnecessary - it seems it was reimplemented in ReCaptcha to
    detect when the captcha had to be shown for the first time and not
    show an error message, but a minor change to passCaptcha ( if (
    $response === null ) { return false; } ) accomplishes that.

    Because the api edit module sets wpCaptchaId and wpCaptchaWord,
    those are passed to recaptcha as recaptcha_challenge_field and
    recaptcha_response_field if the latter two are not set. The api
    returns only the public key and the error message - the client can
    use this to ask the recaptcha server for a captcha, it works in
    javascript at least.

Will test and comment further.

ReCaptcha.php

index 8f6b97b605c09df9be30f51222273c10f2c505c0..25f6fac8692874d0340c72bac2e0e12c4fab73cd 100644 (file)
@@ -87,12 +87,22 @@ class ReCaptcha extends SimpleCaptcha {
         */
        function passCaptcha() {
                global $wgReCaptchaPrivateKey;
+               global $wgRequest;
+
+               //API is hardwired to return wpCaptchaId and wpCaptchaWord, so use that if the standard two are empty
+               $challenge = $wgRequest->getVal('recaptcha_challenge_field',$wgRequest->getVal('wpCaptchaId'));
+               $response = $wgRequest->getVal('recaptcha_response_field',$wgRequest->getVal('wpCaptchaWord'));
+               if ( $response === null ) {
+                       //new captcha session
+                       return false;
+               }
+
                $recaptcha_response =
                        recaptcha_check_answer (
                                $wgReCaptchaPrivateKey,
                                wfGetIP (),
-                               $_POST['recaptcha_challenge_field'],
-                               $_POST['recaptcha_response_field']
+                               $challenge,
+                               $response
                        );
                if (!$recaptcha_response->is_valid) {
                        $this->recaptcha_error = $recaptcha_response->error;
@@ -103,31 +113,12 @@ class ReCaptcha extends SimpleCaptcha {
 
        }
 
-       /**
-        * Called on all edit page saves. (EditFilter events)
-        * @return boolean - true if page save should continue, false if should display Captcha widget.
-        */
-       function confirmEdit( $editPage, $newtext, $section, $merged = false ) {
-               if( $this->shouldCheck( $editPage, $newtext, $section ) ) {
-
-                       if (!isset($_POST['recaptcha_response_field'])) {
-                               //User has not yet been presented with Captcha, show the widget.
-                               $editPage->showEditForm( array( &$this, 'editCallback' ) );
-                               return false;
-                       }
-
-                       if( $this->passCaptcha() ) {
-                               return true;
-                       } else {
-                               //Try again - show the widget
-                               $editPage->showEditForm( array( &$this, 'editCallback' ) );
-                               return false;
-                       }
-
-               } else {
-                       wfDebug( "ConfirmEdit: no need to show captcha.\n" );
-                       return true;
-               }
+       function addCaptchaAPI( &$resultArr ) {
+               global $wgReCaptchaPublicKey;
+               $resultArr['captcha']['type'] = 'recaptcha';
+               $resultArr['captcha']['mime'] = 'image/png';
+               $resultArr['captcha']['key'] = $wgReCaptchaPublicKey;
+               $resultArr['captcha']['error'] = $this->recaptcha_error;
        }
 
        /**