Skip to content

Commit

Permalink
Refactored description truncator
Browse files Browse the repository at this point in the history
  • Loading branch information
clash82 committed Nov 28, 2024
1 parent e0be448 commit 6bb4b03
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 33 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ext-simplexml": "*",
"ext-json": "*",
"ext-libxml": "*",
"ext-dom": "*",
"wolfcast/browser-detection": "^2.9",
"simplepie/simplepie": "^1.8"
},
Expand Down
5 changes: 3 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions config/sample.ini.dist
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
name=Sample RSS channel name
rss=https://link-to-your-working-rss-channel

; remove whitespaces and HTML tags from post content (0|1, disabled by default)
trim=0
22 changes: 14 additions & 8 deletions src/lib/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use MultiRssCombiner\Renderer\PageRenderer;
use MultiRssCombiner\Renderer\RssRenderer;
use MultiRssCombiner\Value\Item;
use SimplePie\SimplePie;
use Wolfcast\BrowserDetection;

class App
Expand All @@ -22,10 +23,17 @@ class App

final public const APP_CHANNEL_CONFIGURATION_PATH = '/config/';

private readonly ChannelConfiguration $channels;

public function __construct()
{
$this->channels = new ChannelConfiguration(self::APP_CHANNEL_CONFIGURATION_PATH);
}

