Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
MessageGroupSubscriptionActionApi.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupProcessing;
5
6use MediaWiki\Api\ApiBase;
7use MediaWiki\Api\ApiMain;
8use StatusValue;
9use Wikimedia\ParamValidator\ParamValidator;
10
18class MessageGroupSubscriptionActionApi extends ApiBase {
19
20 public function __construct(
21 ApiMain $mainModule,
22 string $moduleName,
23 private readonly MessageGroupSubscription $groupSubscription,
24 ) {
25 parent::__construct( $mainModule, $moduleName );
26 }
27
28 public function execute(): void {
29 if ( !$this->groupSubscription->isEnabled() ) {
30 $this->dieWithError( 'apierror-translate-messagegroupsubscription-disabled' );
31 }
32
33 $params = $this->extractRequestParams();
34
35 $groupId = $params['groupId'];
36 $operation = $params['operation'];
37
38 $group = MessageGroups::getGroup( $groupId );
39 if ( $group === null ) {
40 $this->dieWithError( 'apierror-translate-invalidgroup', 'invalidgroup' );
41 }
42
43 $user = $this->getUser();
44 if ( $operation === 'subscribe' ) {
45 $status = $this->groupSubscription->subscribeToGroup( $group, $user );
46 if ( !$status->isOK() ) {
47 $this->handleSubscriptionFailure( $status );
48 }
49 } elseif ( $operation === 'unsubscribe' ) {
50 $this->groupSubscription->unsubscribeFromGroup( $group, $user );
51 }
52
53 $this->getResult()->addValue(
54 null,
55 $this->getModuleName(),
56 [
57 'success' => 1,
58 'group' => [
59 'id' => $groupId,
60 'label' => $group->getLabel( $this->getContext() )
61 ]
62 ]
63 );
64 }
65
66 protected function getAllowedParams(): array {
67 return [
68 'groupId' => [
69 ParamValidator::PARAM_TYPE => 'string',
70 ParamValidator::PARAM_REQUIRED => true,
71 ],
72 'operation' => [
73 ParamValidator::PARAM_TYPE => [ 'subscribe', 'unsubscribe' ],
74 ParamValidator::PARAM_ISMULTI => false,
75 ParamValidator::PARAM_REQUIRED => true,
76 ]
77 ];
78 }
79
80 public function isInternal(): bool {
81 return true;
82 }
83
84 public function needsToken(): string {
85 return 'csrf';
86 }
87
88 private function handleSubscriptionFailure( StatusValue $status ): never {
89 if ( $status->hasMessage( MessageGroupSubscription::NOT_ENABLED ) ) {
90 $this->dieWithError( 'apierror-translate-messagegroupsubscription-disabled' );
91 } elseif ( $status->hasMessage( MessageGroupSubscription::UNNAMED_USER_UNSUPPORTED ) ) {
92 $this->dieWithError(
93 [ 'apierror-mustbeloggedin', $this->msg( 'action-translate-watch-message-group' ) ]
94 );
95 } else {
96 $this->dieWithError( 'apierror-translate-messagegroupsubscription-dynamic-group-unsupported' );
97 }
98 }
99}
Manage user subscriptions to message groups and trigger notifications.
getGroup()
Get the primary MessageGroup this message belongs to.