Make API action=tokens extendable
authorMax Semenik <maxsem.wiki@gmail.com>
Wed, 25 Apr 2012 08:48:53 +0000 (12:48 +0400)
committerMax Semenik <maxsem.wiki@gmail.com>
Wed, 25 Apr 2012 08:48:53 +0000 (12:48 +0400)
LocalSettings snippet for testing:

$wgHooks['ApiTokensGetTokenTypes'][] = function( &$hookTypes ) {
$hookTypes['foo'] = function() { return 'bar'; };
return true;
};

Change-Id: Idf1f4379e677d21059c1b4e12f80e9d2bafb9897

docs/hooks.txt
includes/api/ApiTokens.php

index d87bd3d..1a9f41d 100644 (file)
@@ -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
index 7964095..4a68826 100644 (file)
@@ -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() ),
+                       ),
                );
        }