Index: src/wp-admin/includes/upgrade.php
===================================================================
--- src/wp-admin/includes/upgrade.php	(revision 41547)
+++ src/wp-admin/includes/upgrade.php	(working copy)
@@ -432,11 +432,13 @@
 		upgrade_network();
 	wp_cache_flush();
 
+	$site_id = get_current_blog_id();
+
 	if ( is_multisite() ) {
-		if ( $wpdb->get_row( "SELECT blog_id FROM {$wpdb->blog_versions} WHERE blog_id = '{$wpdb->blogid}'" ) )
-			$wpdb->query( "UPDATE {$wpdb->blog_versions} SET db_version = '{$wp_db_version}' WHERE blog_id = '{$wpdb->blogid}'" );
+		if ( $wpdb->get_row( $wpdb->prepare( 'SELECT blog_id FROM %s WHERE blog_id = %d', $wpdb->blog_versions, $site_id ) ) )
+			$wpdb->query( $wpdb->prepare( 'UPDATE %s SET db_version = %d WHERE blog_id = %d', $wpdb->blog_versions, $wp_db_version, $site_id ) );
 		else
-			$wpdb->query( "INSERT INTO {$wpdb->blog_versions} ( `blog_id` , `db_version` , `last_updated` ) VALUES ( '{$wpdb->blogid}', '{$wp_db_version}', NOW());" );
+			$wpdb->query( $wpdb->prepare( 'INSERT INTO %s ( `blog_id` , `db_version` , `last_updated` ) VALUES ( %d, %d, %s);', $site_id, $wpdb->blog_versions, $wp_db_version, NOW() ) );
 	}
 
 	/**
@@ -1257,7 +1259,7 @@
 			}
 			$start += 20;
 		}
-		refresh_blog_details( $wpdb->blogid );
+		refresh_blog_details( get_current_blog_id() );
 	}
 }
 
Index: src/wp-admin/ms-delete-site.php
===================================================================
--- src/wp-admin/ms-delete-site.php	(revision 41547)
+++ src/wp-admin/ms-delete-site.php	(working copy)
@@ -17,7 +17,7 @@
 
 if ( isset( $_GET['h'] ) && $_GET['h'] != '' && get_option( 'delete_blog_hash' ) != false ) {
 	if ( hash_equals( get_option( 'delete_blog_hash' ), $_GET['h'] ) ) {
-		wpmu_delete_blog( $wpdb->blogid );
+		wpmu_delete_blog( get_current_blog_id() );
 		wp_die( sprintf( __( 'Thank you for using %s, your site has been deleted. Happy trails to you until we meet again.' ), get_network()->site_name ) );
 	} else {
 		wp_die( __( "I'm sorry, the link you clicked is stale. Please select another option." ) );
Index: src/wp-admin/user-new.php
===================================================================
--- src/wp-admin/user-new.php	(revision 41547)
+++ src/wp-admin/user-new.php	(working copy)
@@ -156,7 +156,7 @@
 				add_filter( 'wpmu_signup_user_notification', '__return_false' ); // Disable confirmation email
 				add_filter( 'wpmu_welcome_user_notification', '__return_false' ); // Disable welcome email
 			}
-			wpmu_signup_user( $new_user_login, $new_user_email, array( 'add_to_blog' => $wpdb->blogid, 'new_role' => $_REQUEST['role'] ) );
+			wpmu_signup_user( $new_user_login, $new_user_email, array( 'add_to_blog' => get_current_blog_id(), 'new_role' => $_REQUEST['role'] ) );
 			if ( isset( $_POST[ 'noconfirmation' ] ) && current_user_can( 'manage_network_users' ) ) {
 				$key = $wpdb->get_var( $wpdb->prepare( "SELECT activation_key FROM {$wpdb->signups} WHERE user_login = %s AND user_email = %s", $new_user_login, $new_user_email ) );
 				$new_user = wpmu_activate_signup( $key );
Index: src/wp-includes/ms-blogs.php
===================================================================
--- src/wp-includes/ms-blogs.php	(revision 41547)
+++ src/wp-includes/ms-blogs.php	(working copy)
@@ -12,13 +12,12 @@
  * Update the last_updated field for the current site.
  *
  * @since MU (3.0.0)
- *
- * @global wpdb $wpdb WordPress database abstraction object.
  */
 function wpmu_update_blogs_date() {
-	global $wpdb;
 
-	update_blog_details( $wpdb->blogid, array('last_updated' => current_time('mysql', true)) );
+	$site_id = get_current_blog_id();
+
+	update_blog_details( $site_id, array( 'last_updated' => current_time( 'mysql', true ) ) );
 	/**
 	 * Fires after the blog details are updated.
 	 *
@@ -26,7 +25,7 @@
 	 *
 	 * @param int $blog_id Site ID.
 	 */
-	do_action( 'wpmu_blog_updated', $wpdb->blogid );
+	do_action( 'wpmu_blog_updated', $site_id );
 }
 
 /**
Index: src/wp-includes/ms-default-constants.php
===================================================================
--- src/wp-includes/ms-default-constants.php	(revision 41547)
+++ src/wp-includes/ms-default-constants.php	(working copy)
@@ -14,11 +14,8 @@
  * wp-includes/ms-files.php (wp-content/blogs.php in MU).
  *
  * @since 3.0.0
- *
- * @global wpdb $wpdb WordPress database abstraction object.
  */
 function ms_upload_constants() {
-	global $wpdb;
 
 	// This filter is attached in ms-default-filters.php but that file is not included during SHORTINIT.
 	add_filter( 'default_site_option_ms_files_rewriting', '__return_true' );
@@ -30,14 +27,15 @@
 	if ( !defined( 'UPLOADBLOGSDIR' ) )
 		define( 'UPLOADBLOGSDIR', 'wp-content/blogs.dir' );
 
+	$site_id = get_current_blog_id();
 	// Note, the main site in a post-MU network uses wp-content/uploads.
 	// This is handled in wp_upload_dir() by ignoring UPLOADS for this case.
 	if ( ! defined( 'UPLOADS' ) ) {
-		define( 'UPLOADS', UPLOADBLOGSDIR . "/{$wpdb->blogid}/files/" );
+		define( 'UPLOADS', UPLOADBLOGSDIR . '/' . $site_id . '/files/' );
 
 		// Uploads dir relative to ABSPATH
 		if ( 'wp-content/blogs.dir' == UPLOADBLOGSDIR && ! defined( 'BLOGUPLOADDIR' ) )
-			define( 'BLOGUPLOADDIR', WP_CONTENT_DIR . "/blogs.dir/{$wpdb->blogid}/files/" );
+			define( 'BLOGUPLOADDIR', WP_CONTENT_DIR . '/blogs.dir/' . $site_id . '/files/' );
 	}
 }
 
