WIP on adding feed items (#4)
This commit is contained in:
@@ -52,6 +52,40 @@ class Feed {
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a feed's items
|
||||
* @param int $feedId The ID of the feed to which these items belong
|
||||
* @param SimpleXMLElement $channel The RSS feed items
|
||||
* @return array [ 'ok' => true ] if successful, [ 'error' => message ] if not
|
||||
*/
|
||||
public static function updateItems(int $feedId, SimpleXMLElement $channel): array {
|
||||
try {
|
||||
for ($i = 0; $i < sizeof($channel->item); $i++) {
|
||||
$item = $channel->item[$i];
|
||||
$itemGuid = (string)$item->guid ? $item->guid : $item->link;
|
||||
$isNew = !Data::itemExists($feedId, $itemGuid);
|
||||
if ($isNew) {
|
||||
$title = (string)$item->title;
|
||||
$link = (string)$item->link;
|
||||
$published = (string)$item->pubDate;
|
||||
// TODO: why is this getting all encoded content, and not just the one for the current item?
|
||||
$encodedContent = $item->xpath('//content:encoded');
|
||||
if ($encodedContent) {
|
||||
$content = (string) $encodedContent[$i];
|
||||
$isEncoded = true;
|
||||
} else {
|
||||
$content = $item->description;
|
||||
$isEncoded = false;
|
||||
}
|
||||
Data::addItem($feedId, $itemGuid, $link, $title, $published, $content, $isEncoded);
|
||||
} // TODO: else check updated date; may want to return that from the isNew check instead
|
||||
}
|
||||
} catch (Exception $ex) {
|
||||
return [ 'error' => $ex->getMessage() ];
|
||||
}
|
||||
return [ 'ok', true ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an RSS feed
|
||||
* @param string $url The URL of the RSS feed to add
|
||||
@@ -64,6 +98,9 @@ class Feed {
|
||||
$channel = $feed['ok']->channel;
|
||||
$feedId = Data::addFeed($feed['url'], (string) $channel->title, (string) $channel->lastBuildDate);
|
||||
|
||||
$result = self::updateItems($feedId, $channel);
|
||||
if (array_key_exists('error', $result)) return $result;
|
||||
|
||||
return [ 'ok' => true ];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user