Make WordPress Core

Changeset 54384


Ignore:
Timestamp:
10/04/2022 03:39:28 PM (2 years ago)
Author:
SergeyBiryukov
Message:

Database: Correct MariaDB version check in wpdb::has_cap().

MariaDB version is reported differently between PHP versions:

  • PHP 8.0.16 or later: 10.6.8-MariaDB
  • PHP 8.0.15 or earlier: 5.5.5-10.6.8-MariaDB

The latter includes PHP 7.4.x and PHP 5.6.x as well, where the version is also reported with the 5.5.5- prefix.

This commit makes an adjustment to wpdb::has_cap() to check for the correct MariaDB version.

This resolves an issue where the utf8mb4_unicode_520_ci collation, which is available in MariaDB since version 10.2, was previously not detected correctly.

References:

Follow-up to [37523], [53919].

Props jamieburchell, SergeyBiryukov.
Fixes #54841.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wpdb.php

    r54240 r54384  
    39613961     */
    39623962    public function has_cap( $db_cap ) {
    3963         $version = $this->db_version();
     3963        $db_version     = $this->db_version();
     3964        $db_server_info = $this->db_server_info();
     3965
     3966        // Account for MariaDB version being prefixed with '5.5.5-' on older PHP versions.
     3967        if ( '5.5.5' === $db_version && str_contains( $db_server_info, 'MariaDB' )
     3968            && PHP_VERSION_ID < 80016 // PHP 8.0.15 or older.
     3969        ) {
     3970            // Strip the '5.5.5-' prefix and set the version to the correct value.
     3971            $db_server_info = preg_replace( '/^5\.5\.5-(.*)/', '$1', $db_server_info );
     3972            $db_version     = preg_replace( '/[^0-9.].*/', '', $db_server_info );
     3973        }
    39643974
    39653975        switch ( strtolower( $db_cap ) ) {
     
    39673977            case 'group_concat': // @since 2.7.0
    39683978            case 'subqueries':   // @since 2.7.0
    3969                 return version_compare( $version, '4.1', '>=' );
     3979                return version_compare( $db_version, '4.1', '>=' );
    39703980            case 'set_charset':
    3971                 return version_compare( $version, '5.0.7', '>=' );
     3981                return version_compare( $db_version, '5.0.7', '>=' );
    39723982            case 'utf8mb4':      // @since 4.1.0
    3973                 if ( version_compare( $version, '5.5.3', '<' ) ) {
     3983                if ( version_compare( $db_version, '5.5.3', '<' ) ) {
    39743984                    return false;
    39753985                }
     
    39914001                }
    39924002            case 'utf8mb4_520': // @since 4.6.0
    3993                 return version_compare( $version, '5.6', '>=' );
     4003                return version_compare( $db_version, '5.6', '>=' );
    39944004            case 'identifier_placeholders': // @since 6.1.0
    39954005                /*
  • trunk/tests/phpunit/tests/db/charset.php

    r53919 r54384  
    4848
    4949        // Account for MariaDB version being prefixed with '5.5.5-' on older PHP versions.
    50         if ( str_contains( self::$db_server_info, 'MariaDB' ) && '5.5.5' === self::$db_version
     50        if ( '5.5.5' === self::$db_version && str_contains( self::$db_server_info, 'MariaDB' )
    5151            && PHP_VERSION_ID < 80016 // PHP 8.0.15 or older.
    5252        ) {
     
    11901190        self::$_wpdb->set_charset( self::$_wpdb->dbh );
    11911191    }
     1192
     1193    /**
     1194     * @ticket 54841
     1195     */
     1196    public function test_mariadb_supports_utf8mb4_520() {
     1197        global $wpdb;
     1198
     1199        // utf8mb4_520 is available in MariaDB since version 10.2.
     1200        if ( ! str_contains( self::$db_server_info, 'MariaDB' )
     1201            || version_compare( self::$db_version, '10.2', '<' )
     1202        ) {
     1203            $this->markTestSkipped( 'This test requires MariaDB 10.2 or later.' );
     1204        }
     1205
     1206        $this->assertTrue( $wpdb->has_cap( 'utf8mb4_520' ) );
     1207    }
    11921208}
Note: See TracChangeset for help on using the changeset viewer.