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 = []; $legacyMap = [];
$count = 0; $count = 0;
foreach ($this->rows('tags') as $row) {
$tagName = trim($row['tag']); // 正确的标签来源:sablog_articles.keywords(每篇文章的关键词字段)
if (! $tagName) { // 而不是 sablog_tags.aids(那个映射是错的/过时的)
foreach ($this->rows('articles') as $row) {
$keywords = trim((string) ($row['keywords'] ?? ''));
if ($keywords === '') {
continue; continue;
} }
$tag = Tag::query()->where('name', $tagName)->where('type', 'post')->first(); $postId = (int) $row['articleid'];
if (! $tag) { $post = Post::find($postId);
$tag = new Tag(['name' => $tagName, 'type' => 'post']); if (! $post) {
$tag->slug = SlugGenerator::make($tagName, 'tags', 'slug', fallback: 'tag-'.crc32($tagName)); continue;
$tag->save();
} }
$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) { $tag = Tag::query()->where('name', $tagName)->where('type', 'post')->first();
$post = Post::find((int) trim($aid)); if (! $tag) {
if ($post && ! $post->tags()->where('tags.id', $tag->id)->exists()) { $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); $post->tags()->attach($tag->id);
} }
} }