‪TYPO3CMS  11.5
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 
38 class RssWidget implements WidgetInterface
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(
66  WidgetConfigurationInterface $configuration,
67  Cache $cache,
68  StandaloneView $view,
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  ],
80  $options
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  if (empty($this->options['feedUrl'])) {
100  return [];
101  }
102  $cacheHash = md5($this->options['feedUrl']);
103  if ($items = $this->cache->get($cacheHash)) {
104  return $items;
105  }
106 
107  $rssContent = ‪GeneralUtility::getUrl($this->options['feedUrl']);
108  if ($rssContent === false) {
109  throw new \RuntimeException('RSS URL could not be fetched', 1573385431);
110  }
111  $previousValueOfEntityLoader = null;
112  if (PHP_MAJOR_VERSION < 8) {
113  $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
114  }
115  $rssFeed = simplexml_load_string($rssContent);
116  if (PHP_MAJOR_VERSION < 8) {
117  libxml_disable_entity_loader($previousValueOfEntityLoader);
118  }
119  $items = [];
120  foreach ($rssFeed->channel->item as $item) {
121  $items[] = [
122  'title' => trim((string)$item->title),
123  'link' => trim((string)$item->link),
124  'pubDate' => trim((string)$item->pubDate),
125  'description' => trim(strip_tags((string)$item->description)),
126  ];
127  }
128  usort($items, static function ($item1, $item2) {
129  return new \DateTime($item2['pubDate']) <=> new \DateTime($item1['pubDate']);
130  });
131  $items = array_slice($items, 0, $this->options['limit']);
132 
133  $this->cache->set($cacheHash, $items, ['dashboard_rss'], $this->options['lifeTime']);
134 
135  return $items;
136  }
137 
141  public function getOptions(): array
142  {
143  return $this->options;
144  }
145 }
‪TYPO3\CMS\Core\Utility\GeneralUtility\getUrl
‪static string false getUrl($url)
Definition: GeneralUtility.php:1697
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:31
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Dashboard\Widgets
Definition: AdditionalCssInterface.php:18