You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.5 KiB
PHTML

2 years ago
<?php
namespace Isekai\UserPoints\Api;
use ApiQueryBase;
use ApiQuery;
use MediaWiki\Config\Config;
2 years ago
use MediaWiki\MediaWikiServices;
use WANObjectCache;
use Wikimedia\ParamValidator\ParamValidator;
use Isekai\UserPoints\Service\IsekaiUserPointsFactory;
use Isekai\UserPoints\Utils;
class ApiQueryUsersPoints extends ApiQueryBase {
private const CACHE_VERSION = 2;
private const PREFIX = 'up';
private $params;
/**
* @var Config
*/
private $config;
/**
* @var WANObjectCache
*/
private $cache;
/**
* @param ApiQuery $query API query module object
* @param string $moduleName Name of this query module
*/
public function __construct($query, $moduleName) {
parent::__construct($query, $moduleName, self::PREFIX);
$this->config = MediaWikiServices::getInstance()->getMainConfig();
$this->cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
}
/**
* @throws \ApiUsageException
*/
public function execute() {
$this->checkUserRightsAny('queryuserpoints');
$params = $this->extractRequestParams();
$this->requireMaxOneParameter( $params, 'userids', 'users' );
$users = (array)$params['users'];
$userids = (array)$params['userids'];
$services = MediaWikiServices::getInstance();
$pointConfig = $this->config->get('IsekaiUserPointConfig');
$pointTypes = array_keys($pointConfig);
$pointInfos = [];
foreach ($pointTypes as $pointType) {
$pointInfos[$pointType] = [
'name' => Utils::getPointName($pointType),
'icon' => Utils::getPointIcon($pointType),
];
}
/** @var IsekaiUserPointsFactory */
$userPointsFactory = $services->getService('IsekaiUserPoints');
$userFactory = $services->getUserFactory();
$userInstances = [];
foreach ( $users as $u ) {
$user = $userFactory->newFromName( $u );
$userInstances[] = $user;
}
foreach ( $userids as $u ) {
$user = $userFactory->newFromId( $u );
$userInstances[] = $user;
}
$userPointsInstances = $userPointsFactory->newFromUsers($userInstances, $pointTypes);
$result = $this->getResult();
foreach ($userPointsInstances as $userPointsTuple) {
if ( !$userPointsTuple ) {
continue;
}
2 years ago
list($user, $type, $userPoint) = $userPointsTuple;
$result->addValue(
[ 'query', $this->getModuleName(), "pointdata", $user->getId() ],
$type,
[
'point_type' => $type,
'points' => $userPoint->points,
'timed_points_data' => $userPoint->timedPointsData,
'locked_points' => $userPoint->lockedPoints,
'locked_points_data' => $userPoint->lockedPointsData,
]
);
}
$result->addValue(
[ 'query', $this->getModuleName() ],
'pointinfo',
$pointInfos
);
}
/**
* @param array $params Ignored parameters
* @return string
*/
public function getCacheMode($params) {
return 'public';
}
public function getAllowedParams() {
return [
'users' => [
ParamValidator::PARAM_ISMULTI => true
],
'userids' => [
ParamValidator::PARAM_ISMULTI => true,
ParamValidator::PARAM_TYPE => 'integer'
],
];
}
}