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 __construct( $main, $action ) {
34 parent
::__construct( $main, $action );
37 public function execute() {
38 wfProfileIn( __METHOD__
);
39 $params = $this->extractRequestParams();
42 $types = $this->getTokenTypes();
43 foreach ( $params['type'] as $type ) {
44 $type = strtolower( $type );
46 $val = call_user_func( $types[$type], null, null );
48 if ( $val === false ) {
49 $this->setWarning( "Action '$type' is not allowed for the current user" );
51 $res[$type . 'token'] = $val;
55 $this->getResult()->addValue( null, $this->getModuleName(), $res );
56 wfProfileOut( __METHOD__
);
59 private function getTokenTypes() {
64 wfProfileIn( __METHOD__
);
65 $types = array( 'patrol' => 'ApiQueryRecentChanges::getPatrolToken' );
66 $names = array( 'edit', 'delete', 'protect', 'move', 'block', 'unblock',
67 'email', 'import', 'watch', 'options' );
68 foreach ( $names as $name ) {
69 $types[$name] = 'ApiQueryInfo::get' . ucfirst( $name ) . 'Token';
71 wfRunHooks( 'ApiTokensGetTokenTypes', array( &$types ) );
73 wfProfileOut( __METHOD__
);
77 public function getAllowedParams() {
80 ApiBase
::PARAM_DFLT
=> 'edit',
81 ApiBase
::PARAM_ISMULTI
=> true,
82 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenTypes() ),
87 public function getResultProperties() {
90 'patroltoken' => array(
91 ApiBase
::PROP_TYPE
=> 'string',
92 ApiBase
::PROP_NULLABLE
=> true
95 ApiBase
::PROP_TYPE
=> 'string',
96 ApiBase
::PROP_NULLABLE
=> true
98 'deletetoken' => array(
99 ApiBase
::PROP_TYPE
=> 'string',
100 ApiBase
::PROP_NULLABLE
=> true
102 'protecttoken' => array(
103 ApiBase
::PROP_TYPE
=> 'string',
104 ApiBase
::PROP_NULLABLE
=> true
106 'movetoken' => array(
107 ApiBase
::PROP_TYPE
=> 'string',
108 ApiBase
::PROP_NULLABLE
=> true
110 'blocktoken' => array(
111 ApiBase
::PROP_TYPE
=> 'string',
112 ApiBase
::PROP_NULLABLE
=> true
114 'unblocktoken' => array(
115 ApiBase
::PROP_TYPE
=> 'string',
116 ApiBase
::PROP_NULLABLE
=> true
118 'emailtoken' => array(
119 ApiBase
::PROP_TYPE
=> 'string',
120 ApiBase
::PROP_NULLABLE
=> true
122 'importtoken' => array(
123 ApiBase
::PROP_TYPE
=> 'string',
124 ApiBase
::PROP_NULLABLE
=> true
126 'watchtoken' => array(
127 ApiBase
::PROP_TYPE
=> 'string',
128 ApiBase
::PROP_NULLABLE
=> true
130 'optionstoken' => array(
131 ApiBase
::PROP_TYPE
=> 'string',
132 ApiBase
::PROP_NULLABLE
=> true
138 public function getParamDescription() {
140 'type' => 'Type of token(s) to request'
144 public function getDescription() {
145 return 'Gets tokens for data-modifying actions';
148 protected function getExamples() {
150 'api.php?action=tokens' => 'Retrieve an edit token (the default)',
151 'api.php?action=tokens&type=email|move' => 'Retrieve an email token and a move token'
155 public function getVersion() {
156 return __CLASS__
. ': $Id$';