diff options
Diffstat (limited to 'modules')
-rw-r--r-- | modules/block/block.admin.inc | 2 | ||||
-rw-r--r-- | modules/block/block.api.php | 18 | ||||
-rw-r--r-- | modules/block/block.module | 81 | ||||
-rw-r--r-- | modules/book/book.module | 2 | ||||
-rw-r--r-- | modules/forum/forum.module | 99 | ||||
-rw-r--r-- | modules/locale/locale.module | 2 | ||||
-rw-r--r-- | modules/menu/menu.module | 2 | ||||
-rw-r--r-- | modules/node/node.module | 2 | ||||
-rw-r--r-- | modules/profile/profile.module | 2 | ||||
-rw-r--r-- | modules/search/search.module | 2 | ||||
-rw-r--r-- | modules/statistics/statistics.module | 2 | ||||
-rw-r--r-- | modules/system/system.module | 6 | ||||
-rw-r--r-- | modules/user/user.module | 4 |
13 files changed, 93 insertions, 131 deletions
diff --git a/modules/block/block.admin.inc b/modules/block/block.admin.inc index 30048c33d..a0ecb48df 100644 --- a/modules/block/block.admin.inc +++ b/modules/block/block.admin.inc @@ -467,7 +467,7 @@ function block_add_block_form_submit($form, &$form_state) { 'status' => 0, 'weight' => 0, 'delta' => $delta, - 'cache' => BLOCK_NO_CACHE, + 'cache' => DRUPAL_NO_CACHE, )); } } diff --git a/modules/block/block.api.php b/modules/block/block.api.php index d072a8c3e..4ea6844d5 100644 --- a/modules/block/block.api.php +++ b/modules/block/block.api.php @@ -26,18 +26,18 @@ * - 'info': (required) The human-readable name of the block. * - 'cache': A bitmask of flags describing how the block should behave with * respect to block caching. The following shortcut bitmasks are provided - * as constants in block.module: - * - BLOCK_CACHE_PER_ROLE (default): The block can change depending on the + * as constants in common.inc: + * - DRUPAL_CACHE_PER_ROLE (default): The block can change depending on the * roles the user viewing the page belongs to. - * - BLOCK_CACHE_PER_USER: The block can change depending on the user + * - DRUPAL_CACHE_PER_USER: The block can change depending on the user * viewing the page. This setting can be resource-consuming for sites * with large number of users, and should only be used when - * BLOCK_CACHE_PER_ROLE is not sufficient. - * - BLOCK_CACHE_PER_PAGE: The block can change depending on the page + * DRUPAL_CACHE_PER_ROLE is not sufficient. + * - DRUPAL_CACHE_PER_PAGE: The block can change depending on the page * being viewed. - * - BLOCK_CACHE_GLOBAL: The block is the same for every user on every + * - DRUPAL_CACHE_GLOBAL: The block is the same for every user on every * page where it is visible. - * - BLOCK_NO_CACHE: The block should not get cached. + * - DRUPAL_NO_CACHE: The block should not get cached. * - 'weight', 'status', 'region', 'visibility', 'pages': * You can give your blocks an explicit weight, enable them, limit them to * given pages, etc. These settings will be registered when the block is first @@ -58,12 +58,12 @@ function hook_block_info() { 'weight' => 0, 'status' => 1, 'region' => 'sidebar_first', - // BLOCK_CACHE_PER_ROLE will be assumed for block 0. + // DRUPAL_CACHE_PER_ROLE will be assumed for block 0. ); $blocks['amazing'] = array( 'info' => t('An amazing block provided by Mymodule.'), - 'cache' => BLOCK_CACHE_PER_ROLE | BLOCK_CACHE_PER_PAGE, + 'cache' => DRUPAL_CACHE_PER_ROLE | DRUPAL_CACHE_PER_PAGE, ); return $blocks; diff --git a/modules/block/block.module b/modules/block/block.module index 92c8fc542..803c477fe 100644 --- a/modules/block/block.module +++ b/modules/block/block.module @@ -12,55 +12,6 @@ define('BLOCK_REGION_NONE', -1); /** - * Constants defining cache granularity for blocks. - * - * Modules specify the caching patterns for their blocks using binary - * combinations of these constants in their hook_block_info(): - * $block[delta]['cache'] = BLOCK_CACHE_PER_ROLE | BLOCK_CACHE_PER_PAGE; - * BLOCK_CACHE_PER_ROLE is used as a default when no caching pattern is - * specified. - * - * The block cache is cleared in cache_clear_all(), and uses the same clearing - * policy than page cache (node, comment, user, taxonomy added or updated...). - * Blocks requiring more fine-grained clearing might consider disabling the - * built-in block cache (BLOCK_NO_CACHE) and roll their own. - * - * Note that user 1 is excluded from block caching. - */ - -/** - * The block should not get cached. This setting should be used: - * - for simple blocks (notably those that do not perform any db query), - * where querying the db cache would be more expensive than directly generating - * the content. - * - for blocks that change too frequently. - */ -define('BLOCK_NO_CACHE', -1); - -/** - * The block can change depending on the roles the user viewing the page belongs to. - * This is the default setting, used when the block does not specify anything. - */ -define('BLOCK_CACHE_PER_ROLE', 0x0001); - -/** - * The block can change depending on the user viewing the page. - * This setting can be resource-consuming for sites with large number of users, - * and thus should only be used when BLOCK_CACHE_PER_ROLE is not sufficient. - */ -define('BLOCK_CACHE_PER_USER', 0x0002); - -/** - * The block can change depending on the page being viewed. - */ -define('BLOCK_CACHE_PER_PAGE', 0x0004); - -/** - * The block is the same for every user on every page where it is visible. - */ -define('BLOCK_CACHE_GLOBAL', 0x0008); - -/** * Implement hook_help(). */ function block_help($path, $arg) { @@ -196,7 +147,7 @@ function block_block_info() { foreach ($result as $block) { $blocks[$block->bid]['info'] = $block->info; // Not worth caching. - $blocks[$block->bid]['cache'] = BLOCK_NO_CACHE; + $blocks[$block->bid]['cache'] = DRUPAL_NO_CACHE; } return $blocks; } @@ -707,7 +658,7 @@ function block_block_info_alter(&$blocks) { * An array of block objects such as returned for one region by _block_load_blocks(). * * @return - * An array of visible blocks with subject and content rendered. + * An array of visible blocks as expected by drupal_render(). */ function _block_render_blocks($region_blocks) { foreach ($region_blocks as $key => $block) { @@ -772,37 +723,17 @@ function _block_render_blocks($region_blocks) { * The string used as cache_id for the block. */ function _block_get_cache_id($block) { - global $theme, $base_root, $user; - + global $user; + // User 1 being out of the regular 'roles define permissions' schema, // it brings too many chances of having unwanted output get in the cache // and later be served to other users. We therefore exclude user 1 from // block caching. - if (variable_get('block_cache', 0) && $block->cache != BLOCK_NO_CACHE && $user->uid != 1) { - $cid_parts = array(); - + if (variable_get('block_cache', 0) && !in_array($block->cache, array(DRUPAL_NO_CACHE, DRUPAL_CACHE_CUSTOM)) && $user->uid != 1) { // Start with common sub-patterns: block identification, theme, language. $cid_parts[] = $block->module; $cid_parts[] = $block->delta; - $cid_parts[] = $theme; - if (module_exists('locale')) { - global $language; - $cid_parts[] = $language->language; - } - - // 'PER_ROLE' and 'PER_USER' are mutually exclusive. 'PER_USER' can be a - // resource drag for sites with many users, so when a module is being - // equivocal, we favor the less expensive 'PER_ROLE' pattern. - if ($block->cache & BLOCK_CACHE_PER_ROLE) { - $cid_parts[] = 'r.' . implode(',', array_keys($user->roles)); - } - elseif ($block->cache & BLOCK_CACHE_PER_USER) { - $cid_parts[] = "u.$user->uid"; - } - - if ($block->cache & BLOCK_CACHE_PER_PAGE) { - $cid_parts[] = $base_root . request_uri(); - } + $cid_parts += drupal_render_cid_parts($block->cache); return implode(':', $cid_parts); } diff --git a/modules/book/book.module b/modules/book/book.module index 1eab5d699..ca49a98c4 100644 --- a/modules/book/book.module +++ b/modules/book/book.module @@ -212,7 +212,7 @@ function book_field_build_modes($obj_type) { function book_block_info() { $block = array(); $block['navigation']['info'] = t('Book navigation'); - $block['navigation']['cache'] = BLOCK_CACHE_PER_PAGE | BLOCK_CACHE_PER_ROLE; + $block['navigation']['cache'] = DRUPAL_CACHE_PER_PAGE | DRUPAL_CACHE_PER_ROLE; return $block; } diff --git a/modules/forum/forum.module b/modules/forum/forum.module index d18be27b7..5bedba1f2 100644 --- a/modules/forum/forum.module +++ b/modules/forum/forum.module @@ -479,8 +479,14 @@ function forum_form_alter(&$form, $form_state, $form_id) { * Implement hook_block_info(). */ function forum_block_info() { - $blocks['active']['info'] = t('Active forum topics'); - $blocks['new']['info'] = t('New forum topics'); + $blocks['active'] = array( + 'info' => t('Active forum topics'), + 'cache' => DRUPAL_CACHE_CUSTOM, + ); + $blocks['new'] = array( + 'info' => t('New forum topics'), + 'cache' => DRUPAL_CACHE_CUSTOM, + ); return $blocks; } @@ -506,41 +512,66 @@ function forum_block_save($delta = '', $edit = array()) { * most recently added forum topics. */ function forum_block_view($delta = '') { - if (user_access('access content')) { - $query = db_select('node', 'n'); - $query->join('forum', 'f', 'f.vid = n.vid'); - $query->join('taxonomy_term_data', 'td', 'td.tid = f.tid'); - $query->join('node_comment_statistics', 'ncs', 'n.nid = ncs.nid'); - $query - ->fields('n', array('nid', 'title')) - ->fields('ncs', array('comment_count', 'last_comment_timestamp')) - ->condition('n.status', 1) - ->condition('td.vid', variable_get('forum_nav_vocabulary', 0)) - ->addTag('node_access'); - switch ($delta) { - case 'active': - $title = t('Active forum topics'); - $query - ->orderBy('ncs.last_comment_timestamp', 'DESC') - ->range(0, variable_get('forum_block_num_active', '5')); - break; + $query = db_select('node', 'n'); + $query->join('forum', 'f', 'f.vid = n.vid'); + $query->join('taxonomy_term_data', 'td', 'td.tid = f.tid'); + $query->join('node_comment_statistics', 'ncs', 'n.nid = ncs.nid'); + $query + ->fields('n', array('nid', 'title')) + ->fields('ncs', array('comment_count', 'last_comment_timestamp')) + ->condition('n.status', 1) + ->condition('td.vid', variable_get('forum_nav_vocabulary', 0)) + ->addTag('node_access'); + switch ($delta) { + case 'active': + $title = t('Active forum topics'); + $query + ->orderBy('ncs.last_comment_timestamp', 'DESC') + ->range(0, variable_get('forum_block_num_active', '5')); + break; - case 'new': - $title = t('New forum topics'); - $query - ->orderBy('n.nid', 'DESC') - ->range(0, variable_get('forum_block_num_new', '5')); - break; - } + case 'new': + $title = t('New forum topics'); + $query + ->orderBy('n.nid', 'DESC') + ->range(0, variable_get('forum_block_num_new', '5')); + break; + } - $result = $query->execute(); - $content = node_title_list($result); - if (!empty($content)) { - $block['subject'] = $title; - $block['content'] = $content . theme('more_link', url('forum'), t('Read the latest forum topics.')); - return $block; - } + $cache_keys[] = 'forum'; + $cache_keys[] = $delta; + // Cache based on the altered query. Enables us to cache with node access enabled. + $query->preExecute(); + $cache_keys[] = md5(serialize(array((string) $query, $query->getArguments()))); + + $block['subject'] = $title; + $block['content'] = array( + '#access' => user_access('access content'), + '#pre_render' => array('forum_block_view_pre_render'), + '#cache' => array( + 'keys' => $cache_keys, + 'expire' => CACHE_TEMPORARY, + ), + '#query' => $query, + ); + return $block; +} + +/** +* A #pre_render callback. Lists nodes based on the element's #query property. +* +* @see forum_block_view(). +* +* @return +* A renderable array. +*/ +function forum_block_view_pre_render($elements) { + $result = $elements['#query']->execute(); + if ($node_title_list = node_title_list($result)) { + $elements['forum_list'] = array('#markup' => $node_title_list); + $elements['forum_more'] = array('#markup' => theme('more_link', url('forum'), t('Read the latest forum topics.'))); } + return $elements; } /** diff --git a/modules/locale/locale.module b/modules/locale/locale.module index 65d6c20c4..524e117e0 100644 --- a/modules/locale/locale.module +++ b/modules/locale/locale.module @@ -634,7 +634,7 @@ function locale_css_alter(&$css) { function locale_block_info() { $block['language-switcher']['info'] = t('Language switcher'); // Not worth caching. - $block['language-switcher']['cache'] = BLOCK_NO_CACHE; + $block['language-switcher']['cache'] = DRUPAL_NO_CACHE; return $block; } diff --git a/modules/menu/menu.module b/modules/menu/menu.module index dc83ef640..b27217b33 100644 --- a/modules/menu/menu.module +++ b/modules/menu/menu.module @@ -285,7 +285,7 @@ function menu_block_info() { $blocks[$name]['info'] = check_plain($title); // Menu blocks can't be cached because each menu item can have // a custom access callback. menu.inc manages its own caching. - $blocks[$name]['cache'] = BLOCK_NO_CACHE; + $blocks[$name]['cache'] = DRUPAL_NO_CACHE; } return $blocks; } diff --git a/modules/node/node.module b/modules/node/node.module index 9a4efdfff..8c35dc9d8 100644 --- a/modules/node/node.module +++ b/modules/node/node.module @@ -1751,7 +1751,7 @@ function node_revision_list($node) { function node_block_info() { $blocks['syndicate']['info'] = t('Syndicate'); // Not worth caching. - $blocks['syndicate']['cache'] = BLOCK_NO_CACHE; + $blocks['syndicate']['cache'] = DRUPAL_NO_CACHE; return $blocks; } diff --git a/modules/profile/profile.module b/modules/profile/profile.module index e7d7ac19d..efe8830fe 100644 --- a/modules/profile/profile.module +++ b/modules/profile/profile.module @@ -137,7 +137,7 @@ function profile_menu() { */ function profile_block_info() { $blocks['author-information']['info'] = t('Author information'); - $blocks['author-information']['cache'] = BLOCK_CACHE_PER_PAGE | BLOCK_CACHE_PER_ROLE; + $blocks['author-information']['cache'] = DRUPAL_CACHE_PER_PAGE | DRUPAL_CACHE_PER_ROLE; return $blocks; } diff --git a/modules/search/search.module b/modules/search/search.module index 8c6e1933b..65dfb84e6 100644 --- a/modules/search/search.module +++ b/modules/search/search.module @@ -166,7 +166,7 @@ function search_permission() { function search_block_info() { $blocks['form']['info'] = t('Search form'); // Not worth caching. - $blocks['form']['cache'] = BLOCK_NO_CACHE; + $blocks['form']['cache'] = DRUPAL_NO_CACHE; return $blocks; } diff --git a/modules/statistics/statistics.module b/modules/statistics/statistics.module index 3e5ed0ec5..be4f4af0c 100644 --- a/modules/statistics/statistics.module +++ b/modules/statistics/statistics.module @@ -300,7 +300,7 @@ function statistics_block_info() { if (variable_get('statistics_count_content_views', 0)) { $blocks['popular']['info'] = t('Popular content'); // Too dynamic to cache. - $blocks['popular']['cache'] = BLOCK_NO_CACHE; + $blocks['popular']['cache'] = DRUPAL_NO_CACHE; return $blocks; } } diff --git a/modules/system/system.module b/modules/system/system.module index bd72b1fd8..c7ca46cc3 100644 --- a/modules/system/system.module +++ b/modules/system/system.module @@ -1576,12 +1576,12 @@ function system_block_info() { $blocks['main'] = array( 'info' => t('Main page content'), // Cached elsewhere. - 'cache' => BLOCK_NO_CACHE, + 'cache' => DRUPAL_NO_CACHE, ); $blocks['powered-by'] = array( 'info' => t('Powered by Drupal'), 'weight' => '10', - 'cache' => BLOCK_NO_CACHE, + 'cache' => DRUPAL_NO_CACHE, ); $blocks['help'] = array( 'info' => t('System help'), @@ -1592,7 +1592,7 @@ function system_block_info() { $blocks[$menu_name]['info'] = t($title); // Menu blocks can't be cached because each menu item can have // a custom access callback. menu.inc manages its own caching. - $blocks[$menu_name]['cache'] = BLOCK_NO_CACHE; + $blocks[$menu_name]['cache'] = DRUPAL_NO_CACHE; } return $blocks; } diff --git a/modules/user/user.module b/modules/user/user.module index 72ffbfdcb..bfabe61d0 100644 --- a/modules/user/user.module +++ b/modules/user/user.module @@ -998,13 +998,13 @@ function user_block_info() { $blocks['login']['info'] = t('User login'); // Not worth caching. - $blocks['login']['cache'] = BLOCK_NO_CACHE; + $blocks['login']['cache'] = DRUPAL_NO_CACHE; $blocks['new']['info'] = t('Who\'s new'); // Too dynamic to cache. $blocks['online']['info'] = t('Who\'s online'); - $blocks['online']['cache'] = BLOCK_NO_CACHE; + $blocks['online']['cache'] = DRUPAL_NO_CACHE; return $blocks; } |