Skip to content

Commit 2d32a5b

Browse files
committed
Support DB_PATH as the primary database setting
Use DB_PATH for storage initialization, database connections, installation, and Site Health. Place storage locks beside the configured database. Reject DB_PATH combined with DB_DIR, DB_FILE, FQDBDIR, or FQDB. Keep legacy settings when DB_PATH is absent and deprecate the older constants. Validate explicit database paths in WP_SQLite_Storage before any file operations. Require an absolute filesystem path or :memory:. Cover mixed settings, managed defaults, legacy settings, in-memory databases, and invalid path values. #502
1 parent a8468b5 commit 2d32a5b

7 files changed

Lines changed: 565 additions & 95 deletions

File tree

‎packages/plugin-sqlite-database-integration/constants.php‎

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,49 @@
1313
}
1414

1515
/**
16-
* Notice:
17-
* Your scripts have the permission to create directories or files on your server.
18-
* If you write in your wp-config.php like below, we take these definitions.
19-
* define('DB_DIR', '/full_path_to_the_database_directory/');
20-
* define('DB_FILE', 'database_file_name');
16+
* DB_PATH is the absolute database file path, or ":memory:" for an in-memory database.
17+
*
18+
* Cannot be combined with explicit DB_DIR, DB_FILE, FQDBDIR, or FQDB definitions.
19+
* The database directory is also used for storage locks and must be writable by PHP.
20+
* When not configured, the drop-in defines DB_PATH after initializing the storage.
21+
*
22+
* Example: define( 'DB_PATH', '/private/wordpress/database.sqlite' );
23+
*/
24+
if ( 'sqlite' === DB_ENGINE && defined( 'DB_PATH' ) ) {
25+
if ( defined( 'DB_DIR' ) || defined( 'DB_FILE' ) || defined( 'FQDBDIR' ) || defined( 'FQDB' ) ) {
26+
throw new RuntimeException( 'DB_PATH cannot be combined with DB_DIR, DB_FILE, FQDBDIR, or FQDB. Remove the legacy definitions.' );
27+
}
28+
29+
if ( ! is_string( DB_PATH ) ) {
30+
throw new RuntimeException( 'DB_PATH must be a string.' );
31+
}
32+
}
33+
34+
/**
35+
* DB_DIR selects the database directory when DB_PATH is not configured.
36+
* Must not be configured together with DB_PATH.
37+
*
38+
* @deprecated 3.1.0 Define DB_PATH instead.
39+
*/
40+
41+
/**
42+
* DB_FILE selects a filename inside DB_DIR or FQDBDIR.
43+
* Must not be configured together with DB_PATH.
44+
*
45+
* @deprecated 3.1.0 Define DB_PATH instead.
2146
*/
2247

