WordPress.org

Make WordPress Core

Ticket #27003: 27003.16.diff

File 27003.16.diff, 23.6 KB (added by jeremyfelt, 6 years ago)
  • src/wp-admin/includes/schema.php

     
    889889                $wpdb->insert( $wpdb->site, array( 'domain' => $domain, 'path' => $path, 'id' => $network_id ) );
    890890        }
    891891
     892        wp_cache_delete( 'networks_have_paths', 'site-options' );
     893
    892894        if ( !is_multisite() ) {
    893895                $site_admins = array( $site_user->user_login );
    894896                $users = get_users( array( 'fields' => array( 'ID', 'user_login' ) ) );
  • src/wp-includes/ms-load.php

     
    115115/**
    116116 * Sets current site name.
    117117 *
     118 * @todo deprecate
     119 *
    118120 * @access private
    119121 * @since 3.0.0
    120  * @return object $current_site object with site_name
     122 * @return mixed $current_site object with site_name
    121123 */
    122124function get_current_site_name( $current_site ) {
    123         global $wpdb;
    124 
    125         $current_site->site_name = wp_cache_get( $current_site->id . ':site_name', 'site-options' );
     125        $current_site->site_name = get_site_option( 'site_name' );
    126126        if ( ! $current_site->site_name ) {
    127                 $current_site->site_name = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE site_id = %d AND meta_key = 'site_name'", $current_site->id ) );
    128                 if ( ! $current_site->site_name )
    129                         $current_site->site_name = ucfirst( $current_site->domain );
    130                 wp_cache_set( $current_site->id . ':site_name', $current_site->site_name, 'site-options' );
     127                $current_site->site_name = ucfirst( $current_site->domain );
    131128        }
    132 
    133129        return $current_site;
    134130}
    135131
     
    138134 *
    139135 * @since 3.9.0
    140136 *
    141  * @param string $domain Domain to check.
    142  * @param string $path   Path to check.
     137 * @param string $domain   Domain to check.
     138 * @param string $path     Path to check.
     139 * @param int    $segments Path segments to use. Defaults to null, or the full path.
    143140 * @return object|bool Network object if successful. False when no network is found.
    144141 */
    145 function get_network_by_path( $domain, $path ) {
     142function get_network_by_path( $domain, $path, $segments = null ) {
    146143        global $wpdb;
    147144
    148         $network_id = false;
     145        // Hash the request for cache lookup and storage, as key length will be unknown.
     146        $request_cache_hash = md5( $domain . '/' . $path );
     147        $network_id = wp_cache_get( 'network_' . $request_cache_hash, 'networks' );
    149148
     149        if ( $network_id ) {
     150                return wp_get_network( $network_id );
     151        }
     152
    150153        $domains = $exact_domains = array( $domain );
    151154        $pieces = explode( '.', $domain );
    152155
     
    158161                }
    159162        }
    160163
    161         if ( '/' !== $path ) {
    162                 $paths = array( '/', $path );
    163         } else {
    164                 $paths = array( '/' );
     164        /*
     165         * If we've gotten to this function during normal execution, there is
     166         * more than one network installed. At this point, who knows how many
     167         * we have. Attempt to optimize for the situation where networks are
     168         * only domains, thus meaning paths never need to be considered.
     169         *
     170         * This is a very basic optimization; anything further could have drawbacks
     171         * depending on the setup, so this is best done per-install.
     172         */
     173        $using_paths = true;
     174        if ( wp_using_ext_object_cache() ) {
     175                $using_paths = wp_cache_get( 'networks_have_paths', 'site-options' );
     176                if ( false === $using_paths ) {
     177                        $using_paths = (bool) $wpdb->get_var( "SELECT id FROM $wpdb->site WHERE path <> '/' LIMIT 1" );
     178                        wp_cache_add( 'networks_have_paths', (int) $using_paths, 'site-options'  );
     179                }
    165180        }
    166181
     182        $paths = array();
     183        if ( $using_paths ) {
     184                $path_segments = explode( '/', trim( $path, "/" ) );
     185
     186                /**
     187                 * Filter the number of path segments to consider when searching for a site.
     188                 *
     189                 * @since 3.9.0
     190                 *
     191                 * @param mixed  $segments The number of path segments to consider. WordPress by default looks at
     192                 *                         one path segment. The function default of null only makes sense when you
     193                 *                         know the requested path should match a network.
     194                 * @param string $domain   The requested domain.
     195                 * @param string $path     The requested path, in full.
     196                 */
     197                $segments = apply_filters( 'network_by_path_segments_count', $segments, $domain, $path );
     198
     199                if ( null !== $segments && count($path_segments ) > $segments ) {
     200                        $path_segments = array_slice( $path_segments, 0, $segments );
     201                }
     202                if ( $path_segments ) {
     203                        $path_segment = '/' . implode( '/', $path_segments ) . '/';
     204                        $paths[] = $path_segment;
     205                        array_pop( $path_segments );
     206                        if ( $path_segments ) {
     207                                $paths[] = '/' . implode( '/', $path_segments ) . '/';
     208                        } else {
     209                                $paths[] = '/';
     210                        }
     211                } else {
     212                        $paths[] = '/';
     213                }
     214        }
     215
     216        /**
     217         * Determine a network by its domain and path.
     218         *
     219         * This allows one to short-circuit the default logic, perhaps by
     220         * replacing it with a routine that is more optimal for your setup.
     221         *
     222         * Return null to avoid the short-circuit. Return false if no network
     223         * can be found at the requested domain and path. Otherwise, return
     224         * an object from wp_get_network().
     225         *
     226         * @since 3.9.0
     227         *
     228         * @param string $domain   The requested domain.
     229         * @param string $path     The requested path, in full.
     230         * @param mixed  $segments The suggested number of paths to consult.
     231         *                         Default null, meaning the entire path was to be consulted.
     232         * @param array  $paths    The paths to search for, based on $path and $segments.
     233         */
     234        $pre = apply_filters( 'pre_get_network_by_path', null, $domain, $path, $segments, $paths );
     235        if ( null !== $pre ) {
     236                return $pre;
     237        }
     238
     239        // @todo Consider additional optimization routes, perhaps as an opt-in for plugins.
     240        // We already have paths covered. What about how far domains should be drilled down (including www)?
     241
    167242        $search_domains = "'" . implode( "', '", $wpdb->_escape( $domains ) ) . "'";
    168         $paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
    169243
    170         $networks = $wpdb->get_results( "SELECT id, domain, path FROM $wpdb->site
    171                 WHERE domain IN ($search_domains) AND path IN ($paths)
    172                 ORDER BY CHAR_LENGTH(domain) DESC, CHAR_LENGTH(path) DESC" );
     244        if ( ! $using_paths ) {
     245                $network = $wpdb->get_row( "SELECT id, domain, path FROM $wpdb->site
     246                        WHERE domain IN ($search_domains) ORDER BY CHAR_LENGTH(domain) DESC LIMIT 1" );
     247                if ( $network ) {
     248                        wp_cache_set( 'network_' . $request_cache_hash, $network->id, 'networks' );
    173249
     250                        return wp_get_network( $network );
     251                }
     252                return false;
     253
     254        } else {
     255                $search_paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
     256                $networks = $wpdb->get_results( "SELECT id, domain, path FROM $wpdb->site
     257                        WHERE domain IN ($search_domains) AND path IN ($search_paths)
     258                        ORDER BY CHAR_LENGTH(domain) DESC, CHAR_LENGTH(path) DESC" );
     259        }
     260
    174261        /*
    175262         * Domains are sorted by length of domain, then by length of path.
    176263         * The domain must match for the path to be considered. Otherwise,
     
    179266        $found = false;
    180267        foreach ( $networks as $network ) {
    181268                if ( $network->domain === $domain || "www.$network->domain" === $domain ) {
    182                         if ( $network->path === $path ) {
     269                        if ( in_array( $network->path, $paths, true ) ) {
    183270                                $found = true;
    184271                                break;
    185272                        }
     
    191278        }
    192279
    193280        if ( $found ) {
    194                 $network = wp_get_network( $network );
     281                wp_cache_set( 'network_' . $request_cache_hash, $network->id, 'networks' );
    195282
    196                 return $network;
     283                return wp_get_network( $network );
    197284        }
    198285
    199286        return false;
     
    221308}
    222309
    223310/**
    224  * Sets current_site object.
    225  *
    226  * @access private
    227  * @since 3.0.0
    228  * @return object $current_site object
     311 * @todo deprecate
    229312 */
    230313function wpmu_current_site() {
    231         global $wpdb, $current_site, $domain, $path;
     314}
    232315
    233         if ( empty( $current_site ) )
    234                 $current_site = new stdClass;
     316/**
     317 * Retrieve a site object by its domain and path.
     318 *
     319 * @since 3.9.0
     320 *
     321 * @param string $domain   Domain to check.
     322 * @param string $path     Path to check.
     323 * @param int    $segments Path segments to use. Defaults to null, or the full path.
     324 * @return object|bool Site object if successful. False when no site is found.
     325 */
     326function get_site_by_path( $domain, $path, $segments = null ) {
     327        global $wpdb;
    235328
    236         // 1. If constants are defined, that's our network.
    237         if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
    238                 $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
    239                 $current_site->domain = DOMAIN_CURRENT_SITE;
    240                 $current_site->path   = $path = PATH_CURRENT_SITE;
    241                 if ( defined( 'BLOG_ID_CURRENT_SITE' ) )
    242                         $current_site->blog_id = BLOG_ID_CURRENT_SITE;
    243                 elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) // deprecated.
    244                         $current_site->blog_id = BLOGID_CURRENT_SITE;
     329        $path_segments = explode( '/', trim( $path, "/" ) );
    245330
    246         // 2. Pull the network from cache, if possible.
    247         } elseif ( ! $current_site = wp_cache_get( 'current_site', 'site-options' ) ) {
     331        /**
     332         * Filter the number of path segments to consider when searching for a site.
     333         *
     334         * @since 3.9.0
     335         *
    248336
    249                 // 3. See if they have only one network.
    250                 $networks = $wpdb->get_col( "SELECT id FROM $wpdb->site LIMIT 2" );
     337         * @param mixed  $segments The number of path segments to consider. WordPress by default looks at
     338         *                         one path segment following the network path. The function default of
     339         *                         null only makes sense when you know the requested path should match a site.
     340         * @param string $domain   The requested domain.
     341         * @param string $path     The requested path, in full.
     342         */
     343        $segments = apply_filters( 'site_by_path_segments_count', $segments, $domain, $path );
    251344
    252                 if ( count( $networks ) <= 1 ) {
    253                         $current_site = wp_get_network( $networks[0]->id );
     345        if ( null !== $segments && count($path_segments ) > $segments ) {
     346                $path_segments = array_slice( $path_segments, 0, $segments );
     347        }
     348        if ( $path_segments ) {
     349                $paths[] = '/' . implode( '/', $path_segments ) . '/';
     350                array_pop( $path_segments );
     351                if ( $path_segments ) {
     352                        $paths[] = '/' . implode( '/', $path_segments ) . '/';
     353                } else {
     354                        $paths[] = '/';
     355                }
     356        } else {
     357                $paths[] = '/';
     358        }
    254359
    255                         $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id
    256                                 FROM $wpdb->blogs WHERE domain = %s AND path = %s",
    257                                 $current_site->domain, $current_site->path ) );
     360        /**
     361         * Determine a site by its domain and path.
     362         *
     363         * This allows one to short-circuit the default logic, perhaps by
     364         * replacing it with a routine that is more optimal for your setup.
     365         *
     366         * Return null to avoid the short-circuit. Return false if no site
     367         * can be found at the requested domain and path. Otherwise, return
     368         * a site object.
     369         *
     370         * @since 3.9.0
     371         *
     372         * @param string $domain   The requested domain.
     373         * @param string $path     The requested path, in full.
     374         * @param mixed  $segments The suggested number of paths to consult.
     375         *                         Default null, meaning the entire path was to be consulted.
     376         * @param array  $paths    The paths to search for, based on $path and $segments.
     377         */
     378        $pre = apply_filters( 'pre_get_site_by_path', null, $domain, $path, $segments, $paths );
     379        if ( null !== $pre ) {
     380                return $pre;
     381        }
    258382
    259                         wp_cache_set( 'current_site', 'site-options' );
     383        // @todo
     384        // get_blog_details(), caching, etc. Consider alternative optimization routes,
     385        // perhaps as an opt-in for plugins, rather than using the pre_* filter.
     386        // For example: The segments filter can expand or ignore paths.
     387        // If persistent caching is enabled, we could query the DB for a path <> '/'
     388        // then cache whether we can just always ignore paths.
    260389
    261                 // 4. Multiple networks are in play. Determine which via domain and path.
    262                 } else {
    263                         // Find the first path segment.
    264                         $path = substr( $_SERVER['REQUEST_URI'], 0, 1 + strpos( $_SERVER['REQUEST_URI'], '/', 1 ) );
    265                         $current_site = get_network_by_path( $domain, $path );
     390        if ( count( $paths ) > 1 ) {
     391                $paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
     392                $site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs
     393                        WHERE domain = %s AND path IN ($paths) ORDER BY CHAR_LENGTH(path) DESC LIMIT 1", $domain ) );
     394        } else {
     395                $site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s and path = %s", $domain, $paths[0] ) );
     396        }
    266397
    267                         // Option 1. We did not find anything.
    268                         if ( ! $current_site ) {
    269                                 wp_load_translations_early();
    270                                 wp_die( __( 'No site defined on this host. If you are the owner of this site, please check <a href="http://codex.wordpress.org/Debugging_a_WordPress_Network">Debugging a WordPress Network</a> for help.' ) );
    271                         }
    272                 }
     398        if ( $site ) {
     399                // @todo get_blog_details()
     400                return $site;
    273401        }
    274402
    275         // Option 2. We found something. Load up site meta and return.
    276         wp_load_core_site_options();
    277         $current_site = get_current_site_name( $current_site );
    278         return $current_site;
     403        return false;
    279404}
    280405
    281406/**
    282  * Displays a failure message.
     407 * Displays a failure message or redirects.
    283408 *
    284409 * Used when a blog's tables do not exist. Checks for a missing $wpdb->site table as well.
    285410 *
  • src/wp-includes/ms-settings.php

     
    2222
    2323if ( !isset( $current_site ) || !isset( $current_blog ) ) {
    2424
    25         $domain = addslashes( $_SERVER['HTTP_HOST'] );
    26         if ( false !== strpos( $domain, ':' ) ) {
    27                 if ( substr( $domain, -3 ) == ':80' ) {
    28                         $domain = substr( $domain, 0, -3 );
    29                         $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -3 );
    30                 } elseif ( substr( $domain, -4 ) == ':443' ) {
    31                         $domain = substr( $domain, 0, -4 );
    32                         $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -4 );
    33                 } else {
    34                         wp_load_translations_early();
    35                         wp_die( __( 'Multisite only works without the port number in the URL.' ) );
    36                 }
     25        // Given the domain and path, let's try to identify the network and site.
     26        // Usually, it's easier to query the site first, which declares its network.
     27        // In limited situations, though, we either can or must find the network first.
     28
     29        $domain = strtolower( stripslashes( $_SERVER['HTTP_HOST'] ) );
     30        if ( substr( $domain, -3 ) == ':80' ) {
     31                $domain = substr( $domain, 0, -3 );
     32                $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -3 );
     33        } elseif ( substr( $domain, -4 ) == ':443' ) {
     34                $domain = substr( $domain, 0, -4 );
     35                $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -4 );
    3736        }
    3837
    39         $domain = rtrim( $domain, '.' );
     38        $path = stripslashes( $_SERVER['REQUEST_URI'] );
     39        if ( is_admin() ) {
     40                $path = preg_replace( '#(.*)/wp-admin/.*#', '$1/', $path );
     41        }
     42        list( $path ) = explode( '?', $path );
    4043
    41         $path = preg_replace( '|([a-z0-9-]+.php.*)|', '', $_SERVER['REQUEST_URI'] );
    42         $path = str_replace ( '/wp-admin/', '/', $path );
    43         $path = preg_replace( '|(/[a-z0-9-]+?/).*|', '$1', $path );
     44        // If the network is defined in wp-config.php, we can simply use that.
     45        if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
     46                $current_site = new stdClass;
     47                $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
     48                $current_site->domain = DOMAIN_CURRENT_SITE;
     49                $current_site->path = PATH_CURRENT_SITE;
     50                if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
     51                        $current_site->blog_id = BLOG_ID_CURRENT_SITE;
     52                } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated.
     53                        $current_site->blog_id = BLOGID_CURRENT_SITE;
     54                }
    4455
    45         $current_site = wpmu_current_site();
    46         $current_site->cookie_domain = $current_site->domain;
    47         if ( 'www.' === substr( $current_site->cookie_domain, 0, 4 ) ) {
    48                 $current_site->cookie_domain = substr( $current_site->cookie_domain, 4 );
    49         }
     56                if ( $current_site->domain === $domain && $current_site->path === $path ) {
     57                        $current_blog = get_site_by_path( $domain, $path );
     58                } elseif ( '/' !== $current_site->path && $current_site->domain === $domain && 0 === strpos( $path, $current_site->path ) ) {
     59                        // If the current network has a path and also matches the domain and path of the request,
     60                        // we need to look for a site using the first path segment following the network's path.
     61                        $current_blog = get_site_by_path( $domain, $path, 1 + count( explode( '/', trim( $current_site->path, '/' ) ) ) );
     62                } else {
     63                        // Otherwise, use the first path segment (as usual).
     64                        $current_blog = get_site_by_path( $domain, $path, 1 );
     65                }
    5066
    51         if ( ! isset( $current_site->blog_id ) )
    52                 $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", $current_site->domain, $current_site->path ) );
     67        } elseif ( ! is_subdomain_install() ) {
     68                /*
     69                 * A "subdomain" install can be re-interpreted to mean "can support any domain".
     70                 * If we're not dealing with one of these installs, then the important part is determining
     71                 * the network first, because we need the network's path to identify any sites.
     72                 */
     73                if ( ! $current_site = wp_cache_get( 'current_network', 'site-options' ) ) {
     74                        // Are there even two networks installed?
     75                        $one_network = $wpdb->get_row( "SELECT * FROM $wpdb->site LIMIT 2" ); // [sic]
     76                        if ( 1 === $wpdb->num_rows ) {
     77                                $current_site = wp_get_network( $one_network );
     78                                wp_cache_set( 'current_network', 'site-options' );
     79                        } elseif ( 0 === $wpdb->num_rows ) {
     80                                ms_not_installed();
     81                        }
     82                }
    5383
    54         if ( is_subdomain_install() ) {
    55                 $current_blog = wp_cache_get( 'current_blog_' . $domain, 'site-options' );
    56                 if ( !$current_blog ) {
    57                         $current_blog = get_blog_details( array( 'domain' => $domain ), false );
    58                         if ( $current_blog )
    59                                 wp_cache_set( 'current_blog_' . $domain, $current_blog, 'site-options' );
     84                if ( empty( $current_site ) ) {
     85                        $current_site = get_network_by_path( $domain, $path, 1 );
    6086                }
    61                 if ( $current_blog && $current_blog->site_id != $current_site->id ) {
    62                         $current_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE id = %d", $current_blog->site_id ) );
    63                         if ( ! isset( $current_site->blog_id ) )
    64                                 $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", $current_site->domain, $current_site->path ) );
    65                 } else
    66                         $blogname = substr( $domain, 0, strpos( $domain, '.' ) );
     87
     88                if ( empty( $current_site ) ) {
     89                        ms_not_installed();
     90                } elseif ( $path === $current_site->path ) {
     91                        $current_blog = get_site_by_path( $domain, $path );
     92                } else {
     93                        // Search the network path + one more path segment (on top of the network path).
     94                        $current_blog = get_site_by_path( $domain, $path, 1 + count( explode( '/', trim( $current_site->path, '/' ) ) ) );
     95                }
    6796        } else {
    68                 $blogname = htmlspecialchars( substr( $_SERVER[ 'REQUEST_URI' ], strlen( $path ) ) );
    69                 if ( false !== strpos( $blogname, '/' ) )
    70                         $blogname = substr( $blogname, 0, strpos( $blogname, '/' ) );
    71                 if ( false !== strpos( $blogname, '?' ) )
    72                         $blogname = substr( $blogname, 0, strpos( $blogname, '?' ) );
    73                 $reserved_blognames = array( 'page', 'comments', 'blog', 'wp-admin', 'wp-includes', 'wp-content', 'files', 'feed' );
    74                 if ( $blogname != '' && ! in_array( $blogname, $reserved_blognames ) && ! is_file( $blogname ) )
    75                         $path .= $blogname . '/';
    76                 $current_blog = wp_cache_get( 'current_blog_' . $domain . $path, 'site-options' );
    77                 if ( ! $current_blog ) {
    78                         $current_blog = get_blog_details( array( 'domain' => $domain, 'path' => $path ), false );
    79                         if ( $current_blog )
    80                                 wp_cache_set( 'current_blog_' . $domain . $path, $current_blog, 'site-options' );
     97                // Find the site by the domain and at most the first path segment.
     98                $current_blog = get_site_by_path( $domain, $path, 1 );
     99                if ( $current_blog ) {
     100                        $current_site = wp_get_network( $current_blog->site_id ? $current_blog->site_id : 1 );
     101                } else {
     102                        // If you don't have a site with the same domain/path as a network, you're pretty screwed, but what the hell:
     103                        $current_site = get_network_by_path( $domain, $path, 1 );
    81104                }
    82                 unset($reserved_blognames);
    83105        }
    84106
    85         if ( ! defined( 'WP_INSTALLING' ) && is_subdomain_install() && ! is_object( $current_blog ) ) {
    86                 if ( defined( 'NOBLOGREDIRECT' ) ) {
    87                         $destination = NOBLOGREDIRECT;
    88                         if ( '%siteurl%' == $destination )
    89                                 $destination = "http://" . $current_site->domain . $current_site->path;
     107        // If we don't have a network by now, we have a problem.
     108        if ( empty( $current_site ) ) {
     109                ms_not_installed();
     110        }
     111
     112        // @todo What if the domain of the network doesn't match the current site?
     113        $current_site->cookie_domain = $current_site->domain;
     114        if ( 'www.' === substr( $current_site->cookie_domain, 0, 4 ) ) {
     115                $current_site->cookie_domain = substr( $current_site->cookie_domain, 4 );
     116        }
     117
     118        // Figure out the current network's main site.
     119        if ( ! isset( $current_site->blog_id ) ) {
     120                if ( $current_blog && $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) {
     121                        $current_site->blog_id = $current_blog->blog_id;
    90122                } else {
    91                         $destination = 'http://' . $current_site->domain . $current_site->path . 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
     123                        // @todo we should be able to cache the blog ID of a network's main site easily.
     124                        $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s",
     125                                $current_site->domain, $current_site->path ) );
    92126                }
    93                 header( 'Location: ' . $destination );
    94                 die();
    95127        }
    96128
    97         if ( ! defined( 'WP_INSTALLING' ) ) {
    98                 if ( $current_site && ! $current_blog ) {
    99                         if ( $current_site->domain != $_SERVER[ 'HTTP_HOST' ] ) {
     129        // If we haven't figured out our site, the hell, man.
     130        if ( empty( $current_blog ) ) {
     131                if ( defined( 'WP_INSTALLING' ) ) {
     132                        $current_blog->blog_id = $blog_id = 1;
     133
     134                } elseif ( is_subdomain_install() ) {
     135                        // @todo This is only for an open registration subdomain network.
     136                        if ( defined( 'NOBLOGREDIRECT' ) ) {
     137                                if ( '%siteurl%' === NOBLOGREDIRECT ) {
     138                                        $destination = "http://" . $current_site->domain . $current_site->path;
     139                                } else {
     140                                        $destination = NOBLOGREDIRECT;
     141                                }
     142                        } else {
     143                                $destination = 'http://' . $current_site->domain . $current_site->path . 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
     144                        }
     145                        header( 'Location: ' . $destination );
     146                        exit;
     147
     148                } else {
     149                        if ( 0 !== strcasecmp( $current_site->domain, $domain ) ) {
    100150                                header( 'Location: http://' . $current_site->domain . $current_site->path );
    101151                                exit;
    102152                        }
    103                         $current_blog = get_blog_details( array( 'domain' => $current_site->domain, 'path' => $current_site->path ), false );
     153                        ms_not_installed();
    104154                }
    105                 if ( ! $current_blog || ! $current_site )
    106                         ms_not_installed();
    107155        }
    108156
    109157        $blog_id = $current_blog->blog_id;
    110158        $public  = $current_blog->public;
    111159
    112         if ( empty( $current_blog->site_id ) )
     160        if ( empty( $current_blog->site_id ) ) {
     161                // This dates to [MU134] and shouldn't be relevant anymore,
     162                // but it could be possible for arguments passed to insert_blog() etc.
    113163                $current_blog->site_id = 1;
     164        }
     165
    114166        $site_id = $current_blog->site_id;
    115 
    116         $current_site = get_current_site_name( $current_site );
    117 
    118         if ( ! $blog_id ) {
    119                 if ( defined( 'WP_INSTALLING' ) ) {
    120                         $current_blog->blog_id = $blog_id = 1;
    121                 } else {
    122                         wp_load_translations_early();
    123                         $msg = ! $wpdb->get_var( "SHOW TABLES LIKE '$wpdb->site'" ) ? ' ' . __( 'Database tables are missing.' ) : '';
    124                         wp_die( __( 'No site by that name on this system.' ) . $msg );
    125                 }
     167        wp_load_core_site_options();
     168        $current_site->site_name = get_site_option( 'site_name' );
     169        if ( ! $current_site->site_name ) {
     170                $current_site->site_name = ucfirst( $current_site->domain );
    126171        }
    127172}
     173
    128174$wpdb->set_prefix( $table_prefix, false ); // $table_prefix can be set in sunrise.php
    129175$wpdb->set_blog_id( $current_blog->blog_id, $current_blog->site_id );
    130176$table_prefix = $wpdb->get_blog_prefix();