Debian Codegraph @

extensions/DiscussionTools/includes/ApiDiscussionToolsSubscribe.php

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