Skip to content

Commit b55fb62

Browse files
authored
Support randomized SQLite storage and DB_PATH (#443)
* Fix SQLite driver connection options Use sqlite_pdo and sqlite_journal_mode with the current driver while retaining the legacy PDO option. This keeps the supplied connection instead of opening a separate in-memory database. Update driver stubs and add a regression probe against real integrations. * Support SQLite storage discovery and DB_PATH Detect configured paths, managed storage metadata, and legacy layouts without creating or migrating a database during backend selection. Resolve configured paths before integration defaults are loaded, and initialize managed storage once in the drop-in. Mirror mode uses the resolved path for both the connection and directory protection. Keep primary index reconstruction and stock fallback configuration intact. Cover path precedence, neighboring legacy files, randomized storage, backend overrides, primary indexes, and installation fallbacks with current and older integrations. Related: WordPress/sqlite-database-integration#502 Related: WordPress/sqlite-database-integration#512
1 parent d70dbf4 commit b55fb62

13 files changed

Lines changed: 617 additions & 84 deletions

‎README.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ and the [CLI-first benchmark requirements](tests/bench/README.md#priorities-and-
1717
MDI selects its backend in `wp-content/db.php` before plugins load:
1818

1919
- An explicit `MARKDOWN_DB_BACKEND` configuration always wins.
20-
- Without one, an existing SQLite database (`FQDB`, when it names a file, or
21-
`wp-content/database/.ht.sqlite`) keeps the SQLite backend so an upgrade does
22-
not change an operating site's query engine.
20+
- Without one, an existing SQLite database or managed `db-path.php` file keeps
21+
the SQLite backend so an upgrade does not change an operating site's query
22+
engine. Discovery honors `DB_PATH` before legacy settings and recognizes
23+
legacy `.ht.sqlite` and `.ht.sqlite.php` files. It does not create or migrate
24+
a database.
2325
- Otherwise, MDI selects **`mdi-native`**, where canonical Markdown and JSON
2426
files are the database.
2527

‎db.php‎

Lines changed: 103 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,36 @@ function markdown_database_integration_configured_backend(): string {
8080
if ( defined( 'MARKDOWN_DB_BACKEND' ) ) {
8181
return (string) MARKDOWN_DB_BACKEND;
8282
}
83-
if ( defined( 'FQDB' ) && is_file( FQDB ) ) {
83+
if ( defined( 'DB_PATH' ) || defined( 'FQDB' ) ) {
84+
$path = defined( 'DB_PATH' ) ? DB_PATH : FQDB;
85+
return is_string( $path ) && is_file( $path ) ? 'sqlite' : 'mdi-native';
86+
}
87+
88+
$directory = markdown_database_integration_sqlite_directory();
89+
if ( defined( 'DB_FILE' ) ) {
90+
return is_file( $directory . DB_FILE ) ? 'sqlite' : 'mdi-native';
91+
}
92+
93+
// Handle the randomized secret database path.
94+
if ( is_file( $directory . 'db-path.php' ) ) {
8495
return 'sqlite';
8596
}
86-
return is_file( __DIR__ . '/database/.ht.sqlite' ) ? 'sqlite' : 'mdi-native';
97+
return is_file( $directory . '.ht.sqlite' ) || is_file( $directory . '.ht.sqlite.php' ) ? 'sqlite' : 'mdi-native';
98+
}
99+
}
100+
101+
if ( ! function_exists( 'markdown_database_integration_sqlite_directory' ) ) {
102+
/** Resolve the SQLite storage directory before integration constants load. */
103+
function markdown_database_integration_sqlite_directory(): string {
104+
if ( defined( 'FQDBDIR' ) ) {
105+
$directory = FQDBDIR;
106+
} elseif ( defined( 'DB_DIR' ) ) {
107+
$directory = DB_DIR;
108+
} else {
109+
$directory = defined( 'WP_CONTENT_DIR' ) ? WP_CONTENT_DIR : __DIR__;
110+
$directory .= '/database';
111+
}
112+
return rtrim( $directory, '/\\' ) . '/';
87113
}
88114
}
89115

@@ -326,12 +352,13 @@ function markdown_database_integration_primary_index_path( string $content_dir,
326352
define( 'WP_SQLITE_AST_DRIVER', true );
327353
}
328354

329-
// Primary mode uses markdown-index.sqlite as the active query engine. The
330-
// SQLite Integration install shim opens FQDB directly during wp_install(). Only
331-
// point FQDB at the markdown index when the markdown store already represents an
355+
// Primary mode uses markdown-index.sqlite as the active query engine.
356+
// Only point the SQLite installation shim at the index when the store represents an
332357
// installed site. Partial seed stores fall back to the existing SQLite database
333358
// so already-installed Playground sites do not reset to the install screen.
334-
if ( defined( 'MARKDOWN_DB_MODE' ) && 'primary' === MARKDOWN_DB_MODE ) {
359+
$markdown_db_primary = defined( 'MARKDOWN_DB_MODE' ) && 'primary' === MARKDOWN_DB_MODE;
360+
$markdown_db_install_fallback = false;
361+
if ( $markdown_db_primary ) {
335362
$markdown_db_content_dir = defined( 'MARKDOWN_DB_CONTENT_DIR' )
336363
? MARKDOWN_DB_CONTENT_DIR
337364
: markdown_db_default_content_dir();
@@ -353,6 +380,37 @@ function markdown_database_integration_primary_index_path( string $content_dir,
353380
if ( ! defined( 'FQDB' ) ) {
354381
define( 'FQDB', MARKDOWN_DB_INDEX_PATH );
355382
}
383+
} else {
384+
$markdown_db_install_fallback = true;
385+
if ( ! defined( 'MARKDOWN_DB_INSTALL_FALLBACK' ) ) {
386+
define( 'MARKDOWN_DB_INSTALL_FALLBACK', true );
387+
}
388+
}
389+
}
390+
391+
// Select configured paths before older integrations define FQDB by default.
392+
$markdown_db_sqlite_storage_file = $sqlite_plugin_implementation_folder_path . '/wp-includes/sqlite/class-wp-sqlite-storage.php';
393+
$markdown_db_sqlite_path = null;
394+
if ( defined( 'DB_PATH' ) ) {
395+
$markdown_db_sqlite_path = DB_PATH;
396+
} elseif ( defined( 'FQDB' ) ) {
397+
$markdown_db_sqlite_path = FQDB;
398+
} elseif ( defined( 'DB_FILE' ) ) {
399+
$markdown_db_sqlite_path = markdown_database_integration_sqlite_directory() . DB_FILE;
400+
} elseif ( ! is_file( $markdown_db_sqlite_storage_file ) ) {
401+
$markdown_db_sqlite_path = markdown_database_integration_sqlite_directory() . '.ht.sqlite';
402+
if ( ! is_file( $markdown_db_sqlite_path ) && is_file( $markdown_db_sqlite_path . '.php' ) ) {
403+
$markdown_db_sqlite_path .= '.php';
404+
}
405+
}
406+
407+
// Older integrations read the legacy constants instead of DB_PATH.
408+
if ( null !== $markdown_db_sqlite_path ) {
409+
if ( ! defined( 'FQDB' ) ) {
410+
define( 'FQDB', $markdown_db_sqlite_path );
411+
}
412+
if ( ! defined( 'FQDBDIR' ) && ':memory:' !== $markdown_db_sqlite_path ) {
413+
define( 'FQDBDIR', rtrim( dirname( $markdown_db_sqlite_path ), '/\\' ) . '/' );
356414
}
357415
}
358416

@@ -368,6 +426,11 @@ function markdown_database_integration_primary_index_path( string $content_dir,
368426
// Load the SQLite v2 driver stack (parser, lexer, connection, driver).
369427
require_once $sqlite_plugin_implementation_folder_path . '/wp-includes/database/load.php';
370428

429+
// Load SQLite storage support when available.
430+
if ( is_file( $markdown_db_sqlite_storage_file ) ) {
431+
require_once $markdown_db_sqlite_storage_file;
432+
}
433+
371434
// SQLite Integration renamed the canonical PDO driver after its latest release.
372435
if ( ! class_exists( 'WP_MySQL_On_SQLite', false ) ) {
373436
if ( class_exists( 'WP_PDO_MySQL_On_SQLite', false ) ) {
@@ -386,24 +449,6 @@ class_alias( 'WP_PDO_MySQL_On_SQLite', 'WP_MySQL_On_SQLite' );
386449
require_once $sqlite_plugin_implementation_folder_path . '/wp-includes/sqlite/class-wp-sqlite-db.php';
387450
require_once $sqlite_plugin_implementation_folder_path . '/wp-includes/sqlite/install-functions.php';
388451

389-
if ( defined( 'MARKDOWN_DB_MODE' ) && 'primary' === MARKDOWN_DB_MODE ) {
390-
$markdown_db_content_dir = defined( 'MARKDOWN_DB_CONTENT_DIR' )
391-
? MARKDOWN_DB_CONTENT_DIR
392-
: markdown_db_default_content_dir();
393-
$markdown_db_state_dir = defined( 'MARKDOWN_DB_STATE_DIR' )
394-
? MARKDOWN_DB_STATE_DIR
395-
: $markdown_db_content_dir;
396-
397-
if ( ! markdown_database_integration_store_has_siteurl( $markdown_db_state_dir ) ) {
398-
if ( ! defined( 'MARKDOWN_DB_INSTALL_FALLBACK' ) ) {
399-
define( 'MARKDOWN_DB_INSTALL_FALLBACK', true );
400-
}
401-
$db_name = defined( 'DB_NAME' ) && '' !== DB_NAME ? DB_NAME : 'database_name_here';
402-
$GLOBALS['wpdb'] = new WP_SQLite_DB( $db_name );
403-
return;
404-
}
405-
}
406-
407452
// Load our markdown classes.
408453
$markdown_plugin_dir = null;
409454

@@ -420,32 +465,46 @@ class_alias( 'WP_PDO_MySQL_On_SQLite', 'WP_MySQL_On_SQLite' );
420465
}
421466
}
422467

423-
// If the markdown plugin isn't installed yet, fall back to standard SQLite.
424-
if ( ! $markdown_plugin_dir ) {
425-
// Fallback: standard SQLite behavior.
426-
if ( defined( 'DB_NAME' ) && '' !== DB_NAME ) {
427-
$db_name = DB_NAME;
428-
} else {
429-
$db_name = 'database_name_here';
468+
$markdown_db_sqlite_fallback = $markdown_db_install_fallback || ! $markdown_plugin_dir;
469+
470+
if ( ! $markdown_db_sqlite_fallback ) {
471+
// MDI is storage-only; content-format dependencies belong to the application.
472+
$composer_autoload = $markdown_plugin_dir . '/vendor/autoload.php';
473+
if ( file_exists( $composer_autoload ) ) {
474+
require_once $composer_autoload;
430475
}
431-
$GLOBALS['wpdb'] = new WP_SQLite_DB( $db_name );
432-
return;
476+
477+
require_once $markdown_plugin_dir . '/inc/class-wp-markdown-backend-capabilities.php';
478+
$markdown_db_backend = WP_Markdown_Backend_Resolver::configure_from_globals();
479+
WP_Markdown_Backend_Resolver::require_runtime_capabilities(
480+
$markdown_db_backend,
481+
defined( 'MARKDOWN_DB_MODE' ) ? (string) MARKDOWN_DB_MODE : 'mirror'
482+
);
483+
}
484+
485+
// Primary mode creates its index during reconstruction, after storage is loaded.
486+
if ( class_exists( 'WP_SQLite_Storage' ) && ( ! $markdown_db_primary || $markdown_db_sqlite_fallback ) ) {
487+
$storage = null !== $markdown_db_sqlite_path
488+
? WP_SQLite_Storage::with_explicit_path( $markdown_db_sqlite_path )
489+
: WP_SQLite_Storage::with_secret_path( FQDBDIR );
490+
$markdown_db_sqlite_path = $storage->initialize();
491+
if ( ! defined( 'FQDB' ) ) {
492+
define( 'FQDB', $markdown_db_sqlite_path );
493+
}
494+
}
495+
if ( ! defined( 'DB_PATH' ) ) {
496+
define( 'DB_PATH', $markdown_db_sqlite_path );
433497
}
434498

435-
// Load composer autoloader if present. MDI is storage-only; content-format
436-
// dependencies belong to the application layer above this drop-in.
437-
$composer_autoload = $markdown_plugin_dir . '/vendor/autoload.php';
438-
if ( file_exists( $composer_autoload ) ) {
439-
require_once $composer_autoload;
499+
$db_name = defined( 'DB_NAME' ) && '' !== DB_NAME ? DB_NAME : 'database_name_here';
500+
501+
// Use standard SQLite for installation or when the markdown plugin is missing.
502+
if ( $markdown_db_sqlite_fallback ) {
503+
$GLOBALS['wpdb'] = new WP_SQLite_DB( $db_name );
504+
return;
440505
}
441506

442507
// Load markdown integration classes.
443-
require_once $markdown_plugin_dir . '/inc/class-wp-markdown-backend-capabilities.php';
444-
$markdown_db_backend = WP_Markdown_Backend_Resolver::configure_from_globals();
445-
WP_Markdown_Backend_Resolver::require_runtime_capabilities(
446-
$markdown_db_backend,
447-
defined( 'MARKDOWN_DB_MODE' ) ? (string) MARKDOWN_DB_MODE : 'mirror'
448-
);
449508
require_once $markdown_plugin_dir . '/inc/class-wp-markdown-frontmatter-profiles.php';
450509
require_once $markdown_plugin_dir . '/inc/class-wp-markdown-content-layout-profiles.php';
451510
if ( defined( 'MARKDOWN_DB_CONTENT_LAYOUT_PROFILE_BOOTSTRAP' ) && is_file( MARKDOWN_DB_CONTENT_LAYOUT_PROFILE_BOOTSTRAP ) ) {
@@ -466,12 +525,6 @@ class_alias( 'WP_PDO_MySQL_On_SQLite', 'WP_MySQL_On_SQLite' );
466525
}
467526

468527
// Create the database connection — our WP_Markdown_DB extends WP_SQLite_DB.
469-
if ( defined( 'DB_NAME' ) && '' !== DB_NAME ) {
470-
$db_name = DB_NAME;
471-
} else {
472-
$db_name = 'database_name_here';
473-
}
474-
475528
$GLOBALS['wpdb'] = new WP_Markdown_DB( $db_name );
476529
markdown_database_integration_enable_native_shadow( $GLOBALS['wpdb'], $markdown_plugin_dir );
477530

‎inc/class-wp-markdown-db.php‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ public function db_connect( $allow_bail = true ) {
128128
$pdo = null;
129129
} else {
130130
// Phase 1 / mirror: use on-disk SQLite file.
131-
$db_path = FQDB;
132-
$this->ensure_database_directory_exists();
131+
$db_path = defined( 'DB_PATH' ) ? DB_PATH : FQDB;
132+
$this->ensure_database_directory_exists( $db_path );
133133
}
134134

135135
// Create the markdown storage engine.
@@ -674,8 +674,11 @@ private function ensure_directory_exists( string $dir ): void {
674674
/**
675675
* Ensure the SQLite database directory exists with protections.
676676
*/
677-
private function ensure_database_directory_exists(): void {
678-
$dir = dirname( FQDB );
677+
private function ensure_database_directory_exists( string $db_path ): void {
678+
if ( ':memory:' === $db_path ) {
679+
return;
680+
}
681+
$dir = dirname( $db_path );
679682
$this->ensure_directory_exists( $dir );
680683

681684
// .htaccess protection.

‎inc/sqlite/class-wp-markdown-sqlite-runtime-adapter.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,14 @@ public function __construct(
142142
WP_Markdown_Storage $storage,
143143
?WP_Markdown_Backend_Capabilities $backend_capabilities = null
144144
) {
145+
$pdo_option = defined( 'MARKDOWN_DB_SQLITE_LEGACY_RESULT_API' ) && MARKDOWN_DB_SQLITE_LEGACY_RESULT_API ? 'pdo' : 'sqlite_pdo';
145146
parent::__construct(
146147
sprintf( 'mysql-on-sqlite:dbname=%s', $database ),
147148
null,
148149
null,
149150
array(
150-
'pdo' => $connection->get_pdo(),
151-
'journal_mode' => $connection->get_pdo()->query( 'PRAGMA journal_mode' )->fetchColumn(),
151+
$pdo_option => $connection->get_pdo(),
152+
'sqlite_journal_mode' => $connection->get_pdo()->query( 'PRAGMA journal_mode' )->fetchColumn(),
152153
)
153154
);
154155
$connection->get_pdo()->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, true );

0 commit comments

Comments
 (0)