2020-05-15 17:19:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace EchoPush\Api;
|
|
|
|
|
|
|
|
use ApiBase;
|
|
|
|
use ApiMain;
|
2020-08-11 20:34:21 +00:00
|
|
|
use ApiUsageException;
|
2020-05-15 17:19:03 +00:00
|
|
|
use EchoPush\SubscriptionManager;
|
2020-08-11 20:34:21 +00:00
|
|
|
use EchoPush\Utils;
|
2020-05-15 17:19:03 +00:00
|
|
|
use EchoServices;
|
|
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
|
|
|
|
|
|
|
class ApiEchoPushSubscriptionsDelete extends ApiBase {
|
|
|
|
|
|
|
|
/** @var ApiBase */
|
|
|
|
private $parent;
|
|
|
|
|
|
|
|
/** @var SubscriptionManager */
|
|
|
|
private $subscriptionManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Static entry point for initializing the module
|
|
|
|
* @param ApiBase $parent Parent module
|
|
|
|
* @param string $name Module name
|
|
|
|
* @return ApiEchoPushSubscriptionsDelete
|
|
|
|
*/
|
2021-07-24 04:10:29 +00:00
|
|
|
public static function factory( ApiBase $parent, string $name ): ApiEchoPushSubscriptionsDelete {
|
2020-05-15 17:19:03 +00:00
|
|
|
$subscriptionManager = EchoServices::getInstance()->getPushSubscriptionManager();
|
|
|
|
$module = new self( $parent->getMain(), $name, $subscriptionManager );
|
|
|
|
$module->parent = $parent;
|
|
|
|
return $module;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ApiMain $mainModule
|
|
|
|
* @param string $moduleName
|
|
|
|
* @param SubscriptionManager $subscriptionManager
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
ApiMain $mainModule,
|
|
|
|
string $moduleName,
|
|
|
|
SubscriptionManager $subscriptionManager
|
|
|
|
) {
|
|
|
|
parent::__construct( $mainModule, $moduleName );
|
|
|
|
$this->subscriptionManager = $subscriptionManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Entry point for executing the module.
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function execute(): void {
|
2020-08-11 20:34:21 +00:00
|
|
|
$tokens = $this->getParameter( 'providertoken' );
|
|
|
|
$userId = null;
|
|
|
|
|
2020-11-09 16:17:02 +00:00
|
|
|
if ( !$tokens ) {
|
|
|
|
$this->dieWithError( [ 'apierror-paramempty', 'providertoken' ], 'paramempty_providertoken' );
|
|
|
|
}
|
2020-08-11 20:34:21 +00:00
|
|
|
// Restrict deletion to the user's own token(s) if not a push subscription manager
|
|
|
|
try {
|
|
|
|
$this->checkUserRightsAny( 'manage-all-push-subscriptions' );
|
|
|
|
} catch ( ApiUsageException $e ) {
|
|
|
|
$userId = Utils::getPushUserId( $this->getUser() );
|
|
|
|
}
|
|
|
|
|
|
|
|
$numRowsDeleted = $this->subscriptionManager->delete( $tokens, $userId );
|
2020-05-15 17:19:03 +00:00
|
|
|
if ( $numRowsDeleted == 0 ) {
|
|
|
|
$this->dieWithError( 'apierror-echo-push-token-not-found' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the parent module.
|
|
|
|
* @return ApiBase
|
|
|
|
*/
|
|
|
|
public function getParent(): ApiBase {
|
|
|
|
return $this->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
protected function getAllowedParams(): array {
|
|
|
|
return [
|
|
|
|
'providertoken' => [
|
|
|
|
ParamValidator::PARAM_TYPE => 'string',
|
|
|
|
ParamValidator::PARAM_REQUIRED => true,
|
2020-08-11 20:34:21 +00:00
|
|
|
ParamValidator::PARAM_ISMULTI => true,
|
2020-05-15 17:19:03 +00:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
protected function getExamplesMessages(): array {
|
|
|
|
return [
|
|
|
|
"action=echopushsubscriptions&command=delete&providertoken=ABC123" =>
|
|
|
|
"apihelp-echopushsubscriptions+delete-example"
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
// The parent module already enforces these but they make documentation nicer.
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
public function isWriteMode(): bool {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
public function mustBePosted(): bool {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
public function isInternal(): bool {
|
|
|
|
// experimental!
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|