mediawiki-extensions-Discus.../includes/ApiDiscussionToolsSubscribe.php
Ed Sanders 6d655dee0a Remove DiscussionToolsEnableTopicSubscriptionBackend config
This is now deployed on all wikis, and going forward I don't think
we need to make this configurable.

Change-Id: I231976267ba6cdfeec622efaa15983a84c330649
2022-02-04 18:22:10 +00:00

118 lines
2.3 KiB
PHP

<?php
namespace MediaWiki\Extension\DiscussionTools;
use ApiBase;
use ApiMain;
use Title;
use Wikimedia\ParamValidator\ParamValidator;
class ApiDiscussionToolsSubscribe extends ApiBase {
/** @var SubscriptionStore */
private $subscriptionStore;
/**
* @param ApiMain $main
* @param string $name
* @param SubscriptionStore $subscriptionStore
*/
public function __construct(
ApiMain $main,
$name,
SubscriptionStore $subscriptionStore
) {
parent::__construct( $main, $name );
$this->subscriptionStore = $subscriptionStore;
}
/**
* @inheritDoc
*/
public function execute() {
$user = $this->getUser();
if ( !$user->isRegistered() ) {
$this->dieWithError( 'apierror-mustbeloggedin-generic', 'notloggedin' );
}
$params = $this->extractRequestParams();
$title = Title::newFromText( $params['page'] );
$result = null;
if ( !$title ) {
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['page'] ) ] );
}
$commentName = $params['commentname'];
$subscribe = $params['subscribe'];
if ( $subscribe ) {
$success = $this->subscriptionStore->addSubscriptionForUser(
$user,
$title,
$commentName
);
if ( !$success ) {
$this->dieWithError( 'apierror-discussiontools-subscription-failed-add', 'subscription-failed' );
}
} else {
$success = $this->subscriptionStore->removeSubscriptionForUser(
$user,
$commentName
);
if ( !$success ) {
$this->dieWithError( 'apierror-discussiontools-subscription-failed-remove', 'subscription-failed' );
}
}
$result = [
'page' => $title,
'commentname' => $commentName,
'subscribe' => $subscribe,
];
$this->getResult()->addValue( null, $this->getModuleName(), $result );
}
/**
* @inheritDoc
*/
public function getAllowedParams() {
return [
'page' => [
ParamValidator::PARAM_REQUIRED => true,
],
'token' => [
ParamValidator::PARAM_REQUIRED => true,
],
'commentname' => [
ParamValidator::PARAM_REQUIRED => true,
],
'subscribe' => [
ParamValidator::PARAM_TYPE => 'boolean',
ParamValidator::PARAM_REQUIRED => true,
],
];
}
/**
* @inheritDoc
*/
public function needsToken() {
return 'csrf';
}
/**
* @inheritDoc
*/
public function isInternal() {
return true;
}
/**
* @inheritDoc
*/
public function isWriteMode() {
return true;
}
}