Reduced some master queries by adding flags to Revision functions.
[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 wfProfileIn( __METHOD__ );
39 $params = $this->extractRequestParams();
40 $res = array();
41
42 $types = $this->getTokenTypes();
43 foreach ( $params['type'] as $type ) {
44 $type = strtolower( $type );
45
46 $val = call_user_func( $types[$type], null, null );
47
48 if ( $val === false ) {
49 $this->setWarning( "Action '$type' is not allowed for the current user" );
50 } else {
51 $res[$type . 'token'] = $val;
52 }
53 }
54
55 $this->getResult()->addValue( null, $this->getModuleName(), $res );
56 wfProfileOut( __METHOD__ );
57 }
58
59 private function getTokenTypes() {
60 static $types = null;
61 if ( $types ) {
62 return $types;
63 }
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';
70 }
71 wfRunHooks( 'ApiTokensGetTokenTypes', array( &$types ) );
72 ksort( $types );
73 wfProfileOut( __METHOD__ );
74 return $types;
75 }
76
77 public function getAllowedParams() {
78 return array(
79 'type' => array(
80 ApiBase::PARAM_DFLT => 'edit',
81 ApiBase::PARAM_ISMULTI => true,
82 ApiBase::PARAM_TYPE => array_keys( $this->getTokenTypes() ),
83 ),
84 );
85 }
86
87 public function getParamDescription() {
88 return array(
89 'type' => 'Type of token(s) to request'
90 );
91 }
92
93 public function getDescription() {
94 return 'Gets tokens for data-modifying actions';
95 }
96
97 protected function getExamples() {
98 return array(
99 'api.php?action=tokens' => 'Retrieve an edit token (the default)',
100 'api.php?action=tokens&type=email|move' => 'Retrieve an email token and a move token'
101 );
102 }
103
104 public function getVersion() {
105 return __CLASS__ . ': $Id$';
106 }
107 }