icon Get the most out of Surmunity, read our tips here! Need an interesting blog to read? You've got to read the Surpass Blog! | Welcome! Please register to access all of our features.

» Surpass Web Hosting Forums » Discussions » PHP, MySQL » Rss Exporting a script for you guys

PHP, MySQL General PHP questions. Or go to our PHPsuexec Forum >>

Closed Thread
 
LinkBack Thread Tools Search this Thread Rate Thread
Old July 21st, 2003, 3:28 PM   #1 (permalink)
Registered User
Seasoned Poster
 
Joined in Jul 2003
Lives in Research Triangle Park, NC / Blacksburg, VA
57 posts
Gave thanks: 0
Thanked 0 times
Thumbs down

One thing that the community of users in my old host and I did a lot was swap code, and I just finished a script that some of you might find useful so I thought I'd post it.

I use my own news posting/editing systems which is nice because it does exactly what I want it, but syndicating it is a pain because I'd have to do everything by hand. I decided to write a small script that will export my MySQL database into RSS format so that people can syndicate the newsfeeds that I have.

It's a pretty simple script, probably some things that could have been done better/faster but it works. I tried to make it customizable enough to be within reach of most people without need for heavy modification. So here you go:

<?php

/************************************************** ********\
* RSSExport *
* Author - Justin Belcher *
* Version - 0.1 *
* Last Modification - 7-21-2003 *
* *
* This is a class that will export data from a mysql *
* database into an rss newsfeed. This was made to *
* help those who do not use complex systems of *
* newsposting to provide dynamic content to their *
* users, but still would like the ability to syndicate *
* themselves to other feeds *
* *
* INSTRUCTIONS -- *
* This class, although attempts were made to *
* generalize it, was primarily made for my site *
* and the way my data is organized, so you may *
* need to fiddle with the code to get it working *
* perfectly for you. In general, you edit the *
* rssStructure to contain the column names of *
* where that corresponding data could be found *
* in your table and it spits out the rest. You *
* should update all of the constants accordingly, *
* and if you have anything additional that you need *
* that should be added as well. *
\************************************************* *********/

