2013-06-29 03:50:15 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PicoFeed\Writers;
|
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
use DomDocument;
|
|
|
|
use DomElement;
|
|
|
|
use DomAttr;
|
|
|
|
use PicoFeed\Writer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Atom writer class
|
|
|
|
*
|
|
|
|
* @author Frederic Guillot
|
|
|
|
* @package picofeed
|
|
|
|
*/
|
|
|
|
class Atom extends Writer
|
2013-06-29 03:50:15 +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(
|
2013-06-29 03:50:15 +02:00
|
|
|
'title',
|
|
|
|
'site_url',
|
2013-06-29 19:41:36 +02:00
|
|
|
'feed_url',
|
|
|
|
);
|
|
|
|
|
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',
|
2013-06-29 03:50:15 +02:00
|
|
|
);
|
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
/**
|
|
|
|
* Get the Atom document
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $filename Optional filename
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-06-29 03:50:15 +02:00
|
|
|
public function execute($filename = '')
|
|
|
|
{
|
2013-06-29 19:41:36 +02:00
|
|
|
$this->checkRequiredProperties($this->required_feed_properties, $this);
|
2013-06-29 03:50:15 +02:00
|
|
|
|
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;
|
2013-06-29 03:50:15 +02:00
|
|
|
|
|
|
|
// <feed/>
|
2013-06-29 19:41:36 +02:00
|
|
|
$feed = $this->dom->createElement('feed');
|
2014-05-20 20:20:27 +02:00
|
|
|
$feed->setAttributeNodeNS(new DomAttr('xmlns', 'http://www.w3.org/2005/Atom'));
|
2013-06-29 03:50:15 +02:00
|
|
|
|
|
|
|
// <generator/>
|
2013-06-29 19:41:36 +02:00
|
|
|
$generator = $this->dom->createElement('generator', 'PicoFeed');
|
2013-07-01 16:03:43 +02:00
|
|
|
$generator->setAttribute('uri', 'https://github.com/fguillot/picoFeed');
|
2013-06-29 03:50:15 +02:00
|
|
|
$feed->appendChild($generator);
|
|
|
|
|
|
|
|
// <title/>
|
2013-08-16 02:22:56 +02:00
|
|
|
$title = $this->dom->createElement('title');
|
|
|
|
$title->appendChild($this->dom->createTextNode($this->title));
|
|
|
|
$feed->appendChild($title);
|
2013-06-29 19:41:36 +02:00
|
|
|
|
|
|
|
// <id/>
|
2013-08-16 02:22:56 +02:00
|
|
|
$id = $this->dom->createElement('id');
|
|
|
|
$id->appendChild($this->dom->createTextNode($this->site_url));
|
|
|
|
$feed->appendChild($id);
|
2013-06-29 03:50:15 +02:00
|
|
|
|
|
|
|
// <updated/>
|
2013-06-29 19:41:36 +02:00
|
|
|
$this->addUpdated($feed, isset($this->updated) ? $this->updated : '');
|
2013-06-29 03:50:15 +02:00
|
|
|
|
|
|
|
// <link rel="alternate" type="text/html" href="http://example.org/"/>
|
2013-06-29 19:41:36 +02:00
|
|
|
$this->addLink($feed, $this->site_url);
|
2013-06-29 03:50:15 +02:00
|
|
|
|
|
|
|
// <link rel="self" type="application/atom+xml" href="http://example.org/feed.atom"/>
|
2013-06-29 19:41:36 +02:00
|
|
|
$this->addLink($feed, $this->feed_url, 'self', 'application/atom+xml');
|
2013-06-29 03:50:15 +02:00
|
|
|
|
|
|
|
// <author/>
|
2013-06-29 19:41:36 +02:00
|
|
|
if (isset($this->author)) $this->addAuthor($feed, $this->author);
|
2013-06-29 03:50:15 +02:00
|
|
|
|
|
|
|
// <entry/>
|
|
|
|
foreach ($this->items as $item) {
|
|
|
|
|
2013-06-29 19:41:36 +02:00
|
|
|
$this->checkRequiredProperties($this->required_item_properties, $item);
|
|
|
|
|
|
|
|
$entry = $this->dom->createElement('entry');
|
2013-06-29 03:50:15 +02:00
|
|
|
|
|
|
|
// <title/>
|
2013-08-16 02:22:56 +02:00
|
|
|
$title = $this->dom->createElement('title');
|
|
|
|
$title->appendChild($this->dom->createTextNode($item['title']));
|
|
|
|
$entry->appendChild($title);
|
2013-06-29 19:41:36 +02:00
|
|
|
|
|
|
|
// <id/>
|
2013-08-16 02:22:56 +02:00
|
|
|
$id = $this->dom->createElement('id');
|
|
|
|
$id->appendChild($this->dom->createTextNode(isset($item['id']) ? $item['id'] : $item['url']));
|
|
|
|
$entry->appendChild($id);
|
2013-06-29 03:50:15 +02:00
|
|
|
|
|
|
|
// <updated/>
|
2013-06-29 19:41:36 +02:00
|
|
|
$this->addUpdated($entry, isset($item['updated']) ? $item['updated'] : '');
|
2013-06-29 03:50:15 +02:00
|
|
|
|
|
|
|
// <published/>
|
|
|
|
if (isset($item['published'])) {
|
2013-06-29 19:41:36 +02:00
|
|
|
$entry->appendChild($this->dom->createElement('published', date(DATE_ATOM, $item['published'])));
|
2013-06-29 03:50:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// <link rel="alternate" type="text/html" href="http://example.org/"/>
|
2013-06-29 19:41:36 +02:00
|
|
|
$this->addLink($entry, $item['url']);
|
2013-06-29 03:50:15 +02:00
|
|
|
|
|
|
|
// <summary/>
|
|
|
|
if (isset($item['summary'])) {
|
2013-08-16 02:22:56 +02:00
|
|
|
$summary = $this->dom->createElement('summary');
|
|
|
|
$summary->appendChild($this->dom->createTextNode($item['summary']));
|
|
|
|
$entry->appendChild($summary);
|
2013-06-29 03:50:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// <content/>
|
|
|
|
if (isset($item['content'])) {
|
2013-06-29 19:41:36 +02:00
|
|
|
$content = $this->dom->createElement('content');
|
2013-06-29 03:50:15 +02:00
|
|
|
$content->setAttribute('type', 'html');
|
2013-06-29 19:41:36 +02:00
|
|
|
$content->appendChild($this->dom->createCDATASection($item['content']));
|
2013-06-29 03:50:15 +02:00
|
|
|
$entry->appendChild($content);
|
|
|
|
}
|
|
|
|
|
|
|
|
// <author/>
|
2013-06-29 19:41:36 +02:00
|
|
|
if (isset($item['author'])) $this->addAuthor($entry, $item['author']);
|
2013-06-29 03:50:15 +02:00
|
|
|
|
|
|
|
$feed->appendChild($entry);
|
|
|
|
}
|
|
|
|
|
2013-06-29 19:41:36 +02:00
|
|
|
$this->dom->appendChild($feed);
|
2013-06-29 03:50:15 +02:00
|
|
|
|
|
|
|
if ($filename) {
|
2013-06-29 19:41:36 +02:00
|
|
|
$this->dom->save($filename);
|
2013-06-29 03:50:15 +02:00
|
|
|
}
|
|
|
|
else {
|
2013-06-29 19:41:36 +02:00
|
|
|
return $this->dom->saveXML();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
/**
|
|
|
|
* Add Link
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param DomElement $xml XML node
|
|
|
|
* @param string $url URL
|
|
|
|
* @param string $rel Link rel attribute
|
|
|
|
* @param string $type Link type attribute
|
|
|
|
*/
|
|
|
|
public function addLink(DomElement $xml, $url, $rel = 'alternate', $type = 'text/html')
|
2013-06-29 19:41:36 +02:00
|
|
|
{
|
|
|
|
$link = $this->dom->createElement('link');
|
|
|
|
$link->setAttribute('rel', $rel);
|
|
|
|
$link->setAttribute('type', $type);
|
|
|
|
$link->setAttribute('href', $url);
|
|
|
|
$xml->appendChild($link);
|
|
|
|
}
|
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
/**
|
|
|
|
* Add publication date
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param DomElement $xml XML node
|
|
|
|
* @param string $value Timestamp
|
|
|
|
*/
|
|
|
|
public function addUpdated(DomElement $xml, $value = '')
|
2013-06-29 19:41:36 +02:00
|
|
|
{
|
|
|
|
$xml->appendChild($this->dom->createElement(
|
|
|
|
'updated',
|
|
|
|
date(DATE_ATOM, $value ?: time())
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
/**
|
|
|
|
* Add author
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param DomElement $xml XML node
|
|
|
|
* @param array $values Author name and email
|
|
|
|
*/
|
|
|
|
public function addAuthor(DomElement $xml, array $values)
|
2013-06-29 19:41:36 +02:00
|
|
|
{
|
|
|
|
$author = $this->dom->createElement('author');
|
|
|
|
|
|
|
|
if (isset($values['name'])) {
|
2013-08-16 02:22:56 +02:00
|
|
|
$name = $this->dom->createElement('name');
|
|
|
|
$name->appendChild($this->dom->createTextNode($values['name']));
|
2013-06-29 19:41:36 +02:00
|
|
|
$author->appendChild($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($values['email'])) {
|
2013-08-16 02:22:56 +02:00
|
|
|
$email = $this->dom->createElement('email');
|
|
|
|
$email->appendChild($this->dom->createTextNode($values['email']));
|
2013-06-29 19:41:36 +02:00
|
|
|
$author->appendChild($email);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($values['url'])) {
|
2013-08-16 02:22:56 +02:00
|
|
|
$uri = $this->dom->createElement('uri');
|
|
|
|
$uri->appendChild($this->dom->createTextNode($values['url']));
|
2013-06-29 19:41:36 +02:00
|
|
|
$author->appendChild($uri);
|
2013-06-29 03:50:15 +02:00
|
|
|
}
|
2013-06-29 19:41:36 +02:00
|
|
|
|
|
|
|
$xml->appendChild($author);
|
2013-06-29 03:50:15 +02:00
|
|
|
}
|
2014-05-20 20:20:27 +02:00
|
|
|
}
|