4 * Copyright © 2003-2024 The Galette Team
6 * This file is part of Galette (https://galette.eu).
8 * Galette is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * Galette is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
24 use Galette\Core\Galette
;
25 use Galette\Features\Cacheable
;
30 * News class from rss feed for galette
32 * @author Johan Cwiklinski <johan@x-tnd.be>
38 protected string $cache_filename = '%feed.cache';
39 private int $show = 10;
40 private ?
string $feed_url = null;
41 /** @var array<int, array<string, string>> */
42 private array $posts = [];
43 /** @var array<string, array<string, int|string>> */
44 private array $stream_opts = [
53 * @param string $url Feed URL
54 * @param boolean $nocache Do not try to cache
56 public function __construct(string $url, bool $nocache = false)
58 $this->feed_url
= $this->getFeedURL($url);
60 //only if cache should be used
61 if ($nocache === false && !Galette
::isDebugEnabled()) {
62 $this->handleCache($nocache);
73 protected function getDataTocache(): string
75 return Galette
::jsonEncode(
81 * Called once cache has been loaded.
83 * @param mixed $contents Content from cache
87 protected function cacheLoaded(mixed $contents): bool
89 if (Galette
::isSerialized($contents)) {
91 $this->posts
= unserialize($contents);
93 $this->posts
= Galette
::jsonDecode($contents);
96 //check if posts were cached
97 if (count($this->posts
) == 0) {
106 * Complete path to cache file
110 protected function getCacheFilename(): string
112 return GALETTE_CACHE_DIR
. str_replace(
114 md5($this->feed_url
),
115 $this->cache_filename
124 private function parseFeed(): void
127 if (!$this->allowURLFOpen()) {
128 throw new \
RuntimeException(
129 'allow_url_fopen is set to false; cannot load news.'
133 $context = stream_context_create($this->stream_opts
);
134 $data = file_get_contents($this->feed_url
, false, $context);
136 throw new \
Exception();
139 $xml = simplexml_load_string($data);
141 throw new \
Exception();
146 if (isset($xml->entry
)) {
147 //Reading an atom feed
148 foreach ($xml->entry
as $post) {
150 'title' => (string)$post->title
,
151 'url' => (string)$post->link
['href'],
152 'date' => (string)$post->published
154 if (count($posts) == $this->show
) {
158 } elseif (isset($xml->channel
->item
)) {
160 foreach ($xml->channel
->item
as $post) {
162 'title' => (string)$post->title
,
163 'url' => (string)$post->link
,
164 'date' => (string)$post->pubDate
166 if (count($posts) == $this->show
) {
171 throw new \
RuntimeException(
175 $this->posts
= $posts;
176 } catch (Throwable
$e) {
178 'Unable to load feed from "' . $this->feed_url
.
179 '" :( | ' . $e->getMessage(),
188 * @return array<int, array<string, string>>
190 public function getPosts(): array
196 * Get feed url, handle Galette website to check available langs
198 * @param string $url Requested URL
202 public function getFeedURL(string $url): string
206 if (strpos($url, 'galette.eu') !== false ||
trim($url) == '') {
207 $url = 'https://galette.eu/site';
208 } elseif (strpos($url, 'localhost:4000') !== false) {
209 $url = 'http://localhost:4000/site';
215 $galette_website_langs = $url . '/langs.json';
216 $context = stream_context_create($this->stream_opts
);
217 $langs = json_decode(file_get_contents($galette_website_langs, false, $context));
219 if ($i18n->getAbbrev() != 'en' && in_array($i18n->getAbbrev(), $langs)) {
220 $url .= '/' . $i18n->getAbbrev();
223 } catch (Throwable
$e) {
225 'Unable to load feed languages from "' . $url .
226 '" :( | ' . $e->getMessage(),
235 * Check if allow_url_fopen is enabled
239 protected function allowURLFOpen(): bool
241 return ini_get('allow_url_fopen');
245 * Ensure data to cache are present
249 protected function prepareForCache(): void