Make WordPress Core

Opened 11 years ago

Closed 10 years ago

Last modified 5 years ago

#27703 closed enhancement (fixed)

Need additional db version information in WP3.9+

Reported by: webaware's profile webaware Owned by: pento's profile pento
Milestone: 5.5 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 years ago.
27703.2.diff (853 bytes) - added by pento 11 years ago.

Download all attachments as: .zip

Change History (20)

#1 @nacin
11 years ago

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

#2 @webaware
11 years 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().

@pento
11 years ago

@pento
11 years ago

#3 @pento
11 years 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.

#4 @webaware
11 years 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.

#5 @pento
11 years 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.

#6 @webaware
11 years ago

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

#7 @webaware
11 years 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();
}

#8 @nacin
11 years 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 ) ).

#9 @webaware
11 years 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!

#10 @pento
11 years ago

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

#11 @nicocasel
10 years 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

#12 @dd32
10 years 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 );
}

This ticket was mentioned in Slack in #core by krmoorhouse. View the logs.


6 years ago

This ticket was mentioned in Slack in #core-site-health by sergey. View the logs.


5 years ago

#17 @SergeyBiryukov
5 years ago

In 47451:

Database: Introduce wpdb::db_server_info() to retrieve full MySQL server information string as supplied by mysqli_get_server_info().

This complements wpdb::db_version(), which only returns a numeric version string and strips any additional information, e.g. vendor name.

Props clarinetlord, birgire, webaware, pento.
Fixes #40037. See #27703.

#18 @SergeyBiryukov
5 years ago

  • Milestone set to 5.5
  • Resolution changed from maybelater to fixed
Note: See TracTickets for help on using tickets.