aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorfilip <filip.komar@gmail.com>2018-04-27 20:03:03 +0200
committerfilip <filip.komar@gmail.com>2018-04-27 20:03:03 +0200
commit1e35475cee83c4e19f7c165011064ae77231924b (patch)
treeddc16f0c90d25f72faccc997c92fd435b3cfe13c
parentb379ed46dd9bf2ef5b7e8dd29a4c7b8b8259101b (diff)
show only recent planet&blog post
show localized planet if exists otherwise localized blog if number of localized titles is less then required add English planet and English blog as fallback
-rw-r--r--en/community/index.php19
-rwxr-xr-xen/index.php16
-rw-r--r--lib/news.php124
3 files changed, 138 insertions, 21 deletions
diff --git a/en/community/index.php b/en/community/index.php
index d2d84b7af..994597015 100644
--- a/en/community/index.php
+++ b/en/community/index.php
@@ -47,10 +47,21 @@ include '../../lib/news.php';
<div class="col">
<section>
<?php
- if ((strpos($locale, 'en') === 0) || (_r('https://planet.mageia.org/en/') != 'https://planet.mageia.org/en/')) {
- show_feed($locale, _r('News') . ' (' . _r('Planet') . ')', _r('https://planet.mageia.org/en/'), _r('https://planet.mageia.org/en/') . '?type=rss10', 9, null, true);
- } else {
- show_feed($locale, _r('News') . ' (' . _r('Blog') . ')', _r('https://blog.mageia.org/en/'), _r('https://blog.mageia.org/en/') . '?feed=rss', 9, null, true);
+ $loc_planet_url = _r('https://planet.mageia.org/en/');
+ $loc_blog_url = _r('https://blog.mageia.org/en/');
+ $fresh_blog_post_titles = prepare_fresh_blog_post_titles('https://planet.mageia.org/en/', 'https://blog.mageia.org/en/', $loc_planet_url, $loc_blog_url);
+ $loc_posts_title = $fresh_blog_post_titles[0];
+ $fresh_loc_posts = $fresh_blog_post_titles[1];
+ $num_fresh_loc_posts = $fresh_blog_post_titles[2];
+ $eng_posts_title = $fresh_blog_post_titles[3];
+ $fresh_eng_posts = $fresh_blog_post_titles[4];
+ $num_fresh_eng_posts = count($fresh_eng_posts);
+
+ if(0 != $num_fresh_loc_posts) {
+ show_feed($locale, _r('Localised news') . " ($loc_posts_title)", '', '', 0, null, true, null, $fresh_loc_posts);
+ }
+ if(0 != $num_fresh_eng_posts) {
+ show_feed($locale, _r('News in English') . " ($eng_posts_title)", '', '', 0, null, true, null, $fresh_eng_posts);
}
// don't work:
//show_feed('Mageia MLs', '', 'https://ml.mageia.org/l/rss/active_lists?count=20&for=10');
diff --git a/en/index.php b/en/index.php
index 8f4e2f130..3015df71b 100755
--- a/en/index.php
+++ b/en/index.php
@@ -233,10 +233,18 @@ $dictionary = read_translation_file($locale, array('index', 'news', 'common_foot
$prefix = '<span style="font-weight: bold;">';
$prefix .= sprintf(_r('Latest news from our <a href="%s">blog</a>:'), _r('https://blog.mageia.org/en/'));
$prefix .= '</span> ';
- if (_r('https://planet.mageia.org/en/') != 'https://planet.mageia.org/en/') {
- show_feed($locale, null, _r('https://planet.mageia.org/en/'), _r('https://planet.mageia.org/en/') . '?type=rss10', 1, null, true, $prefix);
- } else {
- show_feed($locale, null, _r('https://blog.mageia.org/en/'), _r('https://blog.mageia.org/en/') . '?feed=rss', 1, null, true, $prefix);
+ $loc_planet_url = _r('https://planet.mageia.org/en/');
+ $loc_blog_url = _r('https://blog.mageia.org/en/');
+ $fresh_blog_post_titles = prepare_fresh_blog_post_titles('https://planet.mageia.org/en/', 'https://blog.mageia.org/en/', $loc_planet_url, $loc_blog_url, 1, 1);
+ $fresh_loc_posts = $fresh_blog_post_titles[1];
+ $num_fresh_loc_posts = $fresh_blog_post_titles[2];
+ $fresh_eng_posts = $fresh_blog_post_titles[4];
+ $num_fresh_eng_posts = count($fresh_eng_posts);
+ if(0 != $num_fresh_loc_posts) {
+ show_feed($locale, null, '', '', 0, null, true, $prefix, $fresh_loc_posts);
+ }
+ if(0 != $num_fresh_eng_posts) {
+ show_feed($locale, null, '', '', 0, null, true, $prefix, $fresh_eng_posts);
}
?>
</div>
diff --git a/lib/news.php b/lib/news.php
index fa1852d14..e4b261081 100644
--- a/lib/news.php
+++ b/lib/news.php
@@ -103,22 +103,25 @@ function get_feed($url, $count = 5, $cache_timeout = 5)
* @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) {
+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;
- $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)
+ 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;
@@ -158,23 +161,110 @@ function show_feed($locale, $news_title, $link, $feed, $count = 5, $skip = null,
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(
- 'el' => 'https://blog.mageia.org/el/',
'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/',
- 'pl' => 'https://blog.mageia.org/en/', // pl when up to date
+ '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/en/', // ru, when up to date
+ 'ru' => 'https://blog.mageia.org/ru/',
+ 'sv' => 'https://blog.mageia.org/sv/',
'tr' => 'https://blog.mageia.org/tr/',
- /* missing: et, fi, lv, nb, nl, sl, zh-cn, zh-tw */
+ 'uk' => 'https://blog.mageia.org/uk/',
+ /* missing: et, fi, lv, nb, sl, zh-cn, zh-tw */
);
if (!array_key_exists($locale, $news))
@@ -187,7 +277,15 @@ function blog_link($locale)
function planet_link($locale)
{
- $planets = array('en', 'fr', 'de', 'es', 'it', 'pt');
+ $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);