vlw.se/api/endpoints/about/languages/POST.php
Victor Westerlund e25b1b6689 feat: add language chart to about page (#14)
Replaces this section on the `/about` page:
![image](/attachments/67ac2f42-3784-4c69-9240-0a7961afb47d)
with:
![image](/attachments/fa073c9c-a016-4281-a3fb-30b7be95881f)

I will replace and fix the colors of the buttons after #15 is merged.

Reviewed-on: https://codeberg.org/vlw/vlw.se/pulls/14
2025-01-28 14:45:52 +00:00

97 lines
No EOL
2.8 KiB
PHP

<?php
use Reflect\Call;
use Reflect\Path;
use Reflect\Response;
const ERRNO_CACHE_FAILED = 0;
const FORGEJO_ENDPOINT_USER = "/api/v1/users/%s";
const FORGEJO_ENDPOINT_SEARCH = "/api/v1/repos/search?uid=%s";
class POST_AboutLanguages {
private array $errors = [];
// Tally of all languages used in all configured repositories
private array $languages = [];
public function __construct() {}
// Fetch JSON from URL
private static function fetch_json(string $url): array {
return json_decode(file_get_contents($url), true);
}
// Fetch JSON from a Forgejo endpoint
private static function fetch_endpoint(string $endpoint): array {
$url = $_ENV["forgejo"]["base_url"] . $endpoint;
return self::fetch_json($url);
}
// Write $this->languages to a JSON file
private function cache_languages(): int|bool {
$cache_filename = $_ENV["forgejo_languages"]["cache_file"];
// Bail out if cache file is not configured
if (empty($cache_filename)) {
return true;
}
return file_put_contents($cache_filename, json_encode($this->languages));
}
// Fetch and add languages to total from a fully-qualified Forgejo URL
private function add_repository_languages(string $url): void {
foreach(self::fetch_json($url) as $language => $bytes) {
// Create key for language if it doesn't exist
if (!array_key_exists($language, $this->languages)) {
$this->languages[$language] = 0;
}
// Add bytes to language in total
$this->languages[$language] += $bytes;
}
}
// Tally languages from public repositories for user id
private function add_public_repositores(int $uid): bool {
$resp = self::fetch_endpoint(sprintf(FORGEJO_ENDPOINT_SEARCH, $uid));
// Bail out if request failed or if response indicated a problem
if (!$resp or $resp["ok"] === false) {
return false;
}
// Add langauges for each public repository
foreach ($resp["data"] as $repo) {
$this->add_repository_languages($repo["languages_url"]);
}
return true;
}
// Add languages from all public repositories for profiles in config
private function add_repositories_from_config_profiles(): void {
foreach(explode(",", $_ENV["forgejo_languages"]["scan_profiles"]) as $profile) {
// Resolve user data from username
$user = self::fetch_endpoint(sprintf(FORGEJO_ENDPOINT_USER, $profile));
if (!$this->add_public_repositores($user["id"])) {
$this->errors[] = $profile;
}
}
}
public function main(): Response {
$this->add_repositories_from_config_profiles();
// Sort langauges bytes tally by largest in descending order
arsort($this->languages);
// Save languages to cache
if (!$this->cache_languages()) {
$this->errors[] = ERRNO_CACHE_FAILED;
}
return new Response($this->languages);
}
}