Files
laysense.cn/site/fix-home-cursor.php

41 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
use App\Content\Database;
use App\Content\Media;
use App\Content\PageHead;
require dirname(__DIR__) . '/vendor/autoload.php';
$old = '86d3c75c2c280084ec6fcee11c501f45.js';
$new = '6354c518797c1214723222c27051d456.js';
$source = dirname(__DIR__) . '/storage/media/' . $new;
$changed = Database::write(static function (PDO $db) use ($old, $new, $source): bool {
$head = $db->query("SELECT head_html FROM pages WHERE slug = 'home'")->fetchColumn();
if (!is_string($head) || !str_contains($head, '/assets/uploads/' . $old)) {
return false;
}
$head = PageHead::normalize(str_replace('/assets/uploads/' . $old, '/assets/uploads/' . $new, $head));
$contents = file_get_contents($source);
if ($contents === false || substr(hash('sha256', $contents), 0, 32) . '.js' !== $new) {
throw new RuntimeException('首页动效文件缺失或校验失败。');
}
$destination = Media::directory() . '/' . $new;
if (is_file($destination)) {
if (hash_file('sha256', $destination) !== hash('sha256', $contents)) {
throw new RuntimeException('媒体库中同名文件的内容不一致。');
}
} elseif (!copy($source, $destination)) {
throw new RuntimeException('无法写入首页动效文件。');
}
$db->prepare("INSERT OR IGNORE INTO media (name, label, mime, size, created_at)
VALUES (?, 'laysense-home.js', 'text/javascript', ?, ?)")->execute([$new, strlen($contents), gmdate('c')]);
$db->prepare("UPDATE pages SET head_html = ?, version = version + 1 WHERE slug = 'home'")->execute([$head]);
return true;
});
echo $changed ? "首页光标已修复。\n" : "首页无需更新。\n";