Improvements and fixes to wpdb::tables(). See #12083 see #11644

git-svn-id: http://svn.automattic.com/wordpress/trunk@13324 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
nacin 2010-02-23 09:06:50 +00:00
parent 3f35e43429
commit 191e0df9d2
2 changed files with 58 additions and 46 deletions

View File

@ -31,7 +31,7 @@ if ( !defined('WP_ALLOW_REPAIR') ) {
$okay = true; $okay = true;
$tables = $wpdb->tables( 'all', true ); $tables = $wpdb->tables( 'all' );
// 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 ) {
$check = $wpdb->get_row("CHECK TABLE $table"); $check = $wpdb->get_row("CHECK TABLE $table");

View File

@ -362,17 +362,6 @@ class wpdb {
*/ */
var $blog_versions; 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 * List of WordPress global tables
* *
@ -383,6 +372,17 @@ class wpdb {
*/ */
var $global_tables = array( 'users', 'usermeta' ); var $global_tables = array( 'users', 'usermeta' );
/**
* List of Multisite global tables
*
* @since 3.0.0
* @access private
* @see wpdb::tables()
* @var array
*/
var $ms_global_tables = array( 'blogs', 'signups', 'site', 'sitemeta',
'sitecategories', 'registration_log', 'blog_versions' );
/** /**
* Format specifiers for DB columns. Columns not listed here default to %s. Initialized during WP load. * Format specifiers for DB columns. Columns not listed here default to %s. Initialized during WP load.
* *
@ -533,9 +533,6 @@ class wpdb {
/** /**
* Sets the table prefix for the WordPress tables. * Sets the table prefix for the WordPress tables.
* *
* 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.
*
* @since 2.5.0 * @since 2.5.0
* *
* @param string $prefix Alphanumeric name for the new prefix. * @param string $prefix Alphanumeric name for the new prefix.
@ -551,25 +548,19 @@ 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->tables( 'global' ) as $table ) foreach ( $this->tables( 'global' ) as $table => $prefixed_table )
$this->$table = $prefix . $table; $this->$table = $prefixed_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( 'blog' ) as $table ) foreach ( (array) $this->tables( 'blog' ) as $table => $prefixed_table )
$this->$table = $this->prefix . $table; $this->$table = $prefixed_table;
foreach ( (array) $this->tables( 'old' ) as $table ) foreach ( (array) $this->tables( 'old' ) as $table => $prefixed_table )
$this->$table = $this->prefix . $table; $this->$table = $prefixed_table;
if ( defined( 'CUSTOM_USER_TABLE' ) )
$this->users = CUSTOM_USER_TABLE;
if ( defined( 'CUSTOM_USER_META_TABLE' ) )
$this->usermeta = CUSTOM_USER_META_TABLE;
return $old_prefix; return $old_prefix;
} }
@ -592,11 +583,11 @@ class wpdb {
$this->prefix = $this->get_blog_prefix( $this->blogid ); $this->prefix = $this->get_blog_prefix( $this->blogid );
foreach ( $this->tables( 'blog' ) as $table ) foreach ( $this->tables( 'blog' ) as $table => $prefixed_table )
$this->$table = $this->prefix . $table; $this->$table = $prefixed_table;
foreach ( $this->tables( 'old' ) as $table ) foreach ( $this->tables( 'old' ) as $table => $prefixed_table )
$this->$table = $this->prefix . $table; $this->$table = $prefixed_table;
return $old_blog_id; return $old_blog_id;
} }
@ -623,19 +614,26 @@ class wpdb {
/** /**
* Returns an array of WordPress tables. * Returns an array of WordPress tables.
* *
* 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.
*
* @since 3.0.0 * @since 3.0.0
* @uses wpdb::tables * @uses wpdb::tables
* @uses wpdb::old_tables * @uses wpdb::old_tables
* @uses wpdb::global_tables * @uses wpdb::global_tables
* @uses wpdb::ms_global_tables
* @uses is_multisite() * @uses is_multisite()
* *
* @param string $scope Can be all, global, blog, or old tables. Default all. * @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. * All returns the blog tables for the queried blog and all global tables.
* @param bool $prefix Whether to include the blog prefix. Default false. * @param bool $prefix Whether to include table prefixes. Default false. If blog
* @param int $blog_id The blog_id to prefix. Defaults to main blog. * prefix is requested, then the custom users and usermeta tables will be mapped.
* @return array Table names. * @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.
*/ */
function tables( $scope = 'all', $prefix = false, $blog_id = 0 ) { function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) {
switch ( $scope ) { switch ( $scope ) {
case 'old' : case 'old' :
$tables = $this->old_tables; $tables = $this->old_tables;
@ -644,20 +642,34 @@ class wpdb {
$tables = $this->tables; $tables = $this->tables;
break; break;
case 'global' : case 'global' :
$tables = array_merge( $this->global_tables, $this->ms_tables ); $tables = $this->global_tables;
if ( is_multisite() )
$tables = array_merge( $tables, $this->ms_global_tables );
break; break;
case 'all' : case 'all' :
$tables = array_merge( $this->global_tables, $this->tables ); $tables = array_merge( $this->global_tables, $this->tables );
if ( is_multisite() ) if ( is_multisite() )
$tables = array_merge( $tables, $this->ms_tables ); $tables = array_merge( $tables, $this->ms_global_tables );
break; break;
} }
if ( $prefix ) { if ( $prefix ) {
$prefix = $this->get_blog_prefix( $blog_id ); $prefix = $this->get_blog_prefix( $blog_id );
$base_prefix = $this->base_prefix;
$global_tables = array_merge( $this->global_tables, $this->ms_global_tables );
foreach ( $tables as $k => $table ) { foreach ( $tables as $k => $table ) {
$tables[$k] = $prefix . $table; if ( in_array( $table, $global_tables ) )
$tables[ $table ] = $base_prefix . $table;
else
$tables[ $table ] = $prefix . $table;
unset( $tables[ $k ] );
} }
if ( isset( $tables['users'] ) && defined( 'CUSTOM_USER_TABLE' ) )
$tables['users'] = CUSTOM_USER_TABLE;
if ( isset( $tables['usermeta'] ) && defined( 'CUSTOM_USER_META_TABLE' ) )
$tables['usermeta'] = CUSTOM_USER_META_TABLE;
} }
return $tables; return $tables;