The RSS Feed

Xerol

Emperor
Joined
Oct 6, 2002
Messages
1,542
Location
In an IDE.
I noticed the orange button on the GOTM page, and clicked (which, by the way, the button itself should be linked as well) and was disappointed in the feed.

Harsh Comment Warning - Some comments ahead might be a bit harsh.
Possible Disclaimer to Harsh Comment Warning - The feed appears to be a built-in function of the CMS in use, so I'm not exactly directing this at anyone in particular. I think.
Excessive Formatting Alert - I'm feeling overly liberal with using bbCode tonight.

Anyway, I'll start with the obvious: The feed's outdated. Since it's a static XML document with a pubDate of 26 March, this might be a result of a maintenance script that hasn't run or a manual update which should be run. (Inline Disclaimer: I'm not familiar with the CMS in use here, so I can only make assumptions about the internal workings.) Also, the individual feed items have no description - while it's not required in a feed, a bit more than just the title would be useful.

Moving on, this next section comes with illustration:


The image link isn't linked right, it goes to http://http//gotm.civfanatics.net/ which obviously isn't right. There's also a bunch of other issues, see this for more on those. Specifically, <date> should be <pubDate> (note the capital D, for some reason that's the ONLY tag where you're required to have a capital letter), <id> should probably be <guid>, and there's no such thing as <timestamp> and <datestamp> (pubDate is what should be used).

Restatement of Possible Disclaimer to Harsh Comment Warning - The feed appears to be a built-in function of the CMS in use, so I'm not exactly directing this at anyone in particular. I think.


Anyway, it shouldn't be too hard to put up a nice working feed. You can use a php document directly as a feed, just in the way you can use a php document directly as an HTML page. The actual DB queries and stuff would be up to whoever's in charge of the coding (I haven't been around for a while, but I'm assuming that's still AlanH?) but the basic code isn't too complex. The feed linked above just has a few basic elements. First you need to make sure you deliver a content type:

Code:
header("Content-Type: text/xml");

Code:
function do_head($title, $link, $description, $pubdate, $category, $imageurl, $imagetitle, $imagelink)
{
	echo "<?xml version=\"1.0\" encoding=\"windows-1252\"?>\n";
	echo "<rss version=\"2.0\">\n";
	echo "<channel>\n";
	echo "<title>$title</title>\n";
	echo "<link>$link</link>\n";
	echo "<description>$description</description>\n";
	echo "<language>en-us</language>\n";
	echo "<pubDate>$pubdate</pubDate>\n";
	if ($category)
		echo "<category>$category</category>\n";
	if ($imageurl || $imagetitle || $imagelink) {
		echo "<image>";
		if ($imageurl)
			echo "<url>$imageurl</url>";
		if ($imagetitle)
			echo "<title>$imagetitle</title>";
		if ($imagelink)
			echo "<link>$imagelink</link>";
		echo "</image>\n";
	}
	echo "<generator>Xerol's phpNuke RSS Generator</generator>\n\n";
}

Ok so you'll probably want to change the last line, it only shows up if you view the source anyway. $pubdate in this case is the publication date of the feed itself, so when you call it, do something like:

Code:
	$title = "CivFanatics GOTM News";
	$link = "http://gotm.civfanatics.net/";
	$description = "(Insert a relevant description here - right now it says 'News management script and more')";
	$pubdate = date('D, d M Y H:i:s T');
	$category = "";
	$imageurl = "";
	$imagetitle = "";
	$imagelink = "";
	do_head($title, $link, $description, $pubdate, $category, $imageurl, $imagetitle, $imagelink);

You'll probably want to put the GOTM site logo in for $imageurl, http://gotm.civfanatics.net/ for $imagelink, and $imagetitle can be whatever. You can ignore $category here, it's not really important in the header anyway.

The next part is the part that'll require DB queries.

Code:
		echo "<item>\n";
		echo "<title>".htmlspecialchars($row["news_title"])."</title>\n";
		echo "<link>".htmlspecialchars($row["news_link"])."</link>\n";

		echo "<description>".htmlspecialchars($row["news_description"])."</description>\n";
		$ptime = $row["news_pubdate"];
		echo "<pubDate>".date('D, d M Y H:i:s T',$ptime)."</pubDate>\n";
		echo "<guid>".htmlspecialchars($row["news_link"])."</guid>\n";
		echo "</item>\n";

I left the $row array in there from my implementation, just make sure it gets replaced with the correct array and/or variables. <title> has the title of the news item, <link> has the anchor link to the item (like this one), <description> will have the actual text of the news item. Note: If you just want the feed to be a "summary" thing, use substr() or something like that to just display the first bit of the description, the <link> will let the reader go to the full news item anyway. The date thing is a bit different, I'm not sure how it'll be stored internally (from the looks of <datestamp> and <timestamp> it looks like it uses the standard internal timestamps) but the FORMAT is what's important here. The GUID doesn't need to be anything special, whatever's generating the <id> tags in the current feed would probably do here, as long as it's unique per <item>.

Anyway, that bit of code needs to be run for every news item (so put it inside a while ($row = sql_fetchrow()) {} or however you want to get the queries) and then after all of that runs, make sure you close it:

Code:
	echo "</channel>\n";
	echo "</rss>";

Attached is the implementation of how this is done in the case of the linked feed (I basically implemented it as a single catch-all rss.php, I was going to implement the individual <item>s in a function as well, eventually), so it's a little messy but it gets the point across. (This is implemented on a phpNuke site that I'm coding for, and I found the built-in feed generator appalling, so I made a new one. Needless to say with phpNuke, implementing ANYTHING with it gets messy.)

One Last Disclaimer/Warning - I wasn't sure whether this belonged here or in Site Feedback, but since it had to do with the GOTM site I felt it probably belonged here more. If I'm wrong, I'm wrong, and I'll enjoy the ride if it gets moved :)
 

Attachments

  • rss_implementation.txt
    8 KB · Views: 87
Thanks for your comments and suggestions.

The RSS feed is indeed a mess. It came with the ready-made news generator script that we added some time ago. I wasn't very enthusiastic about it, but it's the same as the one used on the main site so we used it. Recently it was taken off line because its security was compromised. I left a static copy of the rss and news page online, but have not updated it manually since then.

Trust me it's on my to do list, but the SGOTMs plus real life are taking all my available time currently. My first objective will be to re-instate the news writer with a secure mysql-based one, and if that doesn't come with a decent RSS feed I'll have to hand craft one, and your inputs will come in very useful.
 
Cool. I've really become fond of RSS everywhere lately, probably past the point of obsession, so I like to see nicely-formed feeds and such. Keep up the good work and hopefully I'll find time to finish a game this month.
 
I feel your pain! I'm the original born-again RSS junkie ever since Safari-RSS appeared a year or more ago. News and forum sites without feeds need to be very good these days to keep my attention. I can only think of one UK-specific non-RSS site that I'm prepared to put up with.
 
Top Bottom