public function buildView(bool $showDefault = true): void
{
$configuration = new GeneralConfiguration(self::APP_CONFIGURATION_FILE);
$cache = new RssCacheProvider(self::APP_RSS_CACHE_FILE, $configuration->get()->getLimit());
$cache = new RssCacheProvider($this->channels, self::APP_RSS_CACHE_FILE, $configuration->get()->getLimit());
$browser = new BrowserDetection();

// detect if client belongs to the one of the supported web browsers or is just an RSS reader
Expand All @@ -45,17 +53,15 @@ public function buildView(bool $showDefault = true): void

public function buildCache(): void
{
$channels = new ChannelConfiguration(self::APP_CHANNEL_CONFIGURATION_PATH);

$feed = new \SimplePie();
$feed->enable_order_by_date(true);
$feed = new SimplePie();
$feed->enable_order_by_date();
$feed->force_feed(true);
$feed->enable_cache(false);

$items = [];

// fetch RSS details
foreach ($channels->getAll() as $channel) {
foreach ($this->channels->getAll() as $channel) {
printf('Fetching %s (%s)<br>', $channel->getName(), $channel->getUrl());

$feed->set_feed_url($channel->getUrl());
Expand Down Expand Up @@ -84,7 +90,7 @@ public function buildCache(): void
}

$item = new Item(
$channel->getName(),
$channel,
$item->get_title() ?? '',
$description ?? '',
$item->get_link() ?? '',
Expand All @@ -98,7 +104,7 @@ public function buildCache(): void
}

// reorder items
usort($items, fn($a, $b) => $b->getPubDate() <=> $a->getPubDate());
usort($items, fn ($a, $b) => $b->getPubDate() <=> $a->getPubDate());

// store everything in cache
$cache = new RssCacheManager(self::APP_RSS_CACHE_FILE);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Manager/RssCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function save(): void

foreach ($this->cache as $item) {
$element = $xml->addChild('item');
$element->addChild('channelName', $item->getChannelName());
$element->addAttribute('channel', $item->getChannelId());
$element->addChild('title', $item->getTitle());
$element->addChild('description', $item->getDescription());
$element->addChild('link', $item->getLink());
Expand Down
16 changes: 14 additions & 2 deletions src/lib/Provider/ChannelConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ public function __construct(string $path)

$ini = (array)parse_ini_file($fileInfo->getRealPath());

$this->channels[] = new Channel(
$this->channels[$fileInfo->getFilename()] = new Channel(
$fileInfo->getFilename(),
$ini['name'],
$ini['url']
$ini['url'],
isset($ini['trim']) && $ini['trim']
);
}
}
Expand All @@ -36,4 +38,14 @@ public function getAll(): array
{
return $this->channels;
}

public function getById(string $id): Channel
{
return $this->channels[$id];
}

public function channelExists(string $id): bool
{
return isset($this->channels[$id]);
}
}
4 changes: 2 additions & 2 deletions src/lib/Provider/GeneralConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use MultiRssCombiner\Exception\ConfigurationNotFoundException;
use MultiRssCombiner\Value\General;

class GeneralConfiguration implements GeneralConfigurationProvider
readonly class GeneralConfiguration implements GeneralConfigurationProvider
{
private readonly General $configuration;
private General $configuration;

public function __construct(string $filename)
{
Expand Down
9 changes: 7 additions & 2 deletions src/lib/Provider/RssCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class RssCache implements CacheProvider
/** @var Item[] */
private array $cache = [];

public function __construct(string $fileName, int $limit)
public function __construct(ChannelConfiguration $channelConfiguration, string $fileName, int $limit)
{
$cacheFile = sprintf('%s%s', getcwd(), $fileName);

Expand All @@ -34,11 +34,16 @@ public function __construct(string $fileName, int $limit)
}

$item = $cacheContent['item'][$i];
$channelId = $item['@attributes']['channel'];

if (!$channelConfiguration->channelExists($channelId)) {
continue;
}

$date = new \DateTime($item['pubDate']);

$this->cache[] = new Item(
$item['channelName'],
$channelConfiguration->getById($channelId),
$item['title'],
$item['description'],
$item['link'],
Expand Down
18 changes: 16 additions & 2 deletions src/lib/Value/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@

namespace MultiRssCombiner\Value;

class Channel
readonly class Channel
{
public function __construct(private readonly string $name, private readonly string $url)
public function __construct(
private string $id,
private string $name,
private string $url,
private bool $trimContent
) {
}

public function getId(): string
{
return $this->id;
}

public function getName(): string
Expand All @@ -17,4 +26,9 @@ public function getUrl(): string
{
return $this->url;
}

public function isTrimContent(): bool
{
return $this->trimContent;
}
}
18 changes: 15 additions & 3 deletions src/lib/Value/General.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@

use MultiRssCombiner\App;

class General
readonly class General
{
public function __construct(private readonly string $title, private readonly string $description, private readonly string $link, private readonly string $language, private readonly string $copyright, private readonly string $icon, private readonly int $iconWidth, private readonly int $iconHeight, private readonly string $dateFormat, private readonly int $limit, private readonly string $guidPrefix, private readonly string $feedUrl)
{
public function __construct(
private string $title,
private string $description,
private string $link,
private string $language,
private string $copyright,
private string $icon,
private int $iconWidth,
private int $iconHeight,
private string $dateFormat,
private int $limit,
private string $guidPrefix,
private string $feedUrl
) {
}

public function getTitle(): string
Expand Down
40 changes: 29 additions & 11 deletions src/lib/Value/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,27 @@

use DateTime;

class Item
readonly class Item
{
public function __construct(private readonly string $channelName, private readonly string $title, private readonly string $description, private readonly string $link, private readonly string $guid, private readonly ?string $image, private readonly DateTime $pubDate)
public function __construct(
private Channel $channel,
private string $title,
private string $description,
private string $link,
private string $guid,
private ?string $image,
private DateTime $pubDate
) {
}

public function getChannelId(): string
{
return $this->channel->getId();
}

public function getChannelName(): string
{
return $this->channelName;
return $this->channel->getName();
}

public function getTitle(): string
Expand All @@ -22,17 +34,23 @@ public function getTitle(): string

public function getDescription(): string
{
# remove whitespaces inside HTML tags
$description = preg_replace('/\s+/', ' ', $this->description);
$description = $this->description;

if ($this->channel->isTrimContent()) {
# remove whitespaces inside HTML tags
$description = preg_replace('/\s+/', ' ', $description);

# replace <br /> and redundant <br /> tags
$description = preg_replace('#<br />(\s*<br />)+#', '<br>', $description);

# replace <br /> and redundant <br /> tags
$description = preg_replace('#<br />(\s*<br />)+#', '<br>', $description);
# replace redundant <br> tags
$description = preg_replace('#<br>(\s*<br>)+#', '<br>', $description);

# replace redundant <br> tags
$description = preg_replace('#<br>(\s*<br>)+#', '<br>', $description);
# remove all HTML tags
$description = strip_tags($description);
}

# remove all HTML tags
return strip_tags($description);
return $description;
}

public function getLink(): string
Expand Down

0 comments on commit 6bb4b03

Please sign in to comment.