From 138708182b41972f882c73568a974939ed865d2e Mon Sep 17 00:00:00 2001 From: augushong Date: Thu, 2 Mar 2023 09:43:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=9F=A5=E8=AF=A2=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=EF=BC=9B=E5=A2=9E=E5=8A=A0RSS1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/tools/Rss.php | 48 ++++++++++++++++++++++++++++++++++++ app/common/tools/Site.php | 45 +++++++++++++++++++++++++++++++++ app/common/tools/Sitemap.php | 6 ++--- app/model/Post.php | 14 +++++++++-- composer.json | 3 ++- route/app.php | 29 +++++++++++++++++++++- 6 files changed, 138 insertions(+), 7 deletions(-) create mode 100644 app/common/tools/Rss.php create mode 100644 app/common/tools/Site.php diff --git a/app/common/tools/Rss.php b/app/common/tools/Rss.php new file mode 100644 index 0000000..88ab31c --- /dev/null +++ b/app/common/tools/Rss.php @@ -0,0 +1,48 @@ +setTitle(Site::name()); + $feed->setLink(Site::indexUrl()); + $feed->setDescription(Site::desc()); + + //It's important for RSS 1.0 + $feed->setChannelAbout(Site::keywords()); + + // An image is optional. + $feed->setImage(Site::logo(), 'PHP武器库头像', get_source_link('/')); + + //Adding a feed. Generally this portion will be in a loop and add all feeds. + + $list_post = Site::mapRecentlyPost(); + + foreach ($list_post as $model_post) { + //Create an empty FeedItem + + $new_item = $feed->createNewItem(); + + $new_item->setTitle($model_post->title); + + $new_item->setLink($model_post->read_url); + //The parameter is a timestamp for setDate() function + $new_item->setDate($model_post->publish_time); + $new_item->setDescription($model_post->desc); + //Use core addElement() function for other supported optional elements + + //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 new file mode 100644 index 0000000..31f12dd --- /dev/null +++ b/app/common/tools/Site.php @@ -0,0 +1,45 @@ +where('status', 1)->select(); + + return $list_post; + } + public static function mapRecentlyPost() + { + $list_post = \app\model\Post::cache(60)->where('status', 1)->order('publish_time', "desc")->limit(25)->select(); + + return $list_post; + } + + public static function name() + { + return get_system_config('site_name'); + } + + public static function indexUrl() + { + return Request::scheme() . '://' . get_system_config('main_domain', Request::host()); + } + + public static function desc() + { + return get_system_config('site_desc'); + } + + public static function logo() + { + return get_source_link(get_system_config('site_logo')); + } + + public static function keywords() + { + return get_system_config('site_keywords'); + } +} \ No newline at end of file diff --git a/app/common/tools/Sitemap.php b/app/common/tools/Sitemap.php index 2b875d7..29739d8 100644 --- a/app/common/tools/Sitemap.php +++ b/app/common/tools/Sitemap.php @@ -2,7 +2,7 @@ namespace app\common\tools; -use app\model\Post; + use DateTime; use Thepixeldeveloper\Sitemap\Drivers\XmlWriterDriver; use Thepixeldeveloper\Sitemap\Url; @@ -18,7 +18,7 @@ class Sitemap $urlset_page_post = new Urlset(); - $index_url = new Url(Request::scheme() . '://' . get_system_config('main_domain', Request::host())); + $index_url = new Url(Site::indexUrl()); $index_url->setChangeFreq('always'); @@ -26,7 +26,7 @@ class Sitemap $urlset_page_post->add($index_url); - $list_post = Post::cache(60)->where('status', 1)->select(); + $list_post = Site::mapAllPost(); foreach ($list_post as $model_post) { $url_post = new Url($model_post->read_url); diff --git a/app/model/Post.php b/app/model/Post.php index 8a9364d..76a72ea 100644 --- a/app/model/Post.php +++ b/app/model/Post.php @@ -98,6 +98,15 @@ class Post extends Base return $list; } + public function getDescAttr($value) + { + if(empty($value)){ + + } + + return $value; + } + public function getDescShortAttr() { $desc = $this->getData('desc'); @@ -181,8 +190,9 @@ class Post extends Base public function getReadUrlAttr() { + $path = '/index/a' . $this->getData('uid') . '.html'; - return Request::domain() . '/index/a' . $this->getData('uid') . '.html'; + return Request::domain() . $path; } public function getShareTextAttr() @@ -195,4 +205,4 @@ class Post extends Base return $share_text; } -} +} \ No newline at end of file diff --git a/composer.json b/composer.json index 8634e68..58f6bb4 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,8 @@ "thepixeldeveloper/sitemap": "^5.1", "jaeger/phpquery-single": "^1.1", "league/html-to-markdown": "^5.1", - "topthink/think-filesystem": "^2.0" + "topthink/think-filesystem": "^2.0", + "mibe/feedwriter": "^1.1" }, "require-dev": { "symfony/var-dumper": "^4.2" diff --git a/route/app.php b/route/app.php index d828cbc..e689a0d 100644 --- a/route/app.php +++ b/route/app.php @@ -9,6 +9,7 @@ // | Author: liu21st // +---------------------------------------------------------------------- +use app\common\tools\Rss; use app\common\tools\Sitemap; use app\Request; use think\facade\Cache; @@ -27,7 +28,6 @@ Route::rule('/sitemap.xml', function (Request $request) { if (!empty($if_not_match)) { if ($if_not_match == $last_etag) { - Log::debug('sitemap go for etag cache'); return xml('', 304)->eTag($last_etag); } } @@ -41,3 +41,30 @@ Route::rule('/sitemap.xml', function (Request $request) { return xml($content)->eTag($last_etag); }); + + +Route::rule('/rss1.xml', function (Request $request) { + + $cache_key = 'rss1_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::initRss1(); + + $last_etag = md5($content); + + Cache::set($cache_key, $last_etag); + + return xml($content)->eTag($last_etag); +}); \ No newline at end of file