Create nested function to receive article or element author

This fixes an error that occurred by attempting to access an object that
did not exist (author).
This commit is contained in:
logmanoriginal 2016-08-03 13:16:47 +02:00
parent d3e5711601
commit 4f9e03f346

View file

@ -7,7 +7,7 @@ class FuturaSciencesBridge extends BridgeAbstract {
$this->name = $this->getName(); $this->name = $this->getName();
$this->uri = $this->getURI(); $this->uri = $this->getURI();
$this->description = 'Returns the newest articles.'; $this->description = 'Returns the newest articles.';
$this->update = '2016-03-20'; $this->update = '2016-08-03';
$this->parameters[] = $this->parameters[] =
'[ '[
@ -149,6 +149,23 @@ class FuturaSciencesBridge extends BridgeAbstract {
return $string; return $string;
} }
// Extracts the author from an article or element
function ExtractAuthor($article, $element){
$article_author = $article->find('span.author', 0);
if($article_author){
$authorname = trim(str_replace(', Futura-Sciences', '', $article_author->plaintext));
if(empty($authorname)){
$element_author = $element->find('author', 0);
if($element_author)
$authorname = StripCDATA($element_author->plaintext);
else
return '';
}
return $authorname;
}
return '';
}
if (empty($param['feed'])) if (empty($param['feed']))
$this->returnError('Please select a feed to display.'.$url, 400); $this->returnError('Please select a feed to display.'.$url, 400);
if ($param['feed'] !== preg_replace('/[^a-zA-Z-\/]+/', '', $param['feed']) || substr_count($param['feed'], '/') > 1 || strlen($param['feed'] > 64)) if ($param['feed'] !== preg_replace('/[^a-zA-Z-\/]+/', '', $param['feed']) || substr_count($param['feed'], '/') > 1 || strlen($param['feed'] > 64))
@ -163,9 +180,6 @@ class FuturaSciencesBridge extends BridgeAbstract {
$article_url = str_replace('#xtor=RSS-8', '', StripCDATA($element->find('guid', 0)->plaintext)); $article_url = str_replace('#xtor=RSS-8', '', StripCDATA($element->find('guid', 0)->plaintext));
$article = $this->file_get_html($article_url) or $this->returnError('Could not request Futura-Sciences: '.$article_url, 500); $article = $this->file_get_html($article_url) or $this->returnError('Could not request Futura-Sciences: '.$article_url, 500);
$contents = $article->find('div.content', 0)->innertext; $contents = $article->find('div.content', 0)->innertext;
$author = trim(str_replace(', Futura-Sciences', '', $article->find('span.author', 0)->plaintext));
if (empty($author))
$author = StripCDATA($element->find('author', 0)->plaintext);
foreach (array( foreach (array(
'<div class="clear', '<div class="clear',
@ -192,7 +206,7 @@ class FuturaSciencesBridge extends BridgeAbstract {
$contents = StripWithDelimiters($contents, 'fs:xt:clickname="', '"'); $contents = StripWithDelimiters($contents, 'fs:xt:clickname="', '"');
$item = new \Item(); $item = new \Item();
$item->author = $author; $item->author = ExtractAuthor($article, $element);
$item->uri = $article_url; $item->uri = $article_url;
$item->title = StripCDATA($element->find('title', 0)->innertext); $item->title = StripCDATA($element->find('title', 0)->innertext);
$item->thumbnailUri = StripCDATA($element->find('enclosure', 0)->url); $item->thumbnailUri = StripCDATA($element->find('enclosure', 0)->url);