Merge "Making https svnroot point to https viewvc, not https viewvc to https viewvc...
[lhc/web/wiklou.git] / includes / api / ApiTokens.php
1 <?php
2 /**
3 *
4 *
5 * Created on Jul 29, 2011
6 *
7 * Copyright © 2011 John Du Hart john@johnduhart.me
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27
28 /**
29 * @ingroup API
30 */
31 class ApiTokens extends ApiBase {
32
33 public function __construct( $main, $action ) {
34 parent::__construct( $main, $action );
35 }
36
37 public function execute() {
38 $params = $this->extractRequestParams();
39 $res = array();
40
41 foreach ( $params['type'] as $type ) {
42 $type = strtolower( $type );
43 $func = 'get' .
44 ucfirst( $type ) .
45 'Token';
46 if ( $type === 'patrol' ) {
47 $val = call_user_func( array( 'ApiQueryRecentChanges', $func ), null, null );
48 } else {
49 $val = call_user_func( array( 'ApiQueryInfo', $func ), null, null );
50 }
51 if ( $val === false ) {
52 $this->setWarning( "Action '$type' is not allowed for the current user" );
53 } else {
54 $res[$type . 'token'] = $val;
55 }
56 }
57
58 $this->getResult()->addValue( null, $this->getModuleName(), $res );
59 }
60
61 public function getAllowedParams() {
62 return array(
63 'type' => array(
64 ApiBase::PARAM_DFLT => 'edit',
65 ApiBase::PARAM_ISMULTI => true,
66 ApiBase::PARAM_TYPE => array(
67 'edit', 'delete', 'protect', 'move', 'block', 'unblock',
68 'email', 'import', 'watch', 'patrol'
69 )
70 )
71 );
72 }
73
74 public function getParamDescription() {
75 return array(
76 'type' => 'Type of token(s) to request'
77 );
78 }
79
80 public function getDescription() {
81 return 'Gets tokens for data-modifying actions';
82 }
83
84 protected function getExamples() {
85 return array(
86 'api.php?action=tokens' => 'Retrieve an edit token (the default)',
87 'api.php?action=tokens&type=email|move' => 'Retrieve an email token and a move token'
88 );
89 }
90
91 public function getVersion() {
92 return __CLASS__ . ': $Id$';
93 }
94 }