MediaWiki  REL1_23
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 
00071         return $types;
00072     }
00073 
00074     public function getAllowedParams() {
00075         return array(
00076             'type' => array(
00077                 ApiBase::PARAM_DFLT => 'edit',
00078                 ApiBase::PARAM_ISMULTI => true,
00079                 ApiBase::PARAM_TYPE => array_keys( $this->getTokenTypes() ),
00080             ),
00081         );
00082     }
00083 
00084     public function getResultProperties() {
00085         $props = array(
00086             '' => array(),
00087         );
00088 
00089         self::addTokenProperties( $props, $this->getTokenTypes() );
00090 
00091         return $props;
00092     }
00093 
00094     public function getParamDescription() {
00095         return array(
00096             'type' => 'Type of token(s) to request'
00097         );
00098     }
00099 
00100     public function getDescription() {
00101         return 'Gets tokens for data-modifying actions.';
00102     }
00103 
00104     protected function getExamples() {
00105         return array(
00106             'api.php?action=tokens' => 'Retrieve an edit token (the default)',
00107             'api.php?action=tokens&type=email|move' => 'Retrieve an email token and a move token'
00108         );
00109     }
00110 }