<!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][3880] trunk/bp-forums: Update bbPress standalone installation and update routine to work with newest versions of WordPress and bbPress.</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, #logmsg > ol { margin-left: 0; 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">
<dt>Revision</dt> <dd>3880</dd>
<dt>Author</dt> <dd>johnjamesjacoby</dd>
<dt>Date</dt> <dd>2011-01-24 08:53:59 +0000 (Mon, 24 Jan 2011)</dd>
</dl>
<h3>Log Message</h3>
<pre>Update bbPress standalone installation and update routine to work with newest versions of WordPress and bbPress. Copy of the dbDelta function from WordPress to avoid debug notices and possible function already exists errors.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpforumsbpforumsadminphp">trunk/bp-forums/bp-forums-admin.php</a></li>
<li><a href="#trunkbpforumsbpforumsbbpresssaphp">trunk/bp-forums/bp-forums-bbpress-sa.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpforumsbpforumsadminphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-forums/bp-forums-admin.php (3879 => 3880)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-forums/bp-forums-admin.php        2011-01-24 08:30:23 UTC (rev 3879)
+++ trunk/bp-forums/bp-forums-admin.php        2011-01-24 08:53:59 UTC (rev 3880)
</span><span class="lines">@@ -29,8 +29,9 @@
</span><span class="cx">                 <?php
</span><span class="cx">
</span><span class="cx">                 if ( isset( $_REQUEST['reinstall'] ) || !bp_forums_is_installed_correctly() ) :
</span><del>-                        update_site_option( 'bb-config-location', false );
</del><ins>+
</ins><span class="cx">                         bp_forums_bbpress_install_wizard();
</span><ins>+
</ins><span class="cx">                 else : ?>
</span><span class="cx">
</span><span class="cx">                         <p><?php printf( __( 'bbPress forum integration in BuddyPress has been set up correctly. If you are having problems you can <a href="%s" title="Reinstall bbPress">re-install</a>', 'buddypress' ), site_url( 'wp-admin/admin.php?page=bb-forums-setup&reinstall=1' ) ); ?>
</span><span class="lines">@@ -135,11 +136,11 @@
</span><span class="cx">                 $_REQUEST['bbconfigloc'] .= 'bb-config.php';
</span><span class="cx">         }
</span><span class="cx">
</span><ins>+        update_site_option( 'bb-config-location', $_REQUEST['bbconfigloc'] );
+
</ins><span class="cx">         if ( !file_exists( $_REQUEST['bbconfigloc'] ) )
</span><span class="cx">                 return false;
</span><span class="cx">
</span><del>-        update_site_option( 'bb-config-location', $_REQUEST['bbconfigloc'] );
-
</del><span class="cx">         return true;
</span><span class="cx"> }
</span><span class="cx">
</span></span></pre></div>
<a id="trunkbpforumsbpforumsbbpresssaphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-forums/bp-forums-bbpress-sa.php (3879 => 3880)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-forums/bp-forums-bbpress-sa.php        2011-01-24 08:30:23 UTC (rev 3879)
+++ trunk/bp-forums/bp-forums-bbpress-sa.php        2011-01-24 08:53:59 UTC (rev 3880)
</span><span class="lines">@@ -2,9 +2,9 @@
</span><span class="cx"> function bp_forums_load_bbpress() {
</span><span class="cx">         global $bp, $wpdb, $wp_roles, $current_user, $wp_users_object;
</span><span class="cx">         global $bb, $bbdb, $bb_table_prefix, $bb_current_user;
</span><del>-        global $bb_roles, $wp_taxonomy_object;
</del><ins>+        global $bb_roles, $wp_taxonomy_object, $bb_queries;
</ins><span class="cx">
</span><del>-        /* Return if we've already run this function. */
</del><ins>+        // Return if we've already run this function.
</ins><span class="cx">         if ( is_object( $bbdb ) )
</span><span class="cx">                 return;
</span><span class="cx">
</span><span class="lines">@@ -33,20 +33,22 @@
</span><span class="cx">         require_once( BACKPRESS_PATH . 'class.wp-taxonomy.php' );
</span><span class="cx">         require_once( BB_PATH . BB_INC . 'class.bb-taxonomy.php' );
</span><span class="cx">
</span><ins>+        require_once( BB_PATH . 'bb-admin/includes/functions.bb-admin.php' );
+
</ins><span class="cx">         $bb = new stdClass();
</span><span class="cx">         require_once( $bp->forums->bbconfig );
</span><span class="cx">
</span><span class="cx">         // Setup the global database connection
</span><span class="cx">         $bbdb = new BPDB ( BBDB_USER, BBDB_PASSWORD, BBDB_NAME, BBDB_HOST );
</span><span class="cx">
</span><del>-        /* Set the table names */
-        $bbdb->forums = $bb_table_prefix . 'forums';
-        $bbdb->meta = $bb_table_prefix . 'meta';
-        $bbdb->posts = $bb_table_prefix . 'posts';
-        $bbdb->terms = $bb_table_prefix . 'terms';
</del><ins>+        // Set the table names
+        $bbdb->forums = $bb_table_prefix . 'forums';
+        $bbdb->meta = $bb_table_prefix . 'meta';
+        $bbdb->posts = $bb_table_prefix . 'posts';
+        $bbdb->terms = $bb_table_prefix . 'terms';
</ins><span class="cx">         $bbdb->term_relationships = $bb_table_prefix . 'term_relationships';
</span><del>-        $bbdb->term_taxonomy = $bb_table_prefix . 'term_taxonomy';
-        $bbdb->topics = $bb_table_prefix . 'topics';
</del><ins>+        $bbdb->term_taxonomy = $bb_table_prefix . 'term_taxonomy';
+        $bbdb->topics = $bb_table_prefix . 'topics';
</ins><span class="cx">
</span><span class="cx">         if ( isset( $bb->custom_user_table ) )
</span><span class="cx">                 $bbdb->users = $bb->custom_user_table;
</span><span class="lines">@@ -62,20 +64,14 @@
</span><span class="cx">
</span><span class="cx">         define( 'BB_INSTALLING', false );
</span><span class="cx">
</span><del>-        /* This must be loaded before functionss.bb-admin.php otherwise we get a function conflict. */
-        if ( !$tables_installed = (boolean) $bbdb->get_results( 'DESCRIBE `' . $bbdb->forums . '`;', ARRAY_A ) )
-                require_once( ABSPATH . 'wp-admin/includes/update.php' );
-
-        require_once( BB_PATH . 'bb-admin/includes/functions.bb-admin.php' );
-
</del><span class="cx">         if ( is_object( $wp_roles ) ) {
</span><span class="cx">                 $bb_roles = $wp_roles;
</span><span class="cx">                 bb_init_roles( $bb_roles );
</span><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         do_action( 'bb_got_roles' );
</span><del>-        do_action( 'bb_init' );
-        do_action( 'init_roles' );
</del><ins>+        do_action( 'bb_init' );
+        do_action( 'init_roles' );
</ins><span class="cx">
</span><span class="cx">         $bb_current_user = $current_user;
</span><span class="cx">         $wp_users_object = new BP_Forums_BB_Auth;
</span><span class="lines">@@ -89,17 +85,17 @@
</span><span class="cx">         if ( !isset( $bb->site_id ) )
</span><span class="cx">                 $bb->site_id = BP_ROOT_BLOG;
</span><span class="cx">
</span><del>-        /* Check if the tables are installed, if not, install them */
-        if ( !$tables_installed ) {
</del><ins>+        // Check if the tables are installed, if not, install them
+        if ( !$tables_installed = (boolean) $bbdb->get_results( 'DESCRIBE `' . $bbdb->forums . '`;', ARRAY_A ) ) {
</ins><span class="cx">                 require_once( BB_PATH . 'bb-admin/includes/defaults.bb-schema.php' );
</span><span class="cx">
</span><del>-                /* Backticks and "IF NOT EXISTS" break the dbDelta function. */
-                dbDelta( str_replace( ' IF NOT EXISTS', '', str_replace( '`', '', $bb_queries ) ) );
</del><ins>+                // Backticks and "IF NOT EXISTS" break the dbDelta function.
+                bp_bb_dbDelta( str_replace( ' IF NOT EXISTS', '', str_replace( '`', '', $bb_queries ) ) );
</ins><span class="cx">
</span><del>-                require_once( BB_PATH . 'bb-admin/includes/functions.bb-update.php' );
</del><ins>+                require_once( BB_PATH . 'bb-admin/includes/functions.bb-upgrade.php' );
</ins><span class="cx">                 bb_update_db_version();
</span><span class="cx">
</span><del>-                /* Set the site admins as the keymasters */
</del><ins>+                // Set the site admins as the keymasters
</ins><span class="cx">                 $site_admins = get_site_option( 'site_admins', array('admin') );
</span><span class="cx">                 foreach ( (array)$site_admins as $site_admin )
</span><span class="cx">                         update_user_meta( bp_core_get_userid( $site_admin ), $bb_table_prefix . 'capabilities', array( 'keymaster' => true ) );
</span><span class="lines">@@ -224,4 +220,209 @@
</span><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx"> }
</span><ins>+
+/**
+ * Copied from wp-admin/includes/upgrade.php, this will take care of creating
+ * the bbPress stand-alone tables without loading a conflicting WP Admin.
+ *
+ * @param unknown_type $queries
+ * @param unknown_type $execute
+ * @return unknown
+ */
+function bp_bb_dbDelta($queries, $execute = true) {
+        global $wpdb;
+
+        // Separate individual queries into an array
+        if ( !is_array($queries) ) {
+                $queries = explode( ';', $queries );
+                if ('' == $queries[count($queries) - 1]) array_pop($queries);
+        }
+
+        $cqueries = array(); // Creation Queries
+        $iqueries = array(); // Insertion Queries
+        $for_update = array();
+
+        // Create a tablename index for an array ($cqueries) of queries
+        foreach($queries as $qry) {
+                if (preg_match("|CREATE TABLE ([^ ]*)|", $qry, $matches)) {
+                        $cqueries[trim( strtolower($matches[1]), '`' )] = $qry;
+                        $for_update[$matches[1]] = 'Created table '.$matches[1];
+                } else if (preg_match("|CREATE DATABASE ([^ ]*)|", $qry, $matches)) {
+                        array_unshift($cqueries, $qry);
+                } else if (preg_match("|INSERT INTO ([^ ]*)|", $qry, $matches)) {
+                        $iqueries[] = $qry;
+                } else if (preg_match("|UPDATE ([^ ]*)|", $qry, $matches)) {
+                        $iqueries[] = $qry;
+                } else {
+                        // Unrecognized query type
+                }
+        }
+
+        // Check to see which tables and fields exist
+        if ($tables = $wpdb->get_col('SHOW TABLES;')) {
+                // For every table in the database
+                foreach ($tables as $table) {
+                        // Upgrade global tables only for the main site. Don't upgrade at all if DO_NOT_UPGRADE_GLOBAL_TABLES is defined.
+                        if ( in_array($table, $wpdb->tables('global')) && ( !is_main_site() || defined('DO_NOT_UPGRADE_GLOBAL_TABLES') ) )
+                                continue;
+
+                        // If a table query exists for the database table...
+                        if ( array_key_exists(strtolower($table), $cqueries) ) {
+                                // Clear the field and index arrays
+                                $cfields = $indices = array();
+                                // Get all of the field names in the query from between the parens
+                                preg_match("|\((.*)\)|ms", $cqueries[strtolower($table)], $match2);
+                                $qryline = trim($match2[1]);
+
+                                // Separate field lines into an array
+                                $flds = explode("\n", $qryline);
+
+                                //echo "<hr/><pre>\n".print_r(strtolower($table), true).":\n".print_r($cqueries, true)."</pre><hr/>";
+
+                                // For every field line specified in the query
+                                foreach ($flds as $fld) {
+                                        // Extract the field name
+                                        preg_match("|^([^ ]*)|", trim($fld), $fvals);
+                                        $fieldname = trim( $fvals[1], '`' );
+
+                                        // Verify the found field name
+                                        $validfield = true;
+                                        switch (strtolower($fieldname)) {
+                                        case '':
+                                        case 'primary':
+                                        case 'index':
+                                        case 'fulltext':
+                                        case 'unique':
+                                        case 'key':
+                                                $validfield = false;
+                                                $indices[] = trim(trim($fld), ", \n");
+                                                break;
+                                        }
+                                        $fld = trim($fld);
+
+                                        // If it's a valid field, add it to the field array
+                                        if ($validfield) {
+                                                $cfields[strtolower($fieldname)] = trim($fld, ", \n");
+                                        }
+                                }
+
+                                // Fetch the table column structure from the database
+                                $tablefields = $wpdb->get_results("DESCRIBE {$table};");
+
+                                // For every field in the table
+                                foreach ($tablefields as $tablefield) {
+                                        // If the table field exists in the field array...
+                                        if (array_key_exists(strtolower($tablefield->Field), $cfields)) {
+                                                // Get the field type from the query
+                                                preg_match("|".$tablefield->Field." ([^ ]*( unsigned)?)|i", $cfields[strtolower($tablefield->Field)], $matches);
+                                                $fieldtype = $matches[1];
+
+                                                // Is actual field type different from the field type in query?
+                                                if ($tablefield->Type != $fieldtype) {
+                                                        // Add a query to change the column type
+                                                        $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)];
+                                                        $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
+                                                }
+
+                                                // Get the default value from the array
+                                                        //echo "{$cfields[strtolower($tablefield->Field)]}<br>";
+                                                if (preg_match("| DEFAULT '(.*)'|i", $cfields[strtolower($tablefield->Field)], $matches)) {
+                                                        $default_value = $matches[1];
+                                                        if ($tablefield->Default != $default_value) {
+                                                                // Add a query to change the column's default value
+                                                                $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN {$tablefield->Field} SET DEFAULT '{$default_value}'";
+                                                                $for_update[$table.'.'.$tablefield->Field] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}";
+                                                        }
+                                                }
+
+                                                // Remove the field from the array (so it's not added)
+                                                unset($cfields[strtolower($tablefield->Field)]);
+                                        } else {
+                                                // This field exists in the table, but not in the creation queries?
+                                        }
+                                }
+
+                                // For every remaining field specified for the table
+                                foreach ($cfields as $fieldname => $fielddef) {
+                                        // Push a query line into $cqueries that adds the field to that table
+                                        $cqueries[] = "ALTER TABLE {$table} ADD COLUMN $fielddef";
+                                        $for_update[$table.'.'.$fieldname] = 'Added column '.$table.'.'.$fieldname;
+                                }
+
+                                // Index stuff goes here
+                                // Fetch the table index structure from the database
+                                $tableindices = $wpdb->get_results("SHOW INDEX FROM {$table};");
+
+                                if ($tableindices) {
+                                        // Clear the index array
+                                        unset($index_ary);
+
+                                        // For every index in the table
+                                        foreach ($tableindices as $tableindex) {
+                                                // Add the index to the index data array
+                                                $keyname = $tableindex->Key_name;
+                                                $index_ary[$keyname]['columns'][] = array('fieldname' => $tableindex->Column_name, 'subpart' => $tableindex->Sub_part);
+                                                $index_ary[$keyname]['unique'] = ($tableindex->Non_unique == 0)?true:false;
+                                        }
+
+                                        // For each actual index in the index array
+                                        foreach ($index_ary as $index_name => $index_data) {
+                                                // Build a create string to compare to the query
+                                                $index_string = '';
+                                                if ($index_name == 'PRIMARY') {
+                                                        $index_string .= 'PRIMARY ';
+                                                } else if($index_data['unique']) {
+                                                        $index_string .= 'UNIQUE ';
+                                                }
+                                                $index_string .= 'KEY ';
+                                                if ($index_name != 'PRIMARY') {
+                                                        $index_string .= $index_name;
+                                                }
+                                                $index_columns = '';
+                                                // For each column in the index
+                                                foreach ($index_data['columns'] as $column_data) {
+                                                        if ($index_columns != '') $index_columns .= ',';
+                                                        // Add the field to the column list string
+                                                        $index_columns .= $column_data['fieldname'];
+                                                        if ($column_data['subpart'] != '') {
+                                                                $index_columns .= '('.$column_data['subpart'].')';
+                                                        }
+                                                }
+                                                // Add the column list to the index create string
+                                                $index_string .= ' ('.$index_columns.')';
+                                                if (!(($aindex = array_search($index_string, $indices)) === false)) {
+                                                        unset($indices[$aindex]);
+                                                        //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br />Found index:".$index_string."</pre>\n";
+                                                }
+                                                //else echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br /><b>Did not find index:</b>".$index_string."<br />".print_r($indices, true)."</pre>\n";
+                                        }
+                                }
+
+                                // For every remaining index specified for the table
+                                foreach ( (array) $indices as $index ) {
+                                        // Push a query line into $cqueries that adds the index to that table
+                                        $cqueries[] = "ALTER TABLE {$table} ADD $index";
+                                        $for_update[$table.'.'.$fieldname] = 'Added index '.$table.' '.$index;
+                                }
+
+                                // Remove the original table creation query from processing
+                                unset($cqueries[strtolower($table)]);
+                                unset($for_update[strtolower($table)]);
+                        } else {
+                                // This table exists in the database, but not in the creation queries?
+                        }
+                }
+        }
+
+        $allqueries = array_merge($cqueries, $iqueries);
+        if ($execute) {
+                foreach ($allqueries as $query) {
+                        //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">".print_r($query, true)."</pre>\n";
+                        $wpdb->query($query);
+                }
+        }
+
+        return $for_update;
+}
+
</ins><span class="cx"> ?>
</span><span class="cx">\ No newline at end of file
</span></span></pre>
</div>
</div>
</body>
</html>