]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiTokens.php
MediaWiki 1.30.2-scripts2
[autoinstalls/mediawiki.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  * @deprecated since 1.24
29  * @ingroup API
30  */
31 class ApiTokens extends ApiBase {
32
33         public function execute() {
34                 $this->addDeprecation(
35                         [ 'apiwarn-deprecation-withreplacement', 'action=tokens', 'action=query&meta=tokens' ],
36                         'action=tokens'
37                 );
38
39                 $params = $this->extractRequestParams();
40                 $res = [
41                         ApiResult::META_TYPE => 'assoc',
42                 ];
43
44                 $types = $this->getTokenTypes();
45                 foreach ( $params['type'] as $type ) {
46                         $val = call_user_func( $types[$type], null, null );
47
48                         if ( $val === false ) {
49                                 $this->addWarning( [ 'apiwarn-tokennotallowed', $type ] );
50                         } else {
51                                 $res[$type . 'token'] = $val;
52                         }
53                 }
54
55                 $this->getResult()->addValue( null, $this->getModuleName(), $res );
56         }
57
58         private function getTokenTypes() {
59                 // If we're in a mode that breaks the same-origin policy, no tokens can
60                 // be obtained
61                 if ( $this->lacksSameOriginSecurity() ) {
62                         return [];
63                 }
64
65                 static $types = null;
66                 if ( $types ) {
67                         return $types;
68                 }
69                 $types = [ 'patrol' => [ 'ApiQueryRecentChanges', 'getPatrolToken' ] ];
70                 $names = [ 'edit', 'delete', 'protect', 'move', 'block', 'unblock',
71                         'email', 'import', 'watch', 'options' ];
72                 foreach ( $names as $name ) {
73                         $types[$name] = [ 'ApiQueryInfo', 'get' . ucfirst( $name ) . 'Token' ];
74                 }
75                 Hooks::run( 'ApiTokensGetTokenTypes', [ &$types ] );
76
77                 // For forwards-compat, copy any token types from ApiQueryTokens that
78                 // we don't already have something for.
79                 $user = $this->getUser();
80                 $request = $this->getRequest();
81                 foreach ( ApiQueryTokens::getTokenTypeSalts() as $name => $salt ) {
82                         if ( !isset( $types[$name] ) ) {
83                                 $types[$name] = function () use ( $salt, $user, $request ) {
84                                         return ApiQueryTokens::getToken( $user, $request->getSession(), $salt )->toString();
85                                 };
86                         }
87                 }
88
89                 ksort( $types );
90
91                 return $types;
92         }
93
94         public function isDeprecated() {
95                 return true;
96         }
97
98         public function getAllowedParams() {
99                 return [
100                         'type' => [
101                                 ApiBase::PARAM_DFLT => 'edit',
102                                 ApiBase::PARAM_ISMULTI => true,
103                                 ApiBase::PARAM_TYPE => array_keys( $this->getTokenTypes() ),
104                         ],
105                 ];
106         }
107
108         protected function getExamplesMessages() {
109                 return [
110                         'action=tokens'
111                                 => 'apihelp-tokens-example-edit',
112                         'action=tokens&type=email|move'
113                                 => 'apihelp-tokens-example-emailmove',
114                 ];
115         }
116 }