Edit File by line
/home/barbar84/www/wp-admin/includes
File: upgrade.php
[2500] Fix | Delete
$wpdb->hide_errors();
[2501] Fix | Delete
[2502] Fix | Delete
$wpdb->query( "ALTER TABLE `$table` DROP INDEX `$index`" );
[2503] Fix | Delete
[2504] Fix | Delete
// Now we need to take out all the extra ones we may have created.
[2505] Fix | Delete
for ( $i = 0; $i < 25; $i++ ) {
[2506] Fix | Delete
$wpdb->query( "ALTER TABLE `$table` DROP INDEX `{$index}_$i`" );
[2507] Fix | Delete
}
[2508] Fix | Delete
[2509] Fix | Delete
$wpdb->show_errors();
[2510] Fix | Delete
[2511] Fix | Delete
return true;
[2512] Fix | Delete
}
[2513] Fix | Delete
[2514] Fix | Delete
/**
[2515] Fix | Delete
* Adds an index to a specified table.
[2516] Fix | Delete
*
[2517] Fix | Delete
* @since 1.0.1
[2518] Fix | Delete
*
[2519] Fix | Delete
* @global wpdb $wpdb WordPress database abstraction object.
[2520] Fix | Delete
*
[2521] Fix | Delete
* @param string $table Database table name.
[2522] Fix | Delete
* @param string $index Database table index column.
[2523] Fix | Delete
* @return true True, when done with execution.
[2524] Fix | Delete
*/
[2525] Fix | Delete
function add_clean_index( $table, $index ) {
[2526] Fix | Delete
global $wpdb;
[2527] Fix | Delete
[2528] Fix | Delete
drop_index( $table, $index );
[2529] Fix | Delete
$wpdb->query( "ALTER TABLE `$table` ADD INDEX ( `$index` )" );
[2530] Fix | Delete
[2531] Fix | Delete
return true;
[2532] Fix | Delete
}
[2533] Fix | Delete
[2534] Fix | Delete
/**
[2535] Fix | Delete
* Adds column to a database table, if it doesn't already exist.
[2536] Fix | Delete
*
[2537] Fix | Delete
* @since 1.3.0
[2538] Fix | Delete
*
[2539] Fix | Delete
* @global wpdb $wpdb WordPress database abstraction object.
[2540] Fix | Delete
*
[2541] Fix | Delete
* @param string $table_name Database table name.
[2542] Fix | Delete
* @param string $column_name Table column name.
[2543] Fix | Delete
* @param string $create_ddl SQL statement to add column.
[2544] Fix | Delete
* @return bool True on success or if the column already exists. False on failure.
[2545] Fix | Delete
*/
[2546] Fix | Delete
function maybe_add_column( $table_name, $column_name, $create_ddl ) {
[2547] Fix | Delete
global $wpdb;
[2548] Fix | Delete
[2549] Fix | Delete
foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) {
[2550] Fix | Delete
if ( $column === $column_name ) {
[2551] Fix | Delete
return true;
[2552] Fix | Delete
}
[2553] Fix | Delete
}
[2554] Fix | Delete
[2555] Fix | Delete
// Didn't find it, so try to create it.
[2556] Fix | Delete
$wpdb->query( $create_ddl );
[2557] Fix | Delete
[2558] Fix | Delete
// We cannot directly tell that whether this succeeded!
[2559] Fix | Delete
foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) {
[2560] Fix | Delete
if ( $column === $column_name ) {
[2561] Fix | Delete
return true;
[2562] Fix | Delete
}
[2563] Fix | Delete
}
[2564] Fix | Delete
[2565] Fix | Delete
return false;
[2566] Fix | Delete
}
[2567] Fix | Delete
[2568] Fix | Delete
/**
[2569] Fix | Delete
* If a table only contains utf8 or utf8mb4 columns, convert it to utf8mb4.
[2570] Fix | Delete
*
[2571] Fix | Delete
* @since 4.2.0
[2572] Fix | Delete
*
[2573] Fix | Delete
* @global wpdb $wpdb WordPress database abstraction object.
[2574] Fix | Delete
*
[2575] Fix | Delete
* @param string $table The table to convert.
[2576] Fix | Delete
* @return bool True if the table was converted, false if it wasn't.
[2577] Fix | Delete
*/
[2578] Fix | Delete
function maybe_convert_table_to_utf8mb4( $table ) {
[2579] Fix | Delete
global $wpdb;
[2580] Fix | Delete
[2581] Fix | Delete
$results = $wpdb->get_results( "SHOW FULL COLUMNS FROM `$table`" );
[2582] Fix | Delete
if ( ! $results ) {
[2583] Fix | Delete
return false;
[2584] Fix | Delete
}
[2585] Fix | Delete
[2586] Fix | Delete
foreach ( $results as $column ) {
[2587] Fix | Delete
if ( $column->Collation ) {
[2588] Fix | Delete
list( $charset ) = explode( '_', $column->Collation );
[2589] Fix | Delete
$charset = strtolower( $charset );
[2590] Fix | Delete
if ( 'utf8' !== $charset && 'utf8mb4' !== $charset ) {
[2591] Fix | Delete
// Don't upgrade tables that have non-utf8 columns.
[2592] Fix | Delete
return false;
[2593] Fix | Delete
}
[2594] Fix | Delete
}
[2595] Fix | Delete
}
[2596] Fix | Delete
[2597] Fix | Delete
$table_details = $wpdb->get_row( "SHOW TABLE STATUS LIKE '$table'" );
[2598] Fix | Delete
if ( ! $table_details ) {
[2599] Fix | Delete
return false;
[2600] Fix | Delete
}
[2601] Fix | Delete
[2602] Fix | Delete
list( $table_charset ) = explode( '_', $table_details->Collation );
[2603] Fix | Delete
$table_charset = strtolower( $table_charset );
[2604] Fix | Delete
if ( 'utf8mb4' === $table_charset ) {
[2605] Fix | Delete
return true;
[2606] Fix | Delete
}
[2607] Fix | Delete
[2608] Fix | Delete
return $wpdb->query( "ALTER TABLE $table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci" );
[2609] Fix | Delete
}
[2610] Fix | Delete
[2611] Fix | Delete
/**
[2612] Fix | Delete
* Retrieve all options as it was for 1.2.
[2613] Fix | Delete
*
[2614] Fix | Delete
* @since 1.2.0
[2615] Fix | Delete
*
[2616] Fix | Delete
* @global wpdb $wpdb WordPress database abstraction object.
[2617] Fix | Delete
*
[2618] Fix | Delete
* @return stdClass List of options.
[2619] Fix | Delete
*/
[2620] Fix | Delete
function get_alloptions_110() {
[2621] Fix | Delete
global $wpdb;
[2622] Fix | Delete
$all_options = new stdClass;
[2623] Fix | Delete
$options = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
[2624] Fix | Delete
if ( $options ) {
[2625] Fix | Delete
foreach ( $options as $option ) {
[2626] Fix | Delete
if ( 'siteurl' === $option->option_name || 'home' === $option->option_name || 'category_base' === $option->option_name ) {
[2627] Fix | Delete
$option->option_value = untrailingslashit( $option->option_value );
[2628] Fix | Delete
}
[2629] Fix | Delete
$all_options->{$option->option_name} = stripslashes( $option->option_value );
[2630] Fix | Delete
}
[2631] Fix | Delete
}
[2632] Fix | Delete
return $all_options;
[2633] Fix | Delete
}
[2634] Fix | Delete
[2635] Fix | Delete
/**
[2636] Fix | Delete
* Utility version of get_option that is private to installation/upgrade.
[2637] Fix | Delete
*
[2638] Fix | Delete
* @ignore
[2639] Fix | Delete
* @since 1.5.1
[2640] Fix | Delete
* @access private
[2641] Fix | Delete
*
[2642] Fix | Delete
* @global wpdb $wpdb WordPress database abstraction object.
[2643] Fix | Delete
*
[2644] Fix | Delete
* @param string $setting Option name.
[2645] Fix | Delete
* @return mixed
[2646] Fix | Delete
*/
[2647] Fix | Delete
function __get_option( $setting ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.FunctionDoubleUnderscore
[2648] Fix | Delete
global $wpdb;
[2649] Fix | Delete
[2650] Fix | Delete
if ( 'home' === $setting && defined( 'WP_HOME' ) ) {
[2651] Fix | Delete
return untrailingslashit( WP_HOME );
[2652] Fix | Delete
}
[2653] Fix | Delete
[2654] Fix | Delete
if ( 'siteurl' === $setting && defined( 'WP_SITEURL' ) ) {
[2655] Fix | Delete
return untrailingslashit( WP_SITEURL );
[2656] Fix | Delete
}
[2657] Fix | Delete
[2658] Fix | Delete
$option = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s", $setting ) );
[2659] Fix | Delete
[2660] Fix | Delete
if ( 'home' === $setting && ! $option ) {
[2661] Fix | Delete
return __get_option( 'siteurl' );
[2662] Fix | Delete
}
[2663] Fix | Delete
[2664] Fix | Delete
if ( in_array( $setting, array( 'siteurl', 'home', 'category_base', 'tag_base' ), true ) ) {
[2665] Fix | Delete
$option = untrailingslashit( $option );
[2666] Fix | Delete
}
[2667] Fix | Delete
[2668] Fix | Delete
return maybe_unserialize( $option );
[2669] Fix | Delete
}
[2670] Fix | Delete
[2671] Fix | Delete
/**
[2672] Fix | Delete
* Filters for content to remove unnecessary slashes.
[2673] Fix | Delete
*
[2674] Fix | Delete
* @since 1.5.0
[2675] Fix | Delete
*
[2676] Fix | Delete
* @param string $content The content to modify.
[2677] Fix | Delete
* @return string The de-slashed content.
[2678] Fix | Delete
*/
[2679] Fix | Delete
function deslash( $content ) {
[2680] Fix | Delete
// Note: \\\ inside a regex denotes a single backslash.
[2681] Fix | Delete
[2682] Fix | Delete
/*
[2683] Fix | Delete
* Replace one or more backslashes followed by a single quote with
[2684] Fix | Delete
* a single quote.
[2685] Fix | Delete
*/
[2686] Fix | Delete
$content = preg_replace( "/\\\+'/", "'", $content );
[2687] Fix | Delete
[2688] Fix | Delete
/*
[2689] Fix | Delete
* Replace one or more backslashes followed by a double quote with
[2690] Fix | Delete
* a double quote.
[2691] Fix | Delete
*/
[2692] Fix | Delete
$content = preg_replace( '/\\\+"/', '"', $content );
[2693] Fix | Delete
[2694] Fix | Delete
// Replace one or more backslashes with one backslash.
[2695] Fix | Delete
$content = preg_replace( '/\\\+/', '\\', $content );
[2696] Fix | Delete
[2697] Fix | Delete
return $content;
[2698] Fix | Delete
}
[2699] Fix | Delete
[2700] Fix | Delete
/**
[2701] Fix | Delete
* Modifies the database based on specified SQL statements.
[2702] Fix | Delete
*
[2703] Fix | Delete
* Useful for creating new tables and updating existing tables to a new structure.
[2704] Fix | Delete
*
[2705] Fix | Delete
* @since 1.5.0
[2706] Fix | Delete
*
[2707] Fix | Delete
* @global wpdb $wpdb WordPress database abstraction object.
[2708] Fix | Delete
*
[2709] Fix | Delete
* @param string[]|string $queries Optional. The query to run. Can be multiple queries
[2710] Fix | Delete
* in an array, or a string of queries separated by
[2711] Fix | Delete
* semicolons. Default empty string.
[2712] Fix | Delete
* @param bool $execute Optional. Whether or not to execute the query right away.
[2713] Fix | Delete
* Default true.
[2714] Fix | Delete
* @return array Strings containing the results of the various update queries.
[2715] Fix | Delete
*/
[2716] Fix | Delete
function dbDelta( $queries = '', $execute = true ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
[2717] Fix | Delete
global $wpdb;
[2718] Fix | Delete
[2719] Fix | Delete
if ( in_array( $queries, array( '', 'all', 'blog', 'global', 'ms_global' ), true ) ) {
[2720] Fix | Delete
$queries = wp_get_db_schema( $queries );
[2721] Fix | Delete
}
[2722] Fix | Delete
[2723] Fix | Delete
// Separate individual queries into an array.
[2724] Fix | Delete
if ( ! is_array( $queries ) ) {
[2725] Fix | Delete
$queries = explode( ';', $queries );
[2726] Fix | Delete
$queries = array_filter( $queries );
[2727] Fix | Delete
}
[2728] Fix | Delete
[2729] Fix | Delete
/**
[2730] Fix | Delete
* Filters the dbDelta SQL queries.
[2731] Fix | Delete
*
[2732] Fix | Delete
* @since 3.3.0
[2733] Fix | Delete
*
[2734] Fix | Delete
* @param string[] $queries An array of dbDelta SQL queries.
[2735] Fix | Delete
*/
[2736] Fix | Delete
$queries = apply_filters( 'dbdelta_queries', $queries );
[2737] Fix | Delete
[2738] Fix | Delete
$cqueries = array(); // Creation queries.
[2739] Fix | Delete
$iqueries = array(); // Insertion queries.
[2740] Fix | Delete
$for_update = array();
[2741] Fix | Delete
[2742] Fix | Delete
// Create a tablename index for an array ($cqueries) of queries.
[2743] Fix | Delete
foreach ( $queries as $qry ) {
[2744] Fix | Delete
if ( preg_match( '|CREATE TABLE ([^ ]*)|', $qry, $matches ) ) {
[2745] Fix | Delete
$cqueries[ trim( $matches[1], '`' ) ] = $qry;
[2746] Fix | Delete
$for_update[ $matches[1] ] = 'Created table ' . $matches[1];
[2747] Fix | Delete
} elseif ( preg_match( '|CREATE DATABASE ([^ ]*)|', $qry, $matches ) ) {
[2748] Fix | Delete
array_unshift( $cqueries, $qry );
[2749] Fix | Delete
} elseif ( preg_match( '|INSERT INTO ([^ ]*)|', $qry, $matches ) ) {
[2750] Fix | Delete
$iqueries[] = $qry;
[2751] Fix | Delete
} elseif ( preg_match( '|UPDATE ([^ ]*)|', $qry, $matches ) ) {
[2752] Fix | Delete
$iqueries[] = $qry;
[2753] Fix | Delete
} else {
[2754] Fix | Delete
// Unrecognized query type.
[2755] Fix | Delete
}
[2756] Fix | Delete
}
[2757] Fix | Delete
[2758] Fix | Delete
/**
[2759] Fix | Delete
* Filters the dbDelta SQL queries for creating tables and/or databases.
[2760] Fix | Delete
*
[2761] Fix | Delete
* Queries filterable via this hook contain "CREATE TABLE" or "CREATE DATABASE".
[2762] Fix | Delete
*
[2763] Fix | Delete
* @since 3.3.0
[2764] Fix | Delete
*
[2765] Fix | Delete
* @param string[] $cqueries An array of dbDelta create SQL queries.
[2766] Fix | Delete
*/
[2767] Fix | Delete
$cqueries = apply_filters( 'dbdelta_create_queries', $cqueries );
[2768] Fix | Delete
[2769] Fix | Delete
/**
[2770] Fix | Delete
* Filters the dbDelta SQL queries for inserting or updating.
[2771] Fix | Delete
*
[2772] Fix | Delete
* Queries filterable via this hook contain "INSERT INTO" or "UPDATE".
[2773] Fix | Delete
*
[2774] Fix | Delete
* @since 3.3.0
[2775] Fix | Delete
*
[2776] Fix | Delete
* @param string[] $iqueries An array of dbDelta insert or update SQL queries.
[2777] Fix | Delete
*/
[2778] Fix | Delete
$iqueries = apply_filters( 'dbdelta_insert_queries', $iqueries );
[2779] Fix | Delete
[2780] Fix | Delete
$text_fields = array( 'tinytext', 'text', 'mediumtext', 'longtext' );
[2781] Fix | Delete
$blob_fields = array( 'tinyblob', 'blob', 'mediumblob', 'longblob' );
[2782] Fix | Delete
[2783] Fix | Delete
$global_tables = $wpdb->tables( 'global' );
[2784] Fix | Delete
foreach ( $cqueries as $table => $qry ) {
[2785] Fix | Delete
// Upgrade global tables only for the main site. Don't upgrade at all if conditions are not optimal.
[2786] Fix | Delete
if ( in_array( $table, $global_tables, true ) && ! wp_should_upgrade_global_tables() ) {
[2787] Fix | Delete
unset( $cqueries[ $table ], $for_update[ $table ] );
[2788] Fix | Delete
continue;
[2789] Fix | Delete
}
[2790] Fix | Delete
[2791] Fix | Delete
// Fetch the table column structure from the database.
[2792] Fix | Delete
$suppress = $wpdb->suppress_errors();
[2793] Fix | Delete
$tablefields = $wpdb->get_results( "DESCRIBE {$table};" );
[2794] Fix | Delete
$wpdb->suppress_errors( $suppress );
[2795] Fix | Delete
[2796] Fix | Delete
if ( ! $tablefields ) {
[2797] Fix | Delete
continue;
[2798] Fix | Delete
}
[2799] Fix | Delete
[2800] Fix | Delete
// Clear the field and index arrays.
[2801] Fix | Delete
$cfields = array();
[2802] Fix | Delete
$indices = array();
[2803] Fix | Delete
$indices_without_subparts = array();
[2804] Fix | Delete
[2805] Fix | Delete
// Get all of the field names in the query from between the parentheses.
[2806] Fix | Delete
preg_match( '|\((.*)\)|ms', $qry, $match2 );
[2807] Fix | Delete
$qryline = trim( $match2[1] );
[2808] Fix | Delete
[2809] Fix | Delete
// Separate field lines into an array.
[2810] Fix | Delete
$flds = explode( "\n", $qryline );
[2811] Fix | Delete
[2812] Fix | Delete
// For every field line specified in the query.
[2813] Fix | Delete
foreach ( $flds as $fld ) {
[2814] Fix | Delete
$fld = trim( $fld, " \t\n\r\0\x0B," ); // Default trim characters, plus ','.
[2815] Fix | Delete
[2816] Fix | Delete
// Extract the field name.
[2817] Fix | Delete
preg_match( '|^([^ ]*)|', $fld, $fvals );
[2818] Fix | Delete
$fieldname = trim( $fvals[1], '`' );
[2819] Fix | Delete
$fieldname_lowercased = strtolower( $fieldname );
[2820] Fix | Delete
[2821] Fix | Delete
// Verify the found field name.
[2822] Fix | Delete
$validfield = true;
[2823] Fix | Delete
switch ( $fieldname_lowercased ) {
[2824] Fix | Delete
case '':
[2825] Fix | Delete
case 'primary':
[2826] Fix | Delete
case 'index':
[2827] Fix | Delete
case 'fulltext':
[2828] Fix | Delete
case 'unique':
[2829] Fix | Delete
case 'key':
[2830] Fix | Delete
case 'spatial':
[2831] Fix | Delete
$validfield = false;
[2832] Fix | Delete
[2833] Fix | Delete
/*
[2834] Fix | Delete
* Normalize the index definition.
[2835] Fix | Delete
*
[2836] Fix | Delete
* This is done so the definition can be compared against the result of a
[2837] Fix | Delete
* `SHOW INDEX FROM $table_name` query which returns the current table
[2838] Fix | Delete
* index information.
[2839] Fix | Delete
*/
[2840] Fix | Delete
[2841] Fix | Delete
// Extract type, name and columns from the definition.
[2842] Fix | Delete
// phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- don't remove regex indentation
[2843] Fix | Delete
preg_match(
[2844] Fix | Delete
'/^'
[2845] Fix | Delete
. '(?P<index_type>' // 1) Type of the index.
[2846] Fix | Delete
. 'PRIMARY\s+KEY|(?:UNIQUE|FULLTEXT|SPATIAL)\s+(?:KEY|INDEX)|KEY|INDEX'
[2847] Fix | Delete
. ')'
[2848] Fix | Delete
. '\s+' // Followed by at least one white space character.
[2849] Fix | Delete
. '(?:' // Name of the index. Optional if type is PRIMARY KEY.
[2850] Fix | Delete
. '`?' // Name can be escaped with a backtick.
[2851] Fix | Delete
. '(?P<index_name>' // 2) Name of the index.
[2852] Fix | Delete
. '(?:[0-9a-zA-Z$_-]|[\xC2-\xDF][\x80-\xBF])+'
[2853] Fix | Delete
. ')'
[2854] Fix | Delete
. '`?' // Name can be escaped with a backtick.
[2855] Fix | Delete
. '\s+' // Followed by at least one white space character.
[2856] Fix | Delete
. ')*'
[2857] Fix | Delete
. '\(' // Opening bracket for the columns.
[2858] Fix | Delete
. '(?P<index_columns>'
[2859] Fix | Delete
. '.+?' // 3) Column names, index prefixes, and orders.
[2860] Fix | Delete
. ')'
[2861] Fix | Delete
. '\)' // Closing bracket for the columns.
[2862] Fix | Delete
. '$/im',
[2863] Fix | Delete
$fld,
[2864] Fix | Delete
$index_matches
[2865] Fix | Delete
);
[2866] Fix | Delete
// phpcs:enable
[2867] Fix | Delete
[2868] Fix | Delete
// Uppercase the index type and normalize space characters.
[2869] Fix | Delete
$index_type = strtoupper( preg_replace( '/\s+/', ' ', trim( $index_matches['index_type'] ) ) );
[2870] Fix | Delete
[2871] Fix | Delete
// 'INDEX' is a synonym for 'KEY', standardize on 'KEY'.
[2872] Fix | Delete
$index_type = str_replace( 'INDEX', 'KEY', $index_type );
[2873] Fix | Delete
[2874] Fix | Delete
// Escape the index name with backticks. An index for a primary key has no name.
[2875] Fix | Delete
$index_name = ( 'PRIMARY KEY' === $index_type ) ? '' : '`' . strtolower( $index_matches['index_name'] ) . '`';
[2876] Fix | Delete
[2877] Fix | Delete
// Parse the columns. Multiple columns are separated by a comma.
[2878] Fix | Delete
$index_columns = array_map( 'trim', explode( ',', $index_matches['index_columns'] ) );
[2879] Fix | Delete
$index_columns_without_subparts = $index_columns;
[2880] Fix | Delete
[2881] Fix | Delete
// Normalize columns.
[2882] Fix | Delete
foreach ( $index_columns as $id => &$index_column ) {
[2883] Fix | Delete
// Extract column name and number of indexed characters (sub_part).
[2884] Fix | Delete
preg_match(
[2885] Fix | Delete
'/'
[2886] Fix | Delete
. '`?' // Name can be escaped with a backtick.
[2887] Fix | Delete
. '(?P<column_name>' // 1) Name of the column.
[2888] Fix | Delete
. '(?:[0-9a-zA-Z$_-]|[\xC2-\xDF][\x80-\xBF])+'
[2889] Fix | Delete
. ')'
[2890] Fix | Delete
. '`?' // Name can be escaped with a backtick.
[2891] Fix | Delete
. '(?:' // Optional sub part.
[2892] Fix | Delete
. '\s*' // Optional white space character between name and opening bracket.
[2893] Fix | Delete
. '\(' // Opening bracket for the sub part.
[2894] Fix | Delete
. '\s*' // Optional white space character after opening bracket.
[2895] Fix | Delete
. '(?P<sub_part>'
[2896] Fix | Delete
. '\d+' // 2) Number of indexed characters.
[2897] Fix | Delete
. ')'
[2898] Fix | Delete
. '\s*' // Optional white space character before closing bracket.
[2899] Fix | Delete
. '\)' // Closing bracket for the sub part.
[2900] Fix | Delete
. ')?'
[2901] Fix | Delete
. '/',
[2902] Fix | Delete
$index_column,
[2903] Fix | Delete
$index_column_matches
[2904] Fix | Delete
);
[2905] Fix | Delete
[2906] Fix | Delete
// Escape the column name with backticks.
[2907] Fix | Delete
$index_column = '`' . $index_column_matches['column_name'] . '`';
[2908] Fix | Delete
[2909] Fix | Delete
// We don't need to add the subpart to $index_columns_without_subparts
[2910] Fix | Delete
$index_columns_without_subparts[ $id ] = $index_column;
[2911] Fix | Delete
[2912] Fix | Delete
// Append the optional sup part with the number of indexed characters.
[2913] Fix | Delete
if ( isset( $index_column_matches['sub_part'] ) ) {
[2914] Fix | Delete
$index_column .= '(' . $index_column_matches['sub_part'] . ')';
[2915] Fix | Delete
}
[2916] Fix | Delete
}
[2917] Fix | Delete
[2918] Fix | Delete
// Build the normalized index definition and add it to the list of indices.
[2919] Fix | Delete
$indices[] = "{$index_type} {$index_name} (" . implode( ',', $index_columns ) . ')';
[2920] Fix | Delete
$indices_without_subparts[] = "{$index_type} {$index_name} (" . implode( ',', $index_columns_without_subparts ) . ')';
[2921] Fix | Delete
[2922] Fix | Delete
// Destroy no longer needed variables.
[2923] Fix | Delete
unset( $index_column, $index_column_matches, $index_matches, $index_type, $index_name, $index_columns, $index_columns_without_subparts );
[2924] Fix | Delete
[2925] Fix | Delete
break;
[2926] Fix | Delete
}
[2927] Fix | Delete
[2928] Fix | Delete
// If it's a valid field, add it to the field array.
[2929] Fix | Delete
if ( $validfield ) {
[2930] Fix | Delete
$cfields[ $fieldname_lowercased ] = $fld;
[2931] Fix | Delete
}
[2932] Fix | Delete
}
[2933] Fix | Delete
[2934] Fix | Delete
// For every field in the table.
[2935] Fix | Delete
foreach ( $tablefields as $tablefield ) {
[2936] Fix | Delete
$tablefield_field_lowercased = strtolower( $tablefield->Field );
[2937] Fix | Delete
$tablefield_type_lowercased = strtolower( $tablefield->Type );
[2938] Fix | Delete
[2939] Fix | Delete
// If the table field exists in the field array...
[2940] Fix | Delete
if ( array_key_exists( $tablefield_field_lowercased, $cfields ) ) {
[2941] Fix | Delete
[2942] Fix | Delete
// Get the field type from the query.
[2943] Fix | Delete
preg_match( '|`?' . $tablefield->Field . '`? ([^ ]*( unsigned)?)|i', $cfields[ $tablefield_field_lowercased ], $matches );
[2944] Fix | Delete
$fieldtype = $matches[1];
[2945] Fix | Delete
$fieldtype_lowercased = strtolower( $fieldtype );
[2946] Fix | Delete
[2947] Fix | Delete
// Is actual field type different from the field type in query?
[2948] Fix | Delete
if ( $tablefield->Type != $fieldtype ) {
[2949] Fix | Delete
$do_change = true;
[2950] Fix | Delete
if ( in_array( $fieldtype_lowercased, $text_fields, true ) && in_array( $tablefield_type_lowercased, $text_fields, true ) ) {
[2951] Fix | Delete
if ( array_search( $fieldtype_lowercased, $text_fields, true ) < array_search( $tablefield_type_lowercased, $text_fields, true ) ) {
[2952] Fix | Delete
$do_change = false;
[2953] Fix | Delete
}
[2954] Fix | Delete
}
[2955] Fix | Delete
[2956] Fix | Delete
if ( in_array( $fieldtype_lowercased, $blob_fields, true ) && in_array( $tablefield_type_lowercased, $blob_fields, true ) ) {
[2957] Fix | Delete
if ( array_search( $fieldtype_lowercased, $blob_fields, true ) < array_search( $tablefield_type_lowercased, $blob_fields, true ) ) {
[2958] Fix | Delete
$do_change = false;
[2959] Fix | Delete
}
[2960] Fix | Delete
}
[2961] Fix | Delete
[2962] Fix | Delete
if ( $do_change ) {
[2963] Fix | Delete
// Add a query to change the column type.
[2964] Fix | Delete
$cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN `{$tablefield->Field}` " . $cfields[ $tablefield_field_lowercased ];
[2965] Fix | Delete
[2966] Fix | Delete
$for_update[ $table . '.' . $tablefield->Field ] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
[2967] Fix | Delete
}
[2968] Fix | Delete
}
[2969] Fix | Delete
[2970] Fix | Delete
// Get the default value from the array.
[2971] Fix | Delete
if ( preg_match( "| DEFAULT '(.*?)'|i", $cfields[ $tablefield_field_lowercased ], $matches ) ) {
[2972] Fix | Delete
$default_value = $matches[1];
[2973] Fix | Delete
if ( $tablefield->Default != $default_value ) {
[2974] Fix | Delete
// Add a query to change the column's default value
[2975] Fix | Delete
$cqueries[] = "ALTER TABLE {$table} ALTER COLUMN `{$tablefield->Field}` SET DEFAULT '{$default_value}'";
[2976] Fix | Delete
[2977] Fix | Delete
$for_update[ $table . '.' . $tablefield->Field ] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}";
[2978] Fix | Delete
}
[2979] Fix | Delete
}
[2980] Fix | Delete
[2981] Fix | Delete
// Remove the field from the array (so it's not added).
[2982] Fix | Delete
unset( $cfields[ $tablefield_field_lowercased ] );
[2983] Fix | Delete
} else {
[2984] Fix | Delete
// This field exists in the table, but not in the creation queries?
[2985] Fix | Delete
}
[2986] Fix | Delete
}
[2987] Fix | Delete
[2988] Fix | Delete
// For every remaining field specified for the table.
[2989] Fix | Delete
foreach ( $cfields as $fieldname => $fielddef ) {
[2990] Fix | Delete
// Push a query line into $cqueries that adds the field to that table.
[2991] Fix | Delete
$cqueries[] = "ALTER TABLE {$table} ADD COLUMN $fielddef";
[2992] Fix | Delete
[2993] Fix | Delete
$for_update[ $table . '.' . $fieldname ] = 'Added column ' . $table . '.' . $fieldname;
[2994] Fix | Delete
}
[2995] Fix | Delete
[2996] Fix | Delete
// Index stuff goes here. Fetch the table index structure from the database.
[2997] Fix | Delete
$tableindices = $wpdb->get_results( "SHOW INDEX FROM {$table};" );
[2998] Fix | Delete
[2999] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function