Make WordPress Core

Changeset 57926


Ignore:
Timestamp:
04/04/2024 03:11:37 PM (7 weeks ago)
Author:
johnbillion
Message:

Database: Remove back-compat for database servers that don't support utf8mb4.

Since WordPress 6.5, the minimum supported version of MySQL and MariaDB is 5.5.5. This means all supported database servers now support the utf8mb4 character set and therefore the conditional logic for this is no longer necessary.

Props l1nuxjedi, craigfrancis, OllieJones, johnbillion

Fixes #60096

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-site-health.php

    r57793 r57926  
    12851285
    12861286    /**
    1287      * Tests if the database server is capable of using utf8mb4.
    1288      *
    1289      * @since 5.2.0
    1290      *
    1291      * @return array The test results.
    1292      */
    1293     public function get_test_utf8mb4_support() {
    1294         if ( ! $this->mysql_server_version ) {
    1295             $this->prepare_sql_data();
    1296         }
    1297 
    1298         $result = array(
    1299             'label'       => __( 'UTF8MB4 is supported' ),
    1300             'status'      => 'good',
    1301             'badge'       => array(
    1302                 'label' => __( 'Performance' ),
    1303                 'color' => 'blue',
    1304             ),
    1305             'description' => sprintf(
    1306                 '<p>%s</p>',
    1307                 __( 'UTF8MB4 is the character set WordPress prefers for database storage because it safely supports the widest set of characters and encodings, including Emoji, enabling better support for non-English languages.' )
    1308             ),
    1309             'actions'     => '',
    1310             'test'        => 'utf8mb4_support',
    1311         );
    1312 
    1313         if ( ! $this->is_mariadb ) {
    1314             if ( version_compare( $this->mysql_server_version, '5.5.3', '<' ) ) {
    1315                 $result['status'] = 'recommended';
    1316 
    1317                 $result['label'] = __( 'utf8mb4 requires a MySQL update' );
    1318 
    1319                 $result['description'] .= sprintf(
    1320                     '<p>%s</p>',
    1321                     sprintf(
    1322                         /* translators: %s: Version number. */
    1323                         __( 'WordPress&#8217; utf8mb4 support requires MySQL version %s or greater. Please contact your server administrator.' ),
    1324                         '5.5.3'
    1325                     )
    1326                 );
    1327             } else {
    1328                 $result['description'] .= sprintf(
    1329                     '<p>%s</p>',
    1330                     __( 'Your MySQL version supports utf8mb4.' )
    1331                 );
    1332             }
    1333         } else { // MariaDB introduced utf8mb4 support in 5.5.0.
    1334             if ( version_compare( $this->mysql_server_version, '5.5.0', '<' ) ) {
    1335                 $result['status'] = 'recommended';
    1336 
    1337                 $result['label'] = __( 'utf8mb4 requires a MariaDB update' );
    1338 
    1339                 $result['description'] .= sprintf(
    1340                     '<p>%s</p>',
    1341                     sprintf(
    1342                         /* translators: %s: Version number. */
    1343                         __( 'WordPress&#8217; utf8mb4 support requires MariaDB version %s or greater. Please contact your server administrator.' ),
    1344                         '5.5.0'
    1345                     )
    1346                 );
    1347             } else {
    1348                 $result['description'] .= sprintf(
    1349                     '<p>%s</p>',
    1350                     __( 'Your MariaDB version supports utf8mb4.' )
    1351                 );
    1352             }
    1353         }
    1354 
    1355         // phpcs:ignore WordPress.DB.RestrictedFunctions.mysql_mysqli_get_client_info
    1356         $mysql_client_version = mysqli_get_client_info();
    1357 
    1358         /*
    1359          * libmysql has supported utf8mb4 since 5.5.3, same as the MySQL server.
    1360          * mysqlnd has supported utf8mb4 since 5.0.9.
    1361          */
    1362         if ( str_contains( $mysql_client_version, 'mysqlnd' ) ) {
    1363             $mysql_client_version = preg_replace( '/^\D+([\d.]+).*/', '$1', $mysql_client_version );
    1364             if ( version_compare( $mysql_client_version, '5.0.9', '<' ) ) {
    1365                 $result['status'] = 'recommended';
    1366 
    1367                 $result['label'] = __( 'utf8mb4 requires a newer client library' );
    1368 
    1369                 $result['description'] .= sprintf(
    1370                     '<p>%s</p>',
    1371                     sprintf(
    1372                         /* translators: 1: Name of the library, 2: Number of version. */
    1373                         __( 'WordPress&#8217; utf8mb4 support requires MySQL client library (%1$s) version %2$s or newer. Please contact your server administrator.' ),
    1374                         'mysqlnd',
    1375                         '5.0.9'
    1376                     )
    1377                 );
    1378             }
    1379         } else {
    1380             if ( version_compare( $mysql_client_version, '5.5.3', '<' ) ) {
    1381                 $result['status'] = 'recommended';
    1382 
    1383                 $result['label'] = __( 'utf8mb4 requires a newer client library' );
    1384 
    1385                 $result['description'] .= sprintf(
    1386                     '<p>%s</p>',
    1387                     sprintf(
    1388                         /* translators: 1: Name of the library, 2: Number of version. */
    1389                         __( 'WordPress&#8217; utf8mb4 support requires MySQL client library (%1$s) version %2$s or newer. Please contact your server administrator.' ),
    1390                         'libmysql',
    1391                         '5.5.3'
    1392                     )
    1393                 );
    1394             }
    1395         }
    1396 
    1397         return $result;
    1398     }
    1399 
    1400     /**
    14011287     * Tests if the site can communicate with WordPress.org.
    14021288     *
     
    27402626                    'test'  => 'sql_server',
    27412627                ),
    2742                 'utf8mb4_support'              => array(
    2743                     'label' => __( 'MySQL utf8mb4 support' ),
    2744                     'test'  => 'utf8mb4_support',
    2745                 ),
    27462628                'ssl_support'                  => array(
    27472629                    'label' => __( 'Secure communication' ),
  • trunk/src/wp-admin/setup-config.php

    r57793 r57926  
    397397                    break;
    398398                case 'DB_CHARSET':
    399                     if ( 'utf8mb4' === $wpdb->charset || ( ! $wpdb->charset && $wpdb->has_cap( 'utf8mb4' ) ) ) {
     399                    if ( 'utf8mb4' === $wpdb->charset || ( ! $wpdb->charset ) ) {
    400400                        $config_file[ $line_num ] = "define( '" . $constant . "'," . $padding . "'utf8mb4' );\r\n";
    401401                    }
  • trunk/src/wp-includes/class-wpdb.php

    r57239 r57926  
    879879        }
    880880
    881         if ( 'utf8' === $charset && $this->has_cap( 'utf8mb4' ) ) {
     881        if ( 'utf8' === $charset ) {
    882882            $charset = 'utf8mb4';
    883         }
    884 
    885         if ( 'utf8mb4' === $charset && ! $this->has_cap( 'utf8mb4' ) ) {
    886             $charset = 'utf8';
    887             $collate = str_replace( 'utf8mb4_', 'utf8_', $collate );
    888883        }
    889884
     
    32433238                list( $charset ) = explode( '_', $column->Collation );
    32443239
    3245                 // If the current connection can't support utf8mb4 characters, let's only send 3-byte utf8 characters.
    3246                 if ( 'utf8mb4' === $charset && ! $this->has_cap( 'utf8mb4' ) ) {
    3247                     $charset = 'utf8';
    3248                 }
    3249 
    32503240                $charsets[ strtolower( $charset ) ] = true;
    32513241            }
     
    40584048     * @since 4.6.0 Added support for the 'utf8mb4_520' feature.
    40594049     * @since 6.2.0 Added support for the 'identifier_placeholders' feature.
     4050     * @since 6.6.0 The `utf8mb4` feature now always returns true.
    40604051     *
    40614052     * @see wpdb::db_version()
     
    40934084                return version_compare( $db_version, '5.0.7', '>=' );
    40944085            case 'utf8mb4':      // @since 4.1.0
    4095                 if ( version_compare( $db_version, '5.5.3', '<' ) ) {
    4096                     return false;
    4097                 }
    4098 
    4099                 $client_version = mysqli_get_client_info();
    4100 
    4101                 /*
    4102                  * libmysql has supported utf8mb4 since 5.5.3, same as the MySQL server.
    4103                  * mysqlnd has supported utf8mb4 since 5.0.9.
    4104                  *
    4105                  * Note: str_contains() is not used here, as this file can be included
    4106                  * directly outside of WordPress core, e.g. by HyperDB, in which case
    4107                  * the polyfills from wp-includes/compat.php are not loaded.
    4108                  */
    4109                 if ( false !== strpos( $client_version, 'mysqlnd' ) ) {
    4110                     $client_version = preg_replace( '/^\D+([\d.]+).*/', '$1', $client_version );
    4111                     return version_compare( $client_version, '5.0.9', '>=' );
    4112                 } else {
    4113                     return version_compare( $client_version, '5.5.3', '>=' );
    4114                 }
     4086                return true;
    41154087            case 'utf8mb4_520': // @since 4.6.0
    41164088                return version_compare( $db_version, '5.6', '>=' );
  • trunk/tests/phpunit/tests/db.php

    r57089 r57926  
    14401440        global $wpdb;
    14411441
    1442         if ( ! $wpdb->has_cap( 'utf8mb4' ) ) {
    1443             $this->markTestSkipped( 'This test requires utf8mb4 support.' );
    1444         }
    1445 
    14461442        $charset = 'utf8';
    14471443        $collate = 'utf8_general_ci';
     
    14781474        global $wpdb;
    14791475
    1480         if ( ! $wpdb->has_cap( 'utf8mb4' ) ) {
    1481             $this->markTestSkipped( 'This test requires utf8mb4 support.' );
    1482         }
    1483 
    14841476        $charset = 'utf8';
    14851477        $collate = 'utf8_swedish_ci';
     
    14881480
    14891481        $this->assertSame( 'utf8mb4_swedish_ci', $result['collate'] );
    1490     }
    1491 
    1492     /**
    1493      * @ticket 37982
    1494      */
    1495     public function test_charset_switched_to_utf8() {
    1496         global $wpdb;
    1497 
    1498         if ( $wpdb->has_cap( 'utf8mb4' ) ) {
    1499             $this->markTestSkipped( 'This test requires utf8mb4 to not be supported.' );
    1500         }
    1501 
    1502         $charset = 'utf8mb4';
    1503         $collate = 'utf8mb4_general_ci';
    1504 
    1505         $result = $wpdb->determine_charset( $charset, $collate );
    1506 
    1507         $this->assertSame( 'utf8', $result['charset'] );
    1508         $this->assertSame( 'utf8_general_ci', $result['collate'] );
    15091482    }
    15101483
  • trunk/tests/phpunit/tests/db/charset.php

    r57239 r57926  
    521521        } else {
    522522            $new_charset = $data[0]['charset'];
    523         }
    524 
    525         if ( 'utf8mb4' === $new_charset && ! self::$_wpdb->has_cap( 'utf8mb4' ) ) {
    526             $this->markTestSkipped( "The current MySQL server doesn't support the utf8mb4 character set." );
    527523        }
    528524
     
    796792    public function test_get_table_charset( $drop, $create, $table, $expected_charset ) {
    797793        self::$_wpdb->query( $drop );
    798 
    799         if ( ! self::$_wpdb->has_cap( 'utf8mb4' ) && preg_match( '/utf8mb[34]/i', $create ) ) {
    800             $this->markTestSkipped( "This version of MySQL doesn't support utf8mb4." );
    801         }
    802 
    803794        self::$_wpdb->query( $create );
    804795
     
    837828    public function test_get_column_charset( $drop, $create, $table, $expected_charset ) {
    838829        self::$_wpdb->query( $drop );
    839 
    840         if ( ! self::$_wpdb->has_cap( 'utf8mb4' ) && preg_match( '/utf8mb[34]/i', $create ) ) {
    841             $this->markTestSkipped( "This version of MySQL doesn't support utf8mb4." );
    842         }
    843 
    844830        self::$_wpdb->query( $create );
    845831
     
    865851        self::$_wpdb->query( $drop );
    866852
    867         if ( ! self::$_wpdb->has_cap( 'utf8mb4' ) && preg_match( '/utf8mb[34]/i', $create ) ) {
    868             $this->markTestSkipped( "This version of MySQL doesn't support utf8mb4." );
    869         }
    870 
    871853        self::$_wpdb->is_mysql = false;
    872854
     
    891873    public function test_get_column_charset_is_mysql_undefined( $drop, $create, $table, $columns ) {
    892874        self::$_wpdb->query( $drop );
    893 
    894         if ( ! self::$_wpdb->has_cap( 'utf8mb4' ) && preg_match( '/utf8mb[34]/i', $create ) ) {
    895             $this->markTestSkipped( "This version of MySQL doesn't support utf8mb4." );
    896         }
    897875
    898876        unset( self::$_wpdb->is_mysql );
     
    953931    public function test_strip_invalid_text_from_query( $create, $query, $expected, $drop ) {
    954932        self::$_wpdb->query( $drop );
    955 
    956         if ( ! self::$_wpdb->has_cap( 'utf8mb4' ) && preg_match( '/utf8mb[34]/i', $create ) ) {
    957             $this->markTestSkipped( "This version of MySQL doesn't support utf8mb4." );
    958         }
    959 
    960933        self::$_wpdb->query( $create );
    961934
  • trunk/tests/phpunit/tests/db/dbDelta.php

    r56971 r57926  
    427427        global $wpdb;
    428428
    429         if ( ! $wpdb->has_cap( 'utf8mb4' ) ) {
    430             $this->markTestSkipped( 'This test requires utf8mb4 support in MySQL.' );
    431         }
    432 
    433429        // This table needs to be actually created.
    434430        remove_filter( 'query', array( $this, '_create_temporary_tables' ) );
Note: See TracChangeset for help on using the changeset viewer.