MediaWiki  REL1_24
ApiTokens.php
Go to the documentation of this file.
00001 <?php
00031 class ApiTokens extends ApiBase {
00032 
00033     public function execute() {
00034         $this->setWarning(
00035             "action=tokens has been deprecated. Please use action=query&meta=tokens instead."
00036         );
00037 
00038         $params = $this->extractRequestParams();
00039         $res = array();
00040 
00041         $types = $this->getTokenTypes();
00042         foreach ( $params['type'] as $type ) {
00043             $val = call_user_func( $types[$type], null, null );
00044 
00045             if ( $val === false ) {
00046                 $this->setWarning( "Action '$type' is not allowed for the current user" );
00047             } else {
00048                 $res[$type . 'token'] = $val;
00049             }
00050         }
00051 
00052         $this->getResult()->addValue( null, $this->getModuleName(), $res );
00053     }
00054 
00055     private function getTokenTypes() {
00056         // If we're in JSON callback mode, no tokens can be obtained
00057         if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
00058             return array();
00059         }
00060 
00061         static $types = null;
00062         if ( $types ) {
00063             return $types;
00064         }
00065         wfProfileIn( __METHOD__ );
00066         $types = array( 'patrol' => array( 'ApiQueryRecentChanges', 'getPatrolToken' ) );
00067         $names = array( 'edit', 'delete', 'protect', 'move', 'block', 'unblock',
00068             'email', 'import', 'watch', 'options' );
00069         foreach ( $names as $name ) {
00070             $types[$name] = array( 'ApiQueryInfo', 'get' . ucfirst( $name ) . 'Token' );
00071         }
00072         wfRunHooks( 'ApiTokensGetTokenTypes', array( &$types ) );
00073         ksort( $types );
00074         wfProfileOut( __METHOD__ );
00075 
00076         return $types;
00077     }
00078 
00079     public function getAllowedParams() {
00080         return array(
00081             'type' => array(
00082                 ApiBase::PARAM_DFLT => 'edit',
00083                 ApiBase::PARAM_ISMULTI => true,
00084                 ApiBase::PARAM_TYPE => array_keys( $this->getTokenTypes() ),
00085             ),
00086         );
00087     }
00088 
00089     public function getParamDescription() {
00090         return array(
00091             'type' => 'Type of token(s) to request'
00092         );
00093     }
00094 
00095     public function getDescription() {
00096         return array(
00097             'This module is deprecated in favor of action=query&meta=tokens.',
00098             'Gets tokens for data-modifying actions.'
00099         );
00100     }
00101 
00102     protected function getExamples() {
00103         return array(
00104             'api.php?action=tokens' => 'Retrieve an edit token (the default)',
00105             'api.php?action=tokens&type=email|move' => 'Retrieve an email token and a move token'
00106         );
00107     }
00108 }