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