Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Build/Scripts/classicFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
echo 'Classic autoloading resolved both SimplePie APIs from ' . $expectedPath . PHP_EOL;
exit(0);
}
if ($mode === 'verifyStored') {
$pool = $container->get(\TYPO3\CMS\Core\Database\ConnectionPool::class);
$row = $pool->getConnectionForTable('tx_feeddisplay_feed')->select(['response_data', 'revision'], 'tx_feeddisplay_feed')->fetchAssociative();
if (!is_array($row) || !is_string($row['response_data']) || hash('sha256', $row['response_data']) !== $row['revision']) {
throw new RuntimeException('Classic rendering did not persist a complete feed document.');
}
\ErHaWeb\FeedDisplay\Storage\FeedDocument::decode($row['response_data']);
echo 'Verified the persistent Classic source document.' . PHP_EOL;
exit(0);
}
if ($mode !== 'seed') {
throw new InvalidArgumentException('Expected seed or probe.');
}
Expand Down
11 changes: 11 additions & 0 deletions Build/Scripts/testClassic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ for mode in linked packaged; do
exit 1
fi
done
TYPO3_CONTEXT=Development php Build/Scripts/classicFixture.php "${instance}" verifyStored
rm "${instance}/fileadmin/feed.xml"
(
cd "${instance}"
TYPO3_CONTEXT=Development php typo3/sysext/core/bin/typo3 cache:flush
)
curl --fail --silent --show-error "${base}" -o "${instance}/persistent.html"
if ! grep -q 'Classic bundle integration item' "${instance}/persistent.html"; then
cat "${instance}/server.log" >&2
exit 1
fi
kill "${serverPid}"
wait "${serverPid}" 2>/dev/null || true
serverPid=''
Expand Down
3 changes: 3 additions & 0 deletions Build/Scripts/verifyTerArtefact.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
$manifest = json_decode($zip->getFromName('composer.json'), true, 512, JSON_THROW_ON_ERROR);
$bundlePath = $manifest['extra']['typo3/cms']['Package']['providesPackages']['simplepie/simplepie'] ?? null;
$isBundle = $bundlePath === 'Resources/Private/Php/ComposerVendor';
if (is_file($sourceRoot . '/ext_tables.sql') && $zip->locateName('ext_tables.sql') === false) {
throw new RuntimeException('The TER package is missing the extension database schema.');
}
$temporary = sys_get_temp_dir() . '/feed-display-artefact-' . bin2hex(random_bytes(8));
mkdir($temporary);
try {
Expand Down
69 changes: 69 additions & 0 deletions Classes/Command/RefreshFeedsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/**
* This file is part of the "feed_display" Extension for TYPO3 CMS.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

declare(strict_types=1);

namespace ErHaWeb\FeedDisplay\Command;

use ErHaWeb\FeedDisplay\Service\ConfiguredFeedUrls;
use ErHaWeb\FeedDisplay\Service\FeedRefresher;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand(name: 'feeddisplay:refresh', description: 'Refresh configured and stored feed sources')]
final class RefreshFeedsCommand extends Command
{
public function __construct(private readonly ConfiguredFeedUrls $urls, private readonly FeedRefresher $refresher)
{
parent::__construct();
}

protected function configure(): void
{
$this->addOption('url', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Additional feed source (repeatable; commas remain part of the URL)');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
/** @var list<string> $additional */
$additional = $input->getOption('url');
try {
$urls = $this->urls->collect($additional);
} catch (\Throwable $exception) {
$io->error(OutputFormatter::escape('Could not collect feed sources: ' . $exception->getMessage()));
return self::FAILURE;
}
if ($urls === []) {
$io->success('No feed sources found. Explicit FlexForm URLs, stored sources and --url are considered.');
return self::SUCCESS;
}
$status = self::SUCCESS;
foreach ($urls as $url) {
$result = $this->refresher->refresh($url, true);
$io->writeln(OutputFormatter::escape($result->status . ': ' . $url));
if ($result->error !== null) {
$io->error(OutputFormatter::escape($result->error->getMessage()));
$status = self::FAILURE;
}
}
return $status;
}
}
70 changes: 70 additions & 0 deletions Classes/Command/RemoveFeedSourcesCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/**
* This file is part of the "feed_display" Extension for TYPO3 CMS.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

declare(strict_types=1);

namespace ErHaWeb\FeedDisplay\Command;

use ErHaWeb\FeedDisplay\Service\FeedRefresher;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;

/** The tag keeps this command out of the Scheduler on TYPO3 13 and 14. */
#[AutoconfigureTag('console.command', [
'command' => 'feeddisplay:remove',
'description' => 'Remove explicitly selected stored feed sources',
'schedulable' => false,
])]
final class RemoveFeedSourcesCommand extends Command
{
public function __construct(private readonly FeedRefresher $refresher)
{
parent::__construct();
}

protected function configure(): void
{
$this->addOption('url', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Exact source to remove (required, repeatable)');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
/** @var list<string> $arguments */
$arguments = $input->getOption('url');
$urls = array_values(array_unique(array_map(static fn(string $url): string => trim(stripslashes($url)), $arguments)));
if ($urls === [] || in_array('', $urls, true)) {
$io->error('Specify each source to remove with --url.');
return self::INVALID;
}
$status = self::SUCCESS;
foreach ($urls as $url) {
$result = $this->refresher->remove($url);
$io->writeln(OutputFormatter::escape(($result->status === 'updated' ? 'removed' : $result->status) . ': ' . $url));
if ($result->error !== null) {
$io->error(OutputFormatter::escape($result->error->getMessage()));
}
if ($result->status !== 'updated') {
$status = self::FAILURE;
}
}
return $status;
}
}
40 changes: 22 additions & 18 deletions Classes/Controller/FeedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,42 @@

namespace ErHaWeb\FeedDisplay\Controller;

use ErHaWeb\FeedDisplay\Service\FeedDataService;
use ErHaWeb\FeedDisplay\Service\FeedProvider;
use ErHaWeb\FeedDisplay\Storage\FeedStorage;
use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Cache\CacheDataCollector;
use TYPO3\CMS\Core\Cache\CacheTag;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;

class FeedController extends ActionController
{
public function __construct(
private readonly FrontendInterface $cache,
private readonly FeedDataService $feedDataService
) {}
public function __construct(private readonly FeedProvider $feedProvider) {}

public function displayAction(): ResponseInterface
{
$scheduler = trim((string)($this->settings['fetchMode'] ?? 'live')) === 'scheduler';
if (array_key_exists('fetchMode', $this->settings)) {
unset($this->settings['fetchMode']);
$this->view->assign('settings', $this->settings);
}
if ($this->settings === []) {
return $this->htmlResponse();
}

$cacheIdentifier = 'feeddisplay_' . md5(serialize($this->settings));
$data = $this->cache->get($cacheIdentifier);
$cacheDuration = (int)$this->settings['cacheDuration'];

if ($cacheDuration === 0) {
$data = $this->feedDataService->buildData($this->settings);
$this->cache->remove($cacheIdentifier);
} elseif ($data === false || $data['settings'] !== $this->settings) {
$data = $this->feedDataService->buildData($this->settings);
$this->cache->set($cacheIdentifier, $data, [], $cacheDuration);
$url = trim(stripslashes((string)($this->settings['feedUrl'] ?? '')));
$collector = $this->request->getAttribute('frontend.cache.collector');
if ($scheduler && $url !== '' && $collector instanceof CacheDataCollector) {
$collector->addCacheTags(new CacheTag(FeedStorage::cacheTag($url)));
}
$provided = $this->feedProvider->provide($this->settings, $scheduler);
$data = $provided->data;
if ($scheduler && $url !== '' && $provided->revision !== null && $collector instanceof CacheDataCollector) {
$collector->addCacheTags(new CacheTag(FeedStorage::revisionTag($url, $provided->revision)));
}
if ($scheduler && !isset($data['feed']) && !isset($data['items']) && $collector instanceof CacheDataCollector) {
$collector->restrictMaximumLifetime(60);
}

$this->view->assign('data', $data);

return $this->htmlResponse();
}
}
68 changes: 68 additions & 0 deletions Classes/EventListener/VerifyPageFeedRevisions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/**
* This file is part of the "feed_display" Extension for TYPO3 CMS.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

declare(strict_types=1);

namespace ErHaWeb\FeedDisplay\EventListener;

use ErHaWeb\FeedDisplay\Storage\FeedStorage;
use Psr\Log\LoggerInterface;
use TYPO3\CMS\Core\Attribute\AsEventListener;
use TYPO3\CMS\Core\Cache\CacheDataCollector;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Frontend\Event\AfterCachedPageIsPersistedEvent;

/** @internal Discards pages whose source changed before their cache entry existed. */
#[AsEventListener(identifier: 'feed-display/verify-page-feed-revisions')]
final class VerifyPageFeedRevisions
{
public function __construct(
private readonly FeedStorage $storage,
private readonly CacheManager $cacheManager,
private readonly LoggerInterface $logger,
) {}

public function __invoke(AfterCachedPageIsPersistedEvent $event): void
{
$collector = $event->getRequest()->getAttribute('frontend.cache.collector');
if (!$collector instanceof CacheDataCollector) {
return;
}
$revisions = [];
foreach ($collector->getCacheTags() as $tag) {
if (preg_match('/^feeddisplay_rev_([0-9a-f]{64})_([0-9a-f]{64})$/D', $tag->name, $match) === 1) {
$revisions[$match[1]][$match[2]] = true;
}
}
foreach ($revisions as $urlHash => $usedRevisions) {
$context = ['urlHash' => $urlHash, 'cacheIdentifier' => $event->getCacheIdentifier()];
try {
$current = $this->storage->findRevisionByHash($urlHash);
} catch (\Throwable $exception) {
$this->logger->warning('Could not verify the cached feed revision.', $context + ['exception' => $exception]);
continue;
}
if ($current !== null && count($usedRevisions) === 1 && isset($usedRevisions[$current])) {
continue;
}
try {
$this->cacheManager->getCache('pages')->remove($event->getCacheIdentifier());
} catch (\Throwable $exception) {
$this->logger->warning('Could not remove an obsolete feed page.', $context + ['exception' => $exception]);
}
return;
}
}
}
76 changes: 76 additions & 0 deletions Classes/Service/ConfiguredFeedUrls.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/**
* This file is part of the "feed_display" Extension for TYPO3 CMS.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

declare(strict_types=1);

namespace ErHaWeb\FeedDisplay\Service;

use ErHaWeb\FeedDisplay\Storage\FeedStorage;
use Psr\Log\LoggerInterface;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/** @internal Explicit plugin URLs and previously encountered sources. */
final class ConfiguredFeedUrls
{
public function __construct(private readonly ConnectionPool $connections, private readonly FeedStorage $storage, private readonly LoggerInterface $logger) {}

/** @param list<string> $additional
* @return list<string>
*/
public function collect(array $additional): array
{
$urls = $this->storage->findUrls();
$query = $this->connections->getQueryBuilderForTable('tt_content');
$query->getRestrictions()->removeAll();
$records = $query->select('*')->from('tt_content')->where(
$query->expr()->eq('deleted', 0),
$query->expr()->eq('t3ver_wsid', 0),
$query->expr()->or(
$query->expr()->eq('CType', $query->createNamedParameter('feeddisplay_pi1')),
$query->expr()->eq('CType', $query->createNamedParameter('list')),
),
)->executeQuery()->fetchAllAssociative();
foreach ($records as $record) {
if ($record['CType'] !== 'feeddisplay_pi1' && !($record['CType'] === 'list' && ($record['list_type'] ?? '') === 'feeddisplay_pi1')) {
continue;
}
if (trim((string)($record['pi_flexform'] ?? '')) === '') {
continue;
}
$form = GeneralUtility::xml2array((string)$record['pi_flexform']);
if (!is_array($form)) {
$this->logger->warning('Could not read feed FlexForm.', ['contentElementUid' => (int)$record['uid'], 'error' => $form]);
continue;
}
$sheets = $form['data'] ?? [];
if (!is_array($sheets)) {
$this->logger->warning('Invalid feed FlexForm structure.', ['contentElementUid' => (int)$record['uid']]);
continue;
}
foreach ($sheets as $sheet) {
if (!is_array($sheet)) {
continue;
}
$url = $sheet['lDEF']['settings.feedUrl']['vDEF'] ?? null;
if (is_string($url)) {
$urls[] = $url;
}
}
}
$urls = array_map(static fn(string $url): string => trim(stripslashes($url)), [...$urls, ...$additional]);
return array_values(array_unique(array_filter($urls, static fn(string $url): bool => $url !== '')));
}
}
Loading
Loading