Brion VIBBER has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/106845


Change subject: Use new hooks in API action=createaccount for Captcha
......................................................................

Use new hooks in API action=createaccount for Captcha

Hooks used:
* AddNewAccountApiForm
* AddNewAccountApiResult

This adds a 'captcha' section to the results with the same format
as we provide for editing; you'll get this for instance at the
same time as you do a first-request that prompts for a token.

No modification to the result status is included; presence of
the 'captcha' section is assumed to be enough to prompt the client
to fetch and render the captcha prompt.

Requires core changes: If5b7dab8

Sample API client: https://github.com/brion/api-createaccount

Change-Id: Id628defaeab2bf5979ca8f4284d14fc42d9c3e46
---
M Captcha.php
M ConfirmEdit.php
M ConfirmEditHooks.php
3 files changed, 57 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ConfirmEdit 
refs/changes/45/106845/1

diff --git a/Captcha.php b/Captcha.php
index b15de61..105939d 100644
--- a/Captcha.php
+++ b/Captcha.php
@@ -619,7 +619,7 @@
         * @return bool
         */
        protected function isAPICaptchaModule( $module ) {
-               return $module instanceof ApiEditPage;
+               return $module instanceof ApiEditPage || $module instanceof 
ApiCreateAccount;
        }
 
        /**
@@ -770,4 +770,50 @@
                        $wgOut->addWikiMsg( 'captchahelp-cookies-needed' );
                }
        }
+
+       /**
+        * Pass API captcha parameters on to the login form when using
+        * API account creation.
+        *
+        * @param ApiCreateAccount $apiModule
+        * @param LoginForm $loginForm
+        * @return hook return value
+        */
+       function addNewAccountApiForm( $apiModule, $loginForm ) {
+               global $wgRequest;
+               $main = $apiModule->getMain();
+
+               $id = $main->getVal( 'captchaid' );
+               if ( $id ) {
+                       $wgRequest->setVal( 'wpCaptchaId', $id );
+
+                       // Suppress "unrecognized parameter" warning:
+                       $main->getVal( 'wpCaptchaId' );
+               }
+
+               $word = $main->getVal( 'captchaword' );
+               if ( $word ) {
+                       $wgRequest->setVal( 'wpCaptchaWord', $word );
+
+                       // Suppress "unrecognized parameter" warning:
+                       $main->getVal( 'wpCaptchaWord' );
+               }
+
+               return true;
+       }
+       
+       /**
+        * Pass extra data back in API results for account creation.
+        *
+        * @param ApiCreateAccount $apiModule
+        * @param LoginForm &loginForm
+        * @param array &$params
+        * @return hook return value
+        */
+       function addNewAccountApiResult( $apiModule, $loginPage, &$result ) {
+               if ( $result['result'] !== 'success' ) {
+                       $this->addCaptchaAPI( $result );
+               }
+               return true;
+       }
 }
diff --git a/ConfirmEdit.php b/ConfirmEdit.php
index d861a04..72e1392 100644
--- a/ConfirmEdit.php
+++ b/ConfirmEdit.php
@@ -189,6 +189,8 @@
 $wgHooks['APIEditBeforeSave'][] = 'ConfirmEditHooks::confirmEditAPI';
 $wgHooks['APIGetAllowedParams'][] = 'ConfirmEditHooks::APIGetAllowedParams';
 $wgHooks['APIGetParamDescription'][] = 
'ConfirmEditHooks::APIGetParamDescription';
+$wgHooks['AddNewAccountApiForm'][] = 'ConfirmEditHooks::addNewAccountApiForm';
+$wgHooks['AddNewAccountApiResult'][] = 
'ConfirmEditHooks::addNewAccountApiResult';
 
 $wgAutoloadClasses['ConfirmEditHooks'] = 
"$wgConfirmEditIP/ConfirmEditHooks.php";
 $wgAutoloadClasses['SimpleCaptcha'] = "$wgConfirmEditIP/Captcha.php";
diff --git a/ConfirmEditHooks.php b/ConfirmEditHooks.php
index bc85138..c03e316 100644
--- a/ConfirmEditHooks.php
+++ b/ConfirmEditHooks.php
@@ -26,6 +26,14 @@
        static function confirmEditAPI( $editPage, $newtext, &$resultArr ) {
                return self::getInstance()->confirmEditAPI( $editPage, 
$newtext, $resultArr );
        }
+       
+       static function addNewAccountApiForm( $apiModule, $loginForm ) {
+               return self::getInstance()->addNewAccountApiForm( $apiModule, 
$loginForm );
+       }
+       
+       static function addNewAccountApiResult( $apiModule, $loginPage, 
&$result ) {
+               return self::getInstance()->addNewAccountApiResult( $apiModule, 
$loginPage, $result );
+       }
 
        static function injectUserCreate( &$template ) {
                return self::getInstance()->injectUserCreate( $template );

-- 
To view, visit https://gerrit.wikimedia.org/r/106845
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id628defaeab2bf5979ca8f4284d14fc42d9c3e46
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ConfirmEdit
Gerrit-Branch: master
Gerrit-Owner: Brion VIBBER <br...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to