2016-09-05 19:05:19 +03:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/**
|
2023-10-12 23:14:04 +03:00
|
|
|
* Expands an existing feed
|
2018-11-16 23:48:59 +03:00
|
|
|
*/
|
2016-09-10 20:11:09 +03:00
|
|
|
abstract class FeedExpander extends BridgeAbstract
|
|
|
|
{
|
2023-10-13 02:59:05 +03:00
|
|
|
private array $feed;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-10-12 23:14:04 +03:00
|
|
|
public function collectExpandableDatas(string $url, $maxItems = -1)
|
2016-09-10 21:41:11 +03:00
|
|
|
{
|
2023-10-12 23:14:04 +03:00
|
|
|
if (!$url) {
|
2022-08-06 23:46:28 +03:00
|
|
|
throw new \Exception('There is no $url for this RSS expander');
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2023-10-13 02:59:05 +03:00
|
|
|
$maxItems = (int) $maxItems;
|
2023-10-12 23:14:04 +03:00
|
|
|
if ($maxItems === -1) {
|
|
|
|
$maxItems = 999;
|
|
|
|
}
|
|
|
|
$accept = [MrssFormat::MIME_TYPE, AtomFormat::MIME_TYPE, '*/*'];
|
|
|
|
$httpHeaders = ['Accept: ' . implode(', ', $accept)];
|
|
|
|
$xmlString = getContents($url, $httpHeaders);
|
|
|
|
if ($xmlString === '') {
|
2023-03-20 21:11:51 +03:00
|
|
|
throw new \Exception(sprintf('Unable to parse xml from `%s` because we got the empty string', $url), 10);
|
2022-10-29 11:27:02 +03:00
|
|
|
}
|
2023-10-13 03:31:09 +03:00
|
|
|
// prepare/massage the xml to make it more acceptable
|
|
|
|
$badStrings = [
|
2023-10-13 21:48:08 +03:00
|
|
|
' ',
|
2023-10-13 03:31:09 +03:00
|
|
|
'»',
|
|
|
|
];
|
|
|
|
$xmlString = str_replace($badStrings, '', $xmlString);
|
2023-10-13 02:59:05 +03:00
|
|
|
$feedParser = new FeedParser();
|
|
|
|
$this->feed = $feedParser->parseFeed($xmlString);
|
|
|
|
$items = array_slice($this->feed['items'], 0, $maxItems);
|
2023-10-12 23:14:04 +03:00
|
|
|
foreach ($items as $item) {
|
2023-10-13 02:59:05 +03:00
|
|
|
// Give bridges a chance to modify the item
|
|
|
|
$item = $this->parseItem($item);
|
|
|
|
if ($item) {
|
|
|
|
$this->items[] = $item;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
}
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
/**
|
2023-10-13 02:59:05 +03:00
|
|
|
* This method is overidden by bridges
|
|
|
|
*
|
|
|
|
* @return array
|
2016-09-10 21:41:11 +03:00
|
|
|
*/
|
2023-10-13 02:59:05 +03:00
|
|
|
protected function parseItem(array $item)
|
2016-09-12 11:39:34 +03:00
|
|
|
{
|
2023-10-13 02:59:05 +03:00
|
|
|
return $item;
|
2016-09-12 11:39:34 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
public function getURI()
|
|
|
|
{
|
2023-10-13 02:59:05 +03:00
|
|
|
return $this->feed['uri'] ?? parent::getURI();
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
public function getName()
|
|
|
|
{
|
2023-10-13 02:59:05 +03:00
|
|
|
return $this->feed['title'] ?? parent::getName();
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-08-21 18:46:47 +03:00
|
|
|
public function getIcon()
|
|
|
|
{
|
2023-10-13 02:59:05 +03:00
|
|
|
return $this->feed['icon'] ?? parent::getIcon();
|
2018-08-21 18:46:47 +03:00
|
|
|
}
|
2016-09-05 19:05:19 +03:00
|
|
|
}
|