From c9cc5a4a35b5040ab1235e6ca2c331f6319b2661 Mon Sep 17 00:00:00 2001 From: augushong Date: Thu, 2 Mar 2023 10:14:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0ATOM=E8=AE=A2=E9=98=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/tools/Rss.php | 59 +++++++++++++++++++++++++++++++++++++-- app/common/tools/Site.php | 21 ++++++++++++++ route/app.php | 26 +++++++++++++++++ 3 files changed, 104 insertions(+), 2 deletions(-) diff --git a/app/common/tools/Rss.php b/app/common/tools/Rss.php index c9fa08f..45d1e72 100644 --- a/app/common/tools/Rss.php +++ b/app/common/tools/Rss.php @@ -1,6 +1,7 @@ setChannelElement('pubDate', $list_post[0]->publish_time); - + $feed->setSelfLink(Site::rss2Url()); + $feed->setAtomLink('http://pubsubhubbub.appspot.com', 'hub'); // If you want you can also add a line to publicly announce that you used // this fine piece of software to generate the feed. ;-) @@ -106,7 +108,7 @@ class Rss $new_item->setDate($model_post->publish_time); // If you want you can set the name (and email address) of the author of this feed item. - $new_item->setAuthor($model_post->author_name, $model_post->stie_contact_email); + $new_item->setAuthor($model_post->author_name, get_system_config('stie_contact_email')); // You can set a globally unique identifier. This can be a URL or any other string. // If you set permaLink to true, the identifier must be an URL. The default of the @@ -124,4 +126,57 @@ class Rss return $content; } + public static function initAtom() + { + $feed = new ATOM(); + + //Setting the channel elements +//Use wrapper functions for common elements + $feed->setTitle(Site::name()); + $feed->setDescription(Site::desc()); + + $feed->setLink(Site::indexUrl()); + + $list_post = Site::mapRecentlyPost(); + + $feed->setDate($list_post[0]->publish_time); + $feed->setImage(Site::logo()); + + //For other channel elements, use setChannelElement() function + $feed->setChannelElement('author', array('name' => Site::author())); + + + //You can add additional link elements, e.g. to a PubSubHubbub server with custom relations. + $feed->setSelfLink(Site::atomUrl()); + $feed->setAtomLink('http://pubsubhubbub.appspot.com', 'hub'); + + //Adding a feed. Generally this portion will be in a loop and add all feeds. + + foreach ($list_post as $model_post) { + + $new_item = $feed->createNewItem(); + + //Add elements to the feed item + //Use wrapper functions to add common feed elements + $new_item->setTitle($model_post->title); + + $new_item->setLink($model_post->read_url); + + $new_item->setDate($model_post->publish_time); + + $new_item->setAuthor($model_post->author_name, get_system_config('stie_contact_email')); + + + //Internally changed to "summary" tag for ATOM feed + $new_item->setDescription($model_post->desc); + // $new_item->setContent($model_post->content_html); + + //Now add the feed item + $feed->addItem($new_item); + + } + + $content = $feed->generateFeed(); + return $content; + } } \ No newline at end of file diff --git a/app/common/tools/Site.php b/app/common/tools/Site.php index 31f12dd..d0dbb9d 100644 --- a/app/common/tools/Site.php +++ b/app/common/tools/Site.php @@ -22,6 +22,10 @@ class Site { return get_system_config('site_name'); } + public static function author() + { + return get_system_config('default_author'); + } public static function indexUrl() { @@ -42,4 +46,21 @@ class Site { return get_system_config('site_keywords'); } + + public static function sitemapUrl() + { + return Request::scheme() . '://' . get_system_config('main_domain', Request::host()) . '/sitemap.xml'; + } + public static function rss1Url() + { + return Request::scheme() . '://' . get_system_config('main_domain', Request::host()) . '/rss1.xml'; + } + public static function rss2Url() + { + return Request::scheme() . '://' . get_system_config('main_domain', Request::host()) . '/rss2.xml'; + } + public static function atomUrl() + { + return Request::scheme() . '://' . get_system_config('main_domain', Request::host()) . '/atom.xml'; + } } \ No newline at end of file diff --git a/route/app.php b/route/app.php index 0e8a858..b353421 100644 --- a/route/app.php +++ b/route/app.php @@ -92,5 +92,31 @@ Route::rule('/rss2.xml', function (Request $request) { Cache::set($cache_key, $last_etag); + return xml($content)->eTag($last_etag); +}); + +Route::rule('/atom.xml', function (Request $request) { + + $cache_key = 'atom_cache_key'; + + $last_etag = Cache::get($cache_key); + + if (!empty($cache_key)) { + + $if_not_match = $request->header('If-None-Match'); + + if (!empty($if_not_match)) { + if ($if_not_match == $last_etag) { + return xml('', 304)->eTag($last_etag); + } + } + } + + $content = Rss::initAtom(); + + $last_etag = md5($content); + + Cache::set($cache_key, $last_etag); + return xml($content)->eTag($last_etag); }); \ No newline at end of file