From 98e8daa20abae3b675d550234a1425573c91f9cc Mon Sep 17 00:00:00 2001 From: ryan Date: Sat, 13 Feb 2010 23:09:54 +0000 Subject: [PATCH] Add ability to query by domain and/or path to get_blog_details(). Improve blog details caching. Use get_blog_details() in ms-settings.php so queries are cached. see #11644 git-svn-id: http://svn.automattic.com/wordpress/trunk@13126 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/blogs.php | 71 ++++++++++++++++++++++++++++++++----- wp-includes/ms-settings.php | 6 ++-- wp-settings.php | 12 +++---- 3 files changed, 72 insertions(+), 17 deletions(-) diff --git a/wp-includes/blogs.php b/wp-includes/blogs.php index ac0b125343..4ad7839b56 100644 --- a/wp-includes/blogs.php +++ b/wp-includes/blogs.php @@ -74,15 +74,49 @@ function get_id_from_blogname( $name ) { * Retrieve the details for a blog from the blogs table and blog options. * * @since 3.0 - * @param int $blog_id Blog ID + * @param int|string|array $fields A blog ID, a blog name, or an array of fields to query against. * @param bool $get_all Whether to retrieve all details or only the details in the blogs table. Default is true. * @return object Blog details. */ -function get_blog_details( $blog_id, $get_all = true ) { +function get_blog_details( $fields, $get_all = true ) { global $wpdb; - if ( !is_numeric( $blog_id ) ) - $blog_id = get_id_from_blogname( $blog_id ); + if ( is_array($fields ) ) { + if ( isset($fields['blog_id']) ) { + $blog_id = $fields['blog_id']; + } elseif ( isset($fields['domain']) && isset($fields['path']) ) { + $key = md5( $fields['domain'] . $fields['path'] ); + $blog = wp_cache_get($key, 'blog-lookup'); + if ( false !== $blog ) + return $blog; + $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) ); + if ( $blog ) { + wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details'); + $blog_id = $blog->blog_id; + } else { + return false; + } + } elseif ( isset($fields['domain']) && is_subdomain_install() ) { + $key = md5( $fields['domain'] ); + $blog = wp_cache_get($key, 'blog-lookup'); + if ( false !== $blog ) + return $blog; + $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) ); + if ( $blog ) { + wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details'); + $blog_id = $blog->blog_id; + } else { + return false; + } + } else { + return false; + } + } else { + if ( !is_numeric( $fields ) ) + $blog_id = get_id_from_blogname( $fields ); + else + $blog_id = $fields; + } $blog_id = (int) $blog_id; @@ -100,10 +134,31 @@ function get_blog_details( $blog_id, $get_all = true ) { return $details; } - $details = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE blog_id = %d", $blog_id ) ); - if ( ! $details ) { - wp_cache_set( $blog_id . $all, -1, 'blog-details' ); - return false; + // Try the other cache. + if ( $get_all ) { + $details = wp_cache_get( $blog_id . 'short', 'blog-details' ); + } else { + $details = wp_cache_get( $blog_id, 'blog-details' ); + // If short was requested and full cache is set, we can return. + if ( $details ) { + if ( ! is_object( $details ) ) { + if ( $details == -1 ) + return false; + else + // Clear old pre-serialized objects. Cache clients do better with that. + wp_cache_delete( $blog_id . $all, 'blog-details' ); + } + return $details; + } + } + + if ( !$details ) { + $details = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE blog_id = %d", $blog_id ) ); + if ( ! $details ) { + // Set the full cache. + wp_cache_set( $blog_id, -1, 'blog-details' ); + return false; + } } if ( ! $get_all ) { diff --git a/wp-includes/ms-settings.php b/wp-includes/ms-settings.php index dd5262daba..8242b2498e 100644 --- a/wp-includes/ms-settings.php +++ b/wp-includes/ms-settings.php @@ -48,7 +48,7 @@ if ( ! isset( $current_site->blog_id ) ) if ( is_subdomain_install() ) { $current_blog = wp_cache_get( 'current_blog_' . $domain, 'site-options' ); if ( !$current_blog ) { - $current_blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $domain ) ); + $current_blog = get_blog_details( array('domain' => $domain), false ); if ( $current_blog ) wp_cache_set( 'current_blog_' . $domain, $current_blog, 'site-options' ); } @@ -67,7 +67,7 @@ if ( is_subdomain_install() ) { $path .= $blogname . '/'; $current_blog = wp_cache_get( 'current_blog_' . $domain . $path, 'site-options' ); if ( ! $current_blog ) { - $current_blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $domain, $path ) ); + $current_blog = $current_blog = get_blog_details( array('domain' => $domain, 'path' => $path ), false ); if ( $current_blog ) wp_cache_set( 'current_blog_' . $domain . $path, $current_blog, 'site-options' ); } @@ -91,7 +91,7 @@ if ( ! defined( 'WP_INSTALLING' ) ) { header( 'Location: http://' . $current_site->domain . $current_site->path ); exit; } - $current_blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $current_site->domain, $current_site->path ) ); + $current_blog = get_blog_details( array('domain' => $current_site->domain, 'path' => $current_site->path), false ); } if ( ! $current_blog || ! $current_site ) is_installed(); diff --git a/wp-settings.php b/wp-settings.php index 18d2597dc0..56343ea883 100644 --- a/wp-settings.php +++ b/wp-settings.php @@ -59,7 +59,7 @@ if ( WP_CACHE ) // Define WP_LANG_DIR if not set. wp_set_lang_dir(); -// Include early WordPress files. +// Load early WordPress files. require( ABSPATH . WPINC . '/compat.php' ); require( ABSPATH . WPINC . '/functions.php' ); require( ABSPATH . WPINC . '/classes.php' ); @@ -73,17 +73,17 @@ wp_set_wpdb_vars(); // Start the WordPress object cache, or an external object cache if the drop-in is present. wp_start_object_cache(); +// Load early WordPress files. +require( ABSPATH . WPINC . '/plugin.php' ); +require( ABSPATH . WPINC . '/default-filters.php' ); +include_once( ABSPATH . WPINC . '/pomo/mo.php' ); + // Initialize multisite if enabled. if ( is_multisite() ) { require( ABSPATH . WPINC . '/blogs.php' ); require( ABSPATH . WPINC . '/ms-settings.php' ); } -// Load early WordPress files. -require( ABSPATH . WPINC . '/plugin.php' ); -require( ABSPATH . WPINC . '/default-filters.php' ); -include_once( ABSPATH . WPINC . '/pomo/mo.php' ); - // Stop most of WordPress from being loaded if we just want the basics. if ( SHORTINIT ) return false;