Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 48 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| ApiDiscussionToolsSubscribe | |
0.00% |
0 / 48 |
|
0.00% |
0 / 5 |
110 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
42 | |||
| getAllowedParams | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
2 | |||
| needsToken | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isWriteMode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 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 | 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 | $result = null; |
| 34 | |
| 35 | if ( !$title ) { |
| 36 | $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['page'] ) ] ); |
| 37 | } |
| 38 | $commentName = $params['commentname']; |
| 39 | $subscribe = $params['subscribe']; |
| 40 | |
| 41 | if ( $subscribe ) { |
| 42 | $success = $this->subscriptionStore->addSubscriptionForUser( |
| 43 | $user, |
| 44 | $title, |
| 45 | $commentName |
| 46 | ); |
| 47 | if ( !$success ) { |
| 48 | $this->dieWithError( 'apierror-discussiontools-subscription-failed-add', 'subscription-failed' ); |
| 49 | } |
| 50 | } else { |
| 51 | $success = $this->subscriptionStore->removeSubscriptionForUser( |
| 52 | $user, |
| 53 | $commentName |
| 54 | ); |
| 55 | if ( !$success ) { |
| 56 | $this->dieWithError( 'apierror-discussiontools-subscription-failed-remove', 'subscription-failed' ); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | $result = [ |
| 61 | 'page' => $title, |
| 62 | 'commentname' => $commentName, |
| 63 | 'subscribe' => $subscribe, |
| 64 | ]; |
| 65 | |
| 66 | $this->getResult()->addValue( null, $this->getModuleName(), $result ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @inheritDoc |
| 71 | */ |
| 72 | public function getAllowedParams() { |
| 73 | return [ |
| 74 | 'page' => [ |
| 75 | ParamValidator::PARAM_REQUIRED => true, |
| 76 | ], |
| 77 | 'token' => [ |
| 78 | ParamValidator::PARAM_REQUIRED => true, |
| 79 | ], |
| 80 | 'commentname' => [ |
| 81 | ParamValidator::PARAM_REQUIRED => true, |
| 82 | ], |
| 83 | 'subscribe' => [ |
| 84 | ParamValidator::PARAM_TYPE => 'boolean', |
| 85 | ParamValidator::PARAM_REQUIRED => true, |
| 86 | ], |
| 87 | ]; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @inheritDoc |
| 92 | */ |
| 93 | public function needsToken() { |
| 94 | return 'csrf'; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @inheritDoc |
| 99 | */ |
| 100 | public function isWriteMode() { |
| 101 | return true; |
| 102 | } |
| 103 | } |