Make WordPress Core


Ignore:
Timestamp:
10/06/2011 12:21:24 AM (15 years ago)
Author:
ryan
Message:

Introduce wp_get_db_schema() for rerieving the various flavors of the WP db schema. Eliminates need to use global. Allows multiple calls to wpmu_create_blog(). see #12028

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/schema.php

    r18878 r18899  
    88 * @subpackage Administration
    99 */
     10
     11// Declare these as global in case schema.php is included from a function.
     12global $wpdb, $wp_queries, $charset_collate;
    1013
    1114/**
     
    1720$charset_collate = '';
    1821
    19 // Declare these as global in case schema.php is included from a function.
    20 global $wpdb, $wp_queries;
    21 
    22 if ( ! empty($wpdb->charset) )
     22if ( ! empty( $wpdb->charset ) )
    2323        $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
    24 if ( ! empty($wpdb->collate) )
     24if ( ! empty( $wpdb->collate ) )
    2525        $charset_collate .= " COLLATE $wpdb->collate";
    2626
    27 /** Create WordPress database tables SQL */
    28 $wp_queries = "CREATE TABLE $wpdb->terms (
     27/**
     28 * Retrieve the SQL for creating database tables.
     29 *
     30 * @since 3.3.0
     31 *
     32 * @param string $scope Optional. The tables for which to retrieve SQL. Can be all, global, ms_global, or blog tables. Defaults to all.
     33 * @param int $blog_id Optional. The blog ID for which to retrieve SQL.  Default is the current blog ID.
     34 * @return string The SQL needed to create the requested tables.
     35 */
     36function wp_get_db_schema( $scope = 'all', $blog_id = null ) {
     37        global $wpdb;
     38
     39        if ( ! empty($wpdb->charset) )
     40                $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
     41        if ( ! empty($wpdb->collate) )
     42                $charset_collate .= " COLLATE $wpdb->collate";
     43
     44        if ( $blog_id && $blog_id != $wpdb->blogid )
     45                $old_blog_id = $wpdb->set_blog_id( $blog_id );
     46
     47        // Engage multisite if in the middle of turning it on from network.php.
     48        $is_multisite = is_multisite() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK );
     49
     50        // Blog specific tables.
     51        $blog_tables = "CREATE TABLE $wpdb->terms (
    2952 term_id bigint(20) unsigned NOT NULL auto_increment,
    3053 name varchar(200) NOT NULL default '',
     
    149172  KEY post_parent (post_parent),
    150173  KEY post_author (post_author)
    151 ) $charset_collate;
    152 CREATE TABLE $wpdb->users (
     174) $charset_collate;\n";
     175
     176        // Single site users table. The multisite flavor of the users table is handled below.
     177        $users_single_table = "CREATE TABLE $wpdb->users (
    153178  ID bigint(20) unsigned NOT NULL auto_increment,
    154179  user_login varchar(60) NOT NULL default '',
     
    164189  KEY user_login_key (user_login),
    165190  KEY user_nicename (user_nicename)
    166 ) $charset_collate;
    167 CREATE TABLE $wpdb->usermeta (
     191) $charset_collate;\n";
     192
     193        // Multisite users table
     194        $users_multi_table = "CREATE TABLE $wpdb->users (
     195  ID bigint(20) unsigned NOT NULL auto_increment,
     196  user_login varchar(60) NOT NULL default '',
     197  user_pass varchar(64) NOT NULL default '',
     198  user_nicename varchar(50) NOT NULL default '',
     199  user_email varchar(100) NOT NULL default '',
     200  user_url varchar(100) NOT NULL default '',
     201  user_registered datetime NOT NULL default '0000-00-00 00:00:00',
     202  user_activation_key varchar(60) NOT NULL default '',
     203  user_status int(11) NOT NULL default '0',
     204  display_name varchar(250) NOT NULL default '',
     205  spam tinyint(2) NOT NULL default '0',
     206  deleted tinyint(2) NOT NULL default '0',
     207  PRIMARY KEY  (ID),
     208  KEY user_login_key (user_login),
     209  KEY user_nicename (user_nicename)
     210) $charset_collate;\n";
     211
     212        // usermeta
     213        $usermeta_table = "CREATE TABLE $wpdb->usermeta (
    168214  umeta_id bigint(20) unsigned NOT NULL auto_increment,
    169215  user_id bigint(20) unsigned NOT NULL default '0',
     
    173219  KEY user_id (user_id),
    174220  KEY meta_key (meta_key)
     221) $charset_collate;\n";
     222
     223        // Global tables
     224        if ( $is_multisite )
     225                $global_tables = $users_multi_table . $usermeta_table;
     226        else
     227                $global_tables = $users_single_table . $usermeta_table;
     228
     229        // Multisite global tables.
     230        $ms_global_tables = "CREATE TABLE $wpdb->blogs (
     231  blog_id bigint(20) NOT NULL auto_increment,
     232  site_id bigint(20) NOT NULL default '0',
     233  domain varchar(200) NOT NULL default '',
     234  path varchar(100) NOT NULL default '',
     235  registered datetime NOT NULL default '0000-00-00 00:00:00',
     236  last_updated datetime NOT NULL default '0000-00-00 00:00:00',
     237  public tinyint(2) NOT NULL default '1',
     238  archived enum('0','1') NOT NULL default '0',
     239  mature tinyint(2) NOT NULL default '0',
     240  spam tinyint(2) NOT NULL default '0',
     241  deleted tinyint(2) NOT NULL default '0',
     242  lang_id int(11) NOT NULL default '0',
     243  PRIMARY KEY  (blog_id),
     244  KEY domain (domain(50),path(5)),
     245  KEY lang_id (lang_id)
     246) $charset_collate;
     247CREATE TABLE $wpdb->blog_versions (
     248  blog_id bigint(20) NOT NULL default '0',
     249  db_version varchar(20) NOT NULL default '',
     250  last_updated datetime NOT NULL default '0000-00-00 00:00:00',
     251  PRIMARY KEY  (blog_id),
     252  KEY db_version (db_version)
     253) $charset_collate;
     254CREATE TABLE $wpdb->registration_log (
     255  ID bigint(20) NOT NULL auto_increment,
     256  email varchar(255) NOT NULL default '',
     257  IP varchar(30) NOT NULL default '',
     258  blog_id bigint(20) NOT NULL default '0',
     259  date_registered datetime NOT NULL default '0000-00-00 00:00:00',
     260  PRIMARY KEY  (ID),
     261  KEY IP (IP)
     262) $charset_collate;
     263CREATE TABLE $wpdb->site (
     264  id bigint(20) NOT NULL auto_increment,
     265  domain varchar(200) NOT NULL default '',
     266  path varchar(100) NOT NULL default '',
     267  PRIMARY KEY  (id),
     268  KEY domain (domain,path)
     269) $charset_collate;
     270CREATE TABLE $wpdb->sitemeta (
     271  meta_id bigint(20) NOT NULL auto_increment,
     272  site_id bigint(20) NOT NULL default '0',
     273  meta_key varchar(255) default NULL,
     274  meta_value longtext,
     275  PRIMARY KEY  (meta_id),
     276  KEY meta_key (meta_key),
     277  KEY site_id (site_id)
     278) $charset_collate;
     279CREATE TABLE $wpdb->signups (
     280  domain varchar(200) NOT NULL default '',
     281  path varchar(100) NOT NULL default '',
     282  title longtext NOT NULL,
     283  user_login varchar(60) NOT NULL default '',
     284  user_email varchar(100) NOT NULL default '',
     285  registered datetime NOT NULL default '0000-00-00 00:00:00',
     286  activated datetime NOT NULL default '0000-00-00 00:00:00',
     287  active tinyint(1) NOT NULL default '0',
     288  activation_key varchar(50) NOT NULL default '',
     289  meta longtext,
     290  KEY activation_key (activation_key),
     291  KEY domain (domain)
    175292) $charset_collate;";
     293
     294        switch ( $scope ) {
     295                case 'blog' :
     296                        $queries = $blog_tables;
     297                        break;
     298                case 'global' :
     299                        $queries = $global_tables;
     300                        if ( $is_multisite )
     301                                $queries .= $ms_global_tables;
     302                        break;
     303                case 'ms_global' :
     304                        $queries = $ms_global_tables;
     305                        break;
     306                default:
     307                case 'all' :
     308                        $queries = $global_tables . $blog_tables;
     309                        if ( $is_multisite )
     310                                $queries .= $ms_global_tables;
     311                        break;
     312        }
     313
     314        if ( isset( $old_blog_id ) )
     315                $wpdb->set_blog_id( $old_blog_id );
     316
     317        return $queries;
     318}
     319
     320// Populate for back compat.
     321$wp_queries = wp_get_db_schema( 'all' );
    176322
    177323/**
     
    625771
    626772/**
     773 * Install Network.
     774 *
     775 * @since 3.0.0
     776 *
     777 */
     778if ( !function_exists( 'install_network' ) ) :
     779function install_network() {
     780        if ( ! defined( 'WP_INSTALLING_NETWORK' ) )
     781                define( 'WP_INSTALLING_NETWORK', true );
     782
     783        dbDelta( wp_get_db_schema( 'global' ) );
     784}
     785endif;
     786
     787/**
    627788 * populate network settings
    628789 *
Note: See TracChangeset for help on using the changeset viewer.