]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Migrate cache purge function to a proper class
authorArthurHoaro <arthur@hoa.ro>
Thu, 23 Jan 2020 20:13:41 +0000 (21:13 +0100)
committerArthurHoaro <arthur@hoa.ro>
Thu, 23 Jul 2020 19:19:21 +0000 (21:19 +0200)
And update dependencies and tests.

Note that SESSION['tags'] has been removed a log ago

application/bookmark/BookmarkFileService.php
application/bookmark/BookmarkIO.php
application/feed/Cache.php [deleted file]
application/legacy/LegacyLinkDB.php
application/render/PageCacheManager.php [new file with mode: 0644]
index.php
tests/bootstrap.php
tests/legacy/LegacyLinkDBTest.php
tests/render/PageCacheManagerTest.php [moved from tests/feed/CacheTest.php with 70% similarity]

index 9c59e1396a31418a957be70e9e776fcdd586978d..fef998fdddc61e641e2571d18ea048d3020a9a20 100644 (file)
@@ -12,6 +12,7 @@ use Shaarli\Formatter\BookmarkMarkdownFormatter;
 use Shaarli\History;
 use Shaarli\Legacy\LegacyLinkDB;
 use Shaarli\Legacy\LegacyUpdater;
+use Shaarli\Render\PageCacheManager;
 use Shaarli\Updater\UpdaterUtils;
 
 /**
@@ -39,6 +40,9 @@ class BookmarkFileService implements BookmarkServiceInterface
     /** @var History instance */
     protected $history;
 
+    /** @var PageCacheManager instance */
+    protected $pageCacheManager;
+
     /** @var bool true for logged in users. Default value to retrieve private bookmarks. */
     protected $isLoggedIn;
 
@@ -49,6 +53,7 @@ class BookmarkFileService implements BookmarkServiceInterface
     {
         $this->conf = $conf;
         $this->history = $history;
+        $this->pageCacheManager = new PageCacheManager($this->conf->get('resource.page_cache'));
         $this->bookmarksIO = new BookmarkIO($this->conf);
         $this->isLoggedIn = $isLoggedIn;
 
@@ -275,7 +280,7 @@ class BookmarkFileService implements BookmarkServiceInterface
         }
         $this->bookmarks->reorder();
         $this->bookmarksIO->write($this->bookmarks);
-        invalidateCaches($this->conf->get('resource.page_cache'));
+        $this->pageCacheManager->invalidateCaches();
     }
 
     /**
index ae9ffcb4612bd1acbc633d51b3c48c46cd05ff76..1026e2f978d0a43650f0753e23a6b92529b1f91d 100644 (file)
@@ -102,7 +102,5 @@ class BookmarkIO
             $this->datastore,
             self::$phpPrefix.base64_encode(gzdeflate(serialize($links))).self::$phpSuffix
         );
-
-        invalidateCaches($this->conf->get('resource.page_cache'));
     }
 }
diff --git a/application/feed/Cache.php b/application/feed/Cache.php
deleted file mode 100644 (file)
index e5d43e6..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-<?php
-/**
- * Cache utilities
- */
-
-/**
- * Purges all cached pages
- *
- * @param string $pageCacheDir page cache directory
- *
- * @return mixed an error string if the directory is missing
- */
-function purgeCachedPages($pageCacheDir)
-{
-    if (! is_dir($pageCacheDir)) {
-        $error = sprintf(t('Cannot purge %s: no directory'), $pageCacheDir);
-        error_log($error);
-        return $error;
-    }
-
-    array_map('unlink', glob($pageCacheDir.'/*.cache'));
-}
-
-/**
- * Invalidates caches when the database is changed or the user logs out.
- *
- * @param string $pageCacheDir page cache directory
- */
-function invalidateCaches($pageCacheDir)
-{
-    // Purge cache attached to session.
-    if (isset($_SESSION['tags'])) {
-        unset($_SESSION['tags']);
-    }
-
-    // Purge page cache shared by sessions.
-    purgeCachedPages($pageCacheDir);
-}
index 7ccf5e54a9c4cb9dfb70974a99197a545ef2c92a..947005adddb6184ce807a105ba1344f50cb4c453 100644 (file)
@@ -9,6 +9,7 @@ use Iterator;
 use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
 use Shaarli\Exceptions\IOException;
 use Shaarli\FileUtils;
