<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[BuddyPress] [2272] trunk/bp-activity:
Reduced the query count for selection of activity and activity comments by removing item level queries
.</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd>2272</dd>
<dt>Author</dt> <dd>apeatling</dd>
<dt>Date</dt> <dd>2010-01-06 18:01:09 +0000 (Wed, 06 Jan 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>Reduced the query count for selection of activity and activity comments by removing item level queries.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpactivitybpactivityclassesphp">trunk/bp-activity/bp-activity-classes.php</a></li>
<li><a href="#trunkbpactivitybpactivitytemplatetagsphp">trunk/bp-activity/bp-activity-templatetags.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpactivitybpactivityclassesphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-activity/bp-activity-classes.php (2271 => 2272)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-activity/bp-activity-classes.php        2010-01-06 16:30:10 UTC (rev 2271)
+++ trunk/bp-activity/bp-activity-classes.php        2010-01-06 18:01:09 UTC (rev 2272)
</span><span class="lines">@@ -206,8 +206,26 @@
</span><span class="cx">         function get( $max = false, $page = 1, $per_page = 25, $sort = 'DESC', $search_terms = false, $filter = false, $display_comments = false, $show_hidden = false ) {
</span><span class="cx">                 global $wpdb, $bp;
</span><span class="cx">
</span><ins>+                /* Select conditions */
+                $select_sql = "SELECT a.*, u.user_email, u.user_nicename, u.user_login";
+
+                if ( function_exists( 'xprofile_install' ) )
+                        $select_sql .= ", pd.value as user_fullname";
+
+                $from_sql = " FROM {$bp->activity->table_name} a, {$wpdb->users} u";
+
+                if ( function_exists( 'xprofile_install' ) )
+                        $from_sql .= ", {$bp->profile->table_name_data} pd";
+
+                /* Where conditions */
</ins><span class="cx">                 $where_conditions = array();
</span><ins>+                $where_conditions['user_join'] = "a.user_id = u.ID";
</ins><span class="cx">
</span><ins>+                if ( function_exists( 'xprofile_install' ) ) {
+                        $where_conditions['xprofile_join'] = "a.user_id = pd.user_id";
+                        $where_conditions['xprofile_filter'] = "pd.field_id = 1";
+                }
+
</ins><span class="cx">                 if ( $per_page && $page )
</span><span class="cx">                         $pag_sql = $wpdb->prepare( "LIMIT %d, %d", intval( ( $page - 1 ) * $per_page ), intval( $per_page ) );
</span><span class="cx">
</span><span class="lines">@@ -217,7 +235,7 @@
</span><span class="cx">                 /* Searching */
</span><span class="cx">                 if ( $search_terms ) {
</span><span class="cx">                         $search_terms = $wpdb->escape( $search_terms );
</span><del>-                        $where_conditions['search_sql'] = "content LIKE '%%" . like_escape( $search_terms ) . "%%'";
</del><ins>+                        $where_conditions['search_sql'] = "a.content LIKE '%%" . like_escape( $search_terms ) . "%%'";
</ins><span class="cx">                 }
</span><span class="cx">
</span><span class="cx">                 /* Filtering */
</span><span class="lines">@@ -230,21 +248,21 @@
</span><span class="cx">
</span><span class="cx">                 /* Hide Hidden Items? */
</span><span class="cx">                 if ( !$show_hidden )
</span><del>-                        $where_conditions['hidden_sql'] = "hide_sitewide = 0";
</del><ins>+                        $where_conditions['hidden_sql'] = "a.hide_sitewide = 0";
</ins><span class="cx">
</span><span class="cx">                 /* Alter the query based on whether we want to show activity item comments in the stream like normal comments or threaded below the activity */
</span><span class="cx">                 if ( !$display_comments || 'threaded' == $display_comments ) {
</span><del>-                        $where_conditions[] = "component_action != 'activity_comment'";
</del><ins>+                        $where_conditions[] = "a.component_action != 'activity_comment'";
</ins><span class="cx">                 }
</span><span class="cx">
</span><span class="cx">                 $where_sql = 'WHERE ' . join( ' AND ', $where_conditions );
</span><span class="cx">
</span><span class="cx">                 if ( $per_page && $page && $max )
</span><del>-                        $activities = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name} {$where_sql} ORDER BY date_recorded {$sort} {$pag_sql}" ) );
</del><ins>+                        $activities = $wpdb->get_results( $wpdb->prepare( "{$select_sql} {$from_sql} {$where_sql} ORDER BY a.date_recorded {$sort} {$pag_sql}" ) );
</ins><span class="cx">                 else
</span><del>-                        $activities = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name} {$where_sql} ORDER BY date_recorded {$sort} {$pag_sql} {$max_sql}" ) );
</del><ins>+                        $activities = $wpdb->get_results( $wpdb->prepare( "{$select_sql} {$from_sql} {$where_sql} ORDER BY a.date_recorded {$sort} {$pag_sql} {$max_sql}" ) );
</ins><span class="cx">
</span><del>-                $total_activities = $wpdb->get_var( $wpdb->prepare( "SELECT count(id) FROM {$bp->activity->table_name} {$where_sql} ORDER BY date_recorded {$sort} {$max_sql}" ) );
</del><ins>+                $total_activities = $wpdb->get_var( $wpdb->prepare( "SELECT count(a.id) {$from_sql} {$where_sql} ORDER BY a.date_recorded {$sort} {$max_sql}" ) );
</ins><span class="cx">
</span><span class="cx">                 if ( $activities && $display_comments )
</span><span class="cx">                         $activities = BP_Activity_Activity::append_comments( &$activities );
</span><span class="lines">@@ -301,8 +319,16 @@
</span><span class="cx">                 $stack = array();
</span><span class="cx">
</span><span class="cx">                 /* Retrieve all descendants of the $root node */
</span><del>-                $descendants = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name} WHERE component_action = 'activity_comment' AND item_id = %d AND mptt_left BETWEEN %d AND %d ORDER BY date_recorded ASC", $activity_id, $left, $right ) );
</del><span class="cx">
</span><ins>+                /* Select the user's fullname with the query so we don't have to fetch it for each comment */
+                if ( function_exists( 'xprofile_install' ) ) {
+                        $fullname_select = ", pd.value as user_fullname";
+                        $fullname_from = ", {$bp->profile->table_name_data} pd ";
+                        $fullname_where = "AND pd.user_id = a.user_id AND pd.field_id = 1";
+                }
+
+                $descendants = $wpdb->get_results( $wpdb->prepare( "SELECT a.*, u.user_email, u.user_nicename, u.user_login{$fullname_select} FROM {$bp->activity->table_name} a, {$wpdb->users} u{$fullname_from} WHERE u.ID = a.user_id {$fullname_where} AND a.component_action = 'activity_comment' AND a.item_id = %d AND a.mptt_left BETWEEN %d AND %d ORDER BY a.date_recorded ASC", $activity_id, $left, $right ) );
+
</ins><span class="cx">                 /* Loop descendants and build an assoc array */
</span><span class="cx">                 foreach ( $descendants as $d ) {
</span><span class="cx">                  $d->children = array();
</span><span class="lines">@@ -377,7 +403,7 @@
</span><span class="cx">
</span><span class="cx">                         $counter = 1;
</span><span class="cx">                         foreach( (array) $user_filter as $user ) {
</span><del>-                                $user_sql .= $wpdb->prepare( "user_id = %d", trim( $user ) );
</del><ins>+                                $user_sql .= $wpdb->prepare( "a.user_id = %d", trim( $user ) );
</ins><span class="cx">
</span><span class="cx">                                 if ( $counter != count( $user_filter ) )
</span><span class="cx">                                         $user_sql .= ' || ';
</span><span class="lines">@@ -395,7 +421,7 @@
</span><span class="cx">
</span><span class="cx">                         $counter = 1;
</span><span class="cx">                         foreach( (array) $object_filter as $object ) {
</span><del>-                                $object_sql .= $wpdb->prepare( "component_name = %s", trim( $object ) );
</del><ins>+                                $object_sql .= $wpdb->prepare( "a.component_name = %s", trim( $object ) );
</ins><span class="cx">
</span><span class="cx">                                 if ( $counter != count( $object_filter ) )
</span><span class="cx">                                         $object_sql .= ' || ';
</span><span class="lines">@@ -413,7 +439,7 @@
</span><span class="cx">
</span><span class="cx">                         $counter = 1;
</span><span class="cx">                         foreach( (array) $action_filter as $action ) {
</span><del>-                                $action_sql .= $wpdb->prepare( "component_action = %s", trim( $action ) );
</del><ins>+                                $action_sql .= $wpdb->prepare( "a.component_action = %s", trim( $action ) );
</ins><span class="cx">
</span><span class="cx">                                 if ( $counter != count( $action_filter ) )
</span><span class="cx">                                         $action_sql .= ' || ';
</span><span class="lines">@@ -431,7 +457,7 @@
</span><span class="cx">
</span><span class="cx">                         $counter = 1;
</span><span class="cx">                         foreach( (array) $pid_filter as $pid ) {
</span><del>-                                $pid_sql .= $wpdb->prepare( "item_id = %s", trim( $pid ) );
</del><ins>+                                $pid_sql .= $wpdb->prepare( "a.item_id = %s", trim( $pid ) );
</ins><span class="cx">
</span><span class="cx">                                 if ( $counter != count( $pid_filter ) )
</span><span class="cx">                                         $pid_sql .= ' || ';
</span><span class="lines">@@ -449,7 +475,7 @@
</span><span class="cx">
</span><span class="cx">                         $counter = 1;
</span><span class="cx">                         foreach( (array) $sid_filter as $sid ) {
</span><del>-                                $sid_sql .= $wpdb->prepare( "secondary_item_id = %s", trim( $sid ) );
</del><ins>+                                $sid_sql .= $wpdb->prepare( "a.secondary_item_id = %s", trim( $sid ) );
</ins><span class="cx">
</span><span class="cx">                                 if ( $counter != count( $sid_filter ) )
</span><span class="cx">                                         $sid_sql .= ' || ';
</span></span></pre></div>
<a id="trunkbpactivitybpactivitytemplatetagsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-activity/bp-activity-templatetags.php (2271 => 2272)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-activity/bp-activity-templatetags.php        2010-01-06 16:30:10 UTC (rev 2271)
+++ trunk/bp-activity/bp-activity-templatetags.php        2010-01-06 18:01:09 UTC (rev 2272)
</span><span class="lines">@@ -277,7 +277,8 @@
</span><span class="cx">                         'width' => 20,
</span><span class="cx">                         'height' => 20,
</span><span class="cx">                         'class' => 'avatar',
</span><del>-                        'alt' => __( 'Avatar', 'buddypress' )
</del><ins>+                        'alt' => __( 'Avatar', 'buddypress' ),
+                        'email' => false
</ins><span class="cx">                 );
</span><span class="cx">
</span><span class="cx">                 $r = wp_parse_args( $args, $defaults );
</span><span class="lines">@@ -297,7 +298,11 @@
</span><span class="cx">
</span><span class="cx">                 $object = apply_filters( 'bp_get_activity_avatar_object_' . $activities_template->activity->component_name, $object );
</span><span class="cx">
</span><del>-                return apply_filters( 'bp_get_activity_avatar', bp_core_fetch_avatar( array( 'item_id' => $item_id, 'object' => $object, 'type' => $type, 'alt' => $alt, 'class' => $class, 'width' => $width, 'height' => $height ) ) );
</del><ins>+                /* If this is a user object pass the users' email address for Gravatar so we don't have to refetch it. */
+                if ( 'user' == $object && empty($email) )
+                        $email = $activities_template->activity->user_email;
+
+                return apply_filters( 'bp_get_activity_avatar', bp_core_fetch_avatar( array( 'item_id' => $item_id, 'object' => $object, 'type' => $type, 'alt' => $alt, 'class' => $class, 'width' => $width, 'height' => $height, 'email' => $email ) ) );
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx"> function bp_activity_content() {
</span><span class="lines">@@ -427,7 +432,7 @@
</span><span class="cx">
</span><span class="cx">                 return apply_filters( 'bp_activity_get_comments', $comments_html );
</span><span class="cx">         }
</span><del>-                /* The HTML in this function is temporary, it will be move to template tags once comments are working. */
</del><ins>+                /* TODO: The HTML in this function is temporary and will be moved to the template in a future version. */
</ins><span class="cx">                 function bp_activity_recurse_comments( $comment ) {
</span><span class="cx">                         global $activities_template, $bp;
</span><span class="cx">
</span><span class="lines">@@ -436,12 +441,13 @@
</span><span class="cx">
</span><span class="cx">                         $content .= '<ul>';
</span><span class="cx">                         foreach ( $comment->children as $comment ) {
</span><ins>+                                if ( !$comment->user_fullname )
+                                        $comment->user_fullname = $comment->user_nicename;
+
</ins><span class="cx">                                 $content .= '<li id="acomment-' . $comment->id . '">';
</span><ins>+                                $content .= '<div class="acomment-avatar">' . bp_core_fetch_avatar( array( 'item_id' => $comment->user_id, 'width' => 25, 'height' => 25, 'email' => $comment->user_email ) ) . '</div>';
+                                $content .= '<div class="acomment-meta"><a href="' . bp_core_get_user_domain( $comment->user_id, $comment->user_nicename, $comment->user_login ) . '">' . esc_attr( $comment->user_fullname ) . '</a> &middot; ' . sprintf( __( '%s ago', 'buddypress' ), bp_core_time_since( strtotime( $comment->date_recorded ) ) );
</ins><span class="cx">
</span><del>-                                $content .= '<div class="acomment-avatar">' . bp_core_fetch_avatar( array( 'item_id' => $comment->user_id, 'width' => 25, 'height' => 25 ) ) . '</div>';
-
-                                $content .= '<div class="acomment-meta">' . bp_core_get_userlink( $comment->user_id ) . ' &middot; ' . sprintf( __( '%s ago', 'buddypress' ), bp_core_time_since( strtotime( $comment->date_recorded ) ) );
-
</del><span class="cx">                                 /* Reply link */
</span><span class="cx">                                 if ( is_user_logged_in() )
</span><span class="cx">                                         $content .= ' &middot; <a href="#acomment-' . $comment->id . '" class="acomment-reply" id="acomment-reply-' . $activities_template->activity->id . '">' . __( 'Reply', 'buddypress' ) . '</a>';
</span></span></pre>
</div>
</div>
</body>
</html>