class RSSExport
{
/*
* place the MySQL column names of
* the columns that correspond to
* item information for the rss feed
*/
var $rssStructure = array(
"title" => "title",
"description" => "post",
"author" => "author",
"category" => "category",
"pubDate" => "date",
// "comments" => "",
// "enclosure" => "",
// "guid" => "",
// "source" => "",
);

/*
* Database constants for the exporter
*/
var $dbName = "name";
var $dbUser = "user";
var $dbPass = "pass";
var $dbHost = "localhost";
var $table = "table";
var $connection;
var $result;

/*
* RSS feed constants.
* (note) only title, link, and description are required
* for RSS v2.0
*/
var $rssConstants = array(
"title" => "somebody's news blog",
"link" => "http://www.yoursite.com",
"description" => "This is the newsfeed for events pertaining to somebody",
"language" => "en-US",
"copyright" => "Copyright 2003 yoursite.com",
"managingEditor" => "webmasteryoursite.com",
"webMaster" => "webmasteryoursite.com",
"generator" => "RSSExport v0.1 by Justin Belcher",
"docs" => "http://blogs.law.harvard.edu/tech/rss",
"lastBuildDate" => "",
"ttl" => 360,
// "pubDate" => "",
// "category" => "",
// "cloud" => "",
// "rating" => "",
// "textInput" => "",
// "skipHours" => "",
// "skipDays" => ""
);

/*
* genearl export constansts
*/
var $domain = "yoursite.com";
var $xmlVersion = "1.0";
var $rssVersion = "2.0";
var $rssFileName = "rss.xml";
var $rssPath = "/path/to/rss/location/";
var $indentString = " ";
var $encoding = "iso-8859-1";
var $dateFormat = "r";
var $mode = "w";
var $fp;

/*
* this function makes a preliminary connection to the mysql
* database
*/
function dbSetup() {
$ok = true;
$ok = $this->con = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass);
$ok = mysql_select_db($this->dbName, $this->con);
return $ok;
}
/*
* this function indents a string the given indentation level
* parms: s -- the string to indent
* lev -- the level to indent it
*
* returns: indented string
*/
function indent($s, $lev)
{
for ($i = 0; $i < $lev; $i++)
$spaces .= $this->indentString;
return $spaces . $s;
}
/*
* this function begins the xml tag and prints any attributes
* it might have
* parms: tagName -- the name of the tag
* hasAttr -- whether or not it has attributes
* attr -- an array of alternating name and value strings for the attributes
*
* returns: the string representation of the open tag
*/
function getStartTag($tagName, $hasAttr, $attr)
{
$tag = "<";
$tag .= $tagName;
if ($hasAttr)
{
for ($i = 0; $i < count($attr); $i += 2)
{
$tag .= " ";
$tag .= $attr[$i];
$tag .= "=\"";
$tag .= $attr[$i+1];
$tag .= "\"";
}
}
$tag .= ">";

return $tag;
}
/*
* this function closes the xml tag
* parms: hasName -- whether the tag closes another tag or is inline
* name -- the name of the tag if there is one
*
* returns: the string representation of the close tag
*/
function getEndTag($hasName, $name)
{
if ($hasName)
{
$tag = "</";
$tag .= $name;
$tag .= ">";
}
else
$tag = " />";

return $tag;
}
/*
* this function takes a mysql timestamp and makes it human-readable
* parms: dt -- the date timestamp
* format -- the format to format the timestamp to
* timezone -- whether or not to print out timezone information
*
* returns: the string representation of the formatted date
*/
function mysql_timestamp_to_human($dt, $format, $timezone)
{
$yr=strval(substr($dt,0,4));
$mo=strval(substr($dt,4,2));
$da=strval(substr($dt,6,2));
$hr=strval(substr($dt,8,2));
$mi=strval(substr($dt,10,2));
$se=strval(substr($dt,12,2));

return date($format, mktime ($hr,$mi,$se,$mo,$da,$yr)). (($timezone) ? " EDT" : "");
}
/*
* this function prints out the individual rss items to the stream
* parms: level -- the level of indentation at current
*
* returns: none
*/
function printRSSItems($level)
{
// get the row contents of the table
$query = "SELECT * FROM " . $this->table;
$this->result = mysql_query($query, $this->con);
while ($row = mysql_fetch_array($this->result))
{
// start item
fwrite($this->fp, $this->indent($this->getStartTag("item", true, $attr) . "\n", $level));

// get current level of indent based on position in xml
$level++;
$indentation = "";
for ($i = 0; $i < $level+1; $i++)
$indentation .= $this->indentString;

// itterate through and print out all information from table
foreach ($this->rssStructure as $n => $v)
{
if ($n == "description")
$row[$v] = str_replace("<br />","", $row[$v]);
if ($n == "author")
$row[$v] .= ("".$this->domain);
fwrite($this->fp, $this->indent($this->getStartTag($n, false, NULL), $level));
$level++;
fwrite($this->fp, ($n == "pubDate") ? $this->mysql_timestamp_to_human($row[$v], $this->dateFormat, false) : $row[$v]);
$level--;
fwrite($this->fp, $this->getEndTag(true, $n)."\n");
}
// close out the item
$level--;
fwrite($this->fp, $this->indent($this->getEndTag(true, "item") . "\n", $level));
}

// done with MySQL
mysql_free_result($this->result);
mysql_close($this->con);
}
/*
* this function is the main entry point for the exporting routine
*/
function exportRSS() {

$level = 0;
if ($this->dbSetup())
{
// setup file
$this->fp = fopen($this->rssPath . $this->rssFileName, $this->mode);

// begin document
fwrite($this->fp, "<?xml version=\"" . $this->xmlVersion . "\" ?". ">\n");
$time = date($this->dateFormat, mktime());
$this->rssConstants["lastBuildDate"] = $time;
fwrite($this->fp, "<!-- This RSS was generated with " . $this->rssConstants["generator"] . " on " . $time . " -->\n");
$attr = array("version", $this->rssVersion);
fwrite($this->fp, $this->getStartTag("rss", true, $attr) . "\n");
$level++;

// start channel
fwrite($this->fp, $this->indent($this->getStartTag("channel", false, NULL) . "\n", $level));
$level++;

// print channel tags
foreach($this->rssConstants as $a => $v)
fwrite($this->fp, $this->indent($this->getStartTag($a,false,NULL) . $v . $this->getEndTag(true, $a) . "\n", $level));

// print out news items
$this->printRSSItems($level);

// close document
$level--;
fwrite($this->fp, $this->indent($this->getEndTag(true, "channel") . "\n", $level));
$level--;
fwrite($this->fp, $this->indent($this->getEndTag(true, "rss") . "\n", $level));
fclose($this->fp);
}
else
echo "error connecting to MySQL database, aborting";
}
}
?>

Justin
__________________
"I tried to go to Target but I missed" ~Mitch
VTJustinB is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On