2013-03-18 19:56:12 +00:00
|
|
|
<?php
|
2013-04-05 23:47:49 +00:00
|
|
|
/**
|
|
|
|
* API module to send thanks notifications
|
|
|
|
*
|
|
|
|
* @ingroup API
|
|
|
|
* @ingroup Extensions
|
|
|
|
*/
|
2013-03-18 19:56:12 +00:00
|
|
|
class ApiThank extends ApiBase {
|
|
|
|
public function execute() {
|
2013-11-03 11:13:03 +00:00
|
|
|
$this->dieIfEchoNotInstalled();
|
|
|
|
|
|
|
|
$user = $this->getUser();
|
|
|
|
$this->dieOnBadUser( $user );
|
|
|
|
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
$revision = $this->getRevisionFromParams( $params );
|
|
|
|
|
|
|
|
if ( $this->userAlreadySentThanksForRevision( $user, $revision ) ) {
|
|
|
|
$this->markResultSuccess();
|
|
|
|
} else {
|
2013-11-24 06:45:12 +00:00
|
|
|
$recipient = $this->getUserFromRevision( $revision );
|
|
|
|
$this->dieOnBadRecipient( $user, $recipient );
|
2013-11-03 11:13:03 +00:00
|
|
|
$this->sendThanks(
|
|
|
|
$user,
|
|
|
|
$revision,
|
2013-11-24 06:45:12 +00:00
|
|
|
$recipient,
|
2013-11-03 11:13:03 +00:00
|
|
|
$this->getSourceFromParams( $params )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function userAlreadySentThanksForRevision( User $user, Revision $revision ) {
|
|
|
|
return $user->getRequest()->getSessionData( "thanks-thanked-{$revision->getId()}" );
|
|
|
|
}
|
2013-03-18 19:56:12 +00:00
|
|
|
|
2013-11-03 11:13:03 +00:00
|
|
|
private 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
|
|
|
|
2013-11-03 11:13:03 +00:00
|
|
|
private function dieOnBadUser( User $user ) {
|
|
|
|
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' ) ) {
|
2013-03-18 19:56:12 +00:00
|
|
|
$this->dieUsageMsg( array( 'actionthrottledtext' ) );
|
2013-11-03 11:13:03 +00:00
|
|
|
} elseif ( $user->isBlocked() ) {
|
2013-03-18 19:56:12 +00:00
|
|
|
$this->dieUsageMsg( array( 'blockedtext' ) );
|
|
|
|
}
|
2013-11-03 11:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getRevisionFromParams( $params ) {
|
|
|
|
$revision = Revision::newFromId( $params['rev'] );
|
|
|
|
if ( !$revision ) {
|
2013-03-18 19:56:12 +00:00
|
|
|
$this->dieUsage( 'Revision ID is not valid', 'invalidrevision' );
|
2013-11-03 11:13:03 +00:00
|
|
|
} elseif ( $revision->isDeleted( Revision::DELETED_TEXT ) ) {
|
2013-10-07 20:46:48 +00:00
|
|
|
$this->dieUsage( 'Revision has been deleted', 'revdeleted' );
|
2013-11-03 11:13:03 +00:00
|
|
|
}
|
|
|
|
return $revision;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getTitleFromRevision( Revision $revision ) {
|
|
|
|
$title = Title::newFromID( $revision->getPage() );
|
|
|
|
if ( !$title instanceof Title ) {
|
|
|
|
$this->dieUsage( 'Page title could not be retrieved', 'notitle' );
|
|
|
|
}
|
|
|
|
return $title;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the source of the thanks, e.g. 'diff' or 'history'
|
|
|
|
*/
|
|
|
|
private function getSourceFromParams( $params ) {
|
|
|
|
if ( $params['source'] ) {
|
|
|
|
return trim( $params['source'] );
|
2013-03-18 19:56:12 +00:00
|
|
|
} else {
|
2013-11-03 11:13:03 +00:00
|
|
|
return 'undefined';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-24 06:45:12 +00:00
|
|
|
private function getUserFromRevision( Revision $revision ) {
|
2013-11-03 11:13:03 +00:00
|
|
|
$recipient = $revision->getUser();
|
|
|
|
if ( !$recipient ) {
|
|
|
|
$this->dieUsage( 'No valid recipient found', 'invalidrecipient' );
|
|
|
|
}
|
2013-11-24 06:45:12 +00:00
|
|
|
return User::newFromId( $recipient );
|
2013-11-03 11:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function markResultSuccess(){
|
|
|
|
$this->getResult()->addValue( null, 'result', array( 'success' => 1 ) );
|
|
|
|
}
|
|
|
|
|
2013-11-24 06:45:12 +00:00
|
|
|
private function dieOnBadRecipient( User $agent, User $recipient ) {
|
|
|
|
global $wgThanksSendToBots;
|
|
|
|
|
|
|
|
if ( $agent->getId() === $recipient->getId() ) {
|
|
|
|
$this->dieUsage( 'You cannot thank yourself', 'invalidrecipient' );
|
|
|
|
} elseif ( !$wgThanksSendToBots && in_array( 'bot', $recipient->getGroups() ) ) {
|
|
|
|
$this->dieUsage( 'Bots cannot be thanked', 'invalidrecipient' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function sendThanks( User $user, Revision $revision, User $recipient, $source ) {
|
2013-11-03 11:13:03 +00:00
|
|
|
global $wgThanksLogging;
|
|
|
|
$title = $this->getTitleFromRevision( $revision );
|
|
|
|
|
|
|
|
// Create the notification via Echo extension
|
|
|
|
EchoEvent::create( array(
|
|
|
|
'type' => 'edit-thank',
|
|
|
|
'title' => $title,
|
|
|
|
'extra' => array(
|
|
|
|
'revid' => $revision->getId(),
|
2013-11-24 06:45:12 +00:00
|
|
|
'thanked-user-id' => $recipient->getId(),
|
2013-11-03 11:13:03 +00:00
|
|
|
'source' => $source,
|
|
|
|
),
|
|
|
|
'user' => $user,
|
|
|
|
) );
|
|
|
|
|
|
|
|
// Mark the thank in session to prevent duplicates (Bug 46690)
|
|
|
|
$user->getRequest()->setSessionData( "thanks-thanked-{$revision->getId()}", true );
|
|
|
|
// Set success message
|
|
|
|
$this->markResultSuccess();
|
|
|
|
// Log it if we're supposed to log it
|
|
|
|
if ( $wgThanksLogging ) {
|
|
|
|
$logEntry = new ManualLogEntry( 'thanks', 'thank' );
|
|
|
|
$logEntry->setPerformer( $user );
|
2013-11-24 06:45:12 +00:00
|
|
|
$target = $recipient->getUserPage();
|
2013-11-03 11:13:03 +00:00
|
|
|
$logEntry->setTarget( $target );
|
|
|
|
$logEntry->insert();
|
2013-03-18 19:56:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDescription() {
|
|
|
|
return array(
|
|
|
|
'This API is for sending thank you notifications from one editor to another.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getParamDescription() {
|
|
|
|
return array(
|
|
|
|
'rev' => 'A revision ID for an edit that you want to thank someone for',
|
2013-04-05 23:47:49 +00:00
|
|
|
'token' => 'An edit token (to prevent CSRF abuse)',
|
2013-03-18 19:56:12 +00:00
|
|
|
'source' => "A short string describing the source of the request, for example, 'diff' or 'history'",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAllowedParams() {
|
|
|
|
return array(
|
|
|
|
'rev' => array(
|
|
|
|
ApiBase::PARAM_TYPE => 'integer',
|
|
|
|
ApiBase::PARAM_MIN => 1,
|
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
|
|
|
),
|
|
|
|
'token' => array(
|
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
|
|
|
),
|
|
|
|
'source' => array(
|
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
|
|
|
ApiBase::PARAM_REQUIRED => false,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function needsToken() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
public function getTokenSalt() {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2013-10-27 01:56:37 +00:00
|
|
|
public function getHelpUrls() {
|
|
|
|
return array(
|
|
|
|
'https://www.mediawiki.org/wiki/Extension:Thanks#API_Documentation',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-03-18 19:56:12 +00:00
|
|
|
public function getVersion() {
|
|
|
|
return __CLASS__ . '-1.0';
|
|
|
|
}
|
2013-11-03 11:13:03 +00:00
|
|
|
|
2013-03-18 19:56:12 +00:00
|
|
|
}
|