Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 34 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
ApiULSSetLanguage | |
0.00% |
0 / 34 |
|
0.00% |
0 / 5 |
90 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
30 | |||
getAllowedParams | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
isInternal | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
needsToken | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Update user's preferred language. |
4 | * |
5 | * Copyright (C) 2012 Alolita Sharma, Amir Aharoni, Arun Ganesh, Brandon Harris, |
6 | * Niklas Laxström, Pau Giner, Santhosh Thottingal, Siebrand Mazeland and other |
7 | * contributors. See CREDITS for a list. |
8 | * |
9 | * UniversalLanguageSelector is dual licensed GPLv2 or later and MIT. You don't |
10 | * have to do anything special to choose one license or the other and you don't |
11 | * have to notify anyone which license you are using. You are free to use |
12 | * UniversalLanguageSelector in commercial projects as long as the copyright |
13 | * header is left intact. See files GPL-LICENSE and MIT-LICENSE for details. |
14 | * |
15 | * @file |
16 | * @ingroup Extensions |
17 | * @license GPL-2.0-or-later |
18 | * @license MIT |
19 | */ |
20 | |
21 | namespace UniversalLanguageSelector\Api; |
22 | |
23 | use ApiBase; |
24 | use ApiMain; |
25 | use DeferredUpdates; |
26 | use MediaWiki\Languages\LanguageNameUtils; |
27 | use MediaWiki\User\UserOptionsManager; |
28 | use Wikimedia\ParamValidator\ParamValidator; |
29 | |
30 | /** |
31 | * @ingroup API |
32 | */ |
33 | class ApiULSSetLanguage extends ApiBase { |
34 | /** @var UserOptionsManager */ |
35 | private $userOptionsManager; |
36 | /** @var LanguageNameUtils */ |
37 | private $languageNameUtils; |
38 | |
39 | /** |
40 | * @param ApiMain $main |
41 | * @param string $action |
42 | * @param UserOptionsManager $userOptionsManager |
43 | * @param LanguageNameUtils $languageNameUtils |
44 | */ |
45 | public function __construct( |
46 | ApiMain $main, |
47 | $action, |
48 | UserOptionsManager $userOptionsManager, |
49 | LanguageNameUtils $languageNameUtils |
50 | ) { |
51 | parent::__construct( $main, $action ); |
52 | $this->userOptionsManager = $userOptionsManager; |
53 | $this->languageNameUtils = $languageNameUtils; |
54 | } |
55 | |
56 | /** |
57 | * @inheritDoc |
58 | */ |
59 | public function execute() { |
60 | $request = $this->getRequest(); |
61 | if ( !$request->wasPosted() ) { |
62 | $this->dieWithError( [ 'apierror-mustbeposted', $request->getText( 'action' ) ] ); |
63 | } |
64 | |
65 | $languageCode = $request->getRawVal( 'languagecode', '' ); |
66 | if ( !$this->languageNameUtils->isSupportedLanguage( $languageCode ) ) { |
67 | $this->dieWithError( |
68 | [ 'apierror-invalidlang', $this->encodeParamName( 'languagecode' ) ] |
69 | ); |
70 | } |
71 | |
72 | $user = $this->getUser(); |
73 | if ( !$user->isNamed() ) { |
74 | if ( $this->getConfig()->get( 'ULSAnonCanChangeLanguage' ) ) { |
75 | // Anonymous users can change language. |
76 | // Use a cookie that also can changed by JavaScript. |
77 | $request->response()->setCookie( |
78 | 'language', |
79 | $languageCode, |
80 | 0, |
81 | [ 'httpOnly' => false ] |
82 | ); |
83 | return; |
84 | } |
85 | |
86 | $this->dieWithError( [ 'apierror-ulssetlang-anon-notallowed' ] ); |
87 | } |
88 | |
89 | $updateUser = $user->getInstanceForUpdate(); |
90 | $this->userOptionsManager->setOption( $updateUser, 'language', $languageCode ); |
91 | // Sync the DB on post-send |
92 | DeferredUpdates::addCallableUpdate( static function () use ( $updateUser ) { |
93 | $updateUser->saveSettings(); |
94 | } ); |
95 | } |
96 | |
97 | /** |
98 | * @inheritDoc |
99 | */ |
100 | public function getAllowedParams() { |
101 | return [ |
102 | 'languagecode' => [ |
103 | ParamValidator::PARAM_REQUIRED => true, |
104 | ] |
105 | ]; |
106 | } |
107 | |
108 | /** |
109 | * @inheritDoc |
110 | */ |
111 | public function isInternal() { |
112 | // Try to scare people away from using this externally |
113 | return true; |
114 | } |
115 | |
116 | /** |
117 | * @inheritDoc |
118 | */ |
119 | public function needsToken() { |
120 | return 'csrf'; |
121 | } |
122 | } |