aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-01-23 21:13:41 +0100
committerArthurHoaro <arthur@hoa.ro>2020-07-23 21:19:21 +0200
commitb0428aa9b02b058b72c40b6e8dc2298d55bf692f (patch)
treec57176b24d76836a73608fb273b1094fcb8de785
parent485b168a9677d160b0c0426e4f282b9bd0c632c1 (diff)
downloadShaarli-b0428aa9b02b058b72c40b6e8dc2298d55bf692f.tar.gz
Shaarli-b0428aa9b02b058b72c40b6e8dc2298d55bf692f.tar.zst
Shaarli-b0428aa9b02b058b72c40b6e8dc2298d55bf692f.zip
Migrate cache purge function to a proper class
And update dependencies and tests. Note that SESSION['tags'] has been removed a log ago
-rw-r--r--application/bookmark/BookmarkFileService.php7
-rw-r--r--application/bookmark/BookmarkIO.php2
-rw-r--r--application/feed/Cache.php38
-rw-r--r--application/legacy/LegacyLinkDB.php4
-rw-r--r--application/render/PageCacheManager.php45
-rw-r--r--index.php8
-rw-r--r--tests/bootstrap.php1
-rw-r--r--tests/legacy/LegacyLinkDBTest.php1
-rw-r--r--tests/render/PageCacheManagerTest.php (renamed from tests/feed/CacheTest.php)38
9 files changed, 76 insertions, 68 deletions
diff --git a/application/bookmark/BookmarkFileService.php b/application/bookmark/BookmarkFileService.php
index 9c59e139..fef998fd 100644
--- a/application/bookmark/BookmarkFileService.php
+++ b/application/bookmark/BookmarkFileService.php
@@ -12,6 +12,7 @@ use Shaarli\Formatter\BookmarkMarkdownFormatter;
12use Shaarli\History; 12use Shaarli\History;
13use Shaarli\Legacy\LegacyLinkDB; 13use Shaarli\Legacy\LegacyLinkDB;
14use Shaarli\Legacy\LegacyUpdater; 14use Shaarli\Legacy\LegacyUpdater;
15use Shaarli\Render\PageCacheManager;
15use Shaarli\Updater\UpdaterUtils; 16use Shaarli\Updater\UpdaterUtils;
16 17
17/** 18/**
@@ -39,6 +40,9 @@ class BookmarkFileService implements BookmarkServiceInterface
39 /** @var History instance */ 40 /** @var History instance */
40 protected $history; 41 protected $history;
41 42
43 /** @var PageCacheManager instance */
44 protected $pageCacheManager;
45
42 /** @var bool true for logged in users. Default value to retrieve private bookmarks. */ 46 /** @var bool true for logged in users. Default value to retrieve private bookmarks. */
43 protected $isLoggedIn; 47 protected $isLoggedIn;
44 48
@@ -49,6 +53,7 @@ class BookmarkFileService implements BookmarkServiceInterface
49 { 53 {
50 $this->conf = $conf; 54 $this->conf = $conf;
51 $this->history = $history; 55 $this->history = $history;
56 $this->pageCacheManager = new PageCacheManager($this->conf->get('resource.page_cache'));
52 $this->bookmarksIO = new BookmarkIO($this->conf); 57 $this->bookmarksIO = new BookmarkIO($this->conf);
53 $this->isLoggedIn = $isLoggedIn; 58 $this->isLoggedIn = $isLoggedIn;
54 59
@@ -275,7 +280,7 @@ class BookmarkFileService implements BookmarkServiceInterface
275 } 280 }
276 $this->bookmarks->reorder(); 281 $this->bookmarks->reorder();
277 $this->bookmarksIO->write($this->bookmarks); 282 $this->bookmarksIO->write($this->bookmarks);
278 invalidateCaches($this->conf->get('resource.page_cache')); 283 $this->pageCacheManager->invalidateCaches();
279 } 284 }
280 285
281 /** 286 /**
diff --git a/application/bookmark/BookmarkIO.php b/application/bookmark/BookmarkIO.php
index ae9ffcb4..1026e2f9 100644
--- a/application/bookmark/BookmarkIO.php
+++ b/application/bookmark/BookmarkIO.php
@@ -102,7 +102,5 @@ class BookmarkIO
102 $this->datastore, 102 $this->datastore,
103 self::$phpPrefix.base64_encode(gzdeflate(serialize($links))).self::$phpSuffix 103 self::$phpPrefix.base64_encode(gzdeflate(serialize($links))).self::$phpSuffix
104 ); 104 );
105
106 invalidateCaches($this->conf->get('resource.page_cache'));
107 } 105 }
108} 106}
diff --git a/application/feed/Cache.php b/application/feed/Cache.php
deleted file mode 100644
index e5d43e61..00000000
--- a/application/feed/Cache.php
+++ /dev/null
@@ -1,38 +0,0 @@
1<?php
2/**
3 * Cache utilities
4 */
5
6/**
7 * Purges all cached pages
8 *
9 * @param string $pageCacheDir page cache directory
10 *
11 * @return mixed an error string if the directory is missing
12 */
13function purgeCachedPages($pageCacheDir)
14{
15 if (! is_dir($pageCacheDir)) {
16 $error = sprintf(t('Cannot purge %s: no directory'), $pageCacheDir);
17 error_log($error);
18 return $error;
19 }
20
21 array_map('unlink', glob($pageCacheDir.'/*.cache'));
22}
23
24/**
25 * Invalidates caches when the database is changed or the user logs out.
26 *
27 * @param string $pageCacheDir page cache directory
28 */
29function invalidateCaches($pageCacheDir)
30{
31 // Purge cache attached to session.
32 if (isset($_SESSION['tags'])) {
33 unset($_SESSION['tags']);
34 }
35
36 // Purge page cache shared by sessions.
37 purgeCachedPages($pageCacheDir);
38}
diff --git a/application/legacy/LegacyLinkDB.php b/application/legacy/LegacyLinkDB.php
index 7ccf5e54..947005ad 100644
--- a/application/legacy/LegacyLinkDB.php
+++ b/application/legacy/LegacyLinkDB.php
@@ -9,6 +9,7 @@ use Iterator;
9use Shaarli\Bookmark\Exception\BookmarkNotFoundException; 9use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
10use Shaarli\Exceptions\IOException; 10use Shaarli\Exceptions\IOException;
11use Shaarli\FileUtils; 11use Shaarli\FileUtils;
12use Shaarli\Render\PageCacheManager;
12 13
13/** 14/**
14 * Data storage for bookmarks. 15 * Data storage for bookmarks.
@@ -352,7 +353,8 @@ You use the community supported version of the original Shaarli project, by Seba
352 353
353 $this->write(); 354 $this->write();
354 355
355 invalidateCaches($pageCacheDir); 356 $pageCacheManager = new PageCacheManager($pageCacheDir);
357 $pageCacheManager->invalidateCaches();
356 } 358 }
357 359
358 /** 360 /**
diff --git a/application/render/PageCacheManager.php b/application/render/PageCacheManager.php
new file mode 100644
index 00000000..bd91fe0d
--- /dev/null
+++ b/application/render/PageCacheManager.php
@@ -0,0 +1,45 @@
1<?php
2
3namespace Shaarli\Render;
4
5/**
6 * Cache utilities
7 */
8class PageCacheManager
9{
10 /** @var string Cache directory */
11 protected $pageCacheDir;
12
13 public function __construct(string $pageCacheDir)
14 {
15 $this->pageCacheDir = $pageCacheDir;
16 }
17
18 /**
19 * Purges all cached pages
20 *
21 * @return string|null an error string if the directory is missing
22 */
23 public function purgeCachedPages(): ?string
24 {
25 if (!is_dir($this->pageCacheDir)) {
26 $error = sprintf(t('Cannot purge %s: no directory'), $this->pageCacheDir);
27 error_log($error);
28
29 return $error;
30 }
31
32 array_map('unlink', glob($this->pageCacheDir . '/*.cache'));
33
34 return null;
35 }
36
37 /**
38 * Invalidates caches when the database is changed or the user logs out.
39 */
40 public function invalidateCaches(): void
41 {
42 // Purge page cache shared by sessions.
43 $this->purgeCachedPages();
44 }
45}
diff --git a/index.php b/index.php
index c639a3bc..73d9e022 100644
--- a/index.php
+++ b/index.php
@@ -53,7 +53,6 @@ require_once __DIR__ . '/vendor/autoload.php';
53// Shaarli library 53// Shaarli library
54require_once 'application/bookmark/LinkUtils.php'; 54require_once 'application/bookmark/LinkUtils.php';
55require_once 'application/config/ConfigPlugin.php'; 55require_once 'application/config/ConfigPlugin.php';
56require_once 'application/feed/Cache.php';
57require_once 'application/http/HttpUtils.php'; 56require_once 'application/http/HttpUtils.php';
58require_once 'application/http/UrlUtils.php'; 57require_once 'application/http/UrlUtils.php';
59require_once 'application/updater/UpdaterUtils.php'; 58require_once 'application/updater/UpdaterUtils.php';
@@ -78,6 +77,7 @@ use Shaarli\Languages;
78use Shaarli\Netscape\NetscapeBookmarkUtils; 77use Shaarli\Netscape\NetscapeBookmarkUtils;
79use Shaarli\Plugin\PluginManager; 78use Shaarli\Plugin\PluginManager;
80use Shaarli\Render\PageBuilder; 79use Shaarli\Render\PageBuilder;
80use Shaarli\Render\PageCacheManager;
81use Shaarli\Render\ThemeUtils; 81use Shaarli\Render\ThemeUtils;
82use Shaarli\Router; 82use Shaarli\Router;
83use Shaarli\Security\LoginManager; 83use Shaarli\Security\LoginManager;
@@ -530,6 +530,7 @@ function showLinkList($PAGE, $linkDb, $conf, $pluginManager, $loginManager)
530 */ 530 */
531function renderPage($conf, $pluginManager, $bookmarkService, $history, $sessionManager, $loginManager) 531function renderPage($conf, $pluginManager, $bookmarkService, $history, $sessionManager, $loginManager)
532{ 532{
533 $pageCacheManager = new PageCacheManager($conf->get('resource.page_cache'));
533 $updater = new Updater( 534 $updater = new Updater(
534 UpdaterUtils::read_updates_file($conf->get('resource.updates')), 535 UpdaterUtils::read_updates_file($conf->get('resource.updates')),
535 $bookmarkService, 536 $bookmarkService,
@@ -543,6 +544,8 @@ function renderPage($conf, $pluginManager, $bookmarkService, $history, $sessionM
543 $conf->get('resource.updates'), 544 $conf->get('resource.updates'),
544 $updater->getDoneUpdates() 545 $updater->getDoneUpdates()
545 ); 546 );
547
548 $pageCacheManager->invalidateCaches();
546 } 549 }
547 } catch (Exception $e) { 550 } catch (Exception $e) {
548 die($e->getMessage()); 551 die($e->getMessage());
@@ -1029,7 +1032,7 @@ function renderPage($conf, $pluginManager, $bookmarkService, $history, $sessionM
1029 try { 1032 try {
1030 $conf->write($loginManager->isLoggedIn()); 1033 $conf->write($loginManager->isLoggedIn());
1031 $history->updateSettings(); 1034 $history->updateSettings();
1032 invalidateCaches($conf->get('resource.page_cache')); 1035 $pageCacheManager->invalidateCaches();
1033 } catch (Exception $e) { 1036 } catch (Exception $e) {
1034 error_log( 1037 error_log(
1035 'ERROR while writing config file after configuration update.' . PHP_EOL . 1038 'ERROR while writing config file after configuration update.' . PHP_EOL .
@@ -1914,6 +1917,7 @@ $app->group('/api/v1', function () {
1914 1917
1915$app->group('', function () { 1918$app->group('', function () {
1916 $this->get('/login', '\Shaarli\Front\Controller\LoginController:index')->setName('login'); 1919 $this->get('/login', '\Shaarli\Front\Controller\LoginController:index')->setName('login');
1920 $this->get('/logout', '\Shaarli\Front\Controller\LogoutController:index')->setName('logout');
1917 $this->get('/picture-wall', '\Shaarli\Front\Controller\PictureWallController:index')->setName('picwall'); 1921 $this->get('/picture-wall', '\Shaarli\Front\Controller\PictureWallController:index')->setName('picwall');
1918})->add('\Shaarli\Front\ShaarliMiddleware'); 1922})->add('\Shaarli\Front\ShaarliMiddleware');
1919 1923
diff --git a/tests/bootstrap.php b/tests/bootstrap.php
index 0afbcba6..c80bcb33 100644
--- a/tests/bootstrap.php
+++ b/tests/bootstrap.php
@@ -18,7 +18,6 @@ require_once 'application/bookmark/LinkUtils.php';
18require_once 'application/Utils.php'; 18require_once 'application/Utils.php';
19require_once 'application/http/UrlUtils.php'; 19require_once 'application/http/UrlUtils.php';
20require_once 'application/http/HttpUtils.php'; 20require_once 'application/http/HttpUtils.php';
21require_once 'application/feed/Cache.php';
22require_once 'tests/utils/ReferenceLinkDB.php'; 21require_once 'tests/utils/ReferenceLinkDB.php';
23require_once 'tests/utils/ReferenceHistory.php'; 22require_once 'tests/utils/ReferenceHistory.php';
24require_once 'tests/utils/FakeBookmarkService.php'; 23require_once 'tests/utils/FakeBookmarkService.php';
diff --git a/tests/legacy/LegacyLinkDBTest.php b/tests/legacy/LegacyLinkDBTest.php
index 17b2b0e6..0884ad03 100644
--- a/tests/legacy/LegacyLinkDBTest.php
+++ b/tests/legacy/LegacyLinkDBTest.php
@@ -11,7 +11,6 @@ use ReflectionClass;
11use Shaarli; 11use Shaarli;
12use Shaarli\Bookmark\Bookmark; 12use Shaarli\Bookmark\Bookmark;
13 13
14require_once 'application/feed/Cache.php';
15require_once 'application/Utils.php'; 14require_once 'application/Utils.php';
16require_once 'tests/utils/ReferenceLinkDB.php'; 15require_once 'tests/utils/ReferenceLinkDB.php';
17 16
diff --git a/tests/feed/CacheTest.php b/tests/render/PageCacheManagerTest.php
index c0a9f26f..991515d0 100644
--- a/tests/feed/CacheTest.php
+++ b/tests/render/PageCacheManagerTest.php
@@ -2,17 +2,18 @@
2/** 2/**
3 * Cache tests 3 * Cache tests
4 */ 4 */
5namespace Shaarli\Feed; 5namespace Shaarli\Render;
6
7use PHPUnit\Framework\TestCase;
8use Shaarli\Security\SessionManager;
6 9
7// required to access $_SESSION array 10// required to access $_SESSION array
8session_start(); 11session_start();
9 12
10require_once 'application/feed/Cache.php';
11
12/** 13/**
13 * Unitary tests for cached pages 14 * Unitary tests for cached pages
14 */ 15 */
15class CacheTest extends \PHPUnit\Framework\TestCase 16class PageCacheManagerTest extends TestCase
16{ 17{
17 // test cache directory 18 // test cache directory
18 protected static $testCacheDir = 'sandbox/dummycache'; 19 protected static $testCacheDir = 'sandbox/dummycache';
@@ -20,12 +21,19 @@ class CacheTest extends \PHPUnit\Framework\TestCase
20 // dummy cached file names / content 21 // dummy cached file names / content
21 protected static $pages = array('a', 'toto', 'd7b59c'); 22 protected static $pages = array('a', 'toto', 'd7b59c');
22 23
24 /** @var PageCacheManager */
25 protected $cacheManager;
26
27 /** @var SessionManager */
28 protected $sessionManager;
23 29
24 /** 30 /**
25 * Populate the cache with dummy files 31 * Populate the cache with dummy files
26 */ 32 */
27 public function setUp() 33 public function setUp()
28 { 34 {
35 $this->cacheManager = new PageCacheManager(static::$testCacheDir);
36
29 if (!is_dir(self::$testCacheDir)) { 37 if (!is_dir(self::$testCacheDir)) {
30 mkdir(self::$testCacheDir); 38 mkdir(self::$testCacheDir);
31 } else { 39 } else {
@@ -52,7 +60,7 @@ class CacheTest extends \PHPUnit\Framework\TestCase
52 */ 60 */
53 public function testPurgeCachedPages() 61 public function testPurgeCachedPages()
54 { 62 {
55 purgeCachedPages(self::$testCacheDir); 63 $this->cacheManager->purgeCachedPages();
56 foreach (self::$pages as $page) { 64 foreach (self::$pages as $page) {
57 $this->assertFileNotExists(self::$testCacheDir . '/' . $page . '.cache'); 65 $this->assertFileNotExists(self::$testCacheDir . '/' . $page . '.cache');
58 } 66 }
@@ -65,28 +73,14 @@ class CacheTest extends \PHPUnit\Framework\TestCase
65 */ 73 */
66 public function testPurgeCachedPagesMissingDir() 74 public function testPurgeCachedPagesMissingDir()
67 { 75 {
76 $this->cacheManager = new PageCacheManager(self::$testCacheDir . '_missing');
77
68 $oldlog = ini_get('error_log'); 78 $oldlog = ini_get('error_log');
69 ini_set('error_log', '/dev/null'); 79 ini_set('error_log', '/dev/null');
70 $this->assertEquals( 80 $this->assertEquals(
71 'Cannot purge sandbox/dummycache_missing: no directory', 81 'Cannot purge sandbox/dummycache_missing: no directory',
72 purgeCachedPages(self::$testCacheDir . '_missing') 82 $this->cacheManager->purgeCachedPages()
73 ); 83 );
74 ini_set('error_log', $oldlog); 84 ini_set('error_log', $oldlog);
75 } 85 }
76
77 /**
78 * Purge cached pages and session cache
79 */
80 public function testInvalidateCaches()
81 {
82 $this->assertArrayNotHasKey('tags', $_SESSION);
83 $_SESSION['tags'] = array('goodbye', 'cruel', 'world');
84
85 invalidateCaches(self::$testCacheDir);
86 foreach (self::$pages as $page) {
87 $this->assertFileNotExists(self::$testCacheDir . '/' . $page . '.cache');
88 }
89
90 $this->assertArrayNotHasKey('tags', $_SESSION);
91 }
92} 86}