Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 48 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
MessageGroupSubscriptionActionApi | |
0.00% |
0 / 48 |
|
0.00% |
0 / 6 |
182 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
42 | |||
getAllowedParams | |
0.00% |
0 / 11 |
|
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 | |||
handleSubscriptionFailure | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\MessageGroupProcessing; |
5 | |
6 | use MediaWiki\Api\ApiBase; |
7 | use MediaWiki\Api\ApiMain; |
8 | use StatusValue; |
9 | use Wikimedia\ParamValidator\ParamValidator; |
10 | |
11 | /** |
12 | * API module for watching / stop watching a message group |
13 | * @since 2024.04 |
14 | * @author Abijeet Patro |
15 | * @license GPL-2.0-or-later |
16 | * @ingroup API TranslateAPI |
17 | */ |
18 | class MessageGroupSubscriptionActionApi extends ApiBase { |
19 | private MessageGroupSubscription $groupSubscription; |
20 | |
21 | public function __construct( |
22 | ApiMain $mainModule, |
23 | string $moduleName, |
24 | MessageGroupSubscription $groupSubscription |
25 | ) { |
26 | parent::__construct( $mainModule, $moduleName ); |
27 | $this->groupSubscription = $groupSubscription; |
28 | } |
29 | |
30 | public function execute(): void { |
31 | if ( !$this->groupSubscription->isEnabled() ) { |
32 | $this->dieWithError( 'apierror-translate-messagegroupsubscription-disabled' ); |
33 | } |
34 | |
35 | $params = $this->extractRequestParams(); |
36 | |
37 | $groupId = $params['groupId']; |
38 | $operation = $params['operation']; |
39 | |
40 | $group = MessageGroups::getGroup( $groupId ); |
41 | if ( $group === null ) { |
42 | $this->dieWithError( 'apierror-translate-invalidgroup', 'invalidgroup' ); |
43 | } |
44 | |
45 | $user = $this->getUser(); |
46 | if ( $operation === 'subscribe' ) { |
47 | $status = $this->groupSubscription->subscribeToGroup( $group, $user ); |
48 | if ( !$status->isOK() ) { |
49 | $this->handleSubscriptionFailure( $status ); |
50 | } |
51 | } elseif ( $operation === 'unsubscribe' ) { |
52 | $this->groupSubscription->unsubscribeFromGroup( $group, $user ); |
53 | } |
54 | |
55 | $this->getResult()->addValue( |
56 | null, |
57 | $this->getModuleName(), |
58 | [ |
59 | 'success' => 1, |
60 | 'group' => [ |
61 | 'id' => $groupId, |
62 | 'label' => $group->getLabel( $this->getContext() ) |
63 | ] |
64 | ] |
65 | ); |
66 | } |
67 | |
68 | protected function getAllowedParams(): array { |
69 | return [ |
70 | 'groupId' => [ |
71 | ParamValidator::PARAM_TYPE => 'string', |
72 | ParamValidator::PARAM_REQUIRED => true, |
73 | ], |
74 | 'operation' => [ |
75 | ParamValidator::PARAM_TYPE => [ 'subscribe', 'unsubscribe' ], |
76 | ParamValidator::PARAM_ISMULTI => false, |
77 | ParamValidator::PARAM_REQUIRED => true, |
78 | ] |
79 | ]; |
80 | } |
81 | |
82 | public function isInternal(): bool { |
83 | return true; |
84 | } |
85 | |
86 | public function needsToken(): string { |
87 | return 'csrf'; |
88 | } |
89 | |
90 | private function handleSubscriptionFailure( StatusValue $status ): void { |
91 | if ( $status->hasMessage( MessageGroupSubscription::NOT_ENABLED ) ) { |
92 | $this->dieWithError( 'apierror-translate-messagegroupsubscription-disabled' ); |
93 | } elseif ( $status->hasMessage( MessageGroupSubscription::UNNAMED_USER_UNSUPPORTED ) ) { |
94 | $this->dieWithError( |
95 | [ 'apierror-mustbeloggedin', $this->msg( 'action-translate-watch-message-group' ) ] |
96 | ); |
97 | } else { |
98 | $this->dieWithError( 'apierror-translate-messagegroupsubscription-dynamic-group-unsupported' ); |
99 | } |
100 | } |
101 | } |