Tłumaczenia - poprawki.
This commit is contained in:
+9
-21
@@ -31,30 +31,18 @@ if ($lang === '' || !isset($languages[$lang])) {
|
||||
|
||||
$langConfig = $languages[$lang];
|
||||
$source = (string) ($langConfig['source'] ?? 'upstream');
|
||||
$ttl = max(60, (int) ($config['ttl_seconds'] ?? 86400));
|
||||
$timeout = max(3, (int) ($config['timeout_seconds'] ?? 12));
|
||||
$cacheFile = menuCachePath($lang);
|
||||
|
||||
$cached = readMenuCache($cacheFile);
|
||||
$now = time();
|
||||
$cachedAtTs = 0;
|
||||
if ($cached && !empty($cached['cachedAt'])) {
|
||||
$cachedAtTs = strtotime((string) $cached['cachedAt']) ?: 0;
|
||||
}
|
||||
$isFresh = $cached && $cachedAtTs > 0 && ($now - $cachedAtTs) < $ttl;
|
||||
|
||||
if ($isFresh) {
|
||||
// Cache odświeżamy tylko ręcznie (panel admina / skrypt CLI) — nie fetchujemy upstreamu przy każdym TTL.
|
||||
if ($cached) {
|
||||
echo json_encode($cached, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($source === 'ai') {
|
||||
if ($cached) {
|
||||
// Stary AI-cache jest OK dłużej niż TTL — cron i tak odświeża raz/dzień.
|
||||
echo json_encode($cached, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
exit;
|
||||
}
|
||||
|
||||
$from = strtolower((string) ($langConfig['from'] ?? 'pl'));
|
||||
$fallback = readMenuCache(menuCachePath($from));
|
||||
if ($fallback) {
|
||||
@@ -67,7 +55,7 @@ if ($source === 'ai') {
|
||||
http_response_code(503);
|
||||
echo json_encode([
|
||||
'status' => 'error',
|
||||
'message' => 'AI menu not available yet — run scripts/refresh_menu_cache.php',
|
||||
'message' => 'AI menu not available yet — uruchom: php scripts/refresh_menu_cache.php --force-ai',
|
||||
'lang' => $lang,
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
@@ -78,20 +66,20 @@ $url = $base . '/' . rawurlencode($lang);
|
||||
$raw = fetchUpstreamMenu($url, $timeout);
|
||||
|
||||
if ($raw === null) {
|
||||
if ($cached) {
|
||||
echo json_encode($cached, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
exit;
|
||||
}
|
||||
http_response_code(502);
|
||||
echo json_encode([
|
||||
'status' => 'error',
|
||||
'message' => 'Failed to fetch menu',
|
||||
'message' => 'Failed to fetch menu (brak cache — uruchom odświeżenie w panelu)',
|
||||
'lang' => $lang,
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
$mapped = mapUpstreamMenu($raw, $lang, 'upstream');
|
||||
writeMenuCache($cacheFile, $mapped);
|
||||
try {
|
||||
writeMenuCache($cacheFile, $mapped);
|
||||
} catch (Throwable $e) {
|
||||
// Serwuj świeże dane nawet gdy cache nie da się zapisać
|
||||
}
|
||||
|
||||
echo json_encode($mapped, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
|
||||
+37
-5
@@ -6,6 +6,21 @@ declare(strict_types=1);
|
||||
* Wspólne funkcje cache/upstream menu (api/menu.php + scripts/refresh_menu_cache.php).
|
||||
*/
|
||||
|
||||
if (!function_exists('array_is_list')) {
|
||||
/** Polyfill PHP < 8.1 */
|
||||
function array_is_list(array $array): bool
|
||||
{
|
||||
$i = 0;
|
||||
foreach ($array as $k => $_) {
|
||||
if ($k !== $i++) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function menuCacheDir(): string
|
||||
{
|
||||
$dir = __DIR__ . '/cache';
|
||||
@@ -16,6 +31,20 @@ function menuCacheDir(): string
|
||||
return $dir;
|
||||
}
|
||||
|
||||
function assertMenuCacheWritable(): void
|
||||
{
|
||||
$dir = menuCacheDir();
|
||||
if (!is_dir($dir) || !is_writable($dir)) {
|
||||
$user = function_exists('posix_geteuid')
|
||||
? ((string) (@posix_getpwuid(posix_geteuid())['name'] ?? posix_geteuid()))
|
||||
: get_current_user();
|
||||
throw new RuntimeException(
|
||||
"Katalog cache nie jest zapisywalny: {$dir} (proces jako: {$user}). "
|
||||
. 'Na serwerze: chown -R www-data:www-data api/cache && chmod 775 api/cache'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function menuCachePath(string $lang): string
|
||||
{
|
||||
$safe = preg_replace('/[^a-z0-9_-]/i', '', strtolower($lang)) ?: 'pl';
|
||||
@@ -46,11 +75,14 @@ function readMenuCache(string $cacheFile): ?array
|
||||
|
||||
function writeMenuCache(string $cacheFile, array $payload): void
|
||||
{
|
||||
file_put_contents(
|
||||
$cacheFile,
|
||||
json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
|
||||
LOCK_EX
|
||||
);
|
||||
$json = json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
if ($json === false) {
|
||||
throw new RuntimeException('Nie udało się zserializować cache: ' . $cacheFile);
|
||||
}
|
||||
$ok = @file_put_contents($cacheFile, $json, LOCK_EX);
|
||||
if ($ok === false) {
|
||||
throw new RuntimeException('Permission denied przy zapisie: ' . $cacheFile);
|
||||
}
|
||||
}
|
||||
|
||||
function fetchUpstreamMenu(string $url, int $timeout): ?array
|
||||
|
||||
Reference in New Issue
Block a user