Make WordPress Core

Changeset 13126


Ignore:
Timestamp:
02/13/2010 11:09:54 PM (17 years ago)
Author:
ryan
Message:

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

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/blogs.php

    r13125 r13126  
    7575 *
    7676 * @since 3.0
    77  * @param int $blog_id Blog ID
     77 * @param int|string|array $fields A blog ID, a blog name, or an array of fields to query against.
    7878 * @param bool $get_all Whether to retrieve all details or only the details in the blogs table. Default is true.
    7979 * @return object Blog details.
    8080 */
    81 function get_blog_details( $blog_id, $get_all = true ) {
    82         global $wpdb;
    83 
    84         if ( !is_numeric( $blog_id ) )
    85                 $blog_id = get_id_from_blogname( $blog_id );
     81function get_blog_details( $fields, $get_all = true ) {
     82        global $wpdb;
     83
     84        if ( is_array($fields ) ) {
     85                if ( isset($fields['blog_id']) ) {
     86                        $blog_id = $fields['blog_id'];
     87                } elseif ( isset($fields['domain']) && isset($fields['path']) ) {
     88                        $key = md5( $fields['domain'] . $fields['path'] );
     89                        $blog = wp_cache_get($key, 'blog-lookup');
     90                        if ( false !== $blog )
     91                                return $blog;
     92                        $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) );
     93                        if ( $blog ) {
     94                                wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
     95                                $blog_id = $blog->blog_id;
     96                        } else {
     97                                return false;
     98                        }
     99                } elseif ( isset($fields['domain']) && is_subdomain_install() ) {
     100                        $key = md5( $fields['domain'] );
     101                        $blog = wp_cache_get($key, 'blog-lookup');
     102                        if ( false !== $blog )
     103                                return $blog;
     104                        $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) );
     105                        if ( $blog ) {
     106                                wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
     107                                $blog_id = $blog->blog_id;
     108                        } else {
     109                                return false;
     110                        }
     111                } else {
     112                        return false;
     113                }
     114        } else {
     115                if ( !is_numeric( $fields ) )
     116                        $blog_id = get_id_from_blogname( $fields );
     117                else
     118                        $blog_id = $fields;
     119        }
    86120
    87121        $blog_id = (int) $blog_id;
     
    101135        }
    102136
    103         $details = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE blog_id = %d", $blog_id ) );
    104         if ( ! $details ) {
    105                 wp_cache_set( $blog_id . $all, -1, 'blog-details' );
    106                 return false;
     137        // Try the other cache.
     138        if ( $get_all ) {
     139                $details = wp_cache_get( $blog_id . 'short', 'blog-details' );
     140        } else {
     141                $details = wp_cache_get( $blog_id, 'blog-details' );
     142                // If short was requested and full cache is set, we can return.
     143                if ( $details ) {
     144                        if ( ! is_object( $details ) ) {
     145                                if ( $details == -1 )
     146                                        return false;
     147                                else
     148                                        // Clear old pre-serialized objects. Cache clients do better with that.
     149                                        wp_cache_delete( $blog_id . $all, 'blog-details' );
     150                        }
     151                        return $details;
     152                }
     153        }
     154
     155        if ( !$details ) {
     156                $details = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE blog_id = %d", $blog_id ) );
     157                if ( ! $details ) {
     158                        // Set the full cache.
     159                        wp_cache_set( $blog_id, -1, 'blog-details' );
     160                        return false;
     161                }
    107162        }
    108163
  • trunk/wp-includes/ms-settings.php

    r13065 r13126  
    4949        $current_blog = wp_cache_get( 'current_blog_' . $domain, 'site-options' );
    5050        if ( !$current_blog ) {
    51                 $current_blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $domain ) );
     51                $current_blog = get_blog_details( array('domain' => $domain), false );
    5252                if ( $current_blog )
    5353                        wp_cache_set( 'current_blog_' . $domain, $current_blog, 'site-options' );
     
    6868        $current_blog = wp_cache_get( 'current_blog_' . $domain . $path, 'site-options' );
    6969        if ( ! $current_blog ) {
    70                 $current_blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $domain, $path ) );
     70                $current_blog = $current_blog = get_blog_details( array('domain' => $domain, 'path' => $path ), false );
    7171                if ( $current_blog )
    7272                        wp_cache_set( 'current_blog_' . $domain . $path, $current_blog, 'site-options' );
     
    9292                        exit;
    9393                }
    94                 $current_blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $current_site->domain, $current_site->path ) );
     94                $current_blog = get_blog_details( array('domain' => $current_site->domain, 'path' => $current_site->path), false );
    9595        }
    9696        if ( ! $current_blog || ! $current_site )
  • trunk/wp-settings.php

    r13125 r13126  
    6060wp_set_lang_dir();
    6161
    62 // Include early WordPress files.
     62// Load early WordPress files.
    6363require( ABSPATH . WPINC . '/compat.php' );
    6464require( ABSPATH . WPINC . '/functions.php' );
     
    7373// Start the WordPress object cache, or an external object cache if the drop-in is present.
    7474wp_start_object_cache();
     75
     76// Load early WordPress files.
     77require( ABSPATH . WPINC . '/plugin.php' );
     78require( ABSPATH . WPINC . '/default-filters.php' );
     79include_once( ABSPATH . WPINC . '/pomo/mo.php' );
    7580
    7681// Initialize multisite if enabled.
     
    7984        require( ABSPATH . WPINC . '/ms-settings.php' );
    8085}
    81 
    82 // Load early WordPress files.
    83 require( ABSPATH . WPINC . '/plugin.php' );
    84 require( ABSPATH . WPINC . '/default-filters.php' );
    85 include_once( ABSPATH . WPINC . '/pomo/mo.php' );
    8686
    8787// Stop most of WordPress from being loaded if we just want the basics.
Note: See TracChangeset for help on using the changeset viewer.