Debian Codegraph @

extensions/Thanks/includes/ThanksQueryHelper.php

raw symbols SCIP
1 <?php
2 namespace MediaWiki\Extension\Thanks;
3
4 use MediaWiki\Title\TitleFactory;
5 use MediaWiki\User\UserIdentity;
6 use Wikimedia\Rdbms\DBAccessObjectUtils;
7 use Wikimedia\Rdbms\IConnectionProvider;
8 use Wikimedia\Rdbms\IDBAccessObject;
9
10 /**
11 * Query module
12 */
13 class 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 'log_deleted' => 0,
52 ] )
53 ->limit( $limit )
54 ->caller( __METHOD__ )
55 ->fetchRowCount();
56 }
57 }