Source for file rss.class.php
Documentation is available at rss.class.php
* generates RSS 2.0 feed according to given data
* @author Matej Finwe Humpal <finwe@finwe.info>
* @license http://creativecommons.org/licenses/by-sa/2.5/ Creative Commons Attribution-ShareAlike 2.5
* @link http://weblog.finwe.info/item/php-trida-generujici-rss Tutorial Article at finwe.info (cs-CZ)
* @link http://blogs.law.harvard.edu/tech/rss RSS 2.0 Specification
* If making derivative works, you MUST attribute the author in one or more of following ways:<br>
* Leave rss generator tags and comments in the output<br>
* Place the link http://weblog.finwe.info/item/php-trida-generujici-rss to your product documentation<br>
* Place the link http://weblog.finwe.info/item/php-trida-generujici-rss to your product website
* @author Matej Finwe Humpal <finwe@finwe.info>
* @version Rss.class.php, v 1.3 2007/02/17 14:48:00
* @license http://creativecommons.org/licenses/by-sa/2.5/ Creative Commons Attribution-ShareAlike 2.5
* @link http://weblog.finwe.info/item/php-trida-generujici-rss Tutorial Article at finwe.info (cs_CZ)
* @example rss.php Basic example
* Data retrieval handler setting.
* Allows user to define his own data-retrieval function, even outside of the class.
* string 'getData' will call function gatData
* array('pgSql','getData') will staticaly call method getData of pgSql object (pgSql::getData()).
* there are two pre-prepared methods - getMysqlData() and getMysqliData().
* They both use db parameters specific for Rss class
* @var mixed Either string for external static function or array with two parametres (Class,Method) for calling the method staticaly
* DB host. defaults localhost
* Name of the database to use.
* Title of the feed. Should be the same as the page title.
* Oprional feed namespaces.
* Custom channel subelements (pubDate, ttl, skipHours etc.)
* Array of database fields to use.
* Key is a name of RSS tag, value is name of database field, from which the data are to be taken.
* Value can be an array of db field names for usage with link, comments and guid tags. @see $postLink
* SQL query to run on given database.
* Is prepared for sprintf or vsprintf. Instead of %s variables, data from database will be used.
* If multiple variables used, $dbFields must be set accordingly (array in array value)
* '/item/%s' string with one variable
* '/item/%s-%s' string with multiple variables
* @example rss.php Basic example
* String of item-comment-link.
* Is prepared for sprintf or vsprintf. Instead of %s variables, data from database will be used.
* If multiple variables used, $dbFields must be set accordingly (array in array value)
* '/item/%s' string with one variable
* '/item/%s-%s' string with multiple variables
* @example rss.php Basic example
* Method overloading - allows user to set Rss variables.
public function __call($name,$args)
$forbidden = array('namespaces');
if (substr($name, 0, 3) == 'set' && $var = substr($name, 3)) {
if (isset ($this->$var)) {
throw new Exception(sprintf('Non-existing method %s::%s() called in %s, line %s', __CLASS__ , $name, __FILE__ , __LINE__ ));
* @param string $context Page context-type.
protected function setHeader($context = 'text/plain')
* Adds user-defined namespace
* @param string $namespace Namespace itself
* @param string $link link to namespace definition
* @param string $link link to namespace definition
* @return string Namespaces
* Adds user-defined channel subelement
* @param string $element name of the tag
* @param mixed $data tag data
* @see writeChannelSubelements()
* @uses Rss::$channelSubelements
* writes Channel Subelements XML
* @return string XML of channel subelements
* @uses Rss::$channelSubelements
$out .= '<' . $key . '>' . $val . '</' . $key . '>'. "\n";
$out .= '<' . $key . ' domain="' . $val["domain"] . '" port="' . $val["port"] . '" path="' . $val["path"] . '" registerProcedure="' . $val["registerProcedure"] . '" protocol="' . $val["protocol"] . '" />'. "\n";
$out .= '<' . $key . '>'. "\n";
foreach ($val as $skip) {
$out .= "\t". '<hour>' . $skip . '</hour>'. "\n";
$out .= '</' . $key . '>'. "\n";
* Writes RSS prologue with Channel obligatory tags
$out .= '<?xml version="1.0" encoding="' . $this->encoding . '" ?>'. "\n";
$out .= '<!-- generator="Rss PHP class (http://weblog.finwe.info/item/php-trida-generujici-rss)" -->'. "\n";
$out .= '<channel>'. "\n";
$out .= '<title>' . $this->title . '</title>'. "\n";
$out .= '<link>' . $this->link . '</link>'. "\n";
$out .= '<description>' . $this->descripton . '</description>'. "\n";
$out .= '<generator>Rss PHP class (http://weblog.finwe.info/item/php-trida-generujici-rss)</generator>'. "\n";
$out .= (!empty($this->image)) ? '<image>' . $this->image . '</image>'. "\n" : '';
$out .= '<pubDate>' . date('r') . '</pubDate>';
$out .= '<language>' . $this->language . '</language>'. "\n";
* By RSS Spec, description text should be 450 chars max. Lets ensure it is.
* @author Monte Ohrt <monte at ohrt dot com>
* @return string Truncated string
protected function splitLongText($string, $length = 450, $etc = '...', $break_words = false, $middle = false)
if (strlen($string) > $length) {
if (!$break_words && !$middle) {
return substr($string, 0, $length). $etc;
return substr($string, 0, $length/ 2) . $etc . substr($string, - $length/ 2);
* Database data withdrawal, mysql driver
* @return array $return Data fetched from MySQL < 4.1 database
* Database data withdrawal, mysqli driver
* @return array $returnData fetched from MySQL >= 4.1 database
$db->query("SET CHARACTER SET UTF8");
$query = $db->query($this->dbSql) or die($db->error);
while($array = $query->fetch_array(MYSQLI_ASSOC)){
* Prepares array of data for further usage
* @return array $return Data prepared for insertion to XML
throw new Exception('dbFields parameter is suppossed to be an array');
foreach($data as $array){
foreach ($this->dbFields as $key => $val) {
$return[$i][$key] = '<![CDATA[' . $this->splitLongText($array[$val]) . ']]>';
$return[$i][$key] = $array[$val];
$return[$i][$key] = date('r',$array[$val]);
$return[$i][$key] = $array[$val];
$return[$i][$key] = sprintf($val[1],$array[$val[0]]);
$return[$i][$key] = vsprintf($val[1],$array[$val[0]]);
* Writes ITEM tags of RSS from values prepared by prepareData()
* @param array $data Data prepared from {@link prepareData()}
throw new Exception('Data has to be an array');
foreach ($data as $item) {
foreach ($item as $key => $val) {
$out .= (!empty($val)) ? "\t". '<' . $key . '>' . $val . '</' . $key . '>'. "\n" : '';
$out .= '</channel>'. "\n";
* Complete method for final usage.
|