Index: src/wp-includes/ms-functions.php
===================================================================
--- src/wp-includes/ms-functions.php	(revision 41547)
+++ src/wp-includes/ms-functions.php	(working copy)
@@ -34,19 +34,18 @@
  *
  * @since MU (3.0.0) 1.0
  *
- * @global wpdb $wpdb WordPress database abstraction object.
- *
  * @param int $user_id The unique ID of the user
  * @return WP_Site|void The blog object
  */
 function get_active_blog_for_user( $user_id ) {
-	global $wpdb;
 	$blogs = get_blogs_of_user( $user_id );
 	if ( empty( $blogs ) )
 		return;
 
-	if ( !is_multisite() )
-		return $blogs[$wpdb->blogid];
+	if ( ! is_multisite() ) {
+		$site_id = get_current_blog_id();
+		return $blogs[ $site_id ];
+	}
 
 	$primary_blog = get_user_meta( $user_id, 'primary_blog', true );
 	$first_blog = current($blogs);
@@ -2215,7 +2214,7 @@
 
 	$current_user = wp_get_current_user();
 	if ( $blog_id == 0 ) {
-		$blog_id = $wpdb->blogid;
+		$blog_id = get_current_blog_id();
 	}
 	$local_key = $wpdb->get_blog_prefix( $blog_id ) . $key;
 
Index: tests/phpunit/tests/multisite/getMainSiteId.php
===================================================================
--- tests/phpunit/tests/multisite/getMainSiteId.php	(revision 41547)
+++ tests/phpunit/tests/multisite/getMainSiteId.php	(nonexistent)
@@ -1,110 +0,0 @@
-<?php
-
-if ( is_multisite() ) :
-
-/**
- * Tests for the get_main_site_id() function.
- *
- * @group ms-site
- * @group multisite
- */
-class Tests_Multisite_Get_Main_Site_ID extends WP_UnitTestCase {
-	protected static $network_ids;
-	protected static $site_ids;
-
-	public static function wpSetUpBeforeClass( $factory ) {
-		self::$network_ids = array(
-			'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/' ),
-			'wp.org/'        => array( 'domain' => 'wp.org',        'path' => '/' ), // A network with no sites.
-		);
-
-		foreach ( self::$network_ids as &$id ) {
-			$id = $factory->network->create( $id );
-		}
-		unset( $id );
-
-		self::$site_ids = array(
-			'www.w.org/'                   => array( 'domain' => 'www.w.org',     'path' => '/' ),
-			'wordpress.org/'               => array( 'domain' => 'wordpress.org', 'path' => '/',     'site_id' => self::$network_ids['wordpress.org/'] ),
-			'wordpress.org/foo/'           => array( 'domain' => 'wordpress.org', 'path' => '/foo/', 'site_id' => self::$network_ids['wordpress.org/'] ),
-		);
-
-		foreach ( self::$site_ids as &$id ) {
-			$id = $factory->blog->create( $id );
-		}
-		unset( $id );
-	}
-
-	public static function wpTearDownAfterClass() {
-		foreach( self::$site_ids as $id ) {
-			wpmu_delete_blog( $id, true );
-		}
-
-		global $wpdb;
-
-		foreach( self::$network_ids as $id ) {
-			$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
-			$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
-		}
-
-		wp_update_network_site_counts();
-	}
-
-	/**
-	 * @ticket 29684
-	 */
-	public function test_get_main_site_id_on_main_site_returns_self() {
-		$this->assertSame( get_current_blog_id(), get_main_site_id() );
-	}
-
-	/**
-	 * @ticket 29684
-	 */
-	public function test_get_main_site_id_returns_main_site_in_switched_context() {
-		$main_site_id = get_current_blog_id();
-		$other_site_id = self::$site_ids['www.w.org/'];
-
-		switch_to_blog( $other_site_id );
-		$result = get_main_site_id();
-		restore_current_blog();
-
-		$this->assertSame( $main_site_id, $result );
-	}
-
-	/**
-	 * @ticket 29684
-	 */
-	public function test_get_main_site_id_with_different_network_returns_correct_id() {
-		$this->assertSame( self::$site_ids['wordpress.org/'], get_main_site_id( self::$network_ids['wordpress.org/'] ) );
-	}
-
-	/**
-	 * @ticket 29684
-	 */
-	public function test_get_main_site_id_on_network_without_site_returns_0() {
-		$this->assertSame( 0, get_main_site_id( self::$network_ids['wp.org/'] ) );
-	}
-
-	/**
-	 * @ticket 29684
-	 */
-	public function test_get_main_site_id_on_invalid_network_returns_0() {
-		$this->assertSame( 0, get_main_site_id( 333 ) );
-	}
-
-	/**
-	 * @ticket 29684
-	 */
-	public function test_get_main_site_id_filtered() {
-		add_filter( 'pre_get_main_site_id', array( $this, 'filter_get_main_site_id' ) );
-		$result = get_main_site_id();
-
-		$this->assertSame( 333, $result );
-	}
-
-	public function filter_get_main_site_id() {
-		return 333;
-	}
-}
-
-endif;
Index: tests/phpunit/tests/multisite/site.php
===================================================================
--- tests/phpunit/tests/multisite/site.php	(revision 41547)
+++ tests/phpunit/tests/multisite/site.php	(working copy)
@@ -363,7 +363,7 @@
 		wpmu_update_blogs_date();
 
 		// compare the update time with the current time, allow delta < 2
