]> git.agnieray.net Git - galette.git/commitdiff
Retrieve Galette (new) website RSS in current lang
authorJohan Cwiklinski <johan@x-tnd.be>
Wed, 27 May 2020 22:02:55 +0000 (00:02 +0200)
committerJohan Cwiklinski <johan@x-tnd.be>
Thu, 28 May 2020 18:52:14 +0000 (20:52 +0200)
galette/lib/Galette/IO/News.php
tests/Galette/IO/tests/units/News.php

index 152ca3b5401a6550912e22c56e6aa25b8d359e62..25f237b1d4a9ff80f1ff80f33d67fe148ad6cccd 100644 (file)
@@ -59,6 +59,11 @@ class News
     private $cache_timeout = 24;
     private $feed_url = null;
     private $posts = [];
+    private $stream_opts = [
+        'http' => [
+            'timeout' => 5
+        ]
+    ];
 
     /**
      * Default constructor
@@ -68,7 +73,7 @@ class News
      */
     public function __construct($url, $nocache = false)
     {
-        $this->feed_url = $url;
+        $this->feed_url = $this->getFeedURL($url);
 
         //only if cache should be used
         if ($nocache === false && GALETTE_MODE !== 'DEV') {
@@ -190,12 +195,7 @@ class News
                 );
             }
 
-            $opts = [
-                'http' => [
-                    'timeout' => 5
-                ]
-            ];
-            $context = stream_context_create($opts);
+            $context = stream_context_create($this->stream_opts);
             $data = file_get_contents($this->feed_url, false, $context);
             if (!$data) {
                 throw new \Exception();
@@ -257,4 +257,35 @@ class News
     {
         return $this->posts;
     }
+
+    /**
+     * Get feed url, handle Galette website to check available langs
+     *
+     * @param string $url Requested URL
+     *
+     * @return string
+     */
+    public function getFeedURL($url)
+    {
+        global $i18n;
+
+        if (strpos($url, 'galette.eu') !== false || trim($url) == '') {
+            $url = 'https://galette.eu/site';
+        } elseif (strpos($url, 'localhost:4000') !== false) {
+            $url = 'http://localhost:4000/site';
+        } else {
+            return $url;
+        }
+
+        $galette_website_langs = $url . '/langs.json';
+        $context = stream_context_create($this->stream_opts);
+        $langs = json_decode(file_get_contents($galette_website_langs, false, $context));
+
+        if ($i18n->getAbbrev() != 'en' && in_array($i18n->getAbbrev(), $langs)) {
+            $url .= '/' . $i18n->getAbbrev();
+        }
+        $url .= '/feed.xml';
+
+        return $url;
+    }
 }
index cd2f21e7f7fc379896b1d14c32efdf60b6c8cfa9..a46e7a179e9950d2e8e1d88324ed30d71c983211 100644 (file)
@@ -53,6 +53,22 @@ use \atoum;
  */
 class News extends atoum
 {
+    private $i18n;
+
+    /**
+     * Set up tests
+     *
+     * @param string $testMethod Method name
+     *
+     * @return void
+     */
+    public function beforeTestMethod($testMethod)
+    {
+        $this->i18n = new \Galette\Core\I18n();
+        global $i18n;
+        $i18n = $this->i18n;
+    }
+
     /**
      * Test news loading
      *
@@ -63,7 +79,7 @@ class News extends atoum
         //ensure allow_url_fopen is on
         ini_set('allow_url_fopen', true);
         //load news without caching
-        $news = new \Galette\IO\News('http://galette.eu/dc/index.php/feed/atom', true);
+        $news = new \Galette\IO\News('https://galette.eu/site/feed.xml', true);
         $posts = $news->getPosts();
         $this->array($posts)
             ->size->isGreaterThan(0);
@@ -76,13 +92,14 @@ class News extends atoum
      */
     public function testCacheNews()
     {
-        $file = GALETTE_CACHE_DIR . md5('http://galette.eu/dc/index.php/feed/atom') . '.cache';
+        //will use default lang to build RSS URL
+        $file = GALETTE_CACHE_DIR . md5('https://galette.eu/site/fr/feed.xml') . '.cache';
 
         //ensure file does not exists
         $this->boolean(file_exists($file))->isFalse;
 
         //load news with caching
-        $news = new \Galette\IO\News('http://galette.eu/dc/index.php/feed/atom');
+        $news = new \Galette\IO\News('https://galette.eu/site/feed.xml');
 
         $posts = $news->getPosts();
         $this->array($posts)
@@ -106,7 +123,7 @@ class News extends atoum
         $touched = touch($file, $expired->getTimestamp());
         $this->boolean($touched)->isTrue;
 
-        $news = new \Galette\IO\News('http://galette.eu/dc/index.php/feed/atom');
+        $news = new \Galette\IO\News('https://galette.eu/site/feed.xml');
         $mnewdate = \DateTime::createFromFormat(
             $dformat,
             date(
@@ -131,7 +148,7 @@ class News extends atoum
     {
         $this->assert('News cannot be loaded')
             ->if($this->function->ini_get = 0)
-            ->given($news = new \Galette\IO\News('http://galette.eu/dc/index.php/feed/atom', true))
+            ->given($news = new \Galette\IO\News('https://galette.eu/site/feed.xml', true))
             ->then
                 ->array($news->getPosts())
                 ->hasSize(0);