增加ATOM订阅

This commit is contained in:
2023-03-02 10:14:12 +08:00
parent cc9627f932
commit c9cc5a4a35
3 changed files with 104 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
<?php <?php
namespace app\common\tools; namespace app\common\tools;
use FeedWriter\ATOM;
use FeedWriter\RSS1; use FeedWriter\RSS1;
use FeedWriter\RSS2; use FeedWriter\RSS2;
@@ -78,7 +79,8 @@ class Rss
$feed->setChannelElement('pubDate', $list_post[0]->publish_time); $feed->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 // If you want you can also add a line to publicly announce that you used
// this fine piece of software to generate the feed. ;-) // this fine piece of software to generate the feed. ;-)
@@ -106,7 +108,7 @@ class Rss
$new_item->setDate($model_post->publish_time); $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. // 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. // 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 // If you set permaLink to true, the identifier must be an URL. The default of the
@@ -124,4 +126,57 @@ class Rss
return $content; 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;
}
} }

View File

@@ -22,6 +22,10 @@ class Site
{ {
return get_system_config('site_name'); return get_system_config('site_name');
} }
public static function author()
{
return get_system_config('default_author');
}
public static function indexUrl() public static function indexUrl()
{ {
@@ -42,4 +46,21 @@ class Site
{ {
return get_system_config('site_keywords'); 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';
}
} }

View File

@@ -94,3 +94,29 @@ Route::rule('/rss2.xml', function (Request $request) {
return xml($content)->eTag($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);
});