Make WordPress Core

Ticket #27003: 27003.13.diff

File 27003.13.diff, 23.1 KB (added by nacin, 11 years ago)

Adds hypothetical filters for short-circuiting, for governing how many path segments to consider, etc.

  • 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;
    149 
    150145        $domains = $exact_domains = array( $domain );
    151146        $pieces = explode( '.', $domain );
    152147
     
    158153                }
    159154        }
    160155
    161         if ( '/' !== $path ) {
    162                 $paths = array( '/', $path );
    163         } else {
    164                 $paths = array( '/' );
     156        /*
     157         * If we've gotten to this function during normal execution, there is
     158         * more than one network installed. At this point, who knows how many
     159         * we have. Attempt to optimize for the situation where networks are
     160         * only domains, thus meaning paths never need to be considered.
     161         *
     162         * This is a very basic optimization; anything further could have drawbacks
     163         * depending on the setup, so this is best done per-install.
     164         */
     165        $using_paths = true;
     166        if ( wp_using_ext_object_cache() ) {
     167                $using_paths = wp_cache_get( 'networks_have_paths', 'site-options' );
     168                if ( false === $using_paths ) {
     169                        $using_paths = (bool) $wpdb->get_var( "SELECT id FROM $wpdb->site WHERE path <> '/' LIMIT 1" );
     170                        wp_cache_add( 'networks_have_paths', (int) $using_paths, 'site-options'  );
     171                }
    165172        }
    166173
     174        $paths = array();
     175        if ( $using_paths ) {
     176                $path_segments = explode( '/', trim( $path, "/" ) );
     177
     178                /**
     179                 * Filter the number of path segments to consider when searching for a site.
     180                 *
     181                 * @since 3.9.0
     182                 *
     183                 * @param mixed  $segments The number of path segments to consider. WordPress by default looks at
     184                 *                         one path segment. The function default of null only makes sense when you
     185                 *                         know the requested path should match a network.
     186                 * @param string $domain   The requested domain.
     187                 * @param string $path     The requested path, in full.
     188                 */
     189                $segments = apply_filters( 'network_by_path_segments_count', $segments, $domain, $path );
     190
     191                if ( null !== $segments && count($path_segments ) > $segments ) {
     192                        $path_segments = array_slice( $path_segments, 0, $segments );
     193                }
     194                if ( $path_segments ) {
     195                        $path_segment = '/' . implode( '/', $path_segments ) . '/';
     196                        $paths[] = $path_segment;
     197                        array_pop( $path_segments );
     198                        if ( $path_segments ) {
     199                                $paths[] = '/' . implode( '/', $path_segments ) . '/';
     200                        } else {
     201                                $paths[] = '/';
     202                        }
     203                } else {
     204                        $paths[] = '/';
     205                }
     206        }
     207
     208        /**
     209         * Determine a network by its domain and path.
     210         *
     211         * This allows one to short-circuit the default logic, perhaps by
     212         * replacing it with a routine that is more optimal for your setup.
     213         *
     214         * Return null to avoid the short-circuit. Return false if no network
     215         * can be found at the requested domain and path. Otherwise, return
     216         * an object from wp_get_network().
     217         *
     218         * @since 3.9.0
     219         *
     220         * @param string $domain   The requested domain.
     221         * @param string $path     The requested path, in full.
     222         * @param mixed  $segments The suggested number of paths to consult.
     223         *                         Default null, meaning the entire path was to be consulted.
     224         * @param array  $paths    The paths to search for, based on $path and $segments.
     225         */
     226        $pre = apply_filters( 'pre_get_network_by_path', null, $domain, $path, $segments, $paths );
     227        if ( null !== $pre ) {
     228                return $pre;
     229        }
     230
     231        // @todo Consider additional optimization routes, perhaps as an opt-in for plugins.
     232        // We already have paths covered. What about how far domains should be drilled down (including www)?
     233
    167234        $search_domains = "'" . implode( "', '", $wpdb->_escape( $domains ) ) . "'";
    168         $paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
    169235
    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" );
     236        if ( ! $using_paths ) {
     237                $network = $wpdb->get_row( "SELECT id, domain, path FROM $wpdb->site
     238                        WHERE domain IN ($search_domains) ORDER BY CHAR_LENGTH(domain) DESC LIMIT 1" );
     239                if ( $network ) {
     240                        return wp_get_network( $network );
     241                }
     242                return false;
    173243
     244        } else {
     245                $search_paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
     246                $networks = $wpdb->get_results( "SELECT id, domain, path FROM $wpdb->site
     247                        WHERE domain IN ($search_domains) AND path IN ($search_paths)
     248                        ORDER BY CHAR_LENGTH(domain) DESC, CHAR_LENGTH(path) DESC" );
     249        }
     250
    174251        /*
    175252         * Domains are sorted by length of domain, then by length of path.
    176253         * The domain must match for the path to be considered. Otherwise,
     
    179256        $found = false;
    180257        foreach ( $networks as $network ) {
    181258                if ( $network->domain === $domain || "www.$network->domain" === $domain ) {
    182                         if ( $network->path === $path ) {
     259                        if ( in_array( $network->path, $paths, true ) ) {
    183260                                $found = true;
    184261                                break;
    185262                        }
     
    191268        }
    192269
    193270        if ( $found ) {
    194                 $network = wp_get_network( $network );
    195 
    196                 return $network;
     271                return wp_get_network( $network );
    197272        }
    198273
    199274        return false;
     
    221296}
    222297
    223298/**
    224  * Sets current_site object.
    225  *
    226  * @access private
    227  * @since 3.0.0
    228  * @return object $current_site object
     299 * @todo deprecate
    229300 */
    230301function wpmu_current_site() {
    231         global $wpdb, $current_site, $domain, $path;
     302}
    232303
    233         if ( empty( $current_site ) )
    234                 $current_site = new stdClass;
     304/**
     305 * Retrieve a site object by its domain and path.
     306 *
     307 * @since 3.9.0
     308 *
     309 * @param string $domain   Domain to check.
     310 * @param string $path     Path to check.
     311 * @param int    $segments Path segments to use. Defaults to null, or the full path.
     312 * @return object|bool Site object if successful. False when no site is found.
     313 */
     314function get_site_by_path( $domain, $path, $segments = null ) {
     315        global $wpdb;
    235316
    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;
     317        $path_segments = explode( '/', trim( $path, "/" ) );
    245318
    246         // 2. Pull the network from cache, if possible.
    247         } elseif ( ! $current_site = wp_cache_get( 'current_site', 'site-options' ) ) {
     319        /**
     320         * Filter the number of path segments to consider when searching for a site.
     321         *
     322         * @since 3.9.0
     323         *
    248324
    249                 // 3. See if they have only one network.
    250                 $networks = $wpdb->get_col( "SELECT id FROM $wpdb->site LIMIT 2" );
     325         * @param mixed  $segments The number of path segments to consider. WordPress by default looks at
     326         *                         one path segment following the network path. The function default of
     327         *                         null only makes sense when you know the requested path should match a site.
     328         * @param string $domain   The requested domain.
     329         * @param string $path     The requested path, in full.
     330         */
     331        $segments = apply_filters( 'site_by_path_segments_count', $segments, $domain, $path );
    251332
    252                 if ( count( $networks ) <= 1 ) {
    253                         $current_site = wp_get_network( $networks[0]->id );
     333        if ( null !== $segments && count($path_segments ) > $segments ) {
     334                $path_segments = array_slice( $path_segments, 0, $segments );
     335        }
     336        if ( $path_segments ) {
     337                $paths[] = '/' . implode( '/', $path_segments ) . '/';
     338                array_pop( $path_segments );
     339                if ( $path_segments ) {
     340                        $paths[] = '/' . implode( '/', $path_segments ) . '/';
     341                } else {
     342                        $paths[] = '/';
     343                }
     344        } else {
     345                $paths[] = '/';
     346        }
    254347
    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 ) );
     348        /**
     349         * Determine a site by its domain and path.
     350         *
     351         * This allows one to short-circuit the default logic, perhaps by
     352         * replacing it with a routine that is more optimal for your setup.
     353         *
     354         * Return null to avoid the short-circuit. Return false if no site
     355         * can be found at the requested domain and path. Otherwise, return
     356         * a site object.
     357         *
     358         * @since 3.9.0
     359         *
     360         * @param string $domain   The requested domain.
     361         * @param string $path     The requested path, in full.
     362         * @param mixed  $segments The suggested number of paths to consult.
     363         *                         Default null, meaning the entire path was to be consulted.
     364         * @param array  $paths    The paths to search for, based on $path and $segments.
     365         */
     366        $pre = apply_filters( 'pre_get_site_by_path', null, $domain, $path, $segments, $paths );
     367        if ( null !== $pre ) {
     368                return $pre;
     369        }
    258370
    259                         wp_cache_set( 'current_site', 'site-options' );
     371        // @todo
     372        // get_blog_details(), caching, etc. Consider alternative optimization routes,
     373        // perhaps as an opt-in for plugins, rather than using the pre_* filter.
     374        // For example: The segments filter can expand or ignore paths.
     375        // If persistent caching is enabled, we could query the DB for a path <> '/'
     376        // then cache whether we can just always ignore paths.
    260377
    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 );
     378        if ( count( $paths ) > 1 ) {
     379                $paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
     380                $site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs
     381                        WHERE domain = %s AND path IN ($paths) ORDER BY CHAR_LENGTH(path) DESC LIMIT 1", $domain ) );
     382        } else {
     383                $site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s and path = %s", $domain, $paths[0] ) );
     384        }
    266385
    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                 }
     386        if ( $site ) {
     387                // @todo get_blog_details()
     388                return $site;
    273389        }
    274390
    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;
     391        return false;
    279392}
    280393
    281394/**
    282  * Displays a failure message.
     395 * Displays a failure message or redirects.
    283396 *
    284397 * Used when a blog's tables do not exist. Checks for a missing $wpdb->site table as well.
    285398 *
  • 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 determing
     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                }
     83                if ( empty( $current_site ) ) {
     84                        $current_site = get_network_by_path( $domain, $path, 1 );
     85                }
    5386
    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' );
     87                if ( empty( $current_site ) ) {
     88                        ms_not_installed();
     89                } elseif ( $path === $current_site->path ) {
     90                        $current_blog = get_site_by_path( $domain, $path );
     91                } else {
     92                        // Search the network path + one more path segment (on top of the network path).
     93                        $current_blog = get_site_by_path( $domain, $path, 1 + count( explode( '/', trim( $current_site->path, '/' ) ) ) );
    6094                }
    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, '.' ) );
    6795        } 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' );
     96                // Find the site by the domain and at most the first path segment.
     97                $current_blog = get_site_by_path( $domain, $path, 1 );
     98                if ( $current_blog ) {
     99                        $current_site = wp_get_network( $current_blog->site_id ? $current_blog->site_id : 1 );
     100                } else {
     101                        // If you don't have a site with the same domain/path as a network, you're pretty screwed, but what the hell:
     102                        $current_site = get_network_by_path( $domain, $path, 1 );
    81103                }
    82                 unset($reserved_blognames);
    83104        }
    84105
    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;
     106        // If we don't have a network by now, we have a problem.
     107        if ( empty( $current_site ) ) {
     108                ms_not_installed();
     109        }
     110
     111        // @todo What if the domain of the network doesn't match the current site?
     112        $current_site->cookie_domain = $current_site->domain;
     113        if ( 'www.' === substr( $current_site->cookie_domain, 0, 4 ) ) {
     114                $current_site->cookie_domain = substr( $current_site->cookie_domain, 4 );
     115        }
     116
     117        // Figure out the current network's main site.
     118        if ( ! isset( $current_site->blog_id ) ) {
     119                if ( $current_blog && $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) {
     120                        $current_site->blog_id = $current_blog->blog_id;
    90121                } else {
    91                         $destination = 'http://' . $current_site->domain . $current_site->path . 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
     122                        // @todo we should be able to cache the blog ID of a network's main site easily.
     123                        $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s",
     124                                $current_site->domain, $current_site->path ) );
    92125                }
    93                 header( 'Location: ' . $destination );
    94                 die();
    95126        }
    96127
    97         if ( ! defined( 'WP_INSTALLING' ) ) {
    98                 if ( $current_site && ! $current_blog ) {
    99                         if ( $current_site->domain != $_SERVER[ 'HTTP_HOST' ] ) {
     128        // If we haven't figured out our site, the hell, man.
     129        if ( empty( $current_blog ) ) {
     130                if ( defined( 'WP_INSTALLING' ) ) {
     131                        $current_blog->blog_id = $blog_id = 1;
     132
     133                } elseif ( is_subdomain_install() ) {
     134                        // @todo This is only for an open registration subdomain network.
     135                        if ( defined( 'NOBLOGREDIRECT' ) ) {
     136                                if ( '%siteurl%' === NOBLOGREDIRECT ) {
     137                                        $destination = "http://" . $current_site->domain . $current_site->path;
     138                                } else {
     139                                        $destination = NOBLOGREDIRECT;
     140                                }
     141                        } else {
     142                                $destination = 'http://' . $current_site->domain . $current_site->path . 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
     143                        }
     144                        header( 'Location: ' . $destination );
     145                        exit;
     146
     147                } else {
     148                        if ( 0 !== strcasecmp( $current_site->domain, $domain ) ) {
    100149                                header( 'Location: http://' . $current_site->domain . $current_site->path );
    101150                                exit;
    102151                        }
    103                         $current_blog = get_blog_details( array( 'domain' => $current_site->domain, 'path' => $current_site->path ), false );
     152                        ms_not_installed();
    104153                }
    105                 if ( ! $current_blog || ! $current_site )
    106                         ms_not_installed();
    107154        }
    108155
    109156        $blog_id = $current_blog->blog_id;
    110157        $public  = $current_blog->public;
    111158
    112         if ( empty( $current_blog->site_id ) )
     159        if ( empty( $current_blog->site_id ) ) {
     160                // This dates to [MU134] and shouldn't be relevant anymore,
     161                // but it could be possible for arguments passed to insert_blog() etc.
    113162                $current_blog->site_id = 1;
     163        }
     164
    114165        $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                 }
     166        wp_load_core_site_options();
     167        $current_site->site_name = get_site_option( 'site_name' );
     168        if ( ! $current_site->site_name ) {
     169                $current_site->site_name = ucfirst( $current_site->domain );
    126170        }
    127171}
     172
    128173$wpdb->set_prefix( $table_prefix, false ); // $table_prefix can be set in sunrise.php
    129174$wpdb->set_blog_id( $current_blog->blog_id, $current_blog->site_id );
    130175$table_prefix = $wpdb->get_blog_prefix();