<!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 = &quot;SELECT a.*, u.user_email, u.user_nicename, u.user_login&quot;;
+
+                if ( function_exists( 'xprofile_install' ) )
+                        $select_sql .= &quot;, pd.value as user_fullname&quot;;
+
+                $from_sql = &quot; FROM {$bp-&gt;activity-&gt;table_name} a, {$wpdb-&gt;users} u&quot;;
+
+                if ( function_exists( 'xprofile_install' ) )
+                        $from_sql .= &quot;, {$bp-&gt;profile-&gt;table_name_data} pd&quot;;
+
+                /* Where conditions */
</ins><span class="cx">                 $where_conditions = array();
</span><ins>+                $where_conditions['user_join'] = &quot;a.user_id = u.ID&quot;;
</ins><span class="cx"> 
</span><ins>+                if ( function_exists( 'xprofile_install' ) ) {
+                        $where_conditions['xprofile_join'] = &quot;a.user_id = pd.user_id&quot;;
+                        $where_conditions['xprofile_filter'] = &quot;pd.field_id = 1&quot;;
+                }
+
</ins><span class="cx">                 if ( $per_page &amp;&amp; $page )
</span><span class="cx">                         $pag_sql = $wpdb-&gt;prepare( &quot;LIMIT %d, %d&quot;, 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-&gt;escape( $search_terms );
</span><del>-                        $where_conditions['search_sql'] = &quot;content LIKE '%%&quot; . like_escape( $search_terms ) . &quot;%%'&quot;;
</del><ins>+                        $where_conditions['search_sql'] = &quot;a.content LIKE '%%&quot; . like_escape( $search_terms ) . &quot;%%'&quot;;
</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'] = &quot;hide_sitewide = 0&quot;;
</del><ins>+                        $where_conditions['hidden_sql'] = &quot;a.hide_sitewide = 0&quot;;
</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[] = &quot;component_action != 'activity_comment'&quot;;
</del><ins>+                        $where_conditions[] = &quot;a.component_action != 'activity_comment'&quot;;
</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 &amp;&amp; $page &amp;&amp; $max )
</span><del>-                        $activities = $wpdb-&gt;get_results( $wpdb-&gt;prepare( &quot;SELECT * FROM {$bp-&gt;activity-&gt;table_name} {$where_sql} ORDER BY date_recorded {$sort} {$pag_sql}&quot; ) );
</del><ins>+                        $activities = $wpdb-&gt;get_results( $wpdb-&gt;prepare( &quot;{$select_sql} {$from_sql} {$where_sql} ORDER BY a.date_recorded {$sort} {$pag_sql}&quot; ) );
</ins><span class="cx">                 else
</span><del>-                        $activities = $wpdb-&gt;get_results( $wpdb-&gt;prepare( &quot;SELECT * FROM {$bp-&gt;activity-&gt;table_name} {$where_sql} ORDER BY date_recorded {$sort} {$pag_sql} {$max_sql}&quot; ) );
</del><ins>+                        $activities = $wpdb-&gt;get_results( $wpdb-&gt;prepare( &quot;{$select_sql} {$from_sql} {$where_sql} ORDER BY a.date_recorded {$sort} {$pag_sql} {$max_sql}&quot; ) );
</ins><span class="cx"> 
</span><del>-                $total_activities = $wpdb-&gt;get_var( $wpdb-&gt;prepare( &quot;SELECT count(id) FROM {$bp-&gt;activity-&gt;table_name} {$where_sql} ORDER BY date_recorded {$sort} {$max_sql}&quot; ) );
</del><ins>+                $total_activities = $wpdb-&gt;get_var( $wpdb-&gt;prepare( &quot;SELECT count(a.id) {$from_sql} {$where_sql} ORDER BY a.date_recorded {$sort} {$max_sql}&quot; ) );
</ins><span class="cx"> 
</span><span class="cx">                 if ( $activities &amp;&amp; $display_comments )
</span><span class="cx">                         $activities = BP_Activity_Activity::append_comments( &amp;$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-&gt;get_results( $wpdb-&gt;prepare( &quot;SELECT * FROM {$bp-&gt;activity-&gt;table_name} WHERE component_action = 'activity_comment' AND item_id = %d AND mptt_left BETWEEN %d AND %d ORDER BY date_recorded ASC&quot;, $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 = &quot;, pd.value as user_fullname&quot;;
+                        $fullname_from = &quot;, {$bp-&gt;profile-&gt;table_name_data} pd &quot;;
+                        $fullname_where = &quot;AND pd.user_id = a.user_id AND pd.field_id = 1&quot;;
+                }
+
+                $descendants = $wpdb-&gt;get_results( $wpdb-&gt;prepare( &quot;SELECT a.*, u.user_email, u.user_nicename, u.user_login{$fullname_select} FROM {$bp-&gt;activity-&gt;table_name} a, {$wpdb-&gt;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&quot;, $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-&gt;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-&gt;prepare( &quot;user_id = %d&quot;, trim( $user ) );
</del><ins>+                                $user_sql .= $wpdb-&gt;prepare( &quot;a.user_id = %d&quot;, 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-&gt;prepare( &quot;component_name = %s&quot;, trim( $object ) );
</del><ins>+                                $object_sql .= $wpdb-&gt;prepare( &quot;a.component_name = %s&quot;, 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-&gt;prepare( &quot;component_action = %s&quot;, trim( $action ) );
</del><ins>+                                $action_sql .= $wpdb-&gt;prepare( &quot;a.component_action = %s&quot;, 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-&gt;prepare( &quot;item_id = %s&quot;, trim( $pid ) );
</del><ins>+                                $pid_sql .= $wpdb-&gt;prepare( &quot;a.item_id = %s&quot;, 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-&gt;prepare( &quot;secondary_item_id = %s&quot;, trim( $sid ) );
</del><ins>+                                $sid_sql .= $wpdb-&gt;prepare( &quot;a.secondary_item_id = %s&quot;, 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' =&gt; 20,
</span><span class="cx">                         'height' =&gt; 20,
</span><span class="cx">                         'class' =&gt; 'avatar',
</span><del>-                        'alt' =&gt; __( 'Avatar', 'buddypress' )
</del><ins>+                        'alt' =&gt; __( 'Avatar', 'buddypress' ),
+                        'email' =&gt; 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-&gt;activity-&gt;component_name, $object );
</span><span class="cx"> 
</span><del>-                return apply_filters( 'bp_get_activity_avatar', bp_core_fetch_avatar( array( 'item_id' =&gt; $item_id, 'object' =&gt; $object, 'type' =&gt; $type, 'alt' =&gt; $alt, 'class' =&gt; $class, 'width' =&gt; $width, 'height' =&gt; $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 &amp;&amp; empty($email) )
+                        $email = $activities_template-&gt;activity-&gt;user_email;
+
+                return apply_filters( 'bp_get_activity_avatar', bp_core_fetch_avatar( array( 'item_id' =&gt; $item_id, 'object' =&gt; $object, 'type' =&gt; $type, 'alt' =&gt; $alt, 'class' =&gt; $class, 'width' =&gt; $width, 'height' =&gt; $height, 'email' =&gt; $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 .= '&lt;ul&gt;';
</span><span class="cx">                         foreach ( $comment-&gt;children as $comment ) {
</span><ins>+                                if ( !$comment-&gt;user_fullname )
+                                        $comment-&gt;user_fullname = $comment-&gt;user_nicename;
+
</ins><span class="cx">                                 $content .= '&lt;li id=&quot;acomment-' . $comment-&gt;id . '&quot;&gt;';
</span><ins>+                                $content .= '&lt;div class=&quot;acomment-avatar&quot;&gt;' . bp_core_fetch_avatar( array( 'item_id' =&gt; $comment-&gt;user_id, 'width' =&gt; 25, 'height' =&gt; 25, 'email' =&gt; $comment-&gt;user_email ) ) . '&lt;/div&gt;';
+                                $content .= '&lt;div class=&quot;acomment-meta&quot;&gt;&lt;a href=&quot;' . bp_core_get_user_domain( $comment-&gt;user_id, $comment-&gt;user_nicename, $comment-&gt;user_login ) . '&quot;&gt;' . esc_attr( $comment-&gt;user_fullname ) . '&lt;/a&gt; &amp;middot; ' . sprintf( __( '%s ago', 'buddypress' ), bp_core_time_since( strtotime( $comment-&gt;date_recorded ) ) );
</ins><span class="cx"> 
</span><del>-                                $content .= '&lt;div class=&quot;acomment-avatar&quot;&gt;' . bp_core_fetch_avatar( array( 'item_id' =&gt; $comment-&gt;user_id, 'width' =&gt; 25, 'height' =&gt; 25 ) ) . '&lt;/div&gt;';
-
-                                $content .= '&lt;div class=&quot;acomment-meta&quot;&gt;' . bp_core_get_userlink( $comment-&gt;user_id ) . ' &amp;middot; ' . sprintf( __( '%s ago', 'buddypress' ), bp_core_time_since( strtotime( $comment-&gt;date_recorded ) ) );
-
</del><span class="cx">                                 /* Reply link */
</span><span class="cx">                                 if ( is_user_logged_in() )
</span><span class="cx">                                         $content .= ' &amp;middot; &lt;a href=&quot;#acomment-' . $comment-&gt;id . '&quot; class=&quot;acomment-reply&quot; id=&quot;acomment-reply-' . $activities_template-&gt;activity-&gt;id . '&quot;&gt;' . __( 'Reply', 'buddypress' ) . '&lt;/a&gt;';
</span></span></pre>
</div>
</div>

</body>
</html>