mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/DiscussionTools
synced 2024-11-24 00:13:36 +00:00
db53ae0a48
Bug: T329474 Change-Id: Ie8e745fc83a320134d3790012311c4b35c7f3934
107 lines
2.2 KiB
PHP
107 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools;
|
|
|
|
use ApiBase;
|
|
use ApiMain;
|
|
use ApiUsageException;
|
|
use Title;
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
|
|
|
class ApiDiscussionToolsSubscribe extends ApiBase {
|
|
|
|
private SubscriptionStore $subscriptionStore;
|
|
|
|
public function __construct(
|
|
ApiMain $main,
|
|
string $name,
|
|
SubscriptionStore $subscriptionStore
|
|
) {
|
|
parent::__construct( $main, $name );
|
|
$this->subscriptionStore = $subscriptionStore;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
* @throws ApiUsageException
|
|
*/
|
|
public function execute() {
|
|
$user = $this->getUser();
|
|
if ( !$user->isNamed() ) {
|
|
$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 isWriteMode() {
|
|
return true;
|
|
}
|
|
}
|