+use Shaarli\Render\PageCacheManager;
 
 /**
  * Data storage for bookmarks.
@@ -352,7 +353,8 @@ You use the community supported version of the original Shaarli project, by Seba
 
         $this->write();
 
-        invalidateCaches($pageCacheDir);
+        $pageCacheManager = new PageCacheManager($pageCacheDir);
+        $pageCacheManager->invalidateCaches();
     }
 
     /**
diff --git a/application/render/PageCacheManager.php b/application/render/PageCacheManager.php
new file mode 100644 (file)
index 0000000..bd91fe0
--- /dev/null
@@ -0,0 +1,45 @@
+<?php
+
+namespace Shaarli\Render;
+
+/**
+ * Cache utilities
+ */
+class PageCacheManager
+{
+    /** @var string Cache directory */
+    protected $pageCacheDir;
+
+    public function __construct(string $pageCacheDir)
+    {
+        $this->pageCacheDir = $pageCacheDir;
+    }
+
+    /**
+     * Purges all cached pages
+     *
+     * @return string|null an error string if the directory is missing
+     */
+    public function purgeCachedPages(): ?string
+    {
+        if (!is_dir($this->pageCacheDir)) {
+            $error = sprintf(t('Cannot purge %s: no directory'), $this->pageCacheDir);
+            error_log($error);
+
+            return $error;
+        }
+
+        array_map('unlink', glob($this->pageCacheDir . '/*.cache'));
+
+        return null;
+    }
+
+    /**
+     * Invalidates caches when the database is changed or the user logs out.
+     */
+    public function invalidateCaches(): void
+    {
+        // Purge page cache shared by sessions.
+        $this->purgeCachedPages();
+    }
+}
index c639a3bce141b734fa13407fbbfef6b29b249c8f..73d9e0224d96fe8a784e66459aa16e39b316a5b0 100644 (file)
--- a/index.php
+++ b/index.php
@@ -53,7 +53,6 @@ require_once __DIR__ . '/vendor/autoload.php';
 // Shaarli library
 require_once 'application/bookmark/LinkUtils.php';
 require_once 'application/config/ConfigPlugin.php';
-require_once 'application/feed/Cache.php';
 require_once 'application/http/HttpUtils.php';
 require_once 'application/http/UrlUtils.php';
 require_once 'application/updater/UpdaterUtils.php';
@@ -78,6 +77,7 @@ use Shaarli\Languages;
 use Shaarli\Netscape\NetscapeBookmarkUtils;
 use Shaarli\Plugin\PluginManager;
 use Shaarli\Render\PageBuilder;
+use Shaarli\Render\PageCacheManager;
 use Shaarli\Render\ThemeUtils;
 use Shaarli\Router;
 use Shaarli\Security\LoginManager;
@@ -530,6 +530,7 @@ function showLinkList($PAGE, $linkDb, $conf, $pluginManager, $loginManager)
  */
 function renderPage($conf, $pluginManager, $bookmarkService, $history, $sessionManager, $loginManager)
 {
+    $pageCacheManager = new PageCacheManager($conf->get('resource.page_cache'));
     $updater = new Updater(
         UpdaterUtils::read_updates_file($conf->get('resource.updates')),
         $bookmarkService,
@@ -543,6 +544,8 @@ function renderPage($conf, $pluginManager, $bookmarkService, $history, $sessionM
                 $conf->get('resource.updates'),
                 $updater->getDoneUpdates()
             );
+
+            $pageCacheManager->invalidateCaches();
         }
     } catch (Exception $e) {
         die($e->getMessage());
@@ -1029,7 +1032,7 @@ function renderPage($conf, $pluginManager, $bookmarkService, $history, $sessionM
             try {
                 $conf->write($loginManager->isLoggedIn());
                 $history->updateSettings();
-                invalidateCaches($conf->get('resource.page_cache'));
+                $pageCacheManager->invalidateCaches();
             } catch (Exception $e) {
                 error_log(
                     'ERROR while writing config file after configuration update.' . PHP_EOL .
@@ -1914,6 +1917,7 @@ $app->group('/api/v1', function () {
 
 $app->group('', function () {
     $this->get('/login', '\Shaarli\Front\Controller\LoginController:index')->setName('login');
+    $this->get('/logout', '\Shaarli\Front\Controller\LogoutController:index')->setName('logout');
     $this->get('/picture-wall', '\Shaarli\Front\Controller\PictureWallController:index')->setName('picwall');
 })->add('\Shaarli\Front\ShaarliMiddleware');
 
index 0afbcba61bb04da17b45b9ceb702a086ed816188..c80bcb33a3decdce2080479c65fa50fa6a3decaf 100644 (file)
@@ -18,7 +18,6 @@ require_once 'application/bookmark/LinkUtils.php';
 require_once 'application/Utils.php';
 require_once 'application/http/UrlUtils.php';
 require_once 'application/http/HttpUtils.php';
-require_once 'application/feed/Cache.php';
 require_once 'tests/utils/ReferenceLinkDB.php';
 require_once 'tests/utils/ReferenceHistory.php';
 require_once 'tests/utils/FakeBookmarkService.php';
index 17b2b0e6cc35d277c87ce9949bfa8a7ee525f27d..0884ad03ed2bd40837bc08ac4d1d60adb28820f6 100644 (file)
@@ -11,7 +11,6 @@ use ReflectionClass;
 use Shaarli;
 use Shaarli\Bookmark\Bookmark;
 
-require_once 'application/feed/Cache.php';
 require_once 'application/Utils.php';
 require_once 'tests/utils/ReferenceLinkDB.php';
 
similarity index 70%
rename from tests/feed/CacheTest.php
rename to tests/render/PageCacheManagerTest.php
index c0a9f26f2dc933d7adf8644a8b580d797efe01de..991515d0fdf490e858d8d6ab36f703e724edfd1b 100644 (file)
@@ -2,17 +2,18 @@
 /**
  * Cache tests
  */
-namespace Shaarli\Feed;
+namespace Shaarli\Render;
+
+use PHPUnit\Framework\TestCase;
+use Shaarli\Security\SessionManager;
 
 // required to access $_SESSION array
 session_start();
 
-require_once 'application/feed/Cache.php';
-
 /**
  * Unitary tests for cached pages
  */
-class CacheTest extends \PHPUnit\Framework\TestCase
+class PageCacheManagerTest extends TestCase
 {
     // test cache directory
     protected static $testCacheDir = 'sandbox/dummycache';
@@ -20,12 +21,19 @@ class CacheTest extends \PHPUnit\Framework\TestCase
     // dummy cached file names / content
     protected static $pages = array('a', 'toto', 'd7b59c');
 
+    /** @var PageCacheManager */
+    protected $cacheManager;
+
+    /** @var SessionManager */
+    protected $sessionManager;
 
     /**
      * Populate the cache with dummy files
      */
     public function setUp()
     {
+        $this->cacheManager = new PageCacheManager(static::$testCacheDir);
+
         if (!is_dir(self::$testCacheDir)) {
             mkdir(self::$testCacheDir);
         } else {
@@ -52,7 +60,7 @@ class CacheTest extends \PHPUnit\Framework\TestCase
      */
     public function testPurgeCachedPages()
     {
-        purgeCachedPages(self::$testCacheDir);
+        $this->cacheManager->purgeCachedPages();
         foreach (self::$pages as $page) {
             $this->assertFileNotExists(self::$testCacheDir . '/' . $page . '.cache');
         }
@@ -65,28 +73,14 @@ class CacheTest extends \PHPUnit\Framework\TestCase
      */
     public function testPurgeCachedPagesMissingDir()
     {
+        $this->cacheManager = new PageCacheManager(self::$testCacheDir . '_missing');
+
         $oldlog = ini_get('error_log');
         ini_set('error_log', '/dev/null');
         $this->assertEquals(
             'Cannot purge sandbox/dummycache_missing: no directory',
-            purgeCachedPages(self::$testCacheDir . '_missing')
+            $this->cacheManager->purgeCachedPages()
         );
         ini_set('error_log', $oldlog);
     }
-
-    /**
-     * Purge cached pages and session cache
-     */
-    public function testInvalidateCaches()
-    {
-        $this->assertArrayNotHasKey('tags', $_SESSION);
-        $_SESSION['tags'] = array('goodbye', 'cruel', 'world');
-
-        invalidateCaches(self::$testCacheDir);
-        foreach (self::$pages as $page) {
-            $this->assertFileNotExists(self::$testCacheDir . '/' . $page . '.cache');
-        }
-
-        $this->assertArrayNotHasKey('tags', $_SESSION);
-    }
 }