2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
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
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage integration
|
2007-09-16 17:56:15 +02:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class RSSFeed extends ViewableData {
|
2012-07-01 11:25:57 +02:00
|
|
|
|
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
|
|
|
|
*
|
2011-10-26 08:09:04 +02:00
|
|
|
* @var SS_List
|
2007-09-16 17:56:15 +02:00
|
|
|
*/
|
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;
|
|
|
|
|
2012-07-01 11:25:57 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $template = 'RSSFeed';
|
|
|
|
|
2007-09-16 17:56:15 +02:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2011-10-26 08:09:04 +02:00
|
|
|
* @param SS_List $entries RSS feed entries
|
2007-09-16 17:56:15 +02:00
|
|
|
* @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
|
|
|
|
*/
|
2011-05-02 09:14:05 +02:00
|
|
|
function __construct(SS_List $entries, $link, $title,
|
2007-09-16 17:56:15 +02:00
|
|
|
$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;
|
2009-09-18 05:02:19 +02:00
|
|
|
|
|
|
|
parent::__construct();
|
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
|
|
|
|
*
|
2011-10-26 08:09:04 +02:00
|
|
|
* @return SS_List Returns the {@link RSSFeed_Entry} objects.
|
2007-09-16 17:56:15 +02:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function Entries() {
|
2011-05-05 12:40:24 +02:00
|
|
|
$output = new ArrayList();
|
2012-07-31 10:38:12 +02:00
|
|
|
|
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
|
|
|
|
*
|
2009-10-11 02:07:16 +02:00
|
|
|
* @param string $action
|
2007-09-16 17:56:15 +02:00
|
|
|
* @return string Returns the URL of the feed.
|
|
|
|
*/
|
2009-10-11 02:07:16 +02:00
|
|
|
function Link($action = null) {
|
|
|
|
return Controller::join_links(Director::absoluteURL($this->link), $action);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
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
|
|
|
|
*/
|
2012-07-31 10:38:12 +02:00
|
|
|
public function outputToBrowser() {
|
|
|
|
$prevState = SSViewer::get_source_file_comments();
|
|
|
|
SSViewer::set_source_file_comments(false);
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-07-31 10:38:12 +02:00
|
|
|
if(!headers_sent()) {
|
|
|
|
HTTP::add_cache_headers();
|
|
|
|
header("Content-type: text/xml");
|
|
|
|
}
|
|
|
|
|
2010-10-19 07:06:53 +02:00
|
|
|
SSViewer::set_source_file_comments($prevState);
|
2012-07-31 10:38:12 +02:00
|
|
|
|
|
|
|
return $this->renderWith($this->getTemplate());
|
2008-03-21 06:58:50 +01:00
|
|
|
}
|
2012-07-01 11:25:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the name of the template to use. Actual template will be resolved
|
|
|
|
* via the standard template inclusion process.
|
|
|
|
*
|
|
|
|
* @param string
|
|
|
|
*/
|
|
|
|
public function setTemplate($template) {
|
|
|
|
$this->template = $template;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the name of the template to use.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTemplate() {
|
|
|
|
return $this->template;
|
|
|
|
}
|
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
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @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;
|
2009-09-18 05:02:19 +02:00
|
|
|
|
|
|
|
parent::__construct();
|
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 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() {
|
2010-05-25 05:49:55 +02:00
|
|
|
if($this->authorField) return $this->failover->obj($this->authorField);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
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) {
|
2010-10-04 06:46:41 +02:00
|
|
|
if($this->failover->castingHelper($fieldName)) {
|
2010-05-25 05:49:55 +02:00
|
|
|
$value = $this->failover->$fieldName;
|
|
|
|
$obj = $this->failover->obj($fieldName);
|
|
|
|
$obj->setValue($value);
|
|
|
|
return $obj;
|
2008-03-21 06:58:50 +01:00
|
|
|
} 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());
|
2012-07-19 23:08:40 +02:00
|
|
|
else user_error($this->failover->class . " object has neither 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
|
|
|
}
|
|
|
|
}
|