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.

99 lines
2.9 KiB
PHP

<?php
namespace Isekai\UserPoints\Api;
use ApiQueryBase;
use ApiQuery;
use MediaWiki\Config\Config;
use MediaWiki\MediaWikiServices;
use WANObjectCache;
use Isekai\UserPoints\Service\IsekaiUserPointsFactory;
use Isekai\UserPoints\Utils;
use stdClass;
class ApiQueryUserPoints 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()->getConfigFactory()->makeConfig( 'textextracts' );
$this->cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
}
/**
* @throws \ApiUsageException
*/
public function execute() {
$user = $this->getUser();
if (!$user->isRegistered()) {
$this->dieWithError('apierror-mustbeloggedin-generic', 'login-required');
return false;
}
$pointConfig = $this->config->get('IsekaiUserPointConfig');
$pointTypes = array_keys($pointConfig);
/** @var IsekaiUserPointsFactory */
$userPointsFactory = MediaWikiServices::getInstance()->getService('IsekaiUserPoints');
/** @var array<IsekaiUserPoints> */
$userPoints = [];
foreach ($pointTypes as $pointType) {
$userPoint = $userPointsFactory->newFromUser($user, $pointType);
if ($userPoint) {
$userPoints[$pointType] = $userPointsFactory->newFromUser($user, $pointType);
}
}
if (count($userPoints) === 0) {
$this->dieWithError('apierror-isekaiuserpoints-notfound', 'not-found');
return false;
}
$result = $this->getResult();
foreach ($userPoints as $pointType => $userPoint) {
$pointName = Utils::getPointName($pointType);
$pointIcon = Utils::getPointIcon($pointType);
$result->addValue(['query', $this->getModuleName()], $pointType, [
'points' => $userPoint->points,
'timed_points_data' => $userPoint->timedPointsData,
'locked_points' => $userPoint->lockedPoints,
'locked_points_data' => $userPoint->lockedPointsData,
'name' => $pointName,
'icon' => $pointIcon,
]);
}
if (empty($userPoints)) {
$result->addValue(['query'], $this->getModuleName(), new stdClass());
}
}
/**
* @param array $params Ignored parameters
* @return string
*/
public function getCacheMode($params) {
return 'private';
}
public function getAllowedParams() {
return [];
}
}