diff --git a/app/Console/Commands/SablogImport.php b/app/Console/Commands/SablogImport.php index 32d3b10..eaef55a 100644 --- a/app/Console/Commands/SablogImport.php +++ b/app/Console/Commands/SablogImport.php @@ -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); } }