2013-03-18 19:56:12 +00:00
|
|
|
<?php
|
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' );
|
2013-11-03 11:13:03 +00:00
|
|
|
} elseif ( $user->isBlocked() ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->dieBlocked( $user->getBlock() );
|
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
|
|
|
}
|
|
|
|
|
2014-02-26 02:12:47 +00:00
|
|
|
protected function dieOnBadRecipient( User $user, User $recipient ) {
|
2013-11-24 06:45:12 +00:00
|
|
|
global $wgThanksSendToBots;
|
|
|
|
|
2014-02-26 02:12:47 +00:00
|
|
|
if ( $user->getId() === $recipient->getId() ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->dieWithError( 'thanks-error-invalidrecipient-self', 'invalidrecipient' );
|
2018-10-02 00:24:46 +00:00
|
|
|
} elseif ( !$wgThanksSendToBots && $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 ) {
|
|
|
|
global $wgThanksLogging;
|
|
|
|
if ( !$wgThanksLogging ) {
|
|
|
|
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' );
|
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
|
|
|
}
|