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.

97 lines
3.0 KiB
PHP

<?php
namespace Isekai\OIDC;
use ApiQueryBase;
use MediaWiki\MediaWikiServices;
use Wikimedia\ParamValidator\ParamValidator;
class ApiOIDCAvatar extends ApiQueryBase {
public function __construct( $main, $method ) {
parent::__construct( $main, $method );
}
public function execute() {
$services = MediaWikiServices::getInstance();
$config = $services->getMainConfig();
$wgIsekaiOIDC = $config->get('IsekaiOIDC');
//$this->requireOnlyOneParameter('username', 'userid');
$userid = $this->getParameter('userid');
$username = $this->getParameter('username');
$userFactory = $services->getUserFactory();
if ($userid) {
$user = $userFactory->newFromId($userid);
} else {
$user = $userFactory->newFromName($username);
}
if (!$user) {
$this->dieWithError('isekaioidc-api-user-not-found', 'isekaioidc-api-user-not-found');
}
$size = $this->getParameter('size');
$avatars = [];
$hookContainer = $services->getHookContainer();
$hookContainer->run('Isekai::GetUsersAvatar', [&$avatars, [$user], $size]);
$avatarUrl = '';
if (isset($avatars[$user->getId()])) {
$avatarUrl = $avatars[$user->getId()];
} else {
if (isset($wgIsekaiOIDC['defaultAvatarUrl'])) {
$avatarUrl = $wgIsekaiOIDC['defaultAvatarUrl'];
} else {
$this->dieWithError('isekaioidc-api-user-avatar-not-found', 'isekaioidc-api-user-avatar-not-found', [
'userid' => $userid,
'username' => $username,
]);
}
}
if ($this->getParameter('redirect')) {
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $avatarUrl);
header('Cache-Control: public, max-age=86400');
exit();
} else {
$this->getResult()->addValue('query', 'oidcavatar', $avatarUrl);
}
}
public function getAllowedParams() {
return [
'username' => [
ParamValidator::PARAM_DEFAULT => null,
ParamValidator::PARAM_TYPE => 'string',
],
'userid' => [
ParamValidator::PARAM_DEFAULT => null,
ParamValidator::PARAM_TYPE => 'integer',
],
'size' => [
ParamValidator::PARAM_DEFAULT => 128,
ParamValidator::PARAM_TYPE => 'integer',
],
'redirect' => [
ParamValidator::PARAM_DEFAULT => false,
ParamValidator::PARAM_TYPE => 'boolean',
]
];
}
public function getExamplesMessages() {
return [
'action=query&prop=oidcavatar&username=Example&size=128' => 'isekaioidc-api-example-isekaioidcavatar',
];
}
public function getCacheMode($params) {
return 'public';
}
}