WordPress.org

Make WordPress Core

Opened 11 months ago

Closed 3 weeks ago

#27703 closed enhancement (maybelater)

Need additional db version information in WP3.9+

Reported by: webaware Owned by: pento
Milestone: Priority: normal
Severity: normal Version: 3.9
Component: Database Keywords:
Focuses: administration Cc:

Description

We urgently need a new method on class wpdb that returns the raw database version information string.

$wpdb->db_version() strips out important information required to assist plugin writers with debugging, such as the fact that the database is MariaDB not MySQL. To get around that, many plugins use mysql_get_server_info() to get the raw info string. e.g. to pick just a few:

  • Easy Digital Downloads
  • Jigoshop
  • NextGEN Gallery / Nextcellent Gallery
  • WooCommerce
  • WP Migrate DB Pro

In WordPress 3.9+ using mysqli, this throws a Deprecated warning, which is reasonable since mysqli_get_server_info($wpdb->dbh) should be called instead. Since dbh is a protected member and use_mysqli is a private member, plugin writers have no easy way to access this. We need a new method on wpdb that provides the raw string returned by mysqli_get_server_info.

Plugin writers can then test for that new method with method_exists(), and call it in preference to mysql_get_server_info().

I reckon this really should make it into 3.9, since it affects a bunch of major WordPress plugins.

Attachments (2)

27703.diff (4.4 KB) - added by pento 11 months ago.
27703.2.diff (853 bytes) - added by pento 11 months ago.

Download all attachments as: .zip

Change History (14)

comment:1 @nacin11 months ago

I'm curious, how does knowing it is MariaDB versus MySQL help? For what is the plugin using this information?

comment:2 @webaware11 months ago

Allegedly, dropping MariaDB into a server in place of MySQL will be totally transparent. In practice, different builds of MySQL can have subtle differences that can affect database queries, so why wouldn't MariaDB vs MySQL exhibit differences too? Basically, more information is better when debugging especially when a problem can't be replicated in a developer's environment. You want to find out what the reporter's environment is, which is why mysql_get_server_info() is being called by plugin writers in preference to $wpdb->db_version().

@pento11 months ago

@pento11 months ago

comment:3 @pento11 months ago

  • Keywords has-patch commit added
  • Milestone changed from Awaiting Review to 3.9
  • Owner set to pento
  • Status changed from new to assigned
  • Type changed from defect (bug) to enhancement

attachment:27703.2.diff adds an optional flag to $wpdb->db_version() to return the full version string.

In a brief search of the WP.org plugin repo, I couldn't find any plugins doing things with the version number other than echo-ing it, or version_compare()-ing it.

That said, I have no problem with providing a standard method for getting extra information about the server.

comment:4 @webaware11 months ago

The problem with doing this as part of the same method is that plugin writers will need to version_compare() the WordPress version to detect this feature. Making a separate method allows feature detection, rather than version sniffing for feature inference.

comment:5 @pento11 months ago

  • Keywords has-patch commit removed
  • Milestone changed from 3.9 to Future Release

On further discussion, fixing db_version() is going to be significantly more difficult than I initially thought. It's currently being used to compare against version numbers, which breaks for anything except vanilla MySQL. Other DB servers, such as MariaDB, Postgres, Drizzle, SQLite and SQL Server all have different version numbering.

I suspect in the future we'll offer extended version information, but it would need to offer full information, rather than just a string.

In the mean time, plugin developers can work around this with the following:

if ( $wpdb->use_mysqli ) {
	$ver = mysqli_get_server_info( $wpdb->dbh );
} else {
	$ver = mysql_get_server_info();
}

Note that use_mysqli and dbh can indeed be accessed, as wpdb has a magic getter.

comment:6 @webaware11 months ago

Cool, had not noticed the __get(), and it's @since 3.5.0 which is good enough for us.

comment:7 @webaware11 months ago

NB: because WP pre-3.9 doesn't have the use_mysqli property, you need to test the WP version before testing that property. e.g.

global $wpdb, $wp_version;
if ( version_compare( $wp_version, '3.8.9999', '>' ) && $wpdb->use_mysqli ) {
    $info = mysqli_get_server_info( $wpdb->dbh );
}
else {
    $info = mysql_get_server_info();
}

comment:8 @nacin11 months ago

Just if ( $wpdb->use_mysqli ) would be fine, as it'll be a null value before 3.9. I'm not even sure it'd throw a notice. (If it does, you could use if ( ! empty( $wpdb->use_mysqli ) ).

comment:9 @webaware11 months ago

Was going to say that empty() doesn't work on magic properties, but you have __isset() in that class too -- excellent! Yes, was throwing a notice, but empty() works fine. Thanks!

comment:10 @pento6 months ago

  • Milestone Future Release deleted
  • Resolution set to maybelater
  • Status changed from assigned to closed

comment:11 @nicocasel3 weeks ago

  • Resolution maybelater deleted
  • Status changed from closed to reopened

Hello,

I do not found any info about $wpdb->db_version()
at:
http://codex.wordpress.org/Class_Reference/wpdb#Code_Documentation

As the GD Star Rating plugin still works with mysql_get_server_info(), I would like to modify it for:
mysql_get_server_info($wpdb->db_version())

As mentioned at:
https://make.wordpress.org/core/2014/04/07/mysql-in-wordpress-3-9/#respond

comment:12 @dd323 weeks ago

  • Resolution set to maybelater
  • Status changed from reopened to closed

@nicocasel, A db_version() method wasn't implemented as it wasn't required.

If you require that function you can use the code as shown here:
https://make.wordpress.org/core/2014/04/07/mysql-in-wordpress-3-9/#comment-13174

if ( empty( $wpdb->use_mysqli ) ) {
    $ver = mysql_get_server_info();
} else {
    $ver = mysqli_get_server_info( $wpdb->dbh );
}
Note: See TracTickets for help on using tickets.