‪TYPO3CMS  ‪main
RssWidget.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ServerRequestInterface;
24 
40 {
44  private readonly array ‪$options;
45  private ServerRequestInterface ‪$request;
46 
47  public function ‪__construct(
48  private readonly ‪WidgetConfigurationInterface $configuration,
49  private readonly Cache $cache,
50  private readonly ‪BackendViewFactory $backendViewFactory,
51  private readonly ?‪ButtonProviderInterface $buttonProvider = null,
52  array ‪$options = [],
53  ) {
54  $this->options = array_merge(
55  [
56  'limit' => 5,
57  'lifeTime' => 43200,
58  ],
60  );
61  }
62 
63  public function ‪setRequest(ServerRequestInterface ‪$request): void
64  {
65  $this->request = ‪$request;
66  }
67 
68  public function ‪renderWidgetContent(): string
69  {
70  $view = $this->backendViewFactory->create($this->request);
71  $view->assignMultiple([
72  'items' => $this->‪getRssItems(),
73  'options' => $this->options,
74  'button' => $this->buttonProvider,
75  'configuration' => $this->configuration,
76  ]);
77  return $view->render('Widget/RssWidget');
78  }
79 
80  protected function ‪getRssItems(): array
81  {
82  if (empty($this->options['feedUrl'])) {
83  return [];
84  }
85  $cacheHash = md5($this->options['feedUrl']);
86  if ($items = $this->cache->get($cacheHash)) {
87  return $items;
88  }
89 
90  $rssContent = ‪GeneralUtility::getUrl($this->options['feedUrl']);
91  if ($rssContent === false) {
92  throw new \RuntimeException('RSS URL could not be fetched', 1573385431);
93  }
94  $rssFeed = simplexml_load_string($rssContent);
95  $items = [];
96  foreach ($rssFeed->channel->item as $item) {
97  $items[] = [
98  'title' => trim((string)$item->title),
99  'link' => trim((string)$item->link),
100  'pubDate' => trim((string)$item->pubDate),
101  'description' => trim(strip_tags((string)$item->description)),
102  ];
103  }
104  usort($items, static function ($item1, $item2) {
105  return new \DateTime($item2['pubDate']) <=> new \DateTime($item1['pubDate']);
106  });
107  $items = array_slice($items, 0, $this->options['limit']);
108 
109  $this->cache->set($cacheHash, $items, ['dashboard_rss'], $this->options['lifeTime']);
110 
111  return $items;
112  }
113 
114  public function ‪getOptions(): array
115  {
116  return ‪$this->options;
117  }
118 }
‪TYPO3\CMS\Dashboard\Widgets\RssWidget\__construct
‪__construct(private readonly WidgetConfigurationInterface $configuration, private readonly Cache $cache, private readonly BackendViewFactory $backendViewFactory, private readonly ?ButtonProviderInterface $buttonProvider=null, array $options=[],)
Definition: RssWidget.php:47
‪TYPO3\CMS\Dashboard\Widgets\RssWidget
Definition: RssWidget.php:40
‪TYPO3\CMS\Backend\View\BackendViewFactory
Definition: BackendViewFactory.php:35
‪TYPO3\CMS\Dashboard\Widgets\RequestAwareWidgetInterface
Definition: RequestAwareWidgetInterface.php:29
‪TYPO3\CMS\Dashboard\Widgets\WidgetInterface
Definition: WidgetInterface.php:26
‪TYPO3\CMS\Dashboard\Widgets\RssWidget\getOptions
‪getOptions()
Definition: RssWidget.php:114
‪TYPO3\CMS\Core\Utility\GeneralUtility\getUrl
‪static string false getUrl(string $url)
Definition: GeneralUtility.php:1444
‪TYPO3\CMS\Dashboard\Widgets\RssWidget\$request
‪ServerRequestInterface $request
Definition: RssWidget.php:45
‪TYPO3\CMS\Dashboard\Widgets\RssWidget\getRssItems
‪getRssItems()
Definition: RssWidget.php:80
‪TYPO3\CMS\Dashboard\Widgets\RssWidget\$options
‪readonly array $options
Definition: RssWidget.php:44
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Dashboard\Widgets
Definition: AdditionalCssInterface.php:18
‪TYPO3\CMS\Dashboard\Widgets\ButtonProviderInterface
Definition: ButtonProviderInterface.php:24
‪TYPO3\CMS\Dashboard\Widgets\WidgetConfigurationInterface
Definition: WidgetConfigurationInterface.php:26
‪TYPO3\CMS\Dashboard\Widgets\RssWidget\setRequest
‪setRequest(ServerRequestInterface $request)
Definition: RssWidget.php:63
‪TYPO3\CMS\Dashboard\Widgets\RssWidget\renderWidgetContent
‪renderWidgetContent()
Definition: RssWidget.php:68