Index: modules/system/system.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.test,v
retrieving revision 1.74
diff -u -r1.74 system.test
--- modules/system/system.test	26 Aug 2009 10:53:45 -0000	1.74
+++ modules/system/system.test	30 Aug 2009 10:23:49 -0000
@@ -930,6 +930,102 @@
 
 }
 
+/**
+ * Test form main content render fallback provided by system module.
+ */
+class SystemMainContentFallback extends DrupalWebTestCase {
+  protected $admin_user;
+  protected $web_user;
+
+  public static function getInfo() {
+    return array(
+      'name' => 'Main content rendering fallback',
+      'description' => ' Test system module main content rendering fallback.',
+      'group' => 'System',
+    );
+  }
+
+  function setUp() {
+    parent::setUp('system_test');
+
+    // Create and login admin user
+    $this->admin_user = $this->drupalCreateUser(array(
+      'access administration pages',
+      'administer site configuration',
+      'administer blocks',
+      'administer nodes'
+    ));
+    $this->drupalLogin($this->admin_user);
+
+    // Create a web user.
+    $this->web_user = $this->drupalCreateUser(array('access user profiles', 'access content'));
+
+    // system_test.module should go before system.module.
+    db_update('system')
+      ->fields(array('weight' => -3))
+      ->condition('name', 'system_test')
+      ->execute();
+  }
+
+  /**
+   * Test displaying and hiding the powered-by block.
+   */
+  function testMainContentFallback() {
+    $edit = array();
+    $edit['modules[Core][block][enable]'] = FALSE;
+    $this->drupalPost('admin/config/modules', $edit, t('Save configuration'));
+    $this->assertText(t('The configuration options have been saved.'), t('Modules status has been updated.'));
+    module_list(TRUE);
+    $this->assertFalse(module_exists('block'), t('block.module disabled.'));
+
+    // At this point, block.module is disabled and system.module fallback
+    // should be used.
+    $this->drupalGet('admin/content');
+    $this->assertField('operation', t('Main content found.'));
+    $this->assertNoRaw('id="block-system-main"', t('Main content block rendered by system fallback.'));
+
+    // Test that content assigned to regions is entirely controlled by a
+    // module. The system_test module will generate content in a region of the
+    // page, and no other region will have content assigned.
+    $this->drupalGet('system-test/page-alter');
+    $this->assertRaw('id="system-test-page-alter"', t('Content rendered by a module.'));
+
+    // Enable block.module.
+    $edit = array();
+    $edit['modules[Core][block][enable]'] = 'block';
+    $this->drupalPost('admin/config/modules', $edit, t('Save configuration'));
+    $this->assertText(t('The configuration options have been saved.'), t('Modules status has been updated.'));
+    module_list(TRUE);
+    $this->assertTrue(module_exists('block'), t('block.module enabled.'));
+
+    // Enable the main content block, to be displayed in the footer region.
+    $edit = array();
+    $edit['system_main[region]'] = 'footer';
+    $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
+    $this->assertText(t('The block settings have been updated.'), t('Block successfully move to footer region.'));
+
+    $this->drupalGet('admin/content');
+    $this->assertField('operation', t('Main content block found.'));
+    $this->assertRaw('id="block-system-main"', t('Main content block rendered by block.module.'));
+
+    // Set the block to be hidden on any user path.
+    $edit = array();
+    $edit['pages'] = 'user*';
+    $this->drupalPost('admin/structure/block/configure/system/main', $edit, t('Save block'));
+
+    // Request a user* page
+    $this->drupalLogin($this->web_user);
+    $this->drupalGet('user/' . $this->web_user->uid . '/edit');
+    $this->assertField('mail', t('Main content block found.'));
+    $this->assertNoRaw('id="block-system-main"', t('Main content block rendered by system fallback.'));
+
+    // Request other page different to user*.
+    $this->drupalGet('node');
+    $this->assertRaw('id="block-system-main"', t('Main content block rendered by block.module.'));
+  }
+
+}
+
 class SystemSettingsForm extends DrupalWebTestCase {
   /**
    * Implement getInfo().
@@ -1218,7 +1314,7 @@
     // passed properly through the call stack and being handled correctly by a 'known'
     // token, [node:title].
     $this->assertFalse(strcmp($target, $result), t('Basic placeholder tokens replaced.'));
-    
+
     $raw_tokens = array('title' => '[node:title]');
     $generated = token_generate('node', $raw_tokens, array('node' => $node));
     $this->assertFalse(strcmp($generated['[node:title]'], check_plain($node->title)), t('Token sanitized.'));
Index: modules/system/system.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.module,v
retrieving revision 1.780
diff -u -r1.780 system.module
--- modules/system/system.module	29 Aug 2009 05:46:04 -0000	1.780
+++ modules/system/system.module	30 Aug 2009 10:23:41 -0000
@@ -3078,6 +3078,16 @@
  * Implement hook_page_alter().
  */
 function system_page_alter(&$page) {
+  // If no module has populated any region with main content block provided by
+  // system module, then provide a fallback implementation which inserts the
+  // main page content into the 'content' region. This allows the site to still
+  // be usable even if no modules that control page regions inserted the main
+  // content block. If a module inserts the main content block in at least one
+  // region, then $page['#no_render_main_content'] must be set to TRUE.
+  if (empty($page['#no_render_main_content'])) {
+    $page['content']['system_main'] = drupal_set_page_content();
+  }
+
   // Automatic cron runs.
   // @see system_run_cron_image()
   if (system_run_cron_image_access()) {
Index: modules/simpletest/tests/system_test.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/system_test.module,v
retrieving revision 1.15
diff -u -r1.15 system_test.module
--- modules/simpletest/tests/system_test.module	17 Aug 2009 20:32:30 -0000	1.15
+++ modules/simpletest/tests/system_test.module	30 Aug 2009 10:23:14 -0000
@@ -73,6 +73,13 @@
     'type' => MENU_CALLBACK,
   );
 
+  $items['system-test/page-alter'] = array(
+    'title' => 'Test hook_page_alter()',
+    'page callback' => 'system_test_hook_page_alter',
+    'access callback' => TRUE,
+    'type' => MENU_CALLBACK,
+  );
+
   return $items;
 }
 
@@ -225,3 +232,24 @@
     return 'FALSE: Lock not acquired in system_test_lock_exit()';
   }
 }
