Merge "Revert "Made LCStoreDB try to use a separate DB connection""
[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 * @ingroup API
29 */
30 class ApiTokens extends ApiBase {
31
32 public function execute() {
33 $params = $this->extractRequestParams();
34 $res = array();
35
36 $types = $this->getTokenTypes();
37 foreach ( $params['type'] as $type ) {
38 $val = call_user_func( $types[$type], null, null );
39
40 if ( $val === false ) {
41 $this->setWarning( "Action '$type' is not allowed for the current user" );
42 } else {
43 $res[$type . 'token'] = $val;
44 }
45 }
46
47 $this->getResult()->addValue( null, $this->getModuleName(), $res );
48 }
49
50 private function getTokenTypes() {
51 // If we're in JSON callback mode, no tokens can be obtained
52 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
53 return array();
54 }
55
56 static $types = null;
57 if ( $types ) {
58 return $types;
59 }
60 wfProfileIn( __METHOD__ );
61 $types = array( 'patrol' => array( 'ApiQueryRecentChanges', 'getPatrolToken' ) );
62 $names = array( 'edit', 'delete', 'protect', 'move', 'block', 'unblock',
63 'email', 'import', 'watch', 'options' );
64 foreach ( $names as $name ) {
65 $types[$name] = array( 'ApiQueryInfo', 'get' . ucfirst( $name ) . 'Token' );
66 }
67 wfRunHooks( 'ApiTokensGetTokenTypes', array( &$types ) );
68 ksort( $types );
69 wfProfileOut( __METHOD__ );
70
71 return $types;
72 }
73
74 public function getAllowedParams() {
75 return array(
76 'type' => array(
77 ApiBase::PARAM_DFLT => 'edit',
78 ApiBase::PARAM_ISMULTI => true,
79 ApiBase::PARAM_TYPE => array_keys( $this->getTokenTypes() ),
80 ),
81 );
82 }
83
84 public function getParamDescription() {
85 return array(
86 'type' => 'Type of token(s) to request'
87 );
88 }
89
90 public function getDescription() {
91 return 'Gets tokens for data-modifying actions.';
92 }
93
94 protected function getExamples() {
95 return array(
96 'api.php?action=tokens' => 'Retrieve an edit token (the default)',
97 'api.php?action=tokens&type=email|move' => 'Retrieve an email token and a move token'
98 );
99 }
100 }