Make WordPress Core

Changeset 13229


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

Location:
trunk
Files:
4 edited

Legend:

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

    r13224 r13229  
    3232    $okay = true;
    3333
    34     $tables = array_merge( $wpdb->tables, is_multisite() ? $wpdb->global_tables : array( 'users', 'usermeta' ) );
    35     $prefix = $wpdb->prefix;
    36     if ( is_multisite() && ! defined('MULTISITE') ) // _1 to get MU-era main blog
    37         $prefix .= '_1';
    38 
     34    $tables = $wpdb->tables( 'all', true );
    3935    // Loop over the WP tables, checking and repairing as needed.
    4036    foreach ( $tables as $table ) {
    41         if ( in_array( $table, $wpdb->old_tables ) )
    42             continue;
    43 
    44         $check = $wpdb->get_row("CHECK TABLE {$prefix}$table");
     37        $check = $wpdb->get_row("CHECK TABLE $table");
    4538        if ( 'OK' == $check->Msg_text ) {
    46             echo "<p>The {$prefix}$table table is okay.";
     39            echo "<p>The $table table is okay.";
    4740        } else {
    48             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;";
    49             $repair = $wpdb->get_row("REPAIR TABLE {$prefix}$table");
     41            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;";
     42            $repair = $wpdb->get_row("REPAIR TABLE $table");
    5043            if ( 'OK' == $check->Msg_text ) {
    51                 echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;Successfully repaired the {$prefix}$table table.";
     44                echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;Successfully repaired the $table table.";
    5245            } else {
    53                 echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;Failed to repair the {prefix}$table table. Error: $check->Msg_text<br />";
    54                 $problems["{$prefix}$table"] = $check->Msg_text;
     46                echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;Failed to repair the $table table. Error: $check->Msg_text<br />";
     47                $problems["$table"] = $check->Msg_text;
    5548                $okay = false;
    5649            }
    5750        }
    5851        if ( $okay && $optimize ) {
    59             $check = $wpdb->get_row("ANALYZE TABLE {$prefix}$table");
     52            $check = $wpdb->get_row("ANALYZE TABLE $table");
    6053            if ( 'Table is already up to date' == $check->Msg_text )  {
    61                 echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;The {$prefix}$table table is already optimized.";
     54                echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;The $table table is already optimized.";
    6255            } else {
    63                 $check = $wpdb->get_row("OPTIMIZE TABLE {$prefix}$table");
     56                $check = $wpdb->get_row("OPTIMIZE TABLE $table");
    6457                if ( 'OK' == $check->Msg_text || 'Table is already up to date' == $check->Msg_text )
    65                     echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;Successfully optimized the {$prefix}$table table.";
     58                    echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;Successfully optimized the $table table.";
    6659                else
    67                     echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;Failed to optimize the {$prefix}$table table. Error: $check->Msg_text";
     60                    echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;Failed to optimize the $table table. Error: $check->Msg_text";
    6861            }
    6962        }
  • trunk/wp-includes/deprecated.php

    r13098 r13229  
    22/**
    33 * Deprecated functions from past WordPress versions. You shouldn't use these
    4  * globals and functions and look for the alternatives instead. The functions
    5  * and globals will be removed in a later version.
     4 * functions and look for the alternatives instead. The functions will be
     5 * removed in a later version.
    66 *
    77 * @package WordPress
    88 * @subpackage Deprecated
    99 */
    10 
    11 /*
    12  * Deprecated global variables.
    13  */
    14 
    15 /**
    16  * The name of the Posts table
    17  * @global string $tableposts
    18  * @deprecated Use $wpdb->posts
    19  */
    20 $tableposts = $wpdb->posts;
    21 
    22 /**
    23  * The name of the Users table
    24  * @global string $tableusers
    25  * @deprecated Use $wpdb->users
    26  */
    27 $tableusers = $wpdb->users;
    28 
    29 /**
    30  * The name of the Categories table
    31  * @global string $tablecategories
    32  * @deprecated Use $wpdb->categories
    33  */
    34 $tablecategories = $wpdb->categories;
    35 
    36 /**
    37  * The name of the post to category table
    38  * @global string $tablepost2cat
    39  * @deprecated Use $wpdb->post2cat;
    40  */
    41 $tablepost2cat = $wpdb->post2cat;
    42 
    43 /**
    44  * The name of the comments table
    45  * @global string $tablecomments
    46  * @deprecated Use $wpdb->comments;
    47  */
    48 $tablecomments = $wpdb->comments;
    49 
    50 /**
    51  * The name of the links table
    52  * @global string $tablelinks
    53  * @deprecated Use $wpdb->links;
    54  */
    55 $tablelinks = $wpdb->links;
    56 
    57 /**
    58  * @global string $tablelinkcategories
    59  * @deprecated Not used anymore;
    60  */
    61 $tablelinkcategories = 'linkcategories_is_gone';
    62 
    63 /**
    64  * The name of the options table
    65  * @global string $tableoptions
    66  * @deprecated Use $wpdb->options;
    67  */
    68 $tableoptions = $wpdb->options;
    69 
    70 /**
    71  * The name of the postmeta table
    72  * @global string $tablepostmeta
    73  * @deprecated Use $wpdb->postmeta;
    74  */
    75 $tablepostmeta = $wpdb->postmeta;
    7610
    7711/*
  • trunk/wp-includes/functions.php

    r13210 r13229  
    18281828    $wpdb->suppress_errors( $suppress );
    18291829
     1830    $wp_tables = $wpdb->tables( 'all', true );
    18301831    // Loop over the WP tables.  If none exist, then scratch install is allowed.
    18311832    // If one or more exist, suggest table repair since we got here because the options
    18321833    // table could not be accessed.
    1833     foreach ($wpdb->tables as $table) {
     1834    foreach ( $wp_tables as $table ) {
    18341835        // If one of the WP tables exist, then we are in an insane state.
    1835         if ( in_array($wpdb->prefix . $table, $tables) ) {
     1836        if ( in_array( $table, $tables ) ) {
    18361837            // If visiting repair.php, return true and let it take over.
    18371838            if ( defined('WP_REPAIRING') )
  • 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.