<!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" />
<title>[BuddyPress][10815] trunk: Blogs: Improve how `bp_blogs_record_existing_blogs()` operates.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { 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 #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#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>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://buddypress.trac.wordpress.org/changeset/10815">10815</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://buddypress.trac.wordpress.org/changeset/10815","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>r-a-y</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-05-27 05:21:20 +0000 (Fri, 27 May 2016)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="http://buddypress.trac.wordpress.org/ticket/6940">#6940</a>.)
- 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 <a href="http://buddypress.trac.wordpress.org/ticket/6370">#6370</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpblogsbpblogsfunctionsphp">trunk/src/bp-blogs/bp-blogs-functions.php</a></li>
<li><a href="#trunksrcbpcoreadminbpcoreadmintoolsphp">trunk/src/bp-core/admin/bp-core-admin-tools.php</a></li>
<li><a href="#trunktestsphpunittestcasesblogsfunctionsphp">trunk/tests/phpunit/testcases/blogs/functions.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpblogsbpblogsfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-blogs/bp-blogs-functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-blogs/bp-blogs-functions.php 2016-05-26 19:56:30 UTC (rev 10814)
+++ trunk/src/bp-blogs/bp-blogs-functions.php 2016-05-27 05:21:20 UTC (rev 10815)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -72,68 +72,108 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Populate the BP blogs table with existing blogs.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Warning: By default, this will remove all existing records from the BP
+ * blogs and blogmeta tables before re-populating the tables.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @since 1.0.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 2.6.0 Accepts $args as a parameter.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @global object $wpdb WordPress database object.
- * @uses get_users()
- * @uses bp_blogs_record_blog()
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param array $args {
+ * Array of arguments.
+ * @type int $offset The offset to use.
+ * @type int $limit The number of blogs to record at one time.
+ * @type array $blog_ids Blog IDs to record. If empty, all blogs will be recorded.
+ * @type array $site_id The network site ID to use.
+ * }
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @return bool
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function bp_blogs_record_existing_blogs() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function bp_blogs_record_existing_blogs( $args = array() ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> global $wpdb;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Query for all sites in network.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $r = bp_parse_args( $args, array(
+ 'offset' => false === bp_get_option( '_bp_record_blogs_offset' ) ? 0 : bp_get_option( '_bp_record_blogs_offset' ),
+ 'limit' => 50,
+ 'blog_ids' => array(),
+ 'site_id' => $wpdb->siteid
+ ), 'record_existing_blogs' );
+
+ // Truncate all BP blogs tables if starting fresh
+ if ( empty( $r['offset'] ) && empty( $r['blog_ids'] ) ) {
+ $bp = buddypress();
+
+ // Truncate user blogs table
+ $truncate = $wpdb->query( "TRUNCATE {$bp->blogs->table_name}" );
+ if ( is_wp_error( $truncate ) ) {
+ return false;
+ }
+
+ // Truncate user blogmeta table
+ $truncate = $wpdb->query( "TRUNCATE {$bp->blogs->table_name_blogmeta}" );
+ if ( is_wp_error( $truncate ) ) {
+ return false;
+ }
+ }
+
+ // Multisite
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( is_multisite() ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $sql = array();
+ $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'] );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Get blog ID's if not a large network.
- if ( ! wp_is_large_network() ) {
- $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 ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Omit root blog if large network
+ if ( wp_is_large_network( 'users' ) ) {
+ $sql['omit_root_blog'] = $wpdb->prepare( "AND blog_id != %d", bp_get_root_blog_id() );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // If error running this query, set blog ID's to false.
- if ( is_wp_error( $blog_ids ) ) {
- $blog_ids = false;
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Filter by selected blog IDs
+ if ( ! empty( $r['blog_ids'] ) ) {
+ $in = implode( ',', wp_parse_id_list( $r['blog_ids'] ) );
+ $sql['in'] = "AND blog_id IN ({$in})";
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Large networks are not currently supported.
- } else {
- $blog_ids = false;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $sql['orderby'] = 'ORDER BY blog_id ASC';
+
+ $sql['limit'] = $wpdb->prepare( "LIMIT %d", $r['limit'] );
+
+ if ( ! empty( $r['offset'] ) ) {
+ $sql['offset'] = $wpdb->prepare( "OFFSET %d", $r['offset'] );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $blogs = $wpdb->get_results( implode( ' ', $sql ) );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Record a single site.
</span><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $blog_ids = $wpdb->blogid;
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Just record blog for the current user only.
+ $record = bp_blogs_record_blog( $wpdb->blogid, get_current_user_id(), true );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Bail if there are no blogs in the network.
- if ( empty( $blog_ids ) ) {
- return false;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( false === $record ) {
+ return false;
+ } else {
+ return true;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Get BuddyPress.
- $bp = buddypress();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Bail if there are no blogs
+ if ( empty( $blogs ) ) {
+ // Make sure we remove our offset marker
+ if ( is_multisite() ) {
+ bp_delete_option( '_bp_record_blogs_offset' );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Truncate user blogs table.
- $truncate = $wpdb->query( "TRUNCATE {$bp->blogs->table_name}" );
- if ( is_wp_error( $truncate ) ) {
</del><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Truncate user blogsmeta table.
- $truncate = $wpdb->query( "TRUNCATE {$bp->blogs->table_name_blogmeta}" );
- if ( is_wp_error( $truncate ) ) {
- return false;
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> // Loop through users of blogs and record the relationship.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- foreach ( (array) $blog_ids as $blog_id ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ foreach ( (array) $blogs as $blog ) {
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Ensure that the cache is clear after the table TRUNCATE above.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp_cache_delete( $blog_id, 'blog_meta' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_cache_delete( $blog->blog_id, 'blog_meta' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Get all users.
</span><span class="cx" style="display: block; padding: 0 10px"> $users = get_users( array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'blog_id' => $blog_id
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'blog_id' => $blog->blog_id,
+ 'fields' => 'ID'
</ins><span class="cx" style="display: block; padding: 0 10px"> ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Continue on if no users exist for this site (how did this happen?).
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -142,11 +182,45 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Loop through users and record their relationship to this blog.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- foreach ( (array) $users as $user ) {
- bp_blogs_add_user_to_blog( $user->ID, false, $blog_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ foreach ( (array) $users as $user_id ) {
+ bp_blogs_add_user_to_blog( $user_id, false, $blog->blog_id );
+
+ // Clear cache
+ bp_blogs_clear_blog_object_cache( $blog->blog_id, $user_id );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ // Update blog last activity timestamp
+ if ( ! empty( $blog->last_updated ) && false !== strtotime( $blog->last_updated ) ) {
+ bp_blogs_update_blogmeta( $blog->blog_id, 'last_activity', $blog->last_updated );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // See if we need to do this again
+ if ( is_multisite() && empty( $r['blog_ids'] ) ) {
+ $sql['offset'] = $wpdb->prepare( " OFFSET %d", $r['limit'] + $r['offset'] );
+
+ // Check if there are more blogs to record
+ $blog_ids = $wpdb->get_results( implode( ' ', $sql ) );
+
+ // We have more blogs; record offset and re-run function
+ if ( ! empty( $blog_ids ) ) {
+ bp_update_option( '_bp_record_blogs_offset', $r['limit'] + $r['offset'] );
+ bp_blogs_record_existing_blogs( array(
+ 'offset' => $r['limit'] + $r['offset'],
+ 'limit' => $r['limit'],
+ 'blog_ids' => $r['blog_ids'],
+ 'site_id' => $r['site_id']
+ ) );
+
+ // Bail since we have more blogs to record.
+ return;
+
+ // No more blogs; delete offset marker
+ } else {
+ bp_delete_option( '_bp_record_blogs_offset' );
+ }
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Fires after the BP blogs tables have been populated with existing blogs.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span></span></pre></div>
<a id="trunksrcbpcoreadminbpcoreadmintoolsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-core/admin/bp-core-admin-tools.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-core/admin/bp-core-admin-tools.php 2016-05-26 19:56:30 UTC (rev 10814)
+++ trunk/src/bp-core/admin/bp-core-admin-tools.php 2016-05-27 05:21:20 UTC (rev 10815)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -473,3 +473,24 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return array( 0, __( 'Emails have been successfully reinstalled.', 'buddypress' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Add notice on the "Tools > BuddyPress" page if more sites need recording.
+ *
+ * This notice only shows up in the network admin dashboard.
+ *
+ * @since 2.6.0
+ */
+function bp_core_admin_notice_repopulate_blogs_resume() {
+ $screen = get_current_screen();
+ if ( 'tools_page_bp-tools-network' !== $screen->id ) {
+ return;
+ }
+
+ if ( '' === bp_get_option( '_bp_record_blogs_offset' ) ) {
+ return;
+ }
+
+ 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>';
+}
+add_action( 'network_admin_notices', 'bp_core_admin_notice_repopulate_blogs_resume' );
</ins></span></pre></div>
<a id="trunktestsphpunittestcasesblogsfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/testcases/blogs/functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/testcases/blogs/functions.php 2016-05-26 19:56:30 UTC (rev 10814)
+++ trunk/tests/phpunit/testcases/blogs/functions.php 2016-05-27 05:21:20 UTC (rev 10815)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -984,6 +984,38 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->comment_saved_count += 1;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * @group bp_blogs_record_existing_blogs
+ */
+ public function test_bp_blogs_record_existing_blogs_limit() {
+ if ( ! is_multisite() ) {
+ return;
+ }
+
+ $old_user = get_current_user_id();
+
+ $u = $this->factory->user->create();
+ $this->set_current_user( $u );
+
+ // Create three sites.
+ $this->factory->blog->create_many( 3, array(
+ 'user_id' => $u
+ ) );
+
+ // Record each site one at a time
+ bp_blogs_record_existing_blogs( array(
+ 'limit' => 1
+ ) );
+
+ // Assert!
+ $blogs = bp_blogs_get_blogs( array(
+ 'user_id' => $u
+ ) );
+ $this->assertSame( 3, (int) $blogs['total'] );
+
+ $this->set_current_user( $old_user );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> protected function activity_exists_for_post( $post_id ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $a = bp_activity_get( array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'component' => buddypress()->blogs->id,
</span></span></pre>
</div>
</div>
</body>
</html>