Introduce wpdb::tables() to fetch table names on a global or blog scope. Remove very old and long deprecated $table{table} globals, fixes #11614. See #12083

git-svn-id: http://svn.automattic.com/wordpress/trunk@13229 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
nacin 2010-02-19 19:57:03 +00:00
parent 3b98150c82
commit 23918aa60d
4 changed files with 164 additions and 125 deletions

View File

@ -31,40 +31,33 @@ if ( !defined('WP_ALLOW_REPAIR') ) {
$okay = true; $okay = true;
$tables = array_merge( $wpdb->tables, is_multisite() ? $wpdb->global_tables : array( 'users', 'usermeta' ) ); $tables = $wpdb->tables( 'all', true );
$prefix = $wpdb->prefix;
if ( is_multisite() && ! defined('MULTISITE') ) // _1 to get MU-era main blog
$prefix .= '_1';
// Loop over the WP tables, checking and repairing as needed. // Loop over the WP tables, checking and repairing as needed.
foreach ( $tables as $table ) { foreach ( $tables as $table ) {
if ( in_array( $table, $wpdb->old_tables ) ) $check = $wpdb->get_row("CHECK TABLE $table");
continue;
$check = $wpdb->get_row("CHECK TABLE {$prefix}$table");
if ( 'OK' == $check->Msg_text ) { if ( 'OK' == $check->Msg_text ) {
echo "<p>The {$prefix}$table table is okay."; echo "<p>The $table table is okay.";
} else { } else {
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;"; 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 {$prefix}$table"); $repair = $wpdb->get_row("REPAIR TABLE $table");
if ( 'OK' == $check->Msg_text ) { if ( 'OK' == $check->Msg_text ) {
echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;Successfully repaired the {$prefix}$table table."; echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;Successfully repaired the $table table.";
} else { } else {
echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;Failed to repair the {prefix}$table table. Error: $check->Msg_text<br />"; echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;Failed to repair the $table table. Error: $check->Msg_text<br />";
$problems["{$prefix}$table"] = $check->Msg_text; $problems["$table"] = $check->Msg_text;
$okay = false; $okay = false;
} }
} }
if ( $okay && $optimize ) { if ( $okay && $optimize ) {
$check = $wpdb->get_row("ANALYZE TABLE {$prefix}$table"); $check = $wpdb->get_row("ANALYZE TABLE $table");
if ( 'Table is already up to date' == $check->Msg_text ) { if ( 'Table is already up to date' == $check->Msg_text ) {
echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;The {$prefix}$table table is already optimized."; echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;The $table table is already optimized.";
} else { } else {
$check = $wpdb->get_row("OPTIMIZE TABLE {$prefix}$table"); $check = $wpdb->get_row("OPTIMIZE TABLE $table");
if ( 'OK' == $check->Msg_text || 'Table is already up to date' == $check->Msg_text ) if ( 'OK' == $check->Msg_text || 'Table is already up to date' == $check->Msg_text )
echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;Successfully optimized the {$prefix}$table table."; echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;Successfully optimized the $table table.";
else else
echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;Failed to optimize the {$prefix}$table table. Error: $check->Msg_text"; echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;Failed to optimize the $table table. Error: $check->Msg_text";
} }
} }
echo '</p>'; echo '</p>';

View File

@ -1,79 +1,13 @@
<?php <?php
/** /**
* Deprecated functions from past WordPress versions. You shouldn't use these * Deprecated functions from past WordPress versions. You shouldn't use these
* globals and functions and look for the alternatives instead. The functions * functions and look for the alternatives instead. The functions will be
* and globals will be removed in a later version. * removed in a later version.
* *
* @package WordPress * @package WordPress
* @subpackage Deprecated * @subpackage Deprecated
*/ */
/*
* 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;
/* /*
* Deprecated functions come here to die. * Deprecated functions come here to die.
*/ */

View File

@ -1827,12 +1827,13 @@ function is_blog_installed() {
$tables = $wpdb->get_col('SHOW TABLES'); $tables = $wpdb->get_col('SHOW TABLES');
$wpdb->suppress_errors( $suppress ); $wpdb->suppress_errors( $suppress );
$wp_tables = $wpdb->tables( 'all', true );
// Loop over the WP tables. If none exist, then scratch install is allowed. // Loop over the WP tables. If none exist, then scratch install is allowed.
// If one or more exist, suggest table repair since we got here because the options // If one or more exist, suggest table repair since we got here because the options
// table could not be accessed. // table could not be accessed.
foreach ($wpdb->tables as $table) { foreach ( $wp_tables as $table ) {
// If one of the WP tables exist, then we are in an insane state. // If one of the WP tables exist, then we are in an insane state.
if ( in_array($wpdb->prefix . $table, $tables) ) { if ( in_array( $table, $tables ) ) {
// If visiting repair.php, return true and let it take over. // If visiting repair.php, return true and let it take over.
if ( defined('WP_REPAIRING') ) if ( defined('WP_REPAIRING') )
return true; return true;

View File

@ -137,12 +137,6 @@ class wpdb {
var $ready = false; var $ready = false;
var $blogid = 0; var $blogid = 0;
var $siteid = 0; var $siteid = 0;
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');
/** /**
* WordPress Posts table * WordPress Posts table
@ -162,24 +156,6 @@ class wpdb {
*/ */
var $users; var $users;
/**
* 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;
/** /**
* WordPress Comments table * WordPress Comments table
* *
@ -262,27 +238,112 @@ class wpdb {
var $term_relationships; var $term_relationships;
/** /**
* List of WordPress tables * List of WordPress per-blog tables
* *
* @since {@internal Version Unknown}} * @since {@internal Version Unknown}}
* @access private * @access private
* @see wpdb::tables()
* @var array * @var array
*/ */
var $tables = array('posts', 'categories', 'post2cat', 'comments', 'links', 'link2cat', 'options', var $tables = array( 'posts', 'comments', 'links', 'options', 'postmeta',
'postmeta', 'terms', 'term_taxonomy', 'term_relationships', 'commentmeta'); 'terms', 'term_taxonomy', 'term_relationships', 'commentmeta' );
/** /**
* List of deprecated WordPress tables * List of deprecated WordPress tables
* *
* @since 2.9.0 * @since 2.9.0
* @access private * @access private
* @see wpdb::tables()
* @var array * @var array
*/ */
var $old_tables = array('categories', 'post2cat', 'link2cat'); var $old_tables = array( 'categories', 'post2cat', 'link2cat' );
/** /**
* Format specifiers for DB columns. Columns not listed here default to %s. Initialized in wp-settings.php. * Multisite Blogs table
*
* @since 3.0.0
* @access public
* @var string
*/
var $blogs;
/**
* Multisite Signups table
*
* @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.
* *
* Keys are colmn names, values are format types: 'ID' => '%d' * Keys are colmn names, values are format types: 'ID' => '%d'
* *
@ -290,6 +351,7 @@ class wpdb {
* @see wpdb:prepare() * @see wpdb:prepare()
* @see wpdb:insert() * @see wpdb:insert()
* @see wpdb:update() * @see wpdb:update()
* @see wp_set_wpdb_vars()
* @access public * @access public
* @var array * @var array
*/ */
@ -439,7 +501,7 @@ class wpdb {
* @param string $prefix Alphanumeric name for the new prefix. * @param string $prefix Alphanumeric name for the new prefix.
* @return string|WP_Error Old prefix or WP_Error on error * @return string|WP_Error Old prefix or WP_Error on error
*/ */
function set_prefix($prefix) { function set_prefix( $prefix ) {
if ( preg_match('|[^a-z0-9_]|i', $prefix) ) if ( preg_match('|[^a-z0-9_]|i', $prefix) )
return new WP_Error('invalid_db_prefix', /*WP_I18N_DB_BAD_PREFIX*/'Invalid database prefix'/*/WP_I18N_DB_BAD_PREFIX*/); return new WP_Error('invalid_db_prefix', /*WP_I18N_DB_BAD_PREFIX*/'Invalid database prefix'/*/WP_I18N_DB_BAD_PREFIX*/);
@ -452,15 +514,18 @@ class wpdb {
if ( isset( $this->base_prefix ) ) if ( isset( $this->base_prefix ) )
$old_prefix = $this->base_prefix; $old_prefix = $this->base_prefix;
$this->base_prefix = $prefix; $this->base_prefix = $prefix;
foreach ( $this->global_tables as $table ) foreach ( $this->tables( 'global' ) as $table )
$this->$table = $prefix . $table; $this->$table = $prefix . $table;
if ( defined('VHOST') && empty($this->blogid) ) if ( defined('VHOST') && empty( $this->blogid ) )
return $old_prefix; return $old_prefix;
$this->prefix = $this->get_blog_prefix( $this->blogid ); $this->prefix = $this->get_blog_prefix( $this->blogid );
foreach ( (array) $this->tables as $table ) foreach ( (array) $this->tables( 'blog' ) as $table )
$this->$table = $this->prefix . $table;
foreach ( (array) $this->tables( 'old' ) as $table )
$this->$table = $this->prefix . $table; $this->$table = $this->prefix . $table;
if ( defined('CUSTOM_USER_TABLE') ) if ( defined('CUSTOM_USER_TABLE') )
@ -481,7 +546,10 @@ class wpdb {
$this->prefix = $this->get_blog_prefix( $this->blogid ); $this->prefix = $this->get_blog_prefix( $this->blogid );
foreach ( $this->tables as $table ) foreach ( $this->tables( 'blog' ) as $table )
$this->$table = $this->prefix . $table;
foreach ( $this->tables( 'old' ) as $table )
$this->$table = $this->prefix . $table; $this->$table = $this->prefix . $table;
return $old_blog_id; return $old_blog_id;
@ -498,6 +566,49 @@ class wpdb {
} }
} }
/**
* 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;
}
/** /**
* Selects a database using the current database connection. * Selects a database using the current database connection.
* *