Skip to:
Content

BuddyPress.org

Changeset 10815


Ignore:
Timestamp:
05/27/2016 05:21:20 AM (3 years ago)
Author:
r-a-y
Message:

Blogs: Improve how bp_blogs_record_existing_blogs() operates.

When the "Repopulate blogs records" is used from the "Tools > BuddyPress"
page, on large site installs, this option isn't very effective.

This commit improves bp_blogs_record_existing_blogs() by:

  • Recording an offset so the blog record repopulation can be resumed on failure.
  • Only recording the root blog for the current user if run from single site. (See #6940.)
  • Fixing issues with blog count caches.
  • Giving each blog a better last activity timestamp by using the last_updated column from the wp_blogs DB table.
  • Supporting passing selected blog IDs in case a dev wants to re-record specific blogs.

Fixes #6370.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-blogs/bp-blogs-functions.php

    r10731 r10815  
    7373 * Populate the BP blogs table with existing blogs.
    7474 *
     75 * Warning: By default, this will remove all existing records from the BP
     76 * blogs and blogmeta tables before re-populating the tables.
     77 *
    7578 * @since 1.0.0
    76  *
    77  * @global object $wpdb WordPress database object.
    78  * @uses get_users()
    79  * @uses bp_blogs_record_blog()
     79 * @since 2.6.0 Accepts $args as a parameter.
     80 *
     81 * @param array $args {
     82 *     Array of arguments.
     83 *     @type int   $offset   The offset to use.
     84 *     @type int   $limit    The number of blogs to record at one time.
     85 *     @type array $blog_ids Blog IDs to record. If empty, all blogs will be recorded.
     86 *     @type array $site_id  The network site ID to use.
     87 * }
    8088 *
    8189 * @return bool
    8290 */
    83 function bp_blogs_record_existing_blogs() {
     91function bp_blogs_record_existing_blogs( $args = array() ) {
    8492    global $wpdb;
    8593
    8694    // Query for all sites in network.
     95    $r = bp_parse_args( $args, array(
     96        'offset'   => false === bp_get_option( '_bp_record_blogs_offset' ) ? 0 : bp_get_option( '_bp_record_blogs_offset' ),
     97        'limit'    => 50,
     98        'blog_ids' => array(),
     99        'site_id'  => $wpdb->siteid
     100    ), 'record_existing_blogs' );
     101
     102    // Truncate all BP blogs tables if starting fresh
     103    if ( empty( $r['offset'] ) && empty( $r['blog_ids'] ) ) {
     104        $bp = buddypress();
     105
     106        // Truncate user blogs table
     107        $truncate = $wpdb->query( "TRUNCATE {$bp->blogs->table_name}" );
     108        if ( is_wp_error( $truncate ) ) {
     109            return false;
     110        }
     111
     112        // Truncate user blogmeta table
     113        $truncate = $wpdb->query( "TRUNCATE {$bp->blogs->table_name_blogmeta}" );
     114        if ( is_wp_error( $truncate ) ) {
     115            return false;
     116        }
     117    }
     118
     119    // Multisite
    87120    if ( is_multisite() ) {
    88 
    89         // Get blog ID's if not a large network.
    90         if ( ! wp_is_large_network() ) {
    91             $blog_ids = $wpdb->get_col( $wpdb->prepare( "SELECT blog_id FROM {$wpdb->base_prefix}blogs WHERE mature = 0 AND spam = 0 AND deleted = 0 AND site_id = %d", $wpdb->siteid ) );
    92 
    93             // If error running this query, set blog ID's to false.
    94             if ( is_wp_error( $blog_ids ) ) {
    95                 $blog_ids = false;
    96             }
    97 
    98         // Large networks are not currently supported.
    99         } else {
    100             $blog_ids = false;
    101         }
     121        $sql = array();
     122        $sql['select'] = $wpdb->prepare( "SELECT blog_id, last_updated FROM {$wpdb->base_prefix}blogs WHERE mature = 0 AND spam = 0 AND deleted = 0 AND site_id = %d", $r['site_id'] );
     123
     124        // Omit root blog if large network
     125        if ( wp_is_large_network( 'users' ) ) {
     126            $sql['omit_root_blog'] = $wpdb->prepare( "AND blog_id != %d", bp_get_root_blog_id() );
     127        }
     128
     129        // Filter by selected blog IDs
     130        if ( ! empty( $r['blog_ids'] ) ) {
     131            $in        = implode( ',', wp_parse_id_list( $r['blog_ids'] ) );
     132            $sql['in'] = "AND blog_id IN ({$in})";
     133        }
     134
     135        $sql['orderby'] = 'ORDER BY blog_id ASC';
     136
     137        $sql['limit'] = $wpdb->prepare( "LIMIT %d", $r['limit'] );
     138
     139        if ( ! empty( $r['offset'] ) ) {
     140            $sql['offset'] = $wpdb->prepare( "OFFSET %d", $r['offset'] );
     141        }
     142
     143        $blogs = $wpdb->get_results( implode( ' ', $sql ) );
    102144
    103145    // Record a single site.
    104146    } else {
    105         $blog_ids = $wpdb->blogid;
    106     }
    107 
    108     // Bail if there are no blogs in the network.
    109     if ( empty( $blog_ids ) ) {
     147        // Just record blog for the current user only.
     148        $record = bp_blogs_record_blog( $wpdb->blogid, get_current_user_id(), true );
     149
     150        if ( false === $record ) {
     151            return false;
     152        } else {
     153            return true;
     154        }
     155    }
     156
     157     // Bail if there are no blogs
     158     if ( empty( $blogs ) ) {
     159        // Make sure we remove our offset marker
     160        if ( is_multisite() ) {
     161            bp_delete_option( '_bp_record_blogs_offset' );
     162        }
     163
    110164        return false;
    111165    }
    112166
    113     // Get BuddyPress.
    114     $bp = buddypress();
    115 
    116     // Truncate user blogs table.
    117     $truncate = $wpdb->query( "TRUNCATE {$bp->blogs->table_name}" );
    118     if ( is_wp_error( $truncate ) ) {
    119         return false;
    120     }
    121 
    122     // Truncate user blogsmeta table.
    123     $truncate = $wpdb->query( "TRUNCATE {$bp->blogs->table_name_blogmeta}" );
    124     if ( is_wp_error( $truncate ) ) {
    125         return false;
    126     }
    127 
    128167    // Loop through users of blogs and record the relationship.
    129     foreach ( (array) $blog_ids as $blog_id ) {
     168    foreach ( (array) $blogs as $blog ) {
    130169
    131170        // Ensure that the cache is clear after the table TRUNCATE above.
    132         wp_cache_delete( $blog_id, 'blog_meta' );
     171        wp_cache_delete( $blog->blog_id, 'blog_meta' );
    133172
    134173        // Get all users.
    135174        $users = get_users( array(
    136             'blog_id' => $blog_id
     175            'blog_id' => $blog->blog_id,
     176            'fields'  => 'ID'
    137177        ) );
    138178
     
    143183
    144184        // Loop through users and record their relationship to this blog.
    145         foreach ( (array) $users as $user ) {
    146             bp_blogs_add_user_to_blog( $user->ID, false, $blog_id );
     185        foreach ( (array) $users as $user_id ) {
     186            bp_blogs_add_user_to_blog( $user_id, false, $blog->blog_id );
     187
     188            // Clear cache
     189            bp_blogs_clear_blog_object_cache( $blog->blog_id, $user_id );
     190        }
     191
     192        // Update blog last activity timestamp
     193        if ( ! empty( $blog->last_updated ) && false !== strtotime( $blog->last_updated ) ) {
     194            bp_blogs_update_blogmeta( $blog->blog_id, 'last_activity', $blog->last_updated );
     195        }
     196    }
     197
     198    // See if we need to do this again
     199    if ( is_multisite() && empty( $r['blog_ids'] ) ) {
     200        $sql['offset'] = $wpdb->prepare( " OFFSET %d", $r['limit'] + $r['offset'] );
     201
     202        // Check if there are more blogs to record
     203        $blog_ids = $wpdb->get_results( implode( ' ', $sql ) );
     204
     205        // We have more blogs; record offset and re-run function
     206        if ( ! empty( $blog_ids  ) ) {
     207            bp_update_option( '_bp_record_blogs_offset', $r['limit'] + $r['offset'] );
     208            bp_blogs_record_existing_blogs( array(
     209                'offset'   => $r['limit'] + $r['offset'],
     210                'limit'    => $r['limit'],
     211                'blog_ids' => $r['blog_ids'],
     212                'site_id'  => $r['site_id']
     213            ) );
     214
     215            // Bail since we have more blogs to record.
     216            return;
     217
     218        // No more blogs; delete offset marker
     219        } else {
     220            bp_delete_option( '_bp_record_blogs_offset' );
    147221        }
    148222    }
  • trunk/src/bp-core/admin/bp-core-admin-tools.php

    r10583 r10815  
    474474    return array( 0, __( 'Emails have been successfully reinstalled.', 'buddypress' ) );
    475475}
     476
     477/**
     478 * Add notice on the "Tools > BuddyPress" page if more sites need recording.
     479 *
     480 * This notice only shows up in the network admin dashboard.
     481 *
     482 * @since 2.6.0
     483 */
     484function bp_core_admin_notice_repopulate_blogs_resume() {
     485    $screen = get_current_screen();
     486    if ( 'tools_page_bp-tools-network' !== $screen->id ) {
     487        return;
     488    }
     489
     490    if ( '' === bp_get_option( '_bp_record_blogs_offset' ) ) {
     491        return;
     492    }
     493
     494    echo '<div class="error"><p>' . __( 'It looks like you have more sites to record. Resume recording by checking the "Repopulate site tracking records" option.', 'buddypress' ) . '</p></div>';
     495}
     496add_action( 'network_admin_notices', 'bp_core_admin_notice_repopulate_blogs_resume' );
  • trunk/tests/phpunit/testcases/blogs/functions.php

    r10545 r10815  
    985985    }
    986986
     987    /**
     988     * @group bp_blogs_record_existing_blogs
     989     */
     990    public function test_bp_blogs_record_existing_blogs_limit() {
     991        if ( ! is_multisite() ) {
     992            return;
     993        }
     994
     995        $old_user = get_current_user_id();
     996
     997        $u = $this->factory->user->create();
     998        $this->set_current_user( $u );
     999
     1000        // Create three sites.
     1001        $this->factory->blog->create_many( 3, array(
     1002            'user_id' => $u
     1003        ) );
     1004
     1005        // Record each site one at a time
     1006        bp_blogs_record_existing_blogs( array(
     1007            'limit' => 1
     1008        ) );
     1009
     1010        // Assert!
     1011        $blogs = bp_blogs_get_blogs( array(
     1012            'user_id' => $u
     1013        ) );
     1014        $this->assertSame( 3, (int) $blogs['total'] );
     1015
     1016        $this->set_current_user( $old_user );
     1017    }
     1018
    9871019    protected function activity_exists_for_post( $post_id ) {
    9881020        $a = bp_activity_get( array(
Note: See TracChangeset for help on using the changeset viewer.