Make WordPress Core

Changeset 13324


Ignore:
Timestamp:
02/23/2010 09:06:50 AM (15 years ago)
Author:
nacin
Message:

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

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/maint/repair.php

    r13229 r13324  
    3232    $okay = true;
    3333
    34     $tables = $wpdb->tables( 'all', true );
     34    $tables = $wpdb->tables( 'all' );
    3535    // Loop over the WP tables, checking and repairing as needed.
    3636    foreach ( $tables as $table ) {
  • trunk/wp-includes/wp-db.php

    r13314 r13324  
    9696     * @var int
    9797     */
    98     var $num_rows = 0; 
     98    var $num_rows = 0;
    9999
    100100    /**
     
    364364
    365365    /**
    366      * List of Multisite global tables
     366     * List of WordPress global tables
    367367     *
    368368     * @since 3.0.0
     
    371371     * @var array
    372372     */
    373     var $ms_tables = array( 'blogs', 'signups', 'site', 'sitemeta',
    374         'sitecategories', 'registration_log', 'blog_versions' );
    375 
    376     /**
    377      * List of WordPress global tables
     373    var $global_tables = array( 'users', 'usermeta' );
     374
     375    /**
     376     * List of Multisite global tables
    378377     *
    379378     * @since 3.0.0
     
    382381     * @var array
    383382     */
    384     var $global_tables = array( 'users', 'usermeta' );
     383    var $ms_global_tables = array( 'blogs', 'signups', 'site', 'sitemeta',
     384        'sitecategories', 'registration_log', 'blog_versions' );
    385385
    386386    /**
    387387     * Format specifiers for DB columns. Columns not listed here default to %s. Initialized during WP load.
    388      * 
     388     *
    389389     * Keys are column names, values are format types: 'ID' => '%d'
    390390     *
     
    534534     * Sets the table prefix for the WordPress tables.
    535535     *
    536      * Also allows for the CUSTOM_USER_TABLE and CUSTOM_USER_META_TABLE to
    537      * override the WordPress users and usersmeta tables that would otherwise be determined by the $prefix.
    538      *
    539536     * @since 2.5.0
    540537     *
     
    552549            $old_prefix = $this->base_prefix;
    553550        $this->base_prefix = $prefix;
    554         foreach ( $this->tables( 'global' ) as $table )
    555             $this->$table = $prefix . $table;
     551        foreach ( $this->tables( 'global' ) as $table => $prefixed_table )
     552            $this->$table = $prefixed_table;
    556553
    557554        if ( defined( 'VHOST' ) && empty( $this->blogid ) )
     
    560557        $this->prefix = $this->get_blog_prefix( $this->blogid );
    561558
    562         foreach ( (array) $this->tables( 'blog' ) as $table )
    563             $this->$table = $this->prefix . $table;
    564 
    565         foreach ( (array) $this->tables( 'old' ) as $table )
    566             $this->$table = $this->prefix . $table;
    567 
    568         if ( defined( 'CUSTOM_USER_TABLE' ) )
    569             $this->users = CUSTOM_USER_TABLE;
    570 
    571         if ( defined( 'CUSTOM_USER_META_TABLE' ) )
    572             $this->usermeta = CUSTOM_USER_META_TABLE;
     559        foreach ( (array) $this->tables( 'blog' ) as $table => $prefixed_table )
     560            $this->$table = $prefixed_table;
     561
     562        foreach ( (array) $this->tables( 'old' ) as $table => $prefixed_table )
     563            $this->$table = $prefixed_table;
    573564
    574565        return $old_prefix;
     
    583574     * @param string $site_id. Optional.
    584575     * @return string previous blog id
    585      */ 
     576     */
    586577    function set_blog_id( $blog_id, $site_id = '' ) {
    587578        if ( ! empty( $site_id ) )
     
    593584        $this->prefix = $this->get_blog_prefix( $this->blogid );
    594585
    595         foreach ( $this->tables( 'blog' ) as $table )
    596             $this->$table = $this->prefix . $table;
    597 
    598         foreach ( $this->tables( 'old' ) as $table )
    599             $this->$table = $this->prefix . $table;
     586        foreach ( $this->tables( 'blog' ) as $table => $prefixed_table )
     587            $this->$table = $prefixed_table;
     588
     589        foreach ( $this->tables( 'old' ) as $table => $prefixed_table )
     590            $this->$table = $prefixed_table;
    600591
    601592        return $old_blog_id;
     
    624615     * Returns an array of WordPress tables.
    625616     *
     617     * Also allows for the CUSTOM_USER_TABLE and CUSTOM_USER_META_TABLE to
     618     * override the WordPress users and usersmeta tables that would otherwise
     619     * be determined by the prefix.
     620     *
    626621     * @since 3.0.0
    627622     * @uses wpdb::tables
    628623     * @uses wpdb::old_tables
    629624     * @uses wpdb::global_tables
     625     * @uses wpdb::ms_global_tables
    630626     * @uses is_multisite()
    631627     *
    632628     * @param string $scope Can be all, global, blog, or old tables. Default all.
    633      *  All returns all global tables and the blog tables for the queried blog.
    634      * @param bool $prefix Whether to include the blog prefix. Default false.
    635      * @param int $blog_id The blog_id to prefix. Defaults to main blog.
    636      * @return array Table names.
    637      */
    638     function tables( $scope = 'all', $prefix = false, $blog_id = 0 ) {
     629     *  All returns the blog tables for the queried blog and all global tables.
     630     * @param bool $prefix Whether to include table prefixes. Default false. If blog
     631     *  prefix is requested, then the custom users and usermeta tables will be mapped.
     632     * @param int $blog_id The blog_id to prefix. Defaults to main blog. Used only when prefix is requested.
     633     * @return array Table names. When a prefix is requested, the key is the
     634     *  unprefixed table name.
     635     */
     636    function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) {
    639637        switch ( $scope ) {
    640638            case 'old' :
     
    645643                break;
    646644            case 'global' :
    647                 $tables = array_merge( $this->global_tables, $this->ms_tables );
     645                $tables = $this->global_tables;
     646                if ( is_multisite() )
     647                    $tables = array_merge( $tables, $this->ms_global_tables );
    648648                break;
    649649            case 'all' :
    650650                $tables = array_merge( $this->global_tables, $this->tables );
    651651                if ( is_multisite() )
    652                     $tables = array_merge( $tables, $this->ms_tables );
     652                    $tables = array_merge( $tables, $this->ms_global_tables );
    653653                break;
    654654        }
     
    656656        if ( $prefix ) {
    657657            $prefix = $this->get_blog_prefix( $blog_id );
     658            $base_prefix = $this->base_prefix;
     659            $global_tables = array_merge( $this->global_tables, $this->ms_global_tables );
    658660            foreach ( $tables as $k => $table ) {
    659                 $tables[$k] = $prefix . $table;
     661                if ( in_array( $table, $global_tables ) )
     662                    $tables[ $table ] = $base_prefix . $table;
     663                else
     664                    $tables[ $table ] = $prefix . $table;
     665                unset( $tables[ $k ] );
    660666            }
     667
     668            if ( isset( $tables['users'] ) && defined( 'CUSTOM_USER_TABLE' ) )
     669                $tables['users'] = CUSTOM_USER_TABLE;
     670
     671            if ( isset( $tables['usermeta'] ) && defined( 'CUSTOM_USER_META_TABLE' ) )
     672                $tables['usermeta'] = CUSTOM_USER_META_TABLE;
    661673        }
    662674
     
    786798     * Prepares a SQL query for safe execution.  Uses sprintf()-like syntax.
    787799     *
    788      * The following directives can be used in the query format string: 
    789      *   %d (decimal number) 
     800     * The following directives can be used in the query format string:
     801     *   %d (decimal number)
    790802     *   %s (string)
    791      *   %% (literal percentage sign - no argument needed) 
    792      *
    793      * Both %d and %s are to be left unquoted in the query string and   
     803     *   %% (literal percentage sign - no argument needed)
     804     *
     805     * Both %d and %s are to be left unquoted in the query string and
    794806     * they need an argument passed for them. Literals (%) as parts of
    795807     * the query must be properly written as %%.
Note: See TracChangeset for help on using the changeset viewer.