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
30 class ApiTokens
extends ApiBase
{
32 public function execute() {
33 $params = $this->extractRequestParams();
36 $types = $this->getTokenTypes();
37 foreach ( $params['type'] as $type ) {
38 $val = call_user_func( $types[$type], null, null );
40 if ( $val === false ) {
41 $this->setWarning( "Action '$type' is not allowed for the current user" );
43 $res[$type . 'token'] = $val;
47 $this->getResult()->addValue( null, $this->getModuleName(), $res );
50 private function getTokenTypes() {
55 wfProfileIn( __METHOD__
);
56 $types = array( 'patrol' => array( 'ApiQueryRecentChanges', 'getPatrolToken' ) );
57 $names = array( 'edit', 'delete', 'protect', 'move', 'block', 'unblock',
58 'email', 'import', 'watch', 'options' );
59 foreach ( $names as $name ) {
60 $types[$name] = array( 'ApiQueryInfo', 'get' . ucfirst( $name ) . 'Token' );
62 wfRunHooks( 'ApiTokensGetTokenTypes', array( &$types ) );
64 wfProfileOut( __METHOD__
);
68 public function getAllowedParams() {
71 ApiBase
::PARAM_DFLT
=> 'edit',
72 ApiBase
::PARAM_ISMULTI
=> true,
73 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenTypes() ),
78 public function getResultProperties() {
83 self
::addTokenProperties( $props, $this->getTokenTypes() );
88 public function getParamDescription() {
90 'type' => 'Type of token(s) to request'
94 public function getDescription() {
95 return 'Gets tokens for data-modifying actions';
98 protected function getExamples() {
100 'api.php?action=tokens' => 'Retrieve an edit token (the default)',
101 'api.php?action=tokens&type=email|move' => 'Retrieve an email token and a move token'