-		$blog = get_site( $wpdb->blogid );
+		$blog = get_site( get_current_blog_id() );
 		$current_time = time();
 		$time_difference = $current_time - strtotime( $blog->last_updated );
 		$this->assertLessThan( 2, $time_difference );
Index: tests/phpunit/tests/user/multisite.php
===================================================================
--- tests/phpunit/tests/user/multisite.php	(revision 41547)
+++ tests/phpunit/tests/user/multisite.php	(working copy)
@@ -120,7 +120,7 @@
 		wp_set_current_user( $user1_id );
 
 		$this->assertTrue( is_blog_user() );
-		$this->assertTrue( is_blog_user( $wpdb->blogid ) );
+		$this->assertTrue( is_blog_user( get_current_blog_id() ) );
 
 		$blog_ids = array();
 
@@ -149,12 +149,13 @@
 		$this->assertFalse( is_user_member_of_blog() );
 
 		wp_set_current_user( $user1_id );
+		$site_id = get_current_blog_id();
 
 		$this->assertTrue( is_user_member_of_blog() );
 		$this->assertTrue( is_user_member_of_blog( 0, 0 ) );
-		$this->assertTrue( is_user_member_of_blog( 0, $wpdb->blogid ) );
+		$this->assertTrue( is_user_member_of_blog( 0, $site_id ) );
 		$this->assertTrue( is_user_member_of_blog( $user1_id ) );
-		$this->assertTrue( is_user_member_of_blog( $user1_id, $wpdb->blogid ) );
+		$this->assertTrue( is_user_member_of_blog( $user1_id, $site_id ) );
 
 		$blog_ids = self::factory()->blog->create_many( 1 );
 		foreach ( $blog_ids as $blog_id ) {