+
+/**
+ * Implement hook_page_alter().
+ */
+function system_test_page_alter(&$page) {
+  $menu_item = menu_get_item();
+  if ($menu_item['path'] == 'system-test/page-alter') {
+    $page['#no_render_main_content'] = TRUE;
+    $page['footer'] = array(
+      '#markup' => '<div id="system-test-page-alter">System test page alter</div>',
+    );
+  }
+}
+
+/**
+ * Menu callback to test hook_page_alter().
+ */
+function system_test_hook_page_alter() {
+ // Do nothing, hook_page_alter() will populate a region content.
+ return 'testing hook_page_alter()';
+}
Index: modules/block/block.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/block/block.module,v
retrieving revision 1.371
diff -u -r1.371 block.module
--- modules/block/block.module	29 Aug 2009 05:46:02 -0000	1.371
+++ modules/block/block.module	30 Aug 2009 10:23:12 -0000
@@ -243,12 +243,13 @@
 
   // Populate all block regions
   $all_regions = system_region_list($theme);
-
+  $system_main_block = FALSE;
   // Load all region content assigned via blocks.
   foreach (array_keys($all_regions) as $region) {
     // Assign blocks to region.
     if ($blocks = block_get_blocks_by_region($region)) {
       $page[$region] = $blocks;
+      $system_main_block |= isset($blocks['system_main']);
     }
 
     // Append region description if we are rendering the block admin page.
@@ -264,6 +265,9 @@
       }
     }
   }
+
+  // Let other modules know if the main content block was already inserted.
+  $page['#no_render_main_content'] = $system_main_block;  
 }
 
 /**
