2014-02-26 02:12:47 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2018-03-02 22:59:20 +00:00
|
|
|
* API module to send thanks notifications for revisions
|
2014-02-26 02:12:47 +00:00
|
|
|
*
|
|
|
|
* @ingroup API
|
|
|
|
* @ingroup Extensions
|
|
|
|
*/
|
2018-02-21 03:46:30 +00:00
|
|
|
class ApiCoreThank extends ApiThank {
|
2014-02-26 02:12:47 +00:00
|
|
|
public function execute() {
|
|
|
|
$this->dieIfEchoNotInstalled();
|
2018-03-02 22:59:20 +00:00
|
|
|
|
2014-02-26 02:12:47 +00:00
|
|
|
$user = $this->getUser();
|
|
|
|
$this->dieOnBadUser( $user );
|
2018-03-02 22:59:20 +00:00
|
|
|
|
2014-02-26 02:12:47 +00:00
|
|
|
$params = $this->extractRequestParams();
|
2018-03-02 22:59:20 +00:00
|
|
|
$revision = $this->getRevisionFromParams( $params );
|
2014-02-26 02:12:47 +00:00
|
|
|
|
2018-03-02 22:59:20 +00:00
|
|
|
if ( $this->userAlreadySentThanksForRevision( $user, $revision ) ) {
|
|
|
|
$this->markResultSuccess( $revision->getUserText() );
|
2014-02-26 02:12:47 +00:00
|
|
|
} else {
|
|
|
|
$recipient = $this->getUserFromRevision( $revision );
|
|
|
|
$this->dieOnBadRecipient( $user, $recipient );
|
|
|
|
$this->sendThanks(
|
|
|
|
$user,
|
2018-03-02 22:59:20 +00:00
|
|
|
$revision,
|
2014-02-26 02:12:47 +00:00
|
|
|
$recipient,
|
2018-03-02 22:59:20 +00:00
|
|
|
$this->getSourceFromParams( $params )
|
2014-02-26 02:12:47 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-02 22:59:20 +00:00
|
|
|
protected function userAlreadySentThanksForRevision( User $user, Revision $revision ) {
|
|
|
|
return $user->getRequest()->getSessionData( "thanks-thanked-{$revision->getId()}" );
|
2014-02-26 02:12:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getRevisionFromParams( $params ) {
|
|
|
|
$revision = Revision::newFromId( $params['rev'] );
|
2018-03-02 22:59:20 +00:00
|
|
|
|
2014-04-15 04:28:10 +00:00
|
|
|
// Revision ID 1 means an invalid argument was passed in.
|
|
|
|
if ( !$revision || $revision->getId() === 1 ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->dieWithError( 'thanks-error-invalidrevision', 'invalidrevision' );
|
2014-02-26 02:12:47 +00:00
|
|
|
} elseif ( $revision->isDeleted( Revision::DELETED_TEXT ) ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->dieWithError( 'thanks-error-revdeleted', 'revdeleted' );
|
2014-02-26 02:12:47 +00:00
|
|
|
}
|
|
|
|
return $revision;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getTitleFromRevision( Revision $revision ) {
|
|
|
|
$title = Title::newFromID( $revision->getPage() );
|
|
|
|
if ( !$title instanceof Title ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->dieWithError( 'thanks-error-notitle', 'notitle' );
|
2014-02-26 02:12:47 +00:00
|
|
|
}
|
|
|
|
return $title;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the source of the thanks, e.g. 'diff' or 'history'
|
|
|
|
*/
|
|
|
|
private function getSourceFromParams( $params ) {
|
|
|
|
if ( $params['source'] ) {
|
|
|
|
return trim( $params['source'] );
|
|
|
|
} else {
|
|
|
|
return 'undefined';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getUserFromRevision( Revision $revision ) {
|
|
|
|
$recipient = $revision->getUser();
|
|
|
|
if ( !$recipient ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->dieWithError( 'thanks-error-invalidrecipient', 'invalidrecipient' );
|
2014-02-26 02:12:47 +00:00
|
|
|
}
|
|
|
|
return User::newFromId( $recipient );
|
|
|
|
}
|
|
|
|
|
2018-03-02 22:59:20 +00:00
|
|
|
private function sendThanks( User $user, Revision $revision, User $recipient, $source ) {
|
|
|
|
$uniqueId = "rev-{$revision->getId()}";
|
2015-06-16 00:53:40 +00:00
|
|
|
// Do one last check to make sure we haven't sent Thanks before
|
|
|
|
if ( $this->haveAlreadyThanked( $user, $uniqueId ) ) {
|
|
|
|
// Pretend the thanks were sent
|
|
|
|
$this->markResultSuccess( $recipient->getName() );
|
|
|
|
return;
|
|
|
|
}
|
2014-02-26 02:12:47 +00:00
|
|
|
|
2018-03-02 22:59:20 +00:00
|
|
|
$title = $this->getTitleFromRevision( $revision );
|
2014-02-26 02:12:47 +00:00
|
|
|
// Create the notification via Echo extension
|
2016-04-22 20:13:56 +00:00
|
|
|
EchoEvent::create( [
|
2014-02-26 02:12:47 +00:00
|
|
|
'type' => 'edit-thank',
|
|
|
|
'title' => $title,
|
2016-04-22 20:13:56 +00:00
|
|
|
'extra' => [
|
2018-03-02 22:59:20 +00:00
|
|
|
'revid' => $revision->getId(),
|
2014-02-26 02:12:47 +00:00
|
|
|
'thanked-user-id' => $recipient->getId(),
|
|
|
|
'source' => $source,
|
2018-03-02 22:59:20 +00:00
|
|
|
'excerpt' => EchoDiscussionParser::getEditExcerpt( $revision, $this->getLanguage() ),
|
2016-04-22 20:13:56 +00:00
|
|
|
],
|
2014-02-26 02:12:47 +00:00
|
|
|
'agent' => $user,
|
2016-04-22 20:13:56 +00:00
|
|
|
] );
|
2014-02-26 02:12:47 +00:00
|
|
|
|
2018-03-02 22:59:20 +00:00
|
|
|
// And mark the thank in session for a cheaper check to prevent duplicates (Bug 46690).
|
|
|
|
$user->getRequest()->setSessionData( "thanks-thanked-{$revision->getId()}", true );
|
2014-02-26 02:12:47 +00:00
|
|
|
// Set success message
|
2014-04-15 04:28:10 +00:00
|
|
|
$this->markResultSuccess( $recipient->getName() );
|
2015-06-16 00:53:40 +00:00
|
|
|
$this->logThanks( $user, $recipient, $uniqueId );
|
2014-02-26 02:12:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getAllowedParams() {
|
2016-04-22 20:13:56 +00:00
|
|
|
return [
|
|
|
|
'rev' => [
|
2014-02-26 02:12:47 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'integer',
|
|
|
|
ApiBase::PARAM_MIN => 1,
|
2018-03-02 22:59:20 +00:00
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
2016-04-22 20:13:56 +00:00
|
|
|
],
|
|
|
|
'token' => [
|
2014-02-26 02:12:47 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
2016-04-22 20:13:56 +00:00
|
|
|
],
|
|
|
|
'source' => [
|
2014-02-26 02:12:47 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
|
|
|
ApiBase::PARAM_REQUIRED => false,
|
2016-04-22 20:13:56 +00:00
|
|
|
]
|
|
|
|
];
|
2014-02-26 02:12:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getHelpUrls() {
|
2016-04-22 20:13:56 +00:00
|
|
|
return [
|
2014-02-26 02:12:47 +00:00
|
|
|
'https://www.mediawiki.org/wiki/Extension:Thanks#API_Documentation',
|
2016-04-22 20:13:56 +00:00
|
|
|
];
|
2014-02-26 02:12:47 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 18:58:27 +00:00
|
|
|
/**
|
|
|
|
* @see ApiBase::getExamplesMessages()
|
2017-09-24 19:54:35 +00:00
|
|
|
* @return array
|
2014-10-29 18:58:27 +00:00
|
|
|
*/
|
|
|
|
protected function getExamplesMessages() {
|
2016-04-22 20:13:56 +00:00
|
|
|
return [
|
2014-10-29 18:58:27 +00:00
|
|
|
'action=thank&revid=456&source=diff&token=123ABC'
|
|
|
|
=> 'apihelp-thank-example-1',
|
2016-04-22 20:13:56 +00:00
|
|
|
];
|
2014-10-29 18:58:27 +00:00
|
|
|
}
|
2014-02-26 02:12:47 +00:00
|
|
|
}
|