api: remove duplicate __contruct calls
[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 execute() {
34 wfProfileIn( __METHOD__ );
35 $params = $this->extractRequestParams();
36 $res = array();
37
38 $types = $this->getTokenTypes();
39 foreach ( $params['type'] as $type ) {
40 $type = strtolower( $type );
41
42 $val = call_user_func( $types[$type], null, null );
43
44 if ( $val === false ) {
45 $this->setWarning( "Action '$type' is not allowed for the current user" );
46 } else {
47 $res[$type . 'token'] = $val;
48 }
49 }
50
51 $this->getResult()->addValue( null, $this->getModuleName(), $res );
52 wfProfileOut( __METHOD__ );
53 }
54
55 private function getTokenTypes() {
56 static $types = null;
57 if ( $types ) {
58 return $types;
59 }
60 wfProfileIn( __METHOD__ );
61 $types = array( 'patrol' => 'ApiQueryRecentChanges::getPatrolToken' );
62 $names = array( 'edit', 'delete', 'protect', 'move', 'block', 'unblock',
63 'email', 'import', 'watch', 'options' );
64 foreach ( $names as $name ) {
65 $types[$name] = 'ApiQueryInfo::get' . ucfirst( $name ) . 'Token';
66 }
67 wfRunHooks( 'ApiTokensGetTokenTypes', array( &$types ) );
68 ksort( $types );
69 wfProfileOut( __METHOD__ );
70 return $types;
71 }
72
73 public function getAllowedParams() {
74 return array(
75 'type' => array(
76 ApiBase::PARAM_DFLT => 'edit',
77 ApiBase::PARAM_ISMULTI => true,
78 ApiBase::PARAM_TYPE => array_keys( $this->getTokenTypes() ),
79 ),
80 );
81 }
82
83 public function getResultProperties() {
84 return array(
85 '' => array(
86 'patroltoken' => array(
87 ApiBase::PROP_TYPE => 'string',
88 ApiBase::PROP_NULLABLE => true
89 ),
90 'edittoken' => array(
91 ApiBase::PROP_TYPE => 'string',
92 ApiBase::PROP_NULLABLE => true
93 ),
94 'deletetoken' => array(
95 ApiBase::PROP_TYPE => 'string',
96 ApiBase::PROP_NULLABLE => true
97 ),
98 'protecttoken' => array(
99 ApiBase::PROP_TYPE => 'string',
100 ApiBase::PROP_NULLABLE => true
101 ),
102 'movetoken' => array(
103 ApiBase::PROP_TYPE => 'string',
104 ApiBase::PROP_NULLABLE => true
105 ),
106 'blocktoken' => array(
107 ApiBase::PROP_TYPE => 'string',
108 ApiBase::PROP_NULLABLE => true
109 ),
110 'unblocktoken' => array(
111 ApiBase::PROP_TYPE => 'string',
112 ApiBase::PROP_NULLABLE => true
113 ),
114 'emailtoken' => array(
115 ApiBase::PROP_TYPE => 'string',
116 ApiBase::PROP_NULLABLE => true
117 ),
118 'importtoken' => array(
119 ApiBase::PROP_TYPE => 'string',
120 ApiBase::PROP_NULLABLE => true
121 ),
122 'watchtoken' => array(
123 ApiBase::PROP_TYPE => 'string',
124 ApiBase::PROP_NULLABLE => true
125 ),
126 'optionstoken' => array(
127 ApiBase::PROP_TYPE => 'string',
128 ApiBase::PROP_NULLABLE => true
129 )
130 )
131 );
132 }
133
134 public function getParamDescription() {
135 return array(
136 'type' => 'Type of token(s) to request'
137 );
138 }
139
140 public function getDescription() {
141 return 'Gets tokens for data-modifying actions';
142 }
143
144 protected function getExamples() {
145 return array(
146 'api.php?action=tokens' => 'Retrieve an edit token (the default)',
147 'api.php?action=tokens&type=email|move' => 'Retrieve an email token and a move token'
148 );
149 }
150 }