From 073fca5f0dbbde108bb584a13c8c0aedbfc03dc0 Mon Sep 17 00:00:00 2001 From: ksdhans Date: Mon, 13 Jun 2016 23:20:07 +1200 Subject: [PATCH 1/5] Add RSS feeds for categories and tags This patch adds RSS feeds for tags and categories. The RSS feeds are at the following URLs: - */category//rss - */tag//rss --- code/model/Blog.php | 49 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/code/model/Blog.php b/code/model/Blog.php index b095d66..ced2017 100644 --- a/code/model/Blog.php +++ b/code/model/Blog.php @@ -644,8 +644,8 @@ class Blog_Controller extends Page_Controller * @var array */ private static $url_handlers = array( - 'tag/$Tag!' => 'tag', - 'category/$Category!' => 'category', + 'tag/$Tag!/$Rss' => 'tag', + 'category/$Category!/$Rss' => 'category', 'archive/$Year!/$Month/$Day' => 'archive', 'profile/$URLSegment!' => 'profile', ); @@ -834,7 +834,12 @@ class Blog_Controller extends Page_Controller if ($tag) { $this->blogPosts = $tag->BlogPosts(); - return $this->render(); + + if($this->isRSS()) { + return $this->rssFeed($this->blogPosts); + } else { + return $this->render(); + } } $this->httpError(404, 'Not Found'); @@ -874,7 +879,11 @@ class Blog_Controller extends Page_Controller if ($category) { $this->blogPosts = $category->BlogPosts(); - return $this->render(); + if($this->isRSS()) { + return $this->rssFeed($this->blogPosts); + } else { + return $this->render(); + } } $this->httpError(404, 'Not Found'); @@ -1049,11 +1058,7 @@ class Blog_Controller extends Page_Controller $this->blogPosts = $dataRecord->getBlogPosts(); - $rss = new RSSFeed($this->blogPosts, $this->Link(), $this->MetaTitle, $this->MetaDescription); - - $this->extend('updateRss', $rss); - - return $rss->outputToBrowser(); + return $this->rssFeed($this->blogPosts); } /** @@ -1093,4 +1098,30 @@ class Blog_Controller extends Page_Controller { return $this->Link('rss'); } + + /** + * Displays an RSS feed of the given blog posts. + * + * @return string + */ + public function rssFeed($blogPosts) + { + $rss = new RSSFeed($blogPosts, $this->Link() . 'category/general/', $this->MetaTitle, $this->MetaDescription); + + $this->extend('updateRss', $rss); + + return $rss->outputToBrowser(); + } + + /** + * Returns true if the $Rss sub-action for categories/tags has been set to "rss" + */ + private function isRSS() { + $rss = $this->request->param('Rss'); + if($rss && strcasecmp($rss, "rss") == 0) { + return true; + } else { + return false; + } + } } From 95e14e54961e329c833a22f66f6dc8a721eeea01 Mon Sep 17 00:00:00 2001 From: ksdhans Date: Tue, 14 Jun 2016 17:38:43 +1200 Subject: [PATCH 2/5] Performed suggested changes - Removed hardcoded URL segment (was test code that I forgot to remove) - Changed if($rss to if(is_string($rss) - Moved bracket after isRSS() to next line - Made rssFeed() protected as it's an internal function and shouldn't be part of the public API --- code/model/Blog.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/code/model/Blog.php b/code/model/Blog.php index ced2017..a32f6d2 100644 --- a/code/model/Blog.php +++ b/code/model/Blog.php @@ -1104,9 +1104,9 @@ class Blog_Controller extends Page_Controller * * @return string */ - public function rssFeed($blogPosts) + protected function rssFeed($blogPosts) { - $rss = new RSSFeed($blogPosts, $this->Link() . 'category/general/', $this->MetaTitle, $this->MetaDescription); + $rss = new RSSFeed($blogPosts, $this->Link(), $this->MetaTitle, $this->MetaDescription); $this->extend('updateRss', $rss); @@ -1116,9 +1116,10 @@ class Blog_Controller extends Page_Controller /** * Returns true if the $Rss sub-action for categories/tags has been set to "rss" */ - private function isRSS() { + private function isRSS() + { $rss = $this->request->param('Rss'); - if($rss && strcasecmp($rss, "rss") == 0) { + if(is_string($rss) && strcasecmp($rss, "rss") == 0) { return true; } else { return false; From d42b3ca57c3137966fe29b8abd79b8bfa65a3d1b Mon Sep 17 00:00:00 2001 From: ksdhans Date: Tue, 14 Jun 2016 18:08:31 +1200 Subject: [PATCH 3/5] Fixed RSS feed link for category/tag feeds - The RSS feed link was always the blog's root URL, even for category/tag feeds. FIXED - Changed the return type documented for category() and tag() to include string for the case where an RSS feed is returned (addresses the "Scrutinizer" issues) --- code/model/Blog.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/code/model/Blog.php b/code/model/Blog.php index a32f6d2..c5d3b3b 100644 --- a/code/model/Blog.php +++ b/code/model/Blog.php @@ -826,7 +826,7 @@ class Blog_Controller extends Page_Controller /** * Renders the blog posts for a given tag. * - * @return null|SS_HTTPResponse + * @return null|SS_HTTPResponse|string */ public function tag() { @@ -836,7 +836,7 @@ class Blog_Controller extends Page_Controller $this->blogPosts = $tag->BlogPosts(); if($this->isRSS()) { - return $this->rssFeed($this->blogPosts); + return $this->rssFeed($this->blogPosts, $tag->getLink()); } else { return $this->render(); } @@ -870,7 +870,7 @@ class Blog_Controller extends Page_Controller /** * Renders the blog posts for a given category. * - * @return null|SS_HTTPResponse + * @return null|SS_HTTPResponse|string */ public function category() { @@ -880,7 +880,7 @@ class Blog_Controller extends Page_Controller $this->blogPosts = $category->BlogPosts(); if($this->isRSS()) { - return $this->rssFeed($this->blogPosts); + return $this->rssFeed($this->blogPosts, $category->getLink()); } else { return $this->render(); } @@ -1058,7 +1058,7 @@ class Blog_Controller extends Page_Controller $this->blogPosts = $dataRecord->getBlogPosts(); - return $this->rssFeed($this->blogPosts); + return $this->rssFeed($this->blogPosts, $this->Link()); } /** @@ -1102,11 +1102,14 @@ class Blog_Controller extends Page_Controller /** * Displays an RSS feed of the given blog posts. * + * @param DataList $blogPosts + * @param string $link + * * @return string */ - protected function rssFeed($blogPosts) + protected function rssFeed($blogPosts, $link) { - $rss = new RSSFeed($blogPosts, $this->Link(), $this->MetaTitle, $this->MetaDescription); + $rss = new RSSFeed($blogPosts, $link, $this->MetaTitle, $this->MetaDescription); $this->extend('updateRss', $rss); From ca7f09d0e02b1b09109eaea7603a66b83d7e4d64 Mon Sep 17 00:00:00 2001 From: ksdhans Date: Tue, 14 Jun 2016 18:20:03 +1200 Subject: [PATCH 4/5] Removed category()/tag() documentation changes Adding "string" to the possible return types of category()/tag() didn't stop the scrutinizer from raising two new issues, so I've reverted that change. In future tags/categories may be split into separate controllers (it's cleaner that way), so these "issues" may serve as a reminder. NOTE: This means that the documented return type for rss() is also wrong --- code/model/Blog.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/model/Blog.php b/code/model/Blog.php index c5d3b3b..a0bb3c6 100644 --- a/code/model/Blog.php +++ b/code/model/Blog.php @@ -826,7 +826,7 @@ class Blog_Controller extends Page_Controller /** * Renders the blog posts for a given tag. * - * @return null|SS_HTTPResponse|string + * @return null|SS_HTTPResponse */ public function tag() { @@ -870,7 +870,7 @@ class Blog_Controller extends Page_Controller /** * Renders the blog posts for a given category. * - * @return null|SS_HTTPResponse|string + * @return null|SS_HTTPResponse */ public function category() { From 29ffaa82956718502e93619bddbda7c263174625 Mon Sep 17 00:00:00 2001 From: ksdhans Date: Wed, 15 Jun 2016 19:33:29 +1200 Subject: [PATCH 5/5] Minor layout tweak Correcting indentation of isRSS(). --- code/model/Blog.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/code/model/Blog.php b/code/model/Blog.php index a0bb3c6..0ce1b08 100644 --- a/code/model/Blog.php +++ b/code/model/Blog.php @@ -1119,13 +1119,13 @@ class Blog_Controller extends Page_Controller /** * Returns true if the $Rss sub-action for categories/tags has been set to "rss" */ - private function isRSS() - { - $rss = $this->request->param('Rss'); - if(is_string($rss) && strcasecmp($rss, "rss") == 0) { - return true; - } else { - return false; - } - } + private function isRSS() + { + $rss = $this->request->param('Rss'); + if(is_string($rss) && strcasecmp($rss, "rss") == 0) { + return true; + } else { + return false; + } + } }