MediaWiki
REL1_20
|
00001 <?php 00031 class ApiTokens extends ApiBase { 00032 00033 public function __construct( $main, $action ) { 00034 parent::__construct( $main, $action ); 00035 } 00036 00037 public function execute() { 00038 wfProfileIn( __METHOD__ ); 00039 $params = $this->extractRequestParams(); 00040 $res = array(); 00041 00042 $types = $this->getTokenTypes(); 00043 foreach ( $params['type'] as $type ) { 00044 $type = strtolower( $type ); 00045 00046 $val = call_user_func( $types[$type], null, null ); 00047 00048 if ( $val === false ) { 00049 $this->setWarning( "Action '$type' is not allowed for the current user" ); 00050 } else { 00051 $res[$type . 'token'] = $val; 00052 } 00053 } 00054 00055 $this->getResult()->addValue( null, $this->getModuleName(), $res ); 00056 wfProfileOut( __METHOD__ ); 00057 } 00058 00059 private function getTokenTypes() { 00060 // If we're in JSON callback mode, no tokens can be obtained 00061 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) { 00062 return array(); 00063 } 00064 00065 static $types = null; 00066 if ( $types ) { 00067 return $types; 00068 } 00069 wfProfileIn( __METHOD__ ); 00070 $types = array( 'patrol' => 'ApiQueryRecentChanges::getPatrolToken' ); 00071 $names = array( 'edit', 'delete', 'protect', 'move', 'block', 'unblock', 00072 'email', 'import', 'watch', 'options' ); 00073 foreach ( $names as $name ) { 00074 $types[$name] = 'ApiQueryInfo::get' . ucfirst( $name ) . 'Token'; 00075 } 00076 wfRunHooks( 'ApiTokensGetTokenTypes', array( &$types ) ); 00077 ksort( $types ); 00078 wfProfileOut( __METHOD__ ); 00079 return $types; 00080 } 00081 00082 public function getAllowedParams() { 00083 return array( 00084 'type' => array( 00085 ApiBase::PARAM_DFLT => 'edit', 00086 ApiBase::PARAM_ISMULTI => true, 00087 ApiBase::PARAM_TYPE => array_keys( $this->getTokenTypes() ), 00088 ), 00089 ); 00090 } 00091 00092 public function getResultProperties() { 00093 return array( 00094 '' => array( 00095 'patroltoken' => array( 00096 ApiBase::PROP_TYPE => 'string', 00097 ApiBase::PROP_NULLABLE => true 00098 ), 00099 'edittoken' => array( 00100 ApiBase::PROP_TYPE => 'string', 00101 ApiBase::PROP_NULLABLE => true 00102 ), 00103 'deletetoken' => array( 00104 ApiBase::PROP_TYPE => 'string', 00105 ApiBase::PROP_NULLABLE => true 00106 ), 00107 'protecttoken' => array( 00108 ApiBase::PROP_TYPE => 'string', 00109 ApiBase::PROP_NULLABLE => true 00110 ), 00111 'movetoken' => array( 00112 ApiBase::PROP_TYPE => 'string', 00113 ApiBase::PROP_NULLABLE => true 00114 ), 00115 'blocktoken' => array( 00116 ApiBase::PROP_TYPE => 'string', 00117 ApiBase::PROP_NULLABLE => true 00118 ), 00119 'unblocktoken' => array( 00120 ApiBase::PROP_TYPE => 'string', 00121 ApiBase::PROP_NULLABLE => true 00122 ), 00123 'emailtoken' => array( 00124 ApiBase::PROP_TYPE => 'string', 00125 ApiBase::PROP_NULLABLE => true 00126 ), 00127 'importtoken' => array( 00128 ApiBase::PROP_TYPE => 'string', 00129 ApiBase::PROP_NULLABLE => true 00130 ), 00131 'watchtoken' => array( 00132 ApiBase::PROP_TYPE => 'string', 00133 ApiBase::PROP_NULLABLE => true 00134 ), 00135 'optionstoken' => array( 00136 ApiBase::PROP_TYPE => 'string', 00137 ApiBase::PROP_NULLABLE => true 00138 ) 00139 ) 00140 ); 00141 } 00142 00143 public function getParamDescription() { 00144 return array( 00145 'type' => 'Type of token(s) to request' 00146 ); 00147 } 00148 00149 public function getDescription() { 00150 return 'Gets tokens for data-modifying actions'; 00151 } 00152 00153 protected function getExamples() { 00154 return array( 00155 'api.php?action=tokens' => 'Retrieve an edit token (the default)', 00156 'api.php?action=tokens&type=email|move' => 'Retrieve an email token and a move token' 00157 ); 00158 } 00159 00160 public function getVersion() { 00161 return __CLASS__ . ': $Id$'; 00162 } 00163 }