Skip to content

Instantly share code, notes, and snippets.

@JossWhittle
Created July 7, 2012 23:51
Show Gist options
  • Save JossWhittle/3068620 to your computer and use it in GitHub Desktop.
Save JossWhittle/3068620 to your computer and use it in GitHub Desktop.
A PHP function for simultaneously building 'flat' (hardcoded) RSS, Atom, and Sitemap XMLs...
<?php
// Called after an insertion or update to the `posts` table
// Returns void
function writeScripts() {
$atom_date = getDateRFC(getD()); // Current Date 3339 for Atom feed
$site_date = getDateSite(getD()); // Current Date YYYY-MM-DD for Sitemap
// Beginning of Atom.xml
$atom_str = '<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
<title>L2Program</title>
<subtitle>L2Program.co.uk Atom Feed</subtitle>
<link href="http://www.l2program.co.uk/atom.xml" rel="self"/>
<updated>'.$atom_date.'</updated>
<author>
<name>Joss Whittle</name>
<email>[email protected]</email>
</author>
<id>tag:L2Program.co.uk,2012:http://www.l2program.co.uk/atom.xml</id>';
// Beginning of Rss.xml
$rss_str = '<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0">
<channel>
<title>L2Program</title>
<link>http://www.l2program.co.uk/rss.xml</link>
<description>L2Program.co.uk RSS Feed</description>
<language>en-us</language>
<copyright></copyright>';
// Beginning of Sitemap.xml
$site_str = '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
<url>
<loc>http://www.l2program.co.uk/</loc>
<lastmod>'.$site_date.'</lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>';
// Get all the posts in reverse chronological order and loop through them
$q = mysql_query("SELECT * FROM `posts` ORDER BY `id` DESC");
while ($r = mysql_fetch_array($q)) {
// This is my format function, it runs a series of REGEX's on the plain text
// body of an article to swap my short hands for HTML tags
$body = fArticle($r['body']);
// To embed this in RSS or Atom we have to strip everything except p, a,
// img, ul, and li tags, and fix the links from being local to absolute
$content = strip_tags($body, '<p><a><img><ul><li>');
$content = preg_replace("/\"\/ext/i", '"http://www.l2program.co.uk/ext', $content);
// The URL for this article
$url = 'http://www.l2program.co.uk/post/'.$r['id'].'/'.parseTitle($r['title']);
// Add a node to the Atom Feed for this post
$atom_str .= ' <entry>
<title>'.$r['title'].'</title>
<link type="text/html" href="'.$url.'"/>
<id>tag:L2Program.co.uk,2012:'.$url.'</id>
<updated>'.getDateRFC($r['lastmod']).'</updated>
<author>
<name>Joss Whittle</name>
</author>
<content type="html">'.htmlspecialchars($content).'</content>
</entry>
';
// Add a node to the RSS Feed for this post
$rss_str .= ' <item>
<title>'.$r['title'].'</title>
<description>'.htmlspecialchars($content).'</description>
<link>'.$url.'</link>
<pubDate>'.getDateRSS($r['lastmod']).'</pubDate>
</item>
';
// Add a node to the Sitemap for this post
$site_str .= ' <url>
<loc>'.$url.'</loc>
<lastmod>'.getDateSite($r['lastmod']).'</lastmod>
<changefreq>daily</changefreq>
<priority>0.9</priority>
</url>
';
}
// Clean up and close all the feeds
$atom_str .= '</feed>';
$rss_str .= ' </channel>
</rss>';
$site_str .= '</urlset>';
// Write the three feeds to their corresponding files in the root of the website
$fp = fopen('../../atom.xml', 'w');
fwrite($fp, $atom_str);
fclose($fp);
$fp = fopen('../../rss.xml', 'w');
fwrite($fp, $rss_str);
fclose($fp);
$fp = fopen('../../sitemap.xml', 'w');
fwrite($fp, $site_str);
fclose($fp);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment