2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-02-25 03:10:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage integration
|
|
|
|
*/
|
|
|
|
|
2007-09-16 17:56:15 +02:00
|
|
|
/**
|
|
|
|
* RSSFeed class
|
|
|
|
*
|
|
|
|
* This class is used to create an RSS feed.
|
2008-03-03 00:24:10 +01:00
|
|
|
* @todo Improve documentation
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @subpackage integration
|
2007-09-16 17:56:15 +02:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class RSSFeed extends ViewableData {
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Casting information for this object's methods.
|
|
|
|
* Let's us use $Title.XML in templates
|
|
|
|
*/
|
|
|
|
public static $casting = array(
|
|
|
|
"Title" => "Varchar",
|
|
|
|
"Description" => "Varchar",
|
|
|
|
);
|
2007-09-16 17:56:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds the feed entries
|
|
|
|
*
|
|
|
|
* @var DataObjectSet
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $entries;
|
2007-09-16 17:56:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Title of the feed
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $title;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Description of the feed
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Link to the feed
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $link;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of the title field of feed entries
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $titleField;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of the description field of feed entries
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $descriptionField;
|
|
|
|
|
|
|
|
/**
|
2008-02-25 03:10:37 +01:00
|
|
|
* Name of the author field of feed entries
|
2007-09-16 17:56:15 +02:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $authorField;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Last modification of the RSS feed
|
|
|
|
*
|
|
|
|
* @var int Unix timestamp of the last modification
|
|
|
|
*/
|
|
|
|
protected $lastModified;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ETag for the RSS feed (used for client-site caching)
|
|
|
|
*
|
|
|
|
* @var string The value for the HTTP ETag header.
|
|
|
|
*/
|
|
|
|
protected $etag;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param DataObjectSet $entries RSS feed entries
|
|
|
|
* @param string $link Link to the feed
|
|
|
|
* @param string $title Title of the feed
|
|
|
|
* @param string $description Description of the field
|
|
|
|
* @param string $titleField Name of the field that should be used for the
|
|
|
|
* titles for the feed entries
|
|
|
|
* @param string $descriptionField Name of the field that should be used
|
|
|
|
* for the description for the feed
|
|
|
|
* entries
|
|
|
|
* @param string $authorField Name of the field that should be used for
|
|
|
|
* the author for the feed entries
|
|
|
|
* @param int $lastModified Unix timestamp of the latest modification
|
|
|
|
* (latest posting)
|
|
|
|
* @param string $etag The ETag is an unique identifier that is changed
|
|
|
|
* every time the representation does
|
|
|
|
*/
|
|
|
|
function __construct(DataObjectSet $entries, $link, $title,
|
|
|
|
$description = null, $titleField = "Title",
|
|
|
|
$descriptionField = "Content", $authorField = null,
|
|
|
|
$lastModified = null, $etag = null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->entries = $entries;
|
|
|
|
$this->link = $link;
|
|
|
|
$this->description = $description;
|
|
|
|
$this->title = $title;
|
2007-09-16 17:56:15 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->titleField = $titleField;
|
|
|
|
$this->descriptionField = $descriptionField;
|
|
|
|
$this->authorField = $authorField;
|
2007-09-16 17:56:15 +02:00
|
|
|
|
|
|
|
$this->lastModified = $lastModified;
|
|
|
|
$this->etag = $etag;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 17:56:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Include an link to the feed
|
|
|
|
*
|
|
|
|
* @param string $url URL of the feed
|
|
|
|
* @param string $title Title to show
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static function linkToFeed($url, $title = null) {
|
|
|
|
$title = Convert::raw2xml($title);
|
2007-09-16 17:56:15 +02:00
|
|
|
Requirements::insertHeadTags(
|
|
|
|
'<link rel="alternate" type="application/rss+xml" title="' . $title .
|
|
|
|
'" href="' . $url . '" />');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 17:56:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the RSS feed entries
|
|
|
|
*
|
|
|
|
* @return DataObjectSet Returns the {@link RSSFeed_Entry} objects.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function Entries() {
|
|
|
|
$output = new DataObjectSet();
|
2007-10-26 07:28:05 +02:00
|
|
|
if(isset($this->entries)) {
|
|
|
|
foreach($this->entries as $entry) {
|
|
|
|
$output->push(new RSSFeed_Entry($entry, $this->titleField, $this->descriptionField, $this->authorField));
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
2007-09-16 17:56:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the title of thisfeed
|
|
|
|
*
|
|
|
|
* @return string Returns the title of the feed.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function Title() {
|
2007-09-16 18:00:07 +02:00
|
|
|
return $this->title;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 17:56:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the URL of this feed
|
|
|
|
*
|
|
|
|
* @return string Returns the URL of the feed.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function Link() {
|
|
|
|
return Director::absoluteURL($this->link);
|
|
|
|
}
|
2007-09-16 17:56:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the description of this feed
|
|
|
|
*
|
|
|
|
* @return string Returns the description of the feed.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function Description() {
|
2007-09-16 18:00:07 +02:00
|
|
|
return $this->description;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 17:56:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Output the feed to the browser
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function outputToBrowser() {
|
2007-09-16 17:56:15 +02:00
|
|
|
if(is_int($this->lastModified)) {
|
|
|
|
HTTP::register_modification_timestamp($this->lastModified);
|
|
|
|
header('Last-Modified: ' . gmdate("D, d M Y H:i:s", $this->lastModified) . ' GMT');
|
|
|
|
}
|
|
|
|
if(!empty($this->etag)) {
|
|
|
|
HTTP::register_etag($this->etag);
|
|
|
|
}
|
|
|
|
|
2008-03-21 06:58:50 +01:00
|
|
|
$body = $this->feedContent();
|
2007-09-16 17:56:15 +02:00
|
|
|
HTTP::add_cache_headers($body);
|
2007-07-19 12:40:28 +02:00
|
|
|
header("Content-type: text/xml");
|
2007-09-16 17:56:15 +02:00
|
|
|
echo $body;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-03-21 06:58:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the content of the RSS feed
|
|
|
|
*/
|
|
|
|
function feedContent() {
|
|
|
|
return str_replace(' ', ' ', $this->renderWith('RSSFeed'));
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2007-09-16 17:56:15 +02:00
|
|
|
/**
|
|
|
|
* RSSFeed_Entry class
|
|
|
|
*
|
|
|
|
* This class is used for entries of an RSS feed.
|
|
|
|
*
|
|
|
|
* @see RSSFeed
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @subpackage integration
|
2007-09-16 17:56:15 +02:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class RSSFeed_Entry extends ViewableData {
|
2007-09-16 17:56:15 +02:00
|
|
|
/**
|
|
|
|
* The object that represents the item, it contains all the data.
|
|
|
|
*
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
protected $failover;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of the title field of feed entries
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $titleField;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of the description field of feed entries
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $descriptionField;
|
|
|
|
|
|
|
|
/**
|
2008-02-25 03:10:37 +01:00
|
|
|
* Name of the author field of feed entries
|
2007-09-16 17:56:15 +02:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $authorField;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Create a new RSSFeed entry.
|
|
|
|
*/
|
2007-09-16 17:56:15 +02:00
|
|
|
function __construct($entry, $titleField, $descriptionField,
|
|
|
|
$authorField) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->failover = $entry;
|
|
|
|
$this->titleField = $titleField;
|
|
|
|
$this->descriptionField = $descriptionField;
|
|
|
|
$this->authorField = $authorField;
|
|
|
|
}
|
2007-09-16 17:56:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the description of this entry
|
|
|
|
*
|
|
|
|
* @return string Returns the description of the entry.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function Title() {
|
2008-03-21 06:58:50 +01:00
|
|
|
return $this->rssField($this->titleField, 'Varchar');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 17:56:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the description of this entry
|
|
|
|
*
|
|
|
|
* @return string Returns the description of the entry.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function Description() {
|
2008-03-21 06:58:50 +01:00
|
|
|
return $this->rssField($this->descriptionField, 'Text');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 17:56:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the author of this entry
|
|
|
|
*
|
|
|
|
* @return string Returns the author of the entry.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function Author() {
|
|
|
|
if($this->authorField)
|
|
|
|
return $this->failover->obj($this->authorField);
|
|
|
|
}
|
2007-09-16 17:56:15 +02:00
|
|
|
|
2008-03-21 06:58:50 +01:00
|
|
|
/**
|
|
|
|
* Return the named field as an obj() call from $this->failover.
|
|
|
|
* Default to the given class if there's no casting information.
|
|
|
|
*/
|
|
|
|
function rssField($fieldName, $defaultClass = 'Varchar') {
|
|
|
|
if($fieldName) {
|
|
|
|
if($this->failover->castingHelperPair($fieldName)) {
|
|
|
|
return $this->failover->obj($fieldName);
|
|
|
|
} else {
|
|
|
|
$obj = new $defaultClass($fieldName);
|
|
|
|
$obj->setValue($this->failover->XML_val($fieldName));
|
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-16 17:56:15 +02:00
|
|
|
/**
|
|
|
|
* Get a link to this entry
|
|
|
|
*
|
|
|
|
* @return string Returns the URL of this entry
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function AbsoluteLink() {
|
2008-03-17 09:24:08 +01:00
|
|
|
if($this->failover->hasMethod('AbsoluteLink')) return $this->failover->AbsoluteLink();
|
2008-03-21 06:58:50 +01:00
|
|
|
else if($this->failover->hasMethod('Link')) return Director::absoluteURL($this->failover->Link());
|
2008-03-17 09:24:08 +01:00
|
|
|
else user_error($this->failover->class . " object has either an AbsoluteLink nor a Link method. Can't put a link in the RSS feed", E_USER_WARNING);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2008-04-26 08:47:56 +02:00
|
|
|
?>
|