Location: PHPKode > scripts > Query2RSS > query2rss/query2rss.class.php
<?php
/* This file is really *free* software, not like FSF ones.
*  Do what you want with this piece of code, I just enjoyed coding, don't care.
*/

/**
* Maps the result of a query in a RSS 2 file.
* @author Francesco Ciracì <hide@address.com>
* @link http://sydarex.org
* @version 0.2
* @copyright Copyleft (c) 2009/2010 Francesco Ciracì
*/

/**
* Query2RSS class.
*
* Maps the result of a query in a RSS 2 file.
* @author Francesco Ciracì <hide@address.com>
* @copyright Copyleft (c) 2009, Francesco Ciracì
*/
class Query2RSS {

	/**
	 * The title of the feed. Required.
	 *
	 * @var string
	 */
	public $feed_title = '';

	/**
	 * The link to your website. Required.
	 *
	 * @var string
	 */
	public $feed_link = '';

	/**
	 * The description of your feed. Required.
	 *
	 * @var string
	 */
	public $feed_description = '';

	/**
	 * An associative array of fields->value, for fields not explicity supported. Optional.
	 *
	 * @var array
	 * @access private
	 */
	private $feed_customFields = array();

	/**
	 * An associative array of queryfields->itemfields, for mapping thge query to the item object.
	 *
	 * @var array
	 * @access private
	 */
	private $queryMap = array();

	/**
	 * Class constructor. Sets the required fields.
	 *
	 * @param string $feed_title the feed title.
	 * @param string $feed_link the link to your site.
	 * @param string $feed_description the feed description.
	 */
	function __construct($feed_title, $feed_link, $feed_description) {
		$this->feed_title = $feed_title;
		$this->feed_link = $feed_link;
		$this->feed_description = $feed_description;
	}
	
	/**
	 * String preparing. Escapes the entities in the string, trims. 
	 *
	 * @param string $s the the string to escape.
	 * @return string
	 */
	function prepare($s) {
		$s = htmlentities($s, ENT_QUOTES);
		return $s;
	}
	
	/**
	 * Date format. Formats a date in the right format. 
	 *
	 * @param timestamp $date the date to format.
	 * @return string
	 */
	function dateformat($date) {
		return date('r',$date);
	}
	
	/**
	 * Defines the query-item mapping. 
	 *
	 * If a query is already mapped, the new mapping will overwrite the old one.
	 *
	 * @param array $map queryfield->itemfield mapping associative array.
	 */
	function map($map) {
		$this->queryMap = array_merge($this->queryMap, $map);
	}
	
	/**
	 * Maps a query field to a item field. 
	 *
	 * @param string $q_field the query field.
	 * @param string $i_field the item field.
	 */
	function fieldmap($q_field, $i_field) {
		$this->queryMap[$q_field] = $i_field;
	}
	
	/**
	 * Maps a feed custom property to a value. 
	 *
	 * @param string $field the feed property.
	 * @param string $value the value.
	 */
	function custom($field, $value) {
		$this->feed_customFields[$field] = $value;
	}
	
	/**
	 * Generates item object. 
	 *
	 * @access private
	 * @param resource $recordset the result of the query.
	 * @return string
	 */
	private function itemGen($recordset) {
		$items = '';
		while ($record = mysql_fetch_assoc($recordset)) {
			$current = '';
			foreach ($record as $field => $value) {
				if(array_key_exists($field, $this->queryMap)) {
					$current.="      <{$this->queryMap[$field]}>".$this->prepare($value)."</{$this->queryMap[$field]}>\n";
				}
			}
			if(!empty($current)) $items .= "    <item>\n".$current."    </item>\n";
		}
		return $items;
	}
	
	/**
	 * Generates the feed and returns it. 
	 *
	 * @param resource $recordset the result of the query.
	 * @return string
	 */
	function generate($recordset) {
		// Generate head
		$feed = '<?xml version="1.0"?>'."\n";
		$feed .= '<rss version="2.0">'."\n";
		$feed .= '  <channel>'."\n";
		// Generate channel elements
		$feed .= '    <title>'.$this->prepare($this->feed_title).'</title>'."\n";
		$feed .= '    <link>'.$this->prepare($this->feed_link).'</link>'."\n";
		$feed .= '    <description>'.$this->prepare($this->feed_title).'</description>'."\n";
		foreach ($this->feed_customFields as $field => $value) {
			$feed .= '    <'.$field.'>'.$this->prepare($value).'</'.$field.'>'."\n";
		}
		// Generate items
		$feed .= $this->itemGen($recordset);
		$feed .= '  </channel>'."\n";
		$feed .= '</rss>';
		return $feed;
	}
	
	/**
	 * Generates the feed and writes it to a file. Returns TRUE if success and FALSE otherwise. 
	 *
	 * @param resource $recordset the result of the query.
	 * @param string $out the file to write.
	 * @return bool
	 */
	function gen2file($recordset, $out) {
		$fw = file_put_contents($out, $this->generate($recordset));
		if($fw) return true;
		return false;
	}
 }
 ?>
Return current item: Query2RSS