Merge "Moved $function definition up to make IDE happy."
[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 getResultProperties() {
88 return array(
89 '' => array(
90 'patroltoken' => array(
91 ApiBase::PROP_TYPE => 'string',
92 ApiBase::PROP_NULLABLE => true
93 ),
94 'edittoken' => array(
95 ApiBase::PROP_TYPE => 'string',
96 ApiBase::PROP_NULLABLE => true
97 ),
98 'deletetoken' => array(
99 ApiBase::PROP_TYPE => 'string',
100 ApiBase::PROP_NULLABLE => true
101 ),
102 'protecttoken' => array(
103 ApiBase::PROP_TYPE => 'string',
104 ApiBase::PROP_NULLABLE => true
105 ),
106 'movetoken' => array(
107 ApiBase::PROP_TYPE => 'string',
108 ApiBase::PROP_NULLABLE => true
109 ),
110 'blocktoken' => array(
111 ApiBase::PROP_TYPE => 'string',
112 ApiBase::PROP_NULLABLE => true
113 ),
114 'unblocktoken' => array(
115 ApiBase::PROP_TYPE => 'string',
116 ApiBase::PROP_NULLABLE => true
117 ),
118 'emailtoken' => array(
119 ApiBase::PROP_TYPE => 'string',
120 ApiBase::PROP_NULLABLE => true
121 ),
122 'importtoken' => array(
123 ApiBase::PROP_TYPE => 'string',
124 ApiBase::PROP_NULLABLE => true
125 ),
126 'watchtoken' => array(
127 ApiBase::PROP_TYPE => 'string',
128 ApiBase::PROP_NULLABLE => true
129 ),
130 'optionstoken' => array(
131 ApiBase::PROP_TYPE => 'string',
132 ApiBase::PROP_NULLABLE => true
133 )
134 )
135 );
136 }
137
138 public function getParamDescription() {
139 return array(
140 'type' => 'Type of token(s) to request'
141 );
142 }
143
144 public function getDescription() {
145 return 'Gets tokens for data-modifying actions';
146 }
147
148 protected function getExamples() {
149 return array(
150 'api.php?action=tokens' => 'Retrieve an edit token (the default)',
151 'api.php?action=tokens&type=email|move' => 'Retrieve an email token and a move token'
152 );
153 }
154 }