MediaWiki
master
|
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 static $types = null; 00061 if ( $types ) { 00062 return $types; 00063 } 00064 wfProfileIn( __METHOD__ ); 00065 $types = array( 'patrol' => 'ApiQueryRecentChanges::getPatrolToken' ); 00066 $names = array( 'edit', 'delete', 'protect', 'move', 'block', 'unblock', 00067 'email', 'import', 'watch', 'options' ); 00068 foreach ( $names as $name ) { 00069 $types[$name] = 'ApiQueryInfo::get' . ucfirst( $name ) . 'Token'; 00070 } 00071 wfRunHooks( 'ApiTokensGetTokenTypes', array( &$types ) ); 00072 ksort( $types ); 00073 wfProfileOut( __METHOD__ ); 00074 return $types; 00075 } 00076 00077 public function getAllowedParams() { 00078 return array( 00079 'type' => array( 00080 ApiBase::PARAM_DFLT => 'edit', 00081 ApiBase::PARAM_ISMULTI => true, 00082 ApiBase::PARAM_TYPE => array_keys( $this->getTokenTypes() ), 00083 ), 00084 ); 00085 } 00086 00087 public function getResultProperties() { 00088 return array( 00089 '' => array( 00090 'patroltoken' => array( 00091 ApiBase::PROP_TYPE => 'string', 00092 ApiBase::PROP_NULLABLE => true 00093 ), 00094 'edittoken' => array( 00095 ApiBase::PROP_TYPE => 'string', 00096 ApiBase::PROP_NULLABLE => true 00097 ), 00098 'deletetoken' => array( 00099 ApiBase::PROP_TYPE => 'string', 00100 ApiBase::PROP_NULLABLE => true 00101 ), 00102 'protecttoken' => array( 00103 ApiBase::PROP_TYPE => 'string', 00104 ApiBase::PROP_NULLABLE => true 00105 ), 00106 'movetoken' => array( 00107 ApiBase::PROP_TYPE => 'string', 00108 ApiBase::PROP_NULLABLE => true 00109 ), 00110 'blocktoken' => array( 00111 ApiBase::PROP_TYPE => 'string', 00112 ApiBase::PROP_NULLABLE => true 00113 ), 00114 'unblocktoken' => array( 00115 ApiBase::PROP_TYPE => 'string', 00116 ApiBase::PROP_NULLABLE => true 00117 ), 00118 'emailtoken' => array( 00119 ApiBase::PROP_TYPE => 'string', 00120 ApiBase::PROP_NULLABLE => true 00121 ), 00122 'importtoken' => array( 00123 ApiBase::PROP_TYPE => 'string', 00124 ApiBase::PROP_NULLABLE => true 00125 ), 00126 'watchtoken' => array( 00127 ApiBase::PROP_TYPE => 'string', 00128 ApiBase::PROP_NULLABLE => true 00129 ), 00130 'optionstoken' => array( 00131 ApiBase::PROP_TYPE => 'string', 00132 ApiBase::PROP_NULLABLE => true 00133 ) 00134 ) 00135 ); 00136 } 00137 00138 public function getParamDescription() { 00139 return array( 00140 'type' => 'Type of token(s) to request' 00141 ); 00142 } 00143 00144 public function getDescription() { 00145 return 'Gets tokens for data-modifying actions'; 00146 } 00147 00148 protected function getExamples() { 00149 return array( 00150 'api.php?action=tokens' => 'Retrieve an edit token (the default)', 00151 'api.php?action=tokens&type=email|move' => 'Retrieve an email token and a move token' 00152 ); 00153 } 00154 00155 public function getVersion() { 00156 return __CLASS__ . ': $Id$'; 00157 } 00158 }