| 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 |
|
| 16 |
private $parent; |
| 17 |
|
| 18 |
|
| 19 |
private $subscriptionManager; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 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 |
|
| 45 |
|
| 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 |
|
| 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 |
|
| 69 |
|
| 70 |
|
| 71 |
public function getParent(): ApiBase { |
| 72 |
return $this->parent; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 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 |
|
| 87 |
protected function getExamplesMessages(): array { |
| 88 |
return [ |
| 89 |
"action=echopushsubscriptions&command=delete&providertoken=ABC123" => |
| 90 |
"apihelp-echopushsubscriptions+delete-example" |
| 91 |
]; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public function isWriteMode(): bool { |
| 98 |
return true; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
public function mustBePosted(): bool { |
| 103 |
return true; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
public function isInternal(): bool { |
| 108 |
|
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
} |