2013-06-29 19:41:36 +02:00
|
|
|
<?php
|
|
|
|
|
2014-12-24 03:28:26 +01:00
|
|
|
namespace PicoFeed\Syndication;
|
2013-06-29 19:41:36 +02:00
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
use DomDocument;
|
|
|
|
use DomAttr;
|
|
|
|
use DomElement;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rss 2.0 writer class
|
|
|
|
*
|
|
|
|
* @author Frederic Guillot
|
2014-12-24 03:28:26 +01:00
|
|
|
* @package Syndication
|
2014-05-20 20:20:27 +02:00
|
|
|
*/
|
|
|
|
class Rss20 extends Writer
|
2013-06-29 19:41:36 +02:00
|
|
|
{
|
2014-05-20 20:20:27 +02:00
|
|
|
/**
|
|
|
|
* List of required properties for each feed
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-06-29 19:41:36 +02:00
|
|
|
private $required_feed_properties = array(
|
|
|
|
'title',
|
|
|
|
'site_url',
|
2013-07-01 16:03:43 +02:00
|
|
|
'feed_url',
|
2013-06-29 19:41:36 +02:00
|
|
|
);
|
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
/**
|
|
|
|
* List of required properties for each item
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-06-29 19:41:36 +02:00
|
|
|
private $required_item_properties = array(
|
|
|
|
'title',
|
|
|
|
'url',
|
|
|
|
);
|
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
/**
|
|
|
|
* Get the Rss 2.0 document
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $filename Optional filename
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-06-29 19:41:36 +02:00
|
|
|
public function execute($filename = '')
|
|
|
|
{
|
|
|
|
$this->checkRequiredProperties($this->required_feed_properties, $this);
|
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
$this->dom = new DomDocument('1.0', 'UTF-8');
|
2013-06-29 19:41:36 +02:00
|
|
|
$this->dom->formatOutput = true;
|
|
|
|
|
|
|
|
// <rss/>
|
|
|
|
$rss = $this->dom->createElement('rss');
|
|
|
|
$rss->setAttribute('version', '2.0');
|
2014-05-20 20:20:27 +02:00
|
|
|
$rss->setAttributeNodeNS(new DomAttr('xmlns:content', 'http://purl.org/rss/1.0/modules/content/'));
|
|
|
|
$rss->setAttributeNodeNS(new DomAttr('xmlns:atom', 'http://www.w3.org/2005/Atom'));
|
2013-06-29 19:41:36 +02:00
|
|
|
|
|
|
|
$channel = $this->dom->createElement('channel');
|
|
|
|
|
|
|
|
// <generator/>
|
|
|
|
$generator = $this->dom->createElement('generator', 'PicoFeed (https://github.com/fguillot/picoFeed)');
|
|
|
|
$channel->appendChild($generator);
|
|
|
|
|
|
|
|
// <title/>
|
2013-08-16 02:22:56 +02:00
|
|
|
$title = $this->dom->createElement('title');
|
|
|
|
$title->appendChild($this->dom->createTextNode($this->title));
|
|
|
|
$channel->appendChild($title);
|
2013-06-29 19:41:36 +02:00
|
|
|
|
|
|
|
// <description/>
|
2013-08-16 02:22:56 +02:00
|
|
|
$description = $this->dom->createElement('description');
|
2014-12-24 03:28:26 +01:00
|
|
|
$description->appendChild($this->dom->createTextNode($this->description ?: $this->title));
|
2013-08-16 02:22:56 +02:00
|
|
|
$channel->appendChild($description);
|
2013-06-29 19:41:36 +02:00
|
|
|
|
|
|
|
// <pubDate/>
|
2014-10-19 20:42:31 +02:00
|
|
|
$this->addPubDate($channel, $this->updated);
|
2013-06-29 19:41:36 +02:00
|
|
|
|
2013-07-01 16:03:43 +02:00
|
|
|
// <atom:link/>
|
|
|
|
$link = $this->dom->createElement('atom:link');
|
|
|
|
$link->setAttribute('href', $this->feed_url);
|
|
|
|
$link->setAttribute('rel', 'self');
|
|
|
|
$link->setAttribute('type', 'application/rss+xml');
|
|
|
|
$channel->appendChild($link);
|
|
|
|
|
2013-06-29 19:41:36 +02:00
|
|
|
// <link/>
|
2013-08-16 02:22:56 +02:00
|
|
|
$link = $this->dom->createElement('link');
|
|
|
|
$link->appendChild($this->dom->createTextNode($this->site_url));
|
|
|
|
$channel->appendChild($link);
|
2013-06-29 19:41:36 +02:00
|
|
|
|
|
|
|
// <webMaster/>
|
|
|
|
if (isset($this->author)) $this->addAuthor($channel, 'webMaster', $this->author);
|
|
|
|
|
|
|
|
// <item/>
|
|
|
|
foreach ($this->items as $item) {
|
|
|
|
$this->checkRequiredProperties($this->required_item_properties, $item);
|
2014-10-19 20:42:31 +02:00
|
|
|
$channel->appendChild($this->createEntry($item));
|
2013-06-29 19:41:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$rss->appendChild($channel);
|
|
|
|
$this->dom->appendChild($rss);
|
|
|
|
|
|
|
|
if ($filename) {
|
|
|
|
$this->dom->save($filename);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $this->dom->saveXML();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-19 20:42:31 +02:00
|
|
|
/**
|
|
|
|
* Create item entry
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param arrray $item Item properties
|
|
|
|
* @return DomElement
|
|
|
|
*/
|
|
|
|
public function createEntry(array $item)
|
|
|
|
{
|
|
|
|
$entry = $this->dom->createElement('item');
|
|
|
|
|
|
|
|
// <title/>
|
|
|
|
$title = $this->dom->createElement('title');
|
|
|
|
$title->appendChild($this->dom->createTextNode($item['title']));
|
|
|
|
$entry->appendChild($title);
|
|
|
|
|
|
|
|
// <link/>
|
|
|
|
$link = $this->dom->createElement('link');
|
|
|
|
$link->appendChild($this->dom->createTextNode($item['url']));
|
|
|
|
$entry->appendChild($link);
|
|
|
|
|
|
|
|
// <guid/>
|
|
|
|
if (isset($item['id'])) {
|
|
|
|
$guid = $this->dom->createElement('guid');
|
|
|
|
$guid->setAttribute('isPermaLink', 'false');
|
|
|
|
$guid->appendChild($this->dom->createTextNode($item['id']));
|
|
|
|
$entry->appendChild($guid);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$guid = $this->dom->createElement('guid');
|
|
|
|
$guid->setAttribute('isPermaLink', 'true');
|
|
|
|
$guid->appendChild($this->dom->createTextNode($item['url']));
|
|
|
|
$entry->appendChild($guid);
|
|
|
|
}
|
|
|
|
|
|
|
|
// <pubDate/>
|
|
|
|
$this->addPubDate($entry, isset($item['updated']) ? $item['updated'] : '');
|
|
|
|
|
|
|
|
// <description/>
|
|
|
|
if (isset($item['summary'])) {
|
|
|
|
$description = $this->dom->createElement('description');
|
|
|
|
$description->appendChild($this->dom->createTextNode($item['summary']));
|
|
|
|
$entry->appendChild($description);
|
|
|
|
}
|
|
|
|
|
|
|
|
// <content/>
|
|
|
|
if (isset($item['content'])) {
|
|
|
|
$content = $this->dom->createElement('content:encoded');
|
|
|
|
$content->appendChild($this->dom->createCDATASection($item['content']));
|
|
|
|
$entry->appendChild($content);
|
|
|
|
}
|
|
|
|
|
|
|
|
// <author/>
|
|
|
|
if (isset($item['author'])) {
|
|
|
|
$this->addAuthor($entry, 'author', $item['author']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $entry;
|
|
|
|
}
|
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
/**
|
|
|
|
* Add publication date
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param DomElement $xml XML node
|
2014-10-19 20:42:31 +02:00
|
|
|
* @param integer $value Timestamp
|
2014-05-20 20:20:27 +02:00
|
|
|
*/
|
2014-10-19 20:42:31 +02:00
|
|
|
public function addPubDate(DomElement $xml, $value = 0)
|
2013-06-29 19:41:36 +02:00
|
|
|
{
|
|
|
|
$xml->appendChild($this->dom->createElement(
|
|
|
|
'pubDate',
|
2015-08-02 20:08:21 +02:00
|
|
|
date(DATE_RSS, $value ?: time())
|
2013-06-29 19:41:36 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
/**
|
|
|
|
* Add author
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param DomElement $xml XML node
|
|
|
|
* @param string $tag Tag name
|
|
|
|
* @param array $values Author name and email
|
|
|
|
*/
|
|
|
|
public function addAuthor(DomElement $xml, $tag, array $values)
|
2013-06-29 19:41:36 +02:00
|
|
|
{
|
|
|
|
$value = '';
|
|
|
|
|
|
|
|
if (isset($values['email'])) $value .= $values['email'];
|
|
|
|
if ($value && isset($values['name'])) $value .= ' ('.$values['name'].')';
|
|
|
|
|
|
|
|
if ($value) {
|
2013-08-16 02:22:56 +02:00
|
|
|
$author = $this->dom->createElement($tag);
|
|
|
|
$author->appendChild($this->dom->createTextNode($value));
|
2013-06-29 19:41:36 +02:00
|
|
|
$xml->appendChild($author);
|
|
|
|
}
|
|
|
|
}
|
2014-05-20 20:20:27 +02:00
|
|
|
}
|