From ab2e566ee191e70cb1cae0547473d9d1a7a9fdfb Mon Sep 17 00:00:00 2001 From: fulmeek <36341513+fulmeek@users.noreply.github.com> Date: Mon, 21 Jan 2019 17:22:30 +0100 Subject: [PATCH] [AtomFormat] Update to comply with RFC 4287 (#995) https://tools.ietf.org/html/rfc4287 --- formats/AtomFormat.php | 94 ++++++++++++++----- tests/AtomFormatTest.php | 89 ++++++++++++++++++ .../expectedAtomFormat/feed.common.xml | 77 +++++++++++++++ .../samples/expectedAtomFormat/feed.empty.xml | 15 +++ .../expectedAtomFormat/feed.emptyItems.xml | 30 ++++++ .../expectedAtomFormat/feed.microblog.xml | 30 ++++++ 6 files changed, 312 insertions(+), 23 deletions(-) create mode 100644 tests/AtomFormatTest.php create mode 100644 tests/samples/expectedAtomFormat/feed.common.xml create mode 100644 tests/samples/expectedAtomFormat/feed.empty.xml create mode 100644 tests/samples/expectedAtomFormat/feed.emptyItems.xml create mode 100644 tests/samples/expectedAtomFormat/feed.microblog.xml diff --git a/formats/AtomFormat.php b/formats/AtomFormat.php index bb5e30eb..1e01a0ff 100644 --- a/formats/AtomFormat.php +++ b/formats/AtomFormat.php @@ -1,21 +1,30 @@ xml_encode($_SERVER['REQUEST_URI']) : ''; + public function stringify(){ + $urlPrefix = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://'; + $urlHost = (isset($_SERVER['HTTP_HOST'])) ? $_SERVER['HTTP_HOST'] : ''; + $urlPath = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : ''; + $urlRequest = (isset($_SERVER['REQUEST_URI'])) ? $_SERVER['REQUEST_URI'] : ''; + + $feedUrl = $this->xml_encode($urlPrefix . $urlHost . $urlRequest); $extraInfos = $this->getExtraInfos(); $title = $this->xml_encode($extraInfos['name']); $uri = !empty($extraInfos['uri']) ? $extraInfos['uri'] : REPOSITORY; + // since we can't guarantee that all items have an author, + // a global feed author is mandatory + $feedAuthor = 'RSS-Bridge'; + $uriparts = parse_url($uri); if(!empty($extraInfos['icon'])) { $icon = $extraInfos['icon']; @@ -27,11 +36,35 @@ class AtomFormat extends FormatAbstract{ $entries = ''; foreach($this->getItems() as $item) { - $entryAuthor = $this->xml_encode($item->getAuthor()); + $entryTimestamp = $item->getTimestamp(); $entryTitle = $this->xml_encode($item->getTitle()); - $entryUri = $this->xml_encode($item->getURI()); - $entryTimestamp = $this->xml_encode(date(DATE_ATOM, $item->getTimestamp())); - $entryContent = $this->xml_encode($this->sanitizeHtml($item->getContent())); + $entryContent = $item->getContent(); + $entryUri = $item->getURI(); + + // the item id must be a valid unique URI + $entryID = $this->xml_encode($entryUri); + if (empty($entryID)) + $entryID = 'urn:sha1:' . hash('sha1', $entryTitle . $entryContent); + + if (empty($entryTimestamp)) + $entryTimestamp = $this->lastModified; + + if (empty($entryTitle)) { + $entryTitle = str_replace("\n", ' ', strip_tags($entryContent)); + if (strlen($entryTitle) > self::LIMIT_TITLE) { + $wrapPos = strpos(wordwrap($entryTitle, self::LIMIT_TITLE), "\n"); + $entryTitle = substr($entryTitle, 0, $wrapPos) . '...'; + } + } + + if (empty($entryContent)) + $entryContent = $entryTitle; + + $entryAuthor = $this->xml_encode($item->getAuthor()); + $entryTitle = $this->xml_encode($entryTitle); + $entryUri = $this->xml_encode($entryUri); + $entryTimestamp = $this->xml_encode(gmdate(DATE_ATOM, $entryTimestamp)); + $entryContent = $this->xml_encode($this->sanitizeHtml($entryContent)); $entryEnclosures = ''; foreach($item->getEnclosures() as $enclosure) { @@ -49,16 +82,28 @@ class AtomFormat extends FormatAbstract{ . PHP_EOL; } + $entryLinkAlternate = ''; + if (!empty($entryUri)) { + $entryLinkAlternate = ''; + } + + if (!empty($entryAuthor)) { + $entryAuthor = '' + . $entryAuthor + . ''; + } + $entries .= << - - {$entryAuthor} - {$entryTitle} - - {$entryUri} + {$entryTimestamp} {$entryTimestamp} + {$entryID} + {$entryLinkAlternate} + {$entryAuthor} {$entryContent} {$entryEnclosures} {$entryCategories} @@ -67,21 +112,24 @@ class AtomFormat extends FormatAbstract{ EOD; } - $feedTimestamp = date(DATE_ATOM, time()); - $charset = $this->getCharset(); + $feedTimestamp = gmdate(DATE_ATOM, $this->lastModified); + $charset = $this->getCharset(); /* Data are prepared, now let's begin the "MAGIE !!!" */ $toReturn = << - + {$title} - http{$https}://{$httpHost}{$httpInfo}/ + {$feedUrl} {$icon} {$icon} {$feedTimestamp} + + {$feedAuthor} + - + {$entries} EOD; diff --git a/tests/AtomFormatTest.php b/tests/AtomFormatTest.php new file mode 100644 index 00000000..1a8905fb --- /dev/null +++ b/tests/AtomFormatTest.php @@ -0,0 +1,89 @@ +setSample($path); + $this->initFormat(); + + $this->assertContains( + 'Content-Type: application/atom+xml; charset=' . $this->format->getCharset(), + xdebug_get_headers() + ); + } + + /** + * @dataProvider sampleProvider + * @runInSeparateProcess + */ + public function testOutput($path) { + $this->setSample($path); + $this->initFormat(); + + $this->assertXmlStringEqualsXmlFile($this->sample->expected, $this->data); + } + + //////////////////////////////////////////////////////////////////////////// + + public function sampleProvider() { + $samples = array(); + foreach (glob(self::PATH_SAMPLES . '*.json') as $path) { + $samples[basename($path, '.json')] = array($path); + } + return $samples; + } + + private function setSample($path) { + $data = json_decode(file_get_contents($path), true); + if (isset($data['meta']) && isset($data['items'])) { + if (!empty($data['server'])) + $this->setServerVars($data['server']); + + $items = array(); + foreach($data['items'] as $item) { + $items[] = new \FeedItem($item); + } + + $this->sample = (object)array( + 'meta' => $data['meta'], + 'items' => $items, + 'expected' => self::PATH_EXPECTED . basename($path, '.json') . '.xml' + ); + } else { + $this->fail('invalid test sample: ' . basename($path, '.json')); + } + } + + private function setServerVars($list) { + $_SERVER = array_merge($_SERVER, $list); + } + + private function initFormat() { + $this->format = \Format::create('Atom'); + $this->format->setItems($this->sample->items); + $this->format->setExtraInfos($this->sample->meta); + $this->format->setLastModified(strtotime('2000-01-01 12:00:00 UTC')); + + $this->data = $this->getActualOutput($this->format->display()); + $this->assertNotFalse(simplexml_load_string($this->data)); + ob_clean(); + } +} diff --git a/tests/samples/expectedAtomFormat/feed.common.xml b/tests/samples/expectedAtomFormat/feed.common.xml new file mode 100644 index 00000000..0d696c03 --- /dev/null +++ b/tests/samples/expectedAtomFormat/feed.common.xml @@ -0,0 +1,77 @@ + + + + Sample feed with common data + https://example.com/feed?type=common&items=4 + https://example.com/logo.png + https://example.com/logo.png + 2000-01-01T12:00:00+00:00 + + RSS-Bridge + + + + + + Test Entry + 2018-12-01T12:00:00+00:00 + 2018-12-01T12:00:00+00:00 + http://example.com/blog/test-entry + + + fulmeek + + Hello world, this is a test entry. + + + + + + Announcing JSON Feed + 2017-05-17T13:02:12+00:00 + 2017-05-17T13:02:12+00:00 + https://jsonfeed.org/2017/05/17/announcing_json_feed + + + Brent Simmons and Manton Reece + + <p>We — Manton Reece and Brent Simmons — have noticed that JSON has become the developers’ choice for APIs, and that developers will often go out of their way to avoid XML. JSON is simpler to read and write, and it’s less prone to bugs.</p> + +<p>So we developed JSON Feed, a format similar to <a href="http://cyber.harvard.edu/rss/rss.html">RSS</a> and <a href="https://tools.ietf.org/html/rfc4287">Atom</a> but in JSON. It reflects the lessons learned from our years of work reading and publishing feeds.</p> + +<p><a href="https://jsonfeed.org/version/1">See the spec</a>. It’s at version 1, which may be the only version ever needed. If future versions are needed, version 1 feeds will still be valid feeds.</p> + +<h4>Notes</h4> + +<p>We have a <a href="https://github.com/manton/jsonfeed-wp">WordPress plugin</a> and, coming soon, a JSON Feed Parser for Swift. As more code is written, by us and others, we’ll update the <a href="https://jsonfeed.org/code">code</a> page.</p> + +<p>See <a href="https://jsonfeed.org/mappingrssandatom">Mapping RSS and Atom to JSON Feed</a> for more on the similarities between the formats.</p> + +<p>This website — the Markdown files and supporting resources — <a href="https://github.com/brentsimmons/JSONFeed">is up on GitHub</a>, and you’re welcome to comment there.</p> + +<p>This website is also a blog, and you can subscribe to the <a href="https://jsonfeed.org/xml/rss.xml">RSS feed</a> or the <a href="https://jsonfeed.org/feed.json">JSON feed</a> (if your reader supports it).</p> + +<p>We worked with a number of people on this over the course of several months. We list them, and thank them, at the bottom of the <a href="https://jsonfeed.org/version/1">spec</a>. But — most importantly — <a href="http://furbo.org/">Craig Hockenberry</a> spent a little time making it look pretty. :)</p> + + + Atom draft-07 snapshot + 2005-07-31T12:29:29+00:00 + 2005-07-31T12:29:29+00:00 + http://example.org/2005/04/02/atom + + + Mark Pilgrim + + <p><i>[Update: The Atom draft is finished.]</i></p> + + + + Star City + 2003-06-03T09:39:21+00:00 + 2003-06-03T09:39:21+00:00 + http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp + + How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia's <a href="http://howe.iki.rssi.ru/GCTC/gctc_e.htm">Star City</a>. + + + diff --git a/tests/samples/expectedAtomFormat/feed.empty.xml b/tests/samples/expectedAtomFormat/feed.empty.xml new file mode 100644 index 00000000..371135b6 --- /dev/null +++ b/tests/samples/expectedAtomFormat/feed.empty.xml @@ -0,0 +1,15 @@ + + + + Sample feed with minimum data + https://example.com/feed + https://github.com/favicon.ico + https://github.com/favicon.ico + 2000-01-01T12:00:00+00:00 + + RSS-Bridge + + + + + diff --git a/tests/samples/expectedAtomFormat/feed.emptyItems.xml b/tests/samples/expectedAtomFormat/feed.emptyItems.xml new file mode 100644 index 00000000..462a4e5c --- /dev/null +++ b/tests/samples/expectedAtomFormat/feed.emptyItems.xml @@ -0,0 +1,30 @@ + + + + Sample feed with minimum data + https://example.com/feed + https://github.com/favicon.ico + https://github.com/favicon.ico + 2000-01-01T12:00:00+00:00 + + RSS-Bridge + + + + + + Sample Item #1 + 2000-01-01T12:00:00+00:00 + 2000-01-01T12:00:00+00:00 + urn:sha1:29f59918d266c56a935da13e4122b524298e5a39 + Sample Item #1 + + + Sample Item #2 + 2000-01-01T12:00:00+00:00 + 2000-01-01T12:00:00+00:00 + urn:sha1:edf358cad1a7ae255d6bc97640dd9d27738f1b7b + Sample Item #2 + + + diff --git a/tests/samples/expectedAtomFormat/feed.microblog.xml b/tests/samples/expectedAtomFormat/feed.microblog.xml new file mode 100644 index 00000000..a6264aee --- /dev/null +++ b/tests/samples/expectedAtomFormat/feed.microblog.xml @@ -0,0 +1,30 @@ + + + + Sample microblog feed + https://example.com/feed + https://example.com/logo.png + https://example.com/logo.png + 2000-01-01T12:00:00+00:00 + + RSS-Bridge + + + + + + Oh 😲 I found three monkeys 🙈🙉🙊 + 2018-10-07T16:53:03+00:00 + 2018-10-07T16:53:03+00:00 + urn:sha1:1918f084648b82057c1dd3faa3d091da82a6fac2 + Oh 😲 I found three monkeys 🙈🙉🙊 + + + Something happened + 2018-10-07T16:38:17+00:00 + 2018-10-07T16:38:17+00:00 + urn:sha1:e62189168a06dfa74f61c621c79c33c4c8517e1f + Something happened + + +