Analysis | News Focus | Features | Podcasts | Sidelines 
 

Instructions

1. copy the adjacent php code into a standalone php file anywhere in your webspace. Change the variables in class Vars.

2. right at the bottom you'll find some perl. Copy into gm.cgi near the top immediately after the line:
print "Content-type: text/html\n\n"; (At least you should find this in Greymatter 1.3).

The RSS files will now be rebuilt every time you run gm.cgi, eg when you add an entry to your Blog or run Rebuild

Convert Greymatter to RSS 2.0 and Atom 1.0 feeds

<?php
// greymatter2rss
// creates valid RSS 2.0 and Atom 1.0 feeds from a Greymatter Blog
// version: 2.0
// written: November 2006
// author: Dave Ches
// email: ls8@daveches.co.uk
// http://www.daveches.co.uk/

// change the variables in class Vars

class Vars
{
var $title = "title of your feed";
var $description = "description of your feed";
var $lang = "en-gb";
var $webmasterEmail = "you@xxx.yyy.zzz";
var $editorEmail = "yourfriend@xxx.yyy.zzz";
var $docs = "http://www.rssboard.org/rss-specification";
var $generator = "greymatter2rss version 2.0";
var $archivesDir = "/filesystem/path/to/archives/dir";
var $cgiDir = "/filesystem/path/to/greymatter/dir";
var $siteUrl = "http://xxx.yyy.zzz";
var $archivesUrl = "http://xxx.yyy.zzz/path/to/archives/dir";
var $dirFeed = "/filesystem/path/to/feed/home"; /* dir must exist */
var $fileFeedRSS2 = "nameofrssfeed.xml"; /* file must exist with permissions set: may need world write permission: depends how you're running php */

// atom specific vars
var $authorName = "you";
var $fileFeedAtom = "nameofatomfeed.xml";
var $feedId = "uniqueid"; /* see eg http://www.hesido.com/web.php?page=atomidtimestamp */
var $domainName = "xxx.yyy.zzz";
var $archivesWebDir = "/web/path/to/archives";
var $feedLocation = "http://xxx.yyy.zzz/path/to/feed/dir";

var $limit = 6; /* number of gm entries wanted in feed */
}

$content = getEntryInfo();
writeRSS2Feed($content);
writeAtomFeed($content);

function getEntryInfo()
{
$vars = new Vars;

/* array to store fields about each entry */
$entry_array = array();
$loopcount = 0;
/* read file listing summary of every Greymatter entry */
$entries = file($vars->cgiDir . "/" . "gm-entrylist.cgi");
foreach($entries as $an_entry)
{
$lines = explode("|", $an_entry);
$postnum = $lines[0];
$subject = $lines[2];
$publicationdate = $lines[3];
$publicationtime = $lines[4];
$closed = $lines[5];

/* if entry is marked as Open in Greymatter */
if($closed == "O")
{
/* build name of archive htm file for this entry: needed for link tag in xml file eg 00000001.htm */
$postnum = str_pad($postnum, 8, "0", STR_PAD_LEFT);
$postfile = $postnum.".htm";
/* build name of archive cgi file for this entry so we can get at body content of entry eg 00000001.cgi */
$postfile = $postnum.".cgi";
$entry_details = file($vars->archivesDir . "/" . $postfile);
$entry_text = $entry_details[2];
/* description will be limited to first 150 characters of story */
$entry_text = substr($entry_text, 0, 150);
$entry_text = parse($entry_text);
array_push($entry_array, array("title"=>$subject, "link"=>$postnum, "description"=>$entry_text, "date"=>$publicationdate, "time"=>$publicationtime, "postnum"=>$postnum));
}
$loopcount++;
/* limit items in feed to limit set in vars object */
if($loopcount == $vars->limit)
{
  break;
}
} // end loop
/* return 2 dimensional array created in loop */
return ($entry_array);
}

