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