2013-03-18 19:56:12 +00:00
|
|
|
<?php
|
2018-11-10 21:06:01 +00:00
|
|
|
|
2021-02-11 19:28:29 +00:00
|
|
|
use MediaWiki\CheckUser\Hooks;
|
2019-04-26 16:12:19 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
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 {
|
|
|
|
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' );
|
2018-07-12 13:51:00 +00:00
|
|
|
} elseif ( $user->isBlockedGlobally() ) {
|
|
|
|
$this->dieBlocked( $user->getGlobalBlock() );
|
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
|
|
|
|
*/
|
|
|
|
protected function dieOnBlockedUser( User $user, Title $title ) {
|
|
|
|
$permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
|
|
|
|
if ( $permissionManager->isBlockedFrom( $user, $title ) ) {
|
|
|
|
$this->dieBlocked( $user->getBlock() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the user is sitewide blocked.
|
|
|
|
*
|
|
|
|
* This is separate from dieOnBlockedUser because we need to know if the thank
|
|
|
|
* is related to a revision. (If it is, then use dieOnBlockedUser instead.)
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
*/
|
|
|
|
protected function dieOnSitewideBlockedUser( User $user ) {
|
|
|
|
$block = $user->getBlock();
|
|
|
|
if ( $block && $block->isSitewide() ) {
|
|
|
|
$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
|
|
|
/**
|
2018-02-09 07:09:43 +00:00
|
|
|
* This checks the log_search data.
|
2015-06-16 00:53:40 +00:00
|
|
|
*
|
2018-02-09 07:09:43 +00:00
|
|
|
* @param User $thanker The user sending the thanks.
|
|
|
|
* @param string $uniqueId The identifier for the thanks.
|
2015-06-16 00:53:40 +00:00
|
|
|
* @return bool Whether thanks has already been sent
|
|
|
|
*/
|
|
|
|
protected function haveAlreadyThanked( User $thanker, $uniqueId ) {
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
2018-03-20 19:29:32 +00:00
|
|
|
$logWhere = ActorMigration::newMigration()->getWhere( $dbw, 'log_user', $thanker );
|
2015-06-16 00:53:40 +00:00
|
|
|
return (bool)$dbw->selectRow(
|
2018-03-20 19:29:32 +00:00
|
|
|
[ 'log_search', 'logging' ] + $logWhere['tables'],
|
2016-04-22 20:13:56 +00:00
|
|
|
[ 'ls_value' ],
|
|
|
|
[
|
2018-03-20 19:29:32 +00:00
|
|
|
$logWhere['conds'],
|
2015-06-16 00:53:40 +00:00
|
|
|
'ls_field' => 'thankid',
|
|
|
|
'ls_value' => $uniqueId,
|
2016-04-22 20:13:56 +00:00
|
|
|
],
|
2015-06-16 00:53:40 +00:00
|
|
|
__METHOD__,
|
2016-04-22 20:13:56 +00:00
|
|
|
[],
|
2018-03-20 19:29:32 +00:00
|
|
|
[ 'logging' => [ 'INNER JOIN', 'ls_log_id=log_id' ] ] + $logWhere['joins']
|
2015-06-16 00:53:40 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-09 07:09:43 +00:00
|
|
|
* @param User $user The user performing the thanks (and the log entry).
|
|
|
|
* @param User $recipient The target of the thanks (and the log entry).
|
2015-06-16 00:53:40 +00:00
|
|
|
* @param string $uniqueId A unique Id to identify the event being thanked for, to use
|
|
|
|
* when checking for duplicate thanks
|
|
|
|
*/
|
|
|
|
protected function logThanks( User $user, User $recipient, $uniqueId ) {
|
2019-12-28 20:37:59 +00:00
|
|
|
if ( !$this->getConfig()->get( 'ThanksLogging' ) ) {
|
2015-06-16 00:53:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-02-26 02:12:47 +00:00
|
|
|
$logEntry = new ManualLogEntry( 'thanks', 'thank' );
|
|
|
|
$logEntry->setPerformer( $user );
|
2016-04-22 20:13:56 +00:00
|
|
|
$logEntry->setRelations( [ 'thankid' => $uniqueId ] );
|
2014-02-26 02:12:47 +00:00
|
|
|
$target = $recipient->getUserPage();
|
|
|
|
$logEntry->setTarget( $target );
|
|
|
|
$logId = $logEntry->insert();
|
|
|
|
$logEntry->publish( $logId, 'udp' );
|
2020-05-18 06:46:43 +00:00
|
|
|
|
|
|
|
if ( ExtensionRegistry::getInstance()->isLoaded( 'CheckUser' ) ) {
|
|
|
|
$recentChange = $logEntry->getRecentChange();
|
2021-02-11 19:28:29 +00:00
|
|
|
Hooks::updateCheckUserData( $recentChange );
|
2020-05-18 06:46:43 +00:00
|
|
|
}
|
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
|
|
|
}
|