2348
/**
2449
* FQDBDIR is a directory where the sqlite database file is placed.
25-
* If DB_DIR is defined, it is used as FQDBDIR.
50+
* Defaults to the directory containing DB_PATH, or the legacy directory setting.
51+
* Must not be configured together with DB_PATH.
2652
*
27-
* @deprecated 3.0.0 Define DB_DIR instead of overriding FQDBDIR.
53+
* @deprecated 3.0.0 Define DB_PATH instead of overriding FQDBDIR.
2854
*/
2955
if ( ! defined( 'FQDBDIR' ) ) {
30-
if ( defined( 'DB_DIR' ) ) {
56+
if ( defined( 'DB_PATH' ) && is_string( DB_PATH ) ) {
57+
define( 'FQDBDIR', rtrim( dirname( DB_PATH ), '/\\' ) . '/' );
58+
} elseif ( defined( 'DB_DIR' ) ) {
3159
define( 'FQDBDIR', rtrim( DB_DIR, '/\\' ) . '/' );
3260
} elseif ( defined( 'WP_CONTENT_DIR' ) ) {
3361
define( 'FQDBDIR', WP_CONTENT_DIR . '/database/' );
@@ -39,12 +67,16 @@
3967
/**
4068
* FQDB is the absolute path to the SQLite database file.
4169
*
42-
* If DB_FILE is defined, FQDB is defined here using FQDBDIR. When SQLite is
43-
* used without DB_FILE, managed storage defines FQDB after resolving the
44-
* randomized database path.
70+
* Defaults to DB_PATH, or FQDBDIR combined with DB_FILE. For managed storage,
71+
* the drop-in defines FQDB after resolving the randomized database path.
72+
* Must not be configured together with DB_PATH.
4573
*
46-
* @deprecated 3.0.0 Define DB_DIR and DB_FILE instead of overriding FQDB.
74+
* @deprecated 3.0.0 Define DB_PATH instead of overriding FQDB.
4775
*/
48-
if ( ! defined( 'FQDB' ) && defined( 'DB_FILE' ) ) {
49-
define( 'FQDB', FQDBDIR . DB_FILE );
76+
if ( ! defined( 'FQDB' ) ) {
77+
if ( defined( 'DB_PATH' ) ) {
78+
define( 'FQDB', DB_PATH );
79+
} elseif ( defined( 'DB_FILE' ) ) {
80+
define( 'FQDB', FQDBDIR . DB_FILE );
81+
}
5082
}

‎packages/plugin-sqlite-database-integration/health-check.php‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,22 @@ function sqlite_plugin_filter_debug_data( $info ) {
2626
);
2727

2828
if ( 'sqlite' === $db_engine ) {
29+
$database_path = defined( 'DB_PATH' ) ? DB_PATH : FQDB;
30+
2931
$info['wp-database']['fields']['database_version'] = array(
3032
'label' => __( 'SQLite version', 'sqlite-database-integration' ),
3133
'value' => $info['wp-database']['fields']['server_version'] ?? null,
3234
);
3335

3436
$info['wp-database']['fields']['database_file'] = array(
3537
'label' => __( 'Database file', 'sqlite-database-integration' ),
36-
'value' => FQDB,
38+
'value' => $database_path,
3739
'private' => true,
3840
);
3941

4042
$info['wp-database']['fields']['database_size'] = array(
4143
'label' => __( 'Database size', 'sqlite-database-integration' ),
42-
'value' => size_format( filesize( FQDB ) ),
44+
'value' => size_format( filesize( $database_path ) ),
4345
);
4446

4547
unset( $info['wp-database']['fields']['extension'] );

‎packages/plugin-sqlite-database-integration/wp-includes/sqlite/class-wp-sqlite-db.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ public function db_connect( $allow_bail = true ) {
447447
$dbh = new WP_MySQL_On_SQLite(
448448
sprintf(
449449
'mysql-on-sqlite:path=%s;dbname=%s',
450-
str_replace( ';', ';;', FQDB ),
450+
str_replace( ';', ';;', DB_PATH ),
451451
str_replace( ';', ';;', $this->dbname )
452452
),
453453
null,

‎packages/plugin-sqlite-database-integration/wp-includes/sqlite/class-wp-sqlite-storage.php‎

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class WP_SQLite_Storage {
4949
private const TOKEN_BYTE_LENGTH = 16;
5050

5151
/**
52-
* Managed database root directory.
52+
* Directory with the storage lock files and, for managed storage, the database.
5353
*
5454
* @var string
5555
*/
@@ -107,19 +107,19 @@ class WP_SQLite_Storage {
107107
/**
108108
* Create a SQLite storage manager.
109109
*
110-
* @param string|null $database_root Managed database root. Defaults to FQDBDIR.
111-
* @param string|null $database_path Explicit database path or ":memory:". Managed storage is used when null.
110+
* @param string|null $database_path Absolute database path or ":memory:". Managed storage in FQDBDIR is used when null.
112111
* @throws RuntimeException When the database path is invalid.
113112
*/
114-
public function __construct( ?string $database_root = null, ?string $database_path = null ) {
115-
if ( '' === $database_path ) {
116-
throw new RuntimeException( 'The SQLite database path is invalid.' );
113+
public function __construct( ?string $database_path = null ) {
114+
if ( null !== $database_path && ! $this->is_valid_database_path( $database_path ) ) {
115+
throw new RuntimeException( 'The SQLite database path must be an absolute filesystem path or ":memory:".' );
116+
}
117+
$this->database_path = $database_path;
118+
if ( null === $database_path || ':memory:' === $database_path ) {
119+
$this->set_database_root( FQDBDIR );
120+
} else {
121+
$this->set_database_root( dirname( $database_path ) );
117122
}
118-
$this->database_root = rtrim( $database_root ?? FQDBDIR, '/\\' ) . '/';
119-
$this->database_path = $database_path;
120-
$this->database_path_file = $this->database_root . self::DATABASE_PATH_FILENAME;
121-
$this->lock_path = $this->database_root . self::LOCK_FILENAME;
122-
$this->maintenance_path = $this->database_root . self::MAINTENANCE_FILENAME;
123123
}
124124

125125
/**
@@ -282,6 +282,44 @@ public function unlock(): void {
282282
}
283283
}
284284

285+
/**
286+
* Validate an explicit database path.
287+
*
288+
* @param string $database_path Absolute database path or ":memory:".
289+
* @return bool Whether the database path is valid.
290+
*/
291+
private function is_valid_database_path( string $database_path ): bool {
292+
if ( ':memory:' === $database_path ) {
293+
return true;
294+
}
295+
if ( '' === $database_path || false !== strpos( $database_path, "\0" ) ) {
296+
return false;
297+
}
298+
299+
// Ensure the path is absolute.
300+
if ( '/' === DIRECTORY_SEPARATOR ) {
301+
return '/' === $database_path[0];
302+
} else {
303+
// Match Windows absolute path with a drive letter or UNC share.
304+
return 1 === preg_match(
305+
'~^(?:[a-zA-Z]:/|//[^/]+/[^/]+/)~',
306+
str_replace( '\\', '/', $database_path )
307+
);
308+
}
309+
}
310+
311+
/**
312+
* Set the storage root directory and the paths derived from it.
313+
*
314+
* @param string $database_root Storage root directory.
315+
*/
316+
private function set_database_root( string $database_root ): void {
317+
$this->database_root = rtrim( $database_root, '/\\' ) . '/';
318+
$this->database_path_file = $this->database_root . self::DATABASE_PATH_FILENAME;
319+
$this->lock_path = $this->database_root . self::LOCK_FILENAME;
320+
$this->maintenance_path = $this->database_root . self::MAINTENANCE_FILENAME;
321+
}
322+
285323
/**
286324
* Wait until no maintenance process holds an active storage lock.
287325
*/

‎packages/plugin-sqlite-database-integration/wp-includes/sqlite/db.php‎

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,53 @@
88
*/
99
require_once __DIR__ . '/../database/version.php';
1010

11-
// Require the constants file.
12-
require_once __DIR__ . '/../../constants.php';
11+
try {
12+
// Require the constants file.
13+
require_once __DIR__ . '/../../constants.php';
1314

14-
// Bail early if DB_ENGINE is not defined as sqlite.
15-
if ( ! defined( 'DB_ENGINE' ) || 'sqlite' !== DB_ENGINE ) {
16-
return;
17-
}
15+
// Bail early if DB_ENGINE is not defined as sqlite.
16+
if ( ! defined( 'DB_ENGINE' ) || 'sqlite' !== DB_ENGINE ) {
17+
return;
18+
}
1819

19-
if ( ! extension_loaded( 'pdo' ) ) {
20-
wp_die(
21-
new WP_Error(
22-
'pdo_not_loaded',
23-
sprintf(
24-
'<h1>%1$s</h1><p>%2$s</p>',
25-
'PHP PDO Extension is not loaded',
26-
'Your PHP installation appears to be missing the PDO extension which is required for this version of WordPress and the type of database you have specified.'
27-
)
28-
),
29-
'PHP PDO Extension is not loaded.'
30-
);
31-
}
20+
if ( ! extension_loaded( 'pdo' ) ) {
21+
wp_die(
22+
new WP_Error(
23+
'pdo_not_loaded',
24+
sprintf(
25+
'<h1>%1$s</h1><p>%2$s</p>',
26+
'PHP PDO Extension is not loaded',
27+
'Your PHP installation appears to be missing the PDO extension which is required for this version of WordPress and the type of database you have specified.'
28+
)
29+
),
30+
'PHP PDO Extension is not loaded.'
31+
);
32+
}
3233

33-
if ( ! extension_loaded( 'pdo_sqlite' ) ) {
34-
wp_die(
35-
new WP_Error(
36-
'pdo_driver_not_loaded',
37-
sprintf(
38-
'<h1>%1$s</h1><p>%2$s</p>',
39-
'PDO Driver for SQLite is missing',
40-
'Your PHP installation appears not to have the right PDO drivers loaded. These are required for this version of WordPress and the type of database you have specified.'
41-
)
42-
),
43-
'PDO Driver for SQLite is missing.'
44-
);
45-
}
34+
if ( ! extension_loaded( 'pdo_sqlite' ) ) {
35+
wp_die(
36+
new WP_Error(
37+
'pdo_driver_not_loaded',
38+
sprintf(
39+
'<h1>%1$s</h1><p>%2$s</p>',
40+
'PDO Driver for SQLite is missing',
41+
'Your PHP installation appears not to have the right PDO drivers loaded. These are required for this version of WordPress and the type of database you have specified.'
42+
)
43+
),
44+
'PDO Driver for SQLite is missing.'
45+
);
46+
}
4647

47-
require_once __DIR__ . '/class-wp-sqlite-storage.php';
48+
require_once __DIR__ . '/class-wp-sqlite-storage.php';
4849

49-
try {
50-
$database_storage = new WP_SQLite_Storage( FQDBDIR, defined( 'FQDB' ) ? FQDB : null );
51-
$database_path = $database_storage->initialize();
50+
if ( defined( 'DB_PATH' ) ) {
51+
$database_storage = new WP_SQLite_Storage( DB_PATH );
52+
} elseif ( defined( 'FQDB' ) ) {
53+
$database_storage = new WP_SQLite_Storage( FQDB );
54+
} else {
55+
$database_storage = new WP_SQLite_Storage();
56+
}
57+
$database_path = $database_storage->initialize();
5258
} catch ( Throwable $exception ) {
5359
error_log( 'SQLite database error: ' . (string) $exception );
5460

@@ -62,6 +68,10 @@
6268
wp_die( htmlspecialchars( $exception->getMessage(), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8' ), 'SQLite database error', array( 'response' => 503 ) );
6369
}
6470

71+
if ( ! defined( 'DB_PATH' ) ) {
72+
define( 'DB_PATH', $database_path );
73+
}
74+
6575
if ( ! defined( 'FQDB' ) ) {
6676
define( 'FQDB', $database_path );
6777
}

‎packages/plugin-sqlite-database-integration/wp-includes/sqlite/install-functions.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function sqlite_make_db_sqlite() {
2323
$translator = new WP_MySQL_On_SQLite(
2424
sprintf(
2525
'mysql-on-sqlite:path=%s;dbname=%s',
26-
str_replace( ';', ';;', FQDB ),
26+
str_replace( ';', ';;', DB_PATH ),
2727
str_replace( ';', ';;', $wpdb->dbname )
2828
),
2929
null,

0 commit comments

Comments
 (0)