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 dieIfEchoNotInstalled() {
|
2013-03-18 19:56:12 +00:00
|
|
|
if ( !class_exists( 'EchoNotifier' ) ) {
|
|
|
|
$this->dieUsage( 'Echo is not installed on this wiki', 'echonotinstalled' );
|
|
|
|
}
|
2013-11-03 11:13:03 +00:00
|
|
|
}
|
2013-03-18 19:56:12 +00:00
|
|
|
|
2014-02-26 02:12:47 +00:00
|
|
|
protected function dieOnBadUser( User $user ) {
|
2013-11-03 11:13:03 +00:00
|
|
|
if ( $user->isAnon() ) {
|
2013-03-18 19:56:12 +00:00
|
|
|
$this->dieUsage( 'Anonymous users cannot send thanks', 'notloggedin' );
|
2013-11-03 11:13:03 +00:00
|
|
|
} elseif ( $user->pingLimiter( 'thanks-notification' ) ) {
|
2016-04-22 20:13:56 +00:00
|
|
|
$this->dieUsageMsg( [ 'actionthrottledtext' ] );
|
2013-11-03 11:13:03 +00:00
|
|
|
} elseif ( $user->isBlocked() ) {
|
2016-04-22 20:13:56 +00:00
|
|
|
$this->dieUsageMsg( [ 'blockedtext' ] );
|
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() ) {
|
2013-11-24 06:45:12 +00:00
|
|
|
$this->dieUsage( 'You cannot thank yourself', 'invalidrecipient' );
|
|
|
|
} elseif ( !$wgThanksSendToBots && in_array( 'bot', $recipient->getGroups() ) ) {
|
|
|
|
$this->dieUsage( 'Bots cannot be thanked', 'invalidrecipient' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* This checks the log_search data
|
|
|
|
*
|
|
|
|
* @param User $thanker
|
|
|
|
* @param string $uniqueId
|
|
|
|
* @return bool Whether thanks has already been sent
|
|
|
|
*/
|
|
|
|
protected function haveAlreadyThanked( User $thanker, $uniqueId ) {
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
return (bool)$dbw->selectRow(
|
2016-04-22 20:13:56 +00:00
|
|
|
[ 'log_search', 'logging' ],
|
|
|
|
[ 'ls_value' ],
|
|
|
|
[
|
2015-06-16 00:53:40 +00:00
|
|
|
'log_user' => $thanker->getId(),
|
|
|
|
'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
|
|
|
[],
|
|
|
|
[ 'logging' => [ 'INNER JOIN', 'ls_log_id=log_id' ] ]
|
2015-06-16 00:53:40 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
* @param User $recipient
|
|
|
|
* @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
|
|
|
// Writes to the Echo database and sometimes log tables.
|
|
|
|
public function isWriteMode() {
|
|
|
|
return true;
|
|
|
|
}
|
2013-03-18 19:56:12 +00:00
|
|
|
}
|