2014-02-26 02:12:47 +00:00
|
|
|
<?php
|
2018-03-03 04:00:40 +00:00
|
|
|
|
2023-05-19 11:38:58 +00:00
|
|
|
namespace MediaWiki\Extension\Thanks\Api;
|
2021-03-14 03:43:32 +00:00
|
|
|
|
|
|
|
use ApiBase;
|
2023-05-19 11:38:58 +00:00
|
|
|
use ApiMain;
|
2021-03-14 03:43:32 +00:00
|
|
|
use DatabaseLogEntry;
|
|
|
|
use EchoDiscussionParser;
|
|
|
|
use EchoEvent;
|
|
|
|
use LogEntry;
|
2023-05-19 11:38:58 +00:00
|
|
|
use MediaWiki\Extension\Thanks\Storage\Exceptions\InvalidLogType;
|
|
|
|
use MediaWiki\Extension\Thanks\Storage\Exceptions\LogDeleted;
|
|
|
|
use MediaWiki\Extension\Thanks\Storage\LogStore;
|
|
|
|
use MediaWiki\Permissions\PermissionManager;
|
2019-04-17 23:59:00 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2023-05-19 11:38:58 +00:00
|
|
|
use MediaWiki\Revision\RevisionStore;
|
|
|
|
use MediaWiki\User\UserFactory;
|
2021-02-17 20:44:17 +00:00
|
|
|
use MediaWiki\User\UserIdentity;
|
2021-03-14 03:43:32 +00:00
|
|
|
use Title;
|
|
|
|
use User;
|
2022-04-03 23:30:00 +00:00
|
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
2022-04-03 20:16:45 +00:00
|
|
|
use Wikimedia\ParamValidator\TypeDef\IntegerDef;
|
2019-04-17 23:59:00 +00:00
|
|
|
|
2014-02-26 02:12:47 +00:00
|
|
|
/**
|
2018-03-03 04:00:40 +00:00
|
|
|
* API module to send thanks notifications for revisions and log entries.
|
2014-02-26 02:12:47 +00:00
|
|
|
*
|
|
|
|
* @ingroup API
|
|
|
|
* @ingroup Extensions
|
|
|
|
*/
|
2018-02-21 03:46:30 +00:00
|
|
|
class ApiCoreThank extends ApiThank {
|
2023-05-19 11:38:58 +00:00
|
|
|
protected RevisionStore $revisionStore;
|
|
|
|
protected UserFactory $userFactory;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
ApiMain $main,
|
|
|
|
$action,
|
|
|
|
PermissionManager $permissionManager,
|
|
|
|
RevisionStore $revisionStore,
|
|
|
|
UserFactory $userFactory,
|
|
|
|
LogStore $storage
|
|
|
|
) {
|
|
|
|
parent::__construct(
|
|
|
|
$main,
|
|
|
|
$action,
|
|
|
|
$permissionManager,
|
|
|
|
$storage
|
|
|
|
);
|
|
|
|
$this->revisionStore = $revisionStore;
|
|
|
|
$this->userFactory = $userFactory;
|
|
|
|
}
|
2018-03-03 04:00:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform the API request.
|
2019-12-22 07:25:15 +00:00
|
|
|
* @suppress PhanTypeMismatchArgumentNullable T240141
|
2020-01-30 07:40:35 +00:00
|
|
|
* @suppress PhanPossiblyUndeclaredVariable Phan get's confused by the badly arranged code
|
2018-03-03 04:00:40 +00:00
|
|
|
*/
|
2014-02-26 02:12:47 +00:00
|
|
|
public function execute() {
|
2018-03-03 04:00:40 +00:00
|
|
|
// Initial setup.
|
2014-02-26 02:12:47 +00:00
|
|
|
$user = $this->getUser();
|
|
|
|
$this->dieOnBadUser( $user );
|
2021-05-13 16:51:46 +00:00
|
|
|
$this->dieOnUserBlockedFromThanks( $user );
|
2014-02-26 02:12:47 +00:00
|
|
|
$params = $this->extractRequestParams();
|
2018-09-04 18:57:59 +00:00
|
|
|
$revcreation = false;
|
2014-02-26 02:12:47 +00:00
|
|
|
|
2018-03-03 04:00:40 +00:00
|
|
|
$this->requireOnlyOneParameter( $params, 'rev', 'log' );
|
|
|
|
|
|
|
|
// Extract type and ID from the parameters.
|
|
|
|
if ( isset( $params['rev'] ) && !isset( $params['log'] ) ) {
|
|
|
|
$type = 'rev';
|
|
|
|
$id = $params['rev'];
|
|
|
|
} elseif ( !isset( $params['rev'] ) && isset( $params['log'] ) ) {
|
|
|
|
$type = 'log';
|
|
|
|
$id = $params['log'];
|
2014-02-26 02:12:47 +00:00
|
|
|
} else {
|
2018-03-03 04:00:40 +00:00
|
|
|
$this->dieWithError( 'thanks-error-api-params', 'thanks-error-api-params' );
|
|
|
|
}
|
|
|
|
|
2020-05-31 15:05:43 +00:00
|
|
|
$recipientUsername = null;
|
2018-03-03 04:00:40 +00:00
|
|
|
// Determine thanks parameters.
|
|
|
|
if ( $type === 'log' ) {
|
|
|
|
$logEntry = $this->getLogEntryFromId( $id );
|
|
|
|
// If there's an associated revision, thank for that instead.
|
|
|
|
if ( $logEntry->getAssociatedRevId() ) {
|
|
|
|
$type = 'rev';
|
|
|
|
$id = $logEntry->getAssociatedRevId();
|
|
|
|
} else {
|
2019-04-26 16:12:19 +00:00
|
|
|
// If there's no associated revision, die if the user is sitewide blocked
|
2018-03-03 04:00:40 +00:00
|
|
|
$excerpt = '';
|
|
|
|
$title = $logEntry->getTarget();
|
|
|
|
$recipient = $this->getUserFromLog( $logEntry );
|
|
|
|
$recipientUsername = $recipient->getName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( $type === 'rev' ) {
|
|
|
|
$revision = $this->getRevisionFromId( $id );
|
|
|
|
$excerpt = EchoDiscussionParser::getEditExcerpt( $revision, $this->getLanguage() );
|
|
|
|
$title = $this->getTitleFromRevision( $revision );
|
2021-04-20 15:41:21 +00:00
|
|
|
$this->dieOnUserBlockedFromTitle( $user, $title );
|
2019-04-26 16:12:19 +00:00
|
|
|
|
2014-02-26 02:12:47 +00:00
|
|
|
$recipient = $this->getUserFromRevision( $revision );
|
2019-04-17 23:59:00 +00:00
|
|
|
$recipientUsername = $revision->getUser()->getName();
|
2018-09-04 18:57:59 +00:00
|
|
|
|
|
|
|
// If there is no parent revid of this revision, it's a page creation.
|
2023-05-19 11:38:58 +00:00
|
|
|
if ( !$this->revisionStore->getPreviousRevision( $revision ) ) {
|
2018-09-04 18:57:59 +00:00
|
|
|
$revcreation = true;
|
|
|
|
}
|
2018-03-03 04:00:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send thanks.
|
|
|
|
if ( $this->userAlreadySentThanks( $user, $type, $id ) ) {
|
|
|
|
$this->markResultSuccess( $recipientUsername );
|
|
|
|
} else {
|
2014-02-26 02:12:47 +00:00
|
|
|
$this->dieOnBadRecipient( $user, $recipient );
|
|
|
|
$this->sendThanks(
|
|
|
|
$user,
|
2018-03-03 04:00:40 +00:00
|
|
|
$type,
|
|
|
|
$id,
|
|
|
|
$excerpt,
|
2014-02-26 02:12:47 +00:00
|
|
|
$recipient,
|
2018-03-03 04:00:40 +00:00
|
|
|
$this->getSourceFromParams( $params ),
|
2018-09-04 18:57:59 +00:00
|
|
|
$title,
|
|
|
|
$revcreation
|
2014-02-26 02:12:47 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-03 04:00:40 +00:00
|
|
|
/**
|
|
|
|
* Check the session data for an indication of whether this user has already sent this thanks.
|
|
|
|
* @param User $user The user being thanked.
|
|
|
|
* @param string $type Either 'rev' or 'log'.
|
|
|
|
* @param int $id The revision or log ID.
|
|
|
|
* @return bool
|
|
|
|
*/
|
2019-12-28 19:55:52 +00:00
|
|
|
protected function userAlreadySentThanks( User $user, $type, $id ) {
|
2018-03-03 04:00:40 +00:00
|
|
|
if ( $type === 'rev' ) {
|
|
|
|
// For b/c with old-style keys
|
|
|
|
$type = '';
|
|
|
|
}
|
|
|
|
return (bool)$user->getRequest()->getSessionData( "thanks-thanked-$type$id" );
|
2014-02-26 02:12:47 +00:00
|
|
|
}
|
|
|
|
|
2018-03-03 04:00:40 +00:00
|
|
|
private function getRevisionFromId( $revId ) {
|
2023-05-19 11:38:58 +00:00
|
|
|
$revision = $this->revisionStore->getRevisionById( $revId );
|
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' );
|
2019-04-17 23:59:00 +00:00
|
|
|
} elseif ( $revision->isDeleted( RevisionRecord::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;
|
|
|
|
}
|
|
|
|
|
2018-03-03 04:00:40 +00:00
|
|
|
/**
|
|
|
|
* Get the log entry from the ID.
|
|
|
|
* @param int $logId The log entry ID.
|
|
|
|
* @return DatabaseLogEntry
|
|
|
|
*/
|
2023-05-19 11:38:58 +00:00
|
|
|
protected function getLogEntryFromId( $logId ): DatabaseLogEntry {
|
|
|
|
$logEntry = null;
|
|
|
|
try {
|
|
|
|
$logEntry = $this->storage->getLogEntryFromId( $logId );
|
|
|
|
} catch ( InvalidLogType $e ) {
|
|
|
|
$err = $this->msg( 'thanks-error-invalid-log-type', $e->getLogType() );
|
2018-03-03 04:00:40 +00:00
|
|
|
$this->dieWithError( $err, 'thanks-error-invalid-log-type' );
|
2023-05-19 11:38:58 +00:00
|
|
|
} catch ( LogDeleted $e ) {
|
2018-03-03 04:00:40 +00:00
|
|
|
$this->dieWithError( 'thanks-error-log-deleted', 'thanks-error-log-deleted' );
|
|
|
|
}
|
|
|
|
|
2023-05-19 11:38:58 +00:00
|
|
|
if ( !$logEntry ) {
|
|
|
|
$this->dieWithError( 'thanks-error-invalid-log-id', 'thanks-error-invalid-log-id' );
|
|
|
|
}
|
2019-12-22 07:25:15 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeMismatchReturnNullable T240141
|
2018-03-03 04:00:40 +00:00
|
|
|
return $logEntry;
|
|
|
|
}
|
|
|
|
|
2019-04-17 23:59:00 +00:00
|
|
|
private function getTitleFromRevision( RevisionRecord $revision ) {
|
|
|
|
$title = Title::newFromID( $revision->getPageId() );
|
2014-02-26 02:12:47 +00:00
|
|
|
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'
|
2018-03-03 04:00:40 +00:00
|
|
|
* @param string[] $params Incoming API parameters, with a 'source' key.
|
|
|
|
* @return string The source, or 'undefined' if not provided.
|
2014-02-26 02:12:47 +00:00
|
|
|
*/
|
|
|
|
private function getSourceFromParams( $params ) {
|
|
|
|
if ( $params['source'] ) {
|
|
|
|
return trim( $params['source'] );
|
|
|
|
} else {
|
|
|
|
return 'undefined';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 07:25:15 +00:00
|
|
|
/**
|
|
|
|
* @param RevisionRecord $revision
|
|
|
|
* @return User
|
|
|
|
*/
|
2019-04-17 23:59:00 +00:00
|
|
|
private function getUserFromRevision( RevisionRecord $revision ) {
|
|
|
|
$recipient = $revision->getUser()->getId();
|
2014-02-26 02:12:47 +00:00
|
|
|
if ( !$recipient ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->dieWithError( 'thanks-error-invalidrecipient', 'invalidrecipient' );
|
2014-02-26 02:12:47 +00:00
|
|
|
}
|
2023-05-19 11:38:58 +00:00
|
|
|
return $this->userFactory->newFromId( $recipient );
|
2014-02-26 02:12:47 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 07:25:15 +00:00
|
|
|
/**
|
|
|
|
* @param LogEntry $logEntry
|
2021-02-17 20:44:17 +00:00
|
|
|
* @return UserIdentity
|
2019-12-22 07:25:15 +00:00
|
|
|
*/
|
2018-03-03 04:00:40 +00:00
|
|
|
private function getUserFromLog( LogEntry $logEntry ) {
|
2021-02-17 20:44:17 +00:00
|
|
|
$recipient = $logEntry->getPerformerIdentity();
|
2023-05-19 11:38:58 +00:00
|
|
|
return $this->userFactory->newFromUserIdentity( $recipient );
|
2018-03-03 04:00:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the thanks notification event, and log the thanks.
|
|
|
|
* @param User $user The thanks-sending user.
|
|
|
|
* @param string $type The thanks type ('rev' or 'log').
|
|
|
|
* @param int $id The log or revision ID.
|
|
|
|
* @param string $excerpt The excerpt to display as the thanks notification. This will only
|
|
|
|
* be used if it is not possible to retrieve the relevant excerpt at the time the
|
|
|
|
* notification is displayed (in order to account for changing visibility in the meantime).
|
|
|
|
* @param User $recipient The recipient of the thanks.
|
|
|
|
* @param string $source Where the thanks was given.
|
|
|
|
* @param Title $title The title of the page for which thanks is given.
|
2018-09-04 18:57:59 +00:00
|
|
|
* @param bool $revcreation True if the linked revision is a page creation.
|
2018-03-03 04:00:40 +00:00
|
|
|
*/
|
|
|
|
protected function sendThanks(
|
2018-09-04 18:57:59 +00:00
|
|
|
User $user, $type, $id, $excerpt, User $recipient, $source, Title $title, $revcreation
|
2018-03-03 04:00:40 +00:00
|
|
|
) {
|
|
|
|
$uniqueId = $type . '-' . $id;
|
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
|
|
|
|
|
|
|
// 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-03 04:00:40 +00:00
|
|
|
$type . 'id' => $id,
|
2014-02-26 02:12:47 +00:00
|
|
|
'thanked-user-id' => $recipient->getId(),
|
|
|
|
'source' => $source,
|
2018-03-03 04:00:40 +00:00
|
|
|
'excerpt' => $excerpt,
|
2018-09-04 18:57:59 +00:00
|
|
|
'revcreation' => $revcreation,
|
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-03 04:00:40 +00:00
|
|
|
// And mark the thank in session for a cheaper check to prevent duplicates (Phab:T48690).
|
|
|
|
$user->getRequest()->setSessionData( "thanks-thanked-$type$id", 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' => [
|
2022-04-03 23:30:00 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'integer',
|
2022-04-03 20:16:45 +00:00
|
|
|
IntegerDef::PARAM_MIN => 1,
|
2022-04-03 23:30:00 +00:00
|
|
|
ParamValidator::PARAM_REQUIRED => false,
|
2018-03-03 04:00:40 +00:00
|
|
|
],
|
|
|
|
'log' => [
|
2022-04-03 23:30:00 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'integer',
|
2022-04-03 20:16:45 +00:00
|
|
|
IntegerDef::PARAM_MIN => 1,
|
2022-04-03 23:30:00 +00:00
|
|
|
ParamValidator::PARAM_REQUIRED => false,
|
2016-04-22 20:13:56 +00:00
|
|
|
],
|
|
|
|
'token' => [
|
2022-04-03 23:30:00 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'string',
|
|
|
|
ParamValidator::PARAM_REQUIRED => true,
|
2016-04-22 20:13:56 +00:00
|
|
|
],
|
|
|
|
'source' => [
|
2022-04-03 23:30:00 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'string',
|
|
|
|
ParamValidator::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
|
|
|
}
|