diff --git a/features/config-create.feature b/features/config-create.feature index c93bb604..de7113b6 100644 --- a/features/config-create.feature +++ b/features/config-create.feature @@ -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 diff --git a/src/Config_Command.php b/src/Config_Command.php index 63ac1f55..e361a34d 100644 --- a/src/Config_Command.php +++ b/src/Config_Command.php @@ -120,10 +120,10 @@ private static function get_initial_locale() { * * ## OPTIONS * - * --dbname= + * [--dbname=] * : Set the database name. * - * --dbuser= + * [--dbuser=] * : Set the database user. * * [--dbpass=] @@ -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' ); } @@ -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(); @@ -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' ); + } }