Fixed spacing in api folder
[mediawiki.git] / includes / api / ApiTokens.php
blob7080f547f3c97a21890302a01d5726bf518aa934
1 <?php
2 /**
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
24 * @file
27 /**
28 * @ingroup API
30 class ApiTokens extends ApiBase {
32 public function execute() {
33 $params = $this->extractRequestParams();
34 $res = array();
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" );
42 } else {
43 $res[$type . 'token'] = $val;
47 $this->getResult()->addValue( null, $this->getModuleName(), $res );
50 private function getTokenTypes() {
51 static $types = null;
52 if ( $types ) {
53 return $types;
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 ) );
63 ksort( $types );
64 wfProfileOut( __METHOD__ );
65 return $types;
68 public function getAllowedParams() {
69 return array(
70 'type' => array(
71 ApiBase::PARAM_DFLT => 'edit',
72 ApiBase::PARAM_ISMULTI => true,
73 ApiBase::PARAM_TYPE => array_keys( $this->getTokenTypes() ),
78 public function getResultProperties() {
79 $props = array(
80 '' => array(),
83 self::addTokenProperties( $props, $this->getTokenTypes() );
85 return $props;
88 public function getParamDescription() {
89 return array(
90 'type' => 'Type of token(s) to request'
94 public function getDescription() {
95 return 'Gets tokens for data-modifying actions';
98 protected function getExamples() {
99 return array(
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'