mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Thanks
synced 2024-11-15 02:54:23 +00:00
470eff1700
Since this extension uses extension.json, it already requires 1.25+ so no need to keep the old code around. Change-Id: Ibcbc065f09f863014389d3a28ca0ae9134c88f78
94 lines
2.5 KiB
PHP
94 lines
2.5 KiB
PHP
<?php
|
|
/**
|
|
* Base API module for Thanks
|
|
*
|
|
* @ingroup API
|
|
* @ingroup Extensions
|
|
*/
|
|
abstract class ApiThank extends ApiBase {
|
|
protected function dieIfEchoNotInstalled() {
|
|
if ( !class_exists( 'EchoNotifier' ) ) {
|
|
$this->dieUsage( 'Echo is not installed on this wiki', 'echonotinstalled' );
|
|
}
|
|
}
|
|
|
|
protected function dieOnBadUser( User $user ) {
|
|
if ( $user->isAnon() ) {
|
|
$this->dieUsage( 'Anonymous users cannot send thanks', 'notloggedin' );
|
|
} elseif ( $user->pingLimiter( 'thanks-notification' ) ) {
|
|
$this->dieUsageMsg( [ 'actionthrottledtext' ] );
|
|
} elseif ( $user->isBlocked() ) {
|
|
$this->dieUsageMsg( [ 'blockedtext' ] );
|
|
}
|
|
}
|
|
|
|
protected function dieOnBadRecipient( User $user, User $recipient ) {
|
|
global $wgThanksSendToBots;
|
|
|
|
if ( $user->getId() === $recipient->getId() ) {
|
|
$this->dieUsage( 'You cannot thank yourself', 'invalidrecipient' );
|
|
} elseif ( !$wgThanksSendToBots && in_array( 'bot', $recipient->getGroups() ) ) {
|
|
$this->dieUsage( 'Bots cannot be thanked', 'invalidrecipient' );
|
|
}
|
|
}
|
|
|
|
protected function markResultSuccess( $recipientName ) {
|
|
$this->getResult()->addValue( null, 'result', [
|
|
'success' => 1,
|
|
'recipient' => $recipientName,
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* 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(
|
|
[ 'log_search', 'logging' ],
|
|
[ 'ls_value' ],
|
|
[
|
|
'log_user' => $thanker->getId(),
|
|
'ls_field' => 'thankid',
|
|
'ls_value' => $uniqueId,
|
|
],
|
|
__METHOD__,
|
|
[],
|
|
[ 'logging' => [ 'INNER JOIN', 'ls_log_id=log_id' ] ]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @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;
|
|
}
|
|
$logEntry = new ManualLogEntry( 'thanks', 'thank' );
|
|
$logEntry->setPerformer( $user );
|
|
$logEntry->setRelations( [ 'thankid' => $uniqueId ] );
|
|
$target = $recipient->getUserPage();
|
|
$logEntry->setTarget( $target );
|
|
$logId = $logEntry->insert();
|
|
$logEntry->publish( $logId, 'udp' );
|
|
}
|
|
|
|
public function needsToken() {
|
|
return 'csrf';
|
|
}
|
|
|
|
// Writes to the Echo database and sometimes log tables.
|
|
public function isWriteMode() {
|
|
return true;
|
|
}
|
|
}
|