From ab4e51863b49960591135b40d153dfc11223b5c0 Mon Sep 17 00:00:00 2001 From: Max Semenik Date: Wed, 25 Apr 2012 12:48:53 +0400 Subject: [PATCH] Make API action=tokens extendable LocalSettings snippet for testing: $wgHooks['ApiTokensGetTokenTypes'][] = function( &$hookTypes ) { $hookTypes['foo'] = function() { return 'bar'; }; return true; }; Change-Id: Idf1f4379e677d21059c1b4e12f80e9d2bafb9897 --- docs/hooks.txt | 5 +++++ includes/api/ApiTokens.php | 39 +++++++++++++++++++++++++------------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/docs/hooks.txt b/docs/hooks.txt index d87bd3d3f2..1a9f41dc88 100644 --- a/docs/hooks.txt +++ b/docs/hooks.txt @@ -395,6 +395,11 @@ In this data array, the key-value-pair identified by the apiLink key is required. &$apis: array of services +'ApiTokensGetTokenTypes': use this hook to extend action=tokens with new +token types. +&$tokenTypes: supported token types in format 'type' => callback function +used to retrieve this type of tokens. + 'ArticleAfterFetchContent': after fetching content of an article from the database $article: the article (object) being loaded from the database diff --git a/includes/api/ApiTokens.php b/includes/api/ApiTokens.php index 79640953e9..4a68826e10 100644 --- a/includes/api/ApiTokens.php +++ b/includes/api/ApiTokens.php @@ -35,19 +35,16 @@ class ApiTokens extends ApiBase { } public function execute() { + wfProfileIn( __METHOD__ ); $params = $this->extractRequestParams(); $res = array(); + $types = $this->getTokenTypes(); 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 ); - } + + $val = call_user_func( $types[$type], null, null ); + if ( $val === false ) { $this->setWarning( "Action '$type' is not allowed for the current user" ); } else { @@ -56,6 +53,25 @@ class ApiTokens extends ApiBase { } $this->getResult()->addValue( null, $this->getModuleName(), $res ); + wfProfileOut( __METHOD__ ); + } + + private function getTokenTypes() { + static $types = null; + if ( $types ) { + return $types; + } + wfProfileIn( __METHOD__ ); + $types = array( 'patrol' => 'ApiQueryRecentChanges::getPatrolToken' ); + $names = array( 'edit', 'delete', 'protect', 'move', 'block', 'unblock', + 'email', 'import', 'watch' ); + foreach ( $names as $name ) { + $types[$name] = 'ApiQUeryInfo::get' . ucfirst( $name ) . 'Token'; + } + wfRunHooks( 'ApiTokensGetTokenTypes', array( &$types ) ); + ksort( $types ); + wfProfileOut( __METHOD__ ); + return $types; } public function getAllowedParams() { @@ -63,11 +79,8 @@ class ApiTokens extends ApiBase { 'type' => array( ApiBase::PARAM_DFLT => 'edit', ApiBase::PARAM_ISMULTI => true, - ApiBase::PARAM_TYPE => array( - 'edit', 'delete', 'protect', 'move', 'block', 'unblock', - 'email', 'import', 'watch', 'patrol' - ) - ) + ApiBase::PARAM_TYPE => array_keys( $this->getTokenTypes() ), + ), ); } -- 2.20.1