function writeRSS2Feed($entries)
{
$vars = new Vars;
$fh = fopen ($vars->dirFeed . "/" . $vars->fileFeedRSS2,"w");
fwrite ($fh, "<?xml version=\"1.0\"?>\n");
fwrite ($fh, "<rss version=\"2.0\">");
fwrite ($fh, "<channel>");
fwrite ($fh, "<title>" . $vars->title . "</title>");
fwrite ($fh, "<link>" . $vars->siteUrl . "</link>");
fwrite ($fh, "<description>" . $vars->description . "</description>");
fwrite ($fh, "<language>" . $vars->lang . "</language>");
fwrite ($fh, "<docs>" . $vars->docs . "</docs>");
fwrite ($fh, "<generator>" . $vars->generator . "</generator>");
fwrite ($fh, "<managingEditor>" . $vars->editorEmail . "</managingEditor>");
fwrite ($fh, "<webMaster>" . $vars->webmasterEmail . "</webMaster>");
/* date in RFC822 format so feed validates*/
fwrite ($fh, "<lastBuildDate>" . date("r") . "</lastBuildDate>");
foreach($entries as $entry)
{
fwrite ($fh, "<item>\n");
fwrite ($fh, "<title>\n" . $entry['title'] . "</title>\n");
fwrite ($fh, "<link>\n" . $vars->archivesUrl . "/" . $entry['link'] . ".htm</link>\n");
fwrite ($fh, "<description>\n" . $entry['description'] . "...</description>\n");
/* create rfc822 date for entry publication date */
if (substr($entry['time'],6) == "PM")
{
if ( substr($entry['time'],0,2) < 12)
{
$hour = substr($entry['time'],0,2) + 12;
$time = str_replace( substr($entry['time'],0,2), $hour, $entry['time'] );
/* chop off the 'AM' or 'PM' */
$entry['time'] = substr($time, 0,5);
}
else
{
$entry['time'] = substr($entry['time'], 0,5);
}
}
$month = substr($entry['date'],0,2);
$rfcmonth = monthAsString($month);
$rfcdate = substr($entry['date'],3,2) . " " . $rfcmonth . " 20" . substr($entry['date'],6,2) . " " . $entry['time'] . " GMT";
fwrite ($fh, "<pubDate>" . $rfcdate . "</pubDate>\n");
fwrite ($fh, "<guid isPermaLink=\"true\">" . $vars->archivesUrl . "/" . $entry['link'] . "</guid>\n");
fwrite ($fh, "</item>\n");
}
fwrite ($fh, "</channel>\n</rss>");
fclose($fh);
}

function writeAtomFeed($entries)
{
$vars = new Vars;
$fh = fopen ($vars->dirFeed . "/" . $vars->fileFeedAtom,"w");
fwrite ($fh, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
fwrite ($fh, "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n");
fwrite ($fh, "<title>" . $vars->title . "</title>\n");
fwrite ($fh, "<link href=\"" . $vars->feedLocation . $vars->fileFeedAtom . "\" rel=\"self\"/>\n");
// RFC 3339 time
fwrite ($fh, "<updated>" . date('Y-m-d') ."T" . date('H:i:s') . "Z</updated>\n");
fwrite ($fh, "<id>" . $vars->feedId . "</id>\n");
fwrite ($fh, "<generator>" . $vars->generator . "</generator>\n");
fwrite ($fh, "<author>\n");
fwrite ($fh, "<name>" . $vars->authorName . "</name>\n");
fwrite ($fh, "<email>" . $vars->editorEmail . "</email>\n");
fwrite ($fh, "</author>\n");
foreach($entries as $entry)
{
fwrite ($fh, "<entry>\n");
fwrite ($fh, "<title>" . $entry['title'] . "</title>\n");
fwrite ($fh, "<link href =\"" . $vars->archivesUrl . "/" . $entry['link'] . ".htm\"/z`>\n");
fwrite ($fh, "<summary type = \"html\">" . $entry['description'] . "...</summary>\n");
$fulldate = "20". substr($entry['date'],6) . "-" . substr($entry['date'],0,2) . "-" . substr($entry['date'],3,2);
// see eg http://www.taguri.org/
fwrite ($fh, "<id>tag:" . $vars->domainName . "," . $fulldate . ":" . $vars->archivesWebDir . "/" . $entry['postnum'] . "</id>\n");
// RFC 3339 time
fwrite ($fh, "<updated>20" . substr($entry['date'],6) . "-" . substr($entry['date'],0,2) . "-" . substr($entry['date'],3,2) . "T" . substr($entry['time'], 0,2) . ":" . substr($entry['time'], 3,2) . ":00Z</updated>\n");
fwrite ($fh, "</entry>\n");
}
fwrite ($fh, "</feed>");
fclose($fh);
}

function parse($text)
{
/* break at end of last full word */
$cutoff = strrpos($text, " ");
$text = substr($text, 0, $cutoff);
/* gm uses |*| to represent new line */
$text = str_replace("|*||*|", "&lt;br /&gt;&lt;br /&gt;", $text);
$text = str_replace("|*|", "&lt;br /&gt;", $text);
$text = strip_tags($text);
/* unless you're really careful, RSS illegal chars will occasionally get used in your Greymatter entries */
$text = str_replace("%", "percent", $text);
$text = str_replace("£", "pound", $text);
$text = str_replace(" & ", " and ", $text);
$text = str_replace("’", "'", $text);
return($text);
}

/* used by function writeRSS2Feed to create rfc822 date */
function monthAsString($month)
{
$stringmonth = array("filler", "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
/* get rid of leading zeros */
$month = intval($month);
return $stringmonth[$month];
}

?>

Convert Greymatter to RSS 2.0 and Atom 1.0 feeds: code for gm.cgi

print <<EOF;
<iframe src="http://location.of/greymatter-to-rss.php" width=0 height=0>
</iframe>
EOF
;
About this site
XHTML 1.0