‪TYPO3CMS  10.4
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 
23 
39 {
43  private ‪$configuration;
44 
48  private ‪$view;
49 
53  private ‪$cache;
54 
58  private ‪$options;
59 
63  private ‪$buttonProvider;
64 
65  public function ‪__construct(
67  Cache ‪$cache,
69  ‪$buttonProvider = null,
70  array ‪$options = []
71  ) {
72  $this->configuration = ‪$configuration;
73  $this->view = ‪$view;
74  $this->cache = ‪$cache;
75  $this->options = array_merge(
76  [
77  'limit' => 5,
78  'lifeTime' => 43200
79  ],
81  );
82  $this->buttonProvider = ‪$buttonProvider;
83  }
84 
85  public function ‪renderWidgetContent(): string
86  {
87  $this->view->setTemplate('Widget/RssWidget');
88  $this->view->assignMultiple([
89  'items' => $this->‪getRssItems(),
90  'options' => $this->options,
91  'button' => $this->buttonProvider,
92  'configuration' => $this->configuration,
93  ]);
94  return $this->view->render();
95  }
96 
97  protected function ‪getRssItems(): array
98  {
99  $cacheHash = md5($this->options['feedUrl']);
100  if ($items = $this->cache->get($cacheHash)) {
101  return $items;
102  }
103 
104  $rssContent = ‪GeneralUtility::getUrl($this->options['feedUrl']);
105  if ($rssContent === false) {
106  throw new \RuntimeException('RSS URL could not be fetched', 1573385431);
107  }
108  $previousValueOfEntityLoader = null;
109  if (PHP_MAJOR_VERSION < 8) {
110  $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
111  }
112  $rssFeed = simplexml_load_string($rssContent);
113  if (PHP_MAJOR_VERSION < 8) {
114  libxml_disable_entity_loader($previousValueOfEntityLoader);
115  }
116  $items = [];
117  foreach ($rssFeed->channel->item as $item) {
118  $items[] = [
119  'title' => trim((string)$item->title),
120  'link' => trim((string)$item->link),
121  'pubDate' => trim((string)$item->pubDate),
122  'description' => trim(strip_tags((string)$item->description)),
123  ];
124  }
125  usort($items, function ($item1, $item2) {
126  return new \DateTime($item2['pubDate']) <=> new \DateTime($item1['pubDate']);
127  });
128  $items = array_slice($items, 0, $this->options['limit']);
129 
130  $this->cache->set($cacheHash, $items, ['dashboard_rss'], $this->options['lifeTime']);
131 
132  return $items;
133  }
134 }
‪TYPO3\CMS\Dashboard\Widgets\RssWidget\$view
‪StandaloneView $view
Definition: RssWidget.php:46
‪TYPO3\CMS\Dashboard\Widgets\RssWidget\$cache
‪Cache $cache
Definition: RssWidget.php:50
‪TYPO3\CMS\Dashboard\Widgets\RssWidget
Definition: RssWidget.php:39
‪TYPO3\CMS\Dashboard\Widgets\RssWidget\$buttonProvider
‪ButtonProviderInterface null $buttonProvider
Definition: RssWidget.php:58
‪TYPO3\CMS\Core\Utility\GeneralUtility\getUrl
‪static mixed getUrl($url, $includeHeader=0, $requestHeaders=null, &$report=null)
Definition: GeneralUtility.php:1748
‪TYPO3\CMS\Dashboard\Widgets\WidgetInterface
Definition: WidgetInterface.php:26
‪TYPO3\CMS\Dashboard\Widgets\RssWidget\__construct
‪__construct(WidgetConfigurationInterface $configuration, Cache $cache, StandaloneView $view, $buttonProvider=null, array $options=[])
Definition: RssWidget.php:60
‪TYPO3\CMS\Dashboard\Widgets\RssWidget\getRssItems
‪getRssItems()
Definition: RssWidget.php:92
‪TYPO3\CMS\Dashboard\Widgets\RssWidget\$options
‪array $options
Definition: RssWidget.php:54
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:34
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪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\renderWidgetContent
‪renderWidgetContent()
Definition: RssWidget.php:80
‪TYPO3\CMS\Dashboard\Widgets\RssWidget\$configuration
‪WidgetConfigurationInterface $configuration
Definition: RssWidget.php:42