Skip to content
Open
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions features/config-create.feature
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,33 @@ Feature: Create a wp-config file
Then the return code should be 0
And the subdir/wp-config.php file should exist

@require-sqlite
Scenario: Configure without --dbname and --dbuser when SQLite integration is active
Given a WP install
When I run `rm wp-config.php`
When I run `wp config create --skip-salts`
Then the return code should be 0
And STDOUT should contain:
"""
Generated 'wp-config.php' file.
"""

@skip-sqlite
Scenario: Error when --dbname and --dbuser are missing without SQLite
Given an empty directory
And WP files

When I try `wp config create --skip-check --skip-salts`
Then the return code should be 1
And STDERR should contain:
"""
missing --dbname parameter
"""
And STDERR should contain:
"""
missing --dbuser parameter
"""

@require-mysql @require-mysql-5.7
Scenario: Configure with required SSL connection
Given an empty directory
Expand Down
42 changes: 39 additions & 3 deletions src/Config_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ private static function get_initial_locale() {
*
* ## OPTIONS
*
* --dbname=<dbname>
* [--dbname=<dbname>]
* : Set the database name.
*
* --dbuser=<dbuser>
* [--dbuser=<dbuser>]
* : Set the database user.
*
* [--dbpass=<dbpass>]
Expand Down Expand Up @@ -219,6 +219,24 @@ public function create( $_, $assoc_args ) {
'ssl' => false,
];
$assoc_args = array_merge( $defaults, $assoc_args );

$is_sqlite = self::is_sqlite_integration_active();

if ( ! $is_sqlite ) {
$errors = [];
if ( ! isset( $assoc_args['dbname'] ) ) {
$errors[] = 'missing --dbname parameter';
}
if ( ! isset( $assoc_args['dbuser'] ) ) {
$errors[] = 'missing --dbuser parameter';
}
if ( ! empty( $errors ) ) {
WP_CLI::error(
'Parameter errors:' . "\n " . implode( "\n ", $errors )
);
}
}

if ( empty( $assoc_args['dbprefix'] ) ) {
WP_CLI::error( '--dbprefix cannot be empty' );
}
Expand All @@ -228,7 +246,7 @@ public function create( $_, $assoc_args ) {

// Check DB connection. To make command more portable, we are not using MySQL CLI and using
// mysqli directly instead, as $wpdb is not accessible in this context.
if ( ! Utils\get_flag_value( $assoc_args, 'skip-check' ) ) {
if ( ! $is_sqlite && ! Utils\get_flag_value( $assoc_args, 'skip-check' ) ) {
// phpcs:disable WordPress.DB.RestrictedFunctions
$mysql = mysqli_init();

Expand Down Expand Up @@ -1506,4 +1524,22 @@ private function escape_config_value( $key, $value ) {

return $value;
}

/**
* Check if the SQLite integration drop-in is active.
*
* @return bool
*/
private static function is_sqlite_integration_active() {
$wp_content_dir = defined( 'WP_CONTENT_DIR' ) ? WP_CONTENT_DIR : rtrim( ABSPATH, '/\\' ) . '/wp-content';
$db_dropin_path = $wp_content_dir . '/db.php';

if ( ! is_readable( $db_dropin_path ) ) {
return false;
}

$contents = file_get_contents( $db_dropin_path );

return false !== $contents && false !== strpos( $contents, 'SQLITE_DB_DROPIN_VERSION' );
}
}
Loading