Index: modules/block/block.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/block/block.module,v
retrieving revision 1.307
diff -u -r1.307 block.module
--- modules/block/block.module	26 May 2008 17:12:54 -0000	1.307
+++ modules/block/block.module	2 Jul 2008 01:58:14 -0000
@@ -215,78 +215,100 @@
 /**
  * Update the 'blocks' DB table with the blocks currently exported by modules.
  *
+ * @param $theme
+ *   The name of the theme whose blocks should be updated.
+ *   If NULL, updates blocks for all enabled themes.
+ *
  * @return
- *   Blocks currently exported by modules.
+ *   An array of blocks for themes, keyed by theme names.
  */
-function _block_rehash() {
-  global $theme_key;
-
-  init_theme();
+function _block_rehash($only_theme = NULL) {
+  if (is_null($only_theme)) {
+    foreach(list_themes() as $theme) {
+      if ($theme->status || $theme->name == variable_get('admin_theme', '0')) {
+        $themes[] = $theme->name;
+      }
+    }
+    $result = db_query("SELECT * FROM {blocks}");
+  }
+  else {
+    $themes = array($only_theme);
+    $result = db_query("SELECT * FROM {blocks} WHERE theme = '%s'", $only_theme);
+  }
 
-  $result = db_query("SELECT * FROM {blocks} WHERE theme = '%s'", $theme_key);
   $old_blocks = array();
   while ($old_block = db_fetch_array($result)) {
-    $old_blocks[$old_block['module']][$old_block['delta']] = $old_block;
+    $old_blocks[$old_block['theme']][$old_block['module']][$old_block['delta']] = $old_block;
   }
 
   $blocks = array();
-  // Valid region names for the theme.
-  $regions = system_region_list($theme_key);
 
   foreach (module_list() as $module) {
     $module_blocks = module_invoke($module, 'block', 'list');
     if ($module_blocks) {
-      foreach ($module_blocks as $delta => $block) {
-        if (empty($old_blocks[$module][$delta])) {
-          // If it's a new block, add identifiers.
-          $block['module'] = $module;
-          $block['delta']  = $delta;
-          $block['theme']  = $theme_key;
-          if (!isset($block['pages'])) {
-            // {block}.pages is type 'text', so it cannot have a
-            // default value, and not null, so we need to provide
-            // value if the module did not.
-            $block['pages']  = '';
-          }
-          // Add defaults and save it into the database.
-          drupal_write_record('blocks', $block);
-          // Set region to none if not enabled.
-          $block['region'] = $block['status'] ? $block['region'] : BLOCK_REGION_NONE;
-          // Add to the list of blocks we return.
-          $blocks[] = $block;
-        }
-        else {
-          // If it's an existing block, database settings should overwrite
-          // the code. But aside from 'info' everything that's definable in
-          // code is stored in the database and we do not store 'info', so we
-          // do not need to update the database here.
-          // Add 'info' to this block.
-          $old_blocks[$module][$delta]['info'] = $block['info'];
-          // If the region name does not exist, disable the block and assign it to none.
-          if (!empty($old_blocks[$module][$delta]['region']) && !isset($regions[$old_blocks[$module][$delta]['region']])) {
-            drupal_set_message(t('The block %info was assigned to the invalid region %region and has been disabled.', array('%info' => $old_blocks[$module][$delta]['info'], '%region' => $old_blocks[$module][$delta]['region'])), 'warning');
-            $old_blocks[$module][$delta]['status'] = 0;
-            $old_blocks[$module][$delta]['region'] = BLOCK_REGION_NONE;
+      foreach($themes as $theme) {
+        // Valid region names for the theme.
+        $regions = system_region_list($theme);
+
+        foreach ($module_blocks as $delta => $block) {
+          if (empty($old_blocks[$theme][$module][$delta])) {
+            // If it's a new block, add identifiers.
+            $block['module'] = $module;
+            $block['delta']  = $delta;
+            $block['theme']  = $theme;
+            if (!isset($block['pages'])) {
+              // {block}.pages is type 'text', so it cannot have a
+              // default value, and not null, so we need to provide
+              // value if the module did not.
+              $block['pages']  = '';
+            }
+            // Add defaults and save it into the database.
+            drupal_write_record('blocks', $block);
+            // Set region to none if not enabled.
+            $block['region'] = $block['status'] ? $block['region'] : BLOCK_REGION_NONE;
+            // Add to the list of blocks we return.
+            $blocks[$theme][] = $block;
           }
           else {
-            $old_blocks[$module][$delta]['region'] = $old_blocks[$module][$delta]['status'] ? $old_blocks[$module][$delta]['region'] : BLOCK_REGION_NONE;
+            $old_block = $old_blocks[$theme][$module][$delta];
+            // For existing block, database settings should overwrite the code,
+            // except for possible changes in the cache mode.
+            if ((!isset($block['cache']) && $old_block['cache'] != BLOCK_CACHE_PER_ROLE) || (isset($block['cache']) && $old_block['cache'] != $block['cache'])) {
+              $old_block['cache'] = isset($block['cache']) ? $block['cache'] : BLOCK_CACHE_PER_ROLE;
+              drupal_write_record('blocks', $old_block, array('module', 'delta', 'theme'));
+              cache_clear_all("$module:$delta:", 'cache_block', TRUE);
+            }
+            // 'info' is the only thing that's definable in code but not stored
+            // in the database.
+            $old_block['info'] = $block['info'];
+            // If the region name does not exist, disable the block and assign it to none.
+            if (!empty($old_block['region']) && !isset($regions[$old_block['region']])) {
+              drupal_set_message(t('The block %info was assigned to the invalid region %region and has been disabled.', array('%info' => $old_block['info'], '%region' => $old_block['region'])), 'warning');
+              $old_block['status'] = 0;
+              $old_block['region'] = BLOCK_REGION_NONE;
+            }
+            else {
+              $old_block['region'] = $old_block['status'] ? $old_block['region'] : BLOCK_REGION_NONE;
+            }
+            // Add this block to the list of blocks we return.
+            $blocks[$theme][] = $old_block;
+            // Remove this block from the list of blocks to be deleted.
+            unset($old_blocks[$theme][$module][$delta]);
           }
-          // Add this block to the list of blocks we return.
-          $blocks[] = $old_blocks[$module][$delta];
-          // Remove this block from the list of blocks to be deleted.
-          unset($old_blocks[$module][$delta]);
         }
       }
     }
   }
 
   // Remove blocks that are no longer defined by the code from the database.
-  foreach ($old_blocks as $module => $old_module_blocks) {
-    foreach ($old_module_blocks as $delta => $block) {
-      db_query("DELETE FROM {blocks} WHERE module = '%s' AND delta = '%s' AND theme = '%s'", $module, $delta, $theme_key);
+  foreach ($old_blocks as $theme => $old_theme_blocks) {
+    foreach ($old_theme_blocks as $module => $old_module_blocks) {
+      foreach ($old_module_blocks as $delta => $block) {
+        db_query("DELETE FROM {blocks} WHERE module = '%s' AND delta = '%s' AND theme = '%s'", $module, $delta, $theme);
+      }
     }
   }
-  return $blocks;
+  return is_null($only_theme) ? $blocks : $blocks[$only_theme];
 }
 
 function block_box_get($bid) {
Index: modules/block/block.admin.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/block/block.admin.inc,v
retrieving revision 1.18
diff -u -r1.18 block.admin.inc
--- modules/block/block.admin.inc	25 Jun 2008 09:52:41 -0000	1.18
+++ modules/block/block.admin.inc	2 Jul 2008 01:58:14 -0000
@@ -14,9 +14,10 @@
 
   // If non-default theme configuration has been selected, set the custom theme.
   $custom_theme = isset($theme) ? $theme : variable_get('theme_default', 'garland');
+  init_theme();
 
-  // Fetch and sort blocks.
-  $blocks = _block_rehash();
+  // Fetch and sort blocks
+  $blocks = _block_rehash($custom_theme);
   usort($blocks, '_block_compare');
 
   return drupal_get_form('block_admin_display_form', $blocks, $theme);
