Make WordPress Core


Ignore:
Timestamp:
02/19/2010 07:57:03 PM (15 years ago)
Author:
nacin
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/wp-db.php

    r13209 r13229  
    138138    var $blogid = 0;
    139139    var $siteid = 0;
    140     var $blogs;
    141     var $signups;
    142     var $site;
    143     var $sitemeta;
    144     var $sitecategories;
    145     var $global_tables = array('blogs', 'signups', 'site', 'sitemeta', 'users', 'usermeta', 'sitecategories', 'registration_log', 'blog_versions');
    146140
    147141    /**
     
    164158
    165159    /**
    166      * WordPress Categories table
     160     * WordPress Comments table
    167161     *
    168162     * @since 1.5.0
     
    170164     * @var string
    171165     */
    172     var $categories;
    173 
    174     /**
    175      * WordPress Post to Category table
     166    var $comments;
     167
     168    /**
     169     * WordPress Links table
    176170     *
    177171     * @since 1.5.0
     
    179173     * @var string
    180174     */
    181     var $post2cat;
    182 
    183     /**
    184      * WordPress Comments table
     175    var $links;
     176
     177    /**
     178     * WordPress Options table
    185179     *
    186180     * @since 1.5.0
     
    188182     * @var string
    189183     */
    190     var $comments;
    191 
    192     /**
    193      * WordPress Links table
    194      *
    195      * @since 1.5.0
    196      * @access public
    197      * @var string
    198      */
    199     var $links;
    200 
    201     /**
    202      * WordPress Options table
    203      *
    204      * @since 1.5.0
    205      * @access public
    206      * @var string
    207      */
    208184    var $options;
    209185
     
    263239
    264240    /**
    265      * List of WordPress tables
     241     * List of WordPress per-blog tables
    266242     *
    267243     * @since {@internal Version Unknown}}
    268244     * @access private
     245     * @see wpdb::tables()
    269246     * @var array
    270247     */
    271     var $tables = array('posts', 'categories', 'post2cat', 'comments', 'links', 'link2cat', 'options',
    272             'postmeta', 'terms', 'term_taxonomy', 'term_relationships', 'commentmeta');
     248    var $tables = array( 'posts', 'comments', 'links', 'options', 'postmeta',
     249        'terms', 'term_taxonomy', 'term_relationships', 'commentmeta' );
    273250
    274251    /**
     
    277254     * @since 2.9.0
    278255     * @access private
     256     * @see wpdb::tables()
    279257     * @var array
    280258     */
    281     var $old_tables = array('categories', 'post2cat', 'link2cat');
    282 
    283 
    284     /**
    285      * Format specifiers for DB columns. Columns not listed here default to %s.  Initialized in wp-settings.php.
     259    var $old_tables = array( 'categories', 'post2cat', 'link2cat' );
     260
     261    /**
     262     * Multisite Blogs table
     263     *
     264     * @since 3.0.0
     265     * @access public
     266     * @var string
     267     */
     268    var $blogs;
     269
     270    /**
     271     * Multisite Signups table
     272     *
     273     * @since 3.0.0
     274     * @access public
     275     * @var string
     276     */
     277    var $signups;
     278
     279    /**
     280     * Multisite Sites table
     281     *
     282     * @since 3.0.0
     283     * @access public
     284     * @var string
     285     */
     286    var $site;
     287
     288    /**
     289     * Multisite Site Metadata table
     290     *
     291     * @since 3.0.0
     292     * @access public
     293     * @var string
     294     */
     295    var $sitemeta;
     296
     297    /**
     298     * Multisite Sitewide Terms table
     299     *
     300     * @since 3.0.0
     301     * @access public
     302     * @var string
     303     */
     304    var $sitecategories;
     305
     306    /**
     307     * Multisite Registration Log table
     308     *
     309     * @since 3.0.0
     310     * @access public
     311     * @var string
     312     */
     313    var $registration_log;
     314
     315    /**
     316     * Multisite Blog Versions table
     317     *
     318     * @since 3.0.0
     319     * @access public
     320     * @var string
     321     */
     322    var $blog_versions;
     323
     324    /**
     325     * List of Multisite global tables
     326     *
     327     * @since 3.0.0
     328     * @access private
     329     * @see wpdb::tables()
     330     * @var array
     331     */
     332    var $ms_tables = array( 'blogs', 'signups', 'site', 'sitemeta',
     333        'sitecategories', 'registration_log', 'blog_versions' );
     334
     335    /**
     336     * List of WordPress global tables
     337     *
     338     * @since 3.0.0
     339     * @access private
     340     * @see wpdb::tables()
     341     * @var array
     342     */
     343    var $global_tables = array( 'users', 'usermeta' );
     344
     345    /**
     346     * Format specifiers for DB columns. Columns not listed here default to %s. Initialized in wp-settings.php.
    286347     *
    287348     * Keys are colmn names, values are format types: 'ID' => '%d'
     
    291352     * @see wpdb:insert()
    292353     * @see wpdb:update()
     354     * @see wp_set_wpdb_vars()
    293355     * @access public
    294356     * @var array
     
    440502     * @return string|WP_Error Old prefix or WP_Error on error
    441503     */
    442     function set_prefix($prefix) {
     504    function set_prefix( $prefix ) {
    443505
    444506        if ( preg_match('|[^a-z0-9_]|i', $prefix) )
     
    453515            $old_prefix = $this->base_prefix;
    454516        $this->base_prefix = $prefix;
    455         foreach ( $this->global_tables as $table )
     517        foreach ( $this->tables( 'global' ) as $table )
    456518            $this->$table = $prefix . $table;
    457519
    458         if ( defined('VHOST') && empty($this->blogid) )
     520        if ( defined('VHOST') && empty( $this->blogid ) )
    459521            return $old_prefix;
    460522
    461523        $this->prefix = $this->get_blog_prefix( $this->blogid );
    462524
    463         foreach ( (array) $this->tables as $table )
     525        foreach ( (array) $this->tables( 'blog' ) as $table )
     526            $this->$table = $this->prefix . $table;
     527
     528        foreach ( (array) $this->tables( 'old' ) as $table )
    464529            $this->$table = $this->prefix . $table;
    465530
     
    482547        $this->prefix = $this->get_blog_prefix( $this->blogid );
    483548
    484         foreach ( $this->tables as $table )
     549        foreach ( $this->tables( 'blog' ) as $table )
     550            $this->$table = $this->prefix . $table;
     551
     552        foreach ( $this->tables( 'old' ) as $table )
    485553            $this->$table = $this->prefix . $table;
    486554
     
    497565            return $this->base_prefix;
    498566        }
     567    }
     568
     569    /**
     570     * Returns an array of WordPress tables.
     571     *
     572     * @since 3.0.0
     573     * @uses wpdb::tables
     574     * @uses wpdb::old_tables
     575     * @uses wpdb::global_tables
     576     * @uses is_multisite()
     577     *
     578     * @param string $scope Can be all, global, blog, or old tables. Default all.
     579     *  All returns all global tables and the blog tables for the queried blog.
     580     * @param bool $prefix Whether to include the blog prefix. Default false.
     581     * @param int $blog_id The blog_id to prefix. Defaults to main blog.
     582     * @return array Table names.
     583     */
     584    function tables( $scope = 'all', $prefix = false, $blog_id = 0 ) {
     585        switch ( $scope ) {
     586            case 'old' :
     587                $tables = $this->old_tables;
     588                break;
     589            case 'blog' :
     590                $tables = $this->tables;
     591                break;
     592            case 'global' :
     593                $tables = array_merge( $this->global_tables, $this->ms_tables );
     594                break;
     595            case 'all' :
     596                $tables = array_merge( $this->global_tables, $this->tables );
     597                if ( is_multisite() )
     598                    $tables = array_merge( $tables, $this->ms_tables );
     599                break;
     600        }
     601
     602        if ( $prefix ) {
     603            $prefix = $this->get_blog_prefix( $blog_id );
     604            foreach ( $tables as &$table ) {
     605                $table = $prefix . $table;
     606            }
     607        }
     608
     609        return $tables;
    499610    }
    500611
Note: See TracChangeset for help on using the changeset viewer.