diff --git a/features/config-create.feature b/features/config-create.feature index c93bb604..fd21e7a3 100644 --- a/features/config-create.feature +++ b/features/config-create.feature @@ -290,6 +290,23 @@ 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 an empty directory + And WP files + And a wp-content/db.php file: + """ + + * [--dbname=] * : Set the database name. * - * --dbuser= + * [--dbuser=] * : Set the database user. * * [--dbpass=] @@ -219,6 +219,18 @@ public function create( $_, $assoc_args ) { 'ssl' => false, ]; $assoc_args = array_merge( $defaults, $assoc_args ); + + $is_sqlite = self::is_sqlite_integration_active(); + + if ( ! $is_sqlite ) { + if ( empty( $assoc_args['dbname'] ) ) { + WP_CLI::error( 'Parameter errors:' . PHP_EOL . 'missing --dbname parameter (Set the database name.)' ); + } + if ( empty( $assoc_args['dbuser'] ) ) { + WP_CLI::error( 'Parameter errors:' . PHP_EOL . 'missing --dbuser parameter (Set the database user.)' ); + } + } + if ( empty( $assoc_args['dbprefix'] ) ) { WP_CLI::error( '--dbprefix cannot be empty' ); } @@ -228,7 +240,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(); @@ -1480,6 +1492,25 @@ private function print_dotenv( array $value ) { WP_CLI::line( "{$name}={$variable_value}" ); } + /** + * Check if the SQLite integration drop-in is active. + * + * @return bool True if SQLite integration is detected, false otherwise. + */ + private static function is_sqlite_integration_active() { + $wp_content_dir = defined( 'WP_CONTENT_DIR' ) ? WP_CONTENT_DIR : ABSPATH . 'wp-content'; + $db_dropin_path = $wp_content_dir . '/db.php'; + + if ( file_exists( $db_dropin_path ) ) { + $db_dropin_contents = file_get_contents( $db_dropin_path, false, null, 0, 8192 ); + if ( false !== $db_dropin_contents && false !== strpos( $db_dropin_contents, 'SQLITE_DB_DROPIN_VERSION' ) ) { + return true; + } + } + + return false; + } + /** * Escape a config value so it can be safely used within single quotes. *