Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/12/2014 09:27:20 PM (10 years ago)
Author:
boonebgorges
Message:

Migrate user 'last_activity' data from usermeta to the activity table

Storing last_activity in usermeta caused severe bottlenecks on sites with
large user bases. The usermeta table has a tendency to get bloated. Its
option_value column is not indexed, and even if it were, it would not be
indexed properly for the kind of chronological sorting that BuddyPress was
using it for.

This changeset refactors all core last_activity user functionality, so that
the data is stored in the wp_bp_activity table (even when the activity
component is disabled).

For backward compatibility with plugins that reference last_activity metadata
entries, all last_activity data is retained in wp_usermeta, and new data will
be mirrored there until further notice.

See #5128

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-members/bp-members-functions.php

    r7764 r7860  
    511511
    512512    if ( !$count = get_transient( 'bp_active_member_count' ) ) {
     513        $bp = buddypress();
     514
    513515        // Avoid a costly join by splitting the lookup
    514516        if ( is_multisite() ) {
     
    520522        $exclude_users     = $wpdb->get_col( $sql );
    521523        $exclude_users_sql = !empty( $exclude_users ) ? "AND user_id NOT IN (" . implode( ',', wp_parse_id_list( $exclude_users ) ) . ")" : '';
    522         $count             = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(user_id) FROM {$wpdb->usermeta} WHERE meta_key = %s {$exclude_users_sql}", bp_get_user_meta_key( 'last_activity' ) ) );
     524        $count             = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(user_id) FROM {$bp->members->table_name_last_activity} WHERE component = %s AND type = 'last_activity' {$exclude_users_sql}", $bp->members->id ) );
    523525
    524526        set_transient( 'bp_active_member_count', $count );
     
    818820
    819821/**
    820  * Update a user's last activity
    821  *
    822  * @since BuddyPress (1.9)
    823  * @param int $user_id ID of the user being updated
    824  * @param string $time Time of last activity, in 'Y-m-d H:i:s' format
    825  * @return bool True on success
     822 * Update a user's last activity.
     823 *
     824 * @since BuddyPress (1.9.0)
     825 *
     826 * @param int $user_id ID of the user being updated.
     827 * @param string $time Time of last activity, in 'Y-m-d H:i:s' format.
     828 * @return bool True on success, false on failure.
    826829 */
    827830function bp_update_user_last_activity( $user_id = 0, $time = '' ) {
     
    841844    }
    842845
    843     return bp_update_user_meta( $user_id, 'last_activity', $time );
    844 }
    845 
    846 /**
    847  * Get the last activity for a given user
    848  *
    849  * @param int $user_id The ID of the user
     846    // As of BuddyPress 2.0, last_activity is no longer stored in usermeta.
     847    // However, we mirror it there for backward compatibility. Do not use!
     848    // Remove our warning and re-add.
     849    remove_filter( 'update_user_metadata', '_bp_update_user_meta_last_activity_warning', 10, 4 );
     850    update_user_meta( $user_id, 'last_activity', $time );
     851    add_filter( 'update_user_metadata', '_bp_update_user_meta_last_activity_warning', 10, 4 );
     852
     853    return BP_Core_User::update_last_activity( $user_id, $time );
     854}
     855
     856/**
     857 * Backward compatibility for 'last_activity' usermeta fetching.
     858 *
     859 * In BuddyPress 2.0, user last_activity data was moved out of usermeta. For
     860 * backward compatibility, we continue to mirror the data there. This function
     861 * serves two purposes: it warns plugin authors of the change, and it returns
     862 * the data from the proper location.
     863 *
     864 * @since BuddyPress (2.0.0)
     865 *
     866 * @access private For internal use only.
     867 *
     868 * @param null $retval
     869 * @param int $object_id ID of the user.
     870 * @param string $meta_key Meta key being fetched.
     871 */
     872function _bp_get_user_meta_last_activity_warning( $retval, $object_id, $meta_key ) {
     873    static $warned;
     874
     875    if ( 'last_activity' === $meta_key ) {
     876        // Don't send the warning more than once per pageload
     877        if ( empty( $warned ) ) {
     878            _doing_it_wrong( 'get_user_meta( $user_id, \'last_activity\' )', __( 'User last_activity data is no longer stored in usermeta. Use bp_get_user_last_activity() instead.', 'buddypress' ), '2.0.0' );
     879            $warned = 1;
     880        }
     881
     882        return bp_get_user_last_activity( $object_id );
     883    }
     884
     885    return $retval;
     886}
     887add_filter( 'get_user_metadata', '_bp_get_user_meta_last_activity_warning', 10, 3 );
     888
     889/**
     890 * Backward compatibility for 'last_activity' usermeta setting.
     891 *
     892 * In BuddyPress 2.0, user last_activity data was moved out of usermeta. For
     893 * backward compatibility, we continue to mirror the data there. This function
     894 * serves two purposes: it warns plugin authors of the change, and it updates
     895 * the data in the proper location.
     896 *
     897 * @since BuddyPress (2.0.0)
     898 *
     899 * @access private For internal use only.
     900 *
     901 * @param int $meta_id ID of the just-set usermeta row.
     902 * @param int $object_id ID of the user.
     903 * @param string $meta_key Meta key being fetched.
     904 * @param string $meta_value Active time.
     905 */
     906function _bp_update_user_meta_last_activity_warning( $meta_id, $object_id, $meta_key, $meta_value ) {
     907    if ( 'last_activity' === $meta_key ) {
     908        _doing_it_wrong( 'update_user_meta( $user_id, \'last_activity\' )', __( 'User last_activity data is no longer stored in usermeta. Use bp_update_user_last_activity() instead.', 'buddypress' ), '2.0.0' );
     909        bp_update_user_last_activity( $object_id, $meta_value );
     910    }
     911}
     912add_filter( 'update_user_metadata', '_bp_update_user_meta_last_activity_warning', 10, 4 );
     913
     914/**
     915 * Get the last activity for a given user.
     916 *
     917 * @param int $user_id The ID of the user.
    850918 * @return string Time of last activity, in 'Y-m-d H:i:s' format, or an empty
    851  *   string if none is found
     919 *         string if none is found.
    852920 */
    853921function bp_get_user_last_activity( $user_id = 0 ) {
    854     // Fall back on current user
    855     if ( empty( $user_id ) ) {
    856         $user_id = bp_loggedin_user_id();
    857     }
    858 
    859     $activity = bp_get_user_meta( $user_id, 'last_activity', true );
     922    $activity = '';
     923
     924    $last_activity = BP_Core_User::get_last_activity( $user_id );
     925    if ( ! empty( $last_activity[ $user_id ] ) ) {
     926        $activity = $last_activity[ $user_id ]['date_recorded'];
     927    }
    860928
    861929    return apply_filters( 'bp_get_user_last_activity', $activity, $user_id );
     
    9971065function bp_core_remove_data( $user_id ) {
    9981066
    999     // Remove usermeta
    1000     bp_delete_user_meta( $user_id, 'last_activity' );
     1067    // Remove last_activity data
     1068    BP_Core_User::delete_last_activity( $user_id );
    10011069
    10021070    // Flush the cache to remove the user from all cached objects
Note: See TracChangeset for help on using the changeset viewer.