Permalink
Browse files

Adding a RSS feed.

  • Loading branch information...
1 parent 5c2207d commit db25a6862b71c6b3c898f536df97470a7cd643ea @daveWid committed Dec 12, 2011
Showing with 63 additions and 6 deletions.
  1. +15 −1 classes/soapbox/controller.php
  2. +45 −5 classes/soapbox/model/post.php
  3. +3 −0 config/soapbox.php
@@ -99,10 +99,24 @@ public function action_search()
/**
* The RSS Feed
+ *
+ * @see http://feed2.w3.org/docs/rss2.html
*/
public function action_feed()
{
- var_dump("RSS Feed"); die;
+ $this->auto_render = false; // No need for the template
+
+ $this->response->headers('Content-Type', 'text/xml')
+ ->body(Feed::create(array(
+ 'title' => $this->_config['title'],
+ 'link' => Route::get('soapbox')->uri(),
+ 'description' => $this->_config['description'],
+ 'generator' => "Soapbox"
+ ),
+ Model_Post::feed(),
+ 'rss2',
+ Kohana::$charset
+ ));
}
/**
@@ -156,6 +156,43 @@ public static function search($query)
}
/**
+ * Grabs the latests posts in a format that can be used in a RSS feed.
+ *
+ * @see http://feed2.w3.org/docs/rss2.html
+ *
+ * @param int $num The number of items to grab
+ * @return array The feed items
+ */
+ public static function feed($num = 5)
+ {
+ $posts = DB::select()
+ ->from(static::$table)
+ ->order_by('posted_date', 'DESC')
+ ->order_by(static::$primary, 'DESC')
+ ->as_object()
+ ->limit($num)
+ ->execute();
+
+ $feed = array();
+ foreach($posts as $row)
+ {
+ list($content) = static::truncate($row->content);
+
+ $item = array(
+ 'title' => $row->title,
+ 'link' => static::permalink($row, 'http'),
+ 'description' => htmlentities($content),
+ 'pubDate' => Date::formatted_time($row->posted_date, 'r'),
+ );
+
+ $item['guid'] = $item['link'];
+ $feed[] = $item;
+ }
+
+ return $feed;
+ }
+
+ /**
* Truncates the content using a <!-- more --> tag in the content.
*
* @param string $content The content to truncate
@@ -166,7 +203,7 @@ public static function truncate($content)
$result = preg_split("/<!-- more -->/i", $content);
return array(
- $result[0],
+ trim($result[0]),
count($result) > 1
);
}
@@ -257,16 +294,19 @@ public static function count($slug = null)
/**
* Gets the permalink url to a post.
*
- * @param object $post The post object to permalink.
- * @param string The full url to the post
+ * @param object $post The post object to permalink.
+ * @param string $protocol If a protocol is specified, make a full url
+ * @param string The full url to the post
*/
- public static function permalink($post)
+ public static function permalink($post, $protocol = null)
{
- return Route::url('soapbox/post', array(
+ $route = Route::get('soapbox/post')->uri(array(
'year' => Date::formatted_time($post->posted_date, "Y"),
'month' => Date::formatted_time($post->posted_date, "m"),
'slug' => $post->slug
));
+
+ return URL::site($route, $protocol);
}
/**
View
@@ -5,6 +5,9 @@
// The name of your soapbox blog
'title' => "My Blog",
+ // Short description of the blog
+ 'description' => "A place for my thoughts",
+
/**
* The name of the section on your site the blog will reside. Include trailing slash!!
*

0 comments on commit db25a68

Please sign in to comment.