Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiDiscussionToolsSubscribe
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 5
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
42
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
2
 needsToken
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isWriteMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\DiscussionTools;
4
5use MediaWiki\Api\ApiBase;
6use MediaWiki\Api\ApiMain;
7use MediaWiki\Api\ApiUsageException;
8use MediaWiki\Title\Title;
9use Wikimedia\ParamValidator\ParamValidator;
10
11class ApiDiscussionToolsSubscribe extends ApiBase {
12
13    public function __construct(
14        ApiMain $main,
15        string $name,
16        private readonly SubscriptionStore $subscriptionStore,
17    ) {
18        parent::__construct( $main, $name );
19    }
20
21    /**
22     * @inheritDoc
23     * @throws ApiUsageException
24     */
25    public function execute() {
26        $user = $this->getUser();
27        if ( !$user->isNamed() ) {
28            $this->dieWithError( 'apierror-mustbeloggedin-generic', 'notloggedin' );
29        }
30
31        $params = $this->extractRequestParams();
32        $title = Title::newFromText( $params['page'] );
33        if ( !$title ) {
34            $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['page'] ) ] );
35        }
36        $commentName = $params['commentname'];
37        $subscribe = $params['subscribe'];
38
39        if ( $subscribe ) {
40            $success = $this->subscriptionStore->addSubscriptionForUser(
41                $user,
42                $title,
43                $commentName
44            );
45            if ( !$success ) {
46                $this->dieWithError( 'apierror-discussiontools-subscription-failed-add', 'subscription-failed' );
47            }
48        } else {
49            $success = $this->subscriptionStore->removeSubscriptionForUser(
50                $user,
51                $commentName
52            );
53            if ( !$success ) {
54                $this->dieWithError( 'apierror-discussiontools-subscription-failed-remove', 'subscription-failed' );
55            }
56        }
57
58        $result = [
59            'page' => $title,
60            'commentname' => $commentName,
61            'subscribe' => $subscribe,
62        ];
63
64        $this->getResult()->addValue( null, $this->getModuleName(), $result );
65    }
66
67    /**
68     * @inheritDoc
69     */
70    public function getAllowedParams() {
71        return [
72            'page' => [
73                ParamValidator::PARAM_REQUIRED => true,
74            ],
75            'token' => [
76                ParamValidator::PARAM_REQUIRED => true,
77            ],
78            'commentname' => [
79                ParamValidator::PARAM_REQUIRED => true,
80            ],
81            'subscribe' => [
82                ParamValidator::PARAM_TYPE => 'boolean',
83                ParamValidator::PARAM_REQUIRED => true,
84            ],
85        ];
86    }
87
88    /**
89     * @inheritDoc
90     */
91    public function needsToken() {
92        return 'csrf';
93    }
94
95    /**
96     * @inheritDoc
97     */
98    public function isWriteMode() {
99        return true;
100    }
101}