Files
LittleBeast/src/Site/Controller/Atom.php

95 lines
3.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace Site\Controller;
use Site\Controller\BlogPost;
use Std\Lib\Markdown;
class Atom extends BlogPost {
private string $domain = 'technicalsuwako.moe';
/**
* 最新の5記事のAtomフィードを生成する
*
* @param array $params パラメータ配列
* @return void
*/
public function feed(array $params): void {
try {
// 最新の投稿を取得
$posts = $this->getPosts('/blog/', null);
// 最新の5件に制限
$posts = array_slice($posts, 0, 5);
// サイトのドメインを取得
$domain = $_SERVER['HTTP_HOST'];
$baseUrl = 'https://'.$domain;
// 現在の日時RFC3339形式
$published = date('c');
// XMLヘッダーとコンテンツタイプを設定
header('Content-Type: application/atom+xml; charset=utf-8');
// Atomフィードの開始部分
echo '<?xml version="1.0" encoding="utf-8"?>'."\n";
echo '<feed xmlns="http://www.w3.org/2005/Atom">'."\n";
// フィードの基本情報
echo ' <title>'.SITEINFO['title'].'</title>'."\n";
echo ' <link href="'.$baseUrl.'" />'."\n";
echo ' <link href="'.$baseUrl.'/blog.atom" rel="self" />'."\n";
echo ' <id>'.$baseUrl.'/</id>'."\n";
echo ' <published>'.$published.'</published>'."\n";
echo ' <updated>'.$published.'</updated>'."\n";
echo ' <author>'."\n";
echo ' <name>'.SITEINFO['title'].'</name>'."\n";
echo ' </author>'."\n";
// 各エントリー(記事)
foreach ($posts as $post) {
// 記事の本文を取得(プレーンテキスト)
$path = ROOT.'/blog/'.$post['slug'].'.md';
$content = '';
$postPublished = date('c', strtotime($post['date']));
if (file_exists($path)) {
$fileContent = file_get_contents($path);
$parts = explode('----', $fileContent, 2);
if (count($parts) > 1) {
// 本文をHTMLとして準備
$md = new Markdown($post['slug'], '/blog/');
$content = $md->parse();
// HTMLタグを取り除かないようにCDATAで囲む
$content = '<![CDATA['.$content.']]>';
}
}
echo ' <entry>'."\n";
echo ' <title>'.htmlspecialchars($post['title']).'</title>'."\n";
echo ' <link href="'.$baseUrl.'/blog/'.$post['slug'].'" />'."\n";
echo ' <id>'.$baseUrl.'/blog/'.$post['slug'].'</id>'."\n";
echo ' <published>'.$postPublished.'</published>'."\n";
// カテゴリ(タグ)
if (isset($post['category']) && is_array($post['category'])) {
foreach ($post['category'] as $category) {
echo ' <category term="'.htmlspecialchars($category).'" />'."\n";
}
}
// 本文(要約または全文)
echo ' <content type="html">'.$content.'</content>'."\n";
echo ' </entry>'."\n";
}
// フィードの終了
echo '</feed>';
exit;
} catch (\Exception $e) {
header('Content-Type: text/plain; charset=utf-8');
echo 'フィードの作成に失敗: '.$e->getMessage();
exit;
}
}
}