From fd3a81f1ee9b1621f5be62ac4e7f595f7aa83fc0 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Fri, 12 Jun 2009 23:52:03 +0800 Subject: Move the processing of rss feeds from the rss controller to callbacks in the modules that are supplying the feed. The rss controller becomes a router. In this change the comment and updates fields are distributed. Signed-off-by: --- modules/comment/helpers/comment_rss.php | 35 ++++++++++++ modules/comment/views/comment.mrss.php | 43 +++++++++++++++ modules/gallery/helpers/gallery_rss.php | 15 ++++++ modules/rss/controllers/rss.php | 95 ++++++--------------------------- modules/rss/helpers/rss.php | 8 +++ modules/rss/views/comment.mrss.php | 43 --------------- 6 files changed, 117 insertions(+), 122 deletions(-) create mode 100644 modules/comment/views/comment.mrss.php delete mode 100644 modules/rss/views/comment.mrss.php diff --git a/modules/comment/helpers/comment_rss.php b/modules/comment/helpers/comment_rss.php index 9ae28726..b191c326 100644 --- a/modules/comment/helpers/comment_rss.php +++ b/modules/comment/helpers/comment_rss.php @@ -27,4 +27,39 @@ class comment_rss_Core { "sidebar" => true, "uri" => "comments/{$item->id}")); } + + static function comments($offset, $limit, $id) { + $feed = new stdClass(); + $orm = ORM::factory("comment") + ->where("state", "published") + ->orderby("created", "DESC"); + if (!empty($id)) { + $orm->where("item_id", $id); + } + + $feed->view = "comment.mrss"; + $comments = $orm->find_all($limit, $offset); + $feed->data["children"] = array(); + foreach ($comments as $comment) { + $item = $comment->item(); + $feed->data["children"][] = array( + "pub_date" => date("D, d M Y H:i:s T", $comment->created), + "text" => htmlspecialchars($comment->text), + "thumb_url" => $item->thumb_url(), + "thumb_height" => $item->thumb_height, + "thumb_width" => $item->thumb_width, + "item_link" => htmlspecialchars(url::abs_site("{$item->type}s/$item->id")), + "title" =>htmlspecialchars($item->title), + "author" => + empty($comment->guest_name) ? $comment->author()->full_name : $comment->guest_name + ); + } + + $feed->max_pages = ceil($comments->count() / $limit); + $feed->data["title"] = htmlspecialchars(t("Recent Comments")); + $feed->data["link"] = url::abs_site("albums/" . (empty($id) ? "1" : $id)); + $feed->data["description"] = t("Recent Comments"); + + return $feed; + } } \ No newline at end of file diff --git a/modules/comment/views/comment.mrss.php b/modules/comment/views/comment.mrss.php new file mode 100644 index 00000000..d2177026 --- /dev/null +++ b/modules/comment/views/comment.mrss.php @@ -0,0 +1,43 @@ + +" ?> + + + gallery3 + <?= p::clean($title) ?> + + + en-us + + + + + + + + diff --git a/modules/gallery/helpers/gallery_rss.php b/modules/gallery/helpers/gallery_rss.php index 0b87b1b1..98798346 100644 --- a/modules/gallery/helpers/gallery_rss.php +++ b/modules/gallery/helpers/gallery_rss.php @@ -24,4 +24,19 @@ class gallery_rss_Core { "sidebar" => true, "uri" => "updates")); } + + static function updates($offset, $limit) { + $feed = new stdClass(); + $feed->data["children"] = ORM::factory("item") + ->viewable() + ->where("type !=", "album") + ->orderby("created", "DESC") + ->find_all($limit, $offset); + $feed->max_pages = ceil($feed->data["children"]->count() / $limit); + $feed->data["title"] = t("Recent Updates"); + $feed->data["link"] = url::abs_site("albums/1"); + $feed->data["description"] = t("Recent Updates"); + + return $feed; + } } diff --git a/modules/rss/controllers/rss.php b/modules/rss/controllers/rss.php index 1f8b8a4e..7e5b6193 100644 --- a/modules/rss/controllers/rss.php +++ b/modules/rss/controllers/rss.php @@ -62,47 +62,6 @@ class Rss_Controller extends Controller { print $view; } - public function updates() { - $page = $this->input->get("page", 1); - if ($page < 1) { - url::redirect("rss/updates"); - } - - $items = ORM::factory("item") - ->viewable() - ->where("type !=", "album") - ->orderby("created", "DESC") - ->find_all(self::$page_size, ($page - 1) * self::$page_size); - $max_pages = ceil($items->count() / self::$page_size); - - if ($max_pages && $page > $max_pages) { - url::redirect("rss/updates?page=$max_pages"); - } - - $view = new View("feed.mrss"); - $view->title = t("Recent Updates"); - $view->link = url::abs_site("albums/1"); - $view->description = t("Recent Updates"); - $view->feed_link = url::abs_site("rss/updates"); - $view->children = $items; - - if ($page > 1) { - $previous_page = $page - 1; - $view->previous_page_link = url::site("rss/updates?page={$previous_page}"); - } - - if ($page < $max_pages) { - $next_page = $page + 1; - $view->next_page_link = url::site("rss/updates?page={$next_page}"); - } - - // @todo do we want to add an upload date to the items table? - $view->pub_date = date("D, d M Y H:i:s T"); - - rest::http_content_type(rest::RSS); - print $view; - } - public function tags($id) { $tag = ORM::factory("tag", $id); if (!$tag->loaded) { @@ -145,59 +104,37 @@ class Rss_Controller extends Controller { print $view; } - public function comments($id=null) { + public function __call($method, $arguments) { + $id = empty($arguments) ? null : $arguments[0]; $page = $this->input->get("page", 1); + $feed_uri = "rss/$method" . (empty($id) ? "" : "/$id"); if ($page < 1) { - url::redirect("rss/comments/$id"); + url::redirect($feed_uri); } - $orm = ORM::factory("comment") - ->where("state", "published") - ->orderby("created", "DESC"); - if (!empty($id)) { - $orm->where("item_id", $id); + $feed = rss::process_feed($method, ($page - 1) * self::$page_size, self::$page_size, $id); + if ($feed->max_pages && $page > $feed->max_pages) { + url::redirect("$feed_uri?page={$feed->max_pages}"); } - $comments = $orm->find_all(self::$page_size, ($page - 1) * self::$page_size); - $max_pages = ceil($orm->count_last_query() / self::$page_size); - - if ($max_pages && $page > $max_pages) { - url::redirect("rss/comments/{$item->id}?page=$max_pages"); - } - - $view = new View("comment.mrss"); - $view->title = htmlspecialchars(t("Recent Comments")); - $view->link = url::abs_site("albums/1"); - $view->description = t("Recent Comments"); - $view->feed_link = url::abs_site("rss/comments"); - $view->pub_date = date("D, d M Y H:i:s T"); - - $view->children = array(); - foreach ($comments as $comment) { - $item = $comment->item(); - $view->children[] = array( - "pub_date" => date("D, d M Y H:i:s T", $comment->created), - "text" => htmlspecialchars($comment->text), - "thumb_url" => $item->thumb_url(), - "thumb_height" => $item->thumb_height, - "thumb_width" => $item->thumb_width, - "item_link" => htmlspecialchars(url::abs_site("{$item->type}s/$item->id")), - "title" =>htmlspecialchars($item->title), - "author" => - empty($comment->guest_name) ? $comment->author()->full_name : $comment->guest_name - ); + $view = new View(empty($feed->view) ? "feed.mrss" : $feed->view); + foreach ($feed->data as $field => $value) { + $view->$field = $value; } + $view->feed_link = url::abs_site($feed_uri); if ($page > 1) { $previous_page = $page - 1; - $view->previous_page_link = url::site("rss/comments/{$item->id}?page={$previous_page}"); + $view->previous_page_link = url::site("$feed_uri?page={$previous_page}"); } - if ($page < $max_pages) { + if ($page < $feed->max_pages) { $next_page = $page + 1; - $view->next_page_link = url::site("rss/comments/{$item->id}?page={$next_page}"); + $view->next_page_link = url::site("$feed_uri?page={$next_page}"); } + $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 b0e7b30f..1d30425f 100644 --- a/modules/rss/helpers/rss.php +++ b/modules/rss/helpers/rss.php @@ -48,4 +48,12 @@ class rss_Core { return $feeds; } + static function process_feed($feed, $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); + } + } + } } \ No newline at end of file diff --git a/modules/rss/views/comment.mrss.php b/modules/rss/views/comment.mrss.php deleted file mode 100644 index d2177026..00000000 --- a/modules/rss/views/comment.mrss.php +++ /dev/null @@ -1,43 +0,0 @@ - -" ?> - - - gallery3 - <?= p::clean($title) ?> - - - en-us - - - - - - - - -- cgit v1.2.3