From: Kaldari Date: Wed, 28 Mar 2012 22:02:10 +0000 (-0700) Subject: (bug 27757) API method for retrieving tokens X-Git-Tag: 1.31.0-rc.0~24022^2 X-Git-Url: https://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/exercices/journal.php?a=commitdiff_plain;h=9ec8e3525bd0b4cf47fd1f59b747a62ef602cd8b;p=lhc%2Fweb%2Fwiklou.git (bug 27757) API method for retrieving tokens Change-Id: I58bc5847b996d100712781052f20150f76786ed1 --- diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 301c024406..eaeda494ee 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -368,6 +368,7 @@ $wgAutoloadLocalClasses = array( 'ApiResult' => 'includes/api/ApiResult.php', 'ApiRollback' => 'includes/api/ApiRollback.php', 'ApiRsd' => 'includes/api/ApiRsd.php', + 'ApiTokens' => 'includes/api/ApiTokens.php', 'ApiUnblock' => 'includes/api/ApiUnblock.php', 'ApiUndelete' => 'includes/api/ApiUndelete.php', 'ApiUpload' => 'includes/api/ApiUpload.php', diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php index 15b086162c..554e87bce8 100644 --- a/includes/api/ApiMain.php +++ b/includes/api/ApiMain.php @@ -61,6 +61,7 @@ class ApiMain extends ApiBase { 'paraminfo' => 'ApiParamInfo', 'rsd' => 'ApiRsd', 'compare' => 'ApiComparePages', + 'tokens' => 'ApiTokens', // Write modules 'purge' => 'ApiPurge', diff --git a/includes/api/ApiQueryRecentChanges.php b/includes/api/ApiQueryRecentChanges.php index 0ce5b37330..2d2d9fff93 100644 --- a/includes/api/ApiQueryRecentChanges.php +++ b/includes/api/ApiQueryRecentChanges.php @@ -70,24 +70,37 @@ class ApiQueryRecentChanges extends ApiQueryGeneratorBase { /** * @param $pageid * @param $title - * @param $rc RecentChange + * @param $rc RecentChange (optional) * @return bool|String */ - public static function getPatrolToken( $pageid, $title, $rc ) { + public static function getPatrolToken( $pageid, $title, $rc = null ) { global $wgUser; - if ( !$wgUser->useRCPatrol() && ( !$wgUser->useNPPatrol() || - $rc->getAttribute( 'rc_type' ) != RC_NEW ) ) - { - return false; + + $validTokenUser = false; + + if ( $rc ) { + if ( ( $wgUser->useRCPatrol() && $rc->getAttribute( 'rc_type' ) == RC_EDIT ) || + ( $wgUser->useNPPatrol() && $rc->getAttribute( 'rc_type' ) == RC_NEW ) ) + { + $validTokenUser = true; + } + } else { + if ( $wgUser->useRCPatrol() || $wgUser->useNPPatrol() ) { + $validTokenUser = true; + } } - // The patrol token is always the same, let's exploit that - static $cachedPatrolToken = null; - if ( is_null( $cachedPatrolToken ) ) { - $cachedPatrolToken = $wgUser->getEditToken( 'patrol' ); + if ( $validTokenUser ) { + // The patrol token is always the same, let's exploit that + static $cachedPatrolToken = null; + if ( is_null( $cachedPatrolToken ) ) { + $cachedPatrolToken = $wgUser->getEditToken( 'patrol' ); + } + return $cachedPatrolToken; + } else { + return false; } - return $cachedPatrolToken; } /** diff --git a/includes/api/ApiTokens.php b/includes/api/ApiTokens.php new file mode 100644 index 0000000000..79640953e9 --- /dev/null +++ b/includes/api/ApiTokens.php @@ -0,0 +1,94 @@ +extractRequestParams(); + $res = array(); + + foreach ( $params['type'] as $type ) { + $type = strtolower( $type ); + $func = 'get' . + ucfirst( $type ) . + 'Token'; + if ( $type === 'patrol' ) { + $val = call_user_func( array( 'ApiQueryRecentChanges', $func ), null, null ); + } else { + $val = call_user_func( array( 'ApiQueryInfo', $func ), null, null ); + } + if ( $val === false ) { + $this->setWarning( "Action '$type' is not allowed for the current user" ); + } else { + $res[$type . 'token'] = $val; + } + } + + $this->getResult()->addValue( null, $this->getModuleName(), $res ); + } + + public function getAllowedParams() { + return array( + 'type' => array( + ApiBase::PARAM_DFLT => 'edit', + ApiBase::PARAM_ISMULTI => true, + ApiBase::PARAM_TYPE => array( + 'edit', 'delete', 'protect', 'move', 'block', 'unblock', + 'email', 'import', 'watch', 'patrol' + ) + ) + ); + } + + public function getParamDescription() { + return array( + 'type' => 'Type of token(s) to request' + ); + } + + public function getDescription() { + return 'Gets tokens for data-modifying actions'; + } + + protected function getExamples() { + return array( + 'api.php?action=tokens' => 'Retrieve an edit token (the default)', + 'api.php?action=tokens&type=email|move' => 'Retrieve an email token and a move token' + ); + } + + public function getVersion() { + return __CLASS__ . ': $Id$'; + } +}