<!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>[13324] trunk: Improvements and fixes to wpdb::tables().</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/13324">13324</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2010-02-23 09:06:50 +0000 (Tue, 23 Feb 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>Improvements and fixes to wpdb::tables(). See <a href="http://trac.wordpress.org/ticket/12083">#12083</a> see <a href="http://trac.wordpress.org/ticket/11644">#11644</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminmaintrepairphp">trunk/wp-admin/maint/repair.php</a></li>
<li><a href="#trunkwpincludeswpdbphp">trunk/wp-includes/wp-db.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadminmaintrepairphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/maint/repair.php (13323 => 13324)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/maint/repair.php        2010-02-23 07:10:47 UTC (rev 13323)
+++ trunk/wp-admin/maint/repair.php        2010-02-23 09:06:50 UTC (rev 13324)
</span><span class="lines">@@ -31,7 +31,7 @@
</span><span class="cx"> 
</span><span class="cx">         $okay = true;
</span><span class="cx"> 
</span><del>-        $tables = $wpdb-&gt;tables( 'all', true );
</del><ins>+        $tables = $wpdb-&gt;tables( 'all' );
</ins><span class="cx">         // Loop over the WP tables, checking and repairing as needed.
</span><span class="cx">         foreach ( $tables as $table ) {
</span><span class="cx">                 $check = $wpdb-&gt;get_row(&quot;CHECK TABLE $table&quot;);
</span></span></pre></div>
<a id="trunkwpincludeswpdbphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/wp-db.php (13323 => 13324)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/wp-db.php        2010-02-23 07:10:47 UTC (rev 13323)
+++ trunk/wp-includes/wp-db.php        2010-02-23 09:06:50 UTC (rev 13324)
</span><span class="lines">@@ -95,7 +95,7 @@
</span><span class="cx">          * @since 1.2
</span><span class="cx">          * @var int
</span><span class="cx">          */
</span><del>-        var $num_rows = 0; 
</del><ins>+        var $num_rows = 0;
</ins><span class="cx"> 
</span><span class="cx">         /**
</span><span class="cx">          * Count of affected rows by previous query
</span><span class="lines">@@ -363,29 +363,29 @@
</span><span class="cx">         var $blog_versions;
</span><span class="cx"> 
</span><span class="cx">         /**
</span><del>-         * List of Multisite global tables
</del><ins>+         * List of WordPress global tables
</ins><span class="cx">          *
</span><span class="cx">          * @since 3.0.0
</span><span class="cx">          * @access private
</span><span class="cx">          * @see wpdb::tables()
</span><span class="cx">          * @var array
</span><span class="cx">          */
</span><del>-        var $ms_tables = array( 'blogs', 'signups', 'site', 'sitemeta',
-                'sitecategories', 'registration_log', 'blog_versions' );
</del><ins>+        var $global_tables = array( 'users', 'usermeta' );
</ins><span class="cx"> 
</span><span class="cx">         /**
</span><del>-         * List of WordPress global tables
</del><ins>+         * List of Multisite global tables
</ins><span class="cx">          *
</span><span class="cx">          * @since 3.0.0
</span><span class="cx">          * @access private
</span><span class="cx">          * @see wpdb::tables()
</span><span class="cx">          * @var array
</span><span class="cx">          */
</span><del>-        var $global_tables = array( 'users', 'usermeta' );
</del><ins>+        var $ms_global_tables = array( 'blogs', 'signups', 'site', 'sitemeta',
+                'sitecategories', 'registration_log', 'blog_versions' );
</ins><span class="cx"> 
</span><span class="cx">         /**
</span><span class="cx">          * Format specifiers for DB columns. Columns not listed here default to %s. Initialized during WP load.
</span><del>-         * 
</del><ins>+         *
</ins><span class="cx">          * Keys are column names, values are format types: 'ID' =&gt; '%d'
</span><span class="cx">          *
</span><span class="cx">          * @since 2.8.0
</span><span class="lines">@@ -533,9 +533,6 @@
</span><span class="cx">         /**
</span><span class="cx">          * Sets the table prefix for the WordPress tables.
</span><span class="cx">          *
</span><del>-         * Also allows for the CUSTOM_USER_TABLE and CUSTOM_USER_META_TABLE to
-         * override the WordPress users and usersmeta tables that would otherwise be determined by the $prefix.
-         *
</del><span class="cx">          * @since 2.5.0
</span><span class="cx">          *
</span><span class="cx">          * @param string $prefix Alphanumeric name for the new prefix.
</span><span class="lines">@@ -551,26 +548,20 @@
</span><span class="cx">                 if ( isset( $this-&gt;base_prefix ) )
</span><span class="cx">                         $old_prefix = $this-&gt;base_prefix;
</span><span class="cx">                 $this-&gt;base_prefix = $prefix;
</span><del>-                foreach ( $this-&gt;tables( 'global' ) as $table )
-                        $this-&gt;$table = $prefix . $table;
</del><ins>+                foreach ( $this-&gt;tables( 'global' ) as $table =&gt; $prefixed_table )
+                        $this-&gt;$table = $prefixed_table;
</ins><span class="cx"> 
</span><span class="cx">                 if ( defined( 'VHOST' ) &amp;&amp; empty( $this-&gt;blogid ) )
</span><span class="cx">                         return $old_prefix;
</span><span class="cx"> 
</span><span class="cx">                 $this-&gt;prefix = $this-&gt;get_blog_prefix( $this-&gt;blogid );
</span><span class="cx"> 
</span><del>-                foreach ( (array) $this-&gt;tables( 'blog' ) as $table )
-                        $this-&gt;$table = $this-&gt;prefix . $table;
</del><ins>+                foreach ( (array) $this-&gt;tables( 'blog' ) as $table =&gt; $prefixed_table )
+                        $this-&gt;$table = $prefixed_table;
</ins><span class="cx"> 
</span><del>-                foreach ( (array) $this-&gt;tables( 'old' ) as $table )
-                        $this-&gt;$table = $this-&gt;prefix . $table;
</del><ins>+                foreach ( (array) $this-&gt;tables( 'old' ) as $table =&gt; $prefixed_table )
+                        $this-&gt;$table = $prefixed_table;
</ins><span class="cx"> 
</span><del>-                if ( defined( 'CUSTOM_USER_TABLE' ) )
-                        $this-&gt;users = CUSTOM_USER_TABLE;
-
-                if ( defined( 'CUSTOM_USER_META_TABLE' ) )
-                        $this-&gt;usermeta = CUSTOM_USER_META_TABLE;
-
</del><span class="cx">                 return $old_prefix;
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -582,7 +573,7 @@
</span><span class="cx">          * @param string $blog_id
</span><span class="cx">          * @param string $site_id. Optional.
</span><span class="cx">          * @return string previous blog id
</span><del>-         */        
</del><ins>+         */
</ins><span class="cx">         function set_blog_id( $blog_id, $site_id = '' ) {
</span><span class="cx">                 if ( ! empty( $site_id ) )
</span><span class="cx">                         $this-&gt;siteid = $site_id;
</span><span class="lines">@@ -592,11 +583,11 @@
</span><span class="cx"> 
</span><span class="cx">                 $this-&gt;prefix = $this-&gt;get_blog_prefix( $this-&gt;blogid );
</span><span class="cx"> 
</span><del>-                foreach ( $this-&gt;tables( 'blog' ) as $table )
-                        $this-&gt;$table = $this-&gt;prefix . $table;
</del><ins>+                foreach ( $this-&gt;tables( 'blog' ) as $table =&gt; $prefixed_table )
+                        $this-&gt;$table = $prefixed_table;
</ins><span class="cx"> 
</span><del>-                foreach ( $this-&gt;tables( 'old' ) as $table )
-                        $this-&gt;$table = $this-&gt;prefix . $table;
</del><ins>+                foreach ( $this-&gt;tables( 'old' ) as $table =&gt; $prefixed_table )
+                        $this-&gt;$table = $prefixed_table;
</ins><span class="cx"> 
</span><span class="cx">                 return $old_blog_id;
</span><span class="cx">         }
</span><span class="lines">@@ -623,19 +614,26 @@
</span><span class="cx">         /**
</span><span class="cx">          * Returns an array of WordPress tables.
</span><span class="cx">          *
</span><ins>+         * Also allows for the CUSTOM_USER_TABLE and CUSTOM_USER_META_TABLE to
+         * override the WordPress users and usersmeta tables that would otherwise
+         * be determined by the prefix.
+         *
</ins><span class="cx">          * @since 3.0.0
</span><span class="cx">          * @uses wpdb::tables
</span><span class="cx">          * @uses wpdb::old_tables
</span><span class="cx">          * @uses wpdb::global_tables
</span><ins>+         * @uses wpdb::ms_global_tables
</ins><span class="cx">          * @uses is_multisite()
</span><span class="cx">          *
</span><span class="cx">          * @param string $scope Can be all, global, blog, or old tables. Default all.
</span><del>-         *         All returns all global tables and the blog tables for the queried blog.
-         * @param bool $prefix Whether to include the blog prefix. Default false.
-         * @param int $blog_id The blog_id to prefix. Defaults to main blog.
-         * @return array Table names.
</del><ins>+         *         All returns the blog tables for the queried blog and all global tables.
+         * @param bool $prefix Whether to include table prefixes. Default false. If blog
+         *        prefix is requested, then the custom users and usermeta tables will be mapped.
+         * @param int $blog_id The blog_id to prefix. Defaults to main blog. Used only when prefix is requested.
+         * @return array Table names. When a prefix is requested, the key is the
+         *        unprefixed table name.
</ins><span class="cx">          */
</span><del>-        function tables( $scope = 'all', $prefix = false, $blog_id = 0 ) {
</del><ins>+        function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) {
</ins><span class="cx">                 switch ( $scope ) {
</span><span class="cx">                         case 'old' :
</span><span class="cx">                                 $tables = $this-&gt;old_tables;
</span><span class="lines">@@ -644,20 +642,34 @@
</span><span class="cx">                                 $tables = $this-&gt;tables;
</span><span class="cx">                                 break;
</span><span class="cx">                         case 'global' :
</span><del>-                                $tables = array_merge( $this-&gt;global_tables, $this-&gt;ms_tables );
</del><ins>+                                $tables = $this-&gt;global_tables;
+                                if ( is_multisite() )
+                                        $tables = array_merge( $tables, $this-&gt;ms_global_tables );
</ins><span class="cx">                                 break;
</span><span class="cx">                         case 'all' :
</span><span class="cx">                                 $tables = array_merge( $this-&gt;global_tables, $this-&gt;tables );
</span><span class="cx">                                 if ( is_multisite() )
</span><del>-                                        $tables = array_merge( $tables, $this-&gt;ms_tables );
</del><ins>+                                        $tables = array_merge( $tables, $this-&gt;ms_global_tables );
</ins><span class="cx">                                 break;
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 if ( $prefix ) {
</span><span class="cx">                         $prefix = $this-&gt;get_blog_prefix( $blog_id );
</span><ins>+                        $base_prefix = $this-&gt;base_prefix;
+                        $global_tables = array_merge( $this-&gt;global_tables, $this-&gt;ms_global_tables );
</ins><span class="cx">                         foreach ( $tables as $k =&gt; $table ) {
</span><del>-                                $tables[$k] = $prefix . $table;
</del><ins>+                                if ( in_array( $table, $global_tables ) )
+                                        $tables[ $table ] = $base_prefix . $table;
+                                else
+                                        $tables[ $table ] = $prefix . $table;
+                                unset( $tables[ $k ] );
</ins><span class="cx">                         }
</span><ins>+
+                        if ( isset( $tables['users'] ) &amp;&amp; defined( 'CUSTOM_USER_TABLE' ) )
+                                $tables['users'] = CUSTOM_USER_TABLE;
+
+                        if ( isset( $tables['usermeta'] ) &amp;&amp; defined( 'CUSTOM_USER_META_TABLE' ) )
+                                $tables['usermeta'] = CUSTOM_USER_META_TABLE;
</ins><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 return $tables;
</span><span class="lines">@@ -785,12 +797,12 @@
</span><span class="cx">         /**
</span><span class="cx">          * Prepares a SQL query for safe execution.  Uses sprintf()-like syntax.
</span><span class="cx">          *
</span><del>-         * The following directives can be used in the query format string: 
-         *   %d (decimal number)  
</del><ins>+         * The following directives can be used in the query format string:
+         *   %d (decimal number)
</ins><span class="cx">          *   %s (string)
</span><del>-         *   %% (literal percentage sign - no argument needed) 
</del><ins>+         *   %% (literal percentage sign - no argument needed)
</ins><span class="cx">          *
</span><del>-         * Both %d and %s are to be left unquoted in the query string and   
</del><ins>+         * Both %d and %s are to be left unquoted in the query string and
</ins><span class="cx">          * they need an argument passed for them. Literals (%) as parts of
</span><span class="cx">          * the query must be properly written as %%.
</span><span class="cx">          *
</span></span></pre>
</div>
</div>

</body>
</html>