2013-03-18 19:56:12 +00:00
|
|
|
<?php
|
2018-11-10 21:06:01 +00:00
|
|
|
|
2023-05-19 11:38:58 +00:00
|
|
|
namespace MediaWiki\Extension\Thanks\Api;
|
2021-03-14 03:43:32 +00:00
|
|
|
|
|
|
|
use ApiBase;
|
2023-05-19 11:38:58 +00:00
|
|
|
use ApiMain;
|
|
|
|
use MediaWiki\Extension\Thanks\Storage\LogStore;
|
|
|
|
use MediaWiki\Permissions\PermissionManager;
|
2021-03-14 03:43:32 +00:00
|
|
|
use Title;
|
|
|
|
use User;
|
2019-04-26 16:12:19 +00:00
|
|
|
|
2013-04-05 23:47:49 +00:00
|
|
|
/**
|
2014-02-26 02:12:47 +00:00
|
|
|
* Base API module for Thanks
|
2013-04-05 23:47:49 +00:00
|
|
|
*
|
|
|
|
* @ingroup API
|
|
|
|
* @ingroup Extensions
|
|
|
|
*/
|
2014-02-26 02:12:47 +00:00
|
|
|
abstract class ApiThank extends ApiBase {
|
2023-05-19 11:38:58 +00:00
|
|
|
|
|
|
|
protected PermissionManager $permissionManager;
|
|
|
|
protected LogStore $storage;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
ApiMain $main,
|
|
|
|
$action,
|
|
|
|
PermissionManager $permissionManager,
|
|
|
|
LogStore $storage
|
|
|
|
) {
|
|
|
|
parent::__construct( $main, $action );
|
|
|
|
$this->permissionManager = $permissionManager;
|
|
|
|
$this->storage = $storage;
|
|
|
|
}
|
|
|
|
|
2014-02-26 02:12:47 +00:00
|
|
|
protected function dieOnBadUser( User $user ) {
|
2013-11-03 11:13:03 +00:00
|
|
|
if ( $user->isAnon() ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->dieWithError( 'thanks-error-notloggedin', 'notloggedin' );
|
2013-11-03 11:13:03 +00:00
|
|
|
} elseif ( $user->pingLimiter( 'thanks-notification' ) ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->dieWithError( [ 'thanks-error-ratelimited', $user->getName() ], 'ratelimited' );
|
2013-03-18 19:56:12 +00:00
|
|
|
}
|
2013-11-03 11:13:03 +00:00
|
|
|
}
|
|
|
|
|
2019-04-26 16:12:19 +00:00
|
|
|
/**
|
|
|
|
* Check whether the user is blocked from this title. (This is not the same
|
|
|
|
* as checking whether they are sitewide blocked, because a sitewide blocked
|
|
|
|
* user may still be allowed to thank on their own talk page.)
|
|
|
|
*
|
|
|
|
* This is separate from dieOnBadUser because we need to know the title.
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
* @param Title $title
|
|
|
|
*/
|
2021-04-20 15:41:21 +00:00
|
|
|
protected function dieOnUserBlockedFromTitle( User $user, Title $title ) {
|
2023-05-19 11:38:58 +00:00
|
|
|
if ( $this->permissionManager->isBlockedFrom( $user, $title ) ) {
|
2021-07-23 22:52:23 +00:00
|
|
|
// Block should definitely exist
|
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable
|
2019-04-26 16:12:19 +00:00
|
|
|
$this->dieBlocked( $user->getBlock() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the user is sitewide blocked.
|
|
|
|
*
|
2021-04-20 15:41:21 +00:00
|
|
|
* This is separate from dieOnUserBlockedFromTitle because we need to know if the thank
|
|
|
|
* is related to a revision. (If it is, then use dieOnUserBlockedFromTitle instead.)
|
2019-04-26 16:12:19 +00:00
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
*/
|
2021-04-20 15:41:21 +00:00
|
|
|
protected function dieOnUserBlockedFromThanks( User $user ) {
|
2019-04-26 16:12:19 +00:00
|
|
|
$block = $user->getBlock();
|
2021-04-20 15:41:21 +00:00
|
|
|
if (
|
|
|
|
$block &&
|
|
|
|
( $block->isSitewide() || $block->appliesToRight( 'thanks' ) )
|
|
|
|
) {
|
2019-04-26 16:12:19 +00:00
|
|
|
$this->dieBlocked( $block );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-26 02:12:47 +00:00
|
|
|
protected function dieOnBadRecipient( User $user, User $recipient ) {
|
|
|
|
if ( $user->getId() === $recipient->getId() ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->dieWithError( 'thanks-error-invalidrecipient-self', 'invalidrecipient' );
|
2019-12-28 20:37:59 +00:00
|
|
|
} elseif ( !$this->getConfig()->get( 'ThanksSendToBots' ) && $recipient->isBot() ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->dieWithError( 'thanks-error-invalidrecipient-bot', 'invalidrecipient' );
|
2013-11-24 06:45:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-15 04:28:10 +00:00
|
|
|
protected function markResultSuccess( $recipientName ) {
|
2016-04-22 20:13:56 +00:00
|
|
|
$this->getResult()->addValue( null, 'result', [
|
2014-04-15 04:28:10 +00:00
|
|
|
'success' => 1,
|
|
|
|
'recipient' => $recipientName,
|
2016-04-22 20:13:56 +00:00
|
|
|
] );
|
2014-04-15 04:28:10 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 00:53:40 +00:00
|
|
|
protected function haveAlreadyThanked( User $thanker, $uniqueId ) {
|
2023-05-19 11:38:58 +00:00
|
|
|
return $this->storage->haveThanked( $thanker, $uniqueId );
|
2015-06-16 00:53:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function logThanks( User $user, User $recipient, $uniqueId ) {
|
2023-05-19 11:38:58 +00:00
|
|
|
$this->storage->thank( $user, $recipient, $uniqueId );
|
2013-03-18 19:56:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function needsToken() {
|
2014-08-09 13:07:55 +00:00
|
|
|
return 'csrf';
|
2013-03-18 19:56:12 +00:00
|
|
|
}
|
|
|
|
|
2013-11-23 07:47:54 +00:00
|
|
|
public function isWriteMode() {
|
2017-09-24 19:54:35 +00:00
|
|
|
// Writes to the Echo database and sometimes log tables.
|
2013-11-23 07:47:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-03-18 19:56:12 +00:00
|
|
|
}
|