Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
06/09/2014 06:33:05 PM (8 years ago)
Author:
boonebgorges
Message:

Disable COUNT queries by default in bp_activity_get() stack

The COUNT query performed to get the total activity count has proven to be a
performance bottleneck on large sites. Moreover, the way that activity items
are displayed on the front end (using the Load More link) means that the total
count is not actually used by BuddyPress in most cases. To reduce query
overhead, we introduce a 'count_total' parameter to the bp_activity_get()
stack, and set it to false by default.

For backward compatibility, a few additional changes are introduced:

  • In the activity-loop.php template, the <noscript> pagination markup is removed. In its place, the Load More link is refactored so that it loads the next available page of activity items.
  • The mechanism used to determine whether there are more activity items to show (bp_activity_has_more_items()) has been refined; when no COUNT query takes place, we query for the $per_page value + 1 to infer whether more items are available.

Fixes #5629

Props boonebgorges, r-a-y

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-activity/bp-activity-template.php

    r8477 r8491  
    9898
    9999    var $in_the_loop;
     100
     101    /**
     102     * URL parameter key for activity pagination. Default: 'acpage'.
     103     *
     104     * @since BuddyPress (2.1.0)
     105     * @var string
     106     */
     107    var $pag_arg;
    100108
    101109    var $pag_page;
     
    179187        extract( $r );
    180188
    181         $this->pag_page = isset( $_REQUEST[$page_arg] ) ? intval( $_REQUEST[$page_arg] ) : $page;
     189        $this->pag_arg  = $r['page_arg'];
     190        $this->pag_page = isset( $_REQUEST[ $this->pag_arg ] ) ? intval( $_REQUEST[ $this->pag_arg ] ) : $page;
    182191        $this->pag_num  = isset( $_REQUEST['num'] ) ? intval( $_REQUEST['num'] ) : $per_page;
    183192
     
    221230        }
    222231
    223         if ( !$max || $max >= (int) $this->activities['total'] )
    224             $this->total_activity_count = (int) $this->activities['total'];
    225         else
    226             $this->total_activity_count = (int) $max;
     232        // The total_activity_count property will be set only if a
     233        // 'count_total' query has taken place
     234        if ( ! is_null( $this->activities['total'] ) ) {
     235            if ( ! $max || $max >= (int) $this->activities['total'] ) {
     236                $this->total_activity_count = (int) $this->activities['total'];
     237            } else {
     238                $this->total_activity_count = (int) $max;
     239            }
     240        }
     241
     242        $this->has_more_items = $this->activities['has_more_items'];
    227243
    228244        $this->activities = $this->activities['activities'];
     
    707723
    708724/**
     725 * Output the URL for the Load More link.
     726 *
     727 * @since BuddyPress (2.1.0)
     728 */
     729function bp_activity_load_more_link() {
     730    echo bp_get_activity_load_more_link();
     731}
     732    function bp_get_activity_load_more_link() {
     733        global $activities_template;
     734
     735        $link = bp_get_requested_url();
     736        $link = add_query_arg( $activities_template->pag_arg, $activities_template->pag_page + 1, $link );
     737
     738        return apply_filters( 'bp_get_activity_load_more_link', $link );
     739    }
     740
     741/**
    709742 * Output the activity pagination count.
    710743 *
     
    779812    global $activities_template;
    780813
    781     $remaining_pages = 0;
    782 
    783     if ( ! empty( $activities_template->pag_page ) ) {
    784         $remaining_pages = floor( ( $activities_template->total_activity_count - 1 ) / ( $activities_template->pag_num * $activities_template->pag_page ) );
    785     }
    786 
    787     $has_more_items  = (int) $remaining_pages ? true : false;
     814    if ( ! empty( $activities_template->has_more_items )  ) {
     815        $has_more_items = true;
     816    } else {
     817        $remaining_pages = 0;
     818
     819        if ( ! empty( $activities_template->pag_page ) ) {
     820            $remaining_pages = floor( ( $activities_template->total_activity_count - 1 ) / ( $activities_template->pag_num * $activities_template->pag_page ) );
     821        }
     822
     823        $has_more_items = (int) $remaining_pages > 0;
     824    }
    788825
    789826    return apply_filters( 'bp_activity_has_more_items', $has_more_items );
Note: See TracChangeset for help on using the changeset viewer.