We frequently get asked about how to create RSS feeds for gforum, so here's a quick howto for adding RSS feeds to your forum:
Adrian
- Add an rss.xml template to your default template set. Here's a sample one from our list archive: Code:<?xml version="1.0" encoding="iso-8859-1" ?><%rss_feed%><%if rss_error%>Ooops: <%rss_error%><%else%>
<rss version="2.0">
<channel>
<%loop forum_loop%>
<title><%xmlrss($cat_name)%> | <%xmlrss($forum_name)%></title>
<description>Mailing List Archive by Gossamer Threads</description>
<link>http://www.gossamer-threads.com/lists/<%list%>/<%lc forum_name%>/</link>
<language>en-us</language>
<copyright>(c) Gossamer Threads Inc. All rights reserved.</copyright>
<lastBuildDate><%rss_current_date%></lastBuildDate>
<ttl>120</ttl>
<image>
<title>Gossamer Threads | <%xmlrss($cat_name)%> | <%xmlrss($forum_name)%></title>
<width>75</width>
<height>23</height>
<link>http://www.gossamer-threads.com/lists/<%list%>/<%lc forum_name%>/</link>
<url>http://www.gossamer-threads.com/images/lists/rss_logo.jpg</url>
</image>
<%loop post_loop%><item>
<title><%xmlrss($post_subject)%></title>
<description><%xmlrss($post_summary)%></description>
<pubDate><%post_rss_date%></pubDate>
<link>http://www.gossamer-threads.com/lists/<%list%>/<%lc forum_name%>/<%post_id%></link>
</item><%endloop%>
<%endloop%>
</channel>
</rss><%endif%> - Add the following globals:
rss_feed:xmlrss:Code:sub {
my $forum_id = $IN->param('forum_id');
if ($forum_id !~ /^\d+$/) {
return { rss_error => "Invalid forum id." };
}
my $forum_group_table = $DB->table('ForumGroup');
if (!$forum_group_table->count(GT::SQL::Condition->new('group_id_fk','=','100','forum_perms','>=','3', 'forum_id_fk','=',$forum_id))) {
return { rss_error => "You are not allowed to view this forum." };
}
my $Category = $DB->table('Category');
my $Post = $DB->table('Post');
my $Forum = $DB->table('Forum');
my $count = $IN->param('count');
$count = 10 unless ($count and $count =~ /^(5|10|15|25|50|100)$/);
my $format = $IN->param('format') || 'topics';
$format = 'topics' unless ($format and $format =~ /^(topics|posts)$/);
my @forums;
require GForum::Forum;
require GForum::Post;
my $forum = $Forum->select( { forum_id => $forum_id } )->fetchrow_hashref;
if (! $forum) { return { rss_error => "Unknown forum." }; }
$forum->{cat_id} ||= $forum->{cat_id_fk};
GForum::Forum::normalize($forum);
$Post->select_options('ORDER BY post_time DESC', "LIMIT $count");
my @post_ids;
if ($format eq 'topics') {
@post_ids = $Post->select( 'post_id', { forum_id_fk => $forum_id, post_root_id => 0 })->fetchall_list;
}
else {
# Ugh, MySQL picks the wrong index here.
my $name = $Post->name;
my $sth = $Post->do_query("SELECT post_id FROM $name USE INDEX (p_fmn) WHERE forum_id_fk = $forum_id ORDER BY post_time DESC LIMIT $count");
@post_ids = $sth->fetchall_list;
}
next unless @post_ids;
$Post->select_options('ORDER BY post_time DESC');
my $posts = $Post->select( { post_id => \@post_ids } )->fetchall_hashref;
for my $post (@$posts) {
GForum::Post::plain_text(\$post->{post_message}, $post);
$post->{post_summary} = substr($post->{post_message}, 0, 150);
$post->{post_summary} =~ s/[\w\d]*$//;
$post->{post_rss_date} = GT::Date::date_get($post->{post_time}, "%dd% %mmm% %yyyy% %HH%:%MM%:%ss% -0800");
}
GForum::Post::normalize($posts);
$forum->{post_loop} = $posts;
push @forums, $forum;
my $cat_info = $Category->select( { cat_id => $forum->{cat_id_fk} })->fetchrow_hashref;
my $rss_current_date = GT::Date::date_get(time, "%dd% %mmm% %yyyy% %HH%:%MM%:%ss% -0800");
return { forum_loop => \@forums, %$cat_info, rss_current_date => $rss_current_date };
}Code:sub {
my $review = shift;
$review = GT::CGI::html_escape($review);
$review =~ s/\n/ /g;
$review =~ s/\r/ /g;
$review =~ s/ / /g;
$review =~ tr/\x91\x92/'/;
$review =~ tr/\x93\x94/"/;
$review =~ s/\x95/•/g;
$review =~ tr/\x96/-/;
$review =~ s/\x97/--/;
$review =~ s/^\s+|\s+$//g;
return $review;
} - Add a new action to give access to this page. Go to the admin => Setup => Actions => Add action => rss, with the page=rss.xml
- Finally, add a link on your forum to the rss feed. It will have the format: gforum.cgi?do=rss;forum_id=<%forum_id%>
Adrian