mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-08-16 15:31:26 +02:00
format: Refactor JsonFormat to JSON Feed version 1 (#988)
JsonFormat now implements https://jsonfeed.org/version/1 Closes #618
This commit is contained in:
parent
288d4de218
commit
8801ac9e64
11 changed files with 385 additions and 10 deletions
|
@ -1,17 +1,109 @@
|
|||
<?php
|
||||
/**
|
||||
* Json
|
||||
* Builds a JSON string from $this->items and return it to browser.
|
||||
*/
|
||||
* JsonFormat - JSON Feed Version 1
|
||||
* https://jsonfeed.org/version/1
|
||||
*
|
||||
* Validators:
|
||||
* https://validator.jsonfeed.org
|
||||
* https://github.com/vigetlabs/json-feed-validator
|
||||
*/
|
||||
class JsonFormat extends FormatAbstract {
|
||||
public function stringify(){
|
||||
$items = $this->getItems();
|
||||
$data = array();
|
||||
const VENDOR_EXCLUDES = array(
|
||||
'author',
|
||||
'title',
|
||||
'uri',
|
||||
'timestamp',
|
||||
'content',
|
||||
'enclosures',
|
||||
'categories',
|
||||
);
|
||||
|
||||
foreach($items as $item) {
|
||||
$data[] = $item->toArray();
|
||||
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'] : '';
|
||||
|
||||
$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
|
||||
);
|
||||
|
||||
if (!empty($extraInfos['icon'])) {
|
||||
$data['icon'] = $extraInfos['icon'];
|
||||
$data['favicon'] = $extraInfos['icon'];
|
||||
}
|
||||
|
||||
$items = array();
|
||||
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();
|
||||
|
||||
$vendorFields = $item->toArray();
|
||||
foreach (self::VENDOR_EXCLUDES as $key) {
|
||||
unset($vendorFields[$key]);
|
||||
}
|
||||
|
||||
$entry['id'] = $entryUri;
|
||||
|
||||
if (!empty($entryTitle)) {
|
||||
$entry['title'] = $entryTitle;
|
||||
}
|
||||
if (!empty($entryAuthor)) {
|
||||
$entry['author'] = array(
|
||||
'name' => $entryAuthor
|
||||
);
|
||||
}
|
||||
if (!empty($entryTimestamp)) {
|
||||
$entry['date_modified'] = gmdate(DATE_ATOM, $entryTimestamp);
|
||||
}
|
||||
if (!empty($entryUri)) {
|
||||
$entry['url'] = $entryUri;
|
||||
}
|
||||
if (!empty($entryContent)) {
|
||||
if ($this->isHTML($entryContent)) {
|
||||
$entry['content_html'] = $entryContent;
|
||||
} else {
|
||||
$entry['content_text'] = $entryContent;
|
||||
}
|
||||
}
|
||||
if (!empty($entryEnclosures)) {
|
||||
$entry['attachments'] = array();
|
||||
foreach ($entryEnclosures as $enclosure) {
|
||||
$entry['attachments'][] = array(
|
||||
'url' => $enclosure,
|
||||
'mime_type' => getMimeType($enclosure)
|
||||
);
|
||||
}
|
||||
}
|
||||
if (!empty($entryCategories)) {
|
||||
$entry['tags'] = array();
|
||||
foreach ($entryCategories as $category) {
|
||||
$entry['tags'][] = $category;
|
||||
}
|
||||
}
|
||||
if (!empty($vendorFields)) {
|
||||
$entry['_rssbridge'] = $vendorFields;
|
||||
}
|
||||
|
||||
if (empty($entry['id']))
|
||||
$entry['id'] = hash('sha1', $entryTitle . $entryContent);
|
||||
|
||||
$items[] = $entry;
|
||||
}
|
||||
$data['items'] = $items;
|
||||
|
||||
$toReturn = json_encode($data, JSON_PRETTY_PRINT);
|
||||
|
||||
// Remove invalid non-UTF8 characters
|
||||
|
@ -27,4 +119,8 @@ class JsonFormat extends FormatAbstract {
|
|||
|
||||
return parent::display();
|
||||
}
|
||||
|
||||
private function isHTML($text) {
|
||||
return (strlen(strip_tags($text)) != strlen($text));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue