Skip to content

Commit

Permalink
Support PubSubHubbub
Browse files Browse the repository at this point in the history
Signed-off-by: Hidehito Nozawa <[email protected]>
  • Loading branch information
suin committed Jul 21, 2016
1 parent 906cf5c commit 938b067
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 14 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ $channel
->pubDate(strtotime('Tue, 21 Aug 2012 19:50:37 +0900'))
->lastBuildDate(strtotime('Tue, 21 Aug 2012 19:50:37 +0900'))
->ttl(60)
->pubsubhubbub('http://example.com/feed.xml', 'http://pubsubhubbub.appspot.com') // This is optional. Specify PubSubHubbub discovery if you want.
->appendTo($feed);

// Blog item
Expand Down Expand Up @@ -58,7 +59,7 @@ Output:

```xml
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>Channel Title</title>
<link>http://blog.example.com</link>
Expand All @@ -68,11 +69,13 @@ Output:
<pubDate>Tue, 21 Aug 2012 10:50:37 +0000</pubDate>
<lastBuildDate>Tue, 21 Aug 2012 10:50:37 +0000</lastBuildDate>
<ttl>60</ttl>
<item xmlns:default="http://purl.org/rss/1.0/modules/content/">
<atom:link rel="self" href="http://example.com/feed.xml" type="application/rss+xml"/>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/>
<item>
<title><![CDATA[Blog Entry Title]]></title>
<link>http://blog.example.com/2012/08/21/blog-entry/</link>
<description><![CDATA[<div>Blog body</div>]]></description>
<content:encoded xmlns="http://purl.org/rss/1.0/modules/content/"><![CDATA[<div>Blog body</div>]]></content:encoded>
<content:encoded><![CDATA[<div>Blog body</div>]]></content:encoded>
<guid>http://blog.example.com/2012/08/21/blog-entry/</guid>
<pubDate>Tue, 21 Aug 2012 10:50:37 +0000</pubDate>
<author>Hidehito Nozawa</author>
Expand Down
1 change: 1 addition & 0 deletions examples/simple-feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
->pubDate(strtotime('Tue, 21 Aug 2012 19:50:37 +0900'))
->lastBuildDate(strtotime('Tue, 21 Aug 2012 19:50:37 +0900'))
->ttl(60)
->pubsubhubbub('http://example.com/feed.xml', 'http://pubsubhubbub.appspot.com') // This is optional. Specify PubSubHubbub discovery if you want.
->appendTo($feed);

// Blog item
Expand Down
29 changes: 29 additions & 0 deletions src/Suin/RSSWriter/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class Channel implements ChannelInterface
/** @var int */
protected $ttl;

/** @var string[] */
protected $pubsubhubbub;

/** @var ItemInterface[] */
protected $items = [];

Expand Down Expand Up @@ -129,6 +132,21 @@ public function ttl($ttl)
return $this;
}

/**
* Enable PubSubHubbub discovery
* @param string $feedUrl
* @param string $hubUrl
* @return $this
*/
public function pubsubhubbub($feedUrl, $hubUrl)
{
$this->pubsubhubbub = [
'feedUrl' => $feedUrl,
'hubUrl' => $hubUrl,
];
return $this;
}

/**
* Add item object
* @param ItemInterface $item
Expand Down Expand Up @@ -182,6 +200,17 @@ public function asXML()
$xml->addChild('ttl', $this->ttl);
}

if ($this->pubsubhubbub !== null) {
$feedUrl = $xml->addChild('xmlns:atom:link');
$feedUrl->addAttribute('rel', 'self');
$feedUrl->addAttribute('href', $this->pubsubhubbub['feedUrl']);
$feedUrl->addAttribute('type', 'application/rss+xml');

$hubUrl = $xml->addChild('xmlns:atom:link');
$hubUrl->addAttribute('rel', 'hub');
$hubUrl->addAttribute('href', $this->pubsubhubbub['hubUrl']);
}

foreach ($this->items as $item) {
$toDom = dom_import_simplexml($xml);
$fromDom = dom_import_simplexml($item->asXML());
Expand Down
2 changes: 1 addition & 1 deletion src/Suin/RSSWriter/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function addChannel(ChannelInterface $channel)
*/
public function render()
{
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" />', LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL);
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" />', LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL);

foreach ($this->channels as $channel) {
$toDom = dom_import_simplexml($xml);
Expand Down
2 changes: 1 addition & 1 deletion src/Suin/RSSWriter/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function asXML()
}

if ($this->contentEncoded) {
$xml->addCdataChild('encoded', $this->contentEncoded, 'http://purl.org/rss/1.0/modules/content/');
$xml->addCdataChild('xmlns:content:encoded', $this->contentEncoded);
}

foreach ($this->categories as $category) {
Expand Down
19 changes: 16 additions & 3 deletions src/Suin/RSSWriter/SimpleXMLElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
*/
class SimpleXMLElement extends \SimpleXMLElement
{
/**
* @param string $name
* @param string $value
* @param string $namespace
* @return \SimpleXMLElement
*/
public function addChild($name, $value = null, $namespace = null)
{
if ($value !== null and is_string($value) === true) {
Expand All @@ -17,11 +23,18 @@ public function addChild($name, $value = null, $namespace = null)
return parent::addChild($name, $value, $namespace);
}

/**
* @param string $name
* @param string $value
* @param string $namespace
* @return \SimpleXMLElement
*/
public function addCdataChild($name, $value = null, $namespace = null)
{
$element = $this->addChild($name, null, $namespace);
$element = dom_import_simplexml($element);
$elementOwner = $element->ownerDocument;
$element->appendChild($elementOwner->createCDATASection($value));
$dom = dom_import_simplexml($element);
$elementOwner = $dom->ownerDocument;
$dom->appendChild($elementOwner->createCDATASection($value));
return $element;
}
}
9 changes: 9 additions & 0 deletions tests/Suin/RSSWriter/ChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ public function testTtl()
$this->assertAttributeSame($ttl, 'ttl', $channel);
}

public function testPubsubhubbub()
{
$channel = new Channel();
$channel->pubsubhubbub('http://example.com/feed.xml', 'http://pubsubhubbub.appspot.com');
$xml = $channel->asXML()->asXML();
$this->assertContains('<atom:link rel="self" href="http://example.com/feed.xml" type="application/rss+xml"/>', $xml);
$this->assertContains('<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/>', $xml);
}

public function testAddItem()
{
$item = $this->getMock($this->itemInterface);
Expand Down
6 changes: 3 additions & 3 deletions tests/Suin/RSSWriter/FeedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testRender()
$channel3->expects($this->once())->method('asXML')->will($this->returnValue($xml3));
$this->reveal($feed)->attr('channels', [$channel1, $channel2, $channel3]);
$expect = '<?xml version="1.0" encoding="UTF-8" ?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel><title>channel1</title></channel>
<channel><title>channel2</title></channel>
<channel><title>channel3</title></channel>
Expand All @@ -54,7 +54,7 @@ public function testRender_with_japanese()
$this->reveal($feed)->attr('channels', [$channel1, $channel2, $channel3]);
$expect = <<< 'XML'
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>日本語1</title>
</channel>
Expand Down Expand Up @@ -85,7 +85,7 @@ public function test__toString()
$channel3->expects($this->once())->method('asXML')->will($this->returnValue($xml3));
$this->reveal($feed)->attr('channels', [$channel1, $channel2, $channel3]);
$expect = '<?xml version="1.0" encoding="UTF-8" ?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel><title>channel1</title></channel>
<channel><title>channel2</title></channel>
<channel><title>channel3</title></channel>
Expand Down
6 changes: 3 additions & 3 deletions tests/Suin/RSSWriter/ItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ public function testContentEncoded()
$channel->appendTo($feed);

$expected = '<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title/>
<link/>
<description/>
<item xmlns:default="http://purl.org/rss/1.0/modules/content/">
<item>
<title/>
<link/>
<description/>
<content:encoded xmlns="http://purl.org/rss/1.0/modules/content/"><![CDATA[<div>contents</div>]]></content:encoded>
<content:encoded><![CDATA[<div>contents</div>]]></content:encoded>
</item>
</channel>
</rss>';
Expand Down

0 comments on commit 938b067

Please sign in to comment.