Debian Codegraph @

extensions/Echo/includes/Push/Api/ApiEchoPushSubscriptionsDelete.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Notifications\Push\Api;
4
5 use MediaWiki\Api\ApiBase;
6 use MediaWiki\Api\ApiMain;
7 use MediaWiki\Api\ApiUsageException;
8 use MediaWiki\Extension\Notifications\Push\SubscriptionManager;
9 use MediaWiki\Extension\Notifications\Push\Utils;
10 use MediaWiki\Extension\Notifications\Services;
11 use Wikimedia\ParamValidator\ParamValidator;
12
13 class ApiEchoPushSubscriptionsDelete extends ApiBase {
14
15 /** @var ApiBase */
16 private $parent;
17
18 /** @var SubscriptionManager */
19 private $subscriptionManager;
20
21 /**
22 * Static entry point for initializing the module
23 * @param ApiBase $parent Parent module
24 * @param string $name Module name
25 * @return ApiEchoPushSubscriptionsDelete
26 */
27 public static function factory( ApiBase $parent, string $name ): ApiEchoPushSubscriptionsDelete {
28 $subscriptionManager = Services::getInstance()->getPushSubscriptionManager();
29 $module = new self( $parent->getMain(), $name, $subscriptionManager );
30 $module->parent = $parent;
31 return $module;
32 }
33
34 public function __construct(
35 ApiMain $mainModule,
36 string $moduleName,
37 SubscriptionManager $subscriptionManager
38 ) {
39 parent::__construct( $mainModule, $moduleName );
40 $this->subscriptionManager = $subscriptionManager;
41 }
42
43 /**
44 * Entry point for executing the module.
45 * @inheritDoc
46 */
47 public function execute(): void {
48 $tokens = $this->getParameter( 'providertoken' );
49 $userId = null;
50
51 if ( !$tokens ) {
52 $this->dieWithError( [ 'apierror-paramempty', 'providertoken' ], 'paramempty_providertoken' );
53 }
54 // Restrict deletion to the user's own token(s) if not a push subscription manager
55 try {
56 $this->checkUserRightsAny( 'manage-all-push-subscriptions' );
57 } catch ( ApiUsageException $e ) {
58 $userId = Utils::getPushUserId( $this->getUser() );
59 }
60
61 $numRowsDeleted = $this->subscriptionManager->delete( $tokens, $userId );
62 if ( $numRowsDeleted == 0 ) {
63 $this->dieWithError( 'apierror-echo-push-token-not-found' );
64 }
65 }
66
67 /**
68 * Get the parent module.
69 * @return ApiBase
70 */
71 public function getParent(): ApiBase {
72 return $this->parent;
73 }
74
75 /** @inheritDoc */
76 protected function getAllowedParams(): array {
77 return [
78 'providertoken' => [
79 ParamValidator::PARAM_TYPE => 'string',
80 ParamValidator::PARAM_REQUIRED => true,
81 ParamValidator::PARAM_ISMULTI => true,
82 ],
83 ];
84 }
85
86 /** @inheritDoc */
87 protected function getExamplesMessages(): array {
88 return [
89 "action=echopushsubscriptions&command=delete&providertoken=ABC123" =>
90 "apihelp-echopushsubscriptions+delete-example"
91 ];
92 }
93
94 // The parent module already enforces these but they make documentation nicer.
95
96 /** @inheritDoc */
97 public function isWriteMode(): bool {
98 return true;
99 }
100
101 /** @inheritDoc */
102 public function mustBePosted(): bool {
103 return true;
104 }
105
106 /** @inheritDoc */
107 public function isInternal(): bool {
108 // experimental!
109 return true;
110 }
111
112 }