Skip to content

Commit 227027f

Browse files
committed
Content: Updated purifier and content caching
- Updated page content cache to use app version in cache key - Moved purifier cache into framework to better work with existing expected folders. - Added app version check to purifier so that it will reset its own cache on app version change.
1 parent 0f040fe commit 227027f

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

app/Entities/Tools/PageContent.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace BookStack\Entities\Tools;
44

5+
use BookStack\App\AppVersion;
56
use BookStack\Entities\Models\Page;
67
use BookStack\Entities\Queries\PageQueries;
78
use BookStack\Entities\Tools\Markdown\MarkdownToHtml;
@@ -321,13 +322,12 @@ public function render(bool $blankIncludes = false): string
321322
$cacheKey = $this->getContentCacheKey($doc->getBodyInnerHtml());
322323
$cached = cache()->get($cacheKey, null);
323324
if ($cached !== null) {
324-
// return $cached;
325+
return $cached;
325326
}
326327

327328
$filterConfig = HtmlContentFilterConfig::fromConfigString(config('app.content_filtering'));
328329
$filter = new HtmlContentFilter($filterConfig);
329330
$filtered = $filter->filterDocument($doc);
330-
// $filtered = $doc->getBodyInnerHtml();
331331

332332
$cacheTime = 86400 * 7; // 1 week
333333
cache()->put($cacheKey, $filtered, $cacheTime);
@@ -340,7 +340,8 @@ protected function getContentCacheKey(string $html): string
340340
$contentHash = md5($html);
341341
$contentId = $this->page->id;
342342
$contentTime = $this->page->updated_at->timestamp;
343-
return "page-content-cache::{$contentId}::{$contentTime}::{$contentHash}";
343+
$appVersion = AppVersion::get();
344+
return "page-content-cache::{$appVersion}::{$contentId}::{$contentTime}::{$contentHash}";
344345
}
345346

346347
/**

app/Util/ConfiguredHtmlPurifier.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,29 @@
22

33
namespace BookStack\Util;
44

5+
use BookStack\App\AppVersion;
56
use HTMLPurifier;
67
use HTMLPurifier_Config;
8+
use HTMLPurifier_DefinitionCache_Serializer;
79
use HTMLPurifier_HTML5Config;
810
use HTMLPurifier_HTMLDefinition;
911

12+
/**
13+
* Provides a configured HTML Purifier instance.
14+
* https://github.com/ezyang/htmlpurifier
15+
* Also uses this to extend support to HTML5 elements:
16+
* https://github.com/xemlock/htmlpurifier-html5
17+
*/
1018
class ConfiguredHtmlPurifier
1119
{
1220
protected HTMLPurifier $purifier;
21+
protected static bool $cachedChecked = false;
1322

1423
public function __construct()
1524
{
1625
$config = HTMLPurifier_HTML5Config::createDefault();
1726
$this->setConfig($config);
27+
$this->resetCacheIfNeeded($config);
1828

1929
$htmlDef = $config->getDefinition('HTML', true, true);
2030
if ($htmlDef instanceof HTMLPurifier_HTMLDefinition) {
@@ -24,9 +34,29 @@ public function __construct()
2434
$this->purifier = new HTMLPurifier($config);
2535
}
2636

37+
protected function resetCacheIfNeeded(HTMLPurifier_Config $config): void
38+
{
39+
if (self::$cachedChecked) {
40+
return;
41+
}
42+
43+
$cachedForVersion = cache('htmlpurifier::cache-version');
44+
$appVersion = AppVersion::get();
45+
if ($cachedForVersion !== $appVersion) {
46+
foreach (['HTML', 'CSS', 'URI'] as $name) {
47+
$cache = new HTMLPurifier_DefinitionCache_Serializer($name);
48+
$cache->flush($config);
49+
}
50+
cache()->set('htmlpurifier::cache-version', $appVersion);
51+
}
52+
53+
self::$cachedChecked = true;
54+
}
55+
2756
protected function setConfig(HTMLPurifier_Config $config): void
2857
{
29-
$config->set('Cache.SerializerPath', storage_path('purifier'));
58+
$config->set('Cache.SerializerPath', storage_path('framework/purifier'));
59+
$config->set('Core.AllowHostnameUnderscore', true);
3060
$config->set('CSS.AllowTricky', true);
3161
$config->set('HTML.SafeIframe', true);
3262
$config->set('Attr.EnableID', true);
@@ -44,7 +74,7 @@ protected function setConfig(HTMLPurifier_Config $config): void
4474
'file' => true,
4575
]);
4676

47-
$config->set('Cache.DefinitionImpl', null); // Disable cache during testing
77+
// $config->set('Cache.DefinitionImpl', null); // Disable cache during testing
4878
}
4979

5080
public function configureDefinition(HTMLPurifier_HTMLDefinition $definition): void

0 commit comments

Comments
 (0)