From e85c56ccb87f7000f91472e50cb16d4ac0a304a4 Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Mon, 5 Nov 2012 09:36:54 +1300 Subject: [PATCH] MDL-36297 caching: added for HTML purified text --- lang/en/cache.php | 1 + lib/db/caches.php | 8 ++++++++ lib/weblib.php | 25 ++++++++++++++++++++----- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/lang/en/cache.php b/lang/en/cache.php index 6a42f24f024..371d7cb8f24 100644 --- a/lang/en/cache.php +++ b/lang/en/cache.php @@ -36,6 +36,7 @@ $string['cacheadmin'] = 'Cache administration'; $string['cacheconfig'] = 'Configuration'; $string['cachedef_databasemeta'] = 'Database meta information'; $string['cachedef_eventinvalidation'] = 'Event invalidation'; +$string['cachedef_htmlpurifier'] = 'HTML Purifier - cleaned content'; $string['cachedef_locking'] = 'Locking'; $string['cachedef_questiondata'] = 'Question definitions'; $string['cachedef_string'] = 'Language string cache'; diff --git a/lib/db/caches.php b/lib/db/caches.php index 6c3ebe0085a..44e7e376e78 100644 --- a/lib/db/caches.php +++ b/lib/db/caches.php @@ -75,4 +75,12 @@ $definitions = array( 'datasource' => 'question_finder', 'datasourcefile' => 'question/engine/bank.php', ), + + // HTML Purifier cache + // This caches the html purifier cleaned text. This is done because the text is usually cleaned once for every user + // and context combo. Text caching handles caching for the combonation, this cache is responsible for caching the + // cleaned text which is shareable. + 'htmlpurifier' => array( + 'mode' => cache_store::MODE_APPLICATION, + ) ); diff --git a/lib/weblib.php b/lib/weblib.php index 25b7bb9eb50..f198c14e2ce 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -1578,8 +1578,21 @@ function is_purify_html_necessary($text) { function purify_html($text, $options = array()) { global $CFG; - $type = !empty($options['allowid']) ? 'allowid' : 'normal'; static $purifiers = array(); + static $caches = array(); + + $type = !empty($options['allowid']) ? 'allowid' : 'normal'; + + if (!array_key_exists($type, $caches)) { + $caches[$type] = cache::make('core', 'htmlpurifier', array('type' => $type)); + } + $cache = $caches[$type]; + + $filteredtext = $cache->get($text); + if ($filteredtext !== false) { + return $filteredtext; + } + if (empty($purifiers[$type])) { // make sure the serializer dir exists, it should be fine if it disappears later during cache reset @@ -1627,15 +1640,17 @@ function purify_html($text, $options = array()) { $multilang = (strpos($text, 'class="multilang"') !== false); + $filteredtext = $text; if ($multilang) { - $text = preg_replace('//', '', $text); + $filteredtext = preg_replace('//', '', $filteredtext); } - $text = $purifier->purify($text); + $filteredtext = $purifier->purify($filteredtext); if ($multilang) { - $text = preg_replace('//', '', $text); + $filteredtext = preg_replace('//', '', $filteredtext); } + $cache->set($text, $filteredtext); - return $text; + return $filteredtext; } /**