Files
jccp_a/laravel/app/ContentCenter/ReformatPublished.php
T

82 lines
4.2 KiB
PHP

<?php
namespace App\ContentCenter;
use App\Services\Ranking\ArticleDocument;
use App\Services\Ranking\SitePublisher;
use App\Services\Ranking\WeeklyRanking;
/** Explicit operator repair; no public HTTP route. */
final class ReformatPublished
{
public function run(array $job): array
{
validator($job, [
'approval_id' => 'required|uuid',
'expected_hash' => 'required|regex:/^[a-f0-9]{64}$/D',
'replacement_hash' => 'sometimes|required|regex:/^[a-f0-9]{64}$/D',
'old_artifact' => 'required|array',
'input' => 'required|array',
])->validate();
$old = $job['old_artifact'];
abort_unless(hash_equals(ArticleArtifact::hash($old), $job['expected_hash']), 409);
$p = new SitePublisher(config('content_center.site_root'), config('content_center.node'));
return $p->locked(function () use ($job, $old, $p) {
$artifact = app(ArticleArtifact::class)->build($job['input']);
$hash = ArticleArtifact::hash($artifact);
if (isset($job['replacement_hash'])) {
abort_unless(hash_equals($job['replacement_hash'], $hash), 409);
}
foreach (['source_key', 'revision', 'generation', 'images', 'relative_path', 'permalink'] as $f) {
abort_unless($old[$f] === $artifact[$f], 409);
}
$dir = $p->root.'/laravel/storage/app/private/content-center/publications';
$key = hash('sha256', $artifact['source_key']);
abort_if(is_file($dir.'/deleted-'.$key.'.json'), 409);
$path = $p->root.'/vuepress/docs/'.$artifact['relative_path'];
$files = [$dir.'/'.$key.'.json', $dir.'/'.$job['approval_id'].'.json'];
abort_unless(is_file($path) && is_file($files[0]) && is_file($files[1]), 409);
$records = array_map(fn ($f) => json_decode(file_get_contents($f), true, flags: JSON_THROW_ON_ERROR), $files);
foreach ($records as $x) {
abort_unless($x['status'] === 'published' && $x['approval_id'] === $job['approval_id'] && $x['source_key'] === $artifact['source_key'], 409);
}
$current = file_get_contents($path);
$siteMarkdown = Publication::siteMarkdown($artifact['markdown']);
if ($records[0]['artifact_hash'] === $hash && $records[1]['artifact_hash'] === $hash) {
abort_unless(in_array($current, [$artifact['markdown'], $siteMarkdown], true), 409);
return ['artifact' => $artifact, 'artifact_hash' => $hash, 'receipt' => $records[0], 'changed' => false];
}
$originalMarkdown = [$old['markdown'], Publication::siteMarkdown($old['markdown'])];
abort_unless($records[0]['artifact_hash'] === $job['expected_hash']
&& $records[1]['artifact_hash'] === $job['expected_hash']
&& in_array($current, $originalMarkdown, true), 409);
$backup = $dir.'/reformat-'.$job['approval_id'].'-'.$hash.'.json';
if (! is_file($backup)) {
ArticleDocument::write($backup, json_encode(['artifact' => $old, 'site_markdown' => $current, 'records' => $records], JSON_THROW_ON_ERROR));
chmod($backup, 0600);
}
try {
ArticleDocument::write($path, $siteMarkdown);
(new WeeklyRanking($p))->renderCurrent();
$p->build();
} catch (\Throwable $e) {
ArticleDocument::write($path, $current);
throw new \RuntimeException('格式修复构建失败,原文章已恢复。', 0, $e);
}
$record = $records[0];
$record['previous_artifact_hash'] = $job['expected_hash'];
$record['artifact_hash'] = $hash;
$record['format_version'] = $artifact['format_version'];
$record['reformatted_at'] = now()->toIso8601String();
foreach ($files as $f) {
ArticleDocument::write($f, json_encode($record, JSON_THROW_ON_ERROR));
chmod($f, 0600);
}
return ['artifact' => $artifact, 'artifact_hash' => $hash, 'receipt' => $record, 'changed' => true];
});
}
}