mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Thanks
synced 2024-11-15 02:54:23 +00:00
Adds the ThanksQueryHelper service
Bug: T325094 Change-Id: I3a14d255546c12655dc4492194a07b0d205f0534
This commit is contained in:
parent
a89ede970e
commit
e85b2c6996
15
ServiceWiring.php
Normal file
15
ServiceWiring.php
Normal file
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
use MediaWiki\Extension\Thanks\ThanksQueryHelper;
|
||||
use MediaWiki\MediaWikiServices;
|
||||
|
||||
return [
|
||||
'ThanksQueryHelper' => static function (
|
||||
MediaWikiServices $services
|
||||
): ThanksQueryHelper {
|
||||
return new ThanksQueryHelper(
|
||||
$services->getTitleFactory(),
|
||||
$services->getDBLoadBalancer()
|
||||
);
|
||||
},
|
||||
];
|
|
@ -180,6 +180,9 @@
|
|||
"LogEventsListLineEnding": "MediaWiki\\Extension\\Thanks\\Hooks::onLogEventsListLineEnding",
|
||||
"PageHistoryBeforeList": "MediaWiki\\Extension\\Thanks\\Hooks::onPageHistoryBeforeList"
|
||||
},
|
||||
"ServiceWiringFiles": [
|
||||
"ServiceWiring.php"
|
||||
],
|
||||
"config": {
|
||||
"ThanksSendToBots": {
|
||||
"value": false
|
||||
|
|
59
includes/ThanksQueryHelper.php
Normal file
59
includes/ThanksQueryHelper.php
Normal file
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
namespace MediaWiki\Extension\Thanks;
|
||||
|
||||
use DBAccessObjectUtils;
|
||||
use IDBAccessObject;
|
||||
use MediaWiki\Title\TitleFactory;
|
||||
use MediaWiki\User\UserIdentity;
|
||||
use Wikimedia\Rdbms\ILoadBalancer;
|
||||
|
||||
/**
|
||||
* Query module
|
||||
*/
|
||||
class ThanksQueryHelper {
|
||||
/** @var TitleFactory */
|
||||
private TitleFactory $titleFactory;
|
||||
/** @var ILoadBalancer */
|
||||
private ILoadBalancer $loadBalancer;
|
||||
|
||||
public function __construct( TitleFactory $titleFactory, ILoadBalancer $loadBalancer ) {
|
||||
$this->titleFactory = $titleFactory;
|
||||
$this->loadBalancer = $loadBalancer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts number of thanks a user has received. The query is not cached.
|
||||
*
|
||||
* @param UserIdentity $userIdentity
|
||||
* @param int $limit cap the value of counts queried for performance
|
||||
* @param int $flags database options. If calling in a POST context where a user is being thanked, the
|
||||
* return value will be incorrect if returned from replica. This allows you to query primary if the
|
||||
* exact number is important.
|
||||
* @return int Number of thanks received for the user ID
|
||||
*/
|
||||
public function getThanksReceivedCount(
|
||||
UserIdentity $userIdentity,
|
||||
int $limit = 1000,
|
||||
int $flags = IDBAccessObject::READ_NORMAL
|
||||
): int {
|
||||
$loadBalancer = $this->loadBalancer;
|
||||
list( $index, $options ) = DBAccessObjectUtils::getDBOptions( $flags );
|
||||
$db = $loadBalancer->getConnection( $index );
|
||||
$userPage = $this->titleFactory->newFromText( $userIdentity->getName(), NS_USER );
|
||||
$logTitle = $userPage->getDBkey();
|
||||
return $db->newSelectQueryBuilder()
|
||||
->table( 'logging' )
|
||||
->field( '1' )
|
||||
// There is no type + target index, but there's a target index (log_page_time)
|
||||
// and it's unlikely the user's page has many other log events than thanks,
|
||||
// so the query should be okay.
|
||||
->conds( [
|
||||
'log_type' => 'thanks',
|
||||
'log_action' => 'thank',
|
||||
'log_namespace' => NS_USER,
|
||||
'log_title' => $logTitle,
|
||||
] )
|
||||
->limit( $limit )
|
||||
->fetchRowCount();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue