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
|
|
|
|
2024-07-31 22:57:20 +03:00
|
|
|
public function collectExpandableDatas(string $url, $maxItems = -1, $headers = [])
|
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, '*/*'];
|
2024-07-31 22:57:20 +03:00
|
|
|
$httpHeaders = array_merge(['Accept: ' . implode(', ', $accept)], $headers);
|
2023-10-12 23:14:04 +03:00
|
|
|
$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
|
2024-01-29 23:51:34 +03:00
|
|
|
$problematicStrings = [
|
2023-10-13 21:48:08 +03:00
|
|
|
' ',
|
2023-10-13 03:31:09 +03:00
|
|
|
'»',
|
2024-01-25 18:06:24 +03:00
|
|
|
'’',
|
2023-10-13 03:31:09 +03:00
|
|
|
];
|
2024-01-29 23:51:34 +03:00
|
|
|
$xmlString = str_replace($problematicStrings, '', $xmlString);
|
|
|
|
|
2023-10-13 02:59:05 +03:00
|
|
|
$feedParser = new FeedParser();
|
2024-01-29 23:51:34 +03:00
|
|
|
try {
|
|
|
|
$this->feed = $feedParser->parseFeed($xmlString);
|
|
|
|
} catch (\Exception $e) {
|
2024-04-04 20:12:04 +03:00
|
|
|
// FeedMergeBridge relies on this string
|
2024-01-29 23:51:34 +03:00
|
|
|
throw new \Exception(sprintf('Failed to parse xml from %s: %s', $url, create_sane_exception_message($e)));
|
|
|
|
}
|
|
|
|
|
2023-10-13 02:59:05 +03:00
|
|
|
$items = array_slice($this->feed['items'], 0, $maxItems);
|
2024-01-29 23:51:06 +03:00
|
|
|
// todo: extract parse logic out from FeedParser
|
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
|
|
|
/**
|
2024-01-12 03:31:01 +03:00
|
|
|
* This method is overridden by bridges
|
2023-10-13 02:59:05 +03:00
|
|
|
*
|
|
|
|
* @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
|
|
|
}
|