]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Process Daily RSS feed through Slim controller
authorArthurHoaro <arthur@hoa.ro>
Sun, 17 May 2020 12:16:32 +0000 (14:16 +0200)
committerArthurHoaro <arthur@hoa.ro>
Thu, 23 Jul 2020 19:19:21 +0000 (21:19 +0200)
The daily RSS template has been entirely rewritten to handle the whole feed through the template engine.

15 files changed:
application/bookmark/Bookmark.php
application/bookmark/BookmarkFileService.php
application/container/ContainerBuilder.php
application/front/controllers/DailyController.php
application/http/HttpUtils.php
application/legacy/LegacyLinkDB.php
application/render/PageCacheManager.php
index.php
tests/front/controller/DailyControllerTest.php
tests/http/HttpUtils/IndexUrlTest.php
tests/render/PageCacheManagerTest.php
tpl/default/daily.html
tpl/default/dailyrss.html
tpl/vintage/daily.html
tpl/vintage/dailyrss.html

index 83ddab825abed20492cd37452f3cd446bb6b3de1..90ff5b16a44e2df3dced5cc840dd26a40383a601 100644 (file)
@@ -3,6 +3,7 @@
 namespace Shaarli\Bookmark;
 
 use DateTime;
+use DateTimeInterface;
 use Shaarli\Bookmark\Exception\InvalidBookmarkException;
 
 /**
@@ -42,10 +43,10 @@ class Bookmark
     /** @var bool Set to true if the bookmark is set as sticky */
     protected $sticky;
 
-    /** @var DateTime Creation datetime */
+    /** @var DateTimeInterface Creation datetime */
     protected $created;
 
-    /** @var DateTime Update datetime */
+    /** @var DateTimeInterface datetime */
     protected $updated;
 
     /** @var bool True if the bookmark can only be seen while logged in */
@@ -100,7 +101,7 @@ class Bookmark
             || ! is_int($this->id)
             || empty($this->shortUrl)
             || empty($this->created)
-            || ! $this->created instanceof DateTime
+            || ! $this->created instanceof DateTimeInterface
         ) {
             throw new InvalidBookmarkException($this);
         }
@@ -188,7 +189,7 @@ class Bookmark
     /**
      * Get the Created.
      *
-     * @return DateTime
+     * @return DateTimeInterface
      */
     public function getCreated()
     {
@@ -198,7 +199,7 @@ class Bookmark
     /**
      * Get the Updated.
      *
-     * @return DateTime
+     * @return DateTimeInterface
      */
     public function getUpdated()
     {
@@ -270,7 +271,7 @@ class Bookmark
      * Set the Created.
      * Note: you shouldn't set this manually except for special cases (like bookmark import)
      *
-     * @param DateTime $created
+     * @param DateTimeInterface $created
      *
      * @return Bookmark
      */
@@ -284,7 +285,7 @@ class Bookmark
     /**
      * Set the Updated.
      *
-     * @param DateTime $updated
+     * @param DateTimeInterface $updated
      *
      * @return Bookmark
      */
index 3b3812afcea9512fe15049f286077252969a76e7..7439d8d8d830bf7de9e56d9c342085c923f894e5 100644 (file)
@@ -53,7 +53,7 @@ class BookmarkFileService implements BookmarkServiceInterface
     {
         $this->conf = $conf;
         $this->history = $history;
-        $this->pageCacheManager = new PageCacheManager($this->conf->get('resource.page_cache'));
+        $this->pageCacheManager = new PageCacheManager($this->conf->get('resource.page_cache'), $isLoggedIn);
         $this->bookmarksIO = new BookmarkIO($this->conf);
         $this->isLoggedIn = $isLoggedIn;
 
index c5c4a2c3a2c15ba135ffc8418f051f0f12996f4e..199f3f67303c2d0e93a5727ea2af275c7a876e6f 100644 (file)
@@ -94,7 +94,10 @@ class ContainerBuilder
         };
 
         $container['pageCacheManager'] = function (ShaarliContainer $container): PageCacheManager {
-            return new PageCacheManager($container->conf->get('resource.page_cache'));
+            return new PageCacheManager(
+                $container->conf->get('resource.page_cache'),
+                $container->loginManager->isLoggedIn()
+            );
         };
 
         return $container;
index 271c0ee25147e80db2286e2a9714af2fc2431281..4a0735aa60e42929204cef4a81aff6b640607df6 100644 (file)
@@ -5,6 +5,7 @@ declare(strict_types=1);
 namespace Shaarli\Front\Controller;
 
 use DateTime;
+use DateTimeImmutable;
 use Shaarli\Bookmark\Bookmark;
 use Slim\Http\Request;
 use Slim\Http\Response;
@@ -18,6 +19,8 @@ use Slim\Http\Response;
  */
 class DailyController extends ShaarliController
 {
+    public static $DAILY_RSS_NB_DAYS = 8;
+
     /**
      * Controller displaying all bookmarks published in a single day.
      * It take a `day` date query parameter (format YYYYMMDD).
@@ -87,6 +90,77 @@ class DailyController extends ShaarliController
         return $response->write($this->render('daily'));
     }
 
+    /**
+     * Daily RSS feed: 1 RSS entry per day giving all the bookmarks on that day.
+     * Gives the last 7 days (which have bookmarks).
+     * This RSS feed cannot be filtered and does not trigger plugins yet.
+     */
+    public function rss(Request $request, Response $response): Response
+    {
+        $response = $response->withHeader('Content-Type', 'application/rss+xml; charset=utf-8');
+
+        $pageUrl = page_url($this->container->environment);
+        $cache = $this->container->pageCacheManager->getCachePage($pageUrl);
+
+        $cached = $cache->cachedVersion();
+        if (!empty($cached)) {
+            return $response->write($cached);
+        }
+
+        $days = [];
+        foreach ($this->container->bookmarkService->search() as $bookmark) {
+            $day = $bookmark->getCreated()->format('Ymd');
+
+            // Stop iterating after DAILY_RSS_NB_DAYS entries
+            if (count($days) === static::$DAILY_RSS_NB_DAYS && !isset($days[$day])) {
+                break;
+            }
+
+            $days[$day][] = $bookmark;
+        }
+
+        // Build the RSS feed.
+        $indexUrl = escape(index_url($this->container->environment));
+
+        $formatter = $this->container->formatterFactory->getFormatter();
+        $formatter->addContextData('index_url', $indexUrl);
+
+        $dataPerDay = [];
+
+        /** @var Bookmark[] $bookmarks */
+        foreach ($days as $day => $bookmarks) {
+            $dayDatetime = DateTimeImmutable::createFromFormat(Bookmark::LINK_DATE_FORMAT, $day.'_000000');
+            $dataPerDay[$day] = [
+                'date' => $dayDatetime,
+                'date_rss' => $dayDatetime->format(DateTime::RSS),
+                'date_human' => format_date($dayDatetime, false, true),
+                'absolute_url' => $indexUrl . '/daily?day=' . $day,
+                'links' => [],
+            ];
+
+            foreach ($bookmarks as $key => $bookmark) {
+                $dataPerDay[$day]['links'][$key] = $formatter->format($bookmark);
+
+                // Make permalink URL absolute
+                if ($bookmark->isNote()) {
+                    $dataPerDay[$day]['links'][$key]['url'] = $indexUrl . $bookmark->getUrl();
+                }
+            }
+        }
+
+        $this->assignView('title', $this->container->conf->get('general.title', 'Shaarli'));
+        $this->assignView('index_url', $indexUrl);
+        $this->assignView('page_url', $pageUrl);
+        $this->assignView('hide_timestamps', $this->container->conf->get('privacy.hide_timestamps', false));
+        $this->assignView('days', $dataPerDay);
+
+        $rssContent = $this->render('dailyrss');
+
+        $cache->cache($rssContent);
+
+        return $response->write($rssContent);
+    }
+
     /**
      * We need to spread the articles on 3 columns.
      * did not want to use a JavaScript lib like http://masonry.desandro.com/
index 2ea9195d3550bfd4bfd912ef35cb4b669a5c83c2..f00c4336ad07fb24d3a798fc123f1dad24dbec77 100644 (file)
@@ -369,7 +369,7 @@ function server_url($server)
  */
 function index_url($server)
 {
-    $scriptname = $server['SCRIPT_NAME'];
+    $scriptname = $server['SCRIPT_NAME'] ?? '';
     if (endsWith($scriptname, 'index.php')) {
         $scriptname = substr($scriptname, 0, -9);
     }
@@ -377,7 +377,7 @@ function index_url($server)
 }
 
 /**
- * Returns the absolute URL of the current script, with the query
+ * Returns the absolute URL of the current script, with current route and query
  *
  * If the resource is "index.php", then it is removed (for better-looking URLs)
  *
@@ -387,10 +387,17 @@ function index_url($server)
  */
 function page_url($server)
 {
+    $scriptname = $server['SCRIPT_NAME'] ?? '';
+    if (endsWith($scriptname, 'index.php')) {
+        $scriptname = substr($scriptname, 0, -9);
+    }
+
+    $route = ltrim($server['REQUEST_URI'] ?? '', $scriptname);
     if (! empty($server['QUERY_STRING'])) {
-        return index_url($server).'?'.$server['QUERY_STRING'];
+        return index_url($server) . $route . '?' . $server['QUERY_STRING'];
     }
-    return index_url($server);
+
+    return index_url($server) . $route;
 }
 
 /**
index 947005adddb6184ce807a105ba1344f50cb4c453..7bf76fd471087fe0477b935b4cb1bf771ae1ab46 100644 (file)
@@ -353,7 +353,7 @@ You use the community supported version of the original Shaarli project, by Seba
 
         $this->write();
 
-        $pageCacheManager = new PageCacheManager($pageCacheDir);
+        $pageCacheManager = new PageCacheManager($pageCacheDir, $this->loggedIn);
         $pageCacheManager->invalidateCaches();
     }
 
index bd91fe0d3d54fbb2fb22d0bf71171e23ab4b9900..97805c3524605bae07b30cb06b4c935517c8126c 100644 (file)
@@ -2,6 +2,8 @@
 
 namespace Shaarli\Render;
 
+use Shaarli\Feed\CachedPage;
+
 /**
  * Cache utilities
  */
@@ -10,9 +12,13 @@ class PageCacheManager
     /** @var string Cache directory */
     protected $pageCacheDir;
 
-    public function __construct(string $pageCacheDir)
+    /** @var bool */
+    protected $isLoggedIn;
+
+    public function __construct(string $pageCacheDir, bool $isLoggedIn)
     {
         $this->pageCacheDir = $pageCacheDir;
+        $this->isLoggedIn = $isLoggedIn;
     }
 
     /**
@@ -42,4 +48,13 @@ class PageCacheManager
         // Purge page cache shared by sessions.
         $this->purgeCachedPages();
     }
+
+    public function getCachePage(string $pageUrl): CachedPage
+    {
+        return new CachedPage(
+            $this->pageCacheDir,
+            $pageUrl,
+            false === $this->isLoggedIn
+        );
+    }
 }
index dbb76d3f37638e210bf71acfd613f1516ad859b2..9dc67c4b35926f608d44719250f3be9437de2f1c 100644 (file)
--- a/index.php
+++ b/index.php
@@ -300,104 +300,6 @@ if (!isset($_SESSION['tokens'])) {
     $_SESSION['tokens']=array();  // Token are attached to the session.
 }
 
-/**
- * Daily RSS feed: 1 RSS entry per day giving all the bookmarks on that day.
- * Gives the last 7 days (which have bookmarks).
- * This RSS feed cannot be filtered.
- *
- * @param BookmarkServiceInterface $bookmarkService
- * @param ConfigManager            $conf            Configuration Manager instance
- * @param LoginManager             $loginManager    LoginManager instance
- */
-function showDailyRSS($bookmarkService, $conf, $loginManager)
-{
-    // Cache system
-    $query = $_SERVER['QUERY_STRING'];
-    $cache = new CachedPage(
-        $conf->get('config.PAGE_CACHE'),
-        page_url($_SERVER),
-        startsWith($query, 'do=dailyrss') && !$loginManager->isLoggedIn()
-    );
-    $cached = $cache->cachedVersion();
-    if (!empty($cached)) {
-        echo $cached;
-        exit;
-    }
-
-    /* Some Shaarlies may have very few bookmarks, so we need to look
-       back in time until we have enough days ($nb_of_days).
-    */
-    $nb_of_days = 7; // We take 7 days.
-    $today = date('Ymd');
-    $days = array();
-
-    foreach ($bookmarkService->search() as $bookmark) {
-        $day = $bookmark->getCreated()->format('Ymd'); // Extract day (without time)
-        if (strcmp($day, $today) < 0) {
-            if (empty($days[$day])) {
-                $days[$day] = array();
-            }
-            $days[$day][] = $bookmark;
-        }
-
-        if (count($days) > $nb_of_days) {
-            break; // Have we collected enough days?
-        }
-    }
-
-    // Build the RSS feed.
-    header('Content-Type: application/rss+xml; charset=utf-8');
-    $pageaddr = escape(index_url($_SERVER));
-    echo '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0">';
-    echo '<channel>';
-    echo '<title>Daily - '. $conf->get('general.title') . '</title>';
-    echo '<link>'. $pageaddr .'</link>';
-    echo '<description>Daily shared bookmarks</description>';
-    echo '<language>en-en</language>';
-    echo '<copyright>'. $pageaddr .'</copyright>'. PHP_EOL;
-
-    $factory = new FormatterFactory($conf, $loginManager->isLoggedIn());
-    $formatter = $factory->getFormatter();
-    $formatter->addContextData('index_url', index_url($_SERVER));
-    // For each day.
-    /** @var Bookmark[] $bookmarks */
-    foreach ($days as $day => $bookmarks) {
-        $formattedBookmarks = [];
-        $dayDate = DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, $day.'_000000');
-        $absurl = escape(index_url($_SERVER).'?do=daily&day='.$day);  // Absolute URL of the corresponding "Daily" page.
-
-        // We pre-format some fields for proper output.
-        foreach ($bookmarks as $key => $bookmark) {
-            $formattedBookmarks[$key] = $formatter->format($bookmark);
-            // This page is a bit specific, we need raw description to calculate the length
-            $formattedBookmarks[$key]['formatedDescription'] = $formattedBookmarks[$key]['description'];
-            $formattedBookmarks[$key]['description'] = $bookmark->getDescription();
-
-            if ($bookmark->isNote()) {
-                $link['url'] = index_url($_SERVER) . $bookmark->getUrl();  // make permalink URL absolute
-            }
-        }
-
-        // Then build the HTML for this day:
-        $tpl = new RainTPL();
-        $tpl->assign('title', $conf->get('general.title'));
-        $tpl->assign('daydate', $dayDate->getTimestamp());
-        $tpl->assign('absurl', $absurl);
-        $tpl->assign('links', $formattedBookmarks);
-        $tpl->assign('rssdate', escape($dayDate->format(DateTime::RSS)));
-        $tpl->assign('hide_timestamps', $conf->get('privacy.hide_timestamps', false));
-        $tpl->assign('index_url', $pageaddr);
-        $html = $tpl->draw('dailyrss', true);
-
-        echo $html . PHP_EOL;
-    }
-    echo '</channel></rss><!-- Cached version of '. escape(page_url($_SERVER)) .' -->';
-
-    $cache->cache(ob_get_contents());
-    ob_end_flush();
-    exit;
-}
-
 /**
  * Renders the linklist
  *
@@ -424,7 +326,7 @@ function showLinkList($PAGE, $linkDb, $conf, $pluginManager, $loginManager)
  */
 function renderPage($conf, $pluginManager, $bookmarkService, $history, $sessionManager, $loginManager)
 {
-    $pageCacheManager = new PageCacheManager($conf->get('resource.page_cache'));
+    $pageCacheManager = new PageCacheManager($conf->get('resource.page_cache'), $loginManager->isLoggedIn());
     $updater = new Updater(
         UpdaterUtils::read_updates_file($conf->get('resource.updates')),
         $bookmarkService,
@@ -1715,7 +1617,7 @@ try {
 $linkDb = new BookmarkFileService($conf, $history, $loginManager->isLoggedIn());
 
 if (isset($_SERVER['QUERY_STRING']) && startsWith($_SERVER['QUERY_STRING'], 'do=dailyrss')) {
-    showDailyRSS($linkDb, $conf, $loginManager);
+    header('Location: ./daily-rss');
     exit;
 }
 
@@ -1747,6 +1649,7 @@ $app->group('', function () {
     $this->get('/tag-cloud', '\Shaarli\Front\Controller\TagCloudController:cloud')->setName('tagcloud');
     $this->get('/tag-list', '\Shaarli\Front\Controller\TagCloudController:list')->setName('taglist');
     $this->get('/daily', '\Shaarli\Front\Controller\DailyController:index')->setName('daily');
+    $this->get('/daily-rss', '\Shaarli\Front\Controller\DailyController:rss')->setName('dailyrss');
 
     $this->get('/add-tag/{newTag}', '\Shaarli\Front\Controller\TagController:addTag')->setName('add-tag');
 })->add('\Shaarli\Front\ShaarliMiddleware');
index 2714bfd9e1c44d37281384d4c2938a8d62eaa57d..72a0339f91b6147580e275c499eb19ffecd746ad 100644 (file)
@@ -9,11 +9,13 @@ use Shaarli\Bookmark\Bookmark;
 use Shaarli\Bookmark\BookmarkServiceInterface;
 use Shaarli\Config\ConfigManager;
 use Shaarli\Container\ShaarliContainer;
+use Shaarli\Feed\CachedPage;
 use Shaarli\Formatter\BookmarkFormatter;
 use Shaarli\Formatter\BookmarkRawFormatter;
 use Shaarli\Formatter\FormatterFactory;
 use Shaarli\Plugin\PluginManager;
 use Shaarli\Render\PageBuilder;
+use Shaarli\Render\PageCacheManager;
 use Shaarli\Security\LoginManager;
 use Slim\Http\Request;
 use Slim\Http\Response;
@@ -30,9 +32,10 @@ class DailyControllerTest extends TestCase
     {
         $this->container = $this->createMock(ShaarliContainer::class);
         $this->controller = new DailyController($this->container);
+        DailyController::$DAILY_RSS_NB_DAYS = 2;
     }
 
-    public function testValidControllerInvokeDefault(): void
+    public function testValidIndexControllerInvokeDefault(): void
     {
         $this->createValidContainerMockSet();
 
@@ -173,7 +176,7 @@ class DailyControllerTest extends TestCase
     /**
      * Daily page - test that everything goes fine with no future or past bookmarks
      */
-    public function testValidControllerInvokeNoFutureOrPast(): void
+    public function testValidIndexControllerInvokeNoFutureOrPast(): void
     {
         $this->createValidContainerMockSet();
 
@@ -247,7 +250,7 @@ class DailyControllerTest extends TestCase
     /**
      * Daily page - test that height adjustment in columns is working
      */
-    public function testValidControllerInvokeHeightAdjustment(): void
+    public function testValidIndexControllerInvokeHeightAdjustment(): void
     {
         $this->createValidContainerMockSet();
 
@@ -318,7 +321,7 @@ class DailyControllerTest extends TestCase
     /**
      * Daily page - no bookmark
      */
-    public function testValidControllerInvokeNoBookmark(): void
+    public function testValidIndexControllerInvokeNoBookmark(): void
     {
         $this->createValidContainerMockSet();
 
@@ -364,6 +367,136 @@ class DailyControllerTest extends TestCase
         static::assertEquals((new \DateTime())->setTime(0, 0), $assignedVariables['dayDate']);
     }
 
+    /**
+     * Daily RSS - default behaviour
+     */
+    public function testValidRssControllerInvokeDefault(): void
+    {
+        $this->createValidContainerMockSet();
+
+        $dates = [
+            new \DateTimeImmutable('2020-05-17'),
+            new \DateTimeImmutable('2020-05-15'),
+            new \DateTimeImmutable('2020-05-13'),
+        ];
+
+        $request = $this->createMock(Request::class);
+        $response = new Response();
+
+        $this->container->bookmarkService->expects(static::once())->method('search')->willReturn([
+            (new Bookmark())->setId(1)->setCreated($dates[0])->setUrl('http://domain.tld/1'),
+            (new Bookmark())->setId(2)->setCreated($dates[1])->setUrl('http://domain.tld/2'),
+            (new Bookmark())->setId(3)->setCreated($dates[1])->setUrl('http://domain.tld/3'),
+            (new Bookmark())->setId(4)->setCreated($dates[2])->setUrl('http://domain.tld/4'),
+        ]);
+
+        $this->container->pageCacheManager
+            ->expects(static::once())
+            ->method('getCachePage')
+            ->willReturnCallback(function (): CachedPage {
+                $cachedPage = $this->createMock(CachedPage::class);
+                $cachedPage->expects(static::once())->method('cache')->with('dailyrss');
+
+                return $cachedPage;
+            }
+        );
+
+        // Save RainTPL assigned variables
+        $assignedVariables = [];
+        $this->assignTemplateVars($assignedVariables);
+
+        $result = $this->controller->rss($request, $response);
+
+        static::assertSame(200, $result->getStatusCode());
+        static::assertStringContainsString('application/rss', $result->getHeader('Content-Type')[0]);
+        static::assertSame('dailyrss', (string) $result->getBody());
+        static::assertSame('Shaarli', $assignedVariables['title']);
+        static::assertSame('http://shaarli', $assignedVariables['index_url']);
+        static::assertSame('http://shaarli/daily-rss', $assignedVariables['page_url']);
+        static::assertFalse($assignedVariables['hide_timestamps']);
+        static::assertCount(2, $assignedVariables['days']);
+
+        $day = $assignedVariables['days'][$dates[0]->format('Ymd')];
+
+        static::assertEquals($dates[0], $day['date']);
+        static::assertSame($dates[0]->format(\DateTimeInterface::RSS), $day['date_rss']);
+        static::assertSame(format_date($dates[0], false), $day['date_human']);
+        static::assertSame('http://shaarli/daily?day='. $dates[0]->format('Ymd'), $day['absolute_url']);
+        static::assertCount(1, $day['links']);
+        static::assertSame(1, $day['links'][0]['id']);
+        static::assertSame('http://domain.tld/1', $day['links'][0]['url']);
+        static::assertEquals($dates[0], $day['links'][0]['created']);
+
+        $day = $assignedVariables['days'][$dates[1]->format('Ymd')];
+
+        static::assertEquals($dates[1], $day['date']);
+        static::assertSame($dates[1]->format(\DateTimeInterface::RSS), $day['date_rss']);
+        static::assertSame(format_date($dates[1], false), $day['date_human']);
+        static::assertSame('http://shaarli/daily?day='. $dates[1]->format('Ymd'), $day['absolute_url']);
+        static::assertCount(2, $day['links']);
+
+        static::assertSame(2, $day['links'][0]['id']);
+        static::assertSame('http://domain.tld/2', $day['links'][0]['url']);
+        static::assertEquals($dates[1], $day['links'][0]['created']);
+        static::assertSame(3, $day['links'][1]['id']);
+        static::assertSame('http://domain.tld/3', $day['links'][1]['url']);
+        static::assertEquals($dates[1], $day['links'][1]['created']);
+    }
+
+    /**
+     * Daily RSS - trigger cache rendering
+     */
+    public function testValidRssControllerInvokeTriggerCache(): void
+    {
+        $this->createValidContainerMockSet();
+
+        $request = $this->createMock(Request::class);
+        $response = new Response();
+
+        $this->container->pageCacheManager->method('getCachePage')->willReturnCallback(function (): CachedPage {
+            $cachedPage = $this->createMock(CachedPage::class);
+            $cachedPage->method('cachedVersion')->willReturn('this is cache!');
+
+            return $cachedPage;
+        });
+
+        $this->container->bookmarkService->expects(static::never())->method('search');
+
+        $result = $this->controller->rss($request, $response);
+
+        static::assertSame(200, $result->getStatusCode());
+        static::assertStringContainsString('application/rss', $result->getHeader('Content-Type')[0]);
+        static::assertSame('this is cache!', (string) $result->getBody());
+    }
+
+    /**
+     * Daily RSS - No bookmark
+     */
+    public function testValidRssControllerInvokeNoBookmark(): void
+    {
+        $this->createValidContainerMockSet();
+
+        $request = $this->createMock(Request::class);
+        $response = new Response();
+
+        $this->container->bookmarkService->expects(static::once())->method('search')->willReturn([]);
+
+        // Save RainTPL assigned variables
+        $assignedVariables = [];
+        $this->assignTemplateVars($assignedVariables);
+
+        $result = $this->controller->rss($request, $response);
+
+        static::assertSame(200, $result->getStatusCode());
+        static::assertStringContainsString('application/rss', $result->getHeader('Content-Type')[0]);
+        static::assertSame('dailyrss', (string) $result->getBody());
+        static::assertSame('Shaarli', $assignedVariables['title']);
+        static::assertSame('http://shaarli', $assignedVariables['index_url']);
+        static::assertSame('http://shaarli/daily-rss', $assignedVariables['page_url']);
+        static::assertFalse($assignedVariables['hide_timestamps']);
+        static::assertCount(0, $assignedVariables['days']);
+    }
+
     protected function createValidContainerMockSet(): void
     {
         $loginManager = $this->createMock(LoginManager::class);
@@ -403,6 +536,17 @@ class DailyControllerTest extends TestCase
             })
         ;
         $this->container->formatterFactory = $formatterFactory;
+
+        // CacheManager
+        $pageCacheManager = $this->createMock(PageCacheManager::class);
+        $this->container->pageCacheManager = $pageCacheManager;
+
+        // $_SERVER
+        $this->container->environment = [
+            'SERVER_NAME' => 'shaarli',
+            'SERVER_PORT' => '80',
+            'REQUEST_URI' => '/daily-rss',
+        ];
     }
 
     protected function assignTemplateVars(array &$variables): void
index bcbe59cbf6abe493a392f429fb96f1c54cb2d46c..73d33cd450d783237829c133641783b5715243b3 100644 (file)
@@ -71,4 +71,36 @@ class IndexUrlTest extends \PHPUnit\Framework\TestCase
             )
         );
     }
+
+    /**
+     * The route is stored in REQUEST_URI
+     */
+    public function testPageUrlWithRoute()
+    {
+        $this->assertEquals(
+            'http://host.tld/picture-wall',
+            page_url(
+                array(
+                    'HTTPS' => 'Off',
+                    'SERVER_NAME' => 'host.tld',
+                    'SERVER_PORT' => '80',
+                    'SCRIPT_NAME' => '/index.php',
+                    'REQUEST_URI' => '/picture-wall',
+                )
+            )
+        );
+
+        $this->assertEquals(
+            'http://host.tld/admin/picture-wall',
+            page_url(
+                array(
+                    'HTTPS' => 'Off',
+                    'SERVER_NAME' => 'host.tld',
+                    'SERVER_PORT' => '80',
+                    'SCRIPT_NAME' => '/admin/index.php',
+                    'REQUEST_URI' => '/admin/picture-wall',
+                )
+            )
+        );
+    }
 }
index 991515d0fdf490e858d8d6ab36f703e724edfd1b..b870e6eb4b314a80be742c86223d6bfb58838a19 100644 (file)
@@ -32,7 +32,7 @@ class PageCacheManagerTest extends TestCase
      */
     public function setUp()
     {
-        $this->cacheManager = new PageCacheManager(static::$testCacheDir);
+        $this->cacheManager = new PageCacheManager(static::$testCacheDir, true);
 
         if (!is_dir(self::$testCacheDir)) {
             mkdir(self::$testCacheDir);
@@ -73,7 +73,7 @@ class PageCacheManagerTest extends TestCase
      */
     public function testPurgeCachedPagesMissingDir()
     {
-        $this->cacheManager = new PageCacheManager(self::$testCacheDir . '_missing');
+        $this->cacheManager = new PageCacheManager(self::$testCacheDir . '_missing', true);
 
         $oldlog = ini_get('error_log');
         ini_set('error_log', '/dev/null');
index f07c0a8b910cc1b1a84bcd1b4f55bd24d61bf736..9ccd1e61836af2ed640d599a793641f008a0a154 100644 (file)
@@ -11,7 +11,7 @@
   <div class="pure-u-lg-2-3 pure-u-22-24 page-form page-visitor" id="daily">
     <h2 class="window-title">
       {'The Daily Shaarli'|t}
-      <a href="./?do=dailyrss" title="{'1 RSS entry per day'|t}"><i class="fa fa-rss"></i></a>
+      <a href="./daily-rss" title="{'1 RSS entry per day'|t}"><i class="fa fa-rss"></i></a>
     </h2>
 
     <div id="plugin_zone_start_daily" class="plugin_zone">
index f589b06ead8b0f675c67f347c6b9360437d1bdb9..d40d94968ad6d75b2145b4116af5f449de7788f5 100644 (file)
@@ -1,16 +1,32 @@
-<item>
-    <title>{$title} - {function="strftime('%A %e %B %Y', $daydate)"}</title>
-    <guid>{$absurl}</guid>
-    <link>{$absurl}</link>
-    <pubDate>{$rssdate}</pubDate>
-    <description><![CDATA[
-        {loop="links"}
-               <h3><a href="{$value.url}">{$value.title}</a></h3>
-               <small>{if="!$hide_timestamps"}{function="strftime('%c', $value.timestamp)"} - {/if}{if="$value.tags"}{$value.tags}{/if}<br>
-               {$value.url}</small><br>
-               {if="$value.thumbnail"}<img src="{$index_url}{$value.thumbnail}#" alt="thumbnail" />{/if}<br>
-               {if="$value.description"}{$value.formatedDescription}{/if}
-               <br><br><hr>
-        {/loop}
-    ]]></description>
-</item>
+<?xml version="1.0" encoding="UTF-8"?>
+<rss version="2.0">
+  <channel>
+    <title>Daily - {$title}</title>
+    <link>{$index_url}</link>
+    <description>Daily shaared bookmarks</description>
+    <language>{$language}</language>
+    <copyright>{$index_url}</copyright>
+    <generator>Shaarli</generator>
+
+    {loop="$days"}
+      <item>
+        <title>{$value.date_human} - {$title}</title>
+        <guid>{$value.absolute_url}</guid>
+        <link>{$value.absolute_url}</link>
+        <pubDate>{$value.date_rss}</pubDate>
+        <description><![CDATA[
+          {loop="$value.links"}
+            <h3><a href="{$value.url}">{$value.title}</a></h3>
+            <small>
+              {if="!$hide_timestamps"}{$value.created|format_date} - {/if}{if="$value.tags"}{$value.tags}{/if}<br>
+              {$value.url}
+            </small><br>
+            {if="$value.thumbnail"}<img src="{$index_url}{$value.thumbnail}#" alt="thumbnail" />{/if}<br>
+            {if="$value.description"}{$value.description}{/if}
+            <br><br><hr>
+          {/loop}
+        ]]></description>
+      </item>
+    {/loop}
+  </channel>
+</rss><!-- Cached version of {$page_url} -->
index 4892e0e1c16d65b77c24da102895884cda49e7d4..adcdf6abc6cdbc7f03401a0b25a911b0be667b7a 100644 (file)
@@ -24,7 +24,7 @@
         {/loop}
 
         <br>
-        <a href="./?do=dailyrss" title="1 RSS entry per day"><img src="img/feed-icon-14x14.png" alt="rss_feed">Daily RSS Feed</a>
+        <a href="./daily-rss" title="1 RSS entry per day"><img src="img/feed-icon-14x14.png" alt="rss_feed">Daily RSS Feed</a>
     </div>
 
     <div class="dailyTitle">
index f589b06ead8b0f675c67f347c6b9360437d1bdb9..ff19bbfbae7f8c1ed9903705e849e53f455fe5cd 100644 (file)
@@ -1,16 +1,32 @@
-<item>
-    <title>{$title} - {function="strftime('%A %e %B %Y', $daydate)"}</title>
-    <guid>{$absurl}</guid>
-    <link>{$absurl}</link>
-    <pubDate>{$rssdate}</pubDate>
-    <description><![CDATA[
-        {loop="links"}
-               <h3><a href="{$value.url}">{$value.title}</a></h3>
-               <small>{if="!$hide_timestamps"}{function="strftime('%c', $value.timestamp)"} - {/if}{if="$value.tags"}{$value.tags}{/if}<br>
-               {$value.url}</small><br>
-               {if="$value.thumbnail"}<img src="{$index_url}{$value.thumbnail}#" alt="thumbnail" />{/if}<br>
-               {if="$value.description"}{$value.formatedDescription}{/if}
-               <br><br><hr>
+<?xml version="1.0" encoding="UTF-8"?>
+<rss version="2.0">
+  <channel>
+    <title>Daily - {$title}</title>
+    <link>{$index_url}</link>
+    <description>Daily shaared bookmarks</description>
+    <language>{$language}</language>
+    <copyright>{$index_url}</copyright>
+    <generator>Shaarli</generator>
+
+    {loop="$days"}
+    <item>
+      <title>{$value.date_human} - {$title}</title>
+      <guid>{$value.absolute_url}</guid>
+      <link>{$value.absolute_url}</link>
+      <pubDate>{$value.date_rss}</pubDate>
+      <description><![CDATA[
+        {loop="$value.links"}
+        <h3><a href="{$value.url}">{$value.title}</a></h3>
+        <small>
+          {if="!$hide_timestamps"}{$value.created|format_date} - {/if}{if="$value.tags"}{$value.tags}{/if}<br>
+          {$value.url}
+        </small><br>
+        {if="$value.thumbnail"}<img src="{$index_url}{$value.thumbnail}#" alt="thumbnail" />{/if}<br>
+        {if="$value.description"}{$value.description}{/if}
+        <br><br><hr>
         {/loop}
-    ]]></description>
-</item>
+        ]]></description>
+    </item>
+    {/loop}
+  </channel>
+</rss><!-- Cached version of {$page_url} -->