Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiDiscussionToolsGetSubscriptions
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 3
30
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 / 13
0.00% covered (danger)
0.00%
0 / 1
12
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 6
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 Wikimedia\ParamValidator\ParamValidator;
9
10class ApiDiscussionToolsGetSubscriptions extends ApiBase {
11
12    public function __construct(
13        ApiMain $main,
14        string $name,
15        private readonly SubscriptionStore $subscriptionStore,
16    ) {
17        parent::__construct( $main, $name );
18    }
19
20    /**
21     * @inheritDoc
22     * @throws ApiUsageException
23     */
24    public function execute() {
25        $user = $this->getUser();
26        if ( !$user->isNamed() ) {
27            $this->dieWithError( 'apierror-mustbeloggedin-generic', 'notloggedin' );
28        }
29
30        $params = $this->extractRequestParams();
31        $itemNames = $params['commentname'];
32        $items = $this->subscriptionStore->getSubscriptionItemsForUser(
33            $user,
34            $itemNames
35        );
36
37        // Ensure consistent formatting in JSON and XML formats
38        $this->getResult()->addIndexedTagName( 'subscriptions', 'subscription' );
39        $this->getResult()->addArrayType( 'subscriptions', 'kvp', 'name' );
40
41        foreach ( $items as $item ) {
42            $this->getResult()->addValue( 'subscriptions', $item->getItemName(), $item->getState() );
43        }
44    }
45
46    /**
47     * @inheritDoc
48     */
49    public function getAllowedParams() {
50        return [
51            'commentname' => [
52                ParamValidator::PARAM_REQUIRED => true,
53                ParamValidator::PARAM_ISMULTI => true,
54            ],
55        ];
56    }
57}