From dcdc4f2e73d09d648b7a2160738771aa0e9925aa Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Fri, 29 May 2009 20:06:44 +0000 Subject: [PATCH] - Patch #369011 by catch, sun, et al: performance improvement in text_field_load(). --- modules/field/modules/text/text.module | 58 ++++++++++++++++++++++---- modules/filter/filter.module | 10 +++-- 2 files changed, 57 insertions(+), 11 deletions(-) diff --git a/modules/field/modules/text/text.module b/modules/field/modules/text/text.module index b6eba9f1207..198b69e7def 100644 --- a/modules/field/modules/text/text.module +++ b/modules/field/modules/text/text.module @@ -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) : ''; + // 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; } - else { - $text = check_plain($item['value']); - } - $items[$delta]['safe'] = $text; } } diff --git a/modules/filter/filter.module b/modules/filter/filter.module index f3ccc040a08..40d1af58cab 100644 --- a/modules/filter/filter.module +++ b/modules/filter/filter.module @@ -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)); } }