And update dependencies and tests.
Note that SESSION['tags'] has been removed a log ago
use Shaarli\History;
use Shaarli\Legacy\LegacyLinkDB;
use Shaarli\Legacy\LegacyUpdater;
+use Shaarli\Render\PageCacheManager;
use Shaarli\Updater\UpdaterUtils;
/**
/** @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;
{
$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;
}
$this->bookmarks->reorder();
$this->bookmarksIO->write($this->bookmarks);
- invalidateCaches($this->conf->get('resource.page_cache'));
+ $this->pageCacheManager->invalidateCaches();
}
/**
$this->datastore,
self::$phpPrefix.base64_encode(gzdeflate(serialize($links))).self::$phpSuffix
);
-
- invalidateCaches($this->conf->get('resource.page_cache'));
}
}
+++ /dev/null
-<?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);
-}
use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
use Shaarli\Exceptions\IOException;
use Shaarli\FileUtils;
+use Shaarli\Render\PageCacheManager;
/**
* Data storage for bookmarks.
$this->write();
- invalidateCaches($pageCacheDir);
+ $pageCacheManager = new PageCacheManager($pageCacheDir);
+ $pageCacheManager->invalidateCaches();
}
/**
--- /dev/null
+<?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();
+ }
+}
// 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';
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;
*/
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,
$conf->get('resource.updates'),
$updater->getDoneUpdates()
);
+
+ $pageCacheManager->invalidateCaches();
}
} catch (Exception $e) {
die($e->getMessage());
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 .
$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');
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';
use Shaarli;
use Shaarli\Bookmark\Bookmark;
-require_once 'application/feed/Cache.php';
require_once 'application/Utils.php';
require_once 'tests/utils/ReferenceLinkDB.php';
/**
* 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';
// 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 {
*/
public function testPurgeCachedPages()
{
- purgeCachedPages(self::$testCacheDir);
+ $this->cacheManager->purgeCachedPages();
foreach (self::$pages as $page) {
$this->assertFileNotExists(self::$testCacheDir . '/' . $page . '.cache');
}
*/
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);
- }
}