Commit dcdc4f2e authored by Dries's avatar Dries
Browse files

- Patch #369011 by catch, sun, et al: performance improvement in text_field_load().

parent ccd093b1
......@@ -110,18 +110,60 @@ function text_field_validate($obj_type, $object, $field, $instance, $items, &$er
}
}
/**
* Implement hook_field_load().
*
* Where possible, generate the sanitized version of each field early so that
* it is cached in the field cache. This avoids looking up from the filter cache
* separately.
* @see text_field_sanitize().
*/
function text_field_load($obj_type, $objects, $field, $instances, &$items) {
global $language;
foreach ($objects as $id => $object) {
foreach ($items[$id] as $delta => $item) {
// TODO D7 : this code is really node-related.
if (!empty($instances[$id]['settings']['text_processing'])) {
$check = is_null($object) || (isset($object->build_mode) && $object->build_mode == NODE_BUILD_PREVIEW);
// Only process items with a cacheable format, the rest will be
// handled by text_field_sanitize().
if (filter_format_allowcache($item['format'])) {
$text = isset($item['value']) ? check_markup($item['value'], $item['format'], isset($object->language) ? $object->language : $language->language, $check, FALSE) : '';
}
}
else {
$text = check_plain($item['value']);
}
if (isset($text)) {
$items[$id][$delta]['safe'] = $text;
}
}
}
}
/**
* Implement hook_field_sanitize().
*
* @see text_field_load()
*/
function text_field_sanitize($obj_type, $object, $field, $instance, &$items) {
global $language;
foreach ($items as $delta => $item) {
// TODO D7 : this code is really node-related.
if (!empty($instance['settings']['text_processing'])) {
$check = is_null($object) || (isset($object->build_mode) && $object->build_mode == NODE_BUILD_PREVIEW);
$text = isset($item['value']) ? check_markup($item['value'], $item['format'], isset($object->language) ? $object->language : $language->language, $check) : '';
}
else {
$text = check_plain($item['value']);
// Only sanitize items which were not already processed inside
// text_field_load(), i.e. items with uncacheable text formats.
if (!isset($items[$delta]['safe'])) {
// TODO D7 : this code is really node-related.
if (!empty($instance['settings']['text_processing'])) {
$check = is_null($object) || (isset($object->build_mode) && $object->build_mode == NODE_BUILD_PREVIEW);
$text = isset($item['value']) ? check_markup($item['value'], $item['format'], isset($object->language) ? $object->language : $language->language, $check) : '';
}
else {
$text = check_plain($item['value']);
}
$items[$delta]['safe'] = $text;
}
$items[$delta]['safe'] = $text;
}
}
......
......@@ -423,15 +423,19 @@ function filter_list_format($format) {
* should specify $check = FALSE when viewing other people's content. When
* showing content that is not (yet) stored in the database (eg. upon preview),
* set to TRUE so the user's permissions are checked.
* @param $cache
* Boolean whether to cache the filtered output in the {cache_filter} table.
* The caller may set this to FALSE when the output is already cached
* elsewhere to avoid duplicate cache lookups and storage.
*/
function check_markup($text, $format = FILTER_FORMAT_DEFAULT, $langcode = '', $check = TRUE) {
function check_markup($text, $format = FILTER_FORMAT_DEFAULT, $langcode = '', $check = TRUE, $cache = TRUE) {
// When $check = TRUE, do an access check on $format.
if (isset($text) && (!$check || filter_access($format))) {
$format = filter_resolve_format($format);
// Check for a cached version of this piece of text.
$cache_id = $format . ':' . $langcode . ':' . md5($text);
if ($cached = cache_get($cache_id, 'cache_filter')) {
if ($cache && $cached = cache_get($cache_id, 'cache_filter')) {
return $cached->data;
}
......@@ -453,7 +457,7 @@ function check_markup($text, $format = FILTER_FORMAT_DEFAULT, $langcode = '', $c
}
// Store in cache with a minimum expiration time of 1 day.
if (filter_format_allowcache($format)) {
if ($cache && filter_format_allowcache($format)) {
cache_set($cache_id, $text, 'cache_filter', REQUEST_TIME + (60 * 60 * 24));
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment