81 lines
3.3 KiB
PHP
81 lines
3.3 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
// FUC-XB cutover only. The caller owns the target transaction and checks the instance.
|
|
function fucAnnouncementRows(PDO $source, DateTimeZone $timezone): array
|
|
{
|
|
$rows = $source->query(
|
|
'SELECT id, status, sort, date, content, title, rank
|
|
FROM announcement ORDER BY sort DESC, date DESC, id DESC'
|
|
);
|
|
return fucProjectAnnouncements($rows, $timezone);
|
|
}
|
|
|
|
function fucProjectAnnouncements(iterable $rows, DateTimeZone $timezone): array
|
|
{
|
|
$notices = [];
|
|
foreach ($rows as $row) {
|
|
$id = (int) $row['id'];
|
|
$date = DateTimeImmutable::createFromFormat('!Y-m-d H:i:s', $row['date'], $timezone);
|
|
if ($id < 1 || !$date || $date->format('Y-m-d H:i:s') !== $row['date']
|
|
|| !in_array((string) $row['status'], ['0', '1'], true) || strlen($row['content']) > 65535
|
|
|| mb_strlen($row['title']) > 255) {
|
|
throw new RuntimeException('Announcement cannot be represented in the XB schema');
|
|
}
|
|
$tags = [];
|
|
if ((int) $row['sort'] > 0) $tags[] = '旧站置顶';
|
|
if ($row['rank'] && $row['rank'] !== 'none') $tags[] = $row['rank'];
|
|
$notices[] = [
|
|
'id' => $id,
|
|
'sort' => count($notices) + 1,
|
|
'title' => trim($row['title']) === '' ? '旧站公告 ' . $id : $row['title'],
|
|
'content' => $row['content'],
|
|
'show' => (int) $row['status'],
|
|
'tags' => $tags ? json_encode($tags, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR) : null,
|
|
'created_at' => $date->getTimestamp(),
|
|
'updated_at' => $date->getTimestamp(),
|
|
];
|
|
}
|
|
return $notices;
|
|
}
|
|
|
|
function fucImportAnnouncements(PDO $target, array $notices): array
|
|
{
|
|
if (!$target->inTransaction()) throw new LogicException('An active cutover transaction is required');
|
|
$select = $target->query(
|
|
'SELECT id, sort, title, content, `show`, tags, created_at, updated_at
|
|
FROM v2_notice ORDER BY sort, id DESC FOR UPDATE'
|
|
);
|
|
$existing = $select->fetchAll(PDO::FETCH_ASSOC);
|
|
$select->closeCursor();
|
|
$normalize = static function (array $row): array {
|
|
foreach (['id', 'sort', 'show', 'created_at', 'updated_at'] as $key) $row[$key] = (int) $row[$key];
|
|
if ($row['tags'] !== null) $row['tags'] = json_decode($row['tags'], true, 8, JSON_THROW_ON_ERROR);
|
|
return $row;
|
|
};
|
|
$expected = array_map($normalize, $notices);
|
|
$inserted = 0;
|
|
if (!$existing) {
|
|
$insert = $target->prepare(
|
|
'INSERT INTO v2_notice (id, sort, title, content, `show`, tags, created_at, updated_at)
|
|
VALUES (:id, :sort, :title, :content, :show, :tags, :created_at, :updated_at)'
|
|
);
|
|
foreach ($notices as $notice) {
|
|
$insert->execute($notice);
|
|
$inserted++;
|
|
}
|
|
$select->execute();
|
|
$existing = $select->fetchAll(PDO::FETCH_ASSOC);
|
|
$select->closeCursor();
|
|
}
|
|
if (array_map($normalize, $existing) !== $expected) {
|
|
throw new RuntimeException('XB announcement readback differs or the target contains existing notices');
|
|
}
|
|
return [
|
|
'total' => count($notices),
|
|
'visible' => array_sum(array_column($notices, 'show')),
|
|
'inserted' => $inserted,
|
|
'sha256' => hash('sha256', json_encode($expected, JSON_THROW_ON_ERROR)),
|
|
];
|
|
}
|