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

@ -95,7 +95,7 @@ class wpdb {
* @since 1.2 * @since 1.2
* @var int * @var int
*/ */
var $num_rows = 0; var $num_rows = 0;
/** /**
* Count of affected rows by previous query * Count of affected rows by previous query
@ -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,9 +372,20 @@ 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.
* *
* Keys are column names, values are format types: 'ID' => '%d' * Keys are column names, values are format types: 'ID' => '%d'
* *
* @since 2.8.0 * @since 2.8.0
@ -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;
} }
@ -582,7 +573,7 @@ class wpdb {
* @param string $blog_id * @param string $blog_id
* @param string $site_id. Optional. * @param string $site_id. Optional.
* @return string previous blog id * @return string previous blog id
*/ */
function set_blog_id( $blog_id, $site_id = '' ) { function set_blog_id( $blog_id, $site_id = '' ) {
if ( ! empty( $site_id ) ) if ( ! empty( $site_id ) )
$this->siteid = $site_id; $this->siteid = $site_id;
@ -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;
@ -785,12 +797,12 @@ class wpdb {
/** /**
* Prepares a SQL query for safe execution. Uses sprintf()-like syntax. * Prepares a SQL query for safe execution. Uses sprintf()-like syntax.
* *
* The following directives can be used in the query format string: * The following directives can be used in the query format string:
* %d (decimal number) * %d (decimal number)
* %s (string) * %s (string)
* %% (literal percentage sign - no argument needed) * %% (literal percentage sign - no argument needed)
* *
* Both %d and %s are to be left unquoted in the query string and * Both %d and %s are to be left unquoted in the query string and
* they need an argument passed for them. Literals (%) as parts of * they need an argument passed for them. Literals (%) as parts of
* the query must be properly written as %%. * the query must be properly written as %%.
* *