Skip to content

Commit 776c55b

Browse files
committed
fix: resolve SQLite database path via DB_PATH and recorded secret path
Mirror the sqlite-database-integration 3.1 resolution order in get_sqlite_db_path(): DB_PATH, then FQDB, then FQDBDIR combined with DB_FILE, then the recorded secret path in FQDBDIR/db-path.php, and finally the legacy .ht.sqlite locations. Add Behat scenarios for an explicit DB_PATH outside the web root and for db-path.php resolution.
1 parent d640db8 commit 776c55b

2 files changed

Lines changed: 102 additions & 3 deletions

File tree

‎features/db-sqlite-path.feature‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,87 @@ Feature: Select the configured SQLite database for file operations
6161
| | selected.sqlite | | | |
6262
| | | | selected.sqlite | / |
6363

64+
Scenario: Resolve an explicit DB_PATH located outside the web root
65+
Given a WP install
66+
When I run `wp eval '$outside = dirname( getcwd() ) . "/db-outside.sqlite"; copy( defined( "DB_PATH" ) ? DB_PATH : FQDB, $outside ); copy( $outside, getcwd() . "/other.sqlite" );'`
67+
And I run `sqlite3 ../db-outside.sqlite "CREATE TABLE path_marker (value TEXT); INSERT INTO path_marker VALUES ('selected');"`
68+
And I run `sqlite3 other.sqlite "CREATE TABLE path_marker (value TEXT); INSERT INTO path_marker VALUES ('other');"`
69+
70+
Given a paths.php file:
71+
"""
72+
<?php
73+
define( 'DB_PATH', dirname( __DIR__ ) . '/db-outside.sqlite' );
74+
define( 'FQDB', __DIR__ . '/other.sqlite' );
75+
define( 'FQDBDIR', __DIR__ . '/other/' );
76+
define( 'DB_FILE', 'other.sqlite' );
77+
"""
78+
When I run `wp config set path_constants "require __DIR__ . '/paths.php'" --type=variable --raw`
79+
And I try `wp eval 'echo "ready";'`
80+
Then the return code should be 0
81+
82+
When I run `wp eval 'echo md5_file( "other.sqlite" );' --skip-wordpress`
83+
Then save STDOUT as {OTHER_HASH}
84+
85+
When I try `wp db export selected.sql`
86+
Then the return code should be 0
87+
And the selected.sql file should contain:
88+
"""
89+
INSERT INTO path_marker VALUES('selected');
90+
"""
91+
And the selected.sql file should not contain:
92+
"""
93+
INSERT INTO path_marker VALUES('other');
94+
"""
95+
96+
When I try `wp db drop --yes`
97+
Then the return code should be 0
98+
And the ../db-outside.sqlite file should not exist
99+
100+
When I run `wp eval 'echo md5_file( "other.sqlite" );' --skip-wordpress`
101+
Then STDOUT should be:
102+
"""
103+
{OTHER_HASH}
104+
"""
105+
106+
Scenario: Resolve the recorded secret path from db-path.php
107+
Given a WP install
108+
When I run `wp eval 'mkdir( "recorded" ); copy( defined( "DB_PATH" ) ? DB_PATH : FQDB, "recorded/selected.sqlite" ); copy( "recorded/selected.sqlite", ".ht.sqlite" ); file_put_contents( "recorded/db-path.php", "<?php return " . var_export( getcwd() . "/recorded/selected.sqlite", true ) . ";" );'`
109+
And I run `sqlite3 recorded/selected.sqlite "CREATE TABLE path_marker (value TEXT); INSERT INTO path_marker VALUES ('selected');"`
110+
And I run `sqlite3 .ht.sqlite "CREATE TABLE path_marker (value TEXT); INSERT INTO path_marker VALUES ('other');"`
111+
112+
Given a paths.php file:
113+
"""
114+
<?php
115+
define( 'FQDBDIR', __DIR__ . '/recorded/' );
116+
"""
117+
When I run `wp config set path_constants "require __DIR__ . '/paths.php'" --type=variable --raw`
118+
And I try `wp eval 'echo "ready";'`
119+
Then the return code should be 0
120+
121+
When I run `wp eval 'echo md5_file( ".ht.sqlite" );' --skip-wordpress`
122+
Then save STDOUT as {OTHER_HASH}
123+
124+
When I try `wp db export selected.sql`
125+
Then the return code should be 0
126+
And the selected.sql file should contain:
127+
"""
128+
INSERT INTO path_marker VALUES('selected');
129+
"""
130+
And the selected.sql file should not contain:
131+
"""
132+
INSERT INTO path_marker VALUES('other');
133+
"""
134+
135+
When I try `wp db drop --yes`
136+
Then the return code should be 0
137+
And the recorded/selected.sqlite file should not exist
138+
139+
When I run `wp eval 'echo md5_file( ".ht.sqlite" );' --skip-wordpress`
140+
Then STDOUT should be:
141+
"""
142+
{OTHER_HASH}
143+
"""
144+
64145
Scenario Outline: Reject file operations on an in-memory SQLite database
65146
Given a WP install
66147
And an input.sql file:

‎src/DB_Command_SQLite.php‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,28 @@ protected function get_sqlite_db_path() {
8383
$wp_content_dir = defined( 'WP_CONTENT_DIR' ) ? WP_CONTENT_DIR : ABSPATH . 'wp-content';
8484

8585
// Check for FQDBDIR and construct path.
86-
$db_dir = defined( 'FQDBDIR' ) ? FQDBDIR : $wp_content_dir . '/database';
87-
$db_file = defined( 'DB_FILE' ) ? DB_FILE : '.ht.sqlite';
86+
$db_dir = defined( 'FQDBDIR' ) ? FQDBDIR : $wp_content_dir . '/database';
8887

89-
$db_path = rtrim( $db_dir, '/' ) . '/' . ltrim( $db_file, '/' );
88+
// DB_FILE selects a file inside FQDBDIR (sqlite-database-integration 3.1+).
89+
if ( defined( 'DB_FILE' ) ) {
90+
return rtrim( $db_dir, '/' ) . '/' . ltrim( DB_FILE, '/' );
91+
}
92+
93+
// Check for the recorded secret path (sqlite-database-integration 3.1+).
94+
$path_file = rtrim( $db_dir, '/\\' ) . '/db-path.php';
95+
if ( file_exists( $path_file ) ) {
96+
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- Failure is checked below.
97+
$recorded_path = @include $path_file;
98+
if ( is_string( $recorded_path ) && '' !== $recorded_path ) {
99+
if ( ':memory:' === $recorded_path ) {
100+
WP_CLI::error( 'This command does not support in-memory SQLite databases.' );
101+
}
102+
103+
return $recorded_path;
104+
}
105+
}
106+
107+
$db_path = rtrim( $db_dir, '/' ) . '/.ht.sqlite';
90108

91109
// If the file exists, return it.
92110
if ( file_exists( $db_path ) ) {

0 commit comments

Comments
 (0)