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