Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / api / ApiQueryTokens.php
blob2e107acb88a6a4445316d29bfd33f6822538d89b
1 <?php
2 /**
3 * Module to fetch tokens via action=query&meta=tokens
5 * Created on August 8, 2014
7 * Copyright © 2014 Brad Jorsch bjorsch@wikimedia.org
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.
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.
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
24 * @file
25 * @since 1.24
28 /**
29 * Module to fetch tokens via action=query&meta=tokens
31 * @ingroup API
32 * @since 1.24
34 class ApiQueryTokens extends ApiQueryBase {
36 public function execute() {
37 $params = $this->extractRequestParams();
38 $res = array();
40 if ( $this->getMain()->getRequest()->getVal( 'callback' ) !== null ) {
41 $this->setWarning( 'Tokens may not be obtained when using a callback' );
42 return;
45 $salts = self::getTokenTypeSalts();
46 foreach ( $params['type'] as $type ) {
47 $salt = $salts[$type];
48 $val = $this->getUser()->getEditToken( $salt, $this->getRequest() );
49 $res[$type . 'token'] = $val;
52 $this->getResult()->addValue( 'query', $this->getModuleName(), $res );
55 public static function getTokenTypeSalts() {
56 static $salts = null;
57 if ( !$salts ) {
58 $salts = array(
59 'csrf' => '',
60 'watch' => 'watch',
61 'patrol' => 'patrol',
62 'rollback' => 'rollback',
63 'userrights' => 'userrights',
65 Hooks::run( 'ApiQueryTokensRegisterTypes', array( &$salts ) );
66 ksort( $salts );
69 return $salts;
72 public function getAllowedParams() {
73 return array(
74 'type' => array(
75 ApiBase::PARAM_DFLT => 'csrf',
76 ApiBase::PARAM_ISMULTI => true,
77 ApiBase::PARAM_TYPE => array_keys( self::getTokenTypeSalts() ),
82 protected function getExamplesMessages() {
83 return array(
84 'action=query&meta=tokens'
85 => 'apihelp-query+tokens-example-simple',
86 'action=query&meta=tokens&type=watch|patrol'
87 => 'apihelp-query+tokens-example-types',
91 public function getCacheMode( $params ) {
92 return 'private';