core: Add FeedItem class (#940)

Add transformation from legacy items to FeedItems, before transforming
items to the desired format. This allows using legacy bridges alongside
bridges that return FeedItems.

As discussed in #940, instead of throwing exceptions on invalid
parameters, add messages to the debug log instead

Add support for strings to setTimestamp(). If the provided timestamp
is a string, automatically try to parse it using strtotime().

This allows bridges to simply use `$item['timestamp'] = $timestamp;`
instead of `$item['timestamp'] = strtotime($timestamp);`

Support simple_html_dom_node as input paramter for setURI

Support simple_html_dom_node as input parameter for setContent
This commit is contained in:
LogMANOriginal 2018-12-26 22:41:32 +01:00 committed by GitHub
parent 4095cad9b4
commit 988635dcf3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 583 additions and 66 deletions

View file

@ -27,30 +27,26 @@ class AtomFormat extends FormatAbstract{
$entries = '';
foreach($this->getItems() as $item) {
$entryAuthor = isset($item['author']) ? $this->xml_encode($item['author']) : '';
$entryTitle = isset($item['title']) ? $this->xml_encode($item['title']) : '';
$entryUri = isset($item['uri']) ? $this->xml_encode($item['uri']) : '';
$entryTimestamp = isset($item['timestamp']) ? $this->xml_encode(date(DATE_ATOM, $item['timestamp'])) : '';
$entryContent = isset($item['content']) ? $this->xml_encode($this->sanitizeHtml($item['content'])) : '';
$entryAuthor = $this->xml_encode($item->getAuthor());
$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()));
$entryEnclosures = '';
if(isset($item['enclosures'])) {
foreach($item['enclosures'] as $enclosure) {
$entryEnclosures .= '<link rel="enclosure" href="'
. $this->xml_encode($enclosure)
. '" type="' . getMimeType($enclosure) . '" />'
. PHP_EOL;
}
foreach($item->getEnclosures() as $enclosure) {
$entryEnclosures .= '<link rel="enclosure" href="'
. $this->xml_encode($enclosure)
. '" type="' . getMimeType($enclosure) . '" />'
. PHP_EOL;
}
$entryCategories = '';
if(isset($item['categories'])) {
foreach($item['categories'] as $category) {
$entryCategories .= '<category term="'
. $this->xml_encode($category)
. '"/>'
. PHP_EOL;
}
foreach($item->getCategories() as $category) {
$entryCategories .= '<category term="'
. $this->xml_encode($category)
. '"/>'
. PHP_EOL;
}
$entries .= <<<EOD

View file

@ -9,31 +9,31 @@ class HtmlFormat extends FormatAbstract {
$entries = '';
foreach($this->getItems() as $item) {
$entryAuthor = isset($item['author']) ? '<br /><p class="author">by: ' . $item['author'] . '</p>' : '';
$entryTitle = isset($item['title']) ? $this->sanitizeHtml(strip_tags($item['title'])) : '';
$entryUri = isset($item['uri']) ? $item['uri'] : $uri;
$entryAuthor = $item->getAuthor() ? '<br /><p class="author">by: ' . $item->getAuthor() . '</p>' : '';
$entryTitle = $this->sanitizeHtml(strip_tags($item->getTitle()));
$entryUri = $item->getURI() ?: $uri;
$entryTimestamp = '';
if(isset($item['timestamp'])) {
if($item->getTimestamp()) {
$entryTimestamp = '<time datetime="'
. date(DATE_ATOM, $item['timestamp'])
. date(DATE_ATOM, $item->getTimestamp())
. '">'
. date(DATE_ATOM, $item['timestamp'])
. date(DATE_ATOM, $item->getTimestamp())
. '</time>';
}
$entryContent = '';
if(isset($item['content'])) {
if($item->getContent()) {
$entryContent = '<div class="content">'
. $this->sanitizeHtml($item['content'])
. $this->sanitizeHtml($item->getContent())
. '</div>';
}
$entryEnclosures = '';
if(isset($item['enclosures'])) {
if(!empty($item->getEnclosures())) {
$entryEnclosures = '<div class="attachments"><p>Attachments:</p>';
foreach($item['enclosures'] as $enclosure) {
foreach($item->getEnclosures() as $enclosure) {
$url = $this->sanitizeHtml($enclosure);
$entryEnclosures .= '<li class="enclosure"><a href="'
@ -47,10 +47,10 @@ class HtmlFormat extends FormatAbstract {
}
$entryCategories = '';
if(isset($item['categories']) && count($item['categories']) > 0) {
if(!empty($item->getCategories())) {
$entryCategories = '<div class="categories"><p>Categories:</p>';
foreach($item['categories'] as $category) {
foreach($item->getCategories() as $category) {
$entryCategories .= '<li class="category">'
. $this->sanitizeHtml($category)

View file

@ -6,7 +6,13 @@
class JsonFormat extends FormatAbstract {
public function stringify(){
$items = $this->getItems();
$toReturn = json_encode($items, JSON_PRETTY_PRINT);
$data = array();
foreach($items as $item) {
$data[] = $item->toArray();
}
$toReturn = json_encode($data, JSON_PRETTY_PRINT);
// Remove invalid non-UTF8 characters
ini_set('mbstring.substitute_character', 'none');

View file

@ -25,24 +25,24 @@ class MrssFormat extends FormatAbstract {
$items = '';
foreach($this->getItems() as $item) {
$itemAuthor = isset($item['author']) ? $this->xml_encode($item['author']) : '';
$itemTitle = strip_tags(isset($item['title']) ? $this->xml_encode($item['title']) : '');
$itemUri = isset($item['uri']) ? $this->xml_encode($item['uri']) : '';
$itemTimestamp = isset($item['timestamp']) ? $this->xml_encode(date(DATE_RFC2822, $item['timestamp'])) : '';
$itemContent = isset($item['content']) ? $this->xml_encode($this->sanitizeHtml($item['content'])) : '';
$itemAuthor = $this->xml_encode($item->getAuthor());
$itemTitle = $this->xml_encode($item->getTitle());
$itemUri = $this->xml_encode($item->getURI());
$itemTimestamp = $this->xml_encode(date(DATE_RFC2822, $item->getTimestamp()));
$itemContent = $this->xml_encode($this->sanitizeHtml($item->getContent()));
$entryEnclosuresWarning = '';
$entryEnclosures = '';
if(isset($item['enclosures'])) {
if(!empty($item->getEnclosures())) {
$entryEnclosures .= '<enclosure url="'
. $this->xml_encode($item['enclosures'][0])
. '" type="' . getMimeType($item['enclosures'][0]) . '" />';
. $this->xml_encode($item->getEnclosures()[0])
. '" type="' . getMimeType($item->getEnclosures()[0]) . '" />';
if(count($item['enclosures']) > 1) {
if(count($item->getEnclosures()) > 1) {
$entryEnclosures .= PHP_EOL;
$entryEnclosuresWarning = '&lt;br&gt;Warning:
Some media files might not be shown to you. Consider using the ATOM format instead!';
foreach($item['enclosures'] as $enclosure) {
foreach($item->getEnclosures() as $enclosure) {
$entryEnclosures .= '<atom:link rel="enclosure" href="'
. $enclosure . '" type="' . getMimeType($enclosure) . '" />'
. PHP_EOL;
@ -51,13 +51,10 @@ Some media files might not be shown to you. Consider using the ATOM format inste
}
$entryCategories = '';
if(isset($item['categories'])) {
foreach($item['categories'] as $category) {
$entryCategories .= '<category>'
. $category . '</category>'
. PHP_EOL;
}
foreach($item->getCategories() as $category) {
$entryCategories .= '<category>'
. $category . '</category>'
. PHP_EOL;
}
$items .= <<<EOD

View file

@ -6,7 +6,13 @@
class PlaintextFormat extends FormatAbstract {
public function stringify(){
$items = $this->getItems();
$toReturn = print_r($items, true);
$data = array();
foreach($items as $item) {
$data[] = $item->toArray();
}
$toReturn = print_r($data, true);
// Remove invalid non-UTF8 characters
ini_set('mbstring.substitute_character', 'none');