1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
<?php
/**
*/
date_default_timezone_set(@date_default_timezone_get());
/**
* @param string $locale
*
* @return array
*/
function html_news($locale = 'en', $count = 5)
{
$news = get_news($locale, $count);
$html = '<ul class="news">';
$item_tmpl = '<li><a href="%2$s">%3$s</a> <span class="dt">%1$s</span></li>';
foreach ($news as $item)
{
$html .= sprintf($item_tmpl,
news_date($item['date'], $locale),
$item['link'],
$item['title']
);
}
$html .= '</ul>';
return $html;
}
/**
* @param string $dt date in the form ISO 8601 (2015-02-24T15:42:00+01:00)
* @param string $locale
*
* @return string localised date
*/
function news_date($dt, $locale = 'en')
{
$eng_months = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
$localised_months = array(_r('Jan'), _r('Feb'), _r('Mar'), _r('Apr'), _r('May'), _r('Jun'), _r('Jul'), _r('Aug'), _r('Sep'), _r('Oct'), _r('Nov'), _r('Dec'));
$date_format = _r('M d Y');
if ((strpbrk($date_format, 'dj') == false) // check valid day formats
|| (strpbrk($date_format, 'M') == false) // check valid month formats (all are 'FmMn' but we replace them in 3 letter form)
|| (strpbrk($date_format, 'oYy') == false)) // check valid year formats
{
$date_format = 'M d Y'; // fallback for invalid date format localisation
}
return str_replace($eng_months, $localised_months, date($date_format, strtotime($dt)));
}
/**
* @param string $locale
* @param integer $count
* @param integer $cache_timeout
*
* @return array
*/
function get_news($locale = 'en', $count = 5, $cache_timeout = 5)
{
return get_feed(blog_link($locale), $count, cache_timeout);
}
/**
*/
function get_feed($url, $count = 5, $cache_timeout = 5)
{
include_once G_APP_ROOT . '/lib/simplepie/simplepie.inc';
$feed = new SimplePie();
$feed->set_feed_url($url);
$feed->set_cache_location(realpath(G_APP_ROOT . '/_nav/var/tmp/cache'));
$feed->set_cache_duration(3600 * $cache_timeout);
$feed->set_timeout(2);
$feed->enable_order_by_date(true);
$feed->init();
$feed->handle_content_type();
$items = array();
foreach ($feed->get_items(0, $count) as $item)
{
$items[] = array(
'source' => $item->get_feed()->get_link(),
'link' => $item->get_permalink(),
'title' => $item->get_title(),
'date' => $item->get_date('c'),
'desc' => $item->get_description(),
'author' => $item->get_author()
);
}
unset($feed);
return $items;
}
/**
*
* @param string $locale locale this feed is expected to be in
* @param string $news_title feed title
* @param string $link feed main site title
* @param string $feed feed url
* @param integer $count how many items to return
* @param string $skip url whose element we want to skip (why?)
* @param boolean split title that has been built by aggregator
* @param string $discrete_title more discrete title
* @param array $data if get_feed() is called outside, it's result is passed
*
* @return string
*/
function show_feed($locale, $news_title, $link, $feed, $count = 5, $skip = null, $split = false, $discrete_title = null, $data = null) {
if (!is_null($skip))
$count += 5;
if(is_null($data)) {
$data = get_feed($feed, $count);
if(0 == count($data)) {
$feed = rtrim($feed, '10'); // cut 10 as a fallback (type=rss)
$data = get_feed($feed, $count);
}
if(0 == count($data)) {
$feed = $feed .'10'; // add 10 as a fallback (type=rss10)
$data = get_feed($feed, $count);
}
}
if(0 == count($data)) {
return;
}
$s = '';
$header = '';
$source = null;
$date_separator = (is_null($discrete_title) ? null : '| ');
$s .= '<ul id="newslist">';
foreach ($data as $d) {
if (!is_null($skip) && strpos($d['link'], $skip) !== false)
continue;
if ($split && strpos($d['title'], ') : ') !== false) {
$title = explode(') : ', html_entity_decode($d['title']));
$source = array_shift($title) . '): ';
$source = (is_null($date_separator) ? $source : null);
$title = implode(' : ', $title);
} else {
$title = $d['title'];
$source = null;
}
$s .= sprintf('<li>%s<a href="%s">%s%s</a> %s<span class="dt">%s</span></li>',
$discrete_title, $d['link'], $source, $title, $date_separator, news_date($d['date'], $locale));
$discrete_title = null;
}
$s .= '</ul>';
if (is_null($date_separator)) {
$header = sprintf('<h2><a href="%s">%s</a></h2>', $link, $news_title);
}
$s = $header . $s;
echo $s;
}
/**
* @param string $eng_planet_url = 'https://planet.mageia.org/en/'
* @param string $eng_blog_url = 'https://blog.mageia.org/en/'
* @param string $loc_planet_url = _r('https://planet.mageia.org/en/')
* @param string $loc_blog_url = _r('https://blog.mageia.org/en/')
* @param integer $req_quantity number or requested planet/blog titles
* @param integer $min_num_of_loc_post minimum number of localised posts to return without adding English ones
*
* @return array ($loc_posts_title for show_feed() function, $fresh_loc_posts, $num_fresh_loc_posts, $eng_posts_title for show_feed() function, $fresh_eng_posts)
*/
function prepare_fresh_blog_post_titles($eng_planet_url, $eng_blog_url, $loc_planet_url, $loc_blog_url, $req_quantity = 9, $min_num_of_loc_post = 4)
{
$max_posts_age_days = 90;
$posts_age_limit = time()-($max_posts_age_days * 24*60*60);
$fresh_loc_posts = array();
$fresh_eng_posts = array();
$loc_posts_title = '';
$eng_posts_title = '';
// prepare fresh enough localised planet posts if they exist
if ($eng_planet_url != $loc_planet_url) {
$loc_planet_posts = get_feed($loc_planet_url . '?type=rss10', $req_quantity);
if (count($loc_planet_posts) > 0) {
$loc_posts_title = _r('Planet');
foreach ($loc_planet_posts as $planet_post) {
if ($posts_age_limit < strtotime($planet_post["date"])) {
$planet_post["desc"] = ''; // not needed (conserving memory)
$fresh_loc_posts[] = $planet_post;
}
}
}
}
// prepare fresh enough localised blog posts if there's no localised planet
if(0 == count($fresh_loc_posts) && $eng_blog_url != $loc_blog_url) {
$loc_blog_posts = get_feed($loc_blog_url . '?type=rss10', $req_quantity);
if (count($loc_blog_posts) > 0) {
$loc_posts_title = _r('Blog');
foreach ($loc_blog_posts as $blog_post) {
if ($posts_age_limit < strtotime($blog_post["date"])) {
$blog_post["desc"] = ''; // not needed
$fresh_loc_posts[] = $blog_post;
}
}
}
}
// prepare fresh enough English planet posts if there's only a few or no localised posts
$num_fresh_loc_posts = count($fresh_loc_posts);
if ($min_num_of_loc_post > $num_fresh_loc_posts) {
if (0 < $num_fresh_loc_posts) {
$req_quantity = $req_quantity - $num_fresh_loc_posts - 1;
}
$eng_planet_posts = get_feed($eng_planet_url . '?type=rss10', $req_quantity);
if (count($eng_planet_posts) > 0) {
$eng_posts_title = _r('Planet');
foreach ($eng_planet_posts as $planet_post) {
if ($posts_age_limit < strtotime($planet_post["date"])) {
$planet_post["desc"] = ''; // not needed
$fresh_eng_posts[] = $planet_post;
}
}
}
// prepare fresh enough English blog posts if there's no English planet posts
if (0 == count($fresh_eng_posts)) {
$eng_blog_posts = get_feed($eng_blog_url . '?type=rss10', $req_quantity);
if (count($eng_blog_posts) > 1) {
$eng_posts_title = _r('Blog');
foreach ($eng_blog_posts as $blog_post) {
if ($posts_age_limit < strtotime($blog_post["date"])) {
$blog_post["desc"] = ''; // not needed
$fresh_eng_posts[] = $blog_post;
}
}
}
}
}
return array($loc_posts_title, $fresh_loc_posts, $num_fresh_loc_posts, $eng_posts_title, $fresh_eng_posts);
}
/**
*/
function blog_link($locale)
{
$news = array(
'en' => 'https://blog.mageia.org/en/',
'de' => 'https://blog.mageia.org/de/',
'el' => 'https://blog.mageia.org/el/',
'es' => 'https://blog.mageia.org/es/',
'fr' => 'https://blog.mageia.org/fr/',
'de' => 'https://blog.mageia.org/de/',
'it' => 'https://blog.mageia.org/it/',
'nl' => 'https://blog.mageia.org/nl/',
'pl' => 'https://blog.mageia.org/pl/',
'pt' => 'https://blog.mageia.org/pt/',
'pt-br'=>'https://blog.mageia.org/pt/',
'ro' => 'https://blog.mageia.org/ro/',
'ru' => 'https://blog.mageia.org/ru/',
'sv' => 'https://blog.mageia.org/sv/',
'tr' => 'https://blog.mageia.org/tr/',
'uk' => 'https://blog.mageia.org/uk/',
/* missing: et, fi, lv, nb, sl, zh-cn, zh-tw */
);
if (!array_key_exists($locale, $news))
$locale = 'en';
$source_url = $news[$locale];
return $source_url;
}
function planet_link($locale)
{
$planets = array(
'en',
'de',
'es',
'fr',
'it',
'pl',
'pt',
'pt-br');
$locale = in_array($locale, $planets) ? $locale : 'en';
return sprintf('https://planet.mageia.org/%s/', $locale);
}
|