<!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>[13229] trunk: Introduce wpdb::tables()
to fetch table names on a global or blog scope.</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/13229">13229</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2010-02-19 19:57:03 +0000 (Fri, 19 Feb 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>Introduce wpdb::tables() to fetch table names on a global or blog scope. Remove very old and long deprecated $table{table} globals, fixes <a href="http://trac.wordpress.org/ticket/11614">#11614</a>. See <a href="http://trac.wordpress.org/ticket/12083">#12083</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminmaintrepairphp">trunk/wp-admin/maint/repair.php</a></li>
<li><a href="#trunkwpincludesdeprecatedphp">trunk/wp-includes/deprecated.php</a></li>
<li><a href="#trunkwpincludesfunctionsphp">trunk/wp-includes/functions.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 (13228 => 13229)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/maint/repair.php        2010-02-19 18:01:50 UTC (rev 13228)
+++ trunk/wp-admin/maint/repair.php        2010-02-19 19:57:03 UTC (rev 13229)
</span><span class="lines">@@ -31,40 +31,33 @@
</span><span class="cx">
</span><span class="cx">         $okay = true;
</span><span class="cx">
</span><del>-        $tables = array_merge( $wpdb->tables, is_multisite() ? $wpdb->global_tables : array( 'users', 'usermeta' ) );
-        $prefix = $wpdb->prefix;
-        if ( is_multisite() && ! defined('MULTISITE') ) // _1 to get MU-era main blog
-                $prefix .= '_1';
-
</del><ins>+        $tables = $wpdb->tables( 'all', true );
</ins><span class="cx">         // Loop over the WP tables, checking and repairing as needed.
</span><span class="cx">         foreach ( $tables as $table ) {
</span><del>-                if ( in_array( $table, $wpdb->old_tables ) )
-                        continue;
-
-                $check = $wpdb->get_row("CHECK TABLE {$prefix}$table");
</del><ins>+                $check = $wpdb->get_row("CHECK TABLE $table");
</ins><span class="cx">                 if ( 'OK' == $check->Msg_text ) {
</span><del>-                        echo "<p>The {$prefix}$table table is okay.";
</del><ins>+                        echo "<p>The $table table is okay.";
</ins><span class="cx">                 } else {
</span><del>-                        echo "<p>The {$prefix}$table table is not okay. It is reporting the following error: <code>$check->Msg_text</code>. WordPress will attempt to repair this table&hellip;";
-                        $repair = $wpdb->get_row("REPAIR TABLE {$prefix}$table");
</del><ins>+                        echo "<p>The $table table is not okay. It is reporting the following error: <code>$check->Msg_text</code>. WordPress will attempt to repair this table&hellip;";
+                        $repair = $wpdb->get_row("REPAIR TABLE $table");
</ins><span class="cx">                         if ( 'OK' == $check->Msg_text ) {
</span><del>-                                echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;Successfully repaired the {$prefix}$table table.";
</del><ins>+                                echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;Successfully repaired the $table table.";
</ins><span class="cx">                         } else {
</span><del>-                                echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;Failed to repair the {prefix}$table table. Error: $check->Msg_text<br />";
-                                $problems["{$prefix}$table"] = $check->Msg_text;
</del><ins>+                                echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;Failed to repair the $table table. Error: $check->Msg_text<br />";
+                                $problems["$table"] = $check->Msg_text;
</ins><span class="cx">                                 $okay = false;
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="cx">                 if ( $okay && $optimize ) {
</span><del>-                        $check = $wpdb->get_row("ANALYZE TABLE {$prefix}$table");
</del><ins>+                        $check = $wpdb->get_row("ANALYZE TABLE $table");
</ins><span class="cx">                         if ( 'Table is already up to date' == $check->Msg_text ) {
</span><del>-                                echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;The {$prefix}$table table is already optimized.";
</del><ins>+                                echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;The $table table is already optimized.";
</ins><span class="cx">                         } else {
</span><del>-                                $check = $wpdb->get_row("OPTIMIZE TABLE {$prefix}$table");
</del><ins>+                                $check = $wpdb->get_row("OPTIMIZE TABLE $table");
</ins><span class="cx">                                 if ( 'OK' == $check->Msg_text || 'Table is already up to date' == $check->Msg_text )
</span><del>-                                        echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;Successfully optimized the {$prefix}$table table.";
</del><ins>+                                        echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;Successfully optimized the $table table.";
</ins><span class="cx">                                 else
</span><del>-                                        echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;Failed to optimize the {$prefix}$table table. Error: $check->Msg_text";
</del><ins>+                                        echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;Failed to optimize the $table table. Error: $check->Msg_text";
</ins><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="cx">                 echo '</p>';
</span></span></pre></div>
<a id="trunkwpincludesdeprecatedphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/deprecated.php (13228 => 13229)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/deprecated.php        2010-02-19 18:01:50 UTC (rev 13228)
+++ trunk/wp-includes/deprecated.php        2010-02-19 19:57:03 UTC (rev 13229)
</span><span class="lines">@@ -1,80 +1,14 @@
</span><span class="cx"> <?php
</span><span class="cx"> /**
</span><span class="cx"> * Deprecated functions from past WordPress versions. You shouldn't use these
</span><del>- * globals and functions and look for the alternatives instead. The functions
- * and globals will be removed in a later version.
</del><ins>+ * functions and look for the alternatives instead. The functions will be
+ * removed in a later version.
</ins><span class="cx"> *
</span><span class="cx"> * @package WordPress
</span><span class="cx"> * @subpackage Deprecated
</span><span class="cx"> */
</span><span class="cx">
</span><span class="cx"> /*
</span><del>- * Deprecated global variables.
- */
-
-/**
- * The name of the Posts table
- * @global string $tableposts
- * @deprecated Use $wpdb->posts
- */
-$tableposts = $wpdb->posts;
-
-/**
- * The name of the Users table
- * @global string $tableusers
- * @deprecated Use $wpdb->users
- */
-$tableusers = $wpdb->users;
-
-/**
- * The name of the Categories table
- * @global string $tablecategories
- * @deprecated Use $wpdb->categories
- */
-$tablecategories = $wpdb->categories;
-
-/**
- * The name of the post to category table
- * @global string $tablepost2cat
- * @deprecated Use $wpdb->post2cat;
- */
-$tablepost2cat = $wpdb->post2cat;
-
-/**
- * The name of the comments table
- * @global string $tablecomments
- * @deprecated Use $wpdb->comments;
- */
-$tablecomments = $wpdb->comments;
-
-/**
- * The name of the links table
- * @global string $tablelinks
- * @deprecated Use $wpdb->links;
- */
-$tablelinks = $wpdb->links;
-
-/**
- * @global string $tablelinkcategories
- * @deprecated Not used anymore;
- */
-$tablelinkcategories = 'linkcategories_is_gone';
-
-/**
- * The name of the options table
- * @global string $tableoptions
- * @deprecated Use $wpdb->options;
- */
-$tableoptions = $wpdb->options;
-
-/**
- * The name of the postmeta table
- * @global string $tablepostmeta
- * @deprecated Use $wpdb->postmeta;
- */
-$tablepostmeta = $wpdb->postmeta;
-
-/*
</del><span class="cx"> * Deprecated functions come here to die.
</span><span class="cx"> */
</span><span class="cx">
</span></span></pre></div>
<a id="trunkwpincludesfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/functions.php (13228 => 13229)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/functions.php        2010-02-19 18:01:50 UTC (rev 13228)
+++ trunk/wp-includes/functions.php        2010-02-19 19:57:03 UTC (rev 13229)
</span><span class="lines">@@ -1827,12 +1827,13 @@
</span><span class="cx">         $tables = $wpdb->get_col('SHOW TABLES');
</span><span class="cx">         $wpdb->suppress_errors( $suppress );
</span><span class="cx">
</span><ins>+        $wp_tables = $wpdb->tables( 'all', true );
</ins><span class="cx">         // Loop over the WP tables. If none exist, then scratch install is allowed.
</span><span class="cx">         // If one or more exist, suggest table repair since we got here because the options
</span><span class="cx">         // table could not be accessed.
</span><del>-        foreach ($wpdb->tables as $table) {
</del><ins>+        foreach ( $wp_tables as $table ) {
</ins><span class="cx">                 // If one of the WP tables exist, then we are in an insane state.
</span><del>-                if ( in_array($wpdb->prefix . $table, $tables) ) {
</del><ins>+                if ( in_array( $table, $tables ) ) {
</ins><span class="cx">                         // If visiting repair.php, return true and let it take over.
</span><span class="cx">                         if ( defined('WP_REPAIRING') )
</span><span class="cx">                                 return true;
</span></span></pre></div>
<a id="trunkwpincludeswpdbphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/wp-db.php (13228 => 13229)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/wp-db.php        2010-02-19 18:01:50 UTC (rev 13228)
+++ trunk/wp-includes/wp-db.php        2010-02-19 19:57:03 UTC (rev 13229)
</span><span class="lines">@@ -137,12 +137,6 @@
</span><span class="cx">         var $ready = false;
</span><span class="cx">         var $blogid = 0;
</span><span class="cx">         var $siteid = 0;
</span><del>-        var $blogs;
-        var $signups;
-        var $site;
-        var $sitemeta;
-        var $sitecategories;
-        var $global_tables = array('blogs', 'signups', 'site', 'sitemeta', 'users', 'usermeta', 'sitecategories', 'registration_log', 'blog_versions');
</del><span class="cx">
</span><span class="cx">         /**
</span><span class="cx">          * WordPress Posts table
</span><span class="lines">@@ -163,24 +157,6 @@
</span><span class="cx">         var $users;
</span><span class="cx">
</span><span class="cx">         /**
</span><del>-         * WordPress Categories table
-         *
-         * @since 1.5.0
-         * @access public
-         * @var string
-         */
-        var $categories;
-
-        /**
-         * WordPress Post to Category table
-         *
-         * @since 1.5.0
-         * @access public
-         * @var string
-         */
-        var $post2cat;
-
-        /**
</del><span class="cx">          * WordPress Comments table
</span><span class="cx">          *
</span><span class="cx">          * @since 1.5.0
</span><span class="lines">@@ -262,34 +238,120 @@
</span><span class="cx">         var $term_relationships;
</span><span class="cx">
</span><span class="cx">         /**
</span><del>-         * List of WordPress tables
</del><ins>+         * List of WordPress per-blog tables
</ins><span class="cx">          *
</span><span class="cx">          * @since {@internal Version Unknown}}
</span><span class="cx">          * @access private
</span><ins>+         * @see wpdb::tables()
</ins><span class="cx">          * @var array
</span><span class="cx">          */
</span><del>-        var $tables = array('posts', 'categories', 'post2cat', 'comments', 'links', 'link2cat', 'options',
-                        'postmeta', 'terms', 'term_taxonomy', 'term_relationships', 'commentmeta');
</del><ins>+        var $tables = array( 'posts', 'comments', 'links', 'options', 'postmeta',
+                'terms', 'term_taxonomy', 'term_relationships', 'commentmeta' );
</ins><span class="cx">
</span><span class="cx">         /**
</span><span class="cx">          * List of deprecated WordPress tables
</span><span class="cx">          *
</span><span class="cx">          * @since 2.9.0
</span><span class="cx">          * @access private
</span><ins>+         * @see wpdb::tables()
</ins><span class="cx">          * @var array
</span><span class="cx">          */
</span><del>-        var $old_tables = array('categories', 'post2cat', 'link2cat');
</del><ins>+        var $old_tables = array( 'categories', 'post2cat', 'link2cat' );
</ins><span class="cx">
</span><ins>+        /**
+         * Multisite Blogs table
+         *
+         * @since 3.0.0
+         * @access public
+         * @var string
+         */
+        var $blogs;
</ins><span class="cx">
</span><span class="cx">         /**
</span><del>-         * Format specifiers for DB columns. Columns not listed here default to %s. Initialized in wp-settings.php.
</del><ins>+         * Multisite Signups table
</ins><span class="cx">          *
</span><ins>+         * @since 3.0.0
+         * @access public
+         * @var string
+         */
+        var $signups;
+
+        /**
+         * Multisite Sites table
+         *
+         * @since 3.0.0
+         * @access public
+         * @var string
+         */
+        var $site;
+
+        /**
+         * Multisite Site Metadata table
+         *
+         * @since 3.0.0
+         * @access public
+         * @var string
+         */
+        var $sitemeta;
+
+        /**
+         * Multisite Sitewide Terms table
+         *
+         * @since 3.0.0
+         * @access public
+         * @var string
+         */
+        var $sitecategories;
+
+        /**
+         * Multisite Registration Log table
+         *
+         * @since 3.0.0
+         * @access public
+         * @var string
+         */
+        var $registration_log;
+
+        /**
+         * Multisite Blog Versions table
+         *
+         * @since 3.0.0
+         * @access public
+         * @var string
+         */
+        var $blog_versions;
+
+        /**
+         * List of Multisite global tables
+         *
+         * @since 3.0.0
+         * @access private
+         * @see wpdb::tables()
+         * @var array
+         */
+        var $ms_tables = array( 'blogs', 'signups', 'site', 'sitemeta',
+                'sitecategories', 'registration_log', 'blog_versions' );
+
+        /**
+         * List of WordPress global tables
+         *
+         * @since 3.0.0
+         * @access private
+         * @see wpdb::tables()
+         * @var array
+         */
+        var $global_tables = array( 'users', 'usermeta' );
+
+        /**
+         * Format specifiers for DB columns. Columns not listed here default to %s. Initialized in wp-settings.php.
+         *
</ins><span class="cx">          * Keys are colmn names, values are format types: 'ID' => '%d'
</span><span class="cx">          *
</span><span class="cx">          * @since 2.8.0
</span><span class="cx">          * @see wpdb:prepare()
</span><span class="cx">          * @see wpdb:insert()
</span><span class="cx">          * @see wpdb:update()
</span><ins>+         * @see wp_set_wpdb_vars()
</ins><span class="cx">          * @access public
</span><span class="cx">          * @var array
</span><span class="cx">          */
</span><span class="lines">@@ -439,7 +501,7 @@
</span><span class="cx">          * @param string $prefix Alphanumeric name for the new prefix.
</span><span class="cx">          * @return string|WP_Error Old prefix or WP_Error on error
</span><span class="cx">          */
</span><del>-        function set_prefix($prefix) {
</del><ins>+        function set_prefix( $prefix ) {
</ins><span class="cx">
</span><span class="cx">                 if ( preg_match('|[^a-z0-9_]|i', $prefix) )
</span><span class="cx">                         return new WP_Error('invalid_db_prefix', /*WP_I18N_DB_BAD_PREFIX*/'Invalid database prefix'/*/WP_I18N_DB_BAD_PREFIX*/);
</span><span class="lines">@@ -452,17 +514,20 @@
</span><span class="cx">                 if ( isset( $this->base_prefix ) )
</span><span class="cx">                         $old_prefix = $this->base_prefix;
</span><span class="cx">                 $this->base_prefix = $prefix;
</span><del>-                foreach ( $this->global_tables as $table )
</del><ins>+                foreach ( $this->tables( 'global' ) as $table )
</ins><span class="cx">                         $this->$table = $prefix . $table;
</span><span class="cx">
</span><del>-                if ( defined('VHOST') && empty($this->blogid) )
</del><ins>+                if ( defined('VHOST') && empty( $this->blogid ) )
</ins><span class="cx">                         return $old_prefix;
</span><span class="cx">
</span><span class="cx">                 $this->prefix = $this->get_blog_prefix( $this->blogid );
</span><span class="cx">
</span><del>-                foreach ( (array) $this->tables as $table )
</del><ins>+                foreach ( (array) $this->tables( 'blog' ) as $table )
</ins><span class="cx">                         $this->$table = $this->prefix . $table;
</span><span class="cx">
</span><ins>+                foreach ( (array) $this->tables( 'old' ) as $table )
+                        $this->$table = $this->prefix . $table;
+
</ins><span class="cx">                 if ( defined('CUSTOM_USER_TABLE') )
</span><span class="cx">                         $this->users = CUSTOM_USER_TABLE;
</span><span class="cx">
</span><span class="lines">@@ -481,9 +546,12 @@
</span><span class="cx">
</span><span class="cx">                 $this->prefix = $this->get_blog_prefix( $this->blogid );
</span><span class="cx">
</span><del>-                foreach ( $this->tables as $table )
</del><ins>+                foreach ( $this->tables( 'blog' ) as $table )
</ins><span class="cx">                         $this->$table = $this->prefix . $table;
</span><span class="cx">
</span><ins>+                foreach ( $this->tables( 'old' ) as $table )
+                        $this->$table = $this->prefix . $table;
+
</ins><span class="cx">                 return $old_blog_id;
</span><span class="cx">         }
</span><span class="cx">
</span><span class="lines">@@ -499,6 +567,49 @@
</span><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         /**
</span><ins>+         * Returns an array of WordPress tables.
+         *
+         * @since 3.0.0
+         * @uses wpdb::tables
+         * @uses wpdb::old_tables
+         * @uses wpdb::global_tables
+         * @uses is_multisite()
+         *
+         * @param string $scope Can be all, global, blog, or old tables. Default all.
+         *         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.
+         */
+        function tables( $scope = 'all', $prefix = false, $blog_id = 0 ) {
+                switch ( $scope ) {
+                        case 'old' :
+                                $tables = $this->old_tables;
+                                break;
+                        case 'blog' :
+                                $tables = $this->tables;
+                                break;
+                        case 'global' :
+                                $tables = array_merge( $this->global_tables, $this->ms_tables );
+                                break;
+                        case 'all' :
+                                $tables = array_merge( $this->global_tables, $this->tables );
+                                if ( is_multisite() )
+                                        $tables = array_merge( $tables, $this->ms_tables );
+                                break;
+                }
+
+                if ( $prefix ) {
+                        $prefix = $this->get_blog_prefix( $blog_id );
+                        foreach ( $tables as &$table ) {
+                                $table = $prefix . $table;
+                        }
+                }
+
+                return $tables;
+        }
+
+        /**
</ins><span class="cx">          * Selects a database using the current database connection.
</span><span class="cx">          *
</span><span class="cx">          * The database name will be changed based on the current database
</span></span></pre>
</div>
</div>
</body>
</html>