fix(import): use sablog_articles.keywords for tags instead of sablog_tags.aids
This commit is contained in:
@@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user