MDL-36297 caching: added for HTML purified text

This commit is contained in:
Sam Hemelryk
2013-01-03 10:55:00 +13:00
parent 9da506c2a3
commit e85c56ccb8
3 changed files with 29 additions and 5 deletions
+1
View File
@@ -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';
+8
View File
@@ -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,
)
);
+20 -5
View File
@@ -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('/<span(\s+lang="([a-zA-Z0-9_-]+)"|\s+class="multilang"){2}\s*>/', '<span xxxlang="${2}">', $text);
$filteredtext = preg_replace('/<span(\s+lang="([a-zA-Z0-9_-]+)"|\s+class="multilang"){2}\s*>/', '<span xxxlang="${2}">', $filteredtext);
}
$text = $purifier->purify($text);
$filteredtext = $purifier->purify($filteredtext);
if ($multilang) {
$text = preg_replace('/<span xxxlang="([a-zA-Z0-9_-]+)">/', '<span lang="${1}" class="multilang">', $text);
$filteredtext = preg_replace('/<span xxxlang="([a-zA-Z0-9_-]+)">/', '<span lang="${1}" class="multilang">', $filteredtext);
}
$cache->set($text, $filteredtext);
return $text;
return $filteredtext;
}
/**