From 32ea4406c87049b25d3d82351097e69f13168311 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sun, 14 Jun 2009 21:51:54 -0700 Subject: Refactor the way that the rss module works so that we're not allowing the url to dictate arbitrary static method calls. * Each xxx_rss helper has a single feed() call which takes an id as the argument * xxx_rss::available_feedS() only returns feeds when they're applicable (ie if you're viewing a tag, it won't show you an item feed). * Feed urls are now in the module/feed_id form so that we can bind a feed id to a given module * Tightened up the Rss_Controller by using url::merge and some other tricks. * Made the slideshow module express its own feed. --- modules/comment/helpers/comment_rss.php | 84 +++++++++++++++------------ modules/gallery/helpers/gallery_rss.php | 75 ++++++++++++------------ modules/gallery/helpers/gallery_theme.php | 4 -- modules/rss/controllers/rss.php | 38 +++++++----- modules/rss/helpers/rss.php | 34 +++-------- modules/rss/helpers/rss_theme.php | 19 +++--- modules/rss/views/rss_block.html.php | 4 +- modules/slideshow/helpers/slideshow_theme.php | 12 +++- modules/tag/helpers/tag_rss.php | 35 ++++++----- 9 files changed, 161 insertions(+), 144 deletions(-) diff --git a/modules/comment/helpers/comment_rss.php b/modules/comment/helpers/comment_rss.php index c3eb7b2b..afcc275c 100644 --- a/modules/comment/helpers/comment_rss.php +++ b/modules/comment/helpers/comment_rss.php @@ -19,48 +19,60 @@ */ class comment_rss_Core { - static function available_feeds($item) { - return array(array("description" => t("All new comments"), - "uri" => "comments"), - array("description" => sprintf(t("Comments on %s"), $item->title), - "uri" => "comments/{$item->id}")); + static function available_feeds($item, $tag) { + $feeds["comment/newest"] = t("All new comments"); + if ($item) { + $feeds["comment/item/$item->id"] = + t("Comments on %title", array("title" => p::clean($item->title))); + } + return $feeds; } - static function comments($offset, $limit, $id) { - $comments = ORM::factory("comment") - ->where("state", "published") - ->orderby("created", "DESC"); - if ($id) { - $comments->where("item_id", $id); - } + static function feed($feed_id, $offset, $limit, $id) { + switch ($feed_id) { + case "newest": + $comments = ORM::factory("comment") + ->where("state", "published") + ->orderby("created", "DESC"); + $all_comments = ORM::factory("comment") + ->where("state", "published") + ->orderby("created", "DESC"); + break; - $feed->view = "comment.mrss"; - $comments = $comments->find_all($limit, $offset); - $feed->children = array(); - foreach ($comments as $comment) { - $item = $comment->item(); - $feed->children[] = new ArrayObject( - array("pub_date" => date("D, d M Y H:i:s T", $comment->created), - "text" => $comment->text, - "thumb_url" => $item->thumb_url(), - "thumb_height" => $item->thumb_height, - "thumb_width" => $item->thumb_width, - "item_uri" => url::abs_site("{$item->type}s/$item->id"), - "title" => $item->title, - "author" => $comment->author_name()), - ArrayObject::ARRAY_AS_PROPS); + case "item": + $comments = ORM::factory("comment") + ->where("state", "published") + ->orderby("created", "DESC") + ->where("item_id", $id); + $all_comments = ORM::factory("comment") + ->where("state", "published") + ->where("item_id", $id); } - $all_comments = ORM::factory("comment")->where("state", "published"); - if ($id) { - $all_comments->where("item_id", $id); - } + if (!empty($comments)) { + $feed->view = "comment.mrss"; + $comments = $comments->find_all($limit, $offset); + $feed->children = array(); + foreach ($comments as $comment) { + $item = $comment->item(); + $feed->children[] = new ArrayObject( + array("pub_date" => date("D, d M Y H:i:s T", $comment->created), + "text" => $comment->text, + "thumb_url" => $item->thumb_url(), + "thumb_height" => $item->thumb_height, + "thumb_width" => $item->thumb_width, + "item_uri" => url::abs_site("{$item->type}s/$item->id"), + "title" => $item->title, + "author" => $comment->author_name()), + ArrayObject::ARRAY_AS_PROPS); + } - $feed->max_pages = ceil($all_comments->find_all()->count() / $limit); - $feed->title = htmlspecialchars(t("Recent Comments")); - $feed->uri = url::abs_site("albums/" . (empty($id) ? "1" : $id)); - $feed->description = t("Recent Comments"); + $feed->max_pages = ceil($all_comments->find_all()->count() / $limit); + $feed->title = htmlspecialchars(t("Recent Comments")); + $feed->uri = url::abs_site("albums/" . (empty($id) ? "1" : $id)); + $feed->description = t("Recent Comments"); - return $feed; + return $feed; + } } } \ No newline at end of file diff --git a/modules/gallery/helpers/gallery_rss.php b/modules/gallery/helpers/gallery_rss.php index 413d12a4..6e966bdb 100644 --- a/modules/gallery/helpers/gallery_rss.php +++ b/modules/gallery/helpers/gallery_rss.php @@ -19,45 +19,44 @@ */ class gallery_rss_Core { - static function available_feeds($item) { - return array(array("description" => t("New photos or movies"), - "uri" => "updates"), - array("description" => t("Album feed"), - "uri" => "albums")); + static function available_feeds($item, $tag) { + $feeds["gallery/latest"] = t("Latest photos and movies"); + return $feeds; } - static function updates($offset, $limit) { - $feed->children = ORM::factory("item") - ->viewable() - ->where("type !=", "album") - ->orderby("created", "DESC") - ->find_all($limit, $offset); - - $all_children = ORM::factory("item") - ->viewable() - ->where("type !=", "album") - ->orderby("created", "DESC"); - - $feed->max_pages = ceil($all_children->find_all()->count() / $limit); - $feed->title = t("Recent Updates"); - $feed->link = url::abs_site("albums/1"); - $feed->description = t("Recent Updates"); - - return $feed; - } - - static function albums($offset, $limit, $id) { - $item = ORM::factory("item", $id); - access::required("view", $item); - - $feed->children = $item - ->viewable() - ->descendants($limit, $offset, "photo"); - $feed->max_pages = ceil($item->viewable()->descendants_count("photo") / $limit); - $feed->title = $item->title; - $feed->link = url::abs_site("albums/{$item->id}"); - $feed->description = $item->description; - - return $feed; + static function feed($feed_id, $offset, $limit, $id) { + switch ($feed_id) { + case "latest": + $feed->children = ORM::factory("item") + ->viewable() + ->where("type !=", "album") + ->orderby("created", "DESC") + ->find_all($limit, $offset); + + $all_children = ORM::factory("item") + ->viewable() + ->where("type !=", "album") + ->orderby("created", "DESC"); + + $feed->max_pages = ceil($all_children->find_all()->count() / $limit); + $feed->title = t("Recent Updates"); + $feed->link = url::abs_site("albums/1"); + $feed->description = t("Recent Updates"); + return $feed; + + case "album": + $item = ORM::factory("item", $id); + access::required("view", $item); + + $feed->children = $item + ->viewable() + ->descendants($limit, $offset, "photo"); + $feed->max_pages = ceil($item->viewable()->descendants_count("photo") / $limit); + $feed->title = $item->title; + $feed->link = url::abs_site("albums/{$item->id}"); + $feed->description = $item->description; + + return $feed; + } } } diff --git a/modules/gallery/helpers/gallery_theme.php b/modules/gallery/helpers/gallery_theme.php index 290434ed..f955e8f7 100644 --- a/modules/gallery/helpers/gallery_theme.php +++ b/modules/gallery/helpers/gallery_theme.php @@ -42,10 +42,6 @@ class gallery_theme_Core { $buf .= html::script("modules/gallery/js/fullsize.js"); } - if ($theme->item()) { - $buf .= rss::feed_link("albums/{$theme->item()->id}"); - } - if ($session->get("l10n_mode", false)) { $buf .= ""; diff --git a/modules/rss/controllers/rss.php b/modules/rss/controllers/rss.php index 29300d58..e9dd9fff 100644 --- a/modules/rss/controllers/rss.php +++ b/modules/rss/controllers/rss.php @@ -20,37 +20,43 @@ class Rss_Controller extends Controller { public static $page_size = 20; - public function feed($method, $id=null) { + public function feed($module_id, $feed_id, $id=null) { $page = $this->input->get("page", 1); - $feed_uri = "rss/feed/$method" . (empty($id) ? "" : "/$id"); if ($page < 1) { - url::redirect($feed_uri); + url::redirect(url::merge(array("page" => 1))); } - $feed = rss::feed_data($method, ($page - 1) * self::$page_size, self::$page_size, $id); - $max_pages = $feed->max_pages; - if ($max_pages && $page > $max_pages) { - url::redirect("$feed_uri?page={$max_pages}"); + // Run the appropriate feed callback + if (module::is_active($module_id)) { + $class_name = "{$module_id}_rss"; + if (method_exists($class_name, "feed")) { + $feed = call_user_func( + array($class_name, "feed"), $feed_id, + ($page - 1) * self::$page_size, self::$page_size, $id); + } + } + if (empty($feed)) { + Kohana::show_404(); + } + + if ($feed->max_pages && $page > $feed->max_pages) { + url::redirect(url::merge(array("page" => $feed->max_pages))); } $view = new View(empty($feed->view) ? "feed.mrss" : $feed->view); unset($feed->view); - $feed->uri = url::abs_site($feed_uri); $view->feed = $feed; + $view->pub_date = date("D, d M Y H:i:s T"); + $feed->uri = url::abs_site(Router::$current_uri); if ($page > 1) { - $previous_page = $page - 1; - $feed->previous_page_uri = url::site("$feed_uri?page={$previous_page}"); + $feed->previous_page_uri = url::abs_site(url::merge(array("page" => $page - 1))); } - - if ($page < $max_pages) { - $next_page = $page + 1; - $feed->next_page_uri = url::site("$feed_uri?page={$next_page}"); + if ($page < $feed->max_pages) { + $feed->next_page_uri = url::abs_site(url::merge(array("page" => $page + 1))); } - $view->pub_date = date("D, d M Y H:i:s T"); - rest::http_content_type(rest::RSS); print $view; } diff --git a/modules/rss/helpers/rss.php b/modules/rss/helpers/rss.php index 8a7d0c97..81ff175f 100644 --- a/modules/rss/helpers/rss.php +++ b/modules/rss/helpers/rss.php @@ -19,34 +19,18 @@ */ class rss_Core { - static function feed_link($uri) { - $url = url::site("rss/feed/$uri"); - return ""; - } - /** - * Get all available rss feeds + * Convert a rss feed id into a rss feed url. */ - static function available_feeds($item) { - $feeds = array(); - foreach (module::active() as $module) { - $class_name = "{$module->name}_rss"; - if (method_exists($class_name, "available_feeds")) { - foreach (call_user_func(array($class_name, "available_feeds"), $item) as $feed) { - $feeds[$feed["description"]] = url::site("rss/feed/{$feed['uri']}"); - } - } - } - - return $feeds; + static function url($uri) { + return url::site("rss/feed/$uri"); } - static function feed_data($method, $offset, $limit, $id) { - foreach (module::active() as $module) { - $class_name = "{$module->name}_rss"; - if (method_exists($class_name, $feed)) { - return call_user_func(array($class_name, $feed), $offset, $limit, $id); - } - } + /** + * Return a element for a given rss feed id. + */ + static function feed_link($uri) { + $url = url::site("rss/feed/$uri"); + return ""; } } \ No newline at end of file diff --git a/modules/rss/helpers/rss_theme.php b/modules/rss/helpers/rss_theme.php index 52d988bf..3d1b9a29 100644 --- a/modules/rss/helpers/rss_theme.php +++ b/modules/rss/helpers/rss_theme.php @@ -19,17 +19,22 @@ */ class rss_theme_Core { static function sidebar_blocks($theme) { - // @todo this needs to be data driven - if (!$theme->item()) { - return; - } - $block = new Block(); $block->css_id = "gRss"; $block->title = t("Available RSS Feeds"); $block->content = new View("rss_block.html"); - $block->content->feeds = rss::available_feeds($theme->item()); + $block->content->feeds = array(); + foreach (module::active() as $module) { + $class_name = "{$module->name}_rss"; + if (method_exists($class_name, "available_feeds")) { + $block->content->feeds = array_merge( + $block->content->feeds, + call_user_func(array($class_name, "available_feeds"), $theme->item(), $theme->tag())); + } + } - return $block; + if ($block->content->feeds) { + return $block; + } } } diff --git a/modules/rss/views/rss_block.html.php b/modules/rss/views/rss_block.html.php index f964329c..39921d7d 100644 --- a/modules/rss/views/rss_block.html.php +++ b/modules/rss/views/rss_block.html.php @@ -1,9 +1,9 @@