This commit is contained in:
Ty Clifford
2026-07-04 20:14:52 -04:00
parent 94aea8501f
commit bd0bfdc09f
4 changed files with 751 additions and 0 deletions
+42
View File
@@ -87,6 +87,42 @@ final class CommentService
];
}
/** @param array<string, mixed> $data */
public function import(string $slug, array $data): int
{
$createdAt = $this->normalizeDate((string) ($data['created_at'] ?? $data['date'] ?? ''));
$updatedAt = $this->normalizeDate((string) ($data['updated_at'] ?? $data['modified'] ?? $createdAt));
$status = (string) ($data['status'] ?? 'approved');
if (!in_array($status, ['pending', 'approved', 'spam'], true)) {
$status = match ($status) {
'published', 'published-comment', 'visible', '1', 'true' => 'approved',
'trash', 'deleted', 'spam-comment' => 'spam',
default => 'pending',
};
}
$statement = $this->pdo->prepare(
'INSERT INTO comments
(slug, author, email, website, body, status, ip_hash, user_agent, created_at, updated_at)
VALUES
(:slug, :author, :email, :website, :body, :status, :ip_hash, :user_agent, :created_at, :updated_at)'
);
$statement->execute([
'slug' => $slug,
'author' => trim((string) ($data['author'] ?? $data['name'] ?? 'Imported')),
'email' => trim((string) ($data['email'] ?? '')),
'website' => trim((string) ($data['website'] ?? $data['site_url'] ?? $data['url'] ?? '')),
'body' => trim((string) ($data['body'] ?? $data['content'] ?? $data['comment'] ?? '')),
'status' => $status,
'ip_hash' => trim((string) ($data['ip_hash'] ?? '')),
'user_agent' => substr((string) ($data['user_agent'] ?? $data['agent'] ?? 'import'), 0, 300),
'created_at' => $createdAt,
'updated_at' => $updatedAt,
]);
return (int) $this->pdo->lastInsertId();
}
/** @return array<int, array<string, mixed>> */
public function list(?string $status = null, ?string $slug = null): array
{
@@ -144,4 +180,10 @@ final class CommentService
return hash_hmac('sha256', $ip, $salt);
}
private function normalizeDate(string $value): string
{
$timestamp = strtotime($value);
return date('c', $timestamp ?: time());
}
}
+44
View File
@@ -254,6 +254,50 @@ final class ContentRepository
return $this->find($slug, true) ?? [];
}
/** @param array<string, mixed> $metadata */
public function saveImported(string $slug, array $metadata, string $markdown, bool $rebuildIndex = true): array
{
$slug = self::slugify($slug !== '' ? $slug : (string) ($metadata['title'] ?? 'untitled'));
if ($slug === '') {
throw new RuntimeException('A title or slug is required.');
}
$directory = $this->pagesRoot . '/' . $slug;
if (!is_dir($directory)) {
mkdir($directory, 0775, true);
}
$date = (string) ($metadata['date'] ?? date('c'));
$metadata = array_replace([
'title' => self::titleFromSlug($slug),
'type' => 'post',
'status' => 'published',
'date' => $date,
'modified' => $metadata['modified'] ?? $date,
'category' => 'Notes',
'tags' => [],
'summary' => '',
'author' => 'Editor',
'cover' => '',
'allow_comments' => true,
'menu_order' => 0,
], $metadata);
$metadata['slug'] = $slug;
$metadata['tags'] = self::normalizeList($metadata['tags'] ?? []);
$metadata['allow_comments'] = self::toBool($metadata['allow_comments'] ?? true);
file_put_contents($directory . '/index.txt', $this->serialize($metadata, $markdown));
$timestamp = strtotime((string) ($metadata['modified'] ?? $metadata['date'])) ?: time();
touch($directory . '/index.txt', $timestamp);
if ($rebuildIndex) {
$this->rebuildIndex();
}
return $this->find($slug, true) ?? [];
}
public function delete(string $slug): bool
{
$slug = self::slugify($slug);