Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
MessageGroupSubscriptionActionApi.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupProcessing;
5
6use ApiBase;
7use ApiMain;
8use StatusValue;
9use Wikimedia\ParamValidator\ParamValidator;
10
18class MessageGroupSubscriptionActionApi extends ApiBase {
19 private MessageGroupSubscription $groupSubscription;
20
21 public function __construct(
22 ApiMain $mainModule,
23 $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}
Manage user subscriptions to message groups and trigger notifications.
static getGroup(string $id)
Fetch a message group by id.