Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-36297 caching: added for HTML purified text

  • Loading branch information...
commit e85c56ccb87f7000f91472e50cb16d4ac0a304a4 1 parent 9da506c
@samhemelryk samhemelryk authored
Showing with 29 additions and 5 deletions.
  1. +1 −0  lang/en/cache.php
  2. +8 −0 lib/db/caches.php
  3. +20 −5 lib/weblib.php
View
1  lang/en/cache.php
@@ -36,6 +36,7 @@
$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';
View
8 lib/db/caches.php
@@ -75,4 +75,12 @@
'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,
+ )
);
View
25 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('/<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;
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.