core: Add item uid (#1017)

'uid' represents the unique id for a feed item. This item is null by
default and can be set to any string value. The provided string value
is always hashed to sha1 to make it the same length in all cases.

References #977, #1005
This commit is contained in:
LogMANOriginal 2019-02-03 20:56:41 +01:00 committed by GitHub
parent a29512deee
commit 394149b114
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 71 additions and 23 deletions

View file

@ -40,10 +40,15 @@ class AtomFormat extends FormatAbstract{
$entryTitle = $this->xml_encode($item->getTitle());
$entryContent = $item->getContent();
$entryUri = $item->getURI();
$entryID = '';
// the item id must be a valid unique URI
$entryID = $this->xml_encode($entryUri);
if (empty($entryID))
if (!empty($item->getUid()))
$entryID = 'urn:sha1:' . $item->getUid();
if (empty($entryID)) // Fallback to provided URI
$entryID = $this->xml_encode($entryUri);
if (empty($entryID)) // Fallback to title and content
$entryID = 'urn:sha1:' . hash('sha1', $entryTitle . $entryContent);
if (empty($entryTimestamp))

View file

@ -16,21 +16,22 @@ class JsonFormat extends FormatAbstract {
'content',
'enclosures',
'categories',
'uid',
);
public function stringify(){
$urlScheme = (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'] : '';
$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'] : '';
$extraInfos = $this->getExtraInfos();
$data = array(
'version' => 'https://jsonfeed.org/version/1',
'title' => (!empty($extraInfos['name'])) ? $extraInfos['name'] : $urlHost,
'home_page_url' => (!empty($extraInfos['uri'])) ? $extraInfos['uri'] : REPOSITORY,
'feed_url' => $urlScheme . $urlHost . $urlRequest
'version' => 'https://jsonfeed.org/version/1',
'title' => (!empty($extraInfos['name'])) ? $extraInfos['name'] : $urlHost,
'home_page_url' => (!empty($extraInfos['uri'])) ? $extraInfos['uri'] : REPOSITORY,
'feed_url' => $urlPrefix . $urlHost . $urlRequest
);
if (!empty($extraInfos['icon'])) {
@ -42,20 +43,24 @@ class JsonFormat extends FormatAbstract {
foreach ($this->getItems() as $item) {
$entry = array();
$entryAuthor = $item->getAuthor();
$entryTitle = $item->getTitle();
$entryUri = $item->getURI();
$entryTimestamp = $item->getTimestamp();
$entryContent = $this->sanitizeHtml($item->getContent());
$entryEnclosures = $item->getEnclosures();
$entryCategories = $item->getCategories();
$entryAuthor = $item->getAuthor();
$entryTitle = $item->getTitle();
$entryUri = $item->getURI();
$entryTimestamp = $item->getTimestamp();
$entryContent = $this->sanitizeHtml($item->getContent());
$entryEnclosures = $item->getEnclosures();
$entryCategories = $item->getCategories();
$vendorFields = $item->toArray();
foreach (self::VENDOR_EXCLUDES as $key) {
unset($vendorFields[$key]);
}
$entry['id'] = $entryUri;
$entry['id'] = $item->getUid();
if (empty($entry['id'])) {
$entry['id'] = $entryUri;
}
if (!empty($entryTitle)) {
$entry['title'] = $entryTitle;
@ -82,8 +87,8 @@ class JsonFormat extends FormatAbstract {
$entry['attachments'] = array();
foreach ($entryEnclosures as $enclosure) {
$entry['attachments'][] = array(
'url' => $enclosure,
'mime_type' => getMimeType($enclosure)
'url' => $enclosure,
'mime_type' => getMimeType($enclosure)
);
}
}

View file

@ -55,6 +55,9 @@ class FeedItem {
/** @var array List of category names or tags */
protected $categories = array();
/** @var string Unique ID for the current item */
protected $uid = null;
/** @var array Associative list of additional parameters */
protected $misc = array(); // Custom parameters
@ -391,6 +394,37 @@ class FeedItem {
return $this;
}
/**
* Get unique id
*
* Use {@see FeedItem::setUid()} to set the unique id.
*
* @param string The unique id.
*/
public function getUid() {
return $this->uid;
}
/**
* Set unique id.
*
* Use {@see FeedItem::getUid()} to get the unique id.
*
* @param string $uid A string that uniquely identifies the current item
* @return self
*/
public function setUid($uid) {
$this->uid = null; // Clear previous data
if(!is_string($uid)) {
Debug::log('Unique id must be a string!');
} else {
$this->uid = sha1($uid);
}
return $this;
}
/**
* Add miscellaneous elements to the item.
*
@ -426,6 +460,7 @@ class FeedItem {
'content' => $this->content,
'enclosures' => $this->enclosures,
'categories' => $this->categories,
'uid' => $this->uid,
), $this->misc
);
}
@ -454,6 +489,7 @@ class FeedItem {
case 'content': $this->setContent($value); break;
case 'enclosures': $this->setEnclosures($value); break;
case 'categories': $this->setCategories($value); break;
case 'uid': $this->setUid($value); break;
default: $this->addMisc($name, $value);
}
}
@ -476,6 +512,7 @@ class FeedItem {
case 'content': return $this->getContent();
case 'enclosures': return $this->getEnclosures();
case 'categories': return $this->getCategories();
case 'uid': return $this->getUid();
default:
if(array_key_exists($name, $this->misc))
return $this->misc[$name];

View file

@ -57,7 +57,7 @@
<title type="html">Atom draft-07 snapshot</title>
<published>2005-07-31T12:29:29+00:00</published>
<updated>2005-07-31T12:29:29+00:00</updated>
<id>http://example.org/2005/04/02/atom</id>
<id>urn:sha1:dd6b6c920d3b340ab9e07faf6682f2a7c4f70134</id>
<link href="http://example.org/2005/04/02/atom" rel="alternate" type="text/html"/>
<author>
<name>Mark Pilgrim</name>

View file

@ -26,7 +26,7 @@
},
"content_html": "<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 its less prone to bugs.</p>\n\n<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>\n\n<p><a href=\"https://jsonfeed.org/version/1\">See the spec</a>. Its 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>\n\n<h4>Notes</h4>\n\n<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, well update the <a href=\"https://jsonfeed.org/code\">code</a> page.</p>\n\n<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>\n\n<p>This website — the Markdown files and supporting resources — <a href=\"https://github.com/brentsimmons/JSONFeed\">is up on GitHub</a>, and youre welcome to comment there.</p>\n\n<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>\n\n<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>"
},{
"id": "http://example.org/2005/04/02/atom",
"id": "dd6b6c920d3b340ab9e07faf6682f2a7c4f70134",
"url": "http://example.org/2005/04/02/atom",
"title": "Atom draft-07 snapshot",
"date_modified": "2005-07-31T12:29:29+00:00",

View file

@ -25,6 +25,7 @@
"content": "<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 its less prone to bugs.</p>\n\n<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>\n\n<p><a href=\"https://jsonfeed.org/version/1\">See the spec</a>. Its 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>\n\n<h4>Notes</h4>\n\n<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, well update the <a href=\"https://jsonfeed.org/code\">code</a> page.</p>\n\n<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>\n\n<p>This website — the Markdown files and supporting resources — <a href=\"https://github.com/brentsimmons/JSONFeed\">is up on GitHub</a>, and youre welcome to comment there.</p>\n\n<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>\n\n<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>"
},{
"uri": "http://example.org/2005/04/02/atom",
"uid": "tag:example.org,2003:3.2397",
"title": "Atom draft-07 snapshot",
"timestamp": 1122812969,
"author": "Mark Pilgrim",