Skip to content

Commit 938b067

Browse files
committed
Support PubSubHubbub
Signed-off-by: Hidehito Nozawa <[email protected]>
1 parent 906cf5c commit 938b067

File tree

9 files changed

+69
-14
lines changed

9 files changed

+69
-14
lines changed

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ $channel
2727
->pubDate(strtotime('Tue, 21 Aug 2012 19:50:37 +0900'))
2828
->lastBuildDate(strtotime('Tue, 21 Aug 2012 19:50:37 +0900'))
2929
->ttl(60)
30+
->pubsubhubbub('http://example.com/feed.xml', 'http://pubsubhubbub.appspot.com') // This is optional. Specify PubSubHubbub discovery if you want.
3031
->appendTo($feed);
3132

3233
// Blog item
@@ -58,7 +59,7 @@ Output:
5859

5960
```xml
6061
<?xml version="1.0" encoding="UTF-8"?>
61-
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
62+
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
6263
<channel>
6364
<title>Channel Title</title>
6465
<link>http://blog.example.com</link>
@@ -68,11 +69,13 @@ Output:
6869
<pubDate>Tue, 21 Aug 2012 10:50:37 +0000</pubDate>
6970
<lastBuildDate>Tue, 21 Aug 2012 10:50:37 +0000</lastBuildDate>
7071
<ttl>60</ttl>
71-
<item xmlns:default="http://purl.org/rss/1.0/modules/content/">
72+
<atom:link rel="self" href="http://example.com/feed.xml" type="application/rss+xml"/>
73+
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/>
74+
<item>
7275
<title><![CDATA[Blog Entry Title]]></title>
7376
<link>http://blog.example.com/2012/08/21/blog-entry/</link>
7477
<description><![CDATA[<div>Blog body</div>]]></description>
75-
<content:encoded xmlns="http://purl.org/rss/1.0/modules/content/"><![CDATA[<div>Blog body</div>]]></content:encoded>
78+
<content:encoded><![CDATA[<div>Blog body</div>]]></content:encoded>
7679
<guid>http://blog.example.com/2012/08/21/blog-entry/</guid>
7780
<pubDate>Tue, 21 Aug 2012 10:50:37 +0000</pubDate>
7881
<author>Hidehito Nozawa</author>

examples/simple-feed.php

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
->pubDate(strtotime('Tue, 21 Aug 2012 19:50:37 +0900'))
2323
->lastBuildDate(strtotime('Tue, 21 Aug 2012 19:50:37 +0900'))
2424
->ttl(60)
25+
->pubsubhubbub('http://example.com/feed.xml', 'http://pubsubhubbub.appspot.com') // This is optional. Specify PubSubHubbub discovery if you want.
2526
->appendTo($feed);
2627

2728
// Blog item

src/Suin/RSSWriter/Channel.php

+29
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class Channel implements ChannelInterface
3232
/** @var int */
3333
protected $ttl;
3434

35+
/** @var string[] */
36+
protected $pubsubhubbub;
37+
3538
/** @var ItemInterface[] */
3639
protected $items = [];
3740

@@ -129,6 +132,21 @@ public function ttl($ttl)
129132
return $this;
130133
}
131134

135+
/**
136+
* Enable PubSubHubbub discovery
137+
* @param string $feedUrl
138+
* @param string $hubUrl
139+
* @return $this
140+
*/
141+
public function pubsubhubbub($feedUrl, $hubUrl)
142+
{
143+
$this->pubsubhubbub = [
144+
'feedUrl' => $feedUrl,
145+
'hubUrl' => $hubUrl,
146+
];
147+
return $this;
148+
}
149+
132150
/**
133151
* Add item object
134152
* @param ItemInterface $item
@@ -182,6 +200,17 @@ public function asXML()
182200
$xml->addChild('ttl', $this->ttl);
183201
}
184202

203+
if ($this->pubsubhubbub !== null) {
204+
$feedUrl = $xml->addChild('xmlns:atom:link');
205+
$feedUrl->addAttribute('rel', 'self');
206+
$feedUrl->addAttribute('href', $this->pubsubhubbub['feedUrl']);
207+
$feedUrl->addAttribute('type', 'application/rss+xml');
208+
209+
$hubUrl = $xml->addChild('xmlns:atom:link');
210+
$hubUrl->addAttribute('rel', 'hub');
211+
$hubUrl->addAttribute('href', $this->pubsubhubbub['hubUrl']);
212+
}
213+
185214
foreach ($this->items as $item) {
186215
$toDom = dom_import_simplexml($xml);
187216
$fromDom = dom_import_simplexml($item->asXML());

src/Suin/RSSWriter/Feed.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function addChannel(ChannelInterface $channel)
3030
*/
3131
public function render()
3232
{
33-
$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);
33+
$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);
3434

3535
foreach ($this->channels as $channel) {
3636
$toDom = dom_import_simplexml($xml);

src/Suin/RSSWriter/Item.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function asXML()
126126
}
127127

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

132132
foreach ($this->categories as $category) {

src/Suin/RSSWriter/SimpleXMLElement.php

+16-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
*/
99
class SimpleXMLElement extends \SimpleXMLElement
1010
{
11+
/**
12+
* @param string $name
13+
* @param string $value
14+
* @param string $namespace
15+
* @return \SimpleXMLElement
16+
*/
1117
public function addChild($name, $value = null, $namespace = null)
1218
{
1319
if ($value !== null and is_string($value) === true) {
@@ -17,11 +23,18 @@ public function addChild($name, $value = null, $namespace = null)
1723
return parent::addChild($name, $value, $namespace);
1824
}
1925

26+
/**
27+
* @param string $name
28+
* @param string $value
29+
* @param string $namespace
30+
* @return \SimpleXMLElement
31+
*/
2032
public function addCdataChild($name, $value = null, $namespace = null)
2133
{
2234
$element = $this->addChild($name, null, $namespace);
23-
$element = dom_import_simplexml($element);
24-
$elementOwner = $element->ownerDocument;
25-
$element->appendChild($elementOwner->createCDATASection($value));
35+
$dom = dom_import_simplexml($element);
36+
$elementOwner = $dom->ownerDocument;
37+
$dom->appendChild($elementOwner->createCDATASection($value));
38+
return $element;
2639
}
2740
}

tests/Suin/RSSWriter/ChannelTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ public function testTtl()
7171
$this->assertAttributeSame($ttl, 'ttl', $channel);
7272
}
7373

74+
public function testPubsubhubbub()
75+
{
76+
$channel = new Channel();
77+
$channel->pubsubhubbub('http://example.com/feed.xml', 'http://pubsubhubbub.appspot.com');
78+
$xml = $channel->asXML()->asXML();
79+
$this->assertContains('<atom:link rel="self" href="http://example.com/feed.xml" type="application/rss+xml"/>', $xml);
80+
$this->assertContains('<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/>', $xml);
81+
}
82+
7483
public function testAddItem()
7584
{
7685
$item = $this->getMock($this->itemInterface);

tests/Suin/RSSWriter/FeedTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function testRender()
3030
$channel3->expects($this->once())->method('asXML')->will($this->returnValue($xml3));
3131
$this->reveal($feed)->attr('channels', [$channel1, $channel2, $channel3]);
3232
$expect = '<?xml version="1.0" encoding="UTF-8" ?>
33-
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
33+
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
3434
<channel><title>channel1</title></channel>
3535
<channel><title>channel2</title></channel>
3636
<channel><title>channel3</title></channel>
@@ -54,7 +54,7 @@ public function testRender_with_japanese()
5454
$this->reveal($feed)->attr('channels', [$channel1, $channel2, $channel3]);
5555
$expect = <<< 'XML'
5656
<?xml version="1.0" encoding="UTF-8"?>
57-
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
57+
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
5858
<channel>
5959
<title>日本語1</title>
6060
</channel>
@@ -85,7 +85,7 @@ public function test__toString()
8585
$channel3->expects($this->once())->method('asXML')->will($this->returnValue($xml3));
8686
$this->reveal($feed)->attr('channels', [$channel1, $channel2, $channel3]);
8787
$expect = '<?xml version="1.0" encoding="UTF-8" ?>
88-
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
88+
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
8989
<channel><title>channel1</title></channel>
9090
<channel><title>channel2</title></channel>
9191
<channel><title>channel3</title></channel>

tests/Suin/RSSWriter/ItemTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@ public function testContentEncoded()
4444
$channel->appendTo($feed);
4545

4646
$expected = '<?xml version="1.0" encoding="UTF-8"?>
47-
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
47+
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
4848
<channel>
4949
<title/>
5050
<link/>
5151
<description/>
52-
<item xmlns:default="http://purl.org/rss/1.0/modules/content/">
52+
<item>
5353
<title/>
5454
<link/>
5555
<description/>
56-
<content:encoded xmlns="http://purl.org/rss/1.0/modules/content/"><![CDATA[<div>contents</div>]]></content:encoded>
56+
<content:encoded><![CDATA[<div>contents</div>]]></content:encoded>
5757
</item>
5858
</channel>
5959
</rss>';

0 commit comments

Comments
 (0)