Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
44.00% |
11 / 25 |
|
20.00% |
2 / 10 |
CRAP | |
0.00% |
0 / 1 |
| ApiThank | |
44.00% |
11 / 25 |
|
20.00% |
2 / 10 |
90.25 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| dieOnBadUser | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| dieOnUserBlockedFromPage | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| dieOnUserBlockedFromThanks | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
4 | |||
| dieOnBadRecipient | |
50.00% |
3 / 6 |
|
0.00% |
0 / 1 |
8.12 | |||
| markResultSuccess | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| haveAlreadyThanked | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| logThanks | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| needsToken | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isWriteMode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Extension\Thanks\Api; |
| 4 | |
| 5 | use MediaWiki\Api\ApiBase; |
| 6 | use MediaWiki\Api\ApiMain; |
| 7 | use MediaWiki\Extension\Thanks\Storage\LogStore; |
| 8 | use MediaWiki\Page\PageIdentity; |
| 9 | use MediaWiki\Permissions\PermissionManager; |
| 10 | use MediaWiki\User\TempUser\TempUserDetailsLookup; |
| 11 | use MediaWiki\User\User; |
| 12 | |
| 13 | /** |
| 14 | * Base API module for Thanks |
| 15 | * |
| 16 | * @ingroup API |
| 17 | * @ingroup Extensions |
| 18 | */ |
| 19 | abstract class ApiThank extends ApiBase { |
| 20 | |
| 21 | public function __construct( |
| 22 | ApiMain $main, |
| 23 | string $action, |
| 24 | protected readonly PermissionManager $permissionManager, |
| 25 | protected readonly LogStore $storage, |
| 26 | protected readonly TempUserDetailsLookup $tempUserDetailsLookup, |
| 27 | ) { |
| 28 | parent::__construct( $main, $action ); |
| 29 | } |
| 30 | |
| 31 | protected function dieOnBadUser( User $user ) { |
| 32 | if ( !$user->isNamed() ) { |
| 33 | $this->dieWithError( 'thanks-error-notloggedin', 'notloggedin' ); |
| 34 | } elseif ( $user->pingLimiter( 'thanks-notification' ) ) { |
| 35 | $this->dieWithError( [ 'thanks-error-ratelimited', $user->getName() ], 'ratelimited' ); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Check whether the user is blocked from this title. (This is not the same |
| 41 | * as checking whether they are sitewide blocked, because a sitewide blocked |
| 42 | * user may still be allowed to thank on their own talk page.) |
| 43 | * |
| 44 | * This is separate from dieOnBadUser because we need to know the title. |
| 45 | * |
| 46 | * @param User $user |
| 47 | * @param PageIdentity $page |
| 48 | */ |
| 49 | protected function dieOnUserBlockedFromPage( User $user, PageIdentity $page ) { |
| 50 | if ( $this->permissionManager->isBlockedFrom( $user, $page ) ) { |
| 51 | // Block should definitely exist |
| 52 | // @phan-suppress-next-line PhanTypeMismatchArgumentNullable |
| 53 | $this->dieBlocked( $user->getBlock() ); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Check whether the user is sitewide blocked. |
| 59 | * |
| 60 | * This is separate from dieOnUserBlockedFromPage because we need to know if the thank |
| 61 | * is related to a revision. (If it is, then use dieOnUserBlockedFromPage instead.) |
| 62 | * |
| 63 | * @param User $user |
| 64 | */ |
| 65 | protected function dieOnUserBlockedFromThanks( User $user ) { |
| 66 | $block = $user->getBlock(); |
| 67 | if ( |
| 68 | $block && |
| 69 | ( $block->isSitewide() || $block->appliesToRight( 'thanks' ) ) |
| 70 | ) { |
| 71 | $this->dieBlocked( $block ); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | protected function dieOnBadRecipient( User $user, User $recipient ) { |
| 76 | if ( $user->getId() === $recipient->getId() ) { |
| 77 | $this->dieWithError( 'thanks-error-invalidrecipient-self', 'invalidrecipient' ); |
| 78 | } elseif ( $this->tempUserDetailsLookup->isExpired( $recipient ) ) { |
| 79 | $this->dieWithError( 'thanks-error-invalidrecipient-expired', 'invalidrecipient' ); |
| 80 | } elseif ( !$this->getConfig()->get( 'ThanksSendToBots' ) && $recipient->isBot() ) { |
| 81 | $this->dieWithError( 'thanks-error-invalidrecipient-bot', 'invalidrecipient' ); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | protected function markResultSuccess( string $recipientName ) { |
| 86 | $this->getResult()->addValue( null, 'result', [ |
| 87 | 'success' => 1, |
| 88 | 'recipient' => $recipientName, |
| 89 | ] ); |
| 90 | } |
| 91 | |
| 92 | protected function haveAlreadyThanked( User $thanker, string $uniqueId ): bool { |
| 93 | return $this->storage->haveThanked( $thanker, $uniqueId ); |
| 94 | } |
| 95 | |
| 96 | protected function logThanks( User $user, User $recipient, string $uniqueId ) { |
| 97 | $this->storage->thank( $user, $recipient, $uniqueId ); |
| 98 | } |
| 99 | |
| 100 | /** @inheritDoc */ |
| 101 | public function needsToken() { |
| 102 | return 'csrf'; |
| 103 | } |
| 104 | |
| 105 | /** @inheritDoc */ |
| 106 | public function isWriteMode() { |
| 107 | // Writes to the Echo database and sometimes log tables. |
| 108 | return true; |
| 109 | } |
| 110 | } |