87 lines
2.8 KiB
PowerShell
87 lines
2.8 KiB
PowerShell
|
||||
|
|
<#
|
|||
|
|
.SYNOPSIS
|
|||
|
|
修复 Artalk 导出文件 (.artrans/JSON) 中的 ID 类型问题。
|
|||
|
|
将所有 id 和 rid 字段转换为整数类型,解决父子评论关系丢失的问题。
|
|||
|
|
|
|||
|
|
.DESCRIPTION
|
|||
|
|
Artalk 的某些版本/配置对 ID 的类型敏感(字符串 vs 整数)。
|
|||
|
|
此脚本读取 JSON 文件,强制将 comments 数组中的 id 和 rid 转换为 Int64,
|
|||
|
|
并保存为新的文件。
|
|||
|
|
|
|||
|
|
.PARAMETER InputFile
|
|||
|
|
输入的 .artrans 或 .json 文件路径。
|
|||
|
|
|
|||
|
|
.PARAMETER OutputFile
|
|||
|
|
(可选) 输出文件路径。如果不指定,将添加 "_fixed" 后缀。
|
|||
|
|
|
|||
|
|
.EXAMPLE
|
|||
|
|
.\fix_artalk_json.ps1 -InputFile "backup.artrans"
|
|||
|
|
.\fix_artalk_json.ps1 -InputFile "backup.artrans" -OutputFile "fixed.artrans"
|
|||
|
|
#>
|
|||
|
|
|
|||
|
|
param (
|
|||
|
|
[Parameter(Mandatory=$true)]
|
|||
|
|
[string]$InputFile,
|
|||
|
|
|
|||
|
|
[Parameter(Mandatory=$false)]
|
|||
|
|
[string]$OutputFile
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# 检查输入文件是否存在
|
|||
|
|
if (-not (Test-Path $InputFile)) {
|
|||
|
|
Write-Error "错误: 找不到输入文件 '$InputFile'"
|
|||
|
|
exit 1
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
# 如果未指定输出文件,自动生成
|
|||
|
|
if ([string]::IsNullOrWhiteSpace($OutputFile)) {
|
|||
|
|
$directory = [System.IO.Path]::GetDirectoryName($InputFile)
|
|||
|
|
$filename = [System.IO.Path]::GetFileNameWithoutExtension($InputFile)
|
|||
|
|
$extension = [System.IO.Path]::GetExtension($InputFile)
|
|||
|
|
$OutputFile = Join-Path $directory "${filename}_fixed${extension}"
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Write-Host "正在读取文件: $InputFile ..." -ForegroundColor Cyan
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
# 读取并解析 JSON
|
|||
|
|
$jsonContent = Get-Content -Path $InputFile -Raw -Encoding UTF8
|
|||
|
|
$data = $jsonContent | ConvertFrom-Json
|
|||
|
|
|
|||
|
|
# 检查是否存在 comments 数组
|
|||
|
|
if (-not $data.comments) {
|
|||
|
|
Write-Warning "未在文件中找到 'comments' 数组。这可能不是有效的 Artalk 导出文件。"
|
|||
|
|
} else {
|
|||
|
|
$count = $data.comments.Count
|
|||
|
|
Write-Host "找到 $count 条评论,正在处理..." -ForegroundColor Cyan
|
|||
|
|
|
|||
|
|
# 遍历并转换类型
|
|||
|
|
foreach ($comment in $data.comments) {
|
|||
|
|
# 强制转换为 Int64 (long)
|
|||
|
|
if ($comment.id -ne $null) {
|
|||
|
|
$comment.id = [int64]$comment.id
|
|||
|
|
}
|
|||
|
|
if ($comment.rid -ne $null) {
|
|||
|
|
$comment.rid = [int64]$comment.rid
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Write-Host "正在保存到: $OutputFile ..." -ForegroundColor Cyan
|
|||
|
|
|
|||
|
|
# 转换为 JSON 并保存
|
|||
|
|
# Depth 100 防止深层嵌套被截断
|
|||
|
|
# Compress 选项可减小体积,但为了可读性这里不使用(Artalk 导出通常也是展开的)
|
|||
|
|
$newJson = $data | ConvertTo-Json -Depth 100
|
|||
|
|
|
|||
|
|
# 确保使用 UTF8 保存
|
|||
|
|
[System.IO.File]::WriteAllText($OutputFile, $newJson, [System.Text.Encoding]::UTF8)
|
|||
|
|
|
|||
|
|
Write-Host "修复完成!" -ForegroundColor Green
|
|||
|
|
Write-Host "请使用 Artalk 后台导入 '$OutputFile'。" -ForegroundColor Green
|
|||
|
|
|
|||
|
|
} catch {
|
|||
|
|
Write-Error "处理过程中发生错误: $_"
|
|||
|
|
exit 1
|
|||
|
|
}
|