Split migrated document posts into guides and story

This commit is contained in:
2026-02-10 20:26:57 +08:00
parent 097e332608
commit c4969cd4d2

View File

@@ -24,16 +24,24 @@ class ReclassifyDocumentContent extends Command
'document-d34b35ab',
];
/**
* Move from document to blog/guides.
*
* @var array<int, string>
*/
private array $moveToGuidesSlugs = [
'document-b81734e3',
'document-25e793fb',
'document-48ec7f25',
];
/**
* Move from document to blog/story.
*
* @var array<int, string>
*/
private array $moveToStorySlugs = [
'document-b81734e3',
'document-2f8fad62',
'document-25e793fb',
'document-48ec7f25',
'document-f68da0b8',
'document-a8bdf6d9',
'document-694e4dd6',
@@ -46,7 +54,11 @@ class ReclassifyDocumentContent extends Command
{
$dryRun = (bool) $this->option('dry-run');
$allSlugs = array_values(array_unique(array_merge($this->keepDocumentSlugs, $this->moveToStorySlugs)));
$allSlugs = array_values(array_unique(array_merge(
$this->keepDocumentSlugs,
$this->moveToGuidesSlugs,
$this->moveToStorySlugs
)));
$articles = Article::query()
->whereIn('slug', $allSlugs)
->get()
@@ -63,10 +75,15 @@ class ReclassifyDocumentContent extends Command
['slug' => 'story'],
['name' => '故事', 'sort_order' => 30]
);
$guidesCategory = ArticleCategory::firstOrCreate(
['slug' => 'guides'],
['name' => '建議與指引', 'sort_order' => 25]
);
$this->newLine();
$this->info('Reclassification plan');
$this->line(' Keep as document: '.count($this->keepDocumentSlugs));
$this->line(' Move to guides/blog: '.count($this->moveToGuidesSlugs));
$this->line(' Move to story/blog: '.count($this->moveToStorySlugs));
$this->line(' Found in DB: '.$articles->count());
@@ -80,12 +97,13 @@ class ReclassifyDocumentContent extends Command
collect($allSlugs)->map(function (string $slug) use ($articles) {
$article = $articles->get($slug);
$targetIsDocument = in_array($slug, $this->keepDocumentSlugs, true);
$targetIsGuides = in_array($slug, $this->moveToGuidesSlugs, true);
return [
$slug,
$article?->content_type ?? '(missing)',
$targetIsDocument ? Article::CONTENT_TYPE_DOCUMENT : Article::CONTENT_TYPE_BLOG,
$targetIsDocument ? 'document' : 'story',
$targetIsDocument ? 'document' : ($targetIsGuides ? 'guides' : 'story'),
];
})->all()
);
@@ -97,7 +115,7 @@ class ReclassifyDocumentContent extends Command
return static::SUCCESS;
}
DB::transaction(function () use ($articles, $documentCategory, $storyCategory): void {
DB::transaction(function () use ($articles, $documentCategory, $guidesCategory, $storyCategory): void {
foreach ($this->keepDocumentSlugs as $slug) {
$article = $articles->get($slug);
if (! $article) {
@@ -108,6 +126,16 @@ class ReclassifyDocumentContent extends Command
$article->categories()->sync([$documentCategory->id]);
}
foreach ($this->moveToGuidesSlugs as $slug) {
$article = $articles->get($slug);
if (! $article) {
continue;
}
$article->update(['content_type' => Article::CONTENT_TYPE_BLOG]);
$article->categories()->sync([$guidesCategory->id]);
}
foreach ($this->moveToStorySlugs as $slug) {
$article = $articles->get($slug);
if (! $article) {