Files

69 lines
2.2 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* 简航 SimpleNav —— 一键“全部更新”(分批)。
* 批量刷新每个网站的名称与图标;
* · **保留** 用户自定义简称 short_name
* · **跳过** 用户自定义图标(icon_custom=1)——只刷新名称
* 前端按 offset 分批调用,避免一次请求超时。
* 返回:{ done, total, offset, updated, skipped, failed }
*/
declare(strict_types=1);
require_once __DIR__ . '/_auth.php';
require_login();
send_no_cache();
header('Content-Type: application/json; charset=utf-8');
set_time_limit(90);
$offset = max(0, (int)($_GET['offset'] ?? 0));
$limit = min(12, max(1, (int)($_GET['limit'] ?? 3)));
$pdo = db();
$total = (int)$pdo->query('SELECT COUNT(*) FROM sites')->fetchColumn();
$rows = $pdo->query('SELECT id, url, icon_custom FROM sites ORDER BY id LIMIT ' . $limit . ' OFFSET ' . $offset)->fetchAll();
$updated = 0;
$skipped = 0;
$failed = 0;
$now = date('Y-m-d H:i:s');
foreach ($rows as $row) {
$url = norm_url((string)$row['url']);
$custom = (int)$row['icon_custom'] === 1;
$meta = fetch_site_meta($url);
$name = $meta['name'] !== '' ? $meta['name'] : $meta['title'];
$sets = ['updated_at=?'];
$args = [$now];
if ($name !== '') { $sets[] = 'name=?'; $args[] = $name; }
if ($custom) {
// 用户自定义图标:绝不覆盖
$skipped++;
} else {
// 键取自记录里存的那条 URL(不用 final_url),保证每次算出来都一样
$iconFile = download_best_icon($meta['icons'], icon_key($url));
if ($iconFile !== '') { $sets[] = 'icon=?'; $args[] = $iconFile; }
if ($iconFile !== '' || $name !== '') { $updated++; } else { $failed++; }
}
// 注意:这里绝不修改 short_name(用户自定义简称)
$args[] = (int)$row['id'];
try {
$pdo->prepare('UPDATE sites SET ' . implode(',', $sets) . ' WHERE id=?')->execute($args);
} catch (Throwable $e) {
$failed++;
}
}
$next = $offset + count($rows);
echo json_encode([
'done' => $next >= $total || count($rows) === 0,
'total' => $total,
'offset' => $next,
'updated' => $updated,
'skipped' => $skipped,
'failed' => $failed,
], JSON_UNESCAPED_UNICODE);