wp/wp-admin/includes/upgrade.php
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
     6  *
     6  *
     7  * @package WordPress
     7  * @package WordPress
     8  * @subpackage Administration
     8  * @subpackage Administration
     9  */
     9  */
    10 
    10 
    11 /** Include user install customize script. */
    11 /** Include user installation customization script. */
    12 if ( file_exists(WP_CONTENT_DIR . '/install.php') )
    12 if ( file_exists(WP_CONTENT_DIR . '/install.php') )
    13 	require (WP_CONTENT_DIR . '/install.php');
    13 	require (WP_CONTENT_DIR . '/install.php');
    14 
    14 
    15 /** WordPress Administration API */
    15 /** WordPress Administration API */
    16 require_once(ABSPATH . 'wp-admin/includes/admin.php');
    16 require_once(ABSPATH . 'wp-admin/includes/admin.php');
    25  * Runs the required functions to set up and populate the database,
    25  * Runs the required functions to set up and populate the database,
    26  * including primary admin user and initial options.
    26  * including primary admin user and initial options.
    27  *
    27  *
    28  * @since 2.1.0
    28  * @since 2.1.0
    29  *
    29  *
    30  * @param string $blog_title    Blog title.
    30  * @param string $blog_title    Site title.
    31  * @param string $user_name     User's username.
    31  * @param string $user_name     User's username.
    32  * @param string $user_email    User's email.
    32  * @param string $user_email    User's email.
    33  * @param bool   $public        Whether blog is public.
    33  * @param bool   $public        Whether site is public.
    34  * @param string $deprecated    Optional. Not used.
    34  * @param string $deprecated    Optional. Not used.
    35  * @param string $user_password Optional. User's chosen password. Default empty (random password).
    35  * @param string $user_password Optional. User's chosen password. Default empty (random password).
    36  * @param string $language      Optional. Language chosen. Default empty.
    36  * @param string $language      Optional. Language chosen. Default empty.
    37  * @return array Array keys 'url', 'user_id', 'password', and 'password_message'.
    37  * @return array Array keys 'url', 'user_id', 'password', and 'password_message'.
    38  */
    38  */
    39 function wp_install( $blog_title, $user_name, $user_email, $public, $deprecated = '', $user_password = '', $language = '' ) {
    39 function wp_install( $blog_title, $user_name, $user_email, $public, $deprecated = '', $user_password = '', $language = '' ) {
    40 	if ( !empty( $deprecated ) )
    40 	if ( !empty( $deprecated ) )
    41 		_deprecated_argument( __FUNCTION__, '2.6' );
    41 		_deprecated_argument( __FUNCTION__, '2.6.0' );
    42 
    42 
    43 	wp_check_mysql_version();
    43 	wp_check_mysql_version();
    44 	wp_cache_flush();
    44 	wp_cache_flush();
    45 	make_db_current_silent();
    45 	make_db_current_silent();
    46 	populate_options();
    46 	populate_options();
    48 
    48 
    49 	update_option('blogname', $blog_title);
    49 	update_option('blogname', $blog_title);
    50 	update_option('admin_email', $user_email);
    50 	update_option('admin_email', $user_email);
    51 	update_option('blog_public', $public);
    51 	update_option('blog_public', $public);
    52 
    52 
       
    53 	// Freshness of site - in the future, this could get more specific about actions taken, perhaps.
       
    54 	update_option( 'fresh_site', 1 );
       
    55 
    53 	if ( $language ) {
    56 	if ( $language ) {
    54 		update_option( 'WPLANG', $language );
    57 		update_option( 'WPLANG', $language );
    55 	}
    58 	}
    56 
    59 
    57 	$guessurl = wp_guess_url();
    60 	$guessurl = wp_guess_url();
    62 	if ( ! $public )
    65 	if ( ! $public )
    63 		update_option('default_pingback_flag', 0);
    66 		update_option('default_pingback_flag', 0);
    64 
    67 
    65 	/*
    68 	/*
    66 	 * Create default user. If the user already exists, the user tables are
    69 	 * Create default user. If the user already exists, the user tables are
    67 	 * being shared among blogs. Just set the role in that case.
    70 	 * being shared among sites. Just set the role in that case.
    68 	 */
    71 	 */
    69 	$user_id = username_exists($user_name);
    72 	$user_id = username_exists($user_name);
    70 	$user_password = trim($user_password);
    73 	$user_password = trim($user_password);
    71 	$email_password = false;
    74 	$email_password = false;
    72 	if ( !$user_id && empty($user_password) ) {
    75 	if ( !$user_id && empty($user_password) ) {
    90 
    93 
    91 	wp_install_maybe_enable_pretty_permalinks();
    94 	wp_install_maybe_enable_pretty_permalinks();
    92 
    95 
    93 	flush_rewrite_rules();
    96 	flush_rewrite_rules();
    94 
    97 
    95 	wp_new_blog_notification($blog_title, $guessurl, $user_id, ($email_password ? $user_password : __('The password you chose during the install.') ) );
    98 	wp_new_blog_notification($blog_title, $guessurl, $user_id, ($email_password ? $user_password : __('The password you chose during installation.') ) );
    96 
    99 
    97 	wp_cache_flush();
   100 	wp_cache_flush();
    98 
   101 
    99 	/**
   102 	/**
   100 	 * Fires after a site is fully installed.
   103 	 * Fires after a site is fully installed.
   116  * Adds the default "Uncategorized" category, the first post (with comment),
   119  * Adds the default "Uncategorized" category, the first post (with comment),
   117  * first page, and default widgets for default theme for the current version.
   120  * first page, and default widgets for default theme for the current version.
   118  *
   121  *
   119  * @since 2.1.0
   122  * @since 2.1.0
   120  *
   123  *
       
   124  * @global wpdb       $wpdb
       
   125  * @global WP_Rewrite $wp_rewrite
       
   126  * @global string     $table_prefix
       
   127  *
   121  * @param int $user_id User ID.
   128  * @param int $user_id User ID.
   122  */
   129  */
   123 function wp_install_defaults( $user_id ) {
   130 function wp_install_defaults( $user_id ) {
   124 	global $wpdb, $wp_rewrite, $table_prefix;
   131 	global $wpdb, $wp_rewrite, $table_prefix;
   125 
   132 
   149 	$first_post_guid = get_option( 'home' ) . '/?p=1';
   156 	$first_post_guid = get_option( 'home' ) . '/?p=1';
   150 
   157 
   151 	if ( is_multisite() ) {
   158 	if ( is_multisite() ) {
   152 		$first_post = get_site_option( 'first_post' );
   159 		$first_post = get_site_option( 'first_post' );
   153 
   160 
   154 		if ( empty($first_post) )
   161 		if ( ! $first_post ) {
   155 			$first_post = __( 'Welcome to <a href="SITE_URL">SITE_NAME</a>. This is your first post. Edit or delete it, then start blogging!' );
   162 			/* translators: %s: site link */
   156 
   163 			$first_post = __( 'Welcome to %s. This is your first post. Edit or delete it, then start blogging!' );
   157 		$first_post = str_replace( "SITE_URL", esc_url( network_home_url() ), $first_post );
   164 		}
   158 		$first_post = str_replace( "SITE_NAME", get_current_site()->site_name, $first_post );
   165 
       
   166 		$first_post = sprintf( $first_post,
       
   167 			sprintf( '<a href="%s">%s</a>', esc_url( network_home_url() ), get_network()->site_name )
       
   168 		);
       
   169 
       
   170 		// Back-compat for pre-4.4
       
   171 		$first_post = str_replace( 'SITE_URL', esc_url( network_home_url() ), $first_post );
       
   172 		$first_post = str_replace( 'SITE_NAME', get_network()->site_name, $first_post );
   159 	} else {
   173 	} else {
   160 		$first_post = __('Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!');
   174 		$first_post = __( 'Welcome to WordPress. This is your first post. Edit or delete it, then start writing!' );
   161 	}
   175 	}
   162 
   176 
   163 	$wpdb->insert( $wpdb->posts, array(
   177 	$wpdb->insert( $wpdb->posts, array(
   164 								'post_author' => $user_id,
   178 		'post_author' => $user_id,
   165 								'post_date' => $now,
   179 		'post_date' => $now,
   166 								'post_date_gmt' => $now_gmt,
   180 		'post_date_gmt' => $now_gmt,
   167 								'post_content' => $first_post,
   181 		'post_content' => $first_post,
   168 								'post_excerpt' => '',
   182 		'post_excerpt' => '',
   169 								'post_title' => __('Hello world!'),
   183 		'post_title' => __('Hello world!'),
   170 								/* translators: Default post slug */
   184 		/* translators: Default post slug */
   171 								'post_name' => sanitize_title( _x('hello-world', 'Default post slug') ),
   185 		'post_name' => sanitize_title( _x('hello-world', 'Default post slug') ),
   172 								'post_modified' => $now,
   186 		'post_modified' => $now,
   173 								'post_modified_gmt' => $now_gmt,
   187 		'post_modified_gmt' => $now_gmt,
   174 								'guid' => $first_post_guid,
   188 		'guid' => $first_post_guid,
   175 								'comment_count' => 1,
   189 		'comment_count' => 1,
   176 								'to_ping' => '',
   190 		'to_ping' => '',
   177 								'pinged' => '',
   191 		'pinged' => '',
   178 								'post_content_filtered' => ''
   192 		'post_content_filtered' => ''
   179 								));
   193 	));
   180 	$wpdb->insert( $wpdb->term_relationships, array('term_taxonomy_id' => $cat_tt_id, 'object_id' => 1) );
   194 	$wpdb->insert( $wpdb->term_relationships, array('term_taxonomy_id' => $cat_tt_id, 'object_id' => 1) );
   181 
   195 
   182 	// Default comment
   196 	// Default comment
   183 	$first_comment_author = __('Mr WordPress');
       
   184 	$first_comment_url = 'https://wordpress.org/';
       
   185 	$first_comment = __('Hi, this is a comment.
       
   186 To delete a comment, just log in and view the post&#039;s comments. There you will have the option to edit or delete them.');
       
   187 	if ( is_multisite() ) {
   197 	if ( is_multisite() ) {
   188 		$first_comment_author = get_site_option( 'first_comment_author', $first_comment_author );
   198 		$first_comment_author = get_site_option( 'first_comment_author' );
       
   199 		$first_comment_email = get_site_option( 'first_comment_email' );
   189 		$first_comment_url = get_site_option( 'first_comment_url', network_home_url() );
   200 		$first_comment_url = get_site_option( 'first_comment_url', network_home_url() );
   190 		$first_comment = get_site_option( 'first_comment', $first_comment );
   201 		$first_comment = get_site_option( 'first_comment' );
   191 	}
   202 	}
       
   203 
       
   204 	$first_comment_author = ! empty( $first_comment_author ) ? $first_comment_author : __( 'A WordPress Commenter' );
       
   205 	$first_comment_email = ! empty( $first_comment_email ) ? $first_comment_email : 'wapuu@wordpress.example';
       
   206 	$first_comment_url = ! empty( $first_comment_url ) ? $first_comment_url : 'https://wordpress.org/';
       
   207 	$first_comment = ! empty( $first_comment ) ? $first_comment :  __( 'Hi, this is a comment.
       
   208 To get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.
       
   209 Commenter avatars come from <a href="https://gravatar.com">Gravatar</a>.' );
   192 	$wpdb->insert( $wpdb->comments, array(
   210 	$wpdb->insert( $wpdb->comments, array(
   193 								'comment_post_ID' => 1,
   211 		'comment_post_ID' => 1,
   194 								'comment_author' => $first_comment_author,
   212 		'comment_author' => $first_comment_author,
   195 								'comment_author_email' => '',
   213 		'comment_author_email' => $first_comment_email,
   196 								'comment_author_url' => $first_comment_url,
   214 		'comment_author_url' => $first_comment_url,
   197 								'comment_date' => $now,
   215 		'comment_date' => $now,
   198 								'comment_date_gmt' => $now_gmt,
   216 		'comment_date_gmt' => $now_gmt,
   199 								'comment_content' => $first_comment
   217 		'comment_content' => $first_comment
   200 								));
   218 	));
   201 
   219 
   202 	// First Page
   220 	// First Page
   203 	$first_page = sprintf( __( "This is an example page. It's different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:
   221 	if ( is_multisite() )
   204 
   222 		$first_page = get_site_option( 'first_page' );
   205 <blockquote>Hi there! I'm a bike messenger by day, aspiring actor by night, and this is my blog. I live in Los Angeles, have a great dog named Jack, and I like pi&#241;a coladas. (And gettin' caught in the rain.)</blockquote>
   223 
       
   224 	$first_page = ! empty( $first_page ) ? $first_page : sprintf( __( "This is an example page. It's different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:
       
   225 
       
   226 <blockquote>Hi there! I'm a bike messenger by day, aspiring actor by night, and this is my website. I live in Los Angeles, have a great dog named Jack, and I like pi&#241;a coladas. (And gettin' caught in the rain.)</blockquote>
   206 
   227 
   207 ...or something like this:
   228 ...or something like this:
   208 
   229 
   209 <blockquote>The XYZ Doohickey Company was founded in 1971, and has been providing quality doohickeys to the public ever since. Located in Gotham City, XYZ employs over 2,000 people and does all kinds of awesome things for the Gotham community.</blockquote>
   230 <blockquote>The XYZ Doohickey Company was founded in 1971, and has been providing quality doohickeys to the public ever since. Located in Gotham City, XYZ employs over 2,000 people and does all kinds of awesome things for the Gotham community.</blockquote>
   210 
   231 
   211 As a new WordPress user, you should go to <a href=\"%s\">your dashboard</a> to delete this page and create new pages for your content. Have fun!" ), admin_url() );
   232 As a new WordPress user, you should go to <a href=\"%s\">your dashboard</a> to delete this page and create new pages for your content. Have fun!" ), admin_url() );
   212 	if ( is_multisite() )
   233 
   213 		$first_page = get_site_option( 'first_page', $first_page );
       
   214 	$first_post_guid = get_option('home') . '/?page_id=2';
   234 	$first_post_guid = get_option('home') . '/?page_id=2';
   215 	$wpdb->insert( $wpdb->posts, array(
   235 	$wpdb->insert( $wpdb->posts, array(
   216 								'post_author' => $user_id,
   236 		'post_author' => $user_id,
   217 								'post_date' => $now,
   237 		'post_date' => $now,
   218 								'post_date_gmt' => $now_gmt,
   238 		'post_date_gmt' => $now_gmt,
   219 								'post_content' => $first_page,
   239 		'post_content' => $first_page,
   220 								'post_excerpt' => '',
   240 		'post_excerpt' => '',
   221 								'post_title' => __( 'Sample Page' ),
   241 		'comment_status' => 'closed',
   222 								/* translators: Default page slug */
   242 		'post_title' => __( 'Sample Page' ),
   223 								'post_name' => __( 'sample-page' ),
   243 		/* translators: Default page slug */
   224 								'post_modified' => $now,
   244 		'post_name' => __( 'sample-page' ),
   225 								'post_modified_gmt' => $now_gmt,
   245 		'post_modified' => $now,
   226 								'guid' => $first_post_guid,
   246 		'post_modified_gmt' => $now_gmt,
   227 								'post_type' => 'page',
   247 		'guid' => $first_post_guid,
   228 								'to_ping' => '',
   248 		'post_type' => 'page',
   229 								'pinged' => '',
   249 		'to_ping' => '',
   230 								'post_content_filtered' => ''
   250 		'pinged' => '',
   231 								));
   251 		'post_content_filtered' => ''
       
   252 	));
   232 	$wpdb->insert( $wpdb->postmeta, array( 'post_id' => 2, 'meta_key' => '_wp_page_template', 'meta_value' => 'default' ) );
   253 	$wpdb->insert( $wpdb->postmeta, array( 'post_id' => 2, 'meta_key' => '_wp_page_template', 'meta_value' => 'default' ) );
       
   254 
       
   255 	// Privacy Policy page
       
   256 	if ( is_multisite() ) {
       
   257 		// Disable by default unless the suggested content is provided.
       
   258 		$privacy_policy_content = get_site_option( 'default_privacy_policy_content' );
       
   259 	} else {
       
   260 		if ( ! class_exists( 'WP_Privacy_Policy_Content' ) ) {
       
   261 			include_once( ABSPATH . 'wp-admin/includes/misc.php' );
       
   262 		}
       
   263 
       
   264 		$privacy_policy_content = WP_Privacy_Policy_Content::get_default_content();
       
   265 	}
       
   266 
       
   267 	if ( ! empty( $privacy_policy_content ) ) {
       
   268 		$privacy_policy_guid = get_option( 'home' ) . '/?page_id=3';
       
   269 
       
   270 		$wpdb->insert(
       
   271 			$wpdb->posts, array(
       
   272 				'post_author'           => $user_id,
       
   273 				'post_date'             => $now,
       
   274 				'post_date_gmt'         => $now_gmt,
       
   275 				'post_content'          => $privacy_policy_content,
       
   276 				'post_excerpt'          => '',
       
   277 				'comment_status'        => 'closed',
       
   278 				'post_title'            => __( 'Privacy Policy' ),
       
   279 				/* translators: Privacy Policy page slug */
       
   280 				'post_name'             => __( 'privacy-policy' ),
       
   281 				'post_modified'         => $now,
       
   282 				'post_modified_gmt'     => $now_gmt,
       
   283 				'guid'                  => $privacy_policy_guid,
       
   284 				'post_type'             => 'page',
       
   285 				'post_status'           => 'draft',
       
   286 				'to_ping'               => '',
       
   287 				'pinged'                => '',
       
   288 				'post_content_filtered' => '',
       
   289 			)
       
   290 		);
       
   291 		$wpdb->insert(
       
   292 			$wpdb->postmeta, array(
       
   293 				'post_id'    => 3,
       
   294 				'meta_key'   => '_wp_page_template',
       
   295 				'meta_value' => 'default',
       
   296 			)
       
   297 		);
       
   298 		update_option( 'wp_page_for_privacy_policy', 3 );
       
   299 	}
   233 
   300 
   234 	// Set up default widgets for default theme.
   301 	// Set up default widgets for default theme.
   235 	update_option( 'widget_search', array ( 2 => array ( 'title' => '' ), '_multiwidget' => 1 ) );
   302 	update_option( 'widget_search', array ( 2 => array ( 'title' => '' ), '_multiwidget' => 1 ) );
   236 	update_option( 'widget_recent-posts', array ( 2 => array ( 'title' => '', 'number' => 5 ), '_multiwidget' => 1 ) );
   303 	update_option( 'widget_recent-posts', array ( 2 => array ( 'title' => '', 'number' => 5 ), '_multiwidget' => 1 ) );
   237 	update_option( 'widget_recent-comments', array ( 2 => array ( 'title' => '', 'number' => 5 ), '_multiwidget' => 1 ) );
   304 	update_option( 'widget_recent-comments', array ( 2 => array ( 'title' => '', 'number' => 5 ), '_multiwidget' => 1 ) );
   238 	update_option( 'widget_archives', array ( 2 => array ( 'title' => '', 'count' => 0, 'dropdown' => 0 ), '_multiwidget' => 1 ) );
   305 	update_option( 'widget_archives', array ( 2 => array ( 'title' => '', 'count' => 0, 'dropdown' => 0 ), '_multiwidget' => 1 ) );
   239 	update_option( 'widget_categories', array ( 2 => array ( 'title' => '', 'count' => 0, 'hierarchical' => 0, 'dropdown' => 0 ), '_multiwidget' => 1 ) );
   306 	update_option( 'widget_categories', array ( 2 => array ( 'title' => '', 'count' => 0, 'hierarchical' => 0, 'dropdown' => 0 ), '_multiwidget' => 1 ) );
   240 	update_option( 'widget_meta', array ( 2 => array ( 'title' => '' ), '_multiwidget' => 1 ) );
   307 	update_option( 'widget_meta', array ( 2 => array ( 'title' => '' ), '_multiwidget' => 1 ) );
   241 	update_option( 'sidebars_widgets', array ( 'wp_inactive_widgets' => array (), 'sidebar-1' => array ( 0 => 'search-2', 1 => 'recent-posts-2', 2 => 'recent-comments-2', 3 => 'archives-2', 4 => 'categories-2', 5 => 'meta-2', ), 'array_version' => 3 ) );
   308 	update_option( 'sidebars_widgets', array( 'wp_inactive_widgets' => array(), 'sidebar-1' => array( 0 => 'search-2', 1 => 'recent-posts-2', 2 => 'recent-comments-2', 3 => 'archives-2', 4 => 'categories-2', 5 => 'meta-2' ), 'sidebar-2' => array(), 'sidebar-3' => array(), 'array_version' => 3 ) );
   242 
       
   243 	if ( ! is_multisite() )
   309 	if ( ! is_multisite() )
   244 		update_user_meta( $user_id, 'show_welcome_panel', 1 );
   310 		update_user_meta( $user_id, 'show_welcome_panel', 1 );
   245 	elseif ( ! is_super_admin( $user_id ) && ! metadata_exists( 'user', $user_id, 'show_welcome_panel' ) )
   311 	elseif ( ! is_super_admin( $user_id ) && ! metadata_exists( 'user', $user_id, 'show_welcome_panel' ) )
   246 		update_user_meta( $user_id, 'show_welcome_panel', 2 );
   312 		update_user_meta( $user_id, 'show_welcome_panel', 2 );
   247 
   313 
   263 	}
   329 	}
   264 }
   330 }
   265 endif;
   331 endif;
   266 
   332 
   267 /**
   333 /**
   268  * Maybe enable pretty permalinks on install.
   334  * Maybe enable pretty permalinks on installation.
   269  *
   335  *
   270  * If after enabling pretty permalinks don't work, fallback to query-string permalinks.
   336  * If after enabling pretty permalinks don't work, fallback to query-string permalinks.
   271  *
   337  *
   272  * @since 4.2.0
   338  * @since 4.2.0
   273  *
   339  *
   303 	 	 * Flush rules with the hard option to force refresh of the web-server's
   369 	 	 * Flush rules with the hard option to force refresh of the web-server's
   304 	 	 * rewrite config file (e.g. .htaccess or web.config).
   370 	 	 * rewrite config file (e.g. .htaccess or web.config).
   305 	 	 */
   371 	 	 */
   306 		$wp_rewrite->flush_rules( true );
   372 		$wp_rewrite->flush_rules( true );
   307 
   373 
   308 		// Test against a real WordPress Post, or if none were created, a random 404 page.
   374 		$test_url = '';
   309 		$test_url = get_permalink( 1 );
   375 
   310 
   376 		// Test against a real WordPress Post
   311 		if ( ! $test_url ) {
   377 		$first_post = get_page_by_path( sanitize_title( _x( 'hello-world', 'Default post slug' ) ), OBJECT, 'post' );
   312 			$test_url = home_url( '/wordpress-check-for-rewrites/' );
   378 		if ( $first_post ) {
       
   379 			$test_url = get_permalink( $first_post->ID );
   313 		}
   380 		}
   314 
   381 
   315 		/*
   382 		/*
   316 	 	 * Send a request to the site, and check whether
   383 	 	 * Send a request to the site, and check whether
   317 	 	 * the 'x-pingback' header is returned as expected.
   384 	 	 * the 'x-pingback' header is returned as expected.
   345  * Sends an email with wp_mail to the new administrator that the site setup is complete,
   412  * Sends an email with wp_mail to the new administrator that the site setup is complete,
   346  * and provides them with a record of their login credentials.
   413  * and provides them with a record of their login credentials.
   347  *
   414  *
   348  * @since 2.1.0
   415  * @since 2.1.0
   349  *
   416  *
   350  * @param string $blog_title Blog title.
   417  * @param string $blog_title Site title.
   351  * @param string $blog_url   Blog url.
   418  * @param string $blog_url   Site url.
   352  * @param int    $user_id    User ID.
   419  * @param int    $user_id    User ID.
   353  * @param string $password   User's Password.
   420  * @param string $password   User's Password.
   354  */
   421  */
   355 function wp_new_blog_notification($blog_title, $blog_url, $user_id, $password) {
   422 function wp_new_blog_notification($blog_title, $blog_url, $user_id, $password) {
   356 	$user = new WP_User( $user_id );
   423 	$user = new WP_User( $user_id );
   357 	$email = $user->user_email;
   424 	$email = $user->user_email;
   358 	$name = $user->user_login;
   425 	$name = $user->user_login;
   359 	$login_url = wp_login_url();
   426 	$login_url = wp_login_url();
       
   427 	/* translators: New site notification email. 1: New site URL, 2: User login, 3: User password or password reset link, 4: Login URL */
   360 	$message = sprintf( __( "Your new WordPress site has been successfully set up at:
   428 	$message = sprintf( __( "Your new WordPress site has been successfully set up at:
   361 
   429 
   362 %1\$s
   430 %1\$s
   363 
   431 
   364 You can log in to the administrator account with the following information:
   432 You can log in to the administrator account with the following information:
   383  *
   451  *
   384  * Upgrades the database if needed during a site update.
   452  * Upgrades the database if needed during a site update.
   385  *
   453  *
   386  * @since 2.1.0
   454  * @since 2.1.0
   387  *
   455  *
   388  * @return null If no update is necessary or site isn't completely installed, null.
   456  * @global int  $wp_current_db_version
       
   457  * @global int  $wp_db_version
       
   458  * @global wpdb $wpdb WordPress database abstraction object.
   389  */
   459  */
   390 function wp_upgrade() {
   460 function wp_upgrade() {
   391 	global $wp_current_db_version, $wp_db_version, $wpdb;
   461 	global $wp_current_db_version, $wp_db_version, $wpdb;
   392 
   462 
   393 	$wp_current_db_version = __get_option('db_version');
   463 	$wp_current_db_version = __get_option('db_version');
   407 	if ( is_multisite() && is_main_site() )
   477 	if ( is_multisite() && is_main_site() )
   408 		upgrade_network();
   478 		upgrade_network();
   409 	wp_cache_flush();
   479 	wp_cache_flush();
   410 
   480 
   411 	if ( is_multisite() ) {
   481 	if ( is_multisite() ) {
   412 		if ( $wpdb->get_row( "SELECT blog_id FROM {$wpdb->blog_versions} WHERE blog_id = '{$wpdb->blogid}'" ) )
   482 		$site_id = get_current_blog_id();
   413 			$wpdb->query( "UPDATE {$wpdb->blog_versions} SET db_version = '{$wp_db_version}' WHERE blog_id = '{$wpdb->blogid}'" );
   483 
   414 		else
   484 		if ( $wpdb->get_row( $wpdb->prepare( "SELECT blog_id FROM {$wpdb->blog_versions} WHERE blog_id = %d", $site_id ) ) ) {
   415 			$wpdb->query( "INSERT INTO {$wpdb->blog_versions} ( `blog_id` , `db_version` , `last_updated` ) VALUES ( '{$wpdb->blogid}', '{$wp_db_version}', NOW());" );
   485 			$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->blog_versions} SET db_version = %d WHERE blog_id = %d", $wp_db_version, $site_id ) );
       
   486 		} else {
       
   487 			$wpdb->query( $wpdb->prepare( "INSERT INTO {$wpdb->blog_versions} ( `blog_id` , `db_version` , `last_updated` ) VALUES ( %d, %d, NOW() );", $site_id, $wp_db_version ) );
       
   488 		}
   416 	}
   489 	}
   417 
   490 
   418 	/**
   491 	/**
   419 	 * Fires after a site is fully upgraded.
   492 	 * Fires after a site is fully upgraded.
   420 	 *
   493 	 *
   426 	do_action( 'wp_upgrade', $wp_db_version, $wp_current_db_version );
   499 	do_action( 'wp_upgrade', $wp_db_version, $wp_current_db_version );
   427 }
   500 }
   428 endif;
   501 endif;
   429 
   502 
   430 /**
   503 /**
   431  * Functions to be called in install and upgrade scripts.
   504  * Functions to be called in installation and upgrade scripts.
   432  *
   505  *
   433  * Contains conditional checks to determine which upgrade scripts to run,
   506  * Contains conditional checks to determine which upgrade scripts to run,
   434  * based on database version and WP version being updated-to.
   507  * based on database version and WP version being updated-to.
   435  *
   508  *
       
   509  * @ignore
   436  * @since 1.0.1
   510  * @since 1.0.1
   437  *
   511  *
   438  * @return null If no update is necessary, null.
   512  * @global int $wp_current_db_version
       
   513  * @global int $wp_db_version
   439  */
   514  */
   440 function upgrade_all() {
   515 function upgrade_all() {
   441 	global $wp_current_db_version, $wp_db_version;
   516 	global $wp_current_db_version, $wp_db_version;
   442 	$wp_current_db_version = __get_option('db_version');
   517 	$wp_current_db_version = __get_option('db_version');
   443 
   518 
   522 		upgrade_380();
   597 		upgrade_380();
   523 
   598 
   524 	if ( $wp_current_db_version < 29630 )
   599 	if ( $wp_current_db_version < 29630 )
   525 		upgrade_400();
   600 		upgrade_400();
   526 
   601 
   527 	// Don't harsh my mellow. upgrade_422() must be called before
   602 	if ( $wp_current_db_version < 33055 )
   528 	// upgrade_420() to catch bad comments prior to any auto-expansion of
   603 		upgrade_430();
   529 	// MySQL column widths.
   604 
   530 	if ( $wp_current_db_version < 31534 )
   605 	if ( $wp_current_db_version < 33056 )
   531 		upgrade_422();
   606 		upgrade_431();
   532 
   607 
   533 	if ( $wp_current_db_version < 31351 )
   608 	if ( $wp_current_db_version < 35700 )
   534 		upgrade_420();
   609 		upgrade_440();
       
   610 
       
   611 	if ( $wp_current_db_version < 36686 )
       
   612 		upgrade_450();
       
   613 
       
   614 	if ( $wp_current_db_version < 37965 )
       
   615 		upgrade_460();
   535 
   616 
   536 	maybe_disable_link_manager();
   617 	maybe_disable_link_manager();
   537 
   618 
   538 	maybe_disable_automattic_widgets();
   619 	maybe_disable_automattic_widgets();
   539 
   620 
   542 }
   623 }
   543 
   624 
   544 /**
   625 /**
   545  * Execute changes made in WordPress 1.0.
   626  * Execute changes made in WordPress 1.0.
   546  *
   627  *
       
   628  * @ignore
   547  * @since 1.0.0
   629  * @since 1.0.0
       
   630  *
       
   631  * @global wpdb $wpdb WordPress database abstraction object.
   548  */
   632  */
   549 function upgrade_100() {
   633 function upgrade_100() {
   550 	global $wpdb;
   634 	global $wpdb;
   551 
   635 
   552 	// Get the title and ID of every post, post_name to check if it already has a value
   636 	// Get the title and ID of every post, post_name to check if it already has a value
   553 	$posts = $wpdb->get_results("SELECT ID, post_title, post_name FROM $wpdb->posts WHERE post_name = ''");
   637 	$posts = $wpdb->get_results("SELECT ID, post_title, post_name FROM $wpdb->posts WHERE post_name = ''");
   554 	if ($posts) {
   638 	if ($posts) {
   555 		foreach($posts as $post) {
   639 		foreach ($posts as $post) {
   556 			if ('' == $post->post_name) {
   640 			if ('' == $post->post_name) {
   557 				$newtitle = sanitize_title($post->post_title);
   641 				$newtitle = sanitize_title($post->post_title);
   558 				$wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_name = %s WHERE ID = %d", $newtitle, $post->ID) );
   642 				$wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_name = %s WHERE ID = %d", $newtitle, $post->ID) );
   559 			}
   643 			}
   560 		}
   644 		}
   598 }
   682 }
   599 
   683 
   600 /**
   684 /**
   601  * Execute changes made in WordPress 1.0.1.
   685  * Execute changes made in WordPress 1.0.1.
   602  *
   686  *
       
   687  * @ignore
   603  * @since 1.0.1
   688  * @since 1.0.1
       
   689  *
       
   690  * @global wpdb $wpdb WordPress database abstraction object.
   604  */
   691  */
   605 function upgrade_101() {
   692 function upgrade_101() {
   606 	global $wpdb;
   693 	global $wpdb;
   607 
   694 
   608 	// Clean up indices, add a few
   695 	// Clean up indices, add a few
   616 }
   703 }
   617 
   704 
   618 /**
   705 /**
   619  * Execute changes made in WordPress 1.2.
   706  * Execute changes made in WordPress 1.2.
   620  *
   707  *
       
   708  * @ignore
   621  * @since 1.2.0
   709  * @since 1.2.0
       
   710  *
       
   711  * @global wpdb $wpdb WordPress database abstraction object.
   622  */
   712  */
   623 function upgrade_110() {
   713 function upgrade_110() {
   624 	global $wpdb;
   714 	global $wpdb;
   625 
   715 
   626 	// Set user_nicename.
   716 	// Set user_nicename.
   676 }
   766 }
   677 
   767 
   678 /**
   768 /**
   679  * Execute changes made in WordPress 1.5.
   769  * Execute changes made in WordPress 1.5.
   680  *
   770  *
       
   771  * @ignore
   681  * @since 1.5.0
   772  * @since 1.5.0
       
   773  *
       
   774  * @global wpdb $wpdb WordPress database abstraction object.
   682  */
   775  */
   683 function upgrade_130() {
   776 function upgrade_130() {
   684 	global $wpdb;
   777 	global $wpdb;
   685 
   778 
   686 	// Remove extraneous backslashes.
   779 	// Remove extraneous backslashes.
   687 	$posts = $wpdb->get_results("SELECT ID, post_title, post_content, post_excerpt, guid, post_date, post_name, post_status, post_author FROM $wpdb->posts");
   780 	$posts = $wpdb->get_results("SELECT ID, post_title, post_content, post_excerpt, guid, post_date, post_name, post_status, post_author FROM $wpdb->posts");
   688 	if ($posts) {
   781 	if ($posts) {
   689 		foreach($posts as $post) {
   782 		foreach ($posts as $post) {
   690 			$post_content = addslashes(deslash($post->post_content));
   783 			$post_content = addslashes(deslash($post->post_content));
   691 			$post_title = addslashes(deslash($post->post_title));
   784 			$post_title = addslashes(deslash($post->post_title));
   692 			$post_excerpt = addslashes(deslash($post->post_excerpt));
   785 			$post_excerpt = addslashes(deslash($post->post_excerpt));
   693 			if ( empty($post->guid) )
   786 			if ( empty($post->guid) )
   694 				$guid = get_permalink($post->ID);
   787 				$guid = get_permalink($post->ID);
   701 	}
   794 	}
   702 
   795 
   703 	// Remove extraneous backslashes.
   796 	// Remove extraneous backslashes.
   704 	$comments = $wpdb->get_results("SELECT comment_ID, comment_author, comment_content FROM $wpdb->comments");
   797 	$comments = $wpdb->get_results("SELECT comment_ID, comment_author, comment_content FROM $wpdb->comments");
   705 	if ($comments) {
   798 	if ($comments) {
   706 		foreach($comments as $comment) {
   799 		foreach ($comments as $comment) {
   707 			$comment_content = deslash($comment->comment_content);
   800 			$comment_content = deslash($comment->comment_content);
   708 			$comment_author = deslash($comment->comment_author);
   801 			$comment_author = deslash($comment->comment_author);
   709 
   802 
   710 			$wpdb->update($wpdb->comments, compact('comment_content', 'comment_author'), array('comment_ID' => $comment->comment_ID) );
   803 			$wpdb->update($wpdb->comments, compact('comment_content', 'comment_author'), array('comment_ID' => $comment->comment_ID) );
   711 		}
   804 		}
   712 	}
   805 	}
   713 
   806 
   714 	// Remove extraneous backslashes.
   807 	// Remove extraneous backslashes.
   715 	$links = $wpdb->get_results("SELECT link_id, link_name, link_description FROM $wpdb->links");
   808 	$links = $wpdb->get_results("SELECT link_id, link_name, link_description FROM $wpdb->links");
   716 	if ($links) {
   809 	if ($links) {
   717 		foreach($links as $link) {
   810 		foreach ($links as $link) {
   718 			$link_name = deslash($link->link_name);
   811 			$link_name = deslash($link->link_name);
   719 			$link_description = deslash($link->link_description);
   812 			$link_description = deslash($link->link_description);
   720 
   813 
   721 			$wpdb->update( $wpdb->links, compact('link_name', 'link_description'), array('link_id' => $link->link_id) );
   814 			$wpdb->update( $wpdb->links, compact('link_name', 'link_description'), array('link_id' => $link->link_id) );
   722 		}
   815 		}
   760 }
   853 }
   761 
   854 
   762 /**
   855 /**
   763  * Execute changes made in WordPress 2.0.
   856  * Execute changes made in WordPress 2.0.
   764  *
   857  *
       
   858  * @ignore
   765  * @since 2.0.0
   859  * @since 2.0.0
       
   860  *
       
   861  * @global wpdb $wpdb WordPress database abstraction object.
       
   862  * @global int  $wp_current_db_version
   766  */
   863  */
   767 function upgrade_160() {
   864 function upgrade_160() {
   768 	global $wpdb, $wp_current_db_version;
   865 	global $wpdb, $wp_current_db_version;
   769 
   866 
   770 	populate_roles_160();
   867 	populate_roles_160();
   843 }
   940 }
   844 
   941 
   845 /**
   942 /**
   846  * Execute changes made in WordPress 2.1.
   943  * Execute changes made in WordPress 2.1.
   847  *
   944  *
       
   945  * @ignore
   848  * @since 2.1.0
   946  * @since 2.1.0
       
   947  *
       
   948  * @global wpdb $wpdb WordPress database abstraction object.
       
   949  * @global int  $wp_current_db_version
   849  */
   950  */
   850 function upgrade_210() {
   951 function upgrade_210() {
   851 	global $wpdb, $wp_current_db_version;
   952 	global $wpdb, $wp_current_db_version;
   852 
   953 
   853 	if ( $wp_current_db_version < 3506 ) {
   954 	if ( $wp_current_db_version < 3506 ) {
   887 }
   988 }
   888 
   989 
   889 /**
   990 /**
   890  * Execute changes made in WordPress 2.3.
   991  * Execute changes made in WordPress 2.3.
   891  *
   992  *
       
   993  * @ignore
   892  * @since 2.3.0
   994  * @since 2.3.0
       
   995  *
       
   996  * @global wpdb $wpdb WordPress database abstraction object.
       
   997  * @global int  $wp_current_db_version
   893  */
   998  */
   894 function upgrade_230() {
   999 function upgrade_230() {
   895 	global $wp_current_db_version, $wpdb;
  1000 	global $wp_current_db_version, $wpdb;
   896 
  1001 
   897 	if ( $wp_current_db_version < 5200 ) {
  1002 	if ( $wp_current_db_version < 5200 ) {
  1063 }
  1168 }
  1064 
  1169 
  1065 /**
  1170 /**
  1066  * Remove old options from the database.
  1171  * Remove old options from the database.
  1067  *
  1172  *
       
  1173  * @ignore
  1068  * @since 2.3.0
  1174  * @since 2.3.0
       
  1175  *
       
  1176  * @global wpdb $wpdb WordPress database abstraction object.
  1069  */
  1177  */
  1070 function upgrade_230_options_table() {
  1178 function upgrade_230_options_table() {
  1071 	global $wpdb;
  1179 	global $wpdb;
  1072 	$old_options_fields = array( 'option_can_override', 'option_type', 'option_width', 'option_height', 'option_description', 'option_admin_level' );
  1180 	$old_options_fields = array( 'option_can_override', 'option_type', 'option_width', 'option_height', 'option_description', 'option_admin_level' );
  1073 	$wpdb->hide_errors();
  1181 	$wpdb->hide_errors();
  1077 }
  1185 }
  1078 
  1186 
  1079 /**
  1187 /**
  1080  * Remove old categories, link2cat, and post2cat database tables.
  1188  * Remove old categories, link2cat, and post2cat database tables.
  1081  *
  1189  *
       
  1190  * @ignore
  1082  * @since 2.3.0
  1191  * @since 2.3.0
       
  1192  *
       
  1193  * @global wpdb $wpdb WordPress database abstraction object.
  1083  */
  1194  */
  1084 function upgrade_230_old_tables() {
  1195 function upgrade_230_old_tables() {
  1085 	global $wpdb;
  1196 	global $wpdb;
  1086 	$wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'categories');
  1197 	$wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'categories');
  1087 	$wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'link2cat');
  1198 	$wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'link2cat');
  1089 }
  1200 }
  1090 
  1201 
  1091 /**
  1202 /**
  1092  * Upgrade old slugs made in version 2.2.
  1203  * Upgrade old slugs made in version 2.2.
  1093  *
  1204  *
       
  1205  * @ignore
  1094  * @since 2.2.0
  1206  * @since 2.2.0
       
  1207  *
       
  1208  * @global wpdb $wpdb WordPress database abstraction object.
  1095  */
  1209  */
  1096 function upgrade_old_slugs() {
  1210 function upgrade_old_slugs() {
  1097 	// Upgrade people who were using the Redirect Old Slugs plugin.
  1211 	// Upgrade people who were using the Redirect Old Slugs plugin.
  1098 	global $wpdb;
  1212 	global $wpdb;
  1099 	$wpdb->query("UPDATE $wpdb->postmeta SET meta_key = '_wp_old_slug' WHERE meta_key = 'old_slug'");
  1213 	$wpdb->query("UPDATE $wpdb->postmeta SET meta_key = '_wp_old_slug' WHERE meta_key = 'old_slug'");
  1100 }
  1214 }
  1101 
  1215 
  1102 /**
  1216 /**
  1103  * Execute changes made in WordPress 2.5.0.
  1217  * Execute changes made in WordPress 2.5.0.
  1104  *
  1218  *
       
  1219  * @ignore
  1105  * @since 2.5.0
  1220  * @since 2.5.0
       
  1221  *
       
  1222  * @global int $wp_current_db_version
  1106  */
  1223  */
  1107 function upgrade_250() {
  1224 function upgrade_250() {
  1108 	global $wp_current_db_version;
  1225 	global $wp_current_db_version;
  1109 
  1226 
  1110 	if ( $wp_current_db_version < 6689 ) {
  1227 	if ( $wp_current_db_version < 6689 ) {
  1114 }
  1231 }
  1115 
  1232 
  1116 /**
  1233 /**
  1117  * Execute changes made in WordPress 2.5.2.
  1234  * Execute changes made in WordPress 2.5.2.
  1118  *
  1235  *
       
  1236  * @ignore
  1119  * @since 2.5.2
  1237  * @since 2.5.2
       
  1238  *
       
  1239  * @global wpdb $wpdb WordPress database abstraction object.
  1120  */
  1240  */
  1121 function upgrade_252() {
  1241 function upgrade_252() {
  1122 	global $wpdb;
  1242 	global $wpdb;
  1123 
  1243 
  1124 	$wpdb->query("UPDATE $wpdb->users SET user_activation_key = ''");
  1244 	$wpdb->query("UPDATE $wpdb->users SET user_activation_key = ''");
  1125 }
  1245 }
  1126 
  1246 
  1127 /**
  1247 /**
  1128  * Execute changes made in WordPress 2.6.
  1248  * Execute changes made in WordPress 2.6.
  1129  *
  1249  *
       
  1250  * @ignore
  1130  * @since 2.6.0
  1251  * @since 2.6.0
       
  1252  *
       
  1253  * @global int $wp_current_db_version
  1131  */
  1254  */
  1132 function upgrade_260() {
  1255 function upgrade_260() {
  1133 	global $wp_current_db_version;
  1256 	global $wp_current_db_version;
  1134 
  1257 
  1135 	if ( $wp_current_db_version < 8000 )
  1258 	if ( $wp_current_db_version < 8000 )
  1137 }
  1260 }
  1138 
  1261 
  1139 /**
  1262 /**
  1140  * Execute changes made in WordPress 2.7.
  1263  * Execute changes made in WordPress 2.7.
  1141  *
  1264  *
       
  1265  * @ignore
  1142  * @since 2.7.0
  1266  * @since 2.7.0
       
  1267  *
       
  1268  * @global wpdb $wpdb WordPress database abstraction object.
       
  1269  * @global int  $wp_current_db_version
  1143  */
  1270  */
  1144 function upgrade_270() {
  1271 function upgrade_270() {
  1145 	global $wpdb, $wp_current_db_version;
  1272 	global $wpdb, $wp_current_db_version;
  1146 
  1273 
  1147 	if ( $wp_current_db_version < 8980 )
  1274 	if ( $wp_current_db_version < 8980 )
  1153 }
  1280 }
  1154 
  1281 
  1155 /**
  1282 /**
  1156  * Execute changes made in WordPress 2.8.
  1283  * Execute changes made in WordPress 2.8.
  1157  *
  1284  *
       
  1285  * @ignore
  1158  * @since 2.8.0
  1286  * @since 2.8.0
       
  1287  *
       
  1288  * @global int  $wp_current_db_version
       
  1289  * @global wpdb $wpdb WordPress database abstraction object.
  1159  */
  1290  */
  1160 function upgrade_280() {
  1291 function upgrade_280() {
  1161 	global $wp_current_db_version, $wpdb;
  1292 	global $wp_current_db_version, $wpdb;
  1162 
  1293 
  1163 	if ( $wp_current_db_version < 10360 )
  1294 	if ( $wp_current_db_version < 10360 )
  1164 		populate_roles_280();
  1295 		populate_roles_280();
  1165 	if ( is_multisite() ) {
  1296 	if ( is_multisite() ) {
  1166 		$start = 0;
  1297 		$start = 0;
  1167 		while( $rows = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options ORDER BY option_id LIMIT $start, 20" ) ) {
  1298 		while( $rows = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options ORDER BY option_id LIMIT $start, 20" ) ) {
  1168 			foreach( $rows as $row ) {
  1299 			foreach ( $rows as $row ) {
  1169 				$value = $row->option_value;
  1300 				$value = $row->option_value;
  1170 				if ( !@unserialize( $value ) )
  1301 				if ( !@unserialize( $value ) )
  1171 					$value = stripslashes( $value );
  1302 					$value = stripslashes( $value );
  1172 				if ( $value !== $row->option_value ) {
  1303 				if ( $value !== $row->option_value ) {
  1173 					update_option( $row->option_name, $value );
  1304 					update_option( $row->option_name, $value );
  1174 				}
  1305 				}
  1175 			}
  1306 			}
  1176 			$start += 20;
  1307 			$start += 20;
  1177 		}
  1308 		}
  1178 		refresh_blog_details( $wpdb->blogid );
  1309 		clean_blog_cache( get_current_blog_id() );
  1179 	}
  1310 	}
  1180 }
  1311 }
  1181 
  1312 
  1182 /**
  1313 /**
  1183  * Execute changes made in WordPress 2.9.
  1314  * Execute changes made in WordPress 2.9.
  1184  *
  1315  *
       
  1316  * @ignore
  1185  * @since 2.9.0
  1317  * @since 2.9.0
       
  1318  *
       
  1319  * @global int $wp_current_db_version
  1186  */
  1320  */
  1187 function upgrade_290() {
  1321 function upgrade_290() {
  1188 	global $wp_current_db_version;
  1322 	global $wp_current_db_version;
  1189 
  1323 
  1190 	if ( $wp_current_db_version < 11958 ) {
  1324 	if ( $wp_current_db_version < 11958 ) {
  1197 }
  1331 }
  1198 
  1332 
  1199 /**
  1333 /**
  1200  * Execute changes made in WordPress 3.0.
  1334  * Execute changes made in WordPress 3.0.
  1201  *
  1335  *
       
  1336  * @ignore
  1202  * @since 3.0.0
  1337  * @since 3.0.0
       
  1338  *
       
  1339  * @global int  $wp_current_db_version
       
  1340  * @global wpdb $wpdb WordPress database abstraction object.
  1203  */
  1341  */
  1204 function upgrade_300() {
  1342 function upgrade_300() {
  1205 	global $wp_current_db_version, $wpdb;
  1343 	global $wp_current_db_version, $wpdb;
  1206 
  1344 
  1207 	if ( $wp_current_db_version < 15093 )
  1345 	if ( $wp_current_db_version < 15093 )
  1209 
  1347 
  1210 	if ( $wp_current_db_version < 14139 && is_multisite() && is_main_site() && ! defined( 'MULTISITE' ) && get_site_option( 'siteurl' ) === false )
  1348 	if ( $wp_current_db_version < 14139 && is_multisite() && is_main_site() && ! defined( 'MULTISITE' ) && get_site_option( 'siteurl' ) === false )
  1211 		add_site_option( 'siteurl', '' );
  1349 		add_site_option( 'siteurl', '' );
  1212 
  1350 
  1213 	// 3.0 screen options key name changes.
  1351 	// 3.0 screen options key name changes.
  1214 	if ( is_main_site() && !defined('DO_NOT_UPGRADE_GLOBAL_TABLES') ) {
  1352 	if ( wp_should_upgrade_global_tables() ) {
  1215 		$sql = "DELETE FROM $wpdb->usermeta
  1353 		$sql = "DELETE FROM $wpdb->usermeta
  1216 			WHERE meta_key LIKE %s
  1354 			WHERE meta_key LIKE %s
  1217 			OR meta_key LIKE %s
  1355 			OR meta_key LIKE %s
  1218 			OR meta_key LIKE %s
  1356 			OR meta_key LIKE %s
  1219 			OR meta_key LIKE %s
  1357 			OR meta_key LIKE %s
  1239 }
  1377 }
  1240 
  1378 
  1241 /**
  1379 /**
  1242  * Execute changes made in WordPress 3.3.
  1380  * Execute changes made in WordPress 3.3.
  1243  *
  1381  *
       
  1382  * @ignore
  1244  * @since 3.3.0
  1383  * @since 3.3.0
       
  1384  *
       
  1385  * @global int   $wp_current_db_version
       
  1386  * @global wpdb  $wpdb
       
  1387  * @global array $wp_registered_widgets
       
  1388  * @global array $sidebars_widgets
  1245  */
  1389  */
  1246 function upgrade_330() {
  1390 function upgrade_330() {
  1247 	global $wp_current_db_version, $wpdb, $wp_registered_widgets, $sidebars_widgets;
  1391 	global $wp_current_db_version, $wpdb, $wp_registered_widgets, $sidebars_widgets;
  1248 
  1392 
  1249 	if ( $wp_current_db_version < 19061 && is_main_site() && ! defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) {
  1393 	if ( $wp_current_db_version < 19061 && wp_should_upgrade_global_tables() ) {
  1250 		$wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key IN ('show_admin_bar_admin', 'plugins_last_view')" );
  1394 		$wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key IN ('show_admin_bar_admin', 'plugins_last_view')" );
  1251 	}
  1395 	}
  1252 
  1396 
  1253 	if ( $wp_current_db_version >= 11548 )
  1397 	if ( $wp_current_db_version >= 11548 )
  1254 		return;
  1398 		return;
  1308 }
  1452 }
  1309 
  1453 
  1310 /**
  1454 /**
  1311  * Execute changes made in WordPress 3.4.
  1455  * Execute changes made in WordPress 3.4.
  1312  *
  1456  *
       
  1457  * @ignore
  1313  * @since 3.4.0
  1458  * @since 3.4.0
       
  1459  *
       
  1460  * @global int   $wp_current_db_version
       
  1461  * @global wpdb  $wpdb
  1314  */
  1462  */
  1315 function upgrade_340() {
  1463 function upgrade_340() {
  1316 	global $wp_current_db_version, $wpdb;
  1464 	global $wp_current_db_version, $wpdb;
  1317 
  1465 
  1318 	if ( $wp_current_db_version < 19798 ) {
  1466 	if ( $wp_current_db_version < 19798 ) {
  1325 		$wpdb->hide_errors();
  1473 		$wpdb->hide_errors();
  1326 		$wpdb->query("ALTER TABLE $wpdb->comments DROP INDEX comment_approved");
  1474 		$wpdb->query("ALTER TABLE $wpdb->comments DROP INDEX comment_approved");
  1327 		$wpdb->show_errors();
  1475 		$wpdb->show_errors();
  1328 	}
  1476 	}
  1329 
  1477 
  1330 	if ( $wp_current_db_version < 20022 && is_main_site() && ! defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) {
  1478 	if ( $wp_current_db_version < 20022 && wp_should_upgrade_global_tables() ) {
  1331 		$wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key = 'themes_last_view'" );
  1479 		$wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key = 'themes_last_view'" );
  1332 	}
  1480 	}
  1333 
  1481 
  1334 	if ( $wp_current_db_version < 20080 ) {
  1482 	if ( $wp_current_db_version < 20080 ) {
  1335 		if ( 'yes' == $wpdb->get_var( "SELECT autoload FROM $wpdb->options WHERE option_name = 'uninstall_plugins'" ) ) {
  1483 		if ( 'yes' == $wpdb->get_var( "SELECT autoload FROM $wpdb->options WHERE option_name = 'uninstall_plugins'" ) ) {
  1341 }
  1489 }
  1342 
  1490 
  1343 /**
  1491 /**
  1344  * Execute changes made in WordPress 3.5.
  1492  * Execute changes made in WordPress 3.5.
  1345  *
  1493  *
       
  1494  * @ignore
  1346  * @since 3.5.0
  1495  * @since 3.5.0
       
  1496  *
       
  1497  * @global int   $wp_current_db_version
       
  1498  * @global wpdb  $wpdb
  1347  */
  1499  */
  1348 function upgrade_350() {
  1500 function upgrade_350() {
  1349 	global $wp_current_db_version, $wpdb;
  1501 	global $wp_current_db_version, $wpdb;
  1350 
  1502 
  1351 	if ( $wp_current_db_version < 22006 && $wpdb->get_var( "SELECT link_id FROM $wpdb->links LIMIT 1" ) )
  1503 	if ( $wp_current_db_version < 22006 && $wpdb->get_var( "SELECT link_id FROM $wpdb->links LIMIT 1" ) )
  1352 		update_option( 'link_manager_enabled', 1 ); // Previously set to 0 by populate_options()
  1504 		update_option( 'link_manager_enabled', 1 ); // Previously set to 0 by populate_options()
  1353 
  1505 
  1354 	if ( $wp_current_db_version < 21811 && is_main_site() && ! defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) {
  1506 	if ( $wp_current_db_version < 21811 && wp_should_upgrade_global_tables() ) {
  1355 		$meta_keys = array();
  1507 		$meta_keys = array();
  1356 		foreach ( array_merge( get_post_types(), get_taxonomies() ) as $name ) {
  1508 		foreach ( array_merge( get_post_types(), get_taxonomies() ) as $name ) {
  1357 			if ( false !== strpos( $name, '-' ) )
  1509 			if ( false !== strpos( $name, '-' ) )
  1358 			$meta_keys[] = 'edit_' . str_replace( '-', '_', $name ) . '_per_page';
  1510 			$meta_keys[] = 'edit_' . str_replace( '-', '_', $name ) . '_per_page';
  1359 		}
  1511 		}
  1368 }
  1520 }
  1369 
  1521 
  1370 /**
  1522 /**
  1371  * Execute changes made in WordPress 3.7.
  1523  * Execute changes made in WordPress 3.7.
  1372  *
  1524  *
       
  1525  * @ignore
  1373  * @since 3.7.0
  1526  * @since 3.7.0
       
  1527  *
       
  1528  * @global int $wp_current_db_version
  1374  */
  1529  */
  1375 function upgrade_370() {
  1530 function upgrade_370() {
  1376 	global $wp_current_db_version;
  1531 	global $wp_current_db_version;
  1377 	if ( $wp_current_db_version < 25824 )
  1532 	if ( $wp_current_db_version < 25824 )
  1378 		wp_clear_scheduled_hook( 'wp_auto_updates_maybe_update' );
  1533 		wp_clear_scheduled_hook( 'wp_auto_updates_maybe_update' );
  1379 }
  1534 }
  1380 
  1535 
  1381 /**
  1536 /**
  1382  * Execute changes made in WordPress 3.7.2.
  1537  * Execute changes made in WordPress 3.7.2.
  1383  *
  1538  *
       
  1539  * @ignore
  1384  * @since 3.7.2
  1540  * @since 3.7.2
  1385  * @since 3.8.0
  1541  * @since 3.8.0
       
  1542  *
       
  1543  * @global int $wp_current_db_version
  1386  */
  1544  */
  1387 function upgrade_372() {
  1545 function upgrade_372() {
  1388 	global $wp_current_db_version;
  1546 	global $wp_current_db_version;
  1389 	if ( $wp_current_db_version < 26148 )
  1547 	if ( $wp_current_db_version < 26148 )
  1390 		wp_clear_scheduled_hook( 'wp_maybe_auto_update' );
  1548 		wp_clear_scheduled_hook( 'wp_maybe_auto_update' );
  1391 }
  1549 }
  1392 
  1550 
  1393 /**
  1551 /**
  1394  * Execute changes made in WordPress 3.8.0.
  1552  * Execute changes made in WordPress 3.8.0.
  1395  *
  1553  *
       
  1554  * @ignore
  1396  * @since 3.8.0
  1555  * @since 3.8.0
       
  1556  *
       
  1557  * @global int $wp_current_db_version
  1397  */
  1558  */
  1398 function upgrade_380() {
  1559 function upgrade_380() {
  1399 	global $wp_current_db_version;
  1560 	global $wp_current_db_version;
  1400 	if ( $wp_current_db_version < 26691 ) {
  1561 	if ( $wp_current_db_version < 26691 ) {
  1401 		deactivate_plugins( array( 'mp6/mp6.php' ), true );
  1562 		deactivate_plugins( array( 'mp6/mp6.php' ), true );
  1403 }
  1564 }
  1404 
  1565 
  1405 /**
  1566 /**
  1406  * Execute changes made in WordPress 4.0.0.
  1567  * Execute changes made in WordPress 4.0.0.
  1407  *
  1568  *
       
  1569  * @ignore
  1408  * @since 4.0.0
  1570  * @since 4.0.0
       
  1571  *
       
  1572  * @global int $wp_current_db_version
  1409  */
  1573  */
  1410 function upgrade_400() {
  1574 function upgrade_400() {
  1411 	global $wp_current_db_version;
  1575 	global $wp_current_db_version;
  1412 	if ( $wp_current_db_version < 29630 ) {
  1576 	if ( $wp_current_db_version < 29630 ) {
  1413 		if ( ! is_multisite() && false === get_option( 'WPLANG' ) ) {
  1577 		if ( ! is_multisite() && false === get_option( 'WPLANG' ) ) {
  1421 }
  1585 }
  1422 
  1586 
  1423 /**
  1587 /**
  1424  * Execute changes made in WordPress 4.2.0.
  1588  * Execute changes made in WordPress 4.2.0.
  1425  *
  1589  *
       
  1590  * @ignore
  1426  * @since 4.2.0
  1591  * @since 4.2.0
  1427  */
  1592  *
  1428 function upgrade_420() {
  1593  * @global int   $wp_current_db_version
       
  1594  * @global wpdb  $wpdb
       
  1595  */
       
  1596 function upgrade_420() {}
       
  1597 
       
  1598 /**
       
  1599  * Executes changes made in WordPress 4.3.0.
       
  1600  *
       
  1601  * @ignore
       
  1602  * @since 4.3.0
       
  1603  *
       
  1604  * @global int  $wp_current_db_version Current version.
       
  1605  * @global wpdb $wpdb                  WordPress database abstraction object.
       
  1606  */
       
  1607 function upgrade_430() {
  1429 	global $wp_current_db_version, $wpdb;
  1608 	global $wp_current_db_version, $wpdb;
  1430 
  1609 
  1431 	if ( $wp_current_db_version < 31351 && $wpdb->charset === 'utf8mb4' ) {
  1610 	if ( $wp_current_db_version < 32364 ) {
       
  1611 		upgrade_430_fix_comments();
       
  1612 	}
       
  1613 
       
  1614 	// Shared terms are split in a separate process.
       
  1615 	if ( $wp_current_db_version < 32814 ) {
       
  1616 		update_option( 'finished_splitting_shared_terms', 0 );
       
  1617 		wp_schedule_single_event( time() + ( 1 * MINUTE_IN_SECONDS ), 'wp_split_shared_term_batch' );
       
  1618 	}
       
  1619 
       
  1620 	if ( $wp_current_db_version < 33055 && 'utf8mb4' === $wpdb->charset ) {
  1432 		if ( is_multisite() ) {
  1621 		if ( is_multisite() ) {
  1433 			$tables = $wpdb->tables( 'blog' );
  1622 			$tables = $wpdb->tables( 'blog' );
  1434 		} else {
  1623 		} else {
  1435 			$tables = $wpdb->tables( 'all' );
  1624 			$tables = $wpdb->tables( 'all' );
       
  1625 			if ( ! wp_should_upgrade_global_tables() ) {
       
  1626 				$global_tables = $wpdb->tables( 'global' );
       
  1627 				$tables = array_diff_assoc( $tables, $global_tables );
       
  1628 			}
  1436 		}
  1629 		}
  1437 
  1630 
  1438 		foreach ( $tables as $table ) {
  1631 		foreach ( $tables as $table ) {
  1439 			maybe_convert_table_to_utf8mb4( $table );
  1632 			maybe_convert_table_to_utf8mb4( $table );
  1440 		}
  1633 		}
  1441 	}
  1634 	}
  1442 }
  1635 }
  1443 
  1636 
  1444 /**
  1637 /**
  1445  * Execute changes made in WordPress 4.2.1.
  1638  * Executes comments changes made in WordPress 4.3.0.
  1446  *
  1639  *
  1447  * @since 4.2.1
  1640  * @ignore
  1448  */
  1641  * @since 4.3.0
  1449 function upgrade_421() {
  1642  *
  1450 }
  1643  * @global int  $wp_current_db_version Current version.
  1451 
  1644  * @global wpdb $wpdb                  WordPress database abstraction object.
  1452 /**
  1645  */
  1453  * Execute changes made in WordPress 4.2.2.
  1646 function upgrade_430_fix_comments() {
  1454  *
       
  1455  * @since 4.2.2
       
  1456  */
       
  1457 function upgrade_422() {
       
  1458 	global $wp_current_db_version, $wpdb;
  1647 	global $wp_current_db_version, $wpdb;
  1459 
  1648 
  1460 	if ( $wp_current_db_version < 31534 ) {
  1649 	$content_length = $wpdb->get_col_length( $wpdb->comments, 'comment_content' );
  1461 		$content_length = $wpdb->get_col_length( $wpdb->comments, 'comment_content' );
  1650 
  1462 
  1651 	if ( is_wp_error( $content_length ) ) {
  1463 		if ( is_wp_error( $content_length ) ) {
  1652 		return;
  1464 			return;
  1653 	}
  1465 		}
  1654 
  1466 
  1655 	if ( false === $content_length ) {
  1467 		if ( false === $content_length ) {
  1656 		$content_length = array(
  1468 			$content_length = array(
  1657 			'type'   => 'byte',
  1469 				'type'   => 'byte',
  1658 			'length' => 65535,
  1470 				'length' => 65535,
       
  1471 			);
       
  1472 		} elseif ( ! is_array( $content_length ) ) {
       
  1473 			$length = (int) $content_length > 0 ? (int) $content_length : 65535;
       
  1474 			$content_length = array(
       
  1475 				'type'	 => 'byte',
       
  1476 				'length' => $length
       
  1477 			);
       
  1478 		}
       
  1479 
       
  1480 		if ( 'byte' !== $content_length['type'] || 0 === $content_length['length'] ) {
       
  1481 			// Sites with malformed DB schemas are on their own.
       
  1482 			return;
       
  1483 		}
       
  1484 
       
  1485 		$allowed_length = intval( $content_length['length'] ) - 10;
       
  1486 
       
  1487 		$comments = $wpdb->get_results(
       
  1488 			"SELECT `comment_ID` FROM `{$wpdb->comments}`
       
  1489 				WHERE `comment_date_gmt` > '2015-04-26'
       
  1490 				AND LENGTH( `comment_content` ) >= {$allowed_length}
       
  1491 				AND ( `comment_content` LIKE '%<%' OR `comment_content` LIKE '%>%' )"
       
  1492 		);
  1659 		);
  1493 
  1660 	} elseif ( ! is_array( $content_length ) ) {
  1494 		foreach ( $comments as $comment ) {
  1661 		$length = (int) $content_length > 0 ? (int) $content_length : 65535;
  1495 			wp_delete_comment( $comment->comment_ID, true );
  1662 		$content_length = array(
       
  1663 			'type'	 => 'byte',
       
  1664 			'length' => $length
       
  1665 		);
       
  1666 	}
       
  1667 
       
  1668 	if ( 'byte' !== $content_length['type'] || 0 === $content_length['length'] ) {
       
  1669 		// Sites with malformed DB schemas are on their own.
       
  1670 		return;
       
  1671 	}
       
  1672 
       
  1673 	$allowed_length = intval( $content_length['length'] ) - 10;
       
  1674 
       
  1675 	$comments = $wpdb->get_results(
       
  1676 		"SELECT `comment_ID` FROM `{$wpdb->comments}`
       
  1677 			WHERE `comment_date_gmt` > '2015-04-26'
       
  1678 			AND LENGTH( `comment_content` ) >= {$allowed_length}
       
  1679 			AND ( `comment_content` LIKE '%<%' OR `comment_content` LIKE '%>%' )"
       
  1680 	);
       
  1681 
       
  1682 	foreach ( $comments as $comment ) {
       
  1683 		wp_delete_comment( $comment->comment_ID, true );
       
  1684 	}
       
  1685 }
       
  1686 
       
  1687 /**
       
  1688  * Executes changes made in WordPress 4.3.1.
       
  1689  *
       
  1690  * @ignore
       
  1691  * @since 4.3.1
       
  1692  */
       
  1693 function upgrade_431() {
       
  1694 	// Fix incorrect cron entries for term splitting
       
  1695 	$cron_array = _get_cron_array();
       
  1696 	if ( isset( $cron_array['wp_batch_split_terms'] ) ) {
       
  1697 		unset( $cron_array['wp_batch_split_terms'] );
       
  1698 		_set_cron_array( $cron_array );
       
  1699 	}
       
  1700 }
       
  1701 
       
  1702 /**
       
  1703  * Executes changes made in WordPress 4.4.0.
       
  1704  *
       
  1705  * @ignore
       
  1706  * @since 4.4.0
       
  1707  *
       
  1708  * @global int  $wp_current_db_version Current version.
       
  1709  * @global wpdb $wpdb                  WordPress database abstraction object.
       
  1710  */
       
  1711 function upgrade_440() {
       
  1712 	global $wp_current_db_version, $wpdb;
       
  1713 
       
  1714 	if ( $wp_current_db_version < 34030 ) {
       
  1715 		$wpdb->query( "ALTER TABLE {$wpdb->options} MODIFY option_name VARCHAR(191)" );
       
  1716 	}
       
  1717 
       
  1718 	// Remove the unused 'add_users' role.
       
  1719 	$roles = wp_roles();
       
  1720 	foreach ( $roles->role_objects as $role ) {
       
  1721 		if ( $role->has_cap( 'add_users' ) ) {
       
  1722 			$role->remove_cap( 'add_users' );
       
  1723 		}
       
  1724 	}
       
  1725 }
       
  1726 
       
  1727 /**
       
  1728  * Executes changes made in WordPress 4.5.0.
       
  1729  *
       
  1730  * @ignore
       
  1731  * @since 4.5.0
       
  1732  *
       
  1733  * @global int  $wp_current_db_version Current database version.
       
  1734  * @global wpdb $wpdb                  WordPress database abstraction object.
       
  1735  */
       
  1736 function upgrade_450() {
       
  1737 	global $wp_current_db_version, $wpdb;
       
  1738 
       
  1739 	if ( $wp_current_db_version < 36180 ) {
       
  1740 		wp_clear_scheduled_hook( 'wp_maybe_auto_update' );
       
  1741 	}
       
  1742 
       
  1743 	// Remove unused email confirmation options, moved to usermeta.
       
  1744 	if ( $wp_current_db_version < 36679 && is_multisite() ) {
       
  1745 		$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name REGEXP '^[0-9]+_new_email$'" );
       
  1746 	}
       
  1747 
       
  1748 	// Remove unused user setting for wpLink.
       
  1749 	delete_user_setting( 'wplink' );
       
  1750 }
       
  1751 
       
  1752 /**
       
  1753  * Executes changes made in WordPress 4.6.0.
       
  1754  *
       
  1755  * @ignore
       
  1756  * @since 4.6.0
       
  1757  *
       
  1758  * @global int $wp_current_db_version Current database version.
       
  1759  */
       
  1760 function upgrade_460() {
       
  1761 	global $wp_current_db_version;
       
  1762 
       
  1763 	// Remove unused post meta.
       
  1764 	if ( $wp_current_db_version < 37854 ) {
       
  1765 		delete_post_meta_by_key( '_post_restored_from' );
       
  1766 	}
       
  1767 
       
  1768 	// Remove plugins with callback as an array object/method as the uninstall hook, see #13786.
       
  1769 	if ( $wp_current_db_version < 37965 ) {
       
  1770 		$uninstall_plugins = get_option( 'uninstall_plugins', array() );
       
  1771 
       
  1772 		if ( ! empty( $uninstall_plugins ) ) {
       
  1773 			foreach ( $uninstall_plugins as $basename => $callback ) {
       
  1774 				if ( is_array( $callback ) && is_object( $callback[0] ) ) {
       
  1775 					unset( $uninstall_plugins[ $basename ] );
       
  1776 				}
       
  1777 			}
       
  1778 
       
  1779 			update_option( 'uninstall_plugins', $uninstall_plugins );
  1496 		}
  1780 		}
  1497 	}
  1781 	}
  1498 }
  1782 }
  1499 
  1783 
  1500 /**
  1784 /**
  1501  * Executes network-level upgrade routines.
  1785  * Executes network-level upgrade routines.
  1502  *
  1786  *
  1503  * @since 3.0.0
  1787  * @since 3.0.0
       
  1788  *
       
  1789  * @global int   $wp_current_db_version
       
  1790  * @global wpdb  $wpdb
  1504  */
  1791  */
  1505 function upgrade_network() {
  1792 function upgrade_network() {
  1506 	global $wp_current_db_version, $wpdb;
  1793 	global $wp_current_db_version, $wpdb;
  1507 
  1794 
  1508 	// Always.
  1795 	// Always clear expired transients
  1509 	if ( is_main_network() ) {
  1796 	delete_expired_transients( true );
  1510 		/*
       
  1511 		 * Deletes all expired transients. The multi-table delete syntax is used
       
  1512 		 * to delete the transient record from table a, and the corresponding
       
  1513 		 * transient_timeout record from table b.
       
  1514 		 */
       
  1515 		$time = time();
       
  1516 		$sql = "DELETE a, b FROM $wpdb->sitemeta a, $wpdb->sitemeta b
       
  1517 			WHERE a.meta_key LIKE %s
       
  1518 			AND a.meta_key NOT LIKE %s
       
  1519 			AND b.meta_key = CONCAT( '_site_transient_timeout_', SUBSTRING( a.meta_key, 17 ) )
       
  1520 			AND b.meta_value < %d";
       
  1521 		$wpdb->query( $wpdb->prepare( $sql, $wpdb->esc_like( '_site_transient_' ) . '%', $wpdb->esc_like ( '_site_transient_timeout_' ) . '%', $time ) );
       
  1522 	}
       
  1523 
  1797 
  1524 	// 2.8.
  1798 	// 2.8.
  1525 	if ( $wp_current_db_version < 11549 ) {
  1799 	if ( $wp_current_db_version < 11549 ) {
  1526 		$wpmu_sitewide_plugins = get_site_option( 'wpmu_sitewide_plugins' );
  1800 		$wpmu_sitewide_plugins = get_site_option( 'wpmu_sitewide_plugins' );
  1527 		$active_sitewide_plugins = get_site_option( 'active_sitewide_plugins' );
  1801 		$active_sitewide_plugins = get_site_option( 'active_sitewide_plugins' );
  1536 		delete_site_option( 'wpmu_sitewide_plugins' );
  1810 		delete_site_option( 'wpmu_sitewide_plugins' );
  1537 		delete_site_option( 'deactivated_sitewide_plugins' );
  1811 		delete_site_option( 'deactivated_sitewide_plugins' );
  1538 
  1812 
  1539 		$start = 0;
  1813 		$start = 0;
  1540 		while( $rows = $wpdb->get_results( "SELECT meta_key, meta_value FROM {$wpdb->sitemeta} ORDER BY meta_id LIMIT $start, 20" ) ) {
  1814 		while( $rows = $wpdb->get_results( "SELECT meta_key, meta_value FROM {$wpdb->sitemeta} ORDER BY meta_id LIMIT $start, 20" ) ) {
  1541 			foreach( $rows as $row ) {
  1815 			foreach ( $rows as $row ) {
  1542 				$value = $row->meta_value;
  1816 				$value = $row->meta_value;
  1543 				if ( !@unserialize( $value ) )
  1817 				if ( !@unserialize( $value ) )
  1544 					$value = stripslashes( $value );
  1818 					$value = stripslashes( $value );
  1545 				if ( $value !== $row->meta_value ) {
  1819 				if ( $value !== $row->meta_value ) {
  1546 					update_site_option( $row->meta_key, $value );
  1820 					update_site_option( $row->meta_key, $value );
  1594 		}
  1868 		}
  1595 	}
  1869 	}
  1596 
  1870 
  1597 	// 4.2
  1871 	// 4.2
  1598 	if ( $wp_current_db_version < 31351 && $wpdb->charset === 'utf8mb4' ) {
  1872 	if ( $wp_current_db_version < 31351 && $wpdb->charset === 'utf8mb4' ) {
  1599 		if ( ! ( defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) && DO_NOT_UPGRADE_GLOBAL_TABLES ) ) {
  1873 		if ( wp_should_upgrade_global_tables() ) {
  1600 			$wpdb->query( "ALTER TABLE $wpdb->usermeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
  1874 			$wpdb->query( "ALTER TABLE $wpdb->usermeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
  1601 			$wpdb->query( "ALTER TABLE $wpdb->site DROP INDEX domain, ADD INDEX domain(domain(140),path(51))" );
  1875 			$wpdb->query( "ALTER TABLE $wpdb->site DROP INDEX domain, ADD INDEX domain(domain(140),path(51))" );
  1602 			$wpdb->query( "ALTER TABLE $wpdb->sitemeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
  1876 			$wpdb->query( "ALTER TABLE $wpdb->sitemeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
  1603 			$wpdb->query( "ALTER TABLE $wpdb->signups DROP INDEX domain_path, ADD INDEX domain_path(domain(140),path(51))" );
  1877 			$wpdb->query( "ALTER TABLE $wpdb->signups DROP INDEX domain_path, ADD INDEX domain_path(domain(140),path(51))" );
  1604 
  1878 
  1605 			$tables = $wpdb->tables( 'global' );
  1879 			$tables = $wpdb->tables( 'global' );
  1606 
  1880 
       
  1881 			// sitecategories may not exist.
       
  1882 			if ( ! $wpdb->get_var( "SHOW TABLES LIKE '{$tables['sitecategories']}'" ) ) {
       
  1883 				unset( $tables['sitecategories'] );
       
  1884 			}
       
  1885 
  1607 			foreach ( $tables as $table ) {
  1886 			foreach ( $tables as $table ) {
  1608 				maybe_convert_table_to_utf8mb4( $table );
  1887 				maybe_convert_table_to_utf8mb4( $table );
  1609 			}
  1888 			}
  1610 		}
  1889 		}
  1611 	}
  1890 	}
  1612 
  1891 
  1613 	// 4.2.2
  1892 	// 4.3
  1614 	if ( $wp_current_db_version < 31535 && 'utf8mb4' === $wpdb->charset ) {
  1893 	if ( $wp_current_db_version < 33055 && 'utf8mb4' === $wpdb->charset ) {
  1615 		if ( ! ( defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) && DO_NOT_UPGRADE_GLOBAL_TABLES ) ) {
  1894 		if ( wp_should_upgrade_global_tables() ) {
  1616 			$upgrade = false;
  1895 			$upgrade = false;
  1617 			$indexes = $wpdb->get_results( "SHOW INDEXES FROM $wpdb->signups" );
  1896 			$indexes = $wpdb->get_results( "SHOW INDEXES FROM $wpdb->signups" );
  1618 			foreach( $indexes as $index ) {
  1897 			foreach ( $indexes as $index ) {
  1619 				if ( 'domain_path' == $index->Key_name && 'domain' == $index->Column_name && 140 != $index->Sub_part ) {
  1898 				if ( 'domain_path' == $index->Key_name && 'domain' == $index->Column_name && 140 != $index->Sub_part ) {
  1620 					$upgrade = true;
  1899 					$upgrade = true;
  1621 					break;
  1900 					break;
  1622 				}
  1901 				}
  1623 			}
  1902 			}
  1624 
  1903 
  1625 			if ( $upgrade ) {
  1904 			if ( $upgrade ) {
  1626 				$wpdb->query( "ALTER TABLE $wpdb->signups DROP INDEX domain_path, ADD INDEX domain_path(domain(140),path(51))" );
  1905 				$wpdb->query( "ALTER TABLE $wpdb->signups DROP INDEX domain_path, ADD INDEX domain_path(domain(140),path(51))" );
  1627 			}
  1906 			}
       
  1907 
       
  1908 			$tables = $wpdb->tables( 'global' );
       
  1909 
       
  1910 			// sitecategories may not exist.
       
  1911 			if ( ! $wpdb->get_var( "SHOW TABLES LIKE '{$tables['sitecategories']}'" ) ) {
       
  1912 				unset( $tables['sitecategories'] );
       
  1913 			}
       
  1914 
       
  1915 			foreach ( $tables as $table ) {
       
  1916 				maybe_convert_table_to_utf8mb4( $table );
       
  1917 			}
  1628 		}
  1918 		}
  1629 	}
  1919 	}
  1630 }
  1920 }
  1631 
  1921 
  1632 //
  1922 //
  1640  * already present. It doesn't rely on MySQL's "IF NOT EXISTS" statement, but chooses
  1930  * already present. It doesn't rely on MySQL's "IF NOT EXISTS" statement, but chooses
  1641  * to query all tables first and then run the SQL statement creating the table.
  1931  * to query all tables first and then run the SQL statement creating the table.
  1642  *
  1932  *
  1643  * @since 1.0.0
  1933  * @since 1.0.0
  1644  *
  1934  *
       
  1935  * @global wpdb  $wpdb
       
  1936  *
  1645  * @param string $table_name Database table name to create.
  1937  * @param string $table_name Database table name to create.
  1646  * @param string $create_ddl SQL statement to create table.
  1938  * @param string $create_ddl SQL statement to create table.
  1647  * @return bool If table already exists or was created by function.
  1939  * @return bool If table already exists or was created by function.
  1648  */
  1940  */
  1649 function maybe_create_table($table_name, $create_ddl) {
  1941 function maybe_create_table($table_name, $create_ddl) {
  1668 /**
  1960 /**
  1669  * Drops a specified index from a table.
  1961  * Drops a specified index from a table.
  1670  *
  1962  *
  1671  * @since 1.0.1
  1963  * @since 1.0.1
  1672  *
  1964  *
       
  1965  * @global wpdb  $wpdb
       
  1966  *
  1673  * @param string $table Database table name.
  1967  * @param string $table Database table name.
  1674  * @param string $index Index name to drop.
  1968  * @param string $index Index name to drop.
  1675  * @return bool True, when finished.
  1969  * @return true True, when finished.
  1676  */
  1970  */
  1677 function drop_index($table, $index) {
  1971 function drop_index($table, $index) {
  1678 	global $wpdb;
  1972 	global $wpdb;
  1679 	$wpdb->hide_errors();
  1973 	$wpdb->hide_errors();
  1680 	$wpdb->query("ALTER TABLE `$table` DROP INDEX `$index`");
  1974 	$wpdb->query("ALTER TABLE `$table` DROP INDEX `$index`");
  1689 /**
  1983 /**
  1690  * Adds an index to a specified table.
  1984  * Adds an index to a specified table.
  1691  *
  1985  *
  1692  * @since 1.0.1
  1986  * @since 1.0.1
  1693  *
  1987  *
       
  1988  * @global wpdb  $wpdb
       
  1989  *
  1694  * @param string $table Database table name.
  1990  * @param string $table Database table name.
  1695  * @param string $index Database table index column.
  1991  * @param string $index Database table index column.
  1696  * @return bool True, when done with execution.
  1992  * @return true True, when done with execution.
  1697  */
  1993  */
  1698 function add_clean_index($table, $index) {
  1994 function add_clean_index($table, $index) {
  1699 	global $wpdb;
  1995 	global $wpdb;
  1700 	drop_index($table, $index);
  1996 	drop_index($table, $index);
  1701 	$wpdb->query("ALTER TABLE `$table` ADD INDEX ( `$index` )");
  1997 	$wpdb->query("ALTER TABLE `$table` ADD INDEX ( `$index` )");
  1705 /**
  2001 /**
  1706  * Adds column to a database table if it doesn't already exist.
  2002  * Adds column to a database table if it doesn't already exist.
  1707  *
  2003  *
  1708  * @since 1.3.0
  2004  * @since 1.3.0
  1709  *
  2005  *
       
  2006  * @global wpdb  $wpdb
       
  2007  *
  1710  * @param string $table_name  The table name to modify.
  2008  * @param string $table_name  The table name to modify.
  1711  * @param string $column_name The column name to add to the table.
  2009  * @param string $column_name The column name to add to the table.
  1712  * @param string $create_ddl  The SQL statement used to add the column.
  2010  * @param string $create_ddl  The SQL statement used to add the column.
  1713  * @return True if already exists or on successful completion, false on error.
  2011  * @return bool True if already exists or on successful completion, false on error.
  1714  */
  2012  */
  1715 function maybe_add_column($table_name, $column_name, $create_ddl) {
  2013 function maybe_add_column($table_name, $column_name, $create_ddl) {
  1716 	global $wpdb;
  2014 	global $wpdb;
  1717 	foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) {
  2015 	foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) {
  1718 		if ($column == $column_name) {
  2016 		if ($column == $column_name) {
  1734 
  2032 
  1735 /**
  2033 /**
  1736  * If a table only contains utf8 or utf8mb4 columns, convert it to utf8mb4.
  2034  * If a table only contains utf8 or utf8mb4 columns, convert it to utf8mb4.
  1737  *
  2035  *
  1738  * @since 4.2.0
  2036  * @since 4.2.0
       
  2037  *
       
  2038  * @global wpdb  $wpdb
  1739  *
  2039  *
  1740  * @param string $table The table to convert.
  2040  * @param string $table The table to convert.
  1741  * @return bool true if the table was converted, false if it wasn't.
  2041  * @return bool true if the table was converted, false if it wasn't.
  1742  */
  2042  */
  1743 function maybe_convert_table_to_utf8mb4( $table ) {
  2043 function maybe_convert_table_to_utf8mb4( $table ) {
  1757 				return false;
  2057 				return false;
  1758 			}
  2058 			}
  1759 		}
  2059 		}
  1760 	}
  2060 	}
  1761 
  2061 
       
  2062 	$table_details = $wpdb->get_row( "SHOW TABLE STATUS LIKE '$table'" );
       
  2063 	if ( ! $table_details ) {
       
  2064 		return false;
       
  2065 	}
       
  2066 
       
  2067 	list( $table_charset ) = explode( '_', $table_details->Collation );
       
  2068 	$table_charset = strtolower( $table_charset );
       
  2069 	if ( 'utf8mb4' === $table_charset ) {
       
  2070 		return true;
       
  2071 	}
       
  2072 
  1762 	return $wpdb->query( "ALTER TABLE $table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci" );
  2073 	return $wpdb->query( "ALTER TABLE $table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci" );
  1763 }
  2074 }
  1764 
  2075 
  1765 /**
  2076 /**
  1766  * Retrieve all options as it was for 1.2.
  2077  * Retrieve all options as it was for 1.2.
  1767  *
  2078  *
  1768  * @since 1.2.0
  2079  * @since 1.2.0
       
  2080  *
       
  2081  * @global wpdb  $wpdb
  1769  *
  2082  *
  1770  * @return stdClass List of options.
  2083  * @return stdClass List of options.
  1771  */
  2084  */
  1772 function get_alloptions_110() {
  2085 function get_alloptions_110() {
  1773 	global $wpdb;
  2086 	global $wpdb;
  1781 	}
  2094 	}
  1782 	return $all_options;
  2095 	return $all_options;
  1783 }
  2096 }
  1784 
  2097 
  1785 /**
  2098 /**
  1786  * Utility version of get_option that is private to install/upgrade.
  2099  * Utility version of get_option that is private to installation/upgrade.
  1787  *
  2100  *
  1788  * @ignore
  2101  * @ignore
  1789  * @since 1.5.1
  2102  * @since 1.5.1
  1790  * @access private
  2103  * @access private
       
  2104  *
       
  2105  * @global wpdb  $wpdb
  1791  *
  2106  *
  1792  * @param string $setting Option name.
  2107  * @param string $setting Option name.
  1793  * @return mixed
  2108  * @return mixed
  1794  */
  2109  */
  1795 function __get_option($setting) {
  2110 function __get_option($setting) {
  1845  * Modifies the database based on specified SQL statements.
  2160  * Modifies the database based on specified SQL statements.
  1846  *
  2161  *
  1847  * Useful for creating new tables and updating existing tables to a new structure.
  2162  * Useful for creating new tables and updating existing tables to a new structure.
  1848  *
  2163  *
  1849  * @since 1.5.0
  2164  * @since 1.5.0
       
  2165  *
       
  2166  * @global wpdb  $wpdb
  1850  *
  2167  *
  1851  * @param string|array $queries Optional. The query to run. Can be multiple queries
  2168  * @param string|array $queries Optional. The query to run. Can be multiple queries
  1852  *                              in an array, or a string of queries separated by
  2169  *                              in an array, or a string of queries separated by
  1853  *                              semicolons. Default empty.
  2170  *                              semicolons. Default empty.
  1854  * @param bool         $execute Optional. Whether or not to execute the query right away.
  2171  * @param bool         $execute Optional. Whether or not to execute the query right away.
  1866 		$queries = explode( ';', $queries );
  2183 		$queries = explode( ';', $queries );
  1867 		$queries = array_filter( $queries );
  2184 		$queries = array_filter( $queries );
  1868 	}
  2185 	}
  1869 
  2186 
  1870 	/**
  2187 	/**
  1871 	 * Filter the dbDelta SQL queries.
  2188 	 * Filters the dbDelta SQL queries.
  1872 	 *
  2189 	 *
  1873 	 * @since 3.3.0
  2190 	 * @since 3.3.0
  1874 	 *
  2191 	 *
  1875 	 * @param array $queries An array of dbDelta SQL queries.
  2192 	 * @param array $queries An array of dbDelta SQL queries.
  1876 	 */
  2193 	 */
  1879 	$cqueries = array(); // Creation Queries
  2196 	$cqueries = array(); // Creation Queries
  1880 	$iqueries = array(); // Insertion Queries
  2197 	$iqueries = array(); // Insertion Queries
  1881 	$for_update = array();
  2198 	$for_update = array();
  1882 
  2199 
  1883 	// Create a tablename index for an array ($cqueries) of queries
  2200 	// Create a tablename index for an array ($cqueries) of queries
  1884 	foreach($queries as $qry) {
  2201 	foreach ($queries as $qry) {
  1885 		if ( preg_match( "|CREATE TABLE ([^ ]*)|", $qry, $matches ) ) {
  2202 		if ( preg_match( "|CREATE TABLE ([^ ]*)|", $qry, $matches ) ) {
  1886 			$cqueries[ trim( $matches[1], '`' ) ] = $qry;
  2203 			$cqueries[ trim( $matches[1], '`' ) ] = $qry;
  1887 			$for_update[$matches[1]] = 'Created table '.$matches[1];
  2204 			$for_update[$matches[1]] = 'Created table '.$matches[1];
  1888 		} elseif ( preg_match( "|CREATE DATABASE ([^ ]*)|", $qry, $matches ) ) {
  2205 		} elseif ( preg_match( "|CREATE DATABASE ([^ ]*)|", $qry, $matches ) ) {
  1889 			array_unshift( $cqueries, $qry );
  2206 			array_unshift( $cqueries, $qry );
  1895 			// Unrecognized query type
  2212 			// Unrecognized query type
  1896 		}
  2213 		}
  1897 	}
  2214 	}
  1898 
  2215 
  1899 	/**
  2216 	/**
  1900 	 * Filter the dbDelta SQL queries for creating tables and/or databases.
  2217 	 * Filters the dbDelta SQL queries for creating tables and/or databases.
  1901 	 *
  2218 	 *
  1902 	 * Queries filterable via this hook contain "CREATE TABLE" or "CREATE DATABASE".
  2219 	 * Queries filterable via this hook contain "CREATE TABLE" or "CREATE DATABASE".
  1903 	 *
  2220 	 *
  1904 	 * @since 3.3.0
  2221 	 * @since 3.3.0
  1905 	 *
  2222 	 *
  1906 	 * @param array $cqueries An array of dbDelta create SQL queries.
  2223 	 * @param array $cqueries An array of dbDelta create SQL queries.
  1907 	 */
  2224 	 */
  1908 	$cqueries = apply_filters( 'dbdelta_create_queries', $cqueries );
  2225 	$cqueries = apply_filters( 'dbdelta_create_queries', $cqueries );
  1909 
  2226 
  1910 	/**
  2227 	/**
  1911 	 * Filter the dbDelta SQL queries for inserting or updating.
  2228 	 * Filters the dbDelta SQL queries for inserting or updating.
  1912 	 *
  2229 	 *
  1913 	 * Queries filterable via this hook contain "INSERT INTO" or "UPDATE".
  2230 	 * Queries filterable via this hook contain "INSERT INTO" or "UPDATE".
  1914 	 *
  2231 	 *
  1915 	 * @since 3.3.0
  2232 	 * @since 3.3.0
  1916 	 *
  2233 	 *
  1917 	 * @param array $iqueries An array of dbDelta insert or update SQL queries.
  2234 	 * @param array $iqueries An array of dbDelta insert or update SQL queries.
  1918 	 */
  2235 	 */
  1919 	$iqueries = apply_filters( 'dbdelta_insert_queries', $iqueries );
  2236 	$iqueries = apply_filters( 'dbdelta_insert_queries', $iqueries );
  1920 
  2237 
       
  2238 	$text_fields = array( 'tinytext', 'text', 'mediumtext', 'longtext' );
       
  2239 	$blob_fields = array( 'tinyblob', 'blob', 'mediumblob', 'longblob' );
       
  2240 
  1921 	$global_tables = $wpdb->tables( 'global' );
  2241 	$global_tables = $wpdb->tables( 'global' );
  1922 	foreach ( $cqueries as $table => $qry ) {
  2242 	foreach ( $cqueries as $table => $qry ) {
  1923 		// Upgrade global tables only for the main site. Don't upgrade at all if DO_NOT_UPGRADE_GLOBAL_TABLES is defined.
  2243 		// Upgrade global tables only for the main site. Don't upgrade at all if conditions are not optimal.
  1924 		if ( in_array( $table, $global_tables ) && ( !is_main_site() || defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) ) {
  2244 		if ( in_array( $table, $global_tables ) && ! wp_should_upgrade_global_tables() ) {
  1925 			unset( $cqueries[ $table ], $for_update[ $table ] );
  2245 			unset( $cqueries[ $table ], $for_update[ $table ] );
  1926 			continue;
  2246 			continue;
  1927 		}
  2247 		}
  1928 
  2248 
  1929 		// Fetch the table column structure from the database
  2249 		// Fetch the table column structure from the database
  1933 
  2253 
  1934 		if ( ! $tablefields )
  2254 		if ( ! $tablefields )
  1935 			continue;
  2255 			continue;
  1936 
  2256 
  1937 		// Clear the field and index arrays.
  2257 		// Clear the field and index arrays.
  1938 		$cfields = $indices = array();
  2258 		$cfields = $indices = $indices_without_subparts = array();
  1939 
  2259 
  1940 		// Get all of the field names in the query from between the parentheses.
  2260 		// Get all of the field names in the query from between the parentheses.
  1941 		preg_match("|\((.*)\)|ms", $qry, $match2);
  2261 		preg_match("|\((.*)\)|ms", $qry, $match2);
  1942 		$qryline = trim($match2[1]);
  2262 		$qryline = trim($match2[1]);
  1943 
  2263 
  1944 		// Separate field lines into an array.
  2264 		// Separate field lines into an array.
  1945 		$flds = explode("\n", $qryline);
  2265 		$flds = explode("\n", $qryline);
  1946 
  2266 
  1947 		// todo: Remove this?
       
  1948 		//echo "<hr/><pre>\n".print_r(strtolower($table), true).":\n".print_r($cqueries, true)."</pre><hr/>";
       
  1949 
       
  1950 		// For every field line specified in the query.
  2267 		// For every field line specified in the query.
  1951 		foreach ($flds as $fld) {
  2268 		foreach ( $flds as $fld ) {
       
  2269 			$fld = trim( $fld, " \t\n\r\0\x0B," ); // Default trim characters, plus ','.
  1952 
  2270 
  1953 			// Extract the field name.
  2271 			// Extract the field name.
  1954 			preg_match("|^([^ ]*)|", trim($fld), $fvals);
  2272 			preg_match( '|^([^ ]*)|', $fld, $fvals );
  1955 			$fieldname = trim( $fvals[1], '`' );
  2273 			$fieldname = trim( $fvals[1], '`' );
       
  2274 			$fieldname_lowercased = strtolower( $fieldname );
  1956 
  2275 
  1957 			// Verify the found field name.
  2276 			// Verify the found field name.
  1958 			$validfield = true;
  2277 			$validfield = true;
  1959 			switch (strtolower($fieldname)) {
  2278 			switch ( $fieldname_lowercased ) {
  1960 			case '':
  2279 				case '':
  1961 			case 'primary':
  2280 				case 'primary':
  1962 			case 'index':
  2281 				case 'index':
  1963 			case 'fulltext':
  2282 				case 'fulltext':
  1964 			case 'unique':
  2283 				case 'unique':
  1965 			case 'key':
  2284 				case 'key':
  1966 				$validfield = false;
  2285 				case 'spatial':
  1967 				$indices[] = trim(trim($fld), ", \n");
  2286 					$validfield = false;
  1968 				break;
  2287 
       
  2288 					/*
       
  2289 					 * Normalize the index definition.
       
  2290 					 *
       
  2291 					 * This is done so the definition can be compared against the result of a
       
  2292 					 * `SHOW INDEX FROM $table_name` query which returns the current table
       
  2293 					 * index information.
       
  2294 					 */
       
  2295 
       
  2296 					// Extract type, name and columns from the definition.
       
  2297 					preg_match(
       
  2298 						  '/^'
       
  2299 						.   '(?P<index_type>'             // 1) Type of the index.
       
  2300 						.       'PRIMARY\s+KEY|(?:UNIQUE|FULLTEXT|SPATIAL)\s+(?:KEY|INDEX)|KEY|INDEX'
       
  2301 						.   ')'
       
  2302 						.   '\s+'                         // Followed by at least one white space character.
       
  2303 						.   '(?:'                         // Name of the index. Optional if type is PRIMARY KEY.
       
  2304 						.       '`?'                      // Name can be escaped with a backtick.
       
  2305 						.           '(?P<index_name>'     // 2) Name of the index.
       
  2306 						.               '(?:[0-9a-zA-Z$_-]|[\xC2-\xDF][\x80-\xBF])+'
       
  2307 						.           ')'
       
  2308 						.       '`?'                      // Name can be escaped with a backtick.
       
  2309 						.       '\s+'                     // Followed by at least one white space character.
       
  2310 						.   ')*'
       
  2311 						.   '\('                          // Opening bracket for the columns.
       
  2312 						.       '(?P<index_columns>'
       
  2313 						.           '.+?'                 // 3) Column names, index prefixes, and orders.
       
  2314 						.       ')'
       
  2315 						.   '\)'                          // Closing bracket for the columns.
       
  2316 						. '$/im',
       
  2317 						$fld,
       
  2318 						$index_matches
       
  2319 					);
       
  2320 
       
  2321 					// Uppercase the index type and normalize space characters.
       
  2322 					$index_type = strtoupper( preg_replace( '/\s+/', ' ', trim( $index_matches['index_type'] ) ) );
       
  2323 
       
  2324 					// 'INDEX' is a synonym for 'KEY', standardize on 'KEY'.
       
  2325 					$index_type = str_replace( 'INDEX', 'KEY', $index_type );
       
  2326 
       
  2327 					// Escape the index name with backticks. An index for a primary key has no name.
       
  2328 					$index_name = ( 'PRIMARY KEY' === $index_type ) ? '' : '`' . strtolower( $index_matches['index_name'] ) . '`';
       
  2329 
       
  2330 					// Parse the columns. Multiple columns are separated by a comma.
       
  2331 					$index_columns = $index_columns_without_subparts = array_map( 'trim', explode( ',', $index_matches['index_columns'] ) );
       
  2332 
       
  2333 					// Normalize columns.
       
  2334 					foreach ( $index_columns as $id => &$index_column ) {
       
  2335 						// Extract column name and number of indexed characters (sub_part).
       
  2336 						preg_match(
       
  2337 							  '/'
       
  2338 							.   '`?'                      // Name can be escaped with a backtick.
       
  2339 							.       '(?P<column_name>'    // 1) Name of the column.
       
  2340 							.           '(?:[0-9a-zA-Z$_-]|[\xC2-\xDF][\x80-\xBF])+'
       
  2341 							.       ')'
       
  2342 							.   '`?'                      // Name can be escaped with a backtick.
       
  2343 							.   '(?:'                     // Optional sub part.
       
  2344 							.       '\s*'                 // Optional white space character between name and opening bracket.
       
  2345 							.       '\('                  // Opening bracket for the sub part.
       
  2346 							.           '\s*'             // Optional white space character after opening bracket.
       
  2347 							.           '(?P<sub_part>'
       
  2348 							.               '\d+'         // 2) Number of indexed characters.
       
  2349 							.           ')'
       
  2350 							.           '\s*'             // Optional white space character before closing bracket.
       
  2351 							.        '\)'                 // Closing bracket for the sub part.
       
  2352 							.   ')?'
       
  2353 							. '/',
       
  2354 							$index_column,
       
  2355 							$index_column_matches
       
  2356 						);
       
  2357 
       
  2358 						// Escape the column name with backticks.
       
  2359 						$index_column = '`' . $index_column_matches['column_name'] . '`';
       
  2360 
       
  2361 						// We don't need to add the subpart to $index_columns_without_subparts
       
  2362 						$index_columns_without_subparts[ $id ] = $index_column;
       
  2363 
       
  2364 						// Append the optional sup part with the number of indexed characters.
       
  2365 						if ( isset( $index_column_matches['sub_part'] ) ) {
       
  2366 							$index_column .= '(' . $index_column_matches['sub_part'] . ')';
       
  2367 						}
       
  2368 					}
       
  2369 
       
  2370 					// Build the normalized index definition and add it to the list of indices.
       
  2371 					$indices[] = "{$index_type} {$index_name} (" . implode( ',', $index_columns ) . ")";
       
  2372 					$indices_without_subparts[] = "{$index_type} {$index_name} (" . implode( ',', $index_columns_without_subparts ) . ")";
       
  2373 
       
  2374 					// Destroy no longer needed variables.
       
  2375 					unset( $index_column, $index_column_matches, $index_matches, $index_type, $index_name, $index_columns, $index_columns_without_subparts );
       
  2376 
       
  2377 					break;
  1969 			}
  2378 			}
  1970 			$fld = trim($fld);
       
  1971 
  2379 
  1972 			// If it's a valid field, add it to the field array.
  2380 			// If it's a valid field, add it to the field array.
  1973 			if ($validfield) {
  2381 			if ( $validfield ) {
  1974 				$cfields[strtolower($fieldname)] = trim($fld, ", \n");
  2382 				$cfields[ $fieldname_lowercased ] = $fld;
  1975 			}
  2383 			}
  1976 		}
  2384 		}
  1977 
  2385 
  1978 		// For every field in the table.
  2386 		// For every field in the table.
  1979 		foreach ($tablefields as $tablefield) {
  2387 		foreach ( $tablefields as $tablefield ) {
       
  2388 			$tablefield_field_lowercased = strtolower( $tablefield->Field );
       
  2389 			$tablefield_type_lowercased = strtolower( $tablefield->Type );
  1980 
  2390 
  1981 			// If the table field exists in the field array ...
  2391 			// If the table field exists in the field array ...
  1982 			if (array_key_exists(strtolower($tablefield->Field), $cfields)) {
  2392 			if ( array_key_exists( $tablefield_field_lowercased, $cfields ) ) {
  1983 
  2393 
  1984 				// Get the field type from the query.
  2394 				// Get the field type from the query.
  1985 				preg_match("|".$tablefield->Field." ([^ ]*( unsigned)?)|i", $cfields[strtolower($tablefield->Field)], $matches);
  2395 				preg_match( '|`?' . $tablefield->Field . '`? ([^ ]*( unsigned)?)|i', $cfields[ $tablefield_field_lowercased ], $matches );
  1986 				$fieldtype = $matches[1];
  2396 				$fieldtype = $matches[1];
       
  2397 				$fieldtype_lowercased = strtolower( $fieldtype );
  1987 
  2398 
  1988 				// Is actual field type different from the field type in query?
  2399 				// Is actual field type different from the field type in query?
  1989 				if ($tablefield->Type != $fieldtype) {
  2400 				if ($tablefield->Type != $fieldtype) {
  1990 					// Add a query to change the column type
  2401 					$do_change = true;
  1991 					$cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)];
  2402 					if ( in_array( $fieldtype_lowercased, $text_fields ) && in_array( $tablefield_type_lowercased, $text_fields ) ) {
  1992 					$for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
  2403 						if ( array_search( $fieldtype_lowercased, $text_fields ) < array_search( $tablefield_type_lowercased, $text_fields ) ) {
       
  2404 							$do_change = false;
       
  2405 						}
       
  2406 					}
       
  2407 
       
  2408 					if ( in_array( $fieldtype_lowercased, $blob_fields ) && in_array( $tablefield_type_lowercased, $blob_fields ) ) {
       
  2409 						if ( array_search( $fieldtype_lowercased, $blob_fields ) < array_search( $tablefield_type_lowercased, $blob_fields ) ) {
       
  2410 							$do_change = false;
       
  2411 						}
       
  2412 					}
       
  2413 
       
  2414 					if ( $do_change ) {
       
  2415 						// Add a query to change the column type.
       
  2416 						$cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN `{$tablefield->Field}` " . $cfields[ $tablefield_field_lowercased ];
       
  2417 						$for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
       
  2418 					}
  1993 				}
  2419 				}
  1994 
  2420 
  1995 				// Get the default value from the array
  2421 				// Get the default value from the array.
  1996 					// todo: Remove this?
  2422 				if ( preg_match( "| DEFAULT '(.*?)'|i", $cfields[ $tablefield_field_lowercased ], $matches ) ) {
  1997 					//echo "{$cfields[strtolower($tablefield->Field)]}<br>";
       
  1998 				if (preg_match("| DEFAULT '(.*?)'|i", $cfields[strtolower($tablefield->Field)], $matches)) {
       
  1999 					$default_value = $matches[1];
  2423 					$default_value = $matches[1];
  2000 					if ($tablefield->Default != $default_value) {
  2424 					if ($tablefield->Default != $default_value) {
  2001 						// Add a query to change the column's default value
  2425 						// Add a query to change the column's default value
  2002 						$cqueries[] = "ALTER TABLE {$table} ALTER COLUMN {$tablefield->Field} SET DEFAULT '{$default_value}'";
  2426 						$cqueries[] = "ALTER TABLE {$table} ALTER COLUMN `{$tablefield->Field}` SET DEFAULT '{$default_value}'";
  2003 						$for_update[$table.'.'.$tablefield->Field] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}";
  2427 						$for_update[$table.'.'.$tablefield->Field] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}";
  2004 					}
  2428 					}
  2005 				}
  2429 				}
  2006 
  2430 
  2007 				// Remove the field from the array (so it's not added).
  2431 				// Remove the field from the array (so it's not added).
  2008 				unset($cfields[strtolower($tablefield->Field)]);
  2432 				unset( $cfields[ $tablefield_field_lowercased ] );
  2009 			} else {
  2433 			} else {
  2010 				// This field exists in the table, but not in the creation queries?
  2434 				// This field exists in the table, but not in the creation queries?
  2011 			}
  2435 			}
  2012 		}
  2436 		}
  2013 
  2437 
  2027 
  2451 
  2028 			// For every index in the table.
  2452 			// For every index in the table.
  2029 			foreach ($tableindices as $tableindex) {
  2453 			foreach ($tableindices as $tableindex) {
  2030 
  2454 
  2031 				// Add the index to the index data array.
  2455 				// Add the index to the index data array.
  2032 				$keyname = $tableindex->Key_name;
  2456 				$keyname = strtolower( $tableindex->Key_name );
  2033 				$index_ary[$keyname]['columns'][] = array('fieldname' => $tableindex->Column_name, 'subpart' => $tableindex->Sub_part);
  2457 				$index_ary[$keyname]['columns'][] = array('fieldname' => $tableindex->Column_name, 'subpart' => $tableindex->Sub_part);
  2034 				$index_ary[$keyname]['unique'] = ($tableindex->Non_unique == 0)?true:false;
  2458 				$index_ary[$keyname]['unique'] = ($tableindex->Non_unique == 0)?true:false;
       
  2459 				$index_ary[$keyname]['index_type'] = $tableindex->Index_type;
  2035 			}
  2460 			}
  2036 
  2461 
  2037 			// For each actual index in the index array.
  2462 			// For each actual index in the index array.
  2038 			foreach ($index_ary as $index_name => $index_data) {
  2463 			foreach ($index_ary as $index_name => $index_data) {
  2039 
  2464 
  2040 				// Build a create string to compare to the query.
  2465 				// Build a create string to compare to the query.
  2041 				$index_string = '';
  2466 				$index_string = '';
  2042 				if ($index_name == 'PRIMARY') {
  2467 				if ($index_name == 'primary') {
  2043 					$index_string .= 'PRIMARY ';
  2468 					$index_string .= 'PRIMARY ';
  2044 				} elseif ( $index_data['unique'] ) {
  2469 				} elseif ( $index_data['unique'] ) {
  2045 					$index_string .= 'UNIQUE ';
  2470 					$index_string .= 'UNIQUE ';
  2046 				}
  2471 				}
       
  2472 				if ( 'FULLTEXT' === strtoupper( $index_data['index_type'] ) ) {
       
  2473 					$index_string .= 'FULLTEXT ';
       
  2474 				}
       
  2475 				if ( 'SPATIAL' === strtoupper( $index_data['index_type'] ) ) {
       
  2476 					$index_string .= 'SPATIAL ';
       
  2477 				}
  2047 				$index_string .= 'KEY ';
  2478 				$index_string .= 'KEY ';
  2048 				if ($index_name != 'PRIMARY') {
  2479 				if ( 'primary' !== $index_name  ) {
  2049 					$index_string .= $index_name;
  2480 					$index_string .= '`' . $index_name . '`';
  2050 				}
  2481 				}
  2051 				$index_columns = '';
  2482 				$index_columns = '';
  2052 
  2483 
  2053 				// For each column in the index.
  2484 				// For each column in the index.
  2054 				foreach ($index_data['columns'] as $column_data) {
  2485 				foreach ($index_data['columns'] as $column_data) {
  2055 					if ($index_columns != '') $index_columns .= ',';
  2486 					if ( $index_columns != '' ) {
       
  2487 						$index_columns .= ',';
       
  2488 					}
  2056 
  2489 
  2057 					// Add the field to the column list string.
  2490 					// Add the field to the column list string.
  2058 					$index_columns .= $column_data['fieldname'];
  2491 					$index_columns .= '`' . $column_data['fieldname'] . '`';
  2059 					if ($column_data['subpart'] != '') {
       
  2060 						$index_columns .= '('.$column_data['subpart'].')';
       
  2061 					}
       
  2062 				}
  2492 				}
  2063 
  2493 
  2064 				// The alternative index string doesn't care about subparts
       
  2065 				$alt_index_columns = preg_replace( '/\([^)]*\)/', '', $index_columns );
       
  2066 
       
  2067 				// Add the column list to the index create string.
  2494 				// Add the column list to the index create string.
  2068 				$index_strings = array(
  2495 				$index_string .= " ($index_columns)";
  2069 					"$index_string ($index_columns)",
  2496 
  2070 					"$index_string ($alt_index_columns)",
  2497 				// Check if the index definition exists, ignoring subparts.
  2071 				);
  2498 				if ( ! ( ( $aindex = array_search( $index_string, $indices_without_subparts ) ) === false ) ) {
  2072 
  2499 					// If the index already exists (even with different subparts), we don't need to create it.
  2073 				foreach( $index_strings as $index_string ) {
  2500 					unset( $indices_without_subparts[ $aindex ] );
  2074 					if ( ! ( ( $aindex = array_search( $index_string, $indices ) ) === false ) ) {
  2501 					unset( $indices[ $aindex ] );
  2075 						unset( $indices[ $aindex ] );
       
  2076 						break;
       
  2077 						// todo: Remove this?
       
  2078 						//echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br />Found index:".$index_string."</pre>\n";
       
  2079 					}
       
  2080 				}
  2502 				}
  2081 				// todo: Remove this?
       
  2082 				//else echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br /><b>Did not find index:</b>".$index_string."<br />".print_r($indices, true)."</pre>\n";
       
  2083 			}
  2503 			}
  2084 		}
  2504 		}
  2085 
  2505 
  2086 		// For every remaining index specified for the table.
  2506 		// For every remaining index specified for the table.
  2087 		foreach ( (array) $indices as $index ) {
  2507 		foreach ( (array) $indices as $index ) {
  2095 	}
  2515 	}
  2096 
  2516 
  2097 	$allqueries = array_merge($cqueries, $iqueries);
  2517 	$allqueries = array_merge($cqueries, $iqueries);
  2098 	if ($execute) {
  2518 	if ($execute) {
  2099 		foreach ($allqueries as $query) {
  2519 		foreach ($allqueries as $query) {
  2100 			// todo: Remove this?
       
  2101 			//echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">".print_r($query, true)."</pre>\n";
       
  2102 			$wpdb->query($query);
  2520 			$wpdb->query($query);
  2103 		}
  2521 		}
  2104 	}
  2522 	}
  2105 
  2523 
  2106 	return $for_update;
  2524 	return $for_update;
  2119  * @param string $tables Optional. Which set of tables to update. Default is 'all'.
  2537  * @param string $tables Optional. Which set of tables to update. Default is 'all'.
  2120  */
  2538  */
  2121 function make_db_current( $tables = 'all' ) {
  2539 function make_db_current( $tables = 'all' ) {
  2122 	$alterations = dbDelta( $tables );
  2540 	$alterations = dbDelta( $tables );
  2123 	echo "<ol>\n";
  2541 	echo "<ol>\n";
  2124 	foreach($alterations as $alteration) echo "<li>$alteration</li>\n";
  2542 	foreach ($alterations as $alteration) echo "<li>$alteration</li>\n";
  2125 	echo "</ol>\n";
  2543 	echo "</ol>\n";
  2126 }
  2544 }
  2127 
  2545 
  2128 /**
  2546 /**
  2129  * Updates the database tables to a new schema, but without displaying results.
  2547  * Updates the database tables to a new schema, but without displaying results.
  2178 				if (! @copy(WP_CONTENT_DIR . '/themes/' . WP_DEFAULT_THEME . '/index.php', "$site_dir/$newfile"))
  2596 				if (! @copy(WP_CONTENT_DIR . '/themes/' . WP_DEFAULT_THEME . '/index.php', "$site_dir/$newfile"))
  2179 					return false;
  2597 					return false;
  2180 
  2598 
  2181 				// Don't copy anything.
  2599 				// Don't copy anything.
  2182 				continue;
  2600 				continue;
  2183 				}
  2601 			}
  2184 		}
  2602 		}
  2185 
  2603 
  2186 		if (! @copy("$oldpath/$oldfile", "$site_dir/$newfile"))
  2604 		if (! @copy("$oldpath/$oldfile", "$site_dir/$newfile"))
  2187 			return false;
  2605 			return false;
  2188 
  2606 
  2231  *
  2649  *
  2232  * @since 1.5.0
  2650  * @since 1.5.0
  2233  *
  2651  *
  2234  * @param string $theme_name The name of the theme.
  2652  * @param string $theme_name The name of the theme.
  2235  * @param string $template   The directory name of the theme.
  2653  * @param string $template   The directory name of the theme.
  2236  * @return null|false
  2654  * @return false|void
  2237  */
  2655  */
  2238 function make_site_theme_from_default($theme_name, $template) {
  2656 function make_site_theme_from_default($theme_name, $template) {
  2239 	$site_dir = WP_CONTENT_DIR . "/themes/$template";
  2657 	$site_dir = WP_CONTENT_DIR . "/themes/$template";
  2240 	$default_dir = WP_CONTENT_DIR . '/themes/' . WP_DEFAULT_THEME;
  2658 	$default_dir = WP_CONTENT_DIR . '/themes/' . WP_DEFAULT_THEME;
  2241 
  2659 
  2370 
  2788 
  2371 /**
  2789 /**
  2372  * Checks the version of the installed MySQL binary.
  2790  * Checks the version of the installed MySQL binary.
  2373  *
  2791  *
  2374  * @since 2.1.0
  2792  * @since 2.1.0
       
  2793  *
       
  2794  * @global wpdb  $wpdb
  2375  */
  2795  */
  2376 function wp_check_mysql_version() {
  2796 function wp_check_mysql_version() {
  2377 	global $wpdb;
  2797 	global $wpdb;
  2378 	$result = $wpdb->check_database_version();
  2798 	$result = $wpdb->check_database_version();
  2379 	if ( is_wp_error( $result ) )
  2799 	if ( is_wp_error( $result ) )
  2399 
  2819 
  2400 /**
  2820 /**
  2401  * Disables the Link Manager on upgrade if, at the time of upgrade, no links exist in the DB.
  2821  * Disables the Link Manager on upgrade if, at the time of upgrade, no links exist in the DB.
  2402  *
  2822  *
  2403  * @since 3.5.0
  2823  * @since 3.5.0
       
  2824  *
       
  2825  * @global int  $wp_current_db_version
       
  2826  * @global wpdb $wpdb WordPress database abstraction object.
  2404  */
  2827  */
  2405 function maybe_disable_link_manager() {
  2828 function maybe_disable_link_manager() {
  2406 	global $wp_current_db_version, $wpdb;
  2829 	global $wp_current_db_version, $wpdb;
  2407 
  2830 
  2408 	if ( $wp_current_db_version >= 22006 && get_option( 'link_manager_enabled' ) && ! $wpdb->get_var( "SELECT link_id FROM $wpdb->links LIMIT 1" ) )
  2831 	if ( $wp_current_db_version >= 22006 && get_option( 'link_manager_enabled' ) && ! $wpdb->get_var( "SELECT link_id FROM $wpdb->links LIMIT 1" ) )
  2411 
  2834 
  2412 /**
  2835 /**
  2413  * Runs before the schema is upgraded.
  2836  * Runs before the schema is upgraded.
  2414  *
  2837  *
  2415  * @since 2.9.0
  2838  * @since 2.9.0
       
  2839  *
       
  2840  * @global int  $wp_current_db_version
       
  2841  * @global wpdb $wpdb WordPress database abstraction object.
  2416  */
  2842  */
  2417 function pre_schema_upgrade() {
  2843 function pre_schema_upgrade() {
  2418 	global $wp_current_db_version, $wpdb;
  2844 	global $wp_current_db_version, $wpdb;
  2419 
  2845 
  2420 	// Upgrade versions prior to 2.9
  2846 	// Upgrade versions prior to 2.9
  2428 		// Drop the old option_name index. dbDelta() doesn't do the drop.
  2854 		// Drop the old option_name index. dbDelta() doesn't do the drop.
  2429 		$wpdb->query("ALTER TABLE $wpdb->options DROP INDEX option_name");
  2855 		$wpdb->query("ALTER TABLE $wpdb->options DROP INDEX option_name");
  2430 	}
  2856 	}
  2431 
  2857 
  2432 	// Multisite schema upgrades.
  2858 	// Multisite schema upgrades.
  2433 	if ( $wp_current_db_version < 25448 && is_multisite() && ! defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) && is_main_network() ) {
  2859 	if ( $wp_current_db_version < 25448 && is_multisite() && wp_should_upgrade_global_tables() ) {
  2434 
  2860 
  2435 		// Upgrade verions prior to 3.7
  2861 		// Upgrade versions prior to 3.7
  2436 		if ( $wp_current_db_version < 25179 ) {
  2862 		if ( $wp_current_db_version < 25179 ) {
  2437 			// New primary key for signups.
  2863 			// New primary key for signups.
  2438 			$wpdb->query( "ALTER TABLE $wpdb->signups ADD signup_id BIGINT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST" );
  2864 			$wpdb->query( "ALTER TABLE $wpdb->signups ADD signup_id BIGINT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST" );
  2439 			$wpdb->query( "ALTER TABLE $wpdb->signups DROP INDEX domain" );
  2865 			$wpdb->query( "ALTER TABLE $wpdb->signups DROP INDEX domain" );
  2440 		}
  2866 		}
  2444 			$wpdb->query( "ALTER TABLE $wpdb->blogs CHANGE COLUMN archived archived varchar(1) NOT NULL default '0'" );
  2870 			$wpdb->query( "ALTER TABLE $wpdb->blogs CHANGE COLUMN archived archived varchar(1) NOT NULL default '0'" );
  2445 			$wpdb->query( "ALTER TABLE $wpdb->blogs CHANGE COLUMN archived archived tinyint(2) NOT NULL default 0" );
  2871 			$wpdb->query( "ALTER TABLE $wpdb->blogs CHANGE COLUMN archived archived tinyint(2) NOT NULL default 0" );
  2446 		}
  2872 		}
  2447 	}
  2873 	}
  2448 
  2874 
  2449 	if ( $wp_current_db_version < 30133 ) {
       
  2450 		// dbDelta() can recreate but can't drop the index.
       
  2451 		$wpdb->query( "ALTER TABLE $wpdb->terms DROP INDEX slug" );
       
  2452 	}
       
  2453 
       
  2454 	// Upgrade versions prior to 4.2.
  2875 	// Upgrade versions prior to 4.2.
  2455 	if ( $wp_current_db_version < 31351 ) {
  2876 	if ( $wp_current_db_version < 31351 ) {
  2456 		if ( ! is_multisite() ) {
  2877 		if ( ! is_multisite() && wp_should_upgrade_global_tables() ) {
  2457 			$wpdb->query( "ALTER TABLE $wpdb->usermeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
  2878 			$wpdb->query( "ALTER TABLE $wpdb->usermeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
  2458 		}
  2879 		}
  2459 		$wpdb->query( "ALTER TABLE $wpdb->terms DROP INDEX slug, ADD INDEX slug(slug(191))" );
  2880 		$wpdb->query( "ALTER TABLE $wpdb->terms DROP INDEX slug, ADD INDEX slug(slug(191))" );
  2460 		$wpdb->query( "ALTER TABLE $wpdb->terms DROP INDEX name, ADD INDEX name(name(191))" );
  2881 		$wpdb->query( "ALTER TABLE $wpdb->terms DROP INDEX name, ADD INDEX name(name(191))" );
  2461 		$wpdb->query( "ALTER TABLE $wpdb->commentmeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
  2882 		$wpdb->query( "ALTER TABLE $wpdb->commentmeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
  2462 		$wpdb->query( "ALTER TABLE $wpdb->postmeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
  2883 		$wpdb->query( "ALTER TABLE $wpdb->postmeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
  2463 		$wpdb->query( "ALTER TABLE $wpdb->posts DROP INDEX post_name, ADD INDEX post_name(post_name(191))" );
  2884 		$wpdb->query( "ALTER TABLE $wpdb->posts DROP INDEX post_name, ADD INDEX post_name(post_name(191))" );
  2464 	}
  2885 	}
  2465 }
  2886 
  2466 
  2887 	// Upgrade versions prior to 4.4.
       
  2888 	if ( $wp_current_db_version < 34978 ) {
       
  2889 		// If compatible termmeta table is found, use it, but enforce a proper index and update collation.
       
  2890 		if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->termmeta}'" ) && $wpdb->get_results( "SHOW INDEX FROM {$wpdb->termmeta} WHERE Column_name = 'meta_key'" ) ) {
       
  2891 			$wpdb->query( "ALTER TABLE $wpdb->termmeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
       
  2892 			maybe_convert_table_to_utf8mb4( $wpdb->termmeta );
       
  2893 		}
       
  2894 	}
       
  2895 }
       
  2896 
       
  2897 if ( !function_exists( 'install_global_terms' ) ) :
  2467 /**
  2898 /**
  2468  * Install global terms.
  2899  * Install global terms.
  2469  *
  2900  *
  2470  * @since 3.0.0
  2901  * @since 3.0.0
  2471  *
  2902  *
  2472  */
  2903  * @global wpdb   $wpdb
  2473 if ( !function_exists( 'install_global_terms' ) ) :
  2904  * @global string $charset_collate
       
  2905  */
  2474 function install_global_terms() {
  2906 function install_global_terms() {
  2475 	global $wpdb, $charset_collate;
  2907 	global $wpdb, $charset_collate;
  2476 	$ms_queries = "
  2908 	$ms_queries = "
  2477 CREATE TABLE $wpdb->sitecategories (
  2909 CREATE TABLE $wpdb->sitecategories (
  2478   cat_ID bigint(20) NOT NULL auto_increment,
  2910   cat_ID bigint(20) NOT NULL auto_increment,
  2486 ";
  2918 ";
  2487 // now create tables
  2919 // now create tables
  2488 	dbDelta( $ms_queries );
  2920 	dbDelta( $ms_queries );
  2489 }
  2921 }
  2490 endif;
  2922 endif;
       
  2923 
       
  2924 /**
       
  2925  * Determine if global tables should be upgraded.
       
  2926  *
       
  2927  * This function performs a series of checks to ensure the environment allows
       
  2928  * for the safe upgrading of global WordPress database tables. It is necessary
       
  2929  * because global tables will commonly grow to millions of rows on large
       
  2930  * installations, and the ability to control their upgrade routines can be
       
  2931  * critical to the operation of large networks.
       
  2932  *
       
  2933  * In a future iteration, this function may use `wp_is_large_network()` to more-
       
  2934  * intelligently prevent global table upgrades. Until then, we make sure
       
  2935  * WordPress is on the main site of the main network, to avoid running queries
       
  2936  * more than once in multi-site or multi-network environments.
       
  2937  *
       
  2938  * @since 4.3.0
       
  2939  *
       
  2940  * @return bool Whether to run the upgrade routines on global tables.
       
  2941  */
       
  2942 function wp_should_upgrade_global_tables() {
       
  2943 
       
  2944 	// Return false early if explicitly not upgrading
       
  2945 	if ( defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) {
       
  2946 		return false;
       
  2947 	}
       
  2948 
       
  2949 	// Assume global tables should be upgraded
       
  2950 	$should_upgrade = true;
       
  2951 
       
  2952 	// Set to false if not on main network (does not matter if not multi-network)
       
  2953 	if ( ! is_main_network() ) {
       
  2954 		$should_upgrade = false;
       
  2955 	}
       
  2956 
       
  2957 	// Set to false if not on main site of current network (does not matter if not multi-site)
       
  2958 	if ( ! is_main_site() ) {
       
  2959 		$should_upgrade = false;
       
  2960 	}
       
  2961 
       
  2962 	/**
       
  2963 	 * Filters if upgrade routines should be run on global tables.
       
  2964 	 *
       
  2965 	 * @param bool $should_upgrade Whether to run the upgrade routines on global tables.
       
  2966 	 */
       
  2967 	return apply_filters( 'wp_should_upgrade_global_tables', $should_upgrade );
       
  2968 }