2021-01-26 23:18:22 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools;
|
|
|
|
|
|
|
|
use ApiBase;
|
|
|
|
use ApiMain;
|
2021-09-07 15:25:56 +00:00
|
|
|
use ConfigFactory;
|
2021-01-26 23:18:22 +00:00
|
|
|
use Title;
|
|
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
|
|
|
|
|
|
|
class ApiDiscussionToolsSubscribe extends ApiBase {
|
|
|
|
|
|
|
|
/** @var SubscriptionStore */
|
2021-09-07 15:25:56 +00:00
|
|
|
private $subscriptionStore;
|
|
|
|
|
|
|
|
/** @var ConfigFactory */
|
|
|
|
private $configFactory;
|
2021-01-26 23:18:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ApiMain $main
|
|
|
|
* @param string $name
|
|
|
|
* @param SubscriptionStore $subscriptionStore
|
2021-09-07 15:25:56 +00:00
|
|
|
* @param ConfigFactory $configFactory
|
2021-01-26 23:18:22 +00:00
|
|
|
*/
|
2021-09-07 15:25:56 +00:00
|
|
|
public function __construct(
|
|
|
|
ApiMain $main,
|
|
|
|
$name,
|
|
|
|
SubscriptionStore $subscriptionStore,
|
|
|
|
ConfigFactory $configFactory
|
|
|
|
) {
|
2021-01-26 23:18:22 +00:00
|
|
|
parent::__construct( $main, $name );
|
|
|
|
$this->subscriptionStore = $subscriptionStore;
|
2021-09-07 15:25:56 +00:00
|
|
|
$this->configFactory = $configFactory;
|
2021-01-26 23:18:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function execute() {
|
2021-09-07 15:25:56 +00:00
|
|
|
$dtConfig = $this->configFactory->makeConfig( 'discussiontools' );
|
2021-06-07 20:45:54 +00:00
|
|
|
if ( !$dtConfig->get( 'DiscussionToolsEnableTopicSubscriptionBackend' ) ) {
|
2021-04-08 14:54:10 +00:00
|
|
|
$this->dieWithError( [ 'apierror-moduledisabled', $this->getModuleName() ] );
|
|
|
|
}
|
|
|
|
|
2021-01-26 23:18:22 +00:00
|
|
|
$user = $this->getUser();
|
2021-08-23 18:18:27 +00:00
|
|
|
if ( !$user->isRegistered() ) {
|
|
|
|
$this->dieWithError( 'apierror-mustbeloggedin-generic', 'notloggedin' );
|
2021-01-26 23:18:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$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 ) {
|
2021-04-27 20:21:50 +00:00
|
|
|
$success = $this->subscriptionStore->addSubscriptionForUser(
|
2021-01-26 23:18:22 +00:00
|
|
|
$user,
|
|
|
|
$title,
|
|
|
|
$commentName
|
|
|
|
);
|
2021-04-27 20:21:50 +00:00
|
|
|
if ( !$success ) {
|
|
|
|
$this->dieWithError( 'apierror-discussiontools-subscription-failed-add', 'subscription-failed' );
|
|
|
|
}
|
2021-01-26 23:18:22 +00:00
|
|
|
} else {
|
2021-04-27 20:21:50 +00:00
|
|
|
$success = $this->subscriptionStore->removeSubscriptionForUser(
|
2021-01-26 23:18:22 +00:00
|
|
|
$user,
|
|
|
|
$commentName
|
|
|
|
);
|
2021-04-27 20:21:50 +00:00
|
|
|
if ( !$success ) {
|
|
|
|
$this->dieWithError( 'apierror-discussiontools-subscription-failed-remove', 'subscription-failed' );
|
|
|
|
}
|
2021-01-26 23:18:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$result = [
|
|
|
|
'page' => $title,
|
|
|
|
'commentname' => $commentName,
|
2021-04-27 20:21:50 +00:00
|
|
|
'subscribe' => $subscribe,
|
2021-01-26 23:18:22 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|