wpdb::check_safe_collation( string $query ): bool
- Since
- 4.2.0
- Source
wp-includes/class-wpdb.php:3475
Compatibility
- WordPress
- since 4.2.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$querystring- The query to check.
Return value
bool- True if the collation is safe, false if it isn't.
Performance profile
How much work a call to wpdb::check_safe_collation() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Light
- Scaling
- Scales with input
- Instructions
- 4–49
- Plugin surface
- None
- Called by
- 4
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 57.
Nothing here hands control to plugin code.
4 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wpdb::check_safe_collation() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 4 | none |
$query | 11 | ltrim() |
!$query && ->check_ascii() | 15 | ltrim(), ->check_ascii() |
!$query && !->check_ascii() | 20 | ltrim(), ->check_ascii(), ->get_table_from_query() |
!$query && !->check_ascii() | 30–32 | ltrim(), ->check_ascii(), ->get_table_from_query(), ->get_table_charset() |
!$query && !->check_ascii() && $collation !== false && $collation !== "latin1" | 39–49 | ltrim(), ->check_ascii(), ->get_table_from_query(), ->get_table_charset(), strtolower() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 57 | 4–49 | 11 | |
| 8.5 | 57 | 4–49 | 11 | |
| 8.4 | 57 | 4–49 | 11 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 60 | 4–52 | 11 | |
| 8.2 | 60 | 4–52 | 11 | |
| 8.1 | 60 | 4–52 | 11 | |
| 7.4 | 60 | 4–52 | 11 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 3
- wpdb::check_ascii()Checks if a string is ASCII.
- wpdb::get_table_from_query()Finds the first table name referenced in a query.
- wpdb::get_table_charset()Retrieves the character set for the given table.
Used by · 4
- wpdb::get_col()Retrieves one column from the database.
- wpdb::get_results()Retrieves an entire SQL result set from the database (i.e., many rows).
- wpdb::get_row()Retrieves one row from the database.
- wpdb::get_var()Retrieves one value from the database.
Source code
protected function check_safe_collation( $query ) { if ( $this->checking_collation ) { return true; } // We don't need to check the collation for queries that don't read data. $query = ltrim( $query, "\r\n\t (" ); if ( preg_match( '/^(?:SHOW|DESCRIBE|DESC|EXPLAIN|CREATE)\s/i', $query ) ) { return true; } // All-ASCII queries don't need extra checking. if ( $this->check_ascii( $query ) ) { return true; } $table = $this->get_table_from_query( $query ); if ( ! $table ) { return false; } $this->checking_collation = true; $collation = $this->get_table_charset( $table ); $this->checking_collation = false; // Tables with no collation, or latin1 only, don't need extra checking. if ( false === $collation || 'latin1' === $collation ) { return true; } $table = strtolower( $table ); if ( empty( $this->col_meta[ $table ] ) ) { return false; } // If any of the columns don't have one of these collations, it needs more confidence checking. $safe_collations = array( 'utf8_bin', 'utf8_general_ci', 'utf8mb3_bin', 'utf8mb3_general_ci', 'utf8mb4_bin', 'utf8mb4_general_ci', ); foreach ( $this->col_meta[ $table ] as $col ) { if ( empty( $col->Collation ) ) { continue; } if ( ! in_array( $col->Collation, $safe_collations, true ) ) { return false; } } return true; }Changelog
Introduced in 4.2.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 tag, from
src/wp-includes/class-wpdb.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.