fix(import): use sablog_articles.keywords for tags instead of sablog_tags.aids
Build and Deploy / verify (push) Successful in 33s
Build and Deploy / deploy (push) Successful in 37s

This commit is contained in:
2026-10-11 19:25:36 +00:00
parent debead72a5
commit faf70ec5f4
+26 -12
View File
@@ -318,24 +318,38 @@ class SablogImport extends Command
{
$legacyMap = [];
$count = 0;
foreach ($this->rows('tags') as $row) {
$tagName = trim($row['tag']);
if (! $tagName) {
// 正确的标签来源:sablog_articles.keywords(每篇文章的关键词字段)
// 而不是 sablog_tags.aids(那个映射是错的/过时的)
foreach ($this->rows('articles') as $row) {
$keywords = trim((string) ($row['keywords'] ?? ''));
if ($keywords === '') {
continue;
}
$tag = Tag::query()->where('name', $tagName)->where('type', 'post')->first();
if (! $tag) {
$tag = new Tag(['name' => $tagName, 'type' => 'post']);
$tag->slug = SlugGenerator::make($tagName, 'tags', 'slug', fallback: 'tag-'.crc32($tagName));
$tag->save();
$postId = (int) $row['articleid'];
$post = Post::find($postId);
if (! $post) {
continue;
}
$legacyMap[(int) $row['tagid']] = $tag->id;
// keywords 是逗号分隔的标签名
$tagNames = array_filter(array_map('trim', explode(',', $keywords)));
foreach ($tagNames as $tagName) {
if ($tagName === '') {
continue;
}
foreach (explode(',', (string) $row['aids']) as $aid) {
$post = Post::find((int) trim($aid));
if ($post && ! $post->tags()->where('tags.id', $tag->id)->exists()) {
$tag = Tag::query()->where('name', $tagName)->where('type', 'post')->first();
if (! $tag) {
$tag = new Tag(['name' => $tagName, 'type' => 'post']);
$tag->slug = SlugGenerator::make($tagName, 'tags', 'slug', fallback: 'tag-'.crc32($tagName));
$tag->save();
}
$legacyMap[$tagName] = $tag->id;
if (! $post->tags()->where('tags.id', $tag->id)->exists()) {
$post->tags()->attach($tag->id);
}
}