<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[27178] trunk: Multisite: Add get_network_by_path() and wp_get_network() to begin cleanup of multisite load.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/27178">27178</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2014-02-13 23:06:12 +0000 (Thu, 13 Feb 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Multisite: Add get_network_by_path() and wp_get_network() to begin cleanup of multisite load.

Tries to get network detection under control by simplifying wpmu_current_site(). It now also pops off each subdomain to find a more general match. Adds unit tests for get_network_by_path() and a new network factory for unit tests.

Much of this is likely to change in 3.9 as more of ms-load.php and ms-settings.php gets hacked to bits.

props jeremyfelt.
see <a href="http://core.trac.wordpress.org/ticket/27003">#27003</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesmsloadphp">trunk/src/wp-includes/ms-load.php</a></li>
<li><a href="#trunksrcwpincludesmssettingsphp">trunk/src/wp-includes/ms-settings.php</a></li>
<li><a href="#trunktestsphpunitincludesfactoryphp">trunk/tests/phpunit/includes/factory.php</a></li>
<li><a href="#trunktestsphpunittestsmsphp">trunk/tests/phpunit/tests/ms.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesmsloadphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/ms-load.php (27177 => 27178)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/ms-load.php        2014-02-13 20:12:46 UTC (rev 27177)
+++ trunk/src/wp-includes/ms-load.php   2014-02-13 23:06:12 UTC (rev 27178)
</span><span class="lines">@@ -134,6 +134,93 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><ins>+ * Retrieve a network object by its domain and path.
+ *
+ * @since 3.9.0
+ *
+ * @param string $domain Domain to check.
+ * @param string $path   Path to check.
+ * @return object|bool Network object if successful. False when no network is found.
+ */
+function get_network_by_path( $domain, $path ) {
+       global $wpdb;
+
+       $network_id = false;
+
+       $domains = $exact_domains = array( $domain );
+       $pieces = explode( '.', $domain );
+
+       // It's possible one domain to search is 'com', but it might as well
+       // be 'localhost' or some other locally mapped domain.
+       while ( array_shift( $pieces ) ) {
+               if ( $pieces ) {
+                       $domains[] = implode( '.', $pieces );
+               }
+       }
+
+       if ( '/' !== $path ) {
+               $paths = array( '/', $path );
+       } else {
+               $paths = array( '/' );
+       }
+
+       $search_domains = "'" . implode( "', '", $wpdb->_escape( $domains ) ) . "'";
+       $paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
+
+       $networks = $wpdb->get_results( "SELECT id, domain, path FROM $wpdb->site
+               WHERE domain IN ($search_domains) AND path IN ($paths)
+               ORDER BY CHAR_LENGTH(domain) DESC, CHAR_LENGTH(path) DESC" );
+
+       /*
+        * Domains are sorted by length of domain, then by length of path.
+        * The domain must match for the path to be considered. Otherwise,
+        * a network with the path of / will suffice.
+        */
+       $found = false;
+       foreach ( $networks as $network ) {
+               if ( $network->domain === $domain || "www.$network->domain" === $domain ) {
+                       if ( $network->path === $path ) {
+                               $found = true;
+                               break;
+                       }
+               }
+               if ( $network->path === '/' ) {
+                       $found = true;
+                       break;
+               }
+       }
+
+       if ( $found ) {
+               $network = wp_get_network( $network );
+
+               return $network;
+       }
+
+       return false;
+}
+
+/**
+ * Retrieve an object containing information about the requested network.
+ *
+ * @since 3.9.0
+ *
+ * @param int $network_id The network's DB row or ID.
+ * @return mixed Object containing network information if found, false if not.
+ */
+function wp_get_network( $network ) {
+       global $wpdb;
+
+       if ( ! is_object( $network ) ) {
+               $network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE id = %d", $network ) );
+               if ( ! $network ) {
+                       return false;
+               }
+       }
+
+       return $network;
+}
+
+/**
</ins><span class="cx">  * Sets current_site object.
</span><span class="cx">  *
</span><span class="cx">  * @access private
</span><span class="lines">@@ -141,11 +228,12 @@
</span><span class="cx">  * @return object $current_site object
</span><span class="cx">  */
</span><span class="cx"> function wpmu_current_site() {
</span><del>-       global $wpdb, $current_site, $domain, $path, $sites, $cookie_domain;
</del><ins>+        global $wpdb, $current_site, $domain, $path;
</ins><span class="cx"> 
</span><span class="cx">  if ( empty( $current_site ) )
</span><span class="cx">          $current_site = new stdClass;
</span><span class="cx"> 
</span><ins>+       // 1. If constants are defined, that's our network.
</ins><span class="cx">   if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
</span><span class="cx">          $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
</span><span class="cx">          $current_site->domain = DOMAIN_CURRENT_SITE;
</span><span class="lines">@@ -154,77 +242,40 @@
</span><span class="cx">                  $current_site->blog_id = BLOG_ID_CURRENT_SITE;
</span><span class="cx">          elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) // deprecated.
</span><span class="cx">                  $current_site->blog_id = BLOGID_CURRENT_SITE;
</span><del>-               if ( DOMAIN_CURRENT_SITE == $domain )
-                       $current_site->cookie_domain = $cookie_domain;
-               elseif ( substr( $current_site->domain, 0, 4 ) == 'www.' )
-                       $current_site->cookie_domain = substr( $current_site->domain, 4 );
-               else
-                       $current_site->cookie_domain = $current_site->domain;
</del><span class="cx"> 
</span><del>-               wp_load_core_site_options( $current_site->id );
</del><ins>+        // 2. Pull the network from cache, if possible.
+       } elseif ( ! $current_site = wp_cache_get( 'current_site', 'site-options' ) ) {
</ins><span class="cx"> 
</span><del>-               return $current_site;
-       }
</del><ins>+                // 3. See if they have only one network.
+               $networks = $wpdb->get_col( "SELECT id FROM $wpdb->site LIMIT 2" );
</ins><span class="cx"> 
</span><del>-       $current_site = wp_cache_get( 'current_site', 'site-options' );
-       if ( $current_site )
-               return $current_site;
</del><ins>+                if ( count( $networks ) <= 1 ) {
+                       $current_site = wp_get_network( $networks[0]->id );
</ins><span class="cx"> 
</span><del>-       $sites = $wpdb->get_results( "SELECT * FROM $wpdb->site" ); // usually only one site
-       if ( 1 == count( $sites ) ) {
-               $current_site = $sites[0];
-               wp_load_core_site_options( $current_site->id );
-               $path = $current_site->path;
-               $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 ) );
-               $current_site = get_current_site_name( $current_site );
-               if ( substr( $current_site->domain, 0, 4 ) == 'www.' )
-                       $current_site->cookie_domain = substr( $current_site->domain, 4 );
-               wp_cache_set( 'current_site', $current_site, 'site-options' );
-               return $current_site;
-       }
-       $path = substr( $_SERVER[ 'REQUEST_URI' ], 0, 1 + strpos( $_SERVER[ 'REQUEST_URI' ], '/', 1 ) );
</del><ins>+                        $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 ) );
</ins><span class="cx"> 
</span><del>-       if ( $domain == $cookie_domain )
-               $current_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE domain = %s AND path = %s", $domain, $path ) );
-       else
-               $current_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE domain IN ( %s, %s ) AND path = %s ORDER BY CHAR_LENGTH( domain ) DESC LIMIT 1", $domain, $cookie_domain, $path ) );
</del><ins>+                        wp_cache_set( 'current_site', 'site-options' );
</ins><span class="cx"> 
</span><del>-       if ( ! $current_site ) {
-               if ( $domain == $cookie_domain )
-                       $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path='/'", $domain ) );
-               else
-                       $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain IN ( %s, %s ) AND path = '/' ORDER BY CHAR_LENGTH( domain ) DESC LIMIT 1", $domain, $cookie_domain ) );
-       }
</del><ins>+                // 4. Multiple networks are in play. Determine which via domain and path.
+               } else {
+                       // Find the first path segment.
+                       $path = substr( $_SERVER['REQUEST_URI'], 0, 1 + strpos( $_SERVER['REQUEST_URI'], '/', 1 ) );
+                       $current_site = get_network_by_path( $domain, $path );
</ins><span class="cx"> 
</span><del>-       if ( $current_site ) {
-               $path = $current_site->path;
-               $current_site->cookie_domain = $cookie_domain;
-               return $current_site;
-       }
-
-       if ( is_subdomain_install() ) {
-               $sitedomain = substr( $domain, 1 + strpos( $domain, '.' ) );
-               $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path = %s", $sitedomain, $path) );
-               if ( $current_site ) {
-                       $current_site->cookie_domain = $current_site->domain;
-                       return $current_site;
</del><ins>+                        // Option 1. We did not find anything.
+                       if ( ! $current_site ) {
+                               wp_load_translations_early();
+                               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.' ) );
+                       }
</ins><span class="cx">           }
</span><del>-
-               $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path='/'", $sitedomain) );
</del><span class="cx">   }
</span><span class="cx"> 
</span><del>-       if ( $current_site || defined( 'WP_INSTALLING' ) ) {
-               $path = '/';
-               return $current_site;
-       }
-
-       // Still no dice.
-       wp_load_translations_early();
-
-       if ( 1 == count( $sites ) )
-               wp_die( sprintf( __( 'That site does not exist. Please try <a href="%s">%s</a>.' ), 'http://' . $sites[0]->domain . $sites[0]->path ) );
-       else
-               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.' ) );
</del><ins>+        // Option 2. We found something. Load up site meta and return.
+       wp_load_core_site_options();
+       $current_site = get_current_site_name( $current_site );
+       return $current_site;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span></span></pre></div>
<a id="trunksrcwpincludesmssettingsphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/ms-settings.php (27177 => 27178)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/ms-settings.php    2014-02-13 20:12:46 UTC (rev 27177)
+++ trunk/src/wp-includes/ms-settings.php       2014-02-13 23:06:12 UTC (rev 27178)
</span><span class="lines">@@ -37,15 +37,17 @@
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="cx">  $domain = rtrim( $domain, '.' );
</span><del>-       $cookie_domain = $domain;
-       if ( substr( $cookie_domain, 0, 4 ) == 'www.' )
-               $cookie_domain = substr( $cookie_domain, 4 );
</del><span class="cx"> 
</span><span class="cx">  $path = preg_replace( '|([a-z0-9-]+.php.*)|', '', $_SERVER['REQUEST_URI'] );
</span><span class="cx">  $path = str_replace ( '/wp-admin/', '/', $path );
</span><span class="cx">  $path = preg_replace( '|(/[a-z0-9-]+?/).*|', '$1', $path );
</span><span class="cx"> 
</span><span class="cx">  $current_site = wpmu_current_site();
</span><ins>+       $current_site->cookie_domain = $current_site->domain;
+       if ( 'www.' === substr( $current_site->cookie_domain, 0, 4 ) ) {
+               $current_site->cookie_domain = substr( $current_site->cookie_domain, 4 );
+       }
+
</ins><span class="cx">   if ( ! isset( $current_site->blog_id ) )
</span><span class="cx">          $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 ) );
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunktestsphpunitincludesfactoryphp"></a>
<div class="modfile"><h4>Modified: trunk/tests/phpunit/includes/factory.php (27177 => 27178)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/includes/factory.php 2014-02-13 20:12:46 UTC (rev 27177)
+++ trunk/tests/phpunit/includes/factory.php    2014-02-13 23:06:12 UTC (rev 27178)
</span><span class="lines">@@ -42,6 +42,11 @@
</span><span class="cx">   */
</span><span class="cx">  public $blog;
</span><span class="cx"> 
</span><ins>+       /**
+        * @var WP_UnitTest_Factory_For_Network
+        */
+       public $network;
+
</ins><span class="cx">   function __construct() {
</span><span class="cx">          $this->post = new WP_UnitTest_Factory_For_Post( $this );
</span><span class="cx">          $this->attachment = new WP_UnitTest_Factory_For_Attachment( $this );
</span><span class="lines">@@ -50,8 +55,10 @@
</span><span class="cx">          $this->term = new WP_UnitTest_Factory_For_Term( $this );
</span><span class="cx">          $this->category = new WP_UnitTest_Factory_For_Term( $this, 'category' );
</span><span class="cx">          $this->tag = new WP_UnitTest_Factory_For_Term( $this, 'post_tag' );
</span><del>-               if ( is_multisite() )
</del><ins>+                if ( is_multisite() ) {
</ins><span class="cx">                   $this->blog = new WP_UnitTest_Factory_For_Blog( $this );
</span><ins>+                       $this->network = new WP_UnitTest_Factory_For_Network( $this );
+               }
</ins><span class="cx">   }
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -177,6 +184,39 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+class WP_UnitTest_Factory_For_Network extends WP_UnitTest_Factory_For_Thing {
+
+       function __construct( $factory = null ) {
+               parent::__construct( $factory );
+               $this->default_generation_definitions = array(
+                       'domain' => WP_TESTS_DOMAIN,
+                       'title' => new WP_UnitTest_Generator_Sequence( 'Network %s' ),
+                       'path' => new WP_UnitTest_Generator_Sequence( '/testpath%s/' ),
+                       'network_id' => new WP_UnitTest_Generator_Sequence( '%s', 2 ),
+                       'subdomain_install' => false,
+               );
+       }
+
+       function create_object( $args ) {
+               require_once ABSPATH . 'wp-admin/includes/upgrade.php';
+
+               if ( ! isset( $args['user'] ) ) {
+                       $email = WP_TESTS_EMAIL;
+               } else {
+                       $email = get_userdata( $args['user'] )->user_email;
+               }
+
+               populate_network( $args['network_id'], $args['domain'], $email, $args['title'], $args['path'], $args['subdomain_install'] );
+               return $args['network_id'];
+       }
+
+       function update_object( $network_id, $fields ) {}
+
+       function get_object_by_id( $network_id ) {
+               return wp_get_network( $network_id );
+       }
+}
+
</ins><span class="cx"> class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing {
</span><span class="cx"> 
</span><span class="cx">  private $taxonomy;
</span></span></pre></div>
<a id="trunktestsphpunittestsmsphp"></a>
<div class="modfile"><h4>Modified: trunk/tests/phpunit/tests/ms.php (27177 => 27178)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/tests/ms.php 2014-02-13 20:12:46 UTC (rev 27177)
+++ trunk/tests/phpunit/tests/ms.php    2014-02-13 23:06:12 UTC (rev 27178)
</span><span class="lines">@@ -1172,6 +1172,53 @@
</span><span class="cx">          // Expect 0 sites when using an offset larger than the number of sites
</span><span class="cx">          $this->assertCount( 0, wp_get_sites( array( 'offset' => 20 ) ) );
</span><span class="cx">  }
</span><ins>+
+       /**
+        * @ticket 27003
+        */
+       function test_get_network_by_path() {
+               global $wpdb;
+
+               $ids = array(
+                       'wordpress.org/'         => array( 'domain' => 'wordpress.org', 'path' => '/' ),
+                       'wordpress.org/one/'     => array( 'domain' => 'wordpress.org', 'path' => '/one/' ),
+                       'wordpress.net/'         => array( 'domain' => 'wordpress.net', 'path' => '/' ),
+                       'www.wordpress.net/'     => array( 'domain' => 'www.wordpress.net', 'path' => '/' ),
+                       'www.wordpress.net/two/' => array( 'domain' => 'www.wordpress.net', 'path' => '/two/' ),
+                       'wordpress.net/three/'   => array( 'domain' => 'wordpress.net', 'path' => '/three/' ),
+               );
+
+               foreach ( $ids as &$id ) {
+                       $id = $this->factory->network->create( $id );
+               }
+               unset( $id );
+
+               $this->assertEquals( $ids['www.wordpress.net/'],
+                       get_network_by_path( 'www.wordpress.net', '/notapath/' )->id );
+
+               $this->assertEquals( $ids['www.wordpress.net/two/'],
+                       get_network_by_path( 'www.wordpress.net', '/two/' )->id );
+
+               // This should find /one/ despite the www.
+               $this->assertEquals( $ids['wordpress.org/one/'],
+                       get_network_by_path( 'www.wordpress.org', '/one/' )->id );
+
+               // This should not find /one/ because the domains don't match.
+               $this->assertEquals( $ids['wordpress.org/'],
+                       get_network_by_path( 'site1.wordpress.org', '/one/' )->id );
+
+               $this->assertEquals( $ids['wordpress.net/three/'],
+                       get_network_by_path( 'wordpress.net', '/three/' )->id );
+
+               $this->assertEquals( $ids['wordpress.net/'],
+                       get_network_by_path( 'wordpress.net', '/notapath/' )->id );
+
+               $this->assertEquals( $ids['wordpress.net/'],
+                       get_network_by_path( 'site1.wordpress.net', '/notapath/' )->id );
+
+               $this->assertEquals( $ids['wordpress.net/'],
+                       get_network_by_path( 'site1.wordpress.net', '/three/' )->id );
+       }
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> endif;
</span></span></pre>
</div>
</div>

</body>
</html>