Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ThanksQueryHelper
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 2
6
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getThanksReceivedCount
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2namespace MediaWiki\Extension\Thanks;
3
4use MediaWiki\Title\TitleFactory;
5use MediaWiki\User\UserIdentity;
6use Wikimedia\Rdbms\DBAccessObjectUtils;
7use Wikimedia\Rdbms\IConnectionProvider;
8use Wikimedia\Rdbms\IDBAccessObject;
9
10/**
11 * Query module
12 */
13class ThanksQueryHelper {
14    private TitleFactory $titleFactory;
15    private IConnectionProvider $dbProvider;
16
17    public function __construct( TitleFactory $titleFactory, IConnectionProvider $dbProvider ) {
18        $this->titleFactory = $titleFactory;
19        $this->dbProvider = $dbProvider;
20    }
21
22    /**
23     * Counts number of thanks a user has received. The query is not cached.
24     *
25     * @param UserIdentity $userIdentity
26     * @param int $limit cap the value of counts queried for performance
27     * @param int $flags database options. If calling in a POST context where a user is being thanked, the
28     *  return value will be incorrect if returned from replica. This allows you to query primary if the
29     *  exact number is important.
30     * @return int Number of thanks received for the user ID
31     */
32    public function getThanksReceivedCount(
33        UserIdentity $userIdentity,
34        int $limit = 1000,
35        int $flags = IDBAccessObject::READ_NORMAL
36    ): int {
37        $db = DBAccessObjectUtils::getDBFromRecency( $this->dbProvider, $flags );
38        $userPage = $this->titleFactory->newFromText( $userIdentity->getName(), NS_USER );
39        $logTitle = $userPage->getDBkey();
40        return $db->newSelectQueryBuilder()
41            ->table( 'logging' )
42            ->field( '1' )
43            // There is no type + target index, but there's a target index (log_page_time)
44            // and it's unlikely the user's page has many other log events than thanks,
45            // so the query should be okay.
46            ->conds( [
47                'log_type' => 'thanks',
48                'log_action' => 'thank',
49                'log_namespace' => NS_USER,
50                'log_title' => $logTitle,
51            ] )
52            ->limit( $limit )
53            ->caller( __METHOD__ )
54            ->fetchRowCount();
55    }
56}