Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 81 additions & 1 deletion php/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@
// Get curl error code safely - only if curl is available and handle is valid.
$curl_errno = null;
if ( function_exists( 'curl_errno' ) && ( is_resource( $curl_handle ) || ( is_object( $curl_handle ) && $curl_handle instanceof \CurlHandle ) ) ) {
$curl_errno = curl_errno( $curl_handle );

Check failure on line 908 in php/utils.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Parameter #1 $handle of function curl_errno expects CurlHandle, CurlHandle|resource given.
}
// CURLE_SSL_CACERT = 60
$is_ssl_cacert_error = null !== $curl_errno && 60 === $curl_errno;
Expand All @@ -927,7 +927,7 @@
// Get curl error code safely - only if curl is available and handle is valid.
$curl_errno = null;
if ( function_exists( 'curl_errno' ) && ( is_resource( $curl_handle ) || ( is_object( $curl_handle ) && $curl_handle instanceof \CurlHandle ) ) ) {
$curl_errno = curl_errno( $curl_handle );

Check failure on line 930 in php/utils.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Parameter #1 $handle of function curl_errno expects CurlHandle, CurlHandle|resource given.
}
// CURLE_SSL_CONNECT_ERROR = 35, CURLE_SSL_CERTPROBLEM = 58, CURLE_SSL_CACERT_BADFILE = 77
$is_ssl_error = null !== $curl_errno && in_array( $curl_errno, [ 35, 58, 77 ], true );
Expand Down Expand Up @@ -2028,13 +2028,93 @@
return $version;
}

/**
* Determine which SQL dump binary should be used for export operations.
*
* This helper allows environments to override the dump binary used by WP-CLI
* by defining the `WP_CLI_MYSQLDUMP` environment variable.
*
* Behaviour:
* - If `WP_CLI_MYSQLDUMP` is set to an absolute path, it will be used only
* when the file exists and is executable.
* - If the variable is set to a bare command name (e.g., "mysqldump"),
* WP-CLI will defer to the system `PATH` resolver without further checks.
* - If the variable is not set or unusable, WP-CLI falls back to:
* • `mariadb-dump` on MariaDB systems
* • `mysqldump` otherwise
*
* Examples:
* # Use a specific absolute path
* WP_CLI_MYSQLDUMP=/usr/local/bin/mysqldump wp db export
*
* # Force mysqldump even on MariaDB systems
* WP_CLI_MYSQLDUMP=mysqldump wp db export
*
* @return string The executable (binary or absolute path) to use for SQL dump operations.
*/
function get_mysql_dump_binary() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 main concerns at first glance:

  1. This can just be inlined in get_sql_dump_command. No need for a new function
  2. No need for the extensive logging

$env = getenv( 'WP_CLI_MYSQLDUMP' );

// Honour explicit override when provided.
if ( is_string( $env ) && '' !== $env ) {

// Command name override: allow system PATH resolution.
if ( ! is_path_absolute( $env ) ) {
WP_CLI::debug(
sprintf(
'Using dump command from WP_CLI_MYSQLDUMP via PATH: %s',
$env
),
'db'
);

return $env;
}

// Absolute path override: only trust when executable.
if ( is_executable( $env ) ) {
WP_CLI::debug(
sprintf(
'Using dump command from WP_CLI_MYSQLDUMP: %s',
$env
),
'db'
);

return $env;
}

// Absolute path but not executable → log skip so users know why it was ignored.
WP_CLI::debug(
sprintf(
'Ignoring non-executable dump command from WP_CLI_MYSQLDUMP: %s',
$env
),
'db'
);
}

// Fallback: detect DB type and choose the appropriate default binary.
$binary = ( 'mariadb' === get_db_type() ) ? 'mariadb-dump' : 'mysqldump';

WP_CLI::debug(
sprintf(
'Using default dump command: %s',
$binary
),
'db'
);

return $binary;
}

/**
* Returns the correct `dump` command based on the detected database type.
*
* @return string The appropriate dump command.
*/
function get_sql_dump_command() {
return 'mariadb' === get_db_type() ? 'mariadb-dump' : 'mysqldump';
return get_mysql_dump_binary();
}

/**
Expand Down
Loading