5 * Created on Jul 29, 2011
7 * Copyright © 2011 John Du Hart john@johnduhart.me
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
31 class ApiTokens
extends ApiBase
{
33 public function execute() {
34 wfProfileIn( __METHOD__
);
35 $params = $this->extractRequestParams();
38 $types = $this->getTokenTypes();
39 foreach ( $params['type'] as $type ) {
40 $type = strtolower( $type );
42 $val = call_user_func( $types[$type], null, null );
44 if ( $val === false ) {
45 $this->setWarning( "Action '$type' is not allowed for the current user" );
47 $res[$type . 'token'] = $val;
51 $this->getResult()->addValue( null, $this->getModuleName(), $res );
52 wfProfileOut( __METHOD__
);
55 private function getTokenTypes() {
60 wfProfileIn( __METHOD__
);
61 $types = array( 'patrol' => 'ApiQueryRecentChanges::getPatrolToken' );
62 $names = array( 'edit', 'delete', 'protect', 'move', 'block', 'unblock',
63 'email', 'import', 'watch', 'options' );
64 foreach ( $names as $name ) {
65 $types[$name] = 'ApiQueryInfo::get' . ucfirst( $name ) . 'Token';
67 wfRunHooks( 'ApiTokensGetTokenTypes', array( &$types ) );
69 wfProfileOut( __METHOD__
);
73 public function getAllowedParams() {
76 ApiBase
::PARAM_DFLT
=> 'edit',
77 ApiBase
::PARAM_ISMULTI
=> true,
78 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenTypes() ),
83 public function getResultProperties() {
86 'patroltoken' => array(
87 ApiBase
::PROP_TYPE
=> 'string',
88 ApiBase
::PROP_NULLABLE
=> true
91 ApiBase
::PROP_TYPE
=> 'string',
92 ApiBase
::PROP_NULLABLE
=> true
94 'deletetoken' => array(
95 ApiBase
::PROP_TYPE
=> 'string',
96 ApiBase
::PROP_NULLABLE
=> true
98 'protecttoken' => array(
99 ApiBase
::PROP_TYPE
=> 'string',
100 ApiBase
::PROP_NULLABLE
=> true
102 'movetoken' => array(
103 ApiBase
::PROP_TYPE
=> 'string',
104 ApiBase
::PROP_NULLABLE
=> true
106 'blocktoken' => array(
107 ApiBase
::PROP_TYPE
=> 'string',
108 ApiBase
::PROP_NULLABLE
=> true
110 'unblocktoken' => array(
111 ApiBase
::PROP_TYPE
=> 'string',
112 ApiBase
::PROP_NULLABLE
=> true
114 'emailtoken' => array(
115 ApiBase
::PROP_TYPE
=> 'string',
116 ApiBase
::PROP_NULLABLE
=> true
118 'importtoken' => array(
119 ApiBase
::PROP_TYPE
=> 'string',
120 ApiBase
::PROP_NULLABLE
=> true
122 'watchtoken' => array(
123 ApiBase
::PROP_TYPE
=> 'string',
124 ApiBase
::PROP_NULLABLE
=> true
126 'optionstoken' => array(
127 ApiBase
::PROP_TYPE
=> 'string',
128 ApiBase
::PROP_NULLABLE
=> true
134 public function getParamDescription() {
136 'type' => 'Type of token(s) to request'
140 public function getDescription() {
141 return 'Gets tokens for data-modifying actions';
144 protected function getExamples() {
146 'api.php?action=tokens' => 'Retrieve an edit token (the default)',
147 'api.php?action=tokens&type=email|move' => 'Retrieve an